KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > actions > PropertyAction


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 package org.netbeans.modules.form.actions;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.beans.*;
24 import java.util.ResourceBundle JavaDoc;
25 import javax.swing.*;
26
27 import org.openide.DialogDisplayer;
28 import org.openide.DialogDescriptor;
29 import org.openide.NotifyDescriptor;
30 import org.openide.awt.Mnemonics;
31 import org.openide.util.HelpCtx;
32 import org.openide.util.NbBundle;
33 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor;
34
35 import org.netbeans.modules.form.*;
36
37 /**
38  * Action that invokes custom property editor for the given property.
39  *
40  * @author Jan Stola
41  */

42 public class PropertyAction extends AbstractAction {
43     private static final String JavaDoc OK_COMMAND = "OK"; // NOI18N
44
private static final String JavaDoc CANCEL_COMMAND = "Cancel"; // NOI18N
45
private static final String JavaDoc RESTORE_COMMAND = "Restore"; // NOI18N
46
private RADProperty property;
47     private Dialog dialog;
48     
49     public PropertyAction(RADProperty property) {
50         this.property = property;
51         String JavaDoc name = (String JavaDoc)property.getValue("actionName"); // NOI18N
52
if (name == null) {
53             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(property.getName());
54             sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
55             name = sb.toString();
56         }
57         putValue(Action.NAME, name);
58     }
59
60     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
61         try {
62             PropertyEditor propEd = property.getPropertyEditor();
63             propEd.setValue(property.getValue());
64             final Component custEditor = propEd.getCustomEditor();
65             Object JavaDoc[] options = buttons();
66             DialogDescriptor descriptor = new DialogDescriptor(
67                 custEditor,
68                 (String JavaDoc)getValue(Action.NAME),
69                 true,
70                 options,
71                 DialogDescriptor.CANCEL_OPTION,
72                 DialogDescriptor.DEFAULT_ALIGN,
73                 HelpCtx.DEFAULT_HELP,
74                 new ActionListener() {
75                     public void actionPerformed(ActionEvent e) {
76                         try {
77                             String JavaDoc action = e.getActionCommand();
78                             if (OK_COMMAND.equals(action)) {
79                                 Object JavaDoc value = ((EnhancedCustomPropertyEditor)custEditor).getPropertyValue();
80                                 property.setValue(value);
81                             } else if (RESTORE_COMMAND.equals(action)) {
82                                 property.restoreDefaultValue();
83                             }
84                             dialog.dispose();
85                         } catch (Exception JavaDoc ex) {
86                             NotifyDescriptor descriptor = new NotifyDescriptor.Message(
87                                 NbBundle.getBundle(PropertyAction.class).getString("MSG_InvalidValue")); // NOI18N
88
DialogDisplayer.getDefault().notify(descriptor);
89                         }
90                     }
91                 });
92             descriptor.setClosingOptions(new Object JavaDoc[0]);
93             dialog = DialogDisplayer.getDefault().createDialog(descriptor);
94             dialog.setVisible(true);
95             dialog = null;
96         } catch (Exception JavaDoc ex) {
97             ex.printStackTrace();
98         }
99     }
100     
101     private Object JavaDoc[] buttons() {
102         ResourceBundle JavaDoc bundle = NbBundle.getBundle(PropertyAction.class);
103         JButton okButton = new JButton();
104         Mnemonics.setLocalizedText(okButton, bundle.getString("CTL_OK")); // NOI18N
105
okButton.setActionCommand(OK_COMMAND);
106         JButton cancelButton = new JButton();
107         Mnemonics.setLocalizedText(cancelButton, bundle.getString("CTL_Cancel")); // NOI18N
108
cancelButton.setActionCommand(CANCEL_COMMAND);
109         if (property.isDefaultValue()) {
110             return new Object JavaDoc[] {okButton, cancelButton};
111         } else {
112             JButton restoreButton = new JButton();
113             Mnemonics.setLocalizedText(restoreButton, bundle.getString("CTL_RestoreDefault")); // NOI18N
114
restoreButton.setActionCommand(RESTORE_COMMAND);
115             return new Object JavaDoc[] {okButton, restoreButton, cancelButton};
116         }
117     }
118
119 }
120
Popular Tags