KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SList


1 /*
2  * $Id: SList.java,v 1.17 2005/05/27 09:17:36 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.wings.plaf.ListCG;
17 import org.wings.style.CSSSelector;
18 import org.wings.style.CSSStyleSheet;
19 import org.wings.style.CSSProperty;
20 import org.wings.style.CSSAttributeSet;
21
22 import javax.swing.event.EventListenerList JavaDoc;
23 import javax.swing.event.ListDataListener JavaDoc;
24 import javax.swing.event.ListSelectionEvent JavaDoc;
25 import javax.swing.event.ListSelectionListener JavaDoc;
26 import javax.swing.*;
27 import java.io.Serializable JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.awt.*;
31
32 /**
33  * <em>CAVEAT</em>
34  * A list in a form has special implications to take care of:
35  * Problem with a form request
36  * is, that we should fire the selection change events not until the states
37  * of all components are consistent. Unfortunately we cannot avoid events
38  * before that
39  * entirely. Problem is, that we use Swing Models for selection and they
40  * don't know anything about asynchronous state change. They will fire their
41  * events just after we set a state. But inside a form we have to change
42  * many states of many components, all at once. And events should arise
43  * first, after we set the new state of all components. So as a trade-off we
44  * decided to use the setValueIsAdjusting in the ListSelectionModel as an
45  * indicator,
46  * if components are consistent. That is, if you get an SelectionEvent with
47  * getValueIsAdjusting true, you cannot be sure, that component states are
48  * consistent, so don't use that events. But you will get an event without
49  * isValueAdjusting. You can work with that event. If you want to avoid that
50  * problem, just use the selection events from the list itself, register your
51  * listener at SList rather than at the ListSelectionModel...
52  *
53  * @author <a HREF="mailto:hengels@mercatis.de">Holger Engels</a>
54  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
55  * @version $Revision: 1.17 $
56  * @see javax.swing.ListModel
57  * @see SDefaultListModel
58  * @see javax.swing.ListSelectionModel
59  * @see SListCellRenderer
60  */

61 public class SList extends SComponent implements Scrollable, LowLevelEventListener, ListDataListener JavaDoc {
62
63     /**
64      * The type for an ordered list. See {@link #setType(String)} and ORDER_TYPE_xxx
65      */

66     public static final String JavaDoc ORDERED_LIST = "ol";
67
68     /**
69      * The type for an unordered list. See {@link #setType(String)}
70      */

71     public static final String JavaDoc UNORDERED_LIST = "ul";
72
73     /**
74      * The type for an menu-like list. See {@link #setType(String)}
75      */

76     public static final String JavaDoc MENU_LIST = "menu";
77
78     /**
79      * The type for an TODO list. See {@link #setType(String)}
80      */

81     public static final String JavaDoc DIR_LIST = "dir";
82
83     /**
84      * Order type for for {@link #setOrderType(String)}
85      */

86     public static final String JavaDoc[] ORDER_TYPE_CIRCLE = {"ul", "circle"};
87     /**
88      * Order type for for {@link #setOrderType(String)}
89      */

90     public static final String JavaDoc[] ORDER_TYPE_SQUARE = {"ul", "square"};
91     /**
92      * Order type for for {@link #setOrderType(String)}
93      */

94     public static final String JavaDoc[] ORDER_TYPE_DISC = {"ul", "disc"};
95     /**
96      * Order type for for {@link #setOrderType(String)}
97      */

98     public static final String JavaDoc[] ORDER_TYPE_BIG_ALPHA = {"ol", "A"};
99     /**
100      * Order type for for {@link #setOrderType(String)}
101      */

102     public static final String JavaDoc[] ORDER_TYPE_SMALL_ALPHA = {"ol", "a"};
103     /**
104      * Order type for for {@link #setOrderType(String)}
105      */

106     public static final String JavaDoc[] ORDER_TYPE_NUMBER = {"ol", null};
107     /**
108      * Order type for for {@link #setOrderType(String)}
109      */

110     public static final String JavaDoc[] ORDER_TYPE_NORMAL = {"ul", null};
111     /**
112      * Order type for for {@link #setOrderType(String)}
113      */

114     public static final String JavaDoc[] ORDER_TYPE_BIG_ROMAN = {"ol", "I"};
115     /**
116      * Order type for for {@link #setOrderType(String)}
117      */

118     public static final String JavaDoc[] ORDER_TYPE_SMALL_ROMAN = {"ol", "i"};
119
120     /**
121      * Table selection model. See {@link SList#setSelectionMode(int)}
122      */

123     public static final int NO_SELECTION = SListSelectionModel.NO_SELECTION;
124     /**
125      * Table selection model. See {@link SList#setSelectionMode(int)}
126      */

127     public static final int SINGLE_SELECTION = SListSelectionModel.SINGLE_SELECTION;
128     /**
129      * Table selection model. See {@link SList#setSelectionMode(int)}
130      */

131     public static final int SINGLE_INTERVAL_SELECTION = SListSelectionModel.SINGLE_INTERVAL_SELECTION;
132     /**
133      * Table selection model. See {@link SList#setSelectionMode(int)}
134      */

135     public static final int MULTIPLE_SELECTION = SListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
136     /**
137      * Table selection model. See {@link SList#setSelectionMode(int)}
138      */

139     public static final int MULTIPLE_INTERVAL_SELECTION = SListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
140
141     /**
142      * The Selector for this component.
143      */

144     public static final CSSSelector SELECTOR_SELECTION = new CSSSelector.Pseudo("SELECTION");
145
146     /**
147      * The preferred extent of the list.
148      */

149     private int visibleRowCount = 8;
150
151
152     private SListSelectionModel selectionModel;
153
154
155     private ListModel dataModel;
156
157
158     private SListCellRenderer cellRenderer;
159
160
161     private ListSelectionHandler selectionHandler;
162
163     /**
164      * which extent of the component should be rendered
165      */

166     private Rectangle viewport = null;
167
168     /**
169      * @see LowLevelEventListener#isEpochCheckEnabled()
170      */

171     private boolean epochCheckEnabled = true;
172
173     /**
174      * <li type="...">
175      */

176     protected String JavaDoc type = UNORDERED_LIST;
177
178     /**
179      * <li type="...">
180      */

181     protected String JavaDoc orderType = null;
182
183     /**
184      * <li start="...">
185      */

186     protected int start = 0;
187
188     /**
189      * Construct a SList that displays the elements in the specified model.
190      */

191     public SList(ListModel dataModel) {
192         if (dataModel == null) {
193             throw new IllegalArgumentException JavaDoc("dataModel must not be null");
194         }
195
196         if (this.dataModel != null) this.dataModel.removeListDataListener(this);
197         this.dataModel = dataModel;
198         this.dataModel.addListDataListener(this);
199         selectionModel = createSelectionModel();
200     }
201
202
203     /**
204      * Construct a SList that displays the elements in the specified
205      * array.
206      */

207     public SList(final Object JavaDoc[] listData) {
208         this(new AbstractListModel() {
209             public int getSize() {
210                 return listData.length;
211             }
212
213             public Object JavaDoc getElementAt(int i) {
214                 return listData[i];
215             }
216         });
217     }
218
219
220     /**
221      * Construct a SList that displays the elements in the specified
222      * Vector.
223      */

224     public SList(final List JavaDoc listData) {
225         this(new AbstractListModel() {
226             public int getSize() {
227                 return listData.size();
228             }
229
230             public Object JavaDoc getElementAt(int i) {
231                 return listData.get(i);
232             }
233         });
234     }
235
236
237     /**
238      * Constructs a SList with an empty model.
239      */

240     public SList() {
241         this(new AbstractListModel() {
242             public int getSize() {
243                 return 0;
244             }
245
246             public Object JavaDoc getElementAt(int i) {
247                 return "No Data Model";
248             }
249         });
250     }
251
252     /**
253      * Returns the cell renderer.
254      *
255      * @return the ListCellRenderer
256      * @see #setCellRenderer
257      */

258     public final SListCellRenderer getCellRenderer() {
259         return cellRenderer;
260     }
261
262     /**
263      * Sets the renderer that's used to write out each cell in the list.
264      *
265      * @param cellRenderer the SListCellRenderer that paints list cells
266      * description: The component used to draw the cells.
267      * @see #getCellRenderer
268      */

269     public void setCellRenderer(SListCellRenderer cellRenderer) {
270         SListCellRenderer oldValue = this.cellRenderer;
271         this.cellRenderer = cellRenderer;
272         reloadIfChange(oldValue, cellRenderer);
273     }
274
275
276     /**
277      * Return the background color.
278      *
279      * @return the background color
280      */

281     public Color getSelectionBackground() {
282         return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getBackground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION));
283     }
284
285     /**
286      * Set the foreground color.
287      *
288      * @param color the new foreground color
289      */

290     public void setSelectionBackground(Color color) {
291         setAttribute(SELECTOR_SELECTION, CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color));
292     }
293
294     /**
295      * Return the foreground color.
296      *
297      * @return the foreground color
298      */

299     public Color getSelectionForeground() {
300         return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getForeground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION));
301     }
302
303     /**
304      * Set the foreground color.
305      *
306      * @param color the new foreground color
307      */

308     public void setSelectionForeground(Color color) {
309         setAttribute(SELECTOR_SELECTION, CSSProperty.COLOR, CSSStyleSheet.getAttribute(color));
310     }
311
312     /**
313      * Set the font.
314      *
315      * @param font the new font
316      */

317     public void setSelectionFont(SFont font) {
318         setAttributes(SELECTOR_SELECTION, CSSStyleSheet.getAttributes(font));
319     }
320
321     /**
322      * Return the font.
323      *
324      * @return the font
325      */

326     public SFont getSelectionFont() {
327         return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTION) == null ? null : CSSStyleSheet.getFont((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTION));
328     }
329
330     /**
331      * Return the preferred number of visible rows. If rendered as a form
332      * component it is used for the size-attribute.
333      *
334      * @return the preferred number of rows to display
335      * @see #setVisibleRowCount
336      */

337     public final int getVisibleRowCount() {
338         return visibleRowCount;
339     }
340
341     /**
342      * Set the preferred number of rows in the list that can be displayed
343      * without a scollbar.
344      * <p/>
345      * The default value of this property is 8.
346      *
347      * @param visibleRowCount the preferred number of visible rows
348      * description: The preferred number of cells that can be displayed without a scrollbar.
349      * @see #getVisibleRowCount
350      */

351     public void setVisibleRowCount(int visibleRowCount) {
352         if (this.visibleRowCount != visibleRowCount) {
353             this.visibleRowCount = Math.max(0, visibleRowCount);
354             reload();
355             //firePropertyChange("visibleRowCount", oldValue, visibleRowCount);
356
}
357     }
358
359
360     /**
361      * --- ListModel Support ---
362      */

363
364
365     /**
366      * Returns the data model that holds the items.
367      *
368      * @return the ListModel
369      * @see #setModel
370      */

371     public ListModel getModel() {
372         return dataModel;
373     }
374
375     /**
376      * Sets the model
377      *
378      * @param model the ListModel that provides the list of items
379      * description: The object that contains the data to be shownin the list.
380      * @see #getModel
381      */

382     public void setModel(ListModel model) {
383         if (model == null) {
384             throw new IllegalArgumentException JavaDoc("model must be non null");
385         }
386         if (isDifferent(dataModel, model)) {
387             clearSelection();
388             dataModel = model;
389             dataModel.addListDataListener(this);
390             reload();
391         }
392     }
393
394
395     /**
396      * A convenience method that constructs a ListModel from an array of Objects
397      * and then applies setModel to it.
398      *
399      * @param listData an array of Objects containing the items to display
400      * in the list
401      * @see #setModel
402      */

403     public void setListData(final Object JavaDoc[] listData) {
404         setModel(new AbstractListModel() {
405             public int getSize() {
406                 return listData.length;
407             }
408
409             public Object JavaDoc getElementAt(int i) {
410                 return listData[i];
411             }
412         });
413     }
414
415
416     /**
417      * A convenience method that constructs a ListModel from a List
418      * and then applies setModel to it.
419      *
420      * @param listData a Vector containing the items to display in the list
421      * @see #setModel
422      */

423     public void setListData(final List JavaDoc listData) {
424         setModel(new AbstractListModel() {
425             public int getSize() {
426                 return listData.size();
427             }
428
429             public Object JavaDoc getElementAt(int i) {
430                 return listData.get(i);
431             }
432         });
433     }
434
435     /**
436      * creates the default selection model. It uses the swing
437      * DefaultListSelectionModel, and wraps some methods to support
438      * {@link SListSelectionModel#NO_SELECTION}
439      */

440     protected SListSelectionModel createSelectionModel() {
441         return new SDefaultListSelectionModel();
442     }
443
444
445     /**
446      * Returns the current selection model. If selection mode is
447      * {@link SListSelectionModel#NO_SELECTION} it return <em>null</em>
448      *
449      * @return the ListSelectionModel that implements list selections.
450      * If selection mode is {@link SListSelectionModel#NO_SELECTION} it return
451      * <em>null</em>
452      * @see #setSelectionMode(int)
453      * @see ListSelectionModel
454      */

455     public SListSelectionModel getSelectionModel() {
456         return selectionModel;
457     }
458
459
460     /**
461      * This method notifies all ListSelectionListeners that
462      * the selection model has changed.
463      *
464      * @see #addListSelectionListener
465      * @see #removeListSelectionListener
466      * @see EventListenerList
467      */

468     protected void fireSelectionValueChanged(int firstIndex, int lastIndex,
469                                              boolean isAdjusting) {
470         Object JavaDoc[] listeners = getListenerList();
471         ListSelectionEvent JavaDoc e = null;
472
473         for (int i = listeners.length - 2; i >= 0; i -= 2) {
474             if (listeners[i] == ListSelectionListener JavaDoc.class) {
475                 if (e == null) {
476                     e = new ListSelectionEvent JavaDoc(this, firstIndex, lastIndex,
477                             isAdjusting);
478                 }
479                 ((ListSelectionListener JavaDoc) listeners[i + 1]).valueChanged(e);
480             }
481         }
482     }
483
484
485     /**
486      * A handler that forwards ListSelectionEvents from the selectionModel
487      * to the SList ListSelectionListeners.
488      */

489     private final class ListSelectionHandler
490             implements ListSelectionListener JavaDoc, Serializable JavaDoc {
491
492         public void valueChanged(ListSelectionEvent JavaDoc e) {
493             fireSelectionValueChanged(e.getFirstIndex(),
494                     e.getLastIndex(),
495                     e.getValueIsAdjusting());
496             reload();
497         }
498     }
499
500
501     /**
502      * Add a listener to the list that's notified each time a change
503      * to the selection occurs.
504      *
505      * @param listener A ListSelectionListener to be added
506      * @see #getSelectionModel
507      */

508     public void addListSelectionListener(ListSelectionListener JavaDoc listener) {
509         if (selectionHandler == null) {
510             selectionHandler = new ListSelectionHandler();
511             getSelectionModel().addListSelectionListener(selectionHandler);
512         }
513
514         addEventListener(ListSelectionListener JavaDoc.class, listener);
515     }
516
517
518     /**
519      * Remove a listener from the list that's notified each time a
520      * change to the selection occurs.
521      *
522      * @param listener The ListSelectionListener to remove.
523      * @see #addListSelectionListener
524      * @see #getSelectionModel
525      */

526     public void removeListSelectionListener(ListSelectionListener JavaDoc listener) {
527         removeEventListener(ListSelectionListener JavaDoc.class, listener);
528     }
529
530
531     /**
532      * Returns an array of all the <code>ListSelectionListener</code>s added
533      * to this JList with addListSelectionListener().
534      *
535      * @return all of the ListSelectionListener added
536      * @since 1.4
537      */

538     public ListSelectionListener JavaDoc[] getListSelectionListeners() {
539         return (ListSelectionListener JavaDoc[]) getListeners(ListSelectionListener JavaDoc.class);
540     }
541
542
543     /**
544      * Set the selectionModel for the list.
545      * The selection model keeps track of which items are selected.
546      * <p/>
547      * description: The selection model, recording which cells are selected.
548      *
549      * @see #getSelectionModel
550      */

551     public void setSelectionModel(SListSelectionModel selectionModel) {
552         if (selectionModel == null) {
553             throw new IllegalArgumentException JavaDoc("selectionModel must be non null");
554         }
555
556         if (selectionHandler != null) {
557             this.selectionModel.removeListSelectionListener(selectionHandler);
558             selectionModel.addListSelectionListener(selectionHandler);
559         }
560
561         //SListSelectionModel oldValue = this.selectionModel;
562
this.selectionModel = selectionModel;
563     }
564
565
566     /**
567      * Allow / permit multiple selection
568      * <ul>
569      * <li> <code>SINGLE_SELECTION</code>
570      * Only one list index can be selected at a time.
571      * <li> <code>MULTIPLE_INTERVAL_SELECTION</code>
572      * Multiple items can be selected.
573      * </ul>
574      *
575      * @param selectionMode single or multiple selections
576      * enum: SINGLE_SELECTION ListSelectionModel.SINGLE_SELECTION
577      * MULTIPLE_INTERVAL_SELECTION ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
578      * @see #getSelectionMode
579      */

580     public void setSelectionMode(int selectionMode) {
581         selectionModel.setSelectionMode(selectionMode);
582     }
583
584     /**
585      * Returns whether single-item or multiple-item selections are allowed.
586      *
587      * @return The value of the selectionMode property.
588      * @see #setSelectionMode
589      */

590     public int getSelectionMode() {
591         return getSelectionModel().getSelectionMode();
592     }
593
594
595     /**
596      * @return The index that most recently anchored an interval selection.
597      * @see ListSelectionModel#getAnchorSelectionIndex
598      * @see #addSelectionInterval
599      * @see #setSelectionInterval
600      * @see #addListSelectionListener
601      */

602     public int getAnchorSelectionIndex() {
603         return getSelectionModel().getAnchorSelectionIndex();
604     }
605
606
607     /**
608      * @return The index that most recently ended a interval selection.
609      * @see ListSelectionModel#getLeadSelectionIndex
610      * @see #addSelectionInterval
611      * @see #setSelectionInterval
612      * @see #addListSelectionListener
613      */

614     public int getLeadSelectionIndex() {
615         return getSelectionModel().getLeadSelectionIndex();
616     }
617
618
619     /**
620      * @return The smallest selected cell index.
621      * @see ListSelectionModel#getMinSelectionIndex
622      * @see #addListSelectionListener
623      */

624     public int getMinSelectionIndex() {
625         return getSelectionModel().getMinSelectionIndex();
626     }
627
628
629     /**
630      * @return The largest selected cell index.
631      * @see ListSelectionModel#getMaxSelectionIndex
632      * @see #addListSelectionListener
633      */

634     public int getMaxSelectionIndex() {
635         return getSelectionModel().getMaxSelectionIndex();
636     }
637
638
639     /**
640      * @return True if the specified index is selected.
641      * @see ListSelectionModel#isSelectedIndex
642      * @see #setSelectedIndex
643      * @see #addListSelectionListener
644      */

645     public boolean isSelectedIndex(int index) {
646         return getSelectionModel().isSelectedIndex(index);
647     }
648
649
650     /**
651      * @return True if nothing is selected
652      * @see ListSelectionModel#isSelectionEmpty
653      * @see #clearSelection
654      * @see #addListSelectionListener
655      */

656     public boolean isSelectionEmpty() {
657         return getSelectionModel().isSelectionEmpty();
658     }
659
660
661     /**
662      * @see ListSelectionModel#clearSelection
663      * @see #isSelectionEmpty
664      * @see #addListSelectionListener
665      */

666     public void clearSelection() {
667         if (!getSelectionModel().isSelectionEmpty()) {
668             getSelectionModel().clearSelection();
669             reload();
670         }
671     }
672
673
674     /**
675      * @param anchor The first index to select
676      * @param lead The last index to select
677      * @see ListSelectionModel#setSelectionInterval
678      * @see #addSelectionInterval
679      * @see #removeSelectionInterval
680      * @see #addListSelectionListener
681      */

682     public void setSelectionInterval(int anchor, int lead) {
683         getSelectionModel().setSelectionInterval(anchor, lead);
684     }
685
686
687     /**
688      * @param anchor The first index to add to the selection
689      * @param lead The last index to add to the selection
690      * @see ListSelectionModel#addSelectionInterval
691      * @see #setSelectionInterval
692      * @see #removeSelectionInterval
693      * @see #addListSelectionListener
694      */

695     public void addSelectionInterval(int anchor, int lead) {
696         getSelectionModel().addSelectionInterval(anchor, lead);
697     }
698
699
700     /**
701      * @param index0 The first index to remove from the selection
702      * @param index1 The last index to remove from the selection
703      * @see ListSelectionModel#removeSelectionInterval
704      * @see #setSelectionInterval
705      * @see #addSelectionInterval
706      * @see #addListSelectionListener
707      */

708     public void removeSelectionInterval(int index0, int index1) {
709         getSelectionModel().removeSelectionInterval(index0, index1);
710     }
711
712
713     /**
714      * @param b the value for valueIsAdjusting
715      * @see ListSelectionModel#setValueIsAdjusting
716      */

717     public void setValueIsAdjusting(boolean b) {
718         getSelectionModel().setValueIsAdjusting(b);
719     }
720
721     /**
722      * @return the value of valueIsAdjusting
723      * @see ListSelectionModel#getValueIsAdjusting
724      */

725     public boolean getValueIsAdjusting() {
726         return getSelectionModel().getValueIsAdjusting();
727     }
728
729
730     /**
731      * Return an array of all of the selected indices.
732      *
733      * @return all selected indices.
734      * @see #removeSelectionInterval
735      * @see #addListSelectionListener
736      */

737     public int[] getSelectedIndices() {
738         ListSelectionModel sm = getSelectionModel();
739         int iMin = sm.getMinSelectionIndex();
740         int iMax = sm.getMaxSelectionIndex();
741
742         if ((iMin < 0) || (iMax < 0)) {
743             return new int[0];
744         }
745
746         int[] rvTmp = new int[1 + (iMax - iMin)];
747         int n = 0;
748         for (int i = iMin; i <= iMax; i++) {
749             if (sm.isSelectedIndex(i)) {
750                 rvTmp[n++] = i;
751             }
752         }
753         int[] rv = new int[n];
754         System.arraycopy(rvTmp, 0, rv, 0, n);
755         return rv;
756     }
757
758
759     /**
760      * Select a single cell.
761      *
762      * @param index The index of the one cell to select
763      * @see ListSelectionModel#setSelectionInterval
764      * @see #isSelectedIndex
765      * @see #addListSelectionListener
766      */

767     public void setSelectedIndex(int index) {
768         getSelectionModel().setSelectionInterval(index, index);
769     }
770
771
772     /**
773      * Select some cells.
774      *
775      * @param indices The indices of the cells to select
776      * @see ListSelectionModel#addSelectionInterval
777      * @see #isSelectedIndex
778      * @see #addListSelectionListener
779      */

780     public void setSelectedIndices(int[] indices) {
781         ListSelectionModel sm = getSelectionModel();
782         sm.clearSelection();
783         for (int i = 0; i < indices.length; i++) {
784             sm.addSelectionInterval(indices[i], indices[i]);
785         }
786     }
787
788
789     /**
790      * Return the values of the selected cells.
791      * Returns only the selected elements which are in the model.
792      * If the selection model indices a selection outside the the datamodel it is ignored
793      *
794      * @return the selected values
795      * @see #isSelectedIndex
796      * @see #getModel
797      * @see #addListSelectionListener
798      */

799     public Object JavaDoc[] getSelectedValues() {
800         ListSelectionModel sm = getSelectionModel();
801         ListModel dm = getModel();
802
803         int iMin = sm.getMinSelectionIndex();
804         int iMax = sm.getMaxSelectionIndex();
805
806         if ((iMin < 0) || (iMax < 0)) {
807             return new Object JavaDoc[0];
808         }
809
810         Object JavaDoc[] rvTmp = new Object JavaDoc[1 + (iMax - iMin)];
811         int n = 0;
812         for (int i = iMin; i <= iMax; i++) {
813             if (sm.isSelectedIndex(i) && i < dm.getSize()) {
814                 rvTmp[n++] = dm.getElementAt(i);
815             }
816         }
817         Object JavaDoc[] rv = new Object JavaDoc[n];
818         System.arraycopy(rvTmp, 0, rv, 0, n);
819         return rv;
820     }
821
822
823     /**
824      * A convenience method that returns the first selected index.
825      *
826      * @return The first selected index.
827      * @see #getMinSelectionIndex
828      * @see #addListSelectionListener
829      */

830     public int getSelectedIndex() {
831         return getMinSelectionIndex();
832     }
833
834
835     /**
836      * A convenience method that returns the first selected value
837      * or null, if nothing is selected.
838      *
839      * @return The first selected value.
840      * @see #getMinSelectionIndex
841      * @see #getModel
842      * @see #addListSelectionListener
843      */

844     public Object JavaDoc getSelectedValue() {
845         int i = getMinSelectionIndex();
846         return (i == -1) ? null : getModel().getElementAt(i);
847     }
848
849
850     /**
851      * Selects the specified object.
852      *
853      * @param anObject the Object to be selected
854      */

855     public void setSelectedValue(Object JavaDoc anObject) {
856         if (anObject == null)
857             setSelectedIndex(-1);
858         else if (!anObject.equals(getSelectedValue())) {
859             int i, c;
860             ListModel dm = getModel();
861             for (i = 0, c = dm.getSize(); i < c; i++)
862                 if (anObject.equals(dm.getElementAt(i))) {
863                     setSelectedIndex(i);
864                     return;
865                 }
866             setSelectedIndex(-1);
867         }
868     }
869
870
871     /*
872      * Sets the list type. Use one of the following types:
873      * <UL>
874      * <LI>{@link SConstants#ORDERED_LIST}
875      * <LI>{@link SConstants#UNORDERED_LIST}
876      * <LI>{@link SConstants#MENU_LIST}
877      * <LI>{@link SConstants#DIR_LIST}
878      * </UL>
879      * null sets default list.
880      *
881      * @param t the type
882      */

883     public void setType(String JavaDoc t) {
884         if (t != null)
885             type = t;
886         else
887             type = UNORDERED_LIST;
888     }
889
890     /**
891      * Return the type.
892      *
893      * @return the type;
894      */

895     public String JavaDoc getType() {
896         return type;
897     }
898
899     /**
900      * <li type="...">
901      */

902     public void setOrderType(String JavaDoc t) {
903         orderType = t;
904     }
905
906     /**
907      * <li type="...">
908      */

909     public String JavaDoc getOrderType() {
910         return orderType;
911     }
912
913     /*
914      * <li type="...">
915      * <code>null</code> is default style.
916      */

917     public void setType(String JavaDoc[] t) {
918         if (t == null) {
919             setType((String JavaDoc) null);
920             setOrderType(null);
921         } else if (t.length == 2) {
922             setType(t[0]);
923             setOrderType(t[1]);
924         }
925     }
926
927     /**
928      * <li start="...">
929      */

930     public void setStart(int s) {
931         start = s;
932     }
933
934     /**
935      * <li start="...">
936      */

937     public int getStart() {
938         return start;
939     }
940
941     public void fireIntermediateEvents() {
942         getSelectionModel().fireDelayedIntermediateEvents();
943     }
944
945     public void fireFinalEvents() {
946         super.fireFinalEvents();
947         // fire selection events...
948
getSelectionModel().fireDelayedFinalEvents();
949     }
950
951     /**
952      * @see LowLevelEventListener#isEpochCheckEnabled()
953      */

954     public boolean isEpochCheckEnabled() {
955         return epochCheckEnabled;
956     }
957
958     /**
959      * @see LowLevelEventListener#isEpochCheckEnabled()
960      */

961     public void setEpochCheckEnabled(boolean epochCheckEnabled) {
962         this.epochCheckEnabled = epochCheckEnabled;
963     }
964
965     /*
966      * Implement {@link LowLevelEventListener} interface.
967      * @param action the name
968      * @param value the value
969      */

970     public void processLowLevelEvent(String JavaDoc action, String JavaDoc[] values) {
971         processKeyEvents(values);
972
973         // delay events...
974
getSelectionModel().setDelayEvents(true);
975
976         getSelectionModel().setValueIsAdjusting(true);
977         // in a form, we only get events for selected items, so for every
978
// selected item, which is not in values, deselect it...
979
if (getShowAsFormComponent()) {
980
981             ArrayList JavaDoc selectedIndices = new ArrayList JavaDoc();
982             for (int i = 0; i < values.length; i++) {
983
984
985                 if (values[i].length() < 2) continue; // false format
986

987                 String JavaDoc indexString = values[i].substring(1);
988                 try {
989                     int index = Integer.parseInt(indexString);
990                     
991                     // in a form all parameters are select parameters...
992
if (values[i].charAt(0) == 'a') {
993                         selectedIndices.add(new Integer JavaDoc(index));
994                         addSelectionInterval(index, index);
995                     }
996                 } catch (Exception JavaDoc ex) {
997                     // ignore, this is not the correct format...
998
}
999             }
1000            // remove all selected indices, which are not explicitely selected
1001
// by a parameter
1002
for (int i = 0; i < getModel().getSize(); i++) {
1003                if (isSelectedIndex(i) &&
1004                        !selectedIndices.contains(new Integer JavaDoc(i))) {
1005                    removeSelectionInterval(i, i);
1006                }
1007            }
1008        } else {
1009
1010            for (int i = 0; i < values.length; i++) {
1011
1012                if (values[i].length() < 2) continue; // false format
1013

1014                // first char is select/deselect operator
1015
String JavaDoc indexString = values[i].substring(1);
1016                try {
1017                    int index = Integer.parseInt(indexString);
1018
1019                    if (values[i].charAt(0) == 'a') {
1020                        addSelectionInterval(index, index);
1021                    } else if (values[i].charAt(0) == 'r') {
1022                        removeSelectionInterval(index, index);
1023                    } // else ignore, this is not the correct format...
1024
} catch (Exception JavaDoc ex) {
1025                }
1026
1027            }
1028        }
1029        getSelectionModel().setValueIsAdjusting(false);
1030
1031
1032        getSelectionModel().setDelayEvents(false);
1033
1034        SForm.addArmedComponent(this);
1035    }
1036
1037    //-- Scrollable interface
1038
/**
1039     * Return the scrollable viewport size.
1040     *
1041     * @return the scrollable viewport dimension
1042     */

1043    public Rectangle getScrollableViewportSize() {
1044        return new Rectangle(0, 0, 1, dataModel.getSize());
1045    }
1046
1047    /**
1048     * Set the visible viewport size.
1049     *
1050     * @param d the visible viewport size
1051     */

1052    public void setViewportSize(Rectangle d) {
1053        if (isDifferent(viewport, d)) {
1054            viewport = d;
1055            reload();
1056        }
1057    }
1058
1059    /**
1060     * Return the visible viewport size.
1061     *
1062     * @return the visible viewport size
1063     */

1064    public final Rectangle getViewportSize() {
1065        return viewport;
1066    }
1067
1068    public Dimension getPreferredExtent() {
1069        return new Dimension(1, Math.min(getVisibleRowCount(), getModel().getSize()));
1070    }
1071    //--- /Scrollable Interface
1072

1073    public void setParent(SContainer p) {
1074        super.setParent(p);
1075        if (getCellRendererPane() != null)
1076            getCellRendererPane().setParent(p);
1077    }
1078
1079    protected void setParentFrame(SFrame f) {
1080        super.setParentFrame(f);
1081        if (getCellRendererPane() != null)
1082            getCellRendererPane().setParentFrame(f);
1083    }
1084
1085
1086    // do not initalize with null!
1087
private SCellRendererPane cellRendererPane = new SCellRendererPane();
1088
1089
1090    public SCellRendererPane getCellRendererPane() {
1091        return cellRendererPane;
1092    }
1093
1094
1095    public void removeCellRendererPane() {
1096        cellRendererPane.setParent(null);
1097        cellRendererPane = null;
1098    }
1099
1100    public void setCG(ListCG cg) {
1101        super.setCG(cg);
1102    }
1103
1104    public String JavaDoc getToggleSelectionParameter(int index) {
1105        return isSelectedIndex(index) ? getDeselectionParameter(index) :
1106                getSelectionParameter(index);
1107    }
1108
1109    public String JavaDoc getSelectionParameter(int index) {
1110        return "a" + Integer.toString(index);
1111    }
1112
1113    public String JavaDoc getDeselectionParameter(int index) {
1114        return "r" + Integer.toString(index);
1115    }
1116
1117    /**
1118     * *
1119     * Changes of the List Model should reflect in a reload if possible
1120     */

1121
1122    public void contentsChanged(javax.swing.event.ListDataEvent JavaDoc e) {
1123        reload();
1124    }
1125
1126    public void intervalAdded(javax.swing.event.ListDataEvent JavaDoc e) {
1127        reload();
1128    }
1129
1130    public void intervalRemoved(javax.swing.event.ListDataEvent JavaDoc e) {
1131        reload();
1132    }
1133
1134}
1135
1136
1137
Popular Tags