KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.beans.*;
24 import java.awt.event.*;
25
26 import org.openide.*;
27 import org.openide.nodes.*;
28 import org.openide.util.Utilities;
29
30 /**
31  * Property implementation class for events of metacomponents.
32  * (Events are treated as properties on Events tab of Component Inspector.)
33  *
34  * @author Tomas Pavek
35  */

36
37 class EventProperty extends PropertySupport.ReadWrite {
38
39     private static String JavaDoc NO_EVENT;
40
41     private static boolean somethingChanged; // flag for "postSetAction" relevance
42
private static boolean invalidValueTried; // flag for "postSetAction" relevance
43

44     private Event event;
45
46     private String JavaDoc selectedEventHandler;
47
48     EventProperty(Event event, String JavaDoc eventId) {
49         super(eventId,
50               String JavaDoc.class,
51               event.getListenerMethod().getName(),
52               event.getListenerMethod().getName());
53         this.event = event;
54         setShortDescription(
55             event.getEventSetDescriptor().getListenerType().getName());
56     }
57
58     Event getEvent() {
59         return event;
60     }
61
62     private FormEvents getFormEvents() {
63         return event.getComponent().getFormModel().getFormEvents();
64     }
65
66     private java.lang.reflect.Method JavaDoc getListenerMethod() {
67         return event.getListenerMethod();
68     }
69
70     String JavaDoc[] getEventHandlers() {
71         return event.getEventHandlers();
72     }
73
74     // -------
75

76     /** Getter for the value of the property. It returns name of the last
77      * selected event handler (for property sheet), not the Event object.
78      * @return String name of the selected event handler attached to the event
79      */

80     public Object JavaDoc getValue() {
81         if (selectedEventHandler == null && event.hasEventHandlers())
82             selectedEventHandler = (String JavaDoc) event.getEventHandlerList().get(0);
83         return selectedEventHandler;
84     }
85
86     /** Setter for the value of the property. It accepts String (for adding
87      * new or renaming the last selected event handler), or Change object
88      * (describing multiple changes in event handlers), or null (to refresh
89      * property sheet due to a change in handlers made outside).
90      */

91     public void setValue(Object JavaDoc val) {
92         Change change = null;
93         String JavaDoc newSelectedHandler = null;
94
95         if (val instanceof Change) {
96             change = (Change) val;
97         }
98         else if (val instanceof String JavaDoc) {
99             String JavaDoc[] handlers = getEventHandlers();
100             if (handlers.length > 0) {
101                 // there are already some handlers attached
102
String JavaDoc current = selectedEventHandler != null ?
103                                  selectedEventHandler : handlers[0];
104
105                 if ("".equals(val)) { // NOI18N
106
// empty String => remove current handler
107
change = new Change();
108                     change.getRemoved().add(current);
109                     for (int i=0; i < handlers.length; i++)
110                         if (!handlers[i].equals(current)) {
111                             newSelectedHandler = handlers[i];
112                             break;
113                         }
114                 }
115                 else { // non-empty String => rename current handler
116
newSelectedHandler = (String JavaDoc) val;
117
118                     boolean ignore = false;
119                     for (int i=0; i < handlers.length; i++)
120                         if (handlers[i].equals(val)) { // not a new name
121
ignore = true;
122                             break;
123                         }
124
125                     if (!ignore) { // do rename
126
change = new Change();
127                         change.getRenamedNewNames().add(val);
128                         change.getRenamedOldNames().add(current);
129                     }
130                 }
131             }
132             else { // no handlers yet, add a new one
133
if (!"".equals(val)) {
134                     change = new Change();
135                     change.getAdded().add((String JavaDoc)val);
136                     newSelectedHandler = (String JavaDoc) val;
137                 }
138             }
139         }
140         else if (val == null) {
141             if (selectedEventHandler == null)
142                 return;
143         }
144         else throw new IllegalArgumentException JavaDoc();
145
146         if (change != null) {
147             somethingChanged = true; // something was changed
148

149             FormEvents formEvents = getFormEvents();
150
151             if (change.hasRemoved()) // some handlers to remove
152
for (Iterator it=change.getRemoved().iterator(); it.hasNext(); )
153                     formEvents.detachEvent(event, (String JavaDoc) it.next());
154
155             if (change.hasRenamed()) // some handlers to rename
156
for (int i=0; i < change.getRenamedOldNames().size(); i++) {
157                     String JavaDoc oldName = (String JavaDoc)change.getRenamedOldNames().get(i);
158                     String JavaDoc newName = (String JavaDoc)change.getRenamedNewNames().get(i);
159
160                     try {
161                         formEvents.renameEventHandler(oldName, newName);
162
163                         // hack: update all properties using the renamed handler
164
Event[] events = formEvents.getEventsForHandler(newName);
165                         for (int j=0 ; j < events.length; j++) {
166                             Node.Property prop = events[j].getComponent()
167                                                   .getPropertyByName(getName());
168                             if (prop != null && prop != this) {
169                                 try {
170                                     if (oldName.equals(prop.getValue()))
171                                         prop.setValue(newName);
172                                 }
173                                 catch (Exception JavaDoc ex) { // should not happen
174
ex.printStackTrace();
175                                 }
176                             }
177                         }
178                     }
179                     catch (IllegalArgumentException JavaDoc ex) { // name already used
180
ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
181                         newSelectedHandler = null;
182                     }
183                 }
184
185             if (change.hasAdded()) // some handlers to add
186
for (Iterator it=change.getAdded().iterator(); it.hasNext(); ) {
187                     try {
188                         formEvents.attachEvent(event, (String JavaDoc) it.next(), null);
189                     }
190                     catch (IllegalArgumentException JavaDoc ex) { // name already used
191
ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
192                         newSelectedHandler = null;
193                     }
194                 }
195         }
196
197         selectedEventHandler = newSelectedHandler;
198
199         RADComponentNode node = event.getComponent().getNodeReference();
200         if (node != null)
201             node.firePropertyChangeHelper(getName(), null, null);
202     }
203
204     public Object JavaDoc getValue(String JavaDoc key) {
205         if ("canEditAsText".equals(key)) // NOI18N
206
return Boolean.TRUE;
207
208         if ("initialEditValue".equals(key)) { // NOI18N
209
somethingChanged = false; // entering edit mode
210
invalidValueTried = false;
211             return selectedEventHandler != null ? null :
212                 getFormEvents().findFreeHandlerName(event, event.getComponent());
213         }
214
215         if ("postSetAction".equals(key)) // NOI18N
216
return new javax.swing.AbstractAction JavaDoc() {
217                 public void actionPerformed(ActionEvent ev) {
218                     // if Enter was pressed without echange or existing handler
219
// chosen, switch to editor
220
if (!somethingChanged && !invalidValueTried)
221                         getFormEvents().attachEvent(event,
222                                                     selectedEventHandler,
223                                                     null);
224                 }
225             };
226
227         return super.getValue(key);
228     }
229
230
231 // public String getDisplayName() {
232
// String displayName = super.getDisplayName();
233
// if (selectedEventHandler != null)
234
// displayName = "<html><b>" + displayName + "</b>"; // NOI18N
235
// return displayName;
236
// }
237
//
238
public boolean canWrite() {
239         return !isReadOnly();
240     }
241
242     private boolean isReadOnly() {
243         return event.getComponent().isReadOnly();
244     }
245
246     /** Returns property editor for this property.
247      * @return the property editor for adding/removing/renaming event handlers
248      */

249     public PropertyEditor getPropertyEditor() {
250         return new EventEditor();
251     }
252
253     // --------
254

255     /** Helper class describing changes in event handlers attached to an event.
256      */

257     static class Change {
258         boolean hasAdded() {
259             return added != null && added.size() > 0;
260         }
261         boolean hasRemoved() {
262             return removed != null && removed.size() > 0;
263         }
264         boolean hasRenamed() {
265             return renamedOldName != null && renamedOldName.size() > 0;
266         }
267         List getAdded() {
268             if (added == null)
269                 added = new ArrayList();
270             return added;
271         }
272         List getRemoved() {
273             if (removed == null)
274                 removed = new ArrayList();
275             return removed;
276         }
277         List getRenamedOldNames() {
278             if (renamedOldName == null)
279                 renamedOldName = new ArrayList();
280             return renamedOldName;
281         }
282         List getRenamedNewNames() {
283             if (renamedNewName == null)
284                 renamedNewName = new ArrayList();
285             return renamedNewName;
286         }
287         private List added;
288         private List removed;
289         private List renamedOldName;
290         private List renamedNewName;
291     }
292
293     // --------
294

295     private class EventEditor extends PropertyEditorSupport {
296
297         public String JavaDoc getAsText() {
298             if (this.getValue() == null) {
299                 if (NO_EVENT == null)
300                     NO_EVENT = FormUtils.getBundleString("CTL_NoEvent"); // NOI18N
301
return NO_EVENT;
302             }
303             return this.getValue().toString();
304         }
305
306         public void setAsText(String JavaDoc txt) {
307             if (!"".equals(txt) && !Utilities.isJavaIdentifier(txt)) { // NOI18N
308
// invalid handler name entered
309
invalidValueTried = true;
310                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc();
311                 String JavaDoc annotation = FormUtils.getFormattedBundleString(
312                                         "FMT_MSG_InvalidJavaIdentifier", // NOI18N
313
new Object JavaDoc [] { txt } );
314                 ErrorManager.getDefault().annotate(
315                     iae, ErrorManager.ERROR, "Not a java identifier", // NOI18N
316
annotation, null, null);
317                 throw iae;
318             }
319             if ("".equals(txt) && (this.getValue() == null)) {
320                 // empty string entered when no event handler exist
321
invalidValueTried = true;
322                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc();
323                 String JavaDoc emptyStringTxt = FormUtils.getBundleString("FMT_MSG_EmptyString"); // NOI18N
324
String JavaDoc annotation = FormUtils.getFormattedBundleString(
325                                         "FMT_MSG_InvalidJavaIdentifier", // NOI18N
326
new Object JavaDoc [] { emptyStringTxt } );
327                 ErrorManager.getDefault().annotate(
328                     iae, ErrorManager.ERROR, "Not a java identifier", // NOI18N
329
annotation, null, null);
330                 throw iae;
331             }
332             invalidValueTried = false;
333             this.setValue(txt);
334         }
335
336         public String JavaDoc[] getTags() {
337             String JavaDoc[] handlers = getEventHandlers();
338             return handlers.length > 1 ? handlers : null;
339         }
340
341         public boolean supportsCustomEditor() {
342             return isReadOnly() ? false : true;
343         }
344
345         public java.awt.Component JavaDoc getCustomEditor() {
346             if (isReadOnly())
347                 return null;
348
349             final EventCustomEditor ed = new EventCustomEditor(EventProperty.this);
350             DialogDescriptor dd = new DialogDescriptor(
351                 ed,
352                 FormUtils.getFormattedBundleString(
353                     "FMT_MSG_HandlersFor", // NOI18N
354
new Object JavaDoc [] { getListenerMethod().getName() }),
355                 true,
356                 new ActionListener() {
357                     public void actionPerformed(ActionEvent evt) {
358                         if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) {
359                             ed.doChanges();
360                         }
361                     }
362                 });
363
364             return DialogDisplayer.getDefault().createDialog(dd);
365         }
366     }
367 }
368
Popular Tags