1 19 package org.openide.explorer.view; 20 21 import org.openide.explorer.*; 22 import org.openide.explorer.ExplorerManager.Provider; 23 import org.openide.nodes.Node; 24 import org.openide.nodes.Node.Property; 25 26 import java.beans.*; 27 28 import java.io.*; 29 30 import javax.swing.*; 31 32 33 66 public class ChoiceView extends JComboBox implements Externalizable { 67 68 static final long serialVersionUID = 2522310031223476067L; 69 70 72 transient private ExplorerManager manager; 73 74 75 transient private PropertyIL iListener; 76 77 78 transient private NodeListModel model; 79 80 81 private boolean showExploredContext = true; 82 83 85 86 public ChoiceView() { 87 super(); 88 initializeChoice(); 89 } 90 91 92 private void initializeChoice() { 93 setRenderer(new NodeRenderer()); 94 95 setModel(model = createModel()); 96 97 iListener = new PropertyIL(); 98 } 99 100 104 107 public void writeExternal(ObjectOutput out) throws IOException { 108 out.writeObject(showExploredContext ? Boolean.TRUE : Boolean.FALSE); 109 } 110 111 114 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 115 showExploredContext = ((Boolean ) in.readObject()).booleanValue(); 116 } 117 118 122 124 protected NodeListModel createModel() { 125 return new NodeListModel(); 126 } 127 128 130 133 public void setShowExploredContext(boolean b) { 134 showExploredContext = b; 135 updateChoice(); 136 } 137 138 142 public boolean getShowExploredContext() { 143 return showExploredContext; 144 } 145 146 148 150 public void addNotify() { 151 manager = ExplorerManager.find(this); 152 manager.addVetoableChangeListener(iListener); 153 manager.addPropertyChangeListener(iListener); 154 155 updateChoice(); 156 157 addActionListener(iListener); 158 159 super.addNotify(); 160 } 161 162 164 public void removeNotify() { 165 super.removeNotify(); 166 167 removeActionListener(iListener); 168 169 manager.removeVetoableChangeListener(iListener); 170 manager.removePropertyChangeListener(iListener); 171 } 172 173 private void updateSelection() { 174 Node[] nodes = manager.getSelectedNodes(); 175 176 if (nodes.length > 0) { 177 setSelectedItem(VisualizerNode.getVisualizer(null, nodes[0])); 178 } else { 179 setSelectedItem(showExploredContext ? manager.getExploredContext() : manager.getRootContext()); 180 } 181 } 182 183 private void updateChoice() { 184 if (showExploredContext) { 185 model.setNode(manager.getExploredContext()); 186 } else { 187 model.setNode(manager.getRootContext()); 188 } 189 190 updateSelection(); 191 } 192 193 195 196 final class PropertyIL extends Object implements PropertyChangeListener, VetoableChangeListener, 197 java.awt.event.ActionListener { 198 public void vetoableChange(PropertyChangeEvent evt) 199 throws PropertyVetoException { 200 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 201 Node[] nodes = (Node[]) evt.getNewValue(); 202 203 if (nodes.length > 1) { 204 throw new PropertyVetoException("", evt); } 206 } 207 } 208 209 public void propertyChange(PropertyChangeEvent evt) { 210 ChoiceView.this.removeActionListener(this); 211 212 try { 213 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 214 Node[] selectedNodes = (Node[]) evt.getNewValue(); 215 updateSelection(); 216 217 return; 218 } 219 220 if (!showExploredContext && ExplorerManager.PROP_ROOT_CONTEXT.equals(evt.getPropertyName())) { 221 updateChoice(); 222 223 return; 224 } 225 226 if (showExploredContext && ExplorerManager.PROP_EXPLORED_CONTEXT.equals(evt.getPropertyName())) { 227 updateChoice(); 228 229 return; 230 } 231 } finally { 232 ChoiceView.this.addActionListener(this); 233 } 234 } 235 236 public void actionPerformed(java.awt.event.ActionEvent actionEvent) { 237 int s = getSelectedIndex(); 238 239 if ((s < 0) || (s >= model.getSize())) { 240 return; 241 } 242 243 Node n = Visualizer.findNode(model.getElementAt(s)); 244 245 manager.removeVetoableChangeListener(this); 246 manager.removePropertyChangeListener(this); 247 248 try { 249 manager.setSelectedNodes(new Node[] { n }); 250 } catch (PropertyVetoException ex) { 251 updateChoice(); } finally { 253 manager.addVetoableChangeListener(this); 254 manager.addPropertyChangeListener(this); 255 } 256 } 257 } 258 } 259 | Popular Tags |