View Javadoc
1 /* 2 * @(#)TreeCombo.java 1.7 99/04/23 3 * 4 * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved. 5 * 6 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, 7 * modify and redistribute this software in source and binary code form, 8 * provided that i) this copyright notice and license appear on all copies of 9 * the software; and ii) Licensee does not utilize the software in a manner 10 * which is disparaging to Sun. 11 * 12 * This software is provided "AS IS," without a warranty of any kind. ALL 13 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY 14 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR 15 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE 16 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING 17 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS 18 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 19 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 20 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF 21 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 22 * POSSIBILITY OF SUCH DAMAGES. 23 * 24 * This software is not designed or intended for use in on-line control of 25 * aircraft, air traffic, aircraft navigation or aircraft communications; or in 26 * the design, construction, operation or maintenance of any nuclear 27 * facility. Licensee represents and warrants that it will not use or 28 * redistribute the Software for such purposes. 29 */ 30 package com.sun.xml.registry.client.browser; 31 32 import javax.swing.*; 33 import javax.swing.border.*; 34 import javax.swing.event.*; 35 import javax.swing.plaf.basic.*; 36 import java.util.Vector; 37 import java.awt.*; 38 import java.awt.event.*; 39 import javax.swing.plaf.*; 40 import javax.swing.tree.*; 41 42 public class TreeCombo extends JComboBox { 43 static final int OFFSET = 16; 44 TreeModel treeModel = null; 45 46 public TreeCombo(TreeModel aTreeModel) { 47 super(); 48 49 treeModel = aTreeModel; 50 setModel(new TreeToListModel(aTreeModel)); 51 setRenderer(new ListEntryRenderer()); 52 53 addActionListener(new TreeComboActionListener()); 54 } 55 56 public Object getSelectedItemsObject() { 57 return ((ListEntry)getSelectedItem()).object(); 58 } 59 60 class TreeToListModel extends AbstractListModel implements ComboBoxModel,TreeModelListener { 61 TreeModel source; 62 boolean invalid = true; 63 Object currentValue; 64 Vector cache = new Vector(); 65 66 public TreeToListModel(TreeModel aTreeModel) { 67 source = aTreeModel; 68 aTreeModel.addTreeModelListener(this); 69 setRenderer(new ListEntryRenderer()); 70 } 71 72 public void setSelectedItem(Object anObject) { 73 currentValue = anObject; 74 if (anObject == null) { 75 return; 76 } 77 78 fireContentsChanged(this, -1, -1); 79 } 80 81 public Object getSelectedItem() { 82 return currentValue; 83 } 84 85 public int getSize() { 86 validate(); 87 return cache.size(); 88 } 89 90 public Object getElementAt(int index) { 91 return cache.elementAt(index); 92 } 93 94 public void treeNodesChanged(TreeModelEvent e) { 95 invalid = true; 96 } 97 98 public void treeNodesInserted(TreeModelEvent e) { 99 invalid = true; 100 } 101 102 public void treeNodesRemoved(TreeModelEvent e) { 103 invalid = true; 104 } 105 106 public void treeStructureChanged(TreeModelEvent e) { 107 invalid = true; 108 } 109 110 void validate() { 111 if(invalid) { 112 cache = new Vector(); 113 cacheTree(source.getRoot(),0); 114 if ((cache.size() > 0) && (currentValue == null)) { 115 currentValue = cache.elementAt(0); 116 } 117 invalid = false; 118 fireContentsChanged(this, 0, 0); 119 } 120 } 121 122 void cacheTree(Object anObject,int level) { 123 if(source.isLeaf(anObject)) 124 addListEntry(anObject,level,false); 125 else { 126 int c = source.getChildCount(anObject); 127 int i; 128 Object child; 129 130 addListEntry(anObject,level,true); 131 level++; 132 133 for(i=0;i<c;i++) { 134 child = source.getChild(anObject,i); 135 cacheTree(child,level); 136 } 137 138 level--; 139 } 140 } 141 142 void addListEntry(Object anObject,int level,boolean isNode) { 143 cache.addElement(new ListEntry(anObject,level,isNode)); 144 } 145 146 } 147 148 class ListEntry { 149 Object object; 150 int level; 151 boolean isNode; 152 153 public ListEntry(Object anObject,int aLevel,boolean isNode) { 154 object = anObject; 155 level = aLevel; 156 this.isNode = isNode; 157 } 158 159 public Object object() { 160 return object; 161 } 162 163 public int level() { 164 return level; 165 } 166 167 public boolean isNode() { 168 return isNode; 169 } 170 171 public String toString() { 172 return object.toString(); 173 } 174 } 175 176 static Border emptyBorder = new EmptyBorder(0,0,0,0); 177 178 public class TreeComboActionListener implements ActionListener { 179 public void actionPerformed(ActionEvent e) { 180 TreeCombo cb = (TreeCombo)e.getSource(); 181 Object anObject = getSelectedItem(); 182 183 if (anObject == null) { 184 return; 185 } 186 187 if (anObject instanceof ListEntry) { 188 ListEntry listEntry = (ListEntry)anObject; 189 190 DefaultMutableTreeNode node = (DefaultMutableTreeNode)listEntry.object(); 191 RegistryBrowser.setWaitCursor(); 192 193 int depth = 1; 194 if ((e.getModifiers() & ActionEvent.SHIFT_MASK) != 0) { 195 depth = -1; 196 } 197 198 ((ConceptsTreeModel)treeModel).expandTree(node, depth); 199 RegistryBrowser.setDefaultCursor(); 200 } 201 202 } 203 } 204 205 206 class ListEntryRenderer extends JLabel implements ListCellRenderer { 207 java.net.URL leafUrl = getClass().getClassLoader().getResource("icons/document.gif"); 208 ImageIcon leafIcon = new ImageIcon(leafUrl); 209 210 java.net.URL nodeUrl = getClass().getClassLoader().getResource("icons/folder.gif"); 211 ImageIcon nodeIcon = new ImageIcon(nodeUrl); 212 213 public ListEntryRenderer() { 214 setOpaque(true); 215 } 216 217 public Component getListCellRendererComponent( 218 JList listbox, 219 Object value, 220 int index, 221 boolean isSelected, 222 boolean cellHasFocus) 223 { 224 ListEntry listEntry = (ListEntry)value; 225 if(listEntry != null) { 226 Border border; 227 setText(listEntry.object().toString()); 228 setIcon( listEntry.isNode() ? nodeIcon : leafIcon ); 229 if(index != -1) 230 border = new EmptyBorder(0, OFFSET * listEntry.level(), 0, 0); 231 else 232 border = emptyBorder; 233 234 if(UIManager.getLookAndFeel().getName().equals("CDE/Motif")) { 235 if(index == -1 ) 236 setOpaque(false); 237 else 238 setOpaque(true); 239 } else 240 setOpaque(true); 241 242 setBorder(border); 243 if (isSelected) { 244 setBackground(UIManager.getColor("ComboBox.selectionBackground")); 245 setForeground(UIManager.getColor("ComboBox.selectionForeground")); 246 } else { 247 setBackground(UIManager.getColor("ComboBox.background")); 248 setForeground(UIManager.getColor("ComboBox.foreground")); 249 } 250 } else { 251 setText(""); 252 } 253 return this; 254 } 255 } 256 } 257 258 259 260 261 262 263

This page was automatically generated by Maven