001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.components.categoryselect;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.relations.CmsCategory;
033import org.opencms.relations.CmsCategoryService;
034import org.opencms.ui.A_CmsUI;
035import org.opencms.ui.FontOpenCms;
036import org.opencms.ui.components.CmsBasicDialog;
037import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
038import org.opencms.ui.components.OpenCmsTheme;
039import org.opencms.ui.components.fileselect.I_CmsSelectionHandler;
040import org.opencms.util.CmsStringUtil;
041
042import java.util.ArrayList;
043import java.util.Collection;
044import java.util.List;
045
046import com.vaadin.ui.Button;
047import com.vaadin.ui.Button.ClickEvent;
048import com.vaadin.ui.Button.ClickListener;
049import com.vaadin.ui.Component;
050import com.vaadin.v7.ui.CustomField;
051import com.vaadin.v7.ui.HorizontalLayout;
052import com.vaadin.ui.Window;
053
054/**
055 * The category select field.<p>
056 */
057public class CmsCategorySelectField extends CustomField<String>
058implements I_CmsSelectionHandler<Collection<CmsCategory>> {
059
060    /** The serial version id. */
061    private static final long serialVersionUID = -3080639027333425153L;
062
063    /** The select dialog. */
064    private CmsCategorySelectDialog m_dialog;
065
066    /** The select dialog window. */
067    private Window m_dialogWindow;
068
069    /** The tree to display the selected categories. */
070    private CmsCategoryTree m_tree;
071
072    /**
073     * @see com.vaadin.ui.AbstractField#getType()
074     */
075    @Override
076    public Class<? extends String> getType() {
077
078        return String.class;
079    }
080
081    /**
082     * @see org.opencms.ui.components.fileselect.I_CmsSelectionHandler#onSelection(java.lang.Object)
083     */
084    public void onSelection(Collection<CmsCategory> selected) {
085
086        setValue(getStringValue(selected));
087        m_dialogWindow.close();
088    }
089
090    /**
091     * @see com.vaadin.ui.AbstractField#getInternalValue()
092     */
093    @Override
094    protected String getInternalValue() {
095
096        if (m_tree == null) {
097            String result = super.getInternalValue();
098            return result != null ? result : "";
099        } else {
100            CmsObject cms = A_CmsUI.getCmsObject();
101            String result = "";
102            for (CmsCategory cat : m_tree.getSelectedCategories()) {
103                result += cms.getRequestContext().removeSiteRoot(cat.getRootPath()) + ",";
104            }
105            if (result.length() > 0) {
106                result = result.substring(0, result.length() - 1);
107            }
108            return getStringValue(m_tree.getSelectedCategories());
109        }
110    }
111
112    /**
113     * @see com.vaadin.ui.CustomField#initContent()
114     */
115    @Override
116    protected Component initContent() {
117
118        HorizontalLayout main = new HorizontalLayout();
119        main.setWidth("100%");
120        main.setSpacing(true);
121
122        m_tree = new CmsCategoryTree();
123        m_tree.setWidth("100%");
124        m_tree.setHeight("34px");
125        m_tree.setDisplayOnly(true);
126        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(super.getInternalValue())) {
127            setInternalValue(super.getInternalValue());
128        }
129        main.addComponent(m_tree);
130        main.setExpandRatio(m_tree, 2);
131        Button open = new Button("");
132        open.addStyleName(OpenCmsTheme.BUTTON_ICON);
133        open.setIcon(FontOpenCms.GALLERY);
134
135        open.addClickListener(new ClickListener() {
136
137            private static final long serialVersionUID = 1L;
138
139            public void buttonClick(ClickEvent event) {
140
141                openWindow();
142            }
143        });
144        main.addComponent(open);
145        return main;
146    }
147
148    /**
149     * @see com.vaadin.ui.AbstractField#setInternalValue(java.lang.Object)
150     */
151    @Override
152    protected void setInternalValue(String newValue) {
153
154        if (m_tree != null) {
155            List<CmsCategory> categories = new ArrayList<CmsCategory>();
156            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(newValue)) {
157                CmsObject cms = A_CmsUI.getCmsObject();
158                CmsCategoryService catService = CmsCategoryService.getInstance();
159                for (String path : newValue.split(",")) {
160                    try {
161                        CmsCategory cat = catService.getCategory(cms, path);
162                        categories.add(cat);
163                    } catch (CmsException e) {
164                        // TODO Auto-generated catch block
165                        e.printStackTrace();
166                    }
167                }
168            }
169            m_tree.setCategories(categories);
170            int height = (categories.size() * 33) + 1;
171            if (height > 200) {
172                height = 200;
173            }
174            m_tree.setHeight(height + "px");
175        }
176        super.setInternalValue(newValue);
177    }
178
179    /**
180     * Opens the select window.<p>
181     */
182    void openWindow() {
183
184        if (m_dialogWindow == null) {
185            m_dialogWindow = CmsBasicDialog.prepareWindow(DialogWidth.wide);
186            m_dialogWindow.setCaption("Select categories");
187        }
188        if (m_dialog == null) {
189            m_dialog = new CmsCategorySelectDialog("/");
190            m_dialogWindow.setContent(m_dialog);
191            m_dialog.addSelectionHandler(this);
192        }
193        A_CmsUI.get().addWindow(m_dialogWindow);
194        m_dialogWindow.center();
195        m_dialog.setSelectedCategories(m_tree.getSelectedCategories());
196    }
197
198    /**
199     * Returns the string representation of the selected categories.<p>
200     *
201     * @param categories the selected categories
202     *
203     * @return the string representation of the selected categories
204     */
205    private String getStringValue(Collection<CmsCategory> categories) {
206
207        CmsObject cms = A_CmsUI.getCmsObject();
208        String result = "";
209        for (CmsCategory cat : categories) {
210            result += cms.getRequestContext().removeSiteRoot(cat.getRootPath()) + ",";
211        }
212        if (result.length() > 0) {
213            result = result.substring(0, result.length() - 1);
214        }
215        return result;
216    }
217
218}