1 19 20 package org.netbeans.beaninfo; 21 22 import java.awt.Component ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyEditor ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import javax.accessibility.AccessibleContext ; 31 import javax.swing.DefaultListModel ; 32 import javax.swing.Icon ; 33 import javax.swing.ImageIcon ; 34 import javax.swing.JLabel ; 35 import javax.swing.JList ; 36 import javax.swing.ListCellRenderer ; 37 import javax.swing.ListSelectionModel ; 38 import org.netbeans.core.ActionsPoolNode; 39 import org.openide.awt.Actions; 40 import org.openide.cookies.InstanceCookie; 41 import org.openide.explorer.ExplorerManager; 42 import org.openide.explorer.propertysheet.PropertyEnv; 43 import org.openide.nodes.Node; 44 import org.openide.util.NbBundle; 45 import org.openide.util.actions.SystemAction; 46 47 51 public class LoaderActionsPanel extends javax.swing.JPanel implements PropertyChangeListener , ListCellRenderer { 52 53 private DefaultListModel model; 54 private ExplorerManager mgr; 55 private PropertyEditor editor; 56 57 58 public LoaderActionsPanel (PropertyEditor pe, PropertyEnv env) { 59 env.setState(PropertyEnv.STATE_NEEDS_VALIDATION); 60 env.addPropertyChangeListener(this); 61 this.editor = pe; 62 63 64 initComponents (); 65 model = new DefaultListModel (); 66 SystemAction[] actions = (SystemAction[]) pe.getValue (); 67 if (actions == null) actions = new SystemAction[] { }; 68 for (int i = 0; i < actions.length; i++) 69 model.addElement (actions[i]); 70 list.setModel (model); 71 mgr = explorerPanel.getExplorerManager (); 72 mgr.setRootContext (new ActionsPoolNode ()); 73 mgr.addPropertyChangeListener (new PropertyChangeListener () { 74 public void propertyChange (PropertyChangeEvent ev) { 75 if (ExplorerManager.PROP_SELECTED_NODES.equals (ev.getPropertyName ())) { 76 SystemAction action = findAction (mgr.getSelectedNodes ()); 77 addButton.setEnabled (action != null); 78 } 79 } 80 }); 81 84 splitPane.setDividerLocation (300); 87 88 java.util.ResourceBundle bundle = NbBundle.getBundle(LoaderActionsPanel.class); 89 90 AccessibleContext ac = beanTreeView2.getAccessibleContext(); 91 ac.setAccessibleName(bundle.getString("ACS_LoaderActionsPanel.beanTreeView")); 92 ac.setAccessibleDescription(bundle.getString("ACSD_LoaderActionsPanel.beanTreeView")); 93 ac = list.getAccessibleContext(); 94 ac.setAccessibleName(bundle.getString("ACS_LoaderActionsPanel.list")); 95 ac.setAccessibleDescription(bundle.getString("ACSD_LoaderActionsPanel.list")); 96 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_LoaderActionsPanel")); 97 } 98 99 104 private void initComponents() { 106 java.awt.GridBagConstraints gridBagConstraints; 107 108 splitPane = new javax.swing.JSplitPane (); 109 explorerPanel = new org.netbeans.beaninfo.ExplorerPanel(); 110 beanTreeView2 = new org.openide.explorer.view.BeanTreeView(); 111 jScrollPane1 = new javax.swing.JScrollPane (); 112 list = new javax.swing.JList (); 113 jPanel2 = new javax.swing.JPanel (); 114 jPanel1 = new javax.swing.JPanel (); 115 addButton = new javax.swing.JButton (); 116 separatorButton = new javax.swing.JButton (); 117 removeButton = new javax.swing.JButton (); 118 upButton = new javax.swing.JButton (); 119 downButton = new javax.swing.JButton (); 120 121 setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 11)); 122 setPreferredSize(new java.awt.Dimension (600, 500)); 123 setLayout(new java.awt.BorderLayout (11, 0)); 124 125 splitPane.setLastDividerLocation(300); 126 127 explorerPanel.setLayout(new java.awt.BorderLayout ()); 128 129 beanTreeView2.setDefaultActionAllowed(false); 130 beanTreeView2.setPopupAllowed(false); 131 explorerPanel.add(beanTreeView2, java.awt.BorderLayout.CENTER); 132 133 splitPane.setLeftComponent(explorerPanel); 134 135 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 136 list.setCellRenderer(this); 137 list.addFocusListener(new java.awt.event.FocusAdapter () { 138 public void focusGained(java.awt.event.FocusEvent evt) { 139 listFocusGained(evt); 140 } 141 }); 142 list.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 143 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 144 listValueChanged(evt); 145 } 146 }); 147 jScrollPane1.setViewportView(list); 148 149 splitPane.setRightComponent(jScrollPane1); 150 151 add(splitPane, java.awt.BorderLayout.CENTER); 152 153 jPanel2.setLayout(new java.awt.FlowLayout (java.awt.FlowLayout.CENTER, 0, 0)); 154 155 jPanel1.setLayout(new java.awt.GridBagLayout ()); 156 157 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/beaninfo/Bundle"); org.openide.awt.Mnemonics.setLocalizedText(addButton, bundle.getString("LoaderActionsPanel.jButton1.text")); addButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton1.toolTipText")); addButton.setEnabled(false); 161 addButton.addActionListener(new java.awt.event.ActionListener () { 162 public void actionPerformed(java.awt.event.ActionEvent evt) { 163 addButtonActionPerformed(evt); 164 } 165 }); 166 gridBagConstraints = new java.awt.GridBagConstraints (); 167 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 168 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 169 jPanel1.add(addButton, gridBagConstraints); 170 171 org.openide.awt.Mnemonics.setLocalizedText(separatorButton, bundle.getString("LoaderActionsPanel.jButton2.text")); separatorButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton2.toolTipText")); separatorButton.addActionListener(new java.awt.event.ActionListener () { 174 public void actionPerformed(java.awt.event.ActionEvent evt) { 175 separatorButtonActionPerformed(evt); 176 } 177 }); 178 gridBagConstraints = new java.awt.GridBagConstraints (); 179 gridBagConstraints.gridx = 0; 180 gridBagConstraints.gridy = 1; 181 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 182 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 183 jPanel1.add(separatorButton, gridBagConstraints); 184 185 org.openide.awt.Mnemonics.setLocalizedText(removeButton, bundle.getString("LoaderActionsPanel.jButton3.text")); removeButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton3.toolTipText")); removeButton.setEnabled(false); 188 removeButton.addActionListener(new java.awt.event.ActionListener () { 189 public void actionPerformed(java.awt.event.ActionEvent evt) { 190 removeButtonActionPerformed(evt); 191 } 192 }); 193 gridBagConstraints = new java.awt.GridBagConstraints (); 194 gridBagConstraints.gridx = 0; 195 gridBagConstraints.gridy = 2; 196 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 197 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 198 jPanel1.add(removeButton, gridBagConstraints); 199 200 org.openide.awt.Mnemonics.setLocalizedText(upButton, bundle.getString("LoaderActionsPanel.jButton4.text")); upButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton4.toolTipText")); upButton.setEnabled(false); 203 upButton.addActionListener(new java.awt.event.ActionListener () { 204 public void actionPerformed(java.awt.event.ActionEvent evt) { 205 upButtonActionPerformed(evt); 206 } 207 }); 208 gridBagConstraints = new java.awt.GridBagConstraints (); 209 gridBagConstraints.gridx = 0; 210 gridBagConstraints.gridy = 3; 211 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 212 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 213 jPanel1.add(upButton, gridBagConstraints); 214 215 org.openide.awt.Mnemonics.setLocalizedText(downButton, bundle.getString("LoaderActionsPanel.jButton5.text")); downButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton5.toolTipText")); downButton.setEnabled(false); 218 downButton.addActionListener(new java.awt.event.ActionListener () { 219 public void actionPerformed(java.awt.event.ActionEvent evt) { 220 downButtonActionPerformed(evt); 221 } 222 }); 223 gridBagConstraints = new java.awt.GridBagConstraints (); 224 gridBagConstraints.gridx = 0; 225 gridBagConstraints.gridy = 4; 226 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 227 jPanel1.add(downButton, gridBagConstraints); 228 229 jPanel2.add(jPanel1); 230 231 add(jPanel2, java.awt.BorderLayout.EAST); 232 } 234 private void listFocusGained(java.awt.event.FocusEvent evt) { if (list.getSelectedIndex() == -1 && list.getModel().getSize() > 0) { 237 list.setSelectedIndex(0); 238 } 239 } 241 private void listValueChanged (javax.swing.event.ListSelectionEvent evt) { int index = list.getSelectedIndex (); 243 if (index == -1) { 244 downButton.setEnabled (false); 245 upButton.setEnabled (false); 246 removeButton.setEnabled (false); 247 } else { 248 removeButton.setEnabled (true); 251 downButton.setEnabled (index != model.getSize () - 1); 252 upButton.setEnabled (index != 0); 253 } 254 } 256 private void downButtonActionPerformed (java.awt.event.ActionEvent evt) { int index = list.getSelectedIndex (); 258 if (index == -1 || index == model.getSize () - 1) return; 259 Object temp = model.elementAt (index); 260 model.setElementAt (model.elementAt (index + 1), index); 261 model.setElementAt (temp, index + 1); 262 list.setSelectedIndex (index + 1); 263 } 265 private void upButtonActionPerformed (java.awt.event.ActionEvent evt) { int index = list.getSelectedIndex (); 267 if (index == -1 || index == 0) return; 268 Object temp = model.elementAt (index); 269 model.setElementAt (model.elementAt (index - 1), index); 270 model.setElementAt (temp, index - 1); 271 list.setSelectedIndex (index - 1); 272 } 274 private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) { int index = list.getSelectedIndex (); 276 if (index == -1) return; 277 model.remove (index); 278 if (model.getSize () == 0) 279 list.setSelectedIndices (new int[] { }); 280 else 281 list.setSelectedIndex (Math.min (index, model.getSize () - 1)); 282 } 284 private void separatorButtonActionPerformed (java.awt.event.ActionEvent evt) { model.addElement (null); 286 list.setSelectedIndex (model.getSize () - 1); 287 } 289 private void addButtonActionPerformed (java.awt.event.ActionEvent evt) { Node[] nodes = mgr.getSelectedNodes (); 291 SystemAction action = findAction (nodes); 292 if (action != null) model.addElement (action); 293 list.setSelectedIndex (model.getSize () - 1); 294 } 296 297 private javax.swing.JButton addButton; 299 private org.openide.explorer.view.BeanTreeView beanTreeView2; 300 private javax.swing.JButton downButton; 301 private org.netbeans.beaninfo.ExplorerPanel explorerPanel; 302 private javax.swing.JPanel jPanel1; 303 private javax.swing.JPanel jPanel2; 304 private javax.swing.JScrollPane jScrollPane1; 305 private javax.swing.JList list; 306 private javax.swing.JButton removeButton; 307 private javax.swing.JButton separatorButton; 308 private javax.swing.JSplitPane splitPane; 309 private javax.swing.JButton upButton; 310 312 313 static SystemAction findAction (Node[] nodes) { 314 if (nodes == null || nodes.length == 0 || nodes.length > 1) return null; 315 InstanceCookie inst = (InstanceCookie) nodes[0].getCookie (InstanceCookie.class); 316 if (inst == null) return null; 317 try { 318 Class clazz = inst.instanceClass (); 319 if (! SystemAction.class.isAssignableFrom (clazz)) return null; 320 return (SystemAction) inst.instanceCreate (); 321 } catch (Exception e) { 322 Logger.getLogger(LoaderActionsPanel.class.getName()).log(Level.WARNING, null, e); 323 return null; 324 } 325 } 326 327 332 private Object getPropertyValue() throws IllegalStateException { 333 SystemAction[] actions = new SystemAction[model.getSize ()]; 334 model.copyInto (actions); 335 return actions; 336 } 337 338 public void propertyChange(PropertyChangeEvent evt) { 339 if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) { 340 editor.setValue(getPropertyValue()); 341 } 342 } 343 344 362 public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) { 363 JLabel label = new JLabel (); 364 if (value != null) { 365 SystemAction action = (SystemAction) value; 366 try { 367 String name = action.getName (); 368 if (name == null) name = NbBundle.getBundle(LoaderActionsPanel.class).getString("LBL_no_system_action_name"); 369 label.setText (Actions.cutAmpersand (name)); 370 Icon icon = action.getIcon (); 371 if (icon != null) label.setIcon (icon); 372 } catch (RuntimeException re) { 373 Logger.getLogger(LoaderActionsPanel.class.getName()).log(Level.WARNING, null, re); 376 } 377 } else { 378 label.setText (NbBundle.getBundle(LoaderActionsPanel.class).getString("LBL_separator_rather_than_action")); 379 try { 381 label.setIcon(new ImageIcon (new URL ("nbresloc:/org/openide/resources/actions/empty.gif"))); } catch (MalformedURLException ex) { 384 assert false : ex; 385 } 386 } 387 if (isSelected) { 388 label.setBackground(list.getSelectionBackground()); 389 label.setForeground(list.getSelectionForeground()); 390 } else { 391 label.setBackground(list.getBackground()); 392 label.setForeground(list.getForeground()); 393 } 394 label.setEnabled(list.isEnabled()); 395 label.setFont(list.getFont()); 396 label.setOpaque (true); 397 return label; 398 } 399 } 400
| Popular Tags
|