001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form;
043:
044: import java.util.*;
045: import java.beans.*;
046: import java.awt.event.*;
047:
048: import org.openide.*;
049: import org.openide.nodes.*;
050: import org.openide.util.Utilities;
051:
052: /**
053: * Property implementation class for events of metacomponents.
054: * (Events are treated as properties on Events tab of Component Inspector.)
055: *
056: * @author Tomas Pavek
057: */
058:
059: class EventProperty extends PropertySupport.ReadWrite {
060:
061: private static String NO_EVENT;
062:
063: private static boolean somethingChanged; // flag for "postSetAction" relevance
064: private static boolean invalidValueTried; // flag for "postSetAction" relevance
065:
066: private Event event;
067:
068: private String selectedEventHandler;
069:
070: EventProperty(Event event, String eventId) {
071: super (eventId, String.class, event.getListenerMethod()
072: .getName(), event.getListenerMethod().getName());
073: this .event = event;
074: setShortDescription(event.getEventSetDescriptor()
075: .getListenerType().getName());
076: }
077:
078: Event getEvent() {
079: return event;
080: }
081:
082: private FormEvents getFormEvents() {
083: return event.getComponent().getFormModel().getFormEvents();
084: }
085:
086: private java.lang.reflect.Method getListenerMethod() {
087: return event.getListenerMethod();
088: }
089:
090: String[] getEventHandlers() {
091: return event.getEventHandlers();
092: }
093:
094: // -------
095:
096: /** Getter for the value of the property. It returns name of the last
097: * selected event handler (for property sheet), not the Event object.
098: * @return String name of the selected event handler attached to the event
099: */
100: public Object getValue() {
101: if (selectedEventHandler == null && event.hasEventHandlers())
102: selectedEventHandler = (String) event.getEventHandlerList()
103: .get(0);
104: return selectedEventHandler;
105: }
106:
107: /** Setter for the value of the property. It accepts String (for adding
108: * new or renaming the last selected event handler), or Change object
109: * (describing multiple changes in event handlers), or null (to refresh
110: * property sheet due to a change in handlers made outside).
111: */
112: public void setValue(Object val) {
113: Change change = null;
114: String newSelectedHandler = null;
115:
116: if (val instanceof Change) {
117: change = (Change) val;
118: } else if (val instanceof String) {
119: String[] handlers = getEventHandlers();
120: if (handlers.length > 0) {
121: // there are already some handlers attached
122: String current = selectedEventHandler != null ? selectedEventHandler
123: : handlers[0];
124:
125: if ("".equals(val)) { // NOI18N
126: // empty String => remove current handler
127: change = new Change();
128: change.getRemoved().add(current);
129: for (int i = 0; i < handlers.length; i++)
130: if (!handlers[i].equals(current)) {
131: newSelectedHandler = handlers[i];
132: break;
133: }
134: } else { // non-empty String => rename current handler
135: newSelectedHandler = (String) val;
136:
137: boolean ignore = false;
138: for (int i = 0; i < handlers.length; i++)
139: if (handlers[i].equals(val)) { // not a new name
140: ignore = true;
141: break;
142: }
143:
144: if (!ignore) { // do rename
145: change = new Change();
146: change.getRenamedNewNames().add((String) val);
147: change.getRenamedOldNames().add(current);
148: }
149: }
150: } else { // no handlers yet, add a new one
151: if (!"".equals(val)) {
152: change = new Change();
153: change.getAdded().add((String) val);
154: newSelectedHandler = (String) val;
155: }
156: }
157: } else if (val == null) {
158: if (selectedEventHandler == null)
159: return;
160: } else
161: throw new IllegalArgumentException();
162:
163: if (change != null) {
164: somethingChanged = true; // something was changed
165:
166: FormEvents formEvents = getFormEvents();
167:
168: if (change.hasRemoved()) // some handlers to remove
169: for (Iterator it = change.getRemoved().iterator(); it
170: .hasNext();)
171: formEvents.detachEvent(event, (String) it.next());
172:
173: if (change.hasRenamed()) // some handlers to rename
174: for (int i = 0; i < change.getRenamedOldNames().size(); i++) {
175: String oldName = change.getRenamedOldNames().get(i);
176: String newName = change.getRenamedNewNames().get(i);
177:
178: try {
179: formEvents.renameEventHandler(oldName, newName);
180:
181: // hack: update all properties using the renamed handler
182: Event[] events = formEvents
183: .getEventsForHandler(newName);
184: for (int j = 0; j < events.length; j++) {
185: Node.Property prop = events[j]
186: .getComponent().getPropertyByName(
187: getName());
188: if (prop != null && prop != this ) {
189: try {
190: if (oldName.equals(prop.getValue()))
191: prop.setValue(newName);
192: } catch (Exception ex) { // should not happen
193: ex.printStackTrace();
194: }
195: }
196: }
197: } catch (IllegalArgumentException ex) { // name already used
198: ErrorManager.getDefault().notify(
199: ErrorManager.WARNING, ex);
200: newSelectedHandler = null;
201: }
202: }
203:
204: if (change.hasAdded()) // some handlers to add
205: for (Iterator it = change.getAdded().iterator(); it
206: .hasNext();) {
207: try {
208: formEvents.attachEvent(event, (String) it
209: .next(), null);
210: } catch (IllegalArgumentException ex) { // name already used
211: ErrorManager.getDefault().notify(
212: ErrorManager.WARNING, ex);
213: newSelectedHandler = null;
214: }
215: }
216: }
217:
218: selectedEventHandler = newSelectedHandler;
219:
220: RADComponentNode node = event.getComponent().getNodeReference();
221: if (node != null)
222: node.firePropertyChangeHelper(getName(), null, null);
223: }
224:
225: @Override
226: public Object getValue(String key) {
227: if ("canEditAsText".equals(key)) // NOI18N
228: return Boolean.TRUE;
229:
230: if ("initialEditValue".equals(key)) { // NOI18N
231: somethingChanged = false; // entering edit mode
232: invalidValueTried = false;
233: return selectedEventHandler != null ? null
234: : getFormEvents().findFreeHandlerName(event,
235: event.getComponent());
236: }
237:
238: if ("postSetAction".equals(key)) // NOI18N
239: return new javax.swing.AbstractAction() {
240: public void actionPerformed(ActionEvent ev) {
241: // if Enter was pressed without echange or existing handler
242: // chosen, switch to editor
243: if (!somethingChanged && !invalidValueTried)
244: getFormEvents().attachEvent(event,
245: selectedEventHandler, null);
246: }
247: };
248:
249: return super .getValue(key);
250: }
251:
252: // public String getDisplayName() {
253: // String displayName = super.getDisplayName();
254: // if (selectedEventHandler != null)
255: // displayName = "<html><b>" + displayName + "</b>"; // NOI18N
256: // return displayName;
257: // }
258: //
259: @Override
260: public boolean canWrite() {
261: return !isReadOnly();
262: }
263:
264: private boolean isReadOnly() {
265: return event.getComponent().isReadOnly();
266: }
267:
268: /** Returns property editor for this property.
269: * @return the property editor for adding/removing/renaming event handlers
270: */
271: @Override
272: public PropertyEditor getPropertyEditor() {
273: return new EventEditor();
274: }
275:
276: // --------
277:
278: /** Helper class describing changes in event handlers attached to an event.
279: */
280: static class Change {
281: boolean hasAdded() {
282: return added != null && added.size() > 0;
283: }
284:
285: boolean hasRemoved() {
286: return removed != null && removed.size() > 0;
287: }
288:
289: boolean hasRenamed() {
290: return renamedOldName != null && renamedOldName.size() > 0;
291: }
292:
293: List<String> getAdded() {
294: if (added == null)
295: added = new ArrayList<String>();
296: return added;
297: }
298:
299: List<String> getRemoved() {
300: if (removed == null)
301: removed = new ArrayList<String>();
302: return removed;
303: }
304:
305: List<String> getRenamedOldNames() {
306: if (renamedOldName == null)
307: renamedOldName = new ArrayList<String>();
308: return renamedOldName;
309: }
310:
311: List<String> getRenamedNewNames() {
312: if (renamedNewName == null)
313: renamedNewName = new ArrayList<String>();
314: return renamedNewName;
315: }
316:
317: private List<String> added;
318: private List<String> removed;
319: private List<String> renamedOldName;
320: private List<String> renamedNewName;
321: }
322:
323: // --------
324:
325: private class EventEditor extends PropertyEditorSupport {
326:
327: @Override
328: public String getAsText() {
329: if (this .getValue() == null) {
330: if (NO_EVENT == null)
331: NO_EVENT = FormUtils.getBundleString("CTL_NoEvent"); // NOI18N
332: return NO_EVENT;
333: }
334: return this .getValue().toString();
335: }
336:
337: @Override
338: public void setAsText(String txt) {
339: if (!"".equals(txt) && !Utilities.isJavaIdentifier(txt)) { // NOI18N
340: // invalid handler name entered
341: invalidValueTried = true;
342: IllegalArgumentException iae = new IllegalArgumentException();
343: String annotation = FormUtils.getFormattedBundleString(
344: "FMT_MSG_InvalidJavaIdentifier", // NOI18N
345: new Object[] { txt });
346: ErrorManager.getDefault().annotate(iae,
347: ErrorManager.ERROR, "Not a java identifier", // NOI18N
348: annotation, null, null);
349: throw iae;
350: }
351: if ("".equals(txt) && (this .getValue() == null)) {
352: // empty string entered when no event handler exist
353: invalidValueTried = true;
354: IllegalArgumentException iae = new IllegalArgumentException();
355: String emptyStringTxt = FormUtils
356: .getBundleString("FMT_MSG_EmptyString"); // NOI18N
357: String annotation = FormUtils.getFormattedBundleString(
358: "FMT_MSG_InvalidJavaIdentifier", // NOI18N
359: new Object[] { emptyStringTxt });
360: ErrorManager.getDefault().annotate(iae,
361: ErrorManager.ERROR, "Not a java identifier", // NOI18N
362: annotation, null, null);
363: throw iae;
364: }
365: invalidValueTried = false;
366: this .setValue(txt);
367: }
368:
369: @Override
370: public String[] getTags() {
371: String[] handlers = getEventHandlers();
372: return handlers.length > 1 ? handlers : null;
373: }
374:
375: @Override
376: public boolean supportsCustomEditor() {
377: return isReadOnly() ? false : true;
378: }
379:
380: @Override
381: public java.awt.Component getCustomEditor() {
382: if (isReadOnly())
383: return null;
384:
385: final EventCustomEditor ed = new EventCustomEditor(
386: EventProperty.this );
387: DialogDescriptor dd = new DialogDescriptor(ed, FormUtils
388: .getFormattedBundleString("FMT_MSG_HandlersFor", // NOI18N
389: new Object[] { getListenerMethod()
390: .getName() }), true,
391: new ActionListener() {
392: public void actionPerformed(ActionEvent evt) {
393: if (evt.getSource().equals(
394: DialogDescriptor.OK_OPTION)) {
395: ed.doChanges();
396: }
397: }
398: });
399:
400: return DialogDisplayer.getDefault().createDialog(dd);
401: }
402: }
403: }
|