KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > EventCustomEditor


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.modules.form;
21
22 import java.util.*;
23 import javax.swing.DefaultListModel JavaDoc;
24
25 import org.openide.DialogDisplayer;
26 import org.openide.NotifyDescriptor;
27
28 /**
29  *
30  * @author Pavel Buzek
31  */

32 public class EventCustomEditor extends javax.swing.JPanel JavaDoc {
33
34     static final long serialVersionUID =-4825059521634962952L;
35
36     /** Creates new form EventCustomEditor */
37     public EventCustomEditor(EventProperty eventProperty) {
38         this.eventProperty = eventProperty;
39
40         initComponents();
41         enableButtons();
42
43         addButton.setMnemonic(
44             FormUtils.getBundleString("CTL_EE_ADD_Mnemonic").charAt(0)); // NOI18N
45
removeButton.setMnemonic(
46             FormUtils.getBundleString("CTL_EE_REMOVE_Mnemonic").charAt(0)); // NOI18N
47
editButton.setMnemonic(
48             FormUtils.getBundleString("CTL_EE_RENAME_Mnemonic").charAt(0)); // NOI18N
49
handlersListLabel.setDisplayedMnemonic(
50             FormUtils.getBundleString("CTL_EE_Handlers_Mnemonic").charAt(0)); // NOI18N
51

52         handlersList.getAccessibleContext().setAccessibleDescription(
53             FormUtils.getBundleString("ACSD_CTL_EE_Handlers")); // NOI18N
54
addButton.getAccessibleContext().setAccessibleDescription(
55             FormUtils.getBundleString("ACSD_CTL_EE_ADD")); // NOI18N
56
removeButton.getAccessibleContext().setAccessibleDescription(
57             FormUtils.getBundleString("ACSD_CTL_EE_REMOVE")); // NOI18N
58
editButton.getAccessibleContext().setAccessibleDescription(
59             FormUtils.getBundleString("ACSD_CTL_EE_RENAME")); // NOI18N
60
getAccessibleContext().setAccessibleDescription(
61             FormUtils.getBundleString("ACSD_EventCustomEditor")); // NOI18N
62
}
63
64     /** This method is called from within the constructor to
65      * initialize the form.
66      * WARNING: Do NOT modify this code. The content of this method is
67      * always regenerated by the FormEditor.
68      */

69     private void initComponents() {//GEN-BEGIN:initComponents
70
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
71
72         handlersListLabel = new javax.swing.JLabel JavaDoc();
73         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
74         String JavaDoc[] handlers = eventProperty.getEventHandlers();
75         for (int i=0; i < handlers.length; i++) {
76             handlersModel.addElement(handlers[i]);
77         }
78         handlersList = new javax.swing.JList JavaDoc();
79         handlersList.setModel(handlersModel);
80         if (handlers.length > 0) {
81             handlersList.setSelectedIndex(0);
82         }
83         addButton = new javax.swing.JButton JavaDoc();
84         removeButton = new javax.swing.JButton JavaDoc();
85         editButton = new javax.swing.JButton JavaDoc();
86
87         setLayout(new java.awt.GridBagLayout JavaDoc());
88
89         setPreferredSize(new java.awt.Dimension JavaDoc(300, 300));
90         handlersListLabel.setLabelFor(handlersList);
91         handlersListLabel.setText(FormUtils.getBundleString("CTL_EE_Handlers"));
92         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
93         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
94         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 2, 0);
95         add(handlersListLabel, gridBagConstraints);
96
97         handlersList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
98             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
99                 handlersListValueChanged(evt);
100             }
101         });
102
103         jScrollPane1.setViewportView(handlersList);
104
105         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
106         gridBagConstraints.gridx = 0;
107         gridBagConstraints.gridy = 1;
108         gridBagConstraints.gridheight = 4;
109         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
110         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
111         gridBagConstraints.weightx = 0.9;
112         gridBagConstraints.weighty = 1.0;
113         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 11);
114         add(jScrollPane1, gridBagConstraints);
115
116         addButton.setText(FormUtils.getBundleString("CTL_EE_ADD"));
117         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
118             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
119                 addButtonActionPerformed(evt);
120             }
121         });
122
123         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
124         gridBagConstraints.gridx = 1;
125         gridBagConstraints.gridy = 1;
126         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
127         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
128         gridBagConstraints.weightx = 0.1;
129         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 11);
130         add(addButton, gridBagConstraints);
131
132         removeButton.setText(FormUtils.getBundleString("CTL_EE_REMOVE"));
133         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
134             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
135                 removeButtonActionPerformed(evt);
136             }
137         });
138
139         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
140         gridBagConstraints.gridx = 1;
141         gridBagConstraints.gridy = 2;
142         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
143         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
144         gridBagConstraints.weightx = 0.1;
145         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 11);
146         add(removeButton, gridBagConstraints);
147
148         editButton.setText(FormUtils.getBundleString("CTL_EE_RENAME"));
149         editButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
150             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
151                 editButtonActionPerformed(evt);
152             }
153         });
154
155         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
156         gridBagConstraints.gridx = 1;
157         gridBagConstraints.gridy = 3;
158         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
159         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
160         gridBagConstraints.weightx = 0.1;
161         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 11);
162         add(editButton, gridBagConstraints);
163
164     }//GEN-END:initComponents
165

166     private void handlersListValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_handlersListValueChanged
167
enableButtons();
168     }//GEN-LAST:event_handlersListValueChanged
169

170     private void enableButtons() {
171         if (handlersList.isSelectionEmpty()) {
172             removeButton.setEnabled(false);
173         } else {
174             removeButton.setEnabled(true);
175         }
176         editButton.setEnabled(handlersList.getSelectedIndices().length == 1);
177     }
178
179     private void editButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_editButtonActionPerformed
180
// Add your handling code here:
181
int i = handlersList.getSelectedIndex();
182         if (i >= 0) {
183             String JavaDoc oldName = (String JavaDoc) handlersModel.get(i);
184             NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine(
185                 FormUtils.getBundleString("CTL_EE_RENAME_LABEL"), // NOI18N
186
FormUtils.getBundleString("CTL_EE_RENAME_CAPTION")); // NOI18N
187
nd.setInputText(oldName);
188
189             if (DialogDisplayer.getDefault().notify(nd).equals(NotifyDescriptor.OK_OPTION)) {
190                 String JavaDoc newName = nd.getInputText();
191                 if (newName.equals(oldName)) return; // no change
192

193                 if (!org.openide.util.Utilities.isJavaIdentifier(newName)) { // invalid name
194
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
195                         FormUtils.getBundleString("CTL_EE_NOT_IDENTIFIER"), // NOI18N
196
NotifyDescriptor.ERROR_MESSAGE);
197                     DialogDisplayer.getDefault().notify(msg);
198                     return;
199                 }
200
201                 if (handlersModel.indexOf(newName) >= 0) { // already exists
202
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
203                         FormUtils.getBundleString("CTL_EE_ALREADY_EXIST"), // NOI18N
204
NotifyDescriptor.INFORMATION_MESSAGE);
205                     DialogDisplayer.getDefault().notify(msg);
206                     return;
207                 }
208
209                 int ii = changes.getAdded().indexOf(oldName);
210                 if (ii >= 0) { // a newly added handler was renamed
211
changes.getAdded().set(ii,newName);
212                 }
213                 else {
214                     ii = changes.getRenamedNewNames().indexOf(oldName);
215                     if (ii >= 0) // this handler has been already renamed
216
changes.getRenamedNewNames().set(ii, newName);
217                     else {
218                         changes.getRenamedOldNames().add(oldName);
219                         changes.getRenamedNewNames().add(newName);
220                     }
221                 }
222
223                 handlersModel.set(i,newName);
224                 handlersList.setSelectedIndex(i);
225                 enableButtons();
226             }
227         }
228     }//GEN-LAST:event_editButtonActionPerformed
229

230     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
231
Object JavaDoc[] handlers = handlersList.getSelectedValues();
232         for (int i=0; i < handlers.length; i++) {
233             int ii = changes.getAdded().indexOf(handlers[i]);
234             if (ii >= 0) { // the handler was previously added - cancel it
235
changes.getAdded().remove(ii);
236             }
237             else {
238                 ii = changes.getRenamedNewNames().indexOf(handlers[i]);
239                 String JavaDoc toRemove;
240                 if (ii >= 0) { // the handler was previously renamed - cancel it
241
changes.getRenamedNewNames().remove(ii);
242                     toRemove = (String JavaDoc) changes.getRenamedOldNames().get(ii);
243                     changes.getRenamedOldNames().remove(ii);
244                 }
245                 else toRemove = (String JavaDoc) handlers[i];
246
247                 changes.getRemoved().add(toRemove);
248             }
249             handlersModel.removeElement(handlers[i]);
250             enableButtons();
251         }
252     }//GEN-LAST:event_removeButtonActionPerformed
253

254     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
255
NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine(
256             FormUtils.getBundleString("CTL_EE_ADD_LABEL"), // NOI18N
257
FormUtils.getBundleString("CTL_EE_ADD_CAPTION")); // NOI18N
258
if (DialogDisplayer.getDefault().notify(nd).equals(NotifyDescriptor.OK_OPTION)) {
259             String JavaDoc newHandler = nd.getInputText();
260             if (!org.openide.util.Utilities.isJavaIdentifier(newHandler)) {
261                 NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
262                     FormUtils.getBundleString("CTL_EE_NOT_IDENTIFIER"), // NOI18N
263
NotifyDescriptor.ERROR_MESSAGE);
264                 DialogDisplayer.getDefault().notify(msg);
265                 return;
266             }
267
268             if (handlersModel.indexOf(newHandler) >= 0) {
269                 NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
270                     FormUtils.getBundleString("CTL_EE_ALREADY_EXIST"), // NOI18N
271
NotifyDescriptor.INFORMATION_MESSAGE);
272                 DialogDisplayer.getDefault().notify(msg);
273                 return;
274             }
275
276             int ir = changes.getRemoved().indexOf(newHandler);
277             if (ir >= 0) {
278                 changes.getRemoved().remove(ir);
279             }
280             else {
281                 changes.getAdded().add(newHandler);
282             }
283             handlersModel.addElement(newHandler);
284             handlersList.setSelectedIndex(handlersModel.size() - 1);
285             enableButtons();
286         }
287     }//GEN-LAST:event_addButtonActionPerformed
288

289     public void doChanges() {
290         try {
291             eventProperty.setValue(changes);
292         } catch (Exception JavaDoc e) { // should not happen
293
e.printStackTrace();
294         }
295     }
296
297     // Variables declaration - do not modify//GEN-BEGIN:variables
298
private javax.swing.JButton JavaDoc addButton;
299     private javax.swing.JScrollPane JavaDoc jScrollPane1;
300     private javax.swing.JList JavaDoc handlersList;
301     private javax.swing.JButton JavaDoc editButton;
302     private javax.swing.JButton JavaDoc removeButton;
303     private javax.swing.JLabel JavaDoc handlersListLabel;
304     // End of variables declaration//GEN-END:variables
305

306     EventProperty eventProperty;
307     DefaultListModel JavaDoc handlersModel = new DefaultListModel JavaDoc();
308     EventProperty.Change changes = new EventProperty.Change();
309 }
310
Popular Tags