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

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

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

revision 1.14 by audriusa, Sun Feb 27 17:52:53 2005 UTC revision 1.15 by rabbit78, Mon Jun 20 14:35:53 2005 UTC
# Line 95  public class JComboBox extends JComponen Line 95  public class JComboBox extends JComponen
95    private static final int DEFAULT_MAXIMUM_ROW_COUNT = 8;    private static final int DEFAULT_MAXIMUM_ROW_COUNT = 8;
96    
97    /**    /**
98     * dataModel used by JComboBox to keep track of its list data and currently     * Data model used by JComboBox to keep track of its list data and currently
99     * selected element in the list.     * selected element in the list.
100     */     */
101    protected ComboBoxModel dataModel;    protected ComboBoxModel dataModel;
# Line 290  public class JComboBox extends JComponen Line 290  public class JComboBox extends JComponen
290     */     */
291    public void setModel(ComboBoxModel newDataModel)    public void setModel(ComboBoxModel newDataModel)
292    {    {
   
293      // dataModel is null if it this method is called from inside the constructors.      // dataModel is null if it this method is called from inside the constructors.
294      if(dataModel != null) {      if (dataModel != null)
295          // Prevents unneccessary updates.        {
296          if (dataModel == newDataModel)          // Prevents unneccessary updates.
297                  return;          if (dataModel == newDataModel)
298              return;
299          // Removes itself (as DataListener) from the to-be-replaced model.  
300          dataModel.removeListDataListener(this);          // Removes itself (as DataListener) from the to-be-replaced model.
301      }          dataModel.removeListDataListener(this);
302          }
303            
304      /* Adds itself as a DataListener to the new model.      /* Adds itself as a DataListener to the new model.
305       * It is intentioned that this operation will fail with a NullPointerException if the       * It is intentioned that this operation will fail with a NullPointerException if the
# Line 366  public class JComboBox extends JComponen Line 366  public class JComboBox extends JComponen
366    {    {
367      if (isEditable != editable)      if (isEditable != editable)
368        {        {
369          isEditable = editable;          isEditable = editable;
370          firePropertyChange("editable", ! isEditable, isEditable);          firePropertyChange("editable", !isEditable, isEditable);
371        }        }
372    }    }
373    
# Line 383  public class JComboBox extends JComponen Line 383  public class JComboBox extends JComponen
383    {    {
384      if (maximumRowCount != rowCount)      if (maximumRowCount != rowCount)
385        {        {
386          int oldMaximumRowCount = maximumRowCount;          int oldMaximumRowCount = maximumRowCount;
387          maximumRowCount = rowCount;          maximumRowCount = rowCount;
388          firePropertyChange("maximumRowCount",          firePropertyChange("maximumRowCount", oldMaximumRowCount,
389                             oldMaximumRowCount, maximumRowCount);                             maximumRowCount);
390        }        }
391    }    }
392    
# Line 415  public class JComboBox extends JComponen Line 415  public class JComboBox extends JComponen
415    {    {
416      if (renderer != aRenderer)      if (renderer != aRenderer)
417        {        {
418          ListCellRenderer oldRenderer = renderer;          ListCellRenderer oldRenderer = renderer;
419          renderer = aRenderer;          renderer = aRenderer;
420          firePropertyChange("renderer", oldRenderer,          firePropertyChange("renderer", oldRenderer, renderer);
                            renderer);  
421        }        }
422    }    }
423    
# Line 501  public class JComboBox extends JComponen Line 500  public class JComboBox extends JComponen
500     */     */
501    public void setSelectedIndex(int index)    public void setSelectedIndex(int index)
502    {    {
503          if(index < -1 || index >= dataModel.getSize()) {          if (index < -1 || index >= dataModel.getSize())
504                  // Fails because index is out of bounds.        // Fails because index is out of bounds.
505                  throw new IllegalArgumentException("illegal index: " + index);        throw new IllegalArgumentException("illegal index: " + index);
506          } else {      else
507                  /* Selects the item at the given index or clears the selection if the         // Selects the item at the given index or clears the selection if the
508                   * index value is -1.         // index value is -1.
509                   */        setSelectedItem((index == -1) ? null : dataModel.getElementAt(index));
                 setSelectedItem((index == -1) ? null : dataModel.getElementAt(index));  
         }  
510    }    }
511    
512    /**    /**
513     * Returns index of the item that is currently selected  in the combo box.     * Returns index of the item that is currently selected in the combo box. If
514     * If no item is currently selected, then -1 is returned.     * no item is currently selected, then -1 is returned.
515     *     * <p>
516     * <p>Note: For performance reasons you should minimize invocation of this     * Note: For performance reasons you should minimize invocation of this
517     * method. If the data model is not an instance of     * method. If the data model is not an instance of
518     * <code>DefaultComboBoxModel</code> the complexity is O(n) where     * <code>DefaultComboBoxModel</code> the complexity is O(n) where n is the
519     * n is the number of elements in the combo box.</p>     * number of elements in the combo box.
520     *     * </p>
521     * @return int Index specifying location of the currently selected item in     *
522     *         the combo box or -1 if nothing is selected in the combo box.     * @return int Index specifying location of the currently selected item in the
523       *         combo box or -1 if nothing is selected in the combo box.
524     */     */
525    public int getSelectedIndex()    public int getSelectedIndex()
526    {    {
527      Object selectedItem = getSelectedItem();      Object selectedItem = getSelectedItem();
528        
529      if (selectedItem != null) {      if (selectedItem != null)
530                  {
531                  if(dataModel instanceof DefaultComboBoxModel) {          if (dataModel instanceof DefaultComboBoxModel)
532                          // Uses special method of DefaultComboBoxModel to retrieve the index.            // Uses special method of DefaultComboBoxModel to retrieve the index.
533                          return ((DefaultComboBoxModel) dataModel).getIndexOf(selectedItem);            return ((DefaultComboBoxModel) dataModel).getIndexOf(selectedItem);
534                  } else {          else
535                          // Iterates over all items to retrieve the index.            {
536                          int size = dataModel.getSize();              // Iterates over all items to retrieve the index.
537                                        int size = dataModel.getSize();
538                          for(int i=0; i < size; i++) {  
539                                  Object o = dataModel.getElementAt(i);              for (int i = 0; i < size; i++)
540                                                  {
541                                  // XXX: Is special handling of ComparableS neccessary?                  Object o = dataModel.getElementAt(i);
542                                  if((selectedItem != null) ? selectedItem.equals(o) : o == null) {  
543                                          return i;                  // XXX: Is special handling of ComparableS neccessary?
544                                  }                  if ((selectedItem != null) ? selectedItem.equals(o) : o == null)
545                          }                    return i;
546                  }                }
547      }            }
548          }
549    
550      // returns that no item is currently selected      // returns that no item is currently selected
551      return -1;      return -1;
# Line 571  public class JComboBox extends JComponen Line 570  public class JComboBox extends JComponen
570     */     */
571    public void addItem(Object element)    public void addItem(Object element)
572    {    {
573          if(dataModel instanceof MutableComboBoxModel) {          if (dataModel instanceof MutableComboBoxModel)
574                  ((MutableComboBoxModel) dataModel).addElement(element);        ((MutableComboBoxModel) dataModel).addElement(element);
575          } else {      else
576                  throw new RuntimeException("Unable to add the item because the data model it is not an instance of MutableComboBoxModel.");        throw new RuntimeException("Unable to add the item because the data "
577          }                                   + "model it is not an instance of "
578                                     + "MutableComboBoxModel.");
579    }    }
580    
581    /**    /**
# Line 588  public class JComboBox extends JComponen Line 588  public class JComboBox extends JComponen
588     */     */
589    public void insertItemAt(Object element, int index)    public void insertItemAt(Object element, int index)
590    {    {
591          if(dataModel instanceof MutableComboBoxModel) {          if (dataModel instanceof MutableComboBoxModel)
592                  ((MutableComboBoxModel) dataModel).insertElementAt(element, index);        ((MutableComboBoxModel) dataModel).insertElementAt(element, index);
593          } else {      else
594                  throw new RuntimeException("Unable to insert the item because the data model it is not an instance of MutableComboBoxModel.");        throw new RuntimeException("Unable to insert the item because the data "
595          }                                   + "model it is not an instance of "
596                                     + "MutableComboBoxModel.");
597    }    }
598    
599    /**    /**
# Line 604  public class JComboBox extends JComponen Line 605  public class JComboBox extends JComponen
605     */     */
606    public void removeItem(Object element)    public void removeItem(Object element)
607    {    {
608          if(dataModel instanceof MutableComboBoxModel) {          if (dataModel instanceof MutableComboBoxModel)
609                  ((MutableComboBoxModel) dataModel).removeElement(element);        ((MutableComboBoxModel) dataModel).removeElement(element);
610          } else {      else
611                  throw new RuntimeException("Unable to remove the item because the data model it is not an instance of MutableComboBoxModel.");        throw new RuntimeException("Unable to remove the item because the data "
612          }                                   + "model it is not an instance of "
613                                     + "MutableComboBoxModel.");
614    }    }
615    
616    /**    /**
# Line 621  public class JComboBox extends JComponen Line 623  public class JComboBox extends JComponen
623     */     */
624    public void removeItemAt(int index)    public void removeItemAt(int index)
625    {    {
626          if(dataModel instanceof MutableComboBoxModel) {      if (dataModel instanceof MutableComboBoxModel)
627                  ((MutableComboBoxModel) dataModel).removeElementAt(index);        ((MutableComboBoxModel) dataModel).removeElementAt(index);
628          } else {      else
629                  throw new RuntimeException("Unable to remove the item because the data model it is not an instance of MutableComboBoxModel.");        throw new RuntimeException("Unable to remove the item because the data "
630          }                                   + "model it is not an instance of "
631                                     + "MutableComboBoxModel.");
632    }    }
633    
634    /**    /**
635     * This method removes all elements from this JComboBox.     * This method removes all elements from this JComboBox.
636     * <p>A <code>RuntimeException</code> is thrown if the data model is not     * <p>
637     * an instance of {@link MutableComboBoxModel}.</p>     * A <code>RuntimeException</code> is thrown if the data model is not an
638     *     * instance of {@link MutableComboBoxModel}.
639       * </p>
640     */     */
641    public void removeAllItems()    public void removeAllItems()
642    {    {
643      if (dataModel instanceof DefaultComboBoxModel) {      if (dataModel instanceof DefaultComboBoxModel)
644          // Uses special method if we have a DefaultComboBoxModel.        // Uses special method if we have a DefaultComboBoxModel.
645          ((DefaultComboBoxModel) dataModel).removeAllElements();        ((DefaultComboBoxModel) dataModel).removeAllElements();
646      } else if(dataModel instanceof MutableComboBoxModel){      else if (dataModel instanceof MutableComboBoxModel)
647          // Iterates over all items and removes each.        {
648          MutableComboBoxModel mcbm = (MutableComboBoxModel) dataModel;          // Iterates over all items and removes each.
649            MutableComboBoxModel mcbm = (MutableComboBoxModel) dataModel;
650          /* We intentionally remove the items backwards to support  
651           * models which shift their content to the beginning (e.g.           // We intentionally remove the items backwards to support models which
652           * linked lists)           // shift their content to the beginning (e.g. linked lists)
653           */                      for (int i = mcbm.getSize() - 1; i >= 0; i--)
654          for(int i=mcbm.getSize()-1; i >= 0; i--) {            mcbm.removeElementAt(i);
655                  mcbm.removeElementAt(i);        }
656          }      else
657                  throw new RuntimeException("Unable to remove the items because the data "
658      } else {                                   +"model it is not an instance of "
659          throw new RuntimeException("Unable to remove the items because the data model it is not an instance of MutableComboBoxModel.");                                   + "MutableComboBoxModel.");
     }  
         
660    }    }
661    
662    /**    /**
# Line 755  public class JComboBox extends JComponen Line 757  public class JComboBox extends JComponen
757    {    {
758      if (a == null)      if (a == null)
759        {        {
760          setEnabled(true);          setEnabled(true);
761          setToolTipText(null);          setToolTipText(null);
762        }        }
763      else      else
764        {        {
765          setEnabled(a.isEnabled());          setEnabled(a.isEnabled());
766          setToolTipText((String) (a.getValue(Action.SHORT_DESCRIPTION)));          setToolTipText((String) (a.getValue(Action.SHORT_DESCRIPTION)));
767        }        }
768    }    }
769    
# Line 771  public class JComboBox extends JComponen Line 773  public class JComboBox extends JComponen
773     *     *
774     * @param action action to listen to for property changes     * @param action action to listen to for property changes
775     *     *
776     * @return $PropertyChangeListener$ Listener that listens to changes in     * @return a PropertyChangeListener that listens to changes in
777     *         action properties.     *         action properties.
778     */     */
779    protected PropertyChangeListener createActionPropertyChangeListener(Action action)    protected PropertyChangeListener createActionPropertyChangeListener(Action action)
780    {    {
781      return new PropertyChangeListener()      return new PropertyChangeListener()
782        {        {
783          public void propertyChange(PropertyChangeEvent e)          public void propertyChange(PropertyChangeEvent e)
784          {          {
785            Action act = (Action) (e.getSource());            Action act = (Action) (e.getSource());
786            configurePropertiesFromAction(act);            configurePropertiesFromAction(act);
787          }          }
788        };        };
789    }    }
790    
# Line 936  public class JComboBox extends JComponen Line 938  public class JComboBox extends JComponen
938      boolean oldEnabled = super.isEnabled();      boolean oldEnabled = super.isEnabled();
939      if (enabled != oldEnabled)      if (enabled != oldEnabled)
940        {        {
941          super.setEnabled(enabled);          super.setEnabled(enabled);
942          firePropertyChange("enabled", oldEnabled, enabled);          firePropertyChange("enabled", oldEnabled, enabled);
943        }        }
944    }    }
945    

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.15

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