View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.gui.control;
35  
36  import info.magnolia.cms.gui.misc.CssConstants;
37  
38  import java.util.Hashtable;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang.StringUtils;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  
48  /**
49   * @author Vinzenz Wyser
50   * @version 2.0
51   */
52  public class TreeColumn extends ControlImpl {
53  
54      public static final String EDIT_CSSCLASS_EDIT = CssConstants.CSSCLASS_EDIT;
55  
56      public static final String EDIT_CSSCLASS_SELECT = CssConstants.CSSCLASS_SELECT;
57  
58      public static final String EDIT_NAMEADDITION = "_EditNodeData"; //$NON-NLS-1$
59  
60      public static final String EDIT_JSSAVE = ".saveNodeData(this.value);"; //$NON-NLS-1$
61  
62      public static final String EMPTY = "-"; //$NON-NLS-1$
63  
64      /**
65       * Logger.
66       */
67      private static Logger log = LoggerFactory.getLogger(TreeColumn.class);
68  
69      boolean isMeta;
70  
71      boolean isLabel;
72  
73      boolean isIcons;
74  
75      boolean iconsActivation;
76  
77      boolean iconsPermission;
78  
79      boolean isNodeDataValue;
80  
81      boolean isNodeDataType;
82  
83      String dateFormat;
84  
85      String title = StringUtils.EMPTY;
86  
87      boolean permissionWrite;
88  
89      String htmlEdit = StringUtils.EMPTY;
90  
91      TreeColumnHtmlRenderer htmlRenderer;
92  
93      private String javascriptTree = StringUtils.EMPTY;
94  
95      private int width = 1;
96  
97      // if EMPTY changes, it has to be updated also in tree.js; note: if you use html tags, use upper-case letters (and
98      // test it well ;)
99      private Map keyValue = new Hashtable();
100 
101     public TreeColumn() {
102         this.setCssClass("mgnlTreeText"); //$NON-NLS-1$
103         // default delegate
104         this.setHtmlRenderer(new TreeColumnHtmlRendererImpl());
105 
106     }
107 
108     /**
109      * Constructor.
110      * @param javascriptTree name of the js variable
111      * @param request http request, needed for context path
112      * @deprecated use the empty constuctor
113      */
114     public TreeColumn(String javascriptTree, HttpServletRequest request) {
115         this();
116         this.setJavascriptTree(javascriptTree);
117         this.setRequest(request);
118     }
119 
120     public void setIsMeta(boolean b) {
121         this.isMeta = b;
122     }
123 
124     public boolean getIsMeta() {
125         return this.isMeta;
126     }
127 
128     public void setIsIcons(boolean b) {
129         this.isIcons = b;
130     }
131 
132     public boolean getIsIcons() {
133         return this.isIcons;
134     }
135 
136     public void setIconsActivation(boolean b) {
137         this.iconsActivation = b;
138     }
139 
140     public boolean getIconsActivation() {
141         return this.iconsActivation;
142     }
143 
144     public void setIconsPermission(boolean b) {
145         this.iconsPermission = b;
146     }
147 
148     public boolean getIconsPermission() {
149         return this.iconsPermission;
150     }
151 
152     public void setIsLabel(boolean b) {
153         this.isLabel = b;
154     }
155 
156     public boolean getIsLabel() {
157         return this.isLabel;
158     }
159 
160     public void setIsNodeDataValue(boolean b) {
161         this.isNodeDataValue = b;
162     }
163 
164     public boolean getIsNodeDataValue() {
165         return this.isNodeDataValue;
166     }
167 
168     public void setIsNodeDataType(boolean b) {
169         this.isNodeDataType = b;
170     }
171 
172     public boolean getIsNodeDataType() {
173         return this.isNodeDataType;
174     }
175 
176     public void setDateFormat(String s) {
177         this.dateFormat = s;
178     }
179 
180     public String getDateFormat() {
181         return this.dateFormat;
182     }
183 
184     public void setTitle(String s) {
185         this.title = s;
186     }
187 
188     public String getTitle() {
189         return this.title;
190     }
191 
192     /**
193      * <p>
194      * set the name relative width of the column; default is 1
195      * </p>
196      */
197     public void setWidth(int i) {
198         this.width = i;
199     }
200 
201     public int getWidth() {
202         return this.width;
203     }
204 
205     public void setPermissionWrite(boolean b) {
206         this.permissionWrite = b;
207     }
208 
209     public boolean getPermissionWrite() {
210         return this.permissionWrite;
211     }
212 
213     /**
214      * <p>
215      * set the name of the javascript tree object
216      * </p>
217      * @param variableName
218      */
219     public void setJavascriptTree(String variableName) {
220         this.javascriptTree = variableName;
221     }
222 
223     public String getJavascriptTree() {
224         return this.javascriptTree;
225     }
226 
227     public void setKeyValue(Map h) {
228         this.keyValue = h;
229     }
230 
231     public Map getKeyValue() {
232         return this.keyValue;
233     }
234 
235     public String getKeyValue(String key) {
236         return (String) this.getKeyValue().get(key);
237     }
238 
239     public void addKeyValue(String key, String value) {
240         this.getKeyValue().put(key, value);
241     }
242 
243     public void setHtmlEdit(String s) {
244         this.htmlEdit = s;
245     }
246 
247     public String getHtmlEdit() {
248         return this.htmlEdit;
249     }
250 
251     public void setHtmlEdit() {
252         Edit edit = new Edit();
253         edit.setName(this.getJavascriptTree() + EDIT_NAMEADDITION);
254         edit.setSaveInfo(false);
255         edit.setCssClass(EDIT_CSSCLASS_EDIT);
256         edit.setEvent("onkeydown", this.getJavascriptTree() + ".editNodeDataKeydown(event,this);"); //$NON-NLS-1$ //$NON-NLS-2$
257         edit.setEvent("onblur", this.getJavascriptTree() + EDIT_JSSAVE); //$NON-NLS-1$
258         // edit.setCssStyles("width","100%");
259         this.setHtmlEdit(edit.getHtml());
260     }
261 
262     @Override
263     public String getHtml() {
264         String html = null;
265         try {
266             html = getHtmlRenderer().renderHtml(this, this.getWebsiteNode());
267         }
268         catch (Exception e) {
269             log.error(e.getMessage(), e);
270         }
271         // "" not clickable!
272         if (StringUtils.isEmpty(html)) {
273             html = TreeColumn.EMPTY;
274         }
275         return html;
276     }
277 
278     /**
279      * @return Returns the htmlRenderer.
280      */
281     public TreeColumnHtmlRenderer getHtmlRenderer() {
282         return htmlRenderer;
283     }
284 
285     /**
286      * @param htmlRenderer Set the delecate Object which will render the html column for each row
287      */
288     public void setHtmlRenderer(TreeColumnHtmlRenderer htmlRenderer) {
289         this.htmlRenderer = htmlRenderer;
290     }
291 
292     public static TreeColumn createColumn(Tree tree, String title) {
293         TreeColumn treeColumn = new TreeColumn();
294         // this is done by tree.addColum() !
295         treeColumn.setJavascriptTree(tree.getJavascriptTree());
296         treeColumn.setTitle(title);
297         return treeColumn;
298     }
299 
300     public static TreeColumn createColumn(Tree tree, String title, TreeColumnHtmlRenderer htmlRenderer) {
301         TreeColumn treeColumn = createColumn(tree, title);
302         if (htmlRenderer != null) {
303             treeColumn.setHtmlRenderer(htmlRenderer);
304         }
305         return treeColumn;
306     }
307 
308     public static TreeColumn createLabelColumn(Tree tree, String title, boolean editable) {
309         TreeColumn treeColumn = createColumn(tree, title);
310         treeColumn.setIsLabel(true);
311         if (editable) {
312             treeColumn.setHtmlEdit();
313         }
314         return treeColumn;
315     }
316 
317     public static TreeColumn createNodeDataColumn(Tree tree, String title, String nodeDataName, boolean editable) {
318         TreeColumn treeColumn = createColumn(tree, title);
319         treeColumn.setName(nodeDataName);
320         if (editable) {
321             treeColumn.setHtmlEdit();
322         }
323         return treeColumn;
324     }
325 
326     public static TreeColumn createNodeDataColumn(Tree tree, String title, String nodeDataName, String dateFormat) {
327         TreeColumn treeColumn = createColumn(tree, title);
328         treeColumn.setName(nodeDataName);
329         treeColumn.setDateFormat(dateFormat);
330         return treeColumn;
331     }
332 
333     public static TreeColumn createNodeDataColumn(Tree tree, String title, String nodeDataName, TreeColumnHtmlRenderer renderer) {
334         TreeColumn treeColumn = createColumn(tree, title);
335         treeColumn.setName(nodeDataName);
336         treeColumn.setHtmlRenderer(renderer);
337         return treeColumn;
338     }
339 
340     public static TreeColumn createMetaDataColumn(Tree tree, String title, String name, String dateFormat) {
341         TreeColumn treeColumn = createColumn(tree, title);
342         treeColumn.setIsMeta(true);
343         treeColumn.setName(name);
344         if (dateFormat != null) {
345             treeColumn.setDateFormat(dateFormat);
346         }
347         return treeColumn;
348     }
349 
350     public static TreeColumn createIconColumn(Tree tree, String title, TreeColumnHtmlRenderer htmlRenderer) {
351         TreeColumn treeColumn = createColumn(tree, title, htmlRenderer);
352         treeColumn.setIsIcons(true);
353         return treeColumn;
354     }
355 
356     public static TreeColumn createActivationColumn(Tree tree, String title) {
357         TreeColumn columnActivation = TreeColumn.createIconColumn(tree, "", null);
358         columnActivation.setIconsActivation(true);
359         columnActivation.setCssClass(StringUtils.EMPTY);
360         columnActivation.setTitle(title);
361         columnActivation.setWidth(1);
362         return columnActivation;
363     }
364 
365 }