View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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     public String getHtml() {
263         String html = null;
264         try {
265             html = getHtmlRenderer().renderHtml(this, this.getWebsiteNode());
266         }
267         catch (Exception e) {
268             log.error(e.getMessage(), e);
269         }
270         // "" not clickable!
271         if (StringUtils.isEmpty(html)) {
272             html = TreeColumn.EMPTY;
273         }
274         return html;
275     }
276 
277     /**
278      * @return Returns the htmlRenderer.
279      */
280     public TreeColumnHtmlRenderer getHtmlRenderer() {
281         return htmlRenderer;
282     }
283 
284     /**
285      * @param htmlRenderer Set the delecate Object which will render the html column for each row
286      */
287     public void setHtmlRenderer(TreeColumnHtmlRenderer htmlRenderer) {
288         this.htmlRenderer = htmlRenderer;
289     }
290 
291     public static TreeColumn createColumn(Tree tree, String title) {
292         TreeColumn treeColumn = new TreeColumn();
293         // this is done by tree.addColum() !
294         treeColumn.setJavascriptTree(tree.getJavascriptTree());
295         treeColumn.setTitle(title);
296         return treeColumn;
297     }
298 
299     public static TreeColumn createColumn(Tree tree, String title, TreeColumnHtmlRenderer htmlRenderer) {
300         TreeColumn treeColumn = createColumn(tree, title);
301         if (htmlRenderer != null) {
302             treeColumn.setHtmlRenderer(htmlRenderer);
303         }
304         return treeColumn;
305     }
306 
307     public static TreeColumn createLabelColumn(Tree tree, String title, boolean editable) {
308         TreeColumn treeColumn = createColumn(tree, title);
309         treeColumn.setIsLabel(true);
310         if (editable) {
311             treeColumn.setHtmlEdit();
312         }
313         return treeColumn;
314     }
315 
316     public static TreeColumn createNodeDataColumn(Tree tree, String title, String nodeDataName, boolean editable) {
317         TreeColumn treeColumn = createColumn(tree, title);
318         treeColumn.setName(nodeDataName);
319         if (editable) {
320             treeColumn.setHtmlEdit();
321         }
322         return treeColumn;
323     }
324 
325     public static TreeColumn createNodeDataColumn(Tree tree, String title, String nodeDataName, String dateFormat) {
326         TreeColumn treeColumn = createColumn(tree, title);
327         treeColumn.setName(nodeDataName);
328         treeColumn.setDateFormat(dateFormat);
329         return treeColumn;
330     }
331     
332     public static TreeColumn createNodeDataColumn(Tree tree, String title, String nodeDataName, TreeColumnHtmlRenderer renderer) {
333         TreeColumn treeColumn = createColumn(tree, title);
334         treeColumn.setName(nodeDataName);
335         treeColumn.setHtmlRenderer(renderer);
336         return treeColumn;
337     }
338 
339     public static TreeColumn createMetaDataColumn(Tree tree, String title, String name, String dateFormat) {
340         TreeColumn treeColumn = createColumn(tree, title);
341         treeColumn.setIsMeta(true);
342         treeColumn.setName(name);
343         if (dateFormat != null) {
344             treeColumn.setDateFormat(dateFormat);
345         }
346         return treeColumn;
347     }
348 
349     public static TreeColumn createIconColumn(Tree tree, String title, TreeColumnHtmlRenderer htmlRenderer) {
350         TreeColumn treeColumn = createColumn(tree, title, htmlRenderer);
351         treeColumn.setIsIcons(true);
352         return treeColumn;
353     }
354 
355     public static TreeColumn createActivationColumn(Tree tree, String title) {
356         TreeColumn columnActivation = TreeColumn.createIconColumn(tree, "", null);
357         columnActivation.setIconsActivation(true);
358         columnActivation.setCssClass(StringUtils.EMPTY);
359         columnActivation.setTitle(title);
360         columnActivation.setWidth(1);
361         return columnActivation;
362     }
363 
364 }