1 19 20 package org.netbeans.core.windows.services; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.beans.BeanInfo ; 28 import java.beans.PropertyChangeEvent ; 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyVetoException ; 31 import java.util.ResourceBundle ; 32 import javax.swing.ImageIcon ; 33 import javax.swing.JButton ; 34 import javax.swing.JComboBox ; 35 import javax.swing.JLabel ; 36 import javax.swing.JList ; 37 import javax.swing.JPanel ; 38 import javax.swing.ListCellRenderer ; 39 import javax.swing.UIManager ; 40 import javax.swing.border.Border ; 41 import javax.swing.border.EmptyBorder ; 42 import javax.swing.border.LineBorder ; 43 import org.openide.awt.Mnemonics; 44 import org.openide.explorer.ExplorerManager; 45 import org.openide.explorer.view.BeanTreeView; 46 import org.openide.nodes.Node; 47 import org.openide.nodes.NodeAcceptor; 48 import org.openide.util.NbBundle; 49 50 54 final class FileSelector extends JPanel implements PropertyChangeListener , ExplorerManager.Provider { 55 57 58 static final long serialVersionUID = 6524404012203099065L; 59 60 private final ExplorerManager manager = new ExplorerManager(); 61 62 private BeanTreeView tree; 63 64 65 private JButton okButton; 66 67 private JButton cancelButton; 68 private JButton [] buttons; 69 70 71 private NodeAcceptor acceptor; 72 73 74 76 84 public FileSelector ( String rootLabel, Node root, final NodeAcceptor acceptor, Component top) { 85 super (); 86 87 this.acceptor = acceptor; 88 89 ResourceBundle bundle = NbBundle.getBundle(FileSelector.class); 90 91 92 okButton = new JButton (bundle.getString("CTL_FileSelectorOkButton")); 93 cancelButton = new JButton (bundle.getString("CTL_FileSelectorCancelButton")); 94 okButton.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACSD_FileSelectorOkButton")); 95 cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACSD_FileSelectorCancelButton")); 96 buttons = new JButton [] { okButton, cancelButton }; 97 98 99 manager.setRootContext (root); 101 tree = new BeanTreeView (); 103 tree.setPopupAllowed (false); 104 tree.setDefaultActionAllowed (false); 105 tree.setBorder((Border )UIManager.get("Nb.ScrollPane.border")); tree.getAccessibleContext().setAccessibleName(NbBundle.getBundle(FileSelector.class).getString("ACSN_FileSelectorTreeView")); 108 tree.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FileSelector.class).getString("ACSD_FileSelectorTreeView")); 109 this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FileSelector.class).getString("ACSD_FileSelectorDialog")); 110 setLayout(new BorderLayout ()); 111 add(tree, BorderLayout.CENTER); 112 113 try { 115 Node[] roots; 116 if ( 117 root instanceof org.netbeans.core.DataSystem && 118 (roots = root.getChildren ().getNodes ()).length > 0 119 ) { 120 final JComboBox combo = new JComboBox (roots); 121 combo.setRenderer(new FileSelectRenderer()); 122 combo.addActionListener(new ActionListener () { 123 public void actionPerformed(ActionEvent evt) { 124 Node o = (Node) combo.getSelectedItem(); 125 manager.setRootContext(o); 126 } 127 }); 128 combo.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACSD_FileSelectorComboBox")); 129 manager.setSelectedNodes (new Node[] { roots[0] }); 130 131 JPanel comboPanel = new JPanel (); 133 comboPanel.setLayout(new BorderLayout (5, 0)); 135 136 JLabel label = new JLabel (); 137 Mnemonics.setLocalizedText(label, rootLabel); 138 label.setLabelFor(combo); 139 comboPanel.add(label, BorderLayout.WEST); 140 comboPanel.add(combo, BorderLayout.CENTER); 141 add(comboPanel, BorderLayout.NORTH); 142 } else { 143 manager.setSelectedNodes (new Node[] { root }); 144 JLabel label = new JLabel (rootLabel.replace('&', ' ')); 145 label.setLabelFor(tree); 146 add(label, BorderLayout.NORTH); 147 } 148 } catch(PropertyVetoException pve) { 149 throw new IllegalStateException (pve.getMessage()); 150 } 151 152 153 154 if (top != null) { 156 add(top, BorderLayout.SOUTH); 157 } 158 159 manager.addPropertyChangeListener (this); 160 161 163 if (acceptor.acceptNodes (manager.getSelectedNodes())) { 164 enableButton (); 165 } else { 166 disableButton (); 167 } 168 169 } 170 171 Object [] getOptions() { 172 return buttons; 173 } 174 175 Object getSelectOption() { 176 return okButton; 177 } 178 179 180 181 182 public void propertyChange (PropertyChangeEvent ev) { 183 if (ev.getPropertyName().equals (ExplorerManager.PROP_SELECTED_NODES)) { 184 if (acceptor.acceptNodes (manager.getSelectedNodes())) { 185 enableButton (); 186 } else { 187 disableButton (); 188 } 189 } 190 } 191 192 193 195 public Dimension getPreferredSize() { 196 Dimension dim = super.getPreferredSize(); 197 dim.height = Math.max(dim.height, org.openide.util.Utilities.getUsableScreenBounds().height / 2); 198 return dim; 199 } 200 201 204 public Node[] getNodes() { 205 return manager.getSelectedNodes(); 206 } 207 208 209 void enableButton () { 210 okButton.setEnabled(true); 211 } 212 213 214 void disableButton () { 215 okButton.setEnabled(false); 216 } 217 218 public ExplorerManager getExplorerManager() { 219 return manager; 220 } 221 222 223 224 private static class FileSelectRenderer extends JLabel implements ListCellRenderer { 225 226 static final long serialVersionUID = -7071698027341621636L; 227 228 protected static Border hasFocusBorder; 229 protected static Border noFocusBorder; 230 231 public FileSelectRenderer() { 232 setOpaque(true); 233 setBorder(noFocusBorder); 234 hasFocusBorder = new LineBorder (UIManager.getColor("List.focusCellHighlight")); noFocusBorder = new EmptyBorder (1, 1, 1, 1); 236 } 237 238 239 public Component getListCellRendererComponent( 240 JList list, 241 Object value, int index, boolean isSelected, boolean cellHasFocus) { 246 if (!(value instanceof Node)) return this; 247 248 Node node = (Node)value; 249 250 ImageIcon icon = new ImageIcon (node.getIcon(BeanInfo.ICON_COLOR_16x16)); 251 setIcon(icon); 252 253 setText(node.getDisplayName()); 254 if (isSelected){ 255 super.setBackground(UIManager.getColor("List.selectionBackground")); super.setForeground(UIManager.getColor("List.selectionForeground")); } 258 else { 259 super.setBackground(list.getBackground()); 260 super.setForeground(list.getForeground()); 261 } 262 263 setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder); 264 265 return this; 266 } 267 } } 269 | Popular Tags |