/[classpath]/classpath/javax/swing/JList.java
ViewVC logotype

Diff of /classpath/javax/swing/JList.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.16.2.11 by gnu_andrew, Sat Sep 10 15:31:48 2005 UTC revision 1.16.2.12 by gnu_andrew, Tue Sep 20 18:46:31 2005 UTC
# Line 41  package javax.swing; Line 41  package javax.swing;
41  import java.awt.Color;  import java.awt.Color;
42  import java.awt.Component;  import java.awt.Component;
43  import java.awt.ComponentOrientation;  import java.awt.ComponentOrientation;
44    import java.awt.Cursor;
45  import java.awt.Dimension;  import java.awt.Dimension;
46    import java.awt.Font;
47    import java.awt.FontMetrics;
48  import java.awt.Point;  import java.awt.Point;
49  import java.awt.Rectangle;  import java.awt.Rectangle;
50    import java.awt.event.FocusListener;
51    import java.beans.PropertyChangeEvent;
52    import java.beans.PropertyChangeListener;
53    import java.util.Locale;
54  import java.util.Vector;  import java.util.Vector;
55    
56  import javax.accessibility.Accessible;  import javax.accessibility.Accessible;
57    import javax.accessibility.AccessibleComponent;
58  import javax.accessibility.AccessibleContext;  import javax.accessibility.AccessibleContext;
59    import javax.accessibility.AccessibleRole;
60    import javax.accessibility.AccessibleSelection;
61    import javax.accessibility.AccessibleState;
62    import javax.accessibility.AccessibleStateSet;
63  import javax.swing.event.ListDataEvent;  import javax.swing.event.ListDataEvent;
64  import javax.swing.event.ListDataListener;  import javax.swing.event.ListDataListener;
65  import javax.swing.event.ListSelectionEvent;  import javax.swing.event.ListSelectionEvent;
# Line 108  import javax.swing.text.Position; Line 120  import javax.swing.text.Position;
120    
121  public class JList extends JComponent implements Accessible, Scrollable  public class JList extends JComponent implements Accessible, Scrollable
122  {  {
123    
124      /**
125       * Provides accessibility support for <code>JList</code>.
126       */
127      protected class AccessibleJList extends AccessibleJComponent
128        implements AccessibleSelection, PropertyChangeListener,
129                   ListSelectionListener, ListDataListener
130      {
131    
132        /**
133         * Provides accessibility support for list elements in <code>JList</code>s.
134         */
135        protected class AccessibleJListChild extends AccessibleContext
136          implements Accessible, AccessibleComponent
137        {
138    
139          /**
140           * The parent list.
141           */
142          JList parent;
143    
144          /**
145           * The index in the list for that child.
146           */
147          int listIndex;
148    
149          /**
150           * The cursor for this list child.
151           */
152          // TODO: Testcases show that this class somehow stores state about the
153          // cursor. I cannot make up though how that could affect
154          // the actual list.
155          Cursor cursor = Cursor.getDefaultCursor();
156    
157          /**
158           * Creates a new instance of <code>AccessibleJListChild</code>.
159           *
160           * @param list the list of which this is an accessible child
161           * @param index the list index for this child
162           */
163          public AccessibleJListChild(JList list, int index)
164          {
165            parent = list;
166            listIndex = index;
167          }
168    
169          /**
170           * Returns the accessible context of this object. Returns
171           * <code>this</code> since <code>AccessibleJListChild</code>s are their
172           * own accessible contexts.
173           *
174           * @return the accessible context of this object, <code>this</code>
175           */
176          public AccessibleContext getAccessibleContext()
177          {
178            return this;
179          }
180    
181          /**
182           * Returns the background color for this list child. This returns the
183           * background of the <code>JList</code> itself since the background
184           * cannot be set on list children individually
185           *
186           * @return the background color for this list child
187           */
188          public Color getBackground()
189          {
190            return parent.getBackground();
191          }
192    
193          /**
194           * Calling this method has no effect, since the background color cannot be
195           * set on list children individually.
196           *
197           * @param color not used here.
198           */
199          public void setBackground(Color color)
200          {
201            // Calling this method has no effect, since the background color cannot
202            // be set on list children individually.
203          }
204    
205          /**
206           * Returns the foreground color for this list child. This returns the
207           * background of the <code>JList</code> itself since the foreground
208           * cannot be set on list children individually.
209           *
210           * @return the background color for this list child
211           */
212          public Color getForeground()
213          {
214            return parent.getForeground();
215          }
216    
217          /**
218           * Calling this method has no effect, since the foreground color cannot be
219           * set on list children individually.
220           *
221           * @param color not used here.
222           */
223          public void setForeground(Color color)
224          {
225            // Calling this method has no effect, since the foreground color cannot
226            // be set on list children individually.
227          }
228    
229          /**
230           * Returns the cursor for this list child.
231           *
232           * @return the cursor for this list child
233           */
234          public Cursor getCursor()
235          {
236            // TODO: Testcases show that this method returns the cursor that has
237            // been set by setCursor. I cannot make up though how that could affect
238            // the actual list.
239            return cursor;
240          }
241    
242          /**
243           * Sets the cursor for this list child.
244           */
245          public void setCursor(Cursor cursor)
246          {
247            this.cursor = cursor;
248            // TODO: Testcases show that this method returns the cursor that has
249            // been set by setCursor. I cannot make up though how that could affect
250            // the actual list.
251          }
252    
253          /**
254           * Returns the font of the <code>JList</code> since it is not possible to
255           * set fonts for list children individually.
256           *
257           * @return the font of the <code>JList</code>
258           */
259          public Font getFont()
260          {
261            return parent.getFont();
262          }
263    
264          /**
265           * Does nothing since it is not possible to set the font on list children
266           * individually.
267           *
268           * @param font not used here
269           */
270          public void setFont(Font font)
271          {
272            // Does nothing since it is not possible to set the font on list
273            // children individually.
274          }
275    
276          /**
277           * Returns the font metrics for the specified font. This method forwards
278           * to the parent <code>JList</code>.
279           *
280           * @param font the font for which the font metrics is queried
281           *
282           * @return the font metrics for the specified font
283           */
284          public FontMetrics getFontMetrics(Font font)
285          {
286            return parent.getFontMetrics(font);
287          }
288    
289          /**
290           * Returns <code>true</code> if the parent <code>JList</code> is enabled,
291           * <code>false</code> otherwise. The list children cannot have an enabled
292           * flag set individually.
293           *
294           * @return <code>true</code> if the parent <code>JList</code> is enabled,
295           *         <code>false</code> otherwise
296           */
297          public boolean isEnabled()
298          {
299            return parent.isEnabled();
300          }
301    
302          /**
303           * Does nothing since the enabled flag cannot be set for list children
304           * individually.
305           *
306           * @param b not used here
307           */
308          public void setEnabled(boolean b)
309          {
310            // Does nothing since the enabled flag cannot be set for list children
311            // individually.
312          }
313    
314          /**
315           * Returns <code>true</code> if this list child is visible,
316           * <code>false</code> otherwise. The value of this property depends
317           * on {@link JList#getFirstVisibleIndex()} and
318           * {@link JList#getLastVisibleIndex()}.
319           *
320           * @return <code>true</code> if this list child is visible,
321           *         <code>false</code> otherwise
322           */
323          public boolean isVisible()
324          {
325            return listIndex >= parent.getFirstVisibleIndex()
326                   && listIndex <= parent.getLastVisibleIndex();
327          }
328    
329          /**
330           * The value of the visible property cannot be modified, so this method
331           * does nothing.
332           *
333           * @param b not used here
334           */
335          public void setVisible(boolean b)
336          {
337            // The value of the visible property cannot be modified, so this method
338            // does nothing.
339          }
340    
341          /**
342           * Returns <code>true</code> if this list child is currently showing on
343           * screen and <code>false</code> otherwise. The list child is showing if
344           * it is visible and if it's parent JList is currently showing.
345           *
346           * @return <code>true</code> if this list child is currently showing on
347           *         screen and <code>false</code> otherwise
348           */
349          public boolean isShowing()
350          {
351            return isVisible() && parent.isShowing();
352          }
353    
354          /**
355           * Returns <code>true</code> if this list child covers the screen location
356           * <code>point</code> (relative to the <code>JList</code> coordinate
357           * system, <code>false</code> otherwise.
358           *
359           * @return <code>true</code> if this list child covers the screen location
360           *         <code>point</code> , <code>false</code> otherwise
361           */
362          public boolean contains(Point point)
363          {
364            return getBounds().contains(point);
365          }
366    
367          /**
368           * Returns the absolute screen location of this list child.
369           *
370           * @return the absolute screen location of this list child
371           */
372          public Point getLocationOnScreen()
373          {
374            Point loc = getLocation();
375            SwingUtilities.convertPointToScreen(loc, parent);
376            return loc;
377          }
378    
379          /**
380           * Returns the screen location of this list child relative to it's parent.
381           *
382           * @return the location of this list child relative to it's parent
383           *
384           * @see JList#indexToLocation(int)
385           */
386          public Point getLocation()
387          {
388            return parent.indexToLocation(listIndex);
389          }
390    
391          /**
392           * Does nothing since the screen location cannot be set on list children
393           * explictitly.
394           *
395           * @param point not used here
396           */
397          public void setLocation(Point point)
398          {
399            // Does nothing since the screen location cannot be set on list children
400            // explictitly.
401          }
402    
403          /**
404           * Returns the bounds of this list child.
405           *
406           * @return the bounds of this list child
407           *
408           * @see JList#getCellBounds(int, int)
409           */
410          public Rectangle getBounds()
411          {
412            return parent.getCellBounds(listIndex, listIndex);
413          }
414    
415          /**
416           * Does nothing since the bounds cannot be set on list children
417           * individually.
418           *
419           * @param rectangle not used here
420           */
421          public void setBounds(Rectangle rectangle)
422          {
423            // Does nothing since the bounds cannot be set on list children
424            // individually.
425          }
426    
427          /**
428           * Returns the size of this list child.
429           *
430           * @return the size of this list child
431           */
432          public Dimension getSize()
433          {
434            Rectangle b = getBounds();
435            return b.getSize();
436          }
437    
438          /**
439           * Does nothing since the size cannot be set on list children
440           * individually.
441           *
442           * @param dimension not used here
443           */
444          public void setSize(Dimension dimension)
445          {
446            // Does nothing since the size cannot be set on list children
447            // individually.
448          }
449    
450          /**
451           * Returns <code>null</code> because list children do not have children
452           * themselves
453           *
454           * @return <code>null</code>
455           */
456          public Accessible getAccessibleAt(Point point)
457          {
458            return null;
459          }
460    
461          /**
462           * Returns <code>true</code> since list children are focus traversable.
463           *
464           * @return true
465           */
466          public boolean isFocusTraversable()
467          {
468            // TODO: Is this 100% ok?
469            return true;
470          }
471    
472          /**
473           * Requests focus on the parent list. List children cannot request focus
474           * individually.
475           */
476          public void requestFocus()
477          {
478            // TODO: Is this 100% ok?
479            parent.requestFocus();
480          }
481    
482          /**
483           * Adds a focus listener to the parent list. List children do not have
484           * their own focus management.
485           *
486           * @param listener the focus listener to add
487           */
488          public void addFocusListener(FocusListener listener)
489          {
490            // TODO: Is this 100% ok?
491            parent.addFocusListener(listener);
492          }
493    
494          /**
495           * Removes a focus listener from the parent list. List children do not
496           * have their own focus management.
497           *
498           * @param listener the focus listener to remove
499           */
500          public void removeFocusListener(FocusListener listener)
501          {
502            // TODO: Is this 100%
503            parent.removeFocusListener(listener);
504          }
505    
506          /**
507           * Returns the accessible role of this list item, which is
508           * {@link AccessibleRole#LABEL}.
509           *
510           * @return {@link AccessibleRole#LABEL}
511           */
512          public AccessibleRole getAccessibleRole()
513          {
514            return AccessibleRole.LABEL;
515          }
516    
517          /**
518           * Returns the accessible state set of this list item.
519           *
520           * @return the accessible state set of this list item
521           */
522          public AccessibleStateSet getAccessibleStateSet()
523          {
524            AccessibleStateSet states = new AccessibleStateSet();
525            if (isVisible())
526              states.add(AccessibleState.VISIBLE);
527            if (isShowing())
528              states.add(AccessibleState.SHOWING);
529            if (isFocusTraversable())
530              states.add(AccessibleState.FOCUSABLE);
531            // TODO: How should the active state be handled? The API docs
532            // suggest that this state is set on the activated list child,
533            // that is the one that is drawn with a box. However, I don't know how
534            // to implement this.
535    
536            // TODO: We set the selectable state here because list children are
537            // selectable. Is there a way to disable single children?
538            if (parent.isEnabled())
539              states.add(AccessibleState.SELECTABLE);
540    
541            if (parent.isSelectedIndex(listIndex))
542              states.add(AccessibleState.SELECTED);
543    
544            // TODO: Handle more states here?
545            return states;
546          }
547    
548          /**
549           * Returns the index of this list child within it's parent list.
550           *
551           * @return the index of this list child within it's parent list
552           */
553          public int getAccessibleIndexInParent()
554          {
555            return listIndex;
556          }
557    
558          /**
559           * Returns <code>0</code> since list children don't have children
560           * themselves.
561           *
562           * @return <code>0</code>
563           */
564          public int getAccessibleChildrenCount()
565          {
566            return 0;
567          }
568    
569          /**
570           * Returns <code>null</code> since list children don't have children
571           * themselves.
572           *
573           * @return <code>null</code>
574           */
575          public Accessible getAccessibleChild(int i)
576          {
577            return null;
578          }
579    
580          /**
581           * Returns the locale of this component. This call is forwarded to the
582           * parent list since list children don't have a separate locale setting.
583           *
584           * @return the locale of this component
585           */
586          public Locale getLocale()
587          {
588            return parent.getLocale();
589          }
590    
591          /**
592           * This method does
593           * nothing, list children are transient accessible objects which means
594           * that they don't fire property change events.
595           *
596           * @param l not used here
597           */
598          public void addPropertyChangeListener(PropertyChangeListener l)
599          {
600            // Do nothing here.
601          }
602    
603          /**
604           * This method does
605           * nothing, list children are transient accessible objects which means
606           * that they don't fire property change events.
607           *
608           * @param l not used here
609           */
610          public void removePropertyChangeListener(PropertyChangeListener l)
611          {
612            // Do nothing here.
613          }
614          
615          // TODO: Implement the remaining methods of this class.
616        }
617    
618        /**
619         * Returns the number of selected accessible children.
620         *
621         * @return the number of selected accessible children
622         */
623        public int getAccessibleSelectionCount()
624        {
625          return getSelectedIndices().length;
626        }
627    
628        /**
629         * Returns the n-th selected accessible child.
630         *
631         * @param n the index of the selected child to return
632         *
633         * @return the n-th selected accessible child
634         */
635        public Accessible getAccessibleSelection(int n)
636        {
637          return new AccessibleJListChild(JList.this, getSelectedIndices()[n]);
638        }
639    
640        /**
641         * Returns <code>true</code> if the n-th child is selected,
642         * <code>false</code> otherwise.
643         *
644         * @param n the index of the child of which the selected state is queried
645         *
646         * @return <code>true</code> if the n-th child is selected,
647         *         <code>false</code> otherwise
648         */
649        public boolean isAccessibleChildSelected(int n)
650        {
651          return isSelectedIndex(n);
652        }
653    
654        /**
655         * Adds the accessible item with the specified index to the selected items.
656         * If multiple selections are supported, the item is added to the selection,
657         * otherwise the item replaces the current selection.
658         *
659         * @param i the index of the item to add to the selection
660         */
661        public void addAccessibleSelection(int i)
662        {
663          addSelectionInterval(i, i);
664        }
665    
666        /**
667         * Removes the accessible item with the specified index to the selection.
668         *
669         * @param i the index of the item to be removed from the selection
670         */
671        public void removeAccessibleSelection(int i)
672        {
673          removeSelectionInterval(i, i);
674        }
675    
676        /**
677         * Remove all selection items from the selection.
678         */
679        public void clearAccessibleSelection()
680        {
681          clearSelection();
682        }
683    
684        /**
685         * Selects all items if multiple selections are supported.
686         * Otherwise do nothing.
687         */
688        public void selectAllAccessibleSelection()
689        {
690          addSelectionInterval(0, getModel().getSize());
691        }
692    
693        /**
694         * Receices notification when the list selection is changed. This method
695         * fires two property change events, the first with
696         * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY} and the second
697         * with {@link AccessibleContext#ACCESSIBLE_SELECTION_PROPERTY}.
698         *
699         * @param event the list selection event
700         */
701        public void valueChanged(ListSelectionEvent event)
702        {
703          firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE,
704                             Boolean.TRUE);
705          firePropertyChange(ACCESSIBLE_SELECTION_PROPERTY, Boolean.FALSE,
706                             Boolean.TRUE);
707        }
708    
709        /**
710         * Receives notification when items have changed in the
711         * <code>JList</code>. This method fires a property change event with
712         * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY}.
713         *
714         * @param event the list data event
715         */
716        public void contentsChanged(ListDataEvent event)
717        {
718          firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE,
719                             Boolean.TRUE);
720        }
721    
722        /**
723         * Receives notification when items are inserted into the
724         * <code>JList</code>. This method fires a property change event with
725         * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY}.
726         *
727         * @param event the list data event
728         */
729        public void intervalAdded(ListDataEvent event)
730        {
731          firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE,
732                             Boolean.TRUE);
733        }
734    
735        /**
736         * Receives notification when items are removed from the
737         * <code>JList</code>. This method fires a property change event with
738         * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY}.
739         *
740         * @param event the list data event
741         */
742        public void intervalRemoved(ListDataEvent event)
743        {
744          firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE,
745                             Boolean.TRUE);
746        }
747    
748    
749        /**
750         * Receives notification about changes of the <code>JList</code>'s
751         * properties. This is used to re-register this object as listener to
752         * the data model and selection model when the data model or selection model
753         * changes.
754         *
755         * @param e the property change event
756         */
757        public void propertyChange(PropertyChangeEvent e)
758        {
759          String propertyName = e.getPropertyName();
760          if (propertyName.equals("model"))
761            {
762              ListModel oldModel = (ListModel) e.getOldValue();
763              oldModel.removeListDataListener(this);
764              ListModel newModel = (ListModel) e.getNewValue();
765              newModel.addListDataListener(this);
766            }
767          else if (propertyName.equals("selectionModel"))
768            {
769              ListSelectionModel oldModel = (ListSelectionModel) e.getOldValue();
770              oldModel.removeListSelectionListener(this);
771              ListSelectionModel newModel = (ListSelectionModel) e.getNewValue();
772              oldModel.addListSelectionListener(this);
773            }
774        }
775    
776        /**
777         * Return the state set of the <code>JList</code>.
778         *
779         * @return the state set of the <code>JList</code>
780         */
781        public AccessibleStateSet getAccessibleStateSet()
782        {
783          // TODO: Figure out if there is possibly more state that must be
784          // handled here.
785          AccessibleStateSet s = super.getAccessibleStateSet();
786          if (getSelectionMode() != ListSelectionModel.SINGLE_SELECTION)
787            s.add(AccessibleState.MULTISELECTABLE);
788          return s;
789        }
790    
791        /**
792         * Returns the accessible role for <code>JList</code>,
793         * {@link AccessibleRole#LIST}.
794         *
795         * @return the accessible role for <code>JList</code>
796         */
797        public AccessibleRole getAccessibleRole()
798        {
799          return AccessibleRole.LIST;
800        }
801    
802        /**
803         * Returns the accessible child at the visual location <code>p</code>
804         * (relative to the upper left corner of the <code>JList</code>). If there
805         * is no child at that location, this returns <code>null</code>.
806         *
807         * @param p the screen location for which to return the accessible child
808         *
809         * @return the accessible child at the specified location, or
810         *         <code>null</code> if there is no child at that location
811         */
812        public Accessible getAccessibleAt(Point p)
813        {
814          int childIndex = locationToIndex(p);
815          return getAccessibleChild(childIndex);
816        }
817    
818        /**
819         * Returns the number of accessible children in the <code>JList</code>.
820         *
821         * @return the number of accessible children in the <code>JList</code>
822         */
823        public int getAccessibleChildrenCount()
824        {
825          return getModel().getSize();
826        }
827    
828        /**
829         * Returns the n-th accessible child of this <code>JList</code>. This will
830         * be an instance of {@link AccessibleJListChild}. If there is no child
831         * at that index, <code>null</code> is returned.
832         *
833         * @param n the index of the child to return
834         *
835         * @return the n-th accessible child of this <code>JList</code>
836         */
837        public Accessible getAccessibleChild(int n)
838        {
839          if (getModel().getSize() <= n)
840            return null;
841          return new AccessibleJListChild(JList.this, n);
842        }
843      }
844    
845    private static final long serialVersionUID = 4406629526391098046L;    private static final long serialVersionUID = 4406629526391098046L;
846    
847    /**    /**

Legend:
Removed from v.1.16.2.11  
changed lines
  Added in v.1.16.2.12

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26