2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
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.
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.
42
package org.netbeans.beaninfo;
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;
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;
69
/** Custom editor for data loader actions list.
73
public class LoaderActionsPanel extends javax.swing.JPanel implements PropertyChangeListener, ListCellRenderer {
75
private DefaultListModel model;
76
private ExplorerManager mgr;
77
private PropertyEditor editor;
79
/** Creates new form LoaderActionsPanel */
80
public LoaderActionsPanel (PropertyEditor pe, PropertyEnv env) {
81
env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
82
env.addPropertyChangeListener(this);
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);
103
// bugfix #39369: remove help button in Action dialog property editor
104
//HelpCtx.setHelpIDString (this, LoaderActionsPanel.class.getName ());
106
// Form Editor does not permit you to set this, because
107
// it has both int and double params:
108
splitPane.setDividerLocation (300);
110
java.util.ResourceBundle bundle = NbBundle.getBundle(LoaderActionsPanel.class);
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"));
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.
126
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
127
private void initComponents() {
128
java.awt.GridBagConstraints gridBagConstraints;
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();
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));
147
splitPane.setLastDividerLocation(300);
149
explorerPanel.setLayout(new java.awt.BorderLayout());
151
beanTreeView2.setDefaultActionAllowed(false);
152
beanTreeView2.setPopupAllowed(false);
153
explorerPanel.add(beanTreeView2, java.awt.BorderLayout.CENTER);
155
splitPane.setLeftComponent(explorerPanel);
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);
164
list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
165
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
166
listValueChanged(evt);
169
jScrollPane1.setViewportView(list);
171
splitPane.setRightComponent(jScrollPane1);
173
add(splitPane, java.awt.BorderLayout.CENTER);
175
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));
177
jPanel1.setLayout(new java.awt.GridBagLayout());
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
251
jPanel2.add(jPanel1);
253
add(jPanel2, java.awt.BorderLayout.EAST);
254
}// </editor-fold>//GEN-END:initComponents
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);
261
}//GEN-LAST:event_listFocusGained
263
private void listValueChanged (javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listValueChanged
264
int index = list.getSelectedIndex ();
266
downButton.setEnabled (false);
267
upButton.setEnabled (false);
268
removeButton.setEnabled (false);
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);
276
}//GEN-LAST:event_listValueChanged
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
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
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[] { });
303
list.setSelectedIndex (Math.min (index, model.getSize () - 1));
304
}//GEN-LAST:event_removeButtonActionPerformed
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
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
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
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;
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);
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)
354
private Object getPropertyValue() throws IllegalStateException {
355
SystemAction[] actions = new SystemAction[model.getSize ()];
356
model.copyInto (actions);
360
public void propertyChange(PropertyChangeEvent evt) {
361
if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) {
362
editor.setValue(getPropertyValue());
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>
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.
381
* @see ListSelectionModel
384
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
385
JLabel label = new JLabel ();
387
SystemAction action = (SystemAction) value;
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);
400
label.setText (NbBundle.getBundle(LoaderActionsPanel.class).getString("LBL_separator_rather_than_action"));
404
label.setIcon(new ImageIcon(new URL("nbresloc:/org/openide/resources/actions/empty.gif"))); // NOI18N
405
} catch (MalformedURLException ex) {
410
label.setBackground(list.getSelectionBackground());
411
label.setForeground(list.getSelectionForeground());
413
label.setBackground(list.getBackground());
414
label.setForeground(list.getForeground());
416
label.setEnabled(list.isEnabled());
417
label.setFont(list.getFont());
418
label.setOpaque (true);