~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to core/src/org/netbeans/beaninfo/LoaderActionsPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.beaninfo;
 
43
 
 
44
import java.awt.Component;
 
45
import java.beans.PropertyChangeEvent;
 
46
import java.beans.PropertyChangeListener;
 
47
import java.beans.PropertyEditor;
 
48
import java.net.MalformedURLException;
 
49
import java.net.URL;
 
50
import java.util.logging.Level;
 
51
import java.util.logging.Logger;
 
52
import javax.accessibility.AccessibleContext;
 
53
import javax.swing.DefaultListModel;
 
54
import javax.swing.Icon;
 
55
import javax.swing.ImageIcon;
 
56
import javax.swing.JLabel;
 
57
import javax.swing.JList;
 
58
import javax.swing.ListCellRenderer;
 
59
import javax.swing.ListSelectionModel;
 
60
import org.netbeans.core.ActionsPoolNode;
 
61
import org.openide.awt.Actions;
 
62
import org.openide.cookies.InstanceCookie;
 
63
import org.openide.explorer.ExplorerManager;
 
64
import org.openide.explorer.propertysheet.PropertyEnv;
 
65
import org.openide.nodes.Node;
 
66
import org.openide.util.NbBundle;
 
67
import org.openide.util.actions.SystemAction;
 
68
 
 
69
/** Custom editor for data loader actions list.
 
70
 *
 
71
 * @author Jesse Glick
 
72
 */
 
73
public class LoaderActionsPanel extends javax.swing.JPanel implements PropertyChangeListener, ListCellRenderer {
 
74
 
 
75
    private DefaultListModel model;
 
76
    private ExplorerManager mgr;
 
77
    private PropertyEditor editor;
 
78
 
 
79
    /** Creates new form LoaderActionsPanel */
 
80
    public LoaderActionsPanel (PropertyEditor pe, PropertyEnv env) {
 
81
        env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
 
82
        env.addPropertyChangeListener(this);
 
83
        this.editor = pe;
 
84
 
 
85
 
 
86
        initComponents ();
 
87
        model = new DefaultListModel ();
 
88
        SystemAction[] actions = (SystemAction[]) pe.getValue ();
 
89
        if (actions == null) actions = new SystemAction[] { };
 
90
        for (int i = 0; i < actions.length; i++)
 
91
            model.addElement (actions[i]);
 
92
        list.setModel (model);
 
93
        mgr = explorerPanel.getExplorerManager ();
 
94
        mgr.setRootContext (new ActionsPoolNode ());
 
95
        mgr.addPropertyChangeListener (new PropertyChangeListener () {
 
96
                                           public void propertyChange (PropertyChangeEvent ev) {
 
97
                                               if (ExplorerManager.PROP_SELECTED_NODES.equals (ev.getPropertyName ())) {
 
98
                                                   SystemAction action = findAction (mgr.getSelectedNodes ());
 
99
                                                   addButton.setEnabled (action != null);
 
100
                                               }
 
101
                                           }
 
102
                                       });
 
103
        // bugfix #39369: remove help button in Action dialog property editor
 
104
        //HelpCtx.setHelpIDString (this, LoaderActionsPanel.class.getName ());
 
105
                                       
 
106
        // Form Editor does not permit you to set this, because
 
107
        // it has both int and double params:
 
108
        splitPane.setDividerLocation (300);
 
109
        
 
110
        java.util.ResourceBundle bundle = NbBundle.getBundle(LoaderActionsPanel.class);
 
111
        
 
112
        AccessibleContext ac = beanTreeView2.getAccessibleContext();
 
113
        ac.setAccessibleName(bundle.getString("ACS_LoaderActionsPanel.beanTreeView"));
 
114
        ac.setAccessibleDescription(bundle.getString("ACSD_LoaderActionsPanel.beanTreeView"));
 
115
        ac = list.getAccessibleContext();
 
116
        ac.setAccessibleName(bundle.getString("ACS_LoaderActionsPanel.list"));
 
117
        ac.setAccessibleDescription(bundle.getString("ACSD_LoaderActionsPanel.list"));
 
118
        getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_LoaderActionsPanel"));
 
119
    }
 
120
 
 
121
    /** This method is called from within the constructor to
 
122
     * initialize the form.
 
123
     * WARNING: Do NOT modify this code. The content of this method is
 
124
     * always regenerated by the FormEditor.
 
125
     */
 
126
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
 
127
    private void initComponents() {
 
128
        java.awt.GridBagConstraints gridBagConstraints;
 
129
 
 
130
        splitPane = new javax.swing.JSplitPane();
 
131
        explorerPanel = new org.netbeans.beaninfo.ExplorerPanel();
 
132
        beanTreeView2 = new org.openide.explorer.view.BeanTreeView();
 
133
        jScrollPane1 = new javax.swing.JScrollPane();
 
134
        list = new javax.swing.JList();
 
135
        jPanel2 = new javax.swing.JPanel();
 
136
        jPanel1 = new javax.swing.JPanel();
 
137
        addButton = new javax.swing.JButton();
 
138
        separatorButton = new javax.swing.JButton();
 
139
        removeButton = new javax.swing.JButton();
 
140
        upButton = new javax.swing.JButton();
 
141
        downButton = new javax.swing.JButton();
 
142
 
 
143
        setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 11));
 
144
        setPreferredSize(new java.awt.Dimension(600, 500));
 
145
        setLayout(new java.awt.BorderLayout(11, 0));
 
146
 
 
147
        splitPane.setLastDividerLocation(300);
 
148
 
 
149
        explorerPanel.setLayout(new java.awt.BorderLayout());
 
150
 
 
151
        beanTreeView2.setDefaultActionAllowed(false);
 
152
        beanTreeView2.setPopupAllowed(false);
 
153
        explorerPanel.add(beanTreeView2, java.awt.BorderLayout.CENTER);
 
154
 
 
155
        splitPane.setLeftComponent(explorerPanel);
 
156
 
 
157
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
158
        list.setCellRenderer(this);
 
159
        list.addFocusListener(new java.awt.event.FocusAdapter() {
 
160
            public void focusGained(java.awt.event.FocusEvent evt) {
 
161
                listFocusGained(evt);
 
162
            }
 
163
        });
 
164
        list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
 
165
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
 
166
                listValueChanged(evt);
 
167
            }
 
168
        });
 
169
        jScrollPane1.setViewportView(list);
 
170
 
 
171
        splitPane.setRightComponent(jScrollPane1);
 
172
 
 
173
        add(splitPane, java.awt.BorderLayout.CENTER);
 
174
 
 
175
        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));
 
176
 
 
177
        jPanel1.setLayout(new java.awt.GridBagLayout());
 
178
 
 
179
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/beaninfo/Bundle"); // NOI18N
 
180
        org.openide.awt.Mnemonics.setLocalizedText(addButton, bundle.getString("LoaderActionsPanel.jButton1.text")); // NOI18N
 
181
        addButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton1.toolTipText")); // NOI18N
 
182
        addButton.setEnabled(false);
 
183
        addButton.addActionListener(new java.awt.event.ActionListener() {
 
184
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
185
                addButtonActionPerformed(evt);
 
186
            }
 
187
        });
 
188
        gridBagConstraints = new java.awt.GridBagConstraints();
 
189
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
190
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
 
191
        jPanel1.add(addButton, gridBagConstraints);
 
192
 
 
193
        org.openide.awt.Mnemonics.setLocalizedText(separatorButton, bundle.getString("LoaderActionsPanel.jButton2.text")); // NOI18N
 
194
        separatorButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton2.toolTipText")); // NOI18N
 
195
        separatorButton.addActionListener(new java.awt.event.ActionListener() {
 
196
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
197
                separatorButtonActionPerformed(evt);
 
198
            }
 
199
        });
 
200
        gridBagConstraints = new java.awt.GridBagConstraints();
 
201
        gridBagConstraints.gridx = 0;
 
202
        gridBagConstraints.gridy = 1;
 
203
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
204
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
 
205
        jPanel1.add(separatorButton, gridBagConstraints);
 
206
 
 
207
        org.openide.awt.Mnemonics.setLocalizedText(removeButton, bundle.getString("LoaderActionsPanel.jButton3.text")); // NOI18N
 
208
        removeButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton3.toolTipText")); // NOI18N
 
209
        removeButton.setEnabled(false);
 
210
        removeButton.addActionListener(new java.awt.event.ActionListener() {
 
211
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
212
                removeButtonActionPerformed(evt);
 
213
            }
 
214
        });
 
215
        gridBagConstraints = new java.awt.GridBagConstraints();
 
216
        gridBagConstraints.gridx = 0;
 
217
        gridBagConstraints.gridy = 2;
 
218
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
219
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 11, 0);
 
220
        jPanel1.add(removeButton, gridBagConstraints);
 
221
 
 
222
        org.openide.awt.Mnemonics.setLocalizedText(upButton, bundle.getString("LoaderActionsPanel.jButton4.text")); // NOI18N
 
223
        upButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton4.toolTipText")); // NOI18N
 
224
        upButton.setEnabled(false);
 
225
        upButton.addActionListener(new java.awt.event.ActionListener() {
 
226
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
227
                upButtonActionPerformed(evt);
 
228
            }
 
229
        });
 
230
        gridBagConstraints = new java.awt.GridBagConstraints();
 
231
        gridBagConstraints.gridx = 0;
 
232
        gridBagConstraints.gridy = 3;
 
233
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
234
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
 
235
        jPanel1.add(upButton, gridBagConstraints);
 
236
 
 
237
        org.openide.awt.Mnemonics.setLocalizedText(downButton, bundle.getString("LoaderActionsPanel.jButton5.text")); // NOI18N
 
238
        downButton.setToolTipText(bundle.getString("LoaderActionsPanel.jButton5.toolTipText")); // NOI18N
 
239
        downButton.setEnabled(false);
 
240
        downButton.addActionListener(new java.awt.event.ActionListener() {
 
241
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
242
                downButtonActionPerformed(evt);
 
243
            }
 
244
        });
 
245
        gridBagConstraints = new java.awt.GridBagConstraints();
 
246
        gridBagConstraints.gridx = 0;
 
247
        gridBagConstraints.gridy = 4;
 
248
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
249
        jPanel1.add(downButton, gridBagConstraints);
 
250
 
 
251
        jPanel2.add(jPanel1);
 
252
 
 
253
        add(jPanel2, java.awt.BorderLayout.EAST);
 
254
    }// </editor-fold>//GEN-END:initComponents
 
255
 
 
256
    private void listFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_listFocusGained
 
257
        // Add your handling code here:
 
258
        if (list.getSelectedIndex() == -1 && list.getModel().getSize() > 0) {
 
259
            list.setSelectedIndex(0);
 
260
        }
 
261
    }//GEN-LAST:event_listFocusGained
 
262
 
 
263
    private void listValueChanged (javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listValueChanged
 
264
        int index = list.getSelectedIndex ();
 
265
        if (index == -1) {
 
266
            downButton.setEnabled (false);
 
267
            upButton.setEnabled (false);
 
268
            removeButton.setEnabled (false);
 
269
        } else {
 
270
            // [PENDING] remove button enabled after removing last action
 
271
            // but it is harmless to press it
 
272
            removeButton.setEnabled (true);
 
273
            downButton.setEnabled (index != model.getSize () - 1);
 
274
            upButton.setEnabled (index != 0);
 
275
        }
 
276
    }//GEN-LAST:event_listValueChanged
 
277
 
 
278
    private void downButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed
 
279
        int index = list.getSelectedIndex ();
 
280
        if (index == -1 || index == model.getSize () - 1) return;
 
281
        Object temp = model.elementAt (index);
 
282
        model.setElementAt (model.elementAt (index + 1), index);
 
283
        model.setElementAt (temp, index + 1);
 
284
        list.setSelectedIndex (index + 1);
 
285
    }//GEN-LAST:event_downButtonActionPerformed
 
286
 
 
287
    private void upButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
 
288
        int index = list.getSelectedIndex ();
 
289
        if (index == -1 || index == 0) return;
 
290
        Object temp = model.elementAt (index);
 
291
        model.setElementAt (model.elementAt (index - 1), index);
 
292
        model.setElementAt (temp, index - 1);
 
293
        list.setSelectedIndex (index - 1);
 
294
    }//GEN-LAST:event_upButtonActionPerformed
 
295
 
 
296
    private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
 
297
        int index = list.getSelectedIndex ();
 
298
        if (index == -1) return;
 
299
        model.remove (index);
 
300
        if (model.getSize () == 0)
 
301
            list.setSelectedIndices (new int[] { });
 
302
        else
 
303
            list.setSelectedIndex (Math.min (index, model.getSize () - 1));
 
304
    }//GEN-LAST:event_removeButtonActionPerformed
 
305
 
 
306
    private void separatorButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_separatorButtonActionPerformed
 
307
        model.addElement (null);
 
308
        list.setSelectedIndex (model.getSize () - 1);
 
309
    }//GEN-LAST:event_separatorButtonActionPerformed
 
310
 
 
311
    private void addButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
 
312
        Node[] nodes = mgr.getSelectedNodes ();
 
313
        SystemAction action = findAction (nodes);
 
314
        if (action != null) model.addElement (action);
 
315
        list.setSelectedIndex (model.getSize () - 1);
 
316
    }//GEN-LAST:event_addButtonActionPerformed
 
317
 
 
318
 
 
319
    // Variables declaration - do not modify//GEN-BEGIN:variables
 
320
    private javax.swing.JButton addButton;
 
321
    private org.openide.explorer.view.BeanTreeView beanTreeView2;
 
322
    private javax.swing.JButton downButton;
 
323
    private org.netbeans.beaninfo.ExplorerPanel explorerPanel;
 
324
    private javax.swing.JPanel jPanel1;
 
325
    private javax.swing.JPanel jPanel2;
 
326
    private javax.swing.JScrollPane jScrollPane1;
 
327
    private javax.swing.JList list;
 
328
    private javax.swing.JButton removeButton;
 
329
    private javax.swing.JButton separatorButton;
 
330
    private javax.swing.JSplitPane splitPane;
 
331
    private javax.swing.JButton upButton;
 
332
    // End of variables declaration//GEN-END:variables
 
333
 
 
334
    /** Try to find a system action instance among a set of nodes. */
 
335
    static SystemAction findAction (Node[] nodes) {
 
336
        if (nodes == null || nodes.length == 0 || nodes.length > 1) return null;
 
337
        InstanceCookie inst = (InstanceCookie) nodes[0].getCookie (InstanceCookie.class);
 
338
        if (inst == null) return null;
 
339
        try {
 
340
            Class clazz = inst.instanceClass ();
 
341
            if (! SystemAction.class.isAssignableFrom (clazz)) return null;
 
342
            return (SystemAction) inst.instanceCreate ();
 
343
        } catch (Exception e) {
 
344
            Logger.getLogger(LoaderActionsPanel.class.getName()).log(Level.WARNING, null, e);
 
345
            return null;
 
346
        }
 
347
    }
 
348
 
 
349
    /** Get the customized property value.
 
350
     * @return the property value
 
351
     * @exception InvalidStateException when the custom property editor does not contain a valid property value
 
352
     *            (and thus it should not be set)
 
353
     */
 
354
    private Object getPropertyValue() throws IllegalStateException {
 
355
        SystemAction[] actions = new SystemAction[model.getSize ()];
 
356
        model.copyInto (actions);
 
357
        return actions;
 
358
    }
 
359
 
 
360
    public void propertyChange(PropertyChangeEvent evt) {
 
361
        if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) {
 
362
            editor.setValue(getPropertyValue());
 
363
        }
 
364
    }
 
365
 
 
366
    /** Return a component that has been configured to display the specified
 
367
     * value. That component's <code>paint</code> method is then called to
 
368
     * "render" the cell.  If it is necessary to compute the dimensions
 
369
     * of a list because the list cells do not have a fixed size, this method
 
370
     * is called to generate a component on which <code>getPreferredSize</code>
 
371
     * can be invoked.
 
372
     *
 
373
     * @param list The JList we're painting.
 
374
     * @param value The value returned by list.getModel().getElementAt(index).
 
375
     * @param index The cells index.
 
376
     * @param isSelected True if the specified cell was selected.
 
377
     * @param cellHasFocus True if the specified cell has the focus.
 
378
     * @return A component whose paint() method will render the specified value.
 
379
     *
 
380
     * @see JList
 
381
     * @see ListSelectionModel
 
382
     * @see ListModel
 
383
     */
 
384
    public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
 
385
        JLabel label = new JLabel ();
 
386
        if (value != null) {
 
387
            SystemAction action = (SystemAction) value;
 
388
            try {
 
389
                String name = action.getName ();
 
390
                if (name == null) name = NbBundle.getBundle(LoaderActionsPanel.class).getString("LBL_no_system_action_name");
 
391
                label.setText (Actions.cutAmpersand (name));
 
392
                Icon icon = action.getIcon ();
 
393
                if (icon != null) label.setIcon (icon);
 
394
            } catch (RuntimeException re) {
 
395
                // May happen if actions are misconfigured, bogus icons, etc.
 
396
                // So best to recover semigracefully.
 
397
                Logger.getLogger(LoaderActionsPanel.class.getName()).log(Level.WARNING, null, re);
 
398
            }
 
399
        } else {
 
400
            label.setText (NbBundle.getBundle(LoaderActionsPanel.class).getString("LBL_separator_rather_than_action"));
 
401
            // For alignment:
 
402
            try {
 
403
                // For alignment:
 
404
                label.setIcon(new ImageIcon(new URL("nbresloc:/org/openide/resources/actions/empty.gif"))); // NOI18N
 
405
            } catch (MalformedURLException ex) {
 
406
                assert false : ex;
 
407
            }
 
408
        }
 
409
        if (isSelected) {
 
410
            label.setBackground(list.getSelectionBackground());
 
411
            label.setForeground(list.getSelectionForeground());
 
412
        } else {
 
413
            label.setBackground(list.getBackground());
 
414
            label.setForeground(list.getForeground());
 
415
        }
 
416
        label.setEnabled(list.isEnabled());
 
417
        label.setFont(list.getFont());
 
418
        label.setOpaque (true);
 
419
        return label;
 
420
    }
 
421
}