KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > LoaderActionsPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.beaninfo;
21
22 import java.awt.Component JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.beans.PropertyEditor JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import javax.accessibility.AccessibleContext JavaDoc;
31 import javax.swing.DefaultListModel JavaDoc;
32 import javax.swing.Icon JavaDoc;
33 import javax.swing.ImageIcon JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JList JavaDoc;
36 import javax.swing.ListCellRenderer JavaDoc;
37 import javax.swing.ListSelectionModel JavaDoc;
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 /** Custom editor for data loader actions list.
48  *
49  * @author Jesse Glick
50  */

51 public class LoaderActionsPanel extends javax.swing.JPanel JavaDoc implements PropertyChangeListener JavaDoc, ListCellRenderer JavaDoc {
52
53     private DefaultListModel JavaDoc model;
54     private ExplorerManager mgr;
55     private PropertyEditor JavaDoc editor;
56
57     /** Creates new form LoaderActionsPanel */
58     public LoaderActionsPanel (PropertyEditor JavaDoc 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 JavaDoc ();
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 JavaDoc () {
74                                            public void propertyChange (PropertyChangeEvent JavaDoc 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         // bugfix #39369: remove help button in Action dialog property editor
82
//HelpCtx.setHelpIDString (this, LoaderActionsPanel.class.getName ());
83

84         // Form Editor does not permit you to set this, because
85
// it has both int and double params:
86
splitPane.setDividerLocation (300);
87         
88         java.util.ResourceBundle JavaDoc bundle = NbBundle.getBundle(LoaderActionsPanel.class);
89         
90         AccessibleContext JavaDoc 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     /** This method is called from within the constructor to
100      * initialize the form.
101      * WARNING: Do NOT modify this code. The content of this method is
102      * always regenerated by the FormEditor.
103      */

104     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
105
private void initComponents() {
106         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
107
108         splitPane = new javax.swing.JSplitPane JavaDoc();
109         explorerPanel = new org.netbeans.beaninfo.ExplorerPanel();
110         beanTreeView2 = new org.openide.explorer.view.BeanTreeView();
111         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
112         list = new javax.swing.JList JavaDoc();
113         jPanel2 = new javax.swing.JPanel JavaDoc();
114         jPanel1 = new javax.swing.JPanel JavaDoc();
115         addButton = new javax.swing.JButton JavaDoc();
116         separatorButton = new javax.swing.JButton JavaDoc();
117         removeButton = new javax.swing.JButton JavaDoc();
118         upButton = new javax.swing.JButton JavaDoc();
119         downButton = new javax.swing.JButton JavaDoc();
120
121         setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 11));
122         setPreferredSize(new java.awt.Dimension JavaDoc(600, 500));
123         setLayout(new java.awt.BorderLayout JavaDoc(11, 0));
124
125         splitPane.setLastDividerLocation(300);
126
127         explorerPanel.setLayout(new java.awt.BorderLayout JavaDoc());
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 JavaDoc() {
138             public void focusGained(java.awt.event.FocusEvent JavaDoc evt) {
139                 listFocusGained(evt);
140             }
141         });
142         list.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
143             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc 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 JavaDoc(java.awt.FlowLayout.CENTER, 0, 0));
154
155         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
156
157         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/beaninfo/Bundle"); // NOI18N
158
org.openide.awt.Mnemonics.setLocalizedText(addButton, bundle.getString("LoaderActionsPanel.jButton1.text")); // NOI18N
159
addButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton1.toolTipText")); // NOI18N
160
addButton.setEnabled(false);
161         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
162             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
163                 addButtonActionPerformed(evt);
164             }
165         });
166         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
167         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
168         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
169         jPanel1.add(addButton, gridBagConstraints);
170
171         org.openide.awt.Mnemonics.setLocalizedText(separatorButton, bundle.getString("LoaderActionsPanel.jButton2.text")); // NOI18N
172
separatorButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton2.toolTipText")); // NOI18N
173
separatorButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
174             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
175                 separatorButtonActionPerformed(evt);
176             }
177         });
178         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
179         gridBagConstraints.gridx = 0;
180         gridBagConstraints.gridy = 1;
181         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
182         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
183         jPanel1.add(separatorButton, gridBagConstraints);
184
185         org.openide.awt.Mnemonics.setLocalizedText(removeButton, bundle.getString("LoaderActionsPanel.jButton3.text")); // NOI18N
186
removeButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton3.toolTipText")); // NOI18N
187
removeButton.setEnabled(false);
188         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
189             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
190                 removeButtonActionPerformed(evt);
191             }
192         });
193         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
194         gridBagConstraints.gridx = 0;
195         gridBagConstraints.gridy = 2;
196         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
197         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
198         jPanel1.add(removeButton, gridBagConstraints);
199
200         org.openide.awt.Mnemonics.setLocalizedText(upButton, bundle.getString("LoaderActionsPanel.jButton4.text")); // NOI18N
201
upButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton4.toolTipText")); // NOI18N
202
upButton.setEnabled(false);
203         upButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
204             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
205                 upButtonActionPerformed(evt);
206             }
207         });
208         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
209         gridBagConstraints.gridx = 0;
210         gridBagConstraints.gridy = 3;
211         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
212         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
213         jPanel1.add(upButton, gridBagConstraints);
214
215         org.openide.awt.Mnemonics.setLocalizedText(downButton, bundle.getString("LoaderActionsPanel.jButton5.text")); // NOI18N
216
downButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton5.toolTipText")); // NOI18N
217
downButton.setEnabled(false);
218         downButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
219             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
220                 downButtonActionPerformed(evt);
221             }
222         });
223         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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     }// </editor-fold>//GEN-END:initComponents
233

234     private void listFocusGained(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_listFocusGained
235
// Add your handling code here:
236
if (list.getSelectedIndex() == -1 && list.getModel().getSize() > 0) {
237             list.setSelectedIndex(0);
238         }
239     }//GEN-LAST:event_listFocusGained
240

241     private void listValueChanged (javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_listValueChanged
242
int index = list.getSelectedIndex ();
243         if (index == -1) {
244             downButton.setEnabled (false);
245             upButton.setEnabled (false);
246             removeButton.setEnabled (false);
247         } else {
248             // [PENDING] remove button enabled after removing last action
249
// but it is harmless to press it
250
removeButton.setEnabled (true);
251             downButton.setEnabled (index != model.getSize () - 1);
252             upButton.setEnabled (index != 0);
253         }
254     }//GEN-LAST:event_listValueChanged
255

256     private void downButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_downButtonActionPerformed
257
int index = list.getSelectedIndex ();
258         if (index == -1 || index == model.getSize () - 1) return;
259         Object JavaDoc temp = model.elementAt (index);
260         model.setElementAt (model.elementAt (index + 1), index);
261         model.setElementAt (temp, index + 1);
262         list.setSelectedIndex (index + 1);
263     }//GEN-LAST:event_downButtonActionPerformed
264

265     private void upButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_upButtonActionPerformed
266
int index = list.getSelectedIndex ();
267         if (index == -1 || index == 0) return;
268         Object JavaDoc temp = model.elementAt (index);
269         model.setElementAt (model.elementAt (index - 1), index);
270         model.setElementAt (temp, index - 1);
271         list.setSelectedIndex (index - 1);
272     }//GEN-LAST:event_upButtonActionPerformed
273

274     private void removeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
275
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     }//GEN-LAST:event_removeButtonActionPerformed
283

284     private void separatorButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_separatorButtonActionPerformed
285
model.addElement (null);
286         list.setSelectedIndex (model.getSize () - 1);
287     }//GEN-LAST:event_separatorButtonActionPerformed
288

289     private void addButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
290
Node[] nodes = mgr.getSelectedNodes ();
291         SystemAction action = findAction (nodes);
292         if (action != null) model.addElement (action);
293         list.setSelectedIndex (model.getSize () - 1);
294     }//GEN-LAST:event_addButtonActionPerformed
295

296
297     // Variables declaration - do not modify//GEN-BEGIN:variables
298
private javax.swing.JButton JavaDoc addButton;
299     private org.openide.explorer.view.BeanTreeView beanTreeView2;
300     private javax.swing.JButton JavaDoc downButton;
301     private org.netbeans.beaninfo.ExplorerPanel explorerPanel;
302     private javax.swing.JPanel JavaDoc jPanel1;
303     private javax.swing.JPanel JavaDoc jPanel2;
304     private javax.swing.JScrollPane JavaDoc jScrollPane1;
305     private javax.swing.JList JavaDoc list;
306     private javax.swing.JButton JavaDoc removeButton;
307     private javax.swing.JButton JavaDoc separatorButton;
308     private javax.swing.JSplitPane JavaDoc splitPane;
309     private javax.swing.JButton JavaDoc upButton;
310     // End of variables declaration//GEN-END:variables
311

312     /** Try to find a system action instance among a set of nodes. */
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 JavaDoc clazz = inst.instanceClass ();
319             if (! SystemAction.class.isAssignableFrom (clazz)) return null;
320             return (SystemAction) inst.instanceCreate ();
321         } catch (Exception JavaDoc e) {
322             Logger.getLogger(LoaderActionsPanel.class.getName()).log(Level.WARNING, null, e);
323             return null;
324         }
325     }
326
327     /** Get the customized property value.
328      * @return the property value
329      * @exception InvalidStateException when the custom property editor does not contain a valid property value
330      * (and thus it should not be set)
331      */

332     private Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
333         SystemAction[] actions = new SystemAction[model.getSize ()];
334         model.copyInto (actions);
335         return actions;
336     }
337
338     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
339         if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) {
340             editor.setValue(getPropertyValue());
341         }
342     }
343
344     /** Return a component that has been configured to display the specified
345      * value. That component's <code>paint</code> method is then called to
346      * "render" the cell. If it is necessary to compute the dimensions
347      * of a list because the list cells do not have a fixed size, this method
348      * is called to generate a component on which <code>getPreferredSize</code>
349      * can be invoked.
350      *
351      * @param list The JList we're painting.
352      * @param value The value returned by list.getModel().getElementAt(index).
353      * @param index The cells index.
354      * @param isSelected True if the specified cell was selected.
355      * @param cellHasFocus True if the specified cell has the focus.
356      * @return A component whose paint() method will render the specified value.
357      *
358      * @see JList
359      * @see ListSelectionModel
360      * @see ListModel
361      */

362     public Component JavaDoc getListCellRendererComponent(JList JavaDoc list,Object JavaDoc value,int index,boolean isSelected,boolean cellHasFocus) {
363         JLabel JavaDoc label = new JLabel JavaDoc ();
364         if (value != null) {
365             SystemAction action = (SystemAction) value;
366             try {
367                 String JavaDoc 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 JavaDoc icon = action.getIcon ();
371                 if (icon != null) label.setIcon (icon);
372             } catch (RuntimeException JavaDoc re) {
373                 // May happen if actions are misconfigured, bogus icons, etc.
374
// So best to recover semigracefully.
375
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             // For alignment:
380
try {
381                 // For alignment:
382
label.setIcon(new ImageIcon JavaDoc(new URL JavaDoc("nbresloc:/org/openide/resources/actions/empty.gif"))); // NOI18N
383
} catch (MalformedURLException JavaDoc 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