KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > list > CmsListColumnDefinition


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/list/CmsListColumnDefinition.java,v $
3  * Date : $Date: 2006/03/27 14:52:28 $
4  * Version: $Revision: 1.25 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.list;
33
34 import org.opencms.i18n.CmsMessageContainer;
35 import org.opencms.i18n.CmsMessages;
36 import org.opencms.main.CmsIllegalArgumentException;
37 import org.opencms.util.CmsStringUtil;
38 import org.opencms.workplace.CmsWorkplace;
39 import org.opencms.workplace.tools.A_CmsHtmlIconButton;
40 import org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum;
41
42 import java.text.MessageFormat JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Collections JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.Locale JavaDoc;
48
49 /**
50  * Html list column definition.<p>
51  *
52  * @author Michael Moossen
53  *
54  * @version $Revision: 1.25 $
55  *
56  * @since 6.0.0
57  */

58 public class CmsListColumnDefinition {
59
60     /** Standard list button location. */
61     public static final String JavaDoc ICON_DOWN = "list/arrow_down.png";
62
63     /** Standard list button location. */
64     public static final String JavaDoc ICON_UP = "list/arrow_up.png";
65
66     /** Column alignment. */
67     private CmsListColumnAlignEnum m_align = CmsListColumnAlignEnum.ALIGN_LEFT;
68
69     /** Comparator for sorting. */
70     private I_CmsListItemComparator m_comparator = new CmsListItemDefaultComparator();
71
72     /** Default action. */
73     private List JavaDoc m_defaultActions = new ArrayList JavaDoc();
74
75     /** List of actions. */
76     private List JavaDoc m_directActions = new ArrayList JavaDoc();
77
78     /** Data formatter. */
79     private I_CmsListFormatter m_formatter;
80
81     /** Customized help text. */
82     private CmsMessageContainer m_helpText;
83
84     /** Unique id. */
85     private final String JavaDoc m_id;
86
87     /** List id. */
88     private String JavaDoc m_listId;
89
90     /** Display name. */
91     private CmsMessageContainer m_name;
92
93     /** Is printable flag. */
94     private boolean m_printable = true;
95
96     /** Flag for text wrapping. */
97     private boolean m_textWrapping = false;
98
99     /** Visible Flag. */
100     private boolean m_visible = true;
101
102     /** Column width. */
103     private String JavaDoc m_width;
104
105     /**
106      * Default Constructor.<p>
107      *
108      * @param id the unique id
109      */

110     public CmsListColumnDefinition(String JavaDoc id) {
111
112         if (CmsStringUtil.isEmptyOrWhitespaceOnly(id)) {
113             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LIST_INVALID_NULL_ARG_1, "id"));
114         }
115         m_id = id;
116     }
117
118     /**
119      * Adds a default Action.<p>
120      *
121      * A column could have more than one default action if the visibilities are complementary.<p>
122      *
123      * @param defaultAction the default Action to add
124      */

125     public void addDefaultAction(CmsListDefaultAction defaultAction) {
126
127         if (m_listId != null) {
128             // set the list id
129
defaultAction.setListId(m_listId);
130         }
131         // set the column id
132
defaultAction.setColumnForLink(getId());
133
134         m_defaultActions.add(defaultAction);
135     }
136
137     /**
138      * Adds a new action to the column.<p>
139      *
140      * @param listAction the action to add
141      */

142     public void addDirectAction(I_CmsListDirectAction listAction) {
143
144         if (m_listId != null) {
145             listAction.setListId(m_listId);
146         }
147         m_directActions.add(listAction);
148     }
149
150     /**
151      * returns the csv output for a cell.<p>
152      *
153      * @param item the item to render the cell for
154      * @param wp the workplace context
155      *
156      * @return csv output
157      */

158     public String JavaDoc csvCell(CmsListItem item, CmsWorkplace wp) {
159
160         if (!isVisible()) {
161             return "";
162         }
163         StringBuffer JavaDoc csv = new StringBuffer JavaDoc(512);
164         if (m_formatter == null) {
165             // unformatted output
166
if (item.get(m_id) != null) {
167                 // null values are not showed by default
168
csv.append(item.get(m_id).toString());
169             } else {
170                 Iterator JavaDoc itActions = m_directActions.iterator();
171                 while (itActions.hasNext()) {
172                     I_CmsListDirectAction action = (I_CmsListDirectAction)itActions.next();
173                     if (action.isVisible()) {
174                         action.setItem(item);
175                         csv.append(action.getName().key(wp.getLocale()));
176                     }
177                 }
178             }
179         } else {
180             // formatted output
181
csv.append(m_formatter.format(item.get(m_id), wp.getLocale()));
182         }
183         return csv.toString();
184     }
185
186     /**
187      * Returns the csv output for a column header.<p>
188      *
189      * @param wp the workplace instance
190      *
191      * @return csv header
192      */

193     public String JavaDoc csvHeader(CmsWorkplace wp) {
194
195         if (!isVisible()) {
196             return "";
197         }
198         return getName().key(wp.getLocale());
199     }
200
201     /**
202      * Returns the align.<p>
203      *
204      * @return the align
205      */

206     public CmsListColumnAlignEnum getAlign() {
207
208         return m_align;
209     }
210
211     /**
212      * Returns a default action by id.<p>
213      *
214      * @param actionId the id of the action
215      *
216      * @return the action if found or null
217      */

218     public CmsListDefaultAction getDefaultAction(String JavaDoc actionId) {
219
220         Iterator JavaDoc it = m_defaultActions.iterator();
221         while (it.hasNext()) {
222             CmsListDefaultAction action = (CmsListDefaultAction)it.next();
223             if (action.getId().equals(actionId)) {
224                 return action;
225             }
226         }
227         return null;
228     }
229
230     /**
231      * Returns the default Action Ids list.<p>
232      *
233      * @return the default Action Ids list
234      */

235     public List JavaDoc getDefaultActionIds() {
236
237         List JavaDoc ids = new ArrayList JavaDoc();
238         Iterator JavaDoc itDefActions = m_defaultActions.iterator();
239         while (itDefActions.hasNext()) {
240             I_CmsListDirectAction action = (I_CmsListDirectAction)itDefActions.next();
241             ids.add(action.getId());
242         }
243         return Collections.unmodifiableList(ids);
244     }
245
246     /**
247      * Returns the default Actions list.<p>
248      *
249      * @return the default Actions list
250      */

251     public List JavaDoc getDefaultActions() {
252
253         return Collections.unmodifiableList(m_defaultActions);
254     }
255
256     /**
257      * Returns a direct action by id.<p>
258      *
259      * @param actionId the id of the action
260      *
261      * @return the action if found or null
262      */

263     public I_CmsListDirectAction getDirectAction(String JavaDoc actionId) {
264
265         Iterator JavaDoc it = m_directActions.iterator();
266         while (it.hasNext()) {
267             I_CmsListDirectAction action = (I_CmsListDirectAction)it.next();
268             if (action.getId().equals(actionId)) {
269                 return action;
270             }
271         }
272         return null;
273     }
274
275     /**
276      * Returns the direct Action Ids list.<p>
277      *
278      * @return the direct Action Ids list
279      */

280     public List JavaDoc getDirectActionIds() {
281
282         List JavaDoc ids = new ArrayList JavaDoc();
283         Iterator JavaDoc itDirActions = m_directActions.iterator();
284         while (itDirActions.hasNext()) {
285             I_CmsListDirectAction action = (I_CmsListDirectAction)itDirActions.next();
286             ids.add(action.getId());
287         }
288         return Collections.unmodifiableList(ids);
289     }
290
291     /**
292      * Returns all direct actions.<p>
293      *
294      * @return a list of <code>{@link I_CmsListDirectAction}</code>s.
295      */

296     public List JavaDoc getDirectActions() {
297
298         return Collections.unmodifiableList(m_directActions);
299     }
300
301     /**
302      * Returns the data formatter.<p>
303      *
304      * @return the data formatter
305      */

306     public I_CmsListFormatter getFormatter() {
307
308         return m_formatter;
309     }
310
311     /**
312      * Returns the customized help Text.<p>
313      *
314      * if <code>null</code> a default help text indicating the sort actions is used.<p>
315      *
316      * @return the customized help Text
317      */

318     public CmsMessageContainer getHelpText() {
319
320         return m_helpText;
321     }
322
323     /**
324      * Returns the id.<p>
325      *
326      * @return the id
327      */

328     public String JavaDoc getId() {
329
330         return m_id;
331     }
332
333     /**
334      * Returns the comparator, used for sorting.<p>
335      *
336      * if no comparator was set, the default list item comparator is used.<p>
337      *
338      * @return the comparator
339      *
340      * @see CmsListItemDefaultComparator
341      */

342     public I_CmsListItemComparator getListItemComparator() {
343
344         return m_comparator;
345     }
346
347     /**
348      * Returns the name.<p>
349      *
350      * @return the name
351      */

352     public CmsMessageContainer getName() {
353
354         return m_name;
355     }
356
357     /**
358      * Returns the width.<p>
359      *
360      * @return the width
361      */

362     public String JavaDoc getWidth() {
363
364         return m_width;
365     }
366
367     /**
368      * returns the html for a cell.<p>
369      *
370      * @param item the item to render the cell for
371      * @param wp the workplace context
372      * @param isPrintable if the list is to be printed
373      *
374      * @return html code
375      */

376     public String JavaDoc htmlCell(CmsListItem item, CmsWorkplace wp, boolean isPrintable) {
377
378         StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
379         Iterator JavaDoc itActions = m_directActions.iterator();
380         while (itActions.hasNext()) {
381             I_CmsListDirectAction action = (I_CmsListDirectAction)itActions.next();
382             action.setItem(item);
383             boolean enabled = action.isEnabled();
384             if (isPrintable) {
385                 action.setEnabled(false);
386             }
387             html.append(action.buttonHtml(wp));
388             if (isPrintable) {
389                 action.setEnabled(enabled);
390             }
391         }
392         if (!m_defaultActions.isEmpty()) {
393             Iterator JavaDoc itDefaultActions = m_defaultActions.iterator();
394             while (itDefaultActions.hasNext()) {
395                 I_CmsListDirectAction defAction = (I_CmsListDirectAction)itDefaultActions.next();
396                 defAction.setItem(item);
397                 boolean enabled = defAction.isEnabled();
398                 if (isPrintable) {
399                     defAction.setEnabled(false);
400                 }
401                 html.append(defAction.buttonHtml(wp));
402                 if (isPrintable) {
403                     defAction.setEnabled(enabled);
404                 }
405             }
406         } else {
407             if (m_formatter == null) {
408                 // unformatted output
409
if (item.get(m_id) != null) {
410                     // null values are not showed by default
411
html.append(item.get(m_id).toString());
412                 }
413             } else {
414                 // formatted output
415
html.append(m_formatter.format(item.get(m_id), wp.getLocale()));
416             }
417         }
418         html.append("\n");
419         return html.toString();
420     }
421
422     /**
423      * Returns the html code for a column header.<p>
424      *
425      * @param list the list to generate the header code for
426      * @param wp the workplace instance
427      *
428      * @return html code
429      */

430     public String JavaDoc htmlHeader(CmsHtmlList list, CmsWorkplace wp) {
431
432         if (!isVisible()) {
433             return "";
434         }
435
436         String JavaDoc listId = list.getId();
437         String JavaDoc sortedCol = list.getSortedColumn();
438         CmsListOrderEnum order = list.getCurrentSortOrder();
439
440         StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
441         Locale JavaDoc locale = wp.getLocale();
442         CmsMessages messages = Messages.get().getBundle(locale);
443         html.append("<th");
444         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getWidth())) {
445             html.append(" width='");
446             html.append(getWidth());
447             html.append("'");
448         }
449         if (!isTextWrapping()) {
450             html.append(" style='white-space: nowrap;'");
451         }
452         html.append(">\n");
453
454         boolean isSorted = getId().equals(sortedCol);
455         CmsListOrderEnum nextOrder = CmsListOrderEnum.ORDER_ASCENDING;
456         if (isSorted && order == CmsListOrderEnum.ORDER_ASCENDING) {
457             nextOrder = CmsListOrderEnum.ORDER_DESCENDING;
458         }
459         // button
460
String JavaDoc id = listId + getId() + "Sort";
461         String JavaDoc onClic = "listSort('" + listId + "', '" + getId() + "');";
462         String JavaDoc helpText = null;
463         if (m_helpText != null) {
464             helpText = new MessageFormat JavaDoc(m_helpText.key(locale), locale).format(new Object JavaDoc[] {getName().key(
465                 locale)});
466         } else {
467             if (isSorteable()) {
468                 if (nextOrder.equals(CmsListOrderEnum.ORDER_ASCENDING)) {
469                     helpText = messages.key(Messages.GUI_LIST_COLUMN_ASC_SORT_1, new Object JavaDoc[] {getName().key(
470                         locale)});
471                 } else {
472                     helpText = messages.key(Messages.GUI_LIST_COLUMN_DESC_SORT_1, new Object JavaDoc[] {getName().key(
473                         locale)});
474                 }
475             } else {
476                 helpText = messages.key(
477                     Messages.GUI_LIST_COLUMN_NO_SORT_1,
478                     new Object JavaDoc[] {getName().key(locale)});
479             }
480         }
481         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getWidth()) && getWidth().indexOf('%') < 0) {
482             html.append("\t<div style='display:block; width: ");
483             html.append(getWidth());
484             html.append("px;'>\n");
485         }
486         html.append(A_CmsHtmlIconButton.defaultButtonHtml(
487             wp.getJsp(),
488             CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
489             id,
490             getName().key(locale),
491             helpText,
492             list.isPrintable() ? false : isSorteable(),
493             null,
494             null,
495             onClic));
496         // sort order marker
497
if (isSorted) {
498             if (nextOrder == CmsListOrderEnum.ORDER_ASCENDING) {
499                 html.append("\t\t<img SRC='");
500                 html.append(CmsWorkplace.getSkinUri());
501                 html.append(ICON_UP);
502                 html.append("' alt=''>\n");
503             } else {
504                 html.append("\t\t<img SRC='");
505                 html.append(CmsWorkplace.getSkinUri());
506                 html.append(ICON_DOWN);
507                 html.append("' alt=''>\n");
508             }
509         }
510         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getWidth()) && getWidth().indexOf('%') < 0) {
511             html.append("\t</div>\n");
512         }
513         html.append("</th>\n");
514         return html.toString();
515     }
516
517     /**
518      * Returns the printable .<p>
519      *
520      * @return the printable flag
521      */

522     public boolean isPrintable() {
523
524         return m_printable;
525     }
526
527     /**
528      * Returns the sorteable.<p>
529      *
530      * @return the sorteable
531      */

532     public boolean isSorteable() {
533
534         return getListItemComparator() != null;
535     }
536
537     /**
538      * Returns the text Wrapping flag.<p>
539      *
540      * @return the text Wrapping flag
541      */

542     public boolean isTextWrapping() {
543
544         return m_textWrapping;
545     }
546
547     /**
548      * Returns the visible.<p>
549      *
550      * @return the visible
551      */

552     public boolean isVisible() {
553
554         return m_visible;
555     }
556
557     /**
558      * Sets the align.<p>
559      *
560      * @param align the align to set
561      */

562     public void setAlign(CmsListColumnAlignEnum align) {
563
564         m_align = align;
565     }
566
567     /**
568      * Sets the data formatter.<p>
569      *
570      * @param formatter the data formatter to set
571      */

572     public void setFormatter(I_CmsListFormatter formatter) {
573
574         m_formatter = formatter;
575     }
576
577     /**
578      * Sets the customized help Text.<p>
579      *
580      * if <code>null</code> a default help text indicating the sort actions is used.<p>
581      *
582      * @param helpText the customized help Text to set
583      */

584     public void setHelpText(CmsMessageContainer helpText) {
585
586         m_helpText = helpText;
587     }
588
589     /**
590      * Sets the comparator, used for sorting.<p>
591      *
592      * @param comparator the comparator to set
593      */

594     public void setListItemComparator(I_CmsListItemComparator comparator) {
595
596         m_comparator = comparator;
597     }
598
599     /**
600      * Sets the name.<p>
601      *
602      * @param name the name to set
603      */

604     public void setName(CmsMessageContainer name) {
605
606         m_name = name;
607     }
608
609     /**
610      * Sets the printable flag.<p>
611      *
612      * @param printable the printable flag to set
613      */

614     public void setPrintable(boolean printable) {
615
616         m_printable = printable;
617     }
618
619     /**
620      * Indicates if the current column is sorteable or not.<p>
621      *
622      * if <code>true</code> a default list item comparator is used.<p>
623      *
624      * if <code>false</code> any previously set list item comparator is removed.<p>
625      *
626      * @param sorteable the sorteable flag
627      */

628     public void setSorteable(boolean sorteable) {
629
630         if (sorteable) {
631             setListItemComparator(new CmsListItemDefaultComparator());
632         } else {
633             setListItemComparator(null);
634         }
635     }
636
637     /**
638      * Sets the text Wrapping flag.<p>
639      *
640      * @param textWrapping the text Wrapping flag to set
641      */

642     public void setTextWrapping(boolean textWrapping) {
643
644         m_textWrapping = textWrapping;
645     }
646
647     /**
648      * Sets the visible.<p>
649      *
650      * @param visible the visible to set
651      */

652     public void setVisible(boolean visible) {
653
654         m_visible = visible;
655     }
656
657     /**
658      * Sets the width.<p>
659      *
660      * @param width the width to set
661      */

662     public void setWidth(String JavaDoc width) {
663
664         m_width = width;
665     }
666
667     /**
668      * Sets the id of the list.<p>
669      *
670      * @param listId the id of the list
671      */

672     /*package*/void setListId(String JavaDoc listId) {
673
674         m_listId = listId;
675     }
676 }
Popular Tags