/* * @(#)TreeCombo.java 1.5 98/05/19 * * Copyright 1997, 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ import com.sun.java.swing.*; import com.sun.java.swing.border.*; import com.sun.java.swing.event.*; import com.sun.java.swing.plaf.basic.*; import java.util.Vector; import java.awt.*; import com.sun.java.swing.plaf.*; import com.sun.java.swing.tree.*; public class TreeCombo extends JComboBox { static final int OFFSET = 16; public TreeCombo(TreeModel aTreeModel) { super(); setModel(new TreeToListModel(aTreeModel)); setRenderer(new ListEntryRenderer()); } class TreeToListModel extends AbstractListModel implements ComboBoxModel,TreeModelListener { TreeModel source; boolean invalid = true; Object currentValue; Vector cache = new Vector(); public TreeToListModel(TreeModel aTreeModel) { source = aTreeModel; aTreeModel.addTreeModelListener(this); setRenderer(new ListEntryRenderer()); } public void setSelectedItem(Object anObject) { currentValue = anObject; fireContentsChanged(this, -1, -1); } public Object getSelectedItem() { return currentValue; } public int getSize() { validate(); return cache.size(); } public Object getElementAt(int index) { return cache.elementAt(index); } public void treeNodesChanged(TreeModelEvent e) { invalid = true; } public void treeNodesInserted(TreeModelEvent e) { invalid = true; } public void treeNodesRemoved(TreeModelEvent e) { invalid = true; } public void treeStructureChanged(TreeModelEvent e) { invalid = true; } void validate() { if(invalid) { cache = new Vector(); cacheTree(source.getRoot(),0); if(cache.size() > 0) currentValue = cache.elementAt(0); invalid = false; fireContentsChanged(this, 0, 0); } } void cacheTree(Object anObject,int level) { if(source.isLeaf(anObject)) addListEntry(anObject,level,false); else { int c = source.getChildCount(anObject); int i; Object child; addListEntry(anObject,level,true); level++; for(i=0;i