KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/list/CmsHtmlList.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.35 $
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.CmsDialog;
39 import org.opencms.workplace.CmsWorkplace;
40 import org.opencms.workplace.tools.A_CmsHtmlIconButton;
41 import org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum;
42
43 import java.util.ArrayList JavaDoc;
44 import java.util.Collection JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Locale JavaDoc;
49
50 /**
51  * The main class of the html list widget.<p>
52  *
53  * @author Michael Moossen
54  *
55  * @version $Revision: 1.35 $
56  *
57  * @since 6.0.0
58  */

59 public class CmsHtmlList {
60
61     /** Standard list button location. */
62     public static final String JavaDoc ICON_LEFT = "list/leftarrow.png";
63
64     /** Standard list button location. */
65     public static final String JavaDoc ICON_RIGHT = "list/rightarrow.png";
66
67     /** Constant for item separator char used for coding/encoding multiselection. */
68     public static final String JavaDoc ITEM_SEPARATOR = "|";
69
70     /** Var name for error message if no item has been selected. */
71     public static final String JavaDoc NO_SELECTION_HELP_VAR = "noSelHelp";
72
73     /** Var name for error message if number of selected items does not match. */
74     public static final String JavaDoc NO_SELECTION_MATCH_HELP_VAR = "noSelMatchHelp";
75
76     /** Current displayed page number. */
77     private int m_currentPage;
78
79     /** Current sort order. */
80     private CmsListOrderEnum m_currentSortOrder;
81
82     /** Filtered list of items or <code>null</code> if no filter is set and not sorted. */
83     private List JavaDoc m_filteredItems;
84
85     /** Dhtml id. */
86     private final String JavaDoc m_id;
87
88     /** Maximum number of items per page. */
89     private int m_maxItemsPerPage = 20;
90
91     /** Metadata for building the list. */
92     private CmsListMetadata m_metadata;
93
94     /** Display Name of the list. */
95     private final CmsMessageContainer m_name;
96
97     /** Really content of the list. */
98     private final List JavaDoc m_originalItems = new ArrayList JavaDoc();
99
100     /** printable flag. */
101     private boolean m_printable;
102
103     /** Search filter text. */
104     private String JavaDoc m_searchFilter;
105
106     /** Column name to be sorted. */
107     private String JavaDoc m_sortedColumn;
108
109     /** Items currently displayed. */
110     private List JavaDoc m_visibleItems;
111
112     /**
113      * Default Constructor.<p>
114      *
115      * @param id unique id of the list, is used as name for controls and js functions and vars
116      * @param name the display name
117      * @param metadata the list's metadata
118      */

119     public CmsHtmlList(String JavaDoc id, CmsMessageContainer name, CmsListMetadata metadata) {
120
121         m_id = id;
122         m_name = name;
123         m_metadata = metadata;
124         m_currentPage = 1;
125     }
126
127     /**
128      * Adds a collection new list items to the content of the list.<p>
129      *
130      * If you need to add or remove items from or to the list at a later step, use the
131      * <code>{@link #insertAllItems(Collection, Locale)}</code> or
132      * <code>{@link #removeAllItems(Collection, Locale)}</code> methods.<p>
133      *
134      * @param listItems the collection of list items to add
135      *
136      * @see List#addAll(Collection)
137      */

138     public void addAllItems(Collection JavaDoc listItems) {
139
140         m_originalItems.addAll(listItems);
141     }
142
143     /**
144      * Adds a new item to the content of the list.<p>
145      *
146      * If you need to add or remove an item from or to the list at a later step, use the
147      * <code>{@link #insertItem(CmsListItem, Locale)}</code> or
148      * <code>{@link #removeItem(String, Locale)}</code> methods.<p>
149      *
150      * @param listItem the list item
151      *
152      * @see List#add(Object)
153      */

154     public void addItem(CmsListItem listItem) {
155
156         m_originalItems.add(listItem);
157     }
158
159     /**
160      * This method resets the content of the list (no the metadata).<p>
161      *
162      * @param locale the locale for sorting/searching
163      */

164     public void clear(Locale JavaDoc locale) {
165
166         m_originalItems.clear();
167         m_filteredItems = null;
168         if (m_visibleItems != null) {
169             m_visibleItems.clear();
170         }
171         setSearchFilter("", locale);
172         m_sortedColumn = null;
173     }
174
175     /**
176      * Returns all list items in the list, may be not visible and sorted.<p>
177      *
178      * @return all list items
179      */

180     public List JavaDoc getAllContent() {
181
182         return Collections.unmodifiableList(m_originalItems);
183     }
184
185     /**
186      * Returns the filtered list of list items.<p>
187      *
188      * Equals to <code>{@link #getAllContent()}</code> if no filter is set.<p>
189      *
190      * @return the filtered list of list items
191      */

192     public List JavaDoc getContent() {
193
194         if (m_filteredItems == null) {
195             return getAllContent();
196         } else {
197             return Collections.unmodifiableList(m_filteredItems);
198         }
199     }
200
201     /**
202      * returns the number of the current page.<p>
203      *
204      * @return the number of the current page
205      */

206     public int getCurrentPage() {
207
208         return m_currentPage;
209     }
210
211     /**
212      * Returns all items of the current page.<p>
213      *
214      * @return all items of the current page, a list of {@link CmsListItem} objects
215      */

216     public List JavaDoc getCurrentPageItems() {
217
218         if (getSize() == 0) {
219             return Collections.EMPTY_LIST;
220         }
221         return Collections.unmodifiableList(getContent().subList(displayedFrom() - 1, displayedTo()));
222     }
223
224     /**
225      * Returns the current used sort order.<p>
226      *
227      * @return the current used sort order
228      */

229     public CmsListOrderEnum getCurrentSortOrder() {
230
231         return m_currentSortOrder;
232     }
233
234     /**
235      * Returns the id.<p>
236      *
237      * @return the id
238      */

239     public String JavaDoc getId() {
240
241         return m_id;
242     }
243
244     /**
245      * This method returns the item identified by the parameter id.<p>
246      *
247      * Only current visible item can be retrieved using this method.<p>
248      *
249      * @param id the id of the item to look for
250      *
251      * @return the requested item or <code>null</code> if not found
252      */

253     public CmsListItem getItem(String JavaDoc id) {
254
255         Iterator JavaDoc it = m_originalItems.iterator();
256         while (it.hasNext()) {
257             CmsListItem item = (CmsListItem)it.next();
258             if (item.getId().equals(id)) {
259                 return item;
260             }
261         }
262         return null;
263     }
264
265     /**
266      * Returns the maximum number of items per page.<p>
267      *
268      * @return the maximum number of items per page
269      */

270     public int getMaxItemsPerPage() {
271
272         return m_maxItemsPerPage;
273     }
274
275     /**
276      * Returns the metadata.<p>
277      *
278      * @return the metadata
279      */

280     public CmsListMetadata getMetadata() {
281
282         return m_metadata;
283     }
284
285     /**
286      * Returns the name of the list.<p>
287      *
288      * @return the list's name
289      */

290     public CmsMessageContainer getName() {
291
292         return m_name;
293     }
294
295     /**
296      * Returns the filtered number of pages.<p>
297      *
298      * Equals to <code>{@link #getTotalNumberOfPages()}</code> if no filter is set.<p>
299      *
300      * @return the filtered of pages
301      */

302     public int getNumberOfPages() {
303
304         return (int)Math.ceil((double)getSize() / getMaxItemsPerPage());
305     }
306
307     /**
308      * Returns the search filter.<p>
309      *
310      * @return the search filter
311      */

312     public String JavaDoc getSearchFilter() {
313
314         return m_searchFilter;
315     }
316
317     /**
318      * Return the filtered number of items.<p>
319      *
320      * Equals to <code>{@link #getTotalSize()}</code> if no filter is set.<p>
321      *
322      * @return the filtered number of items
323      */

324     public int getSize() {
325
326         return getContent().size();
327     }
328
329     /**
330      * Returns the sorted column's name.<p>
331      *
332      * @return the sorted column's name
333      */

334     public String JavaDoc getSortedColumn() {
335
336         return m_sortedColumn;
337     }
338
339     /**
340      * Returns a filled list state.<p>
341      *
342      * @return the state of the list
343      */

344     public CmsListState getState() {
345
346         return new CmsListState(this);
347     }
348
349     /**
350      * Returns the total number of pages.<p>
351      *
352      * @return the total number of pages
353      */

354     public int getTotalNumberOfPages() {
355
356         return (int)Math.ceil((double)getAllContent().size() / getMaxItemsPerPage());
357     }
358
359     /**
360      * Return the total number of items.<p>
361      *
362      * @return the total number of items
363      */

364     public int getTotalSize() {
365
366         return getAllContent().size();
367     }
368
369     /**
370      * Inserts a collection of list items in an already initialized list.<p>
371      *
372      * Keeping care of all the state like sorted column, sorting order, displayed page and search filter.<p>
373      *
374      * @param listItems the collection of list items to insert
375      * @param locale the locale
376      */

377     public void insertAllItems(Collection JavaDoc listItems, Locale JavaDoc locale) {
378
379         CmsListState state = null;
380         if (m_filteredItems != null || m_visibleItems != null) {
381             state = getState();
382         }
383         addAllItems(listItems);
384         if (m_filteredItems != null) {
385             m_filteredItems.addAll(listItems);
386         }
387         if (m_visibleItems != null) {
388             m_visibleItems.addAll(listItems);
389         }
390         if (state != null) {
391             setState(state, locale);
392         }
393     }
394
395     /**
396      * Inserts an item in an already initialized list.<p>
397      *
398      * Keeping care of all the state like sorted column, sorting order, displayed page and search filter.<p>
399      *
400      * @param listItem the list item to insert
401      * @param locale the locale
402      */

403     public void insertItem(CmsListItem listItem, Locale JavaDoc locale) {
404
405         CmsListState state = null;
406         if (m_filteredItems != null || m_visibleItems != null) {
407             state = getState();
408         }
409         addItem(listItem);
410         if (m_filteredItems != null) {
411             m_filteredItems.add(listItem);
412         }
413         if (m_visibleItems != null) {
414             m_visibleItems.add(listItem);
415         }
416         if (state != null) {
417             setState(state, locale);
418         }
419     }
420
421     /**
422      * Returns the printable flag.<p>
423      *
424      * @return the printable flag
425      */

426     public boolean isPrintable() {
427
428         return m_printable;
429     }
430
431     /**
432      * Generates the csv output for the list.<p>
433      *
434      * Synchronized to not collide with <code>{@link #listHtml(CmsWorkplace)}</code>.<p>
435      *
436      * @param wp the workplace object
437      *
438      * @return csv output
439      */

440     public synchronized String JavaDoc listCsv(CmsWorkplace wp) {
441
442         StringBuffer JavaDoc csv = new StringBuffer JavaDoc(5120);
443         csv.append(m_metadata.csvHeader(wp));
444         if (getContent().isEmpty()) {
445             csv.append(m_metadata.csvEmptyList());
446         } else {
447             Iterator JavaDoc itItems = getContent().iterator();
448             while (itItems.hasNext()) {
449                 CmsListItem item = (CmsListItem)itItems.next();
450                 csv.append(m_metadata.csvItem(item, wp));
451             }
452         }
453         return wp.resolveMacros(csv.toString());
454     }
455
456     /**
457      * Generates the html code for the list.<p>
458      *
459      * Synchronized to not collide with <code>{@link #printableHtml(CmsWorkplace)}</code>.<p>
460      *
461      * @param wp the workplace object
462      *
463      * @return html code
464      */

465     public synchronized String JavaDoc listHtml(CmsWorkplace wp) {
466
467         if (isPrintable()) {
468             m_visibleItems = new ArrayList JavaDoc(getContent());
469         } else {
470             m_visibleItems = new ArrayList JavaDoc(getCurrentPageItems());
471         }
472
473         StringBuffer JavaDoc html = new StringBuffer JavaDoc(5120);
474         html.append(htmlBegin(wp));
475         if (!isPrintable()) {
476             html.append(htmlTitle(wp));
477             html.append(htmlToolBar(wp));
478         } else {
479             html.append("<style type='text/css'>\n");
480             html.append("td.listdetailitem, \n");
481             html.append(".linkdisabled {\n");
482             html.append("\tcolor: black;\n");
483             html.append("}\n");
484             html.append(".list th {\n");
485             html.append("\tborder: 1px solid black;\n");
486             html.append("}\n");
487             html.append(".list {\n");
488             html.append("\tborder: 1px solid black;\n");
489             html.append("}\n");
490             html.append("</style>");
491         }
492         html.append("<table width='100%' cellpadding='1' cellspacing='0' class='list'>\n");
493         html.append(m_metadata.htmlHeader(this, wp));
494         if (m_visibleItems.isEmpty()) {
495             html.append(m_metadata.htmlEmptyTable(wp.getLocale()));
496         } else {
497             Iterator JavaDoc itItems = m_visibleItems.iterator();
498             boolean odd = true;
499             while (itItems.hasNext()) {
500                 CmsListItem item = (CmsListItem)itItems.next();
501                 html.append(m_metadata.htmlItem(item, wp, odd, isPrintable()));
502                 odd = !odd;
503             }
504         }
505         html.append("</table>\n");
506         if (!isPrintable()) {
507             html.append(htmlPagingBar(wp));
508         }
509         html.append(htmlEnd(wp));
510         return wp.resolveMacros(html.toString());
511     }
512
513     /**
514      * Generate the need js code for the list.<p>
515      *
516      * @param locale the locale
517      *
518      * @return js code
519      */

520     public String JavaDoc listJs(Locale JavaDoc locale) {
521
522         StringBuffer JavaDoc js = new StringBuffer JavaDoc(1024);
523         CmsMessages messages = Messages.get().getBundle(locale);
524         js.append("<script type='text/javascript' SRC='");
525         js.append(CmsWorkplace.getSkinUri());
526         js.append("admin/javascript/list.js'></script>\n");
527         if (!m_metadata.getMultiActions().isEmpty()) {
528             js.append("<script type='text/javascript'>\n");
529             js.append("\tvar ");
530             js.append(NO_SELECTION_HELP_VAR);
531             js.append(" = '");
532             js.append(CmsStringUtil.escapeJavaScript(messages.key(Messages.GUI_LIST_ACTION_NO_SELECTION_0)));
533             js.append("';\n");
534             Iterator JavaDoc it = m_metadata.getMultiActions().iterator();
535             while (it.hasNext()) {
536                 CmsListMultiAction action = (CmsListMultiAction)it.next();
537                 if (action instanceof CmsListRadioMultiAction) {
538                     CmsListRadioMultiAction rAction = (CmsListRadioMultiAction)action;
539                     js.append("\tvar ");
540                     js.append(NO_SELECTION_MATCH_HELP_VAR);
541                     js.append(rAction.getId());
542                     js.append(" = '");
543                     js.append(CmsStringUtil.escapeJavaScript(messages.key(
544                         Messages.GUI_LIST_ACTION_NO_SELECTION_MATCH_1,
545                         new Integer JavaDoc(rAction.getSelections()))));
546                     js.append("';\n");
547                 }
548             }
549             js.append("</script>\n");
550         }
551         return js.toString();
552     }
553
554     /**
555      * Returns a new list item for this list.<p>
556      *
557      * @param id the id of the item has to be unique
558      * @return a new list item
559      */

560     public CmsListItem newItem(String JavaDoc id) {
561
562         return new CmsListItem(getMetadata(), id);
563     }
564
565     /**
566      * Returns html code for printing the list.<p>
567      *
568      * Synchronized to not collide with <code>{@link #listHtml(CmsWorkplace)}</code>.<p>
569      *
570      * @param wp the workplace object
571      *
572      * @return html code
573      */

574     public synchronized String JavaDoc printableHtml(CmsWorkplace wp) {
575
576         m_printable = true;
577         String JavaDoc html = listHtml(wp);
578         m_printable = false;
579         return html;
580     }
581
582     /**
583      * Removes a collection of list items from the list.<p>
584      *
585      * Keeping care of all the state like sorted column, sorting order, displayed page and search filter.<p>
586      *
587      * Try to use it instead of <code>{@link A_CmsListDialog#refreshList()}</code>.<p>
588      *
589      * @param ids the collection of ids of the items to remove
590      * @param locale the locale
591      *
592      * @return the list of removed list items
593      */

594     public List JavaDoc removeAllItems(Collection JavaDoc ids, Locale JavaDoc locale) {
595
596         List JavaDoc removedItems = new ArrayList JavaDoc();
597         Iterator JavaDoc itItems = m_originalItems.iterator();
598         while (itItems.hasNext()) {
599             CmsListItem listItem = (CmsListItem)itItems.next();
600             if (ids.contains(listItem.getId())) {
601                 removedItems.add(listItem);
602             }
603         }
604         if (removedItems.isEmpty()) {
605             return removedItems;
606         }
607         CmsListState state = null;
608         if (m_filteredItems != null || m_visibleItems != null) {
609             state = getState();
610         }
611         m_originalItems.removeAll(removedItems);
612         if (m_filteredItems != null) {
613             m_filteredItems.removeAll(removedItems);
614         }
615         if (m_visibleItems != null) {
616             m_visibleItems.removeAll(removedItems);
617         }
618         if (state != null) {
619             setState(state, locale);
620         }
621         return removedItems;
622     }
623
624     /**
625      * Removes an item from the list.<p>
626      *
627      * Keeping care of all the state like sorted column, sorting order, displayed page and search filter.<p>
628      *
629      * Try to use it instead of <code>{@link A_CmsListDialog#refreshList()}</code>.<p>
630      *
631      * @param id the id of the item to remove
632      * @param locale the locale
633      *
634      * @return the removed list item
635      */

636     public CmsListItem removeItem(String JavaDoc id, Locale JavaDoc locale) {
637
638         CmsListItem item = null;
639         Iterator JavaDoc itItems = m_originalItems.iterator();
640         while (itItems.hasNext()) {
641             CmsListItem listItem = (CmsListItem)itItems.next();
642             if (listItem.getId().equals(id)) {
643                 item = listItem;
644                 break;
645             }
646         }
647         if (item == null) {
648             return null;
649         }
650         CmsListState state = null;
651         if (m_filteredItems != null || m_visibleItems != null) {
652             state = getState();
653         }
654         m_originalItems.remove(item);
655         if (m_filteredItems != null) {
656             m_filteredItems.remove(item);
657         }
658         if (m_visibleItems != null) {
659             m_visibleItems.remove(item);
660         }
661         if (state != null) {
662             setState(state, locale);
663         }
664         return item;
665     }
666
667     /**
668      * Replace a list of items in the list.<p>
669      *
670      * Keeping care of all the state like sorted column, sorting order, displayed page and search filter.<p>
671      *
672      * If the list already contains an item with the id of a given list item, it will be removed and
673      * replaced by the new list item. if not, this method is the same as the
674      * <code>{@link #insertAllItems(Collection, Locale)}</code> method.
675      *
676      * Try to use it instead of <code>{@link A_CmsListDialog#refreshList()}</code>.<p>
677      *
678      * @param listItems the list of <code>{@link CmsListItem}</code>s to replace
679      * @param locale the locale
680      *
681      * @return the removed list item, or <code>null</code>
682      */

683     public List JavaDoc replaceAllItems(List JavaDoc listItems, Locale JavaDoc locale) {
684
685         List JavaDoc removedItems = new ArrayList JavaDoc();
686         Iterator JavaDoc itItems = m_originalItems.iterator();
687         while (itItems.hasNext()) {
688             CmsListItem listItem = (CmsListItem)itItems.next();
689             Iterator JavaDoc itNewItems = listItems.iterator();
690             while (itNewItems.hasNext()) {
691                 if (listItem.equals(((CmsListItem)itNewItems.next()).getId())) {
692                     removedItems.add(listItem);
693                 }
694             }
695         }
696         CmsListState state = null;
697         if (m_filteredItems != null || m_visibleItems != null) {
698             state = getState();
699         }
700         if (!removedItems.isEmpty()) {
701             m_originalItems.removeAll(removedItems);
702         }
703         addAllItems(listItems);
704         if (state != null) {
705             setState(state, locale);
706         }
707         return removedItems;
708     }
709
710     /**
711      * Replace an item in the list.<p>
712      *
713      * Keeping care of all the state like sorted column, sorting order, displayed page and search filter.<p>
714      *
715      * If the list already contains an item with the id of the given list item, it will be removed and
716      * replaced by the new list item. if not, this method is the same as the
717      * <code>{@link #insertItem(CmsListItem, Locale)}</code> method.
718      *
719      * Try to use it instead of <code>{@link A_CmsListDialog#refreshList()}</code>.<p>
720      *
721      * @param listItem the listItem to replace
722      * @param locale the locale
723      *
724      * @return the removed list item, or <code>null</code>
725      */

726     public CmsListItem replaceItem(CmsListItem listItem, Locale JavaDoc locale) {
727
728         CmsListItem item = null;
729         Iterator JavaDoc itItems = m_originalItems.iterator();
730         while (itItems.hasNext()) {
731             CmsListItem tmp = (CmsListItem)itItems.next();
732             if (tmp.getId().equals(listItem.getId())) {
733                 item = tmp;
734                 break;
735             }
736         }
737         CmsListState state = null;
738         if (m_filteredItems != null || m_visibleItems != null) {
739             state = getState();
740         }
741         if (item != null) {
742             m_originalItems.remove(item);
743         }
744         addItem(listItem);
745         if (state != null) {
746             setState(state, locale);
747         }
748         return item;
749     }
750
751     /**
752      * Sets the current page.<p>
753      *
754      * @param currentPage the current page to set
755      *
756      * @throws CmsIllegalArgumentException if the argument is invalid
757      */

758     public void setCurrentPage(int currentPage) throws CmsIllegalArgumentException {
759
760         if (getSize() != 0) {
761             if (currentPage < 1 || currentPage > getNumberOfPages()) {
762                 throw new CmsIllegalArgumentException(Messages.get().container(
763                     Messages.ERR_LIST_INVALID_PAGE_1,
764                     new Integer JavaDoc(currentPage)));
765             }
766         }
767         m_currentPage = currentPage;
768     }
769
770     /**
771      * Sets the maximum number of items per page.<p>
772      *
773      * @param maxItemsPerPage the maximum number of items per page to set
774      */

775     public void setMaxItemsPerPage(int maxItemsPerPage) {
776
777         this.m_maxItemsPerPage = maxItemsPerPage;
778     }
779
780     /**
781      * Sets the search filter.<p>
782      *
783      * @param searchFilter the search filter to set
784      * @param locale the used locale for searching/sorting
785      */

786     public void setSearchFilter(String JavaDoc searchFilter, Locale JavaDoc locale) {
787
788         if (!m_metadata.isSearchable()) {
789             return;
790         }
791         if (CmsStringUtil.isEmptyOrWhitespaceOnly(searchFilter)) {
792             // reset content if filter is empty
793
m_filteredItems = null;
794             m_searchFilter = "";
795             getMetadata().getSearchAction().getShowAllAction().setVisible(false);
796         } else {
797             m_filteredItems = getMetadata().getSearchAction().filter(getAllContent(), searchFilter);
798             m_searchFilter = searchFilter;
799             getMetadata().getSearchAction().getShowAllAction().setVisible(true);
800         }
801         String JavaDoc sCol = m_sortedColumn;
802         m_sortedColumn = "";
803         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(sCol)) {
804             CmsListOrderEnum order = getCurrentSortOrder();
805             setSortedColumn(sCol, locale);
806             if (order == CmsListOrderEnum.ORDER_DESCENDING) {
807                 setSortedColumn(sCol, locale);
808             }
809         }
810         setCurrentPage(1);
811     }
812
813     /**
814      * Sets the sorted column.<p>
815      *
816      * @param sortedColumn the sorted column to set
817      * @param locale the used locale for sorting
818      *
819      * @throws CmsIllegalArgumentException if the <code>sortedColumn</code> argument is invalid
820      */

821     public void setSortedColumn(String JavaDoc sortedColumn, Locale JavaDoc locale) throws CmsIllegalArgumentException {
822
823         if (getMetadata().getColumnDefinition(sortedColumn) == null
824             || !getMetadata().getColumnDefinition(sortedColumn).isSorteable()) {
825             return;
826         }
827         // check if the parameter is valid
828
if (m_metadata.getColumnDefinition(sortedColumn) == null) {
829             throw new CmsIllegalArgumentException(Messages.get().container(
830                 Messages.ERR_LIST_INVALID_COLUMN_1,
831                 sortedColumn));
832         }
833         // reset view
834
setCurrentPage(1);
835         // only reverse order if the to sort column is already sorted
836
if (sortedColumn.equals(m_sortedColumn)) {
837             if (m_currentSortOrder == CmsListOrderEnum.ORDER_ASCENDING) {
838                 m_currentSortOrder = CmsListOrderEnum.ORDER_DESCENDING;
839             } else {
840                 m_currentSortOrder = CmsListOrderEnum.ORDER_ASCENDING;
841             }
842             Collections.reverse(m_filteredItems);
843             return;
844         }
845         // sort new column
846
m_sortedColumn = sortedColumn;
847         m_currentSortOrder = CmsListOrderEnum.ORDER_ASCENDING;
848         I_CmsListItemComparator c = getMetadata().getColumnDefinition(sortedColumn).getListItemComparator();
849         if (m_filteredItems == null) {
850             m_filteredItems = new ArrayList JavaDoc(getAllContent());
851         }
852         Collections.sort(m_filteredItems, c.getComparator(sortedColumn, locale));
853     }
854
855     /**
856      * Sets the list state.<p>
857      *
858      * This may involve sorting, filtering and paging.<p>
859      *
860      * @param listState the state to be set
861      * @param locale the locale
862      */

863     public void setState(CmsListState listState, Locale JavaDoc locale) {
864
865         setSearchFilter(listState.getFilter(), locale);
866         setSortedColumn(listState.getColumn(), locale);
867         if (listState.getOrder() == CmsListOrderEnum.ORDER_DESCENDING) {
868             setSortedColumn(listState.getColumn(), locale);
869         }
870         if (listState.getPage() > 0) {
871             if (listState.getPage() <= getNumberOfPages()) {
872                 setCurrentPage(listState.getPage());
873             } else {
874                 setCurrentPage(1);
875             }
876         }
877     }
878
879     /**
880      * Sets the metadata for this list.<p>
881      *
882      * Should only be used by the <code>{@link A_CmsListDialog}</code> class
883      * for temporaly removing the metadata object while the list is saved in the
884      * <code>{@link org.opencms.workplace.CmsWorkplaceSettings}</code>.<p>
885      *
886      * @param metadata the list metadata
887      */

888     /*package*/void setMetadata(CmsListMetadata metadata) {
889
890         m_metadata = metadata;
891     }
892
893     /**
894      * Returns the number (from 1) of the first displayed item.<p>
895      *
896      * @return the number (from 1) of the first displayed item, or zero if the list is empty
897      */

898     private int displayedFrom() {
899
900         if (getSize() != 0) {
901             if (isPrintable()) {
902                 return 1;
903             } else {
904                 return (getCurrentPage() - 1) * getMaxItemsPerPage() + 1;
905             }
906         }
907         return 0;
908     }
909
910     /**
911      * Returns the number (from 1) of the last displayed item.<p>
912      *
913      * @return the number (from 1) of the last displayed item, or zero if the list is empty
914      */

915     private int displayedTo() {
916
917         if (getSize() != 0) {
918             if (!isPrintable()) {
919                 if (getCurrentPage() * getMaxItemsPerPage() < getSize()) {
920                     return getCurrentPage() * getMaxItemsPerPage();
921                 }
922             }
923         }
924         return getSize();
925     }
926
927     /**
928      * Generates the initial html code.<p>
929      *
930      * @param wp the workplace context
931      *
932      * @return html code
933      */

934     private String JavaDoc htmlBegin(CmsWorkplace wp) {
935
936         StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
937         // help & confirmation text for actions if needed
938
if (!isPrintable() && m_visibleItems != null && !m_visibleItems.isEmpty()) {
939             Iterator JavaDoc cols = getMetadata().getColumnDefinitions().iterator();
940             while (cols.hasNext()) {
941                 CmsListColumnDefinition col = (CmsListColumnDefinition)cols.next();
942                 Iterator JavaDoc actions = col.getDirectActions().iterator();
943                 while (actions.hasNext()) {
944                     I_CmsListDirectAction action = (I_CmsListDirectAction)actions.next();
945                     action.setItem((CmsListItem)m_visibleItems.get(0));
946                     html.append(action.helpTextHtml(wp));
947                     html.append(action.confirmationTextHtml(wp));
948                 }
949                 Iterator JavaDoc defActions = col.getDefaultActions().iterator();
950                 while (defActions.hasNext()) {
951                     I_CmsListDirectAction action = (I_CmsListDirectAction)defActions.next();
952                     action.setItem((CmsListItem)m_visibleItems.get(0));
953                     html.append(action.helpTextHtml(wp));
954                     html.append(action.confirmationTextHtml(wp));
955                 }
956             }
957         }
958         // start list code
959
html.append("<div class='listArea'>\n");
960         html.append(((CmsDialog)wp).dialogBlock(CmsWorkplace.HTML_START, m_name.key(wp.getLocale()), false));
961         html.append("\t\t<table width='100%' cellspacing='0' cellpadding='0' border='0'>\n");
962         html.append("\t\t\t<tr><td>\n");
963         return html.toString();
964     }
965
966     /**
967      * Generates the need html code for ending a lsit.<p>
968      *
969      * @param wp the workplace context
970      *
971      * @return html code
972      */

973     private String JavaDoc htmlEnd(CmsWorkplace wp) {
974
975         StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
976         html.append("\t\t\t</td></tr>\n");
977         html.append("\t\t</table>\n");
978         html.append(((CmsDialog)wp).dialogBlock(CmsWorkplace.HTML_END, m_name.key(wp.getLocale()), false));
979         html.append("</div>\n");
980         if (!isPrintable() && getMetadata().isSearchable()) {
981             html.append("<script type='text/javascript'>\n");
982             html.append("\tvar form = document.forms['");
983             html.append(getId());
984             html.append("-form'];\n");
985             html.append("\tform.listSearchFilter.value='");
986             html.append(getSearchFilter() != null ? CmsStringUtil.escapeJavaScript(getSearchFilter()) : "");
987             html.append("';\n");
988             html.append("</script>\n");
989         }
990         return html.toString();
991     }
992
993     /**
994      * Generates the needed html code for the paging bar.<p>
995      *
996      * @param wp the workplace instance
997      *
998      * @return html code
999      */

1000    private String JavaDoc htmlPagingBar(CmsWorkplace wp) {
1001
1002        if (getNumberOfPages() < 2) {
1003            return "";
1004        }
1005        StringBuffer JavaDoc html = new StringBuffer JavaDoc(1024);
1006        CmsMessages messages = Messages.get().getBundle(wp.getLocale());
1007        html.append("<table width='100%' cellspacing='0' style='margin-top: 5px;'>\n");
1008        html.append("\t<tr>\n");
1009        html.append("\t\t<td class='main'>\n");
1010        // prev button
1011
String JavaDoc id = "listPrev";
1012        String JavaDoc name = messages.key(Messages.GUI_LIST_PAGING_PREVIOUS_NAME_0);
1013        String JavaDoc iconPath = ICON_LEFT;
1014        boolean enabled = getCurrentPage() > 1;
1015        String JavaDoc helpText = messages.key(Messages.GUI_LIST_PAGING_PREVIOUS_HELP_0);
1016        if (!enabled) {
1017            helpText = messages.key(Messages.GUI_LIST_PAGING_PREVIOUS_HELPDIS_0);
1018        }
1019        String JavaDoc onClic = "listSetPage('" + getId() + "', " + (getCurrentPage() - 1) + ")";
1020        html.append(A_CmsHtmlIconButton.defaultButtonHtml(
1021            wp.getJsp(),
1022            CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
1023            id,
1024            name,
1025            helpText,
1026            enabled,
1027            iconPath,
1028            null,
1029            onClic));
1030        html.append("\n");
1031        // next button
1032
id = "listNext";
1033        name = messages.key(Messages.GUI_LIST_PAGING_NEXT_NAME_0);
1034        iconPath = ICON_RIGHT;
1035        enabled = getCurrentPage() < getNumberOfPages();
1036        helpText = messages.key(Messages.GUI_LIST_PAGING_NEXT_HELP_0);
1037        if (!enabled) {
1038            helpText = messages.key(Messages.GUI_LIST_PAGING_NEXT_HELPDIS_0);
1039        }
1040        onClic = "listSetPage('" + getId() + "', " + (getCurrentPage() + 1) + ")";
1041        html.append(A_CmsHtmlIconButton.defaultButtonHtml(
1042            wp.getJsp(),
1043            CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
1044            id,
1045            name,
1046            helpText,
1047            enabled,
1048            iconPath,
1049            null,
1050            onClic));
1051        html.append("\n");
1052        // page selection list
1053
html.append("\t\t\t&nbsp;&nbsp;&nbsp;");
1054        html.append("\t\t\t<select name='listPageSet' id='id-page_set' onChange =\"listSetPage('");
1055        html.append(getId());
1056        html.append("', this.value);\" style='vertical-align: bottom;'>\n");
1057        for (int i = 0; i < getNumberOfPages(); i++) {
1058            int displayedFrom = i * getMaxItemsPerPage() + 1;
1059            int displayedTo = (i + 1) * getMaxItemsPerPage() < getSize() ? (i + 1) * getMaxItemsPerPage() : getSize();
1060            html.append("\t\t\t\t<option value='");
1061            html.append(i + 1);
1062            html.append("'");
1063            html.append((i + 1) == getCurrentPage() ? " selected" : "");
1064            html.append(">");
1065            html.append(displayedFrom);
1066            html.append(" - ");
1067            html.append(displayedTo);
1068            html.append("</option>\n");
1069        }
1070        html.append("\t\t\t</select>\n");
1071        html.append("\t\t\t&nbsp;&nbsp;&nbsp;");
1072        if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_searchFilter)) {
1073            html.append(messages.key(
1074                Messages.GUI_LIST_PAGING_TEXT_2,
1075                new Object JavaDoc[] {m_name.key(wp.getLocale()), new Integer JavaDoc(getTotalSize())}));
1076        } else {
1077            html.append(messages.key(
1078                Messages.GUI_LIST_PAGING_FILTER_TEXT_3,
1079                new Object JavaDoc[] {m_name.key(wp.getLocale()), new Integer JavaDoc(getSize()), new Integer JavaDoc(getTotalSize())}));
1080        }
1081        html.append("\t\t</td>\n");
1082        html.append("\t</tr>\n");
1083        html.append("</table>\n");
1084        return html.toString();
1085    }
1086
1087    /**
1088     * returns the html for the title of the list.<p>
1089     *
1090     * @param wp the workplace context
1091     *
1092     * @return html code
1093     */

1094    private String JavaDoc htmlTitle(CmsWorkplace wp) {
1095
1096        StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
1097        CmsMessages messages = Messages.get().getBundle(wp.getLocale());
1098        html.append("<table width='100%' cellspacing='0'>");
1099        html.append("\t<tr>\n");
1100        html.append("\t\t<td align='left'>\n");
1101        html.append("\t\t\t");
1102        if (getTotalNumberOfPages() > 1) {
1103            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_searchFilter)) {
1104                html.append(messages.key(Messages.GUI_LIST_TITLE_TEXT_4, new Object JavaDoc[] {
1105                    m_name.key(wp.getLocale()),
1106                    new Integer JavaDoc(displayedFrom()),
1107                    new Integer JavaDoc(displayedTo()),
1108                    new Integer JavaDoc(getTotalSize())}));
1109            } else {
1110                html.append(messages.key(Messages.GUI_LIST_TITLE_FILTERED_TEXT_5, new Object JavaDoc[] {
1111                    m_name.key(wp.getLocale()),
1112                    new Integer JavaDoc(displayedFrom()),
1113                    new Integer JavaDoc(displayedTo()),
1114                    new Integer JavaDoc(getSize()),
1115                    new Integer JavaDoc(getTotalSize())}));
1116            }
1117        } else {
1118            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_searchFilter)) {
1119                html.append(messages.key(Messages.GUI_LIST_SINGLE_TITLE_TEXT_2, new Object JavaDoc[] {
1120                    m_name.key(wp.getLocale()),
1121                    new Integer JavaDoc(getTotalSize())}));
1122            } else {
1123                html.append(messages.key(Messages.GUI_LIST_SINGLE_TITLE_FILTERED_TEXT_3, new Object JavaDoc[] {
1124                    m_name.key(wp.getLocale()),
1125                    new Integer JavaDoc(getSize()),
1126                    new Integer JavaDoc(getTotalSize())}));
1127            }
1128        }
1129        html.append("\n");
1130        html.append("\t\t</td>\n\t\t");
1131        html.append(getMetadata().htmlActionBar(wp));
1132        html.append("\n\t</tr>\n");
1133        html.append("</table>\n");
1134        return html.toString();
1135    }
1136
1137    /**
1138     * Returns the html code for the toolbar (search bar + multiactions bar).<p>
1139     *
1140     * @param wp the workplace context
1141     *
1142     * @return html code
1143     */

1144    private String JavaDoc htmlToolBar(CmsWorkplace wp) {
1145
1146        StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
1147        html.append("<table width='100%' cellspacing='0' style='margin-bottom: 5px'>\n");
1148        html.append("\t<tr>\n");
1149        html.append(m_metadata.htmlSearchBar(wp));
1150        html.append(m_metadata.htmlMultiActionBar(wp));
1151        html.append("\t</tr>\n");
1152        html.append("</table>\n");
1153        return html.toString();
1154    }
1155}
Popular Tags