KickJava   Java API By Example, From Geeks To Geeks.

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


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.undo.*;
24
25 import org.openide.nodes.Node;
26
27 import org.netbeans.modules.form.layoutsupport.*;
28 import org.openide.ErrorManager;
29
30 /**
31  * Describes single change in FormModel. Provides UndoableEdit capable to
32  * undo/redo the change.
33  *
34  * @author Tran Duc Trung, Tomas Pavek
35  */

36
37 public class FormModelEvent extends EventObject
38 {
39     // possible types of changes
40
public static final int FORM_LOADED = 1;
41     public static final int FORM_TO_BE_SAVED = 2;
42     public static final int FORM_TO_BE_CLOSED = 3;
43     public static final int CONTAINER_LAYOUT_EXCHANGED = 4;
44     public static final int CONTAINER_LAYOUT_CHANGED = 5;
45     public static final int COMPONENT_LAYOUT_CHANGED = 6;
46     public static final int COMPONENT_ADDED = 7;
47     public static final int COMPONENT_REMOVED = 8;
48     public static final int COMPONENTS_REORDERED = 9;
49     public static final int COMPONENT_PROPERTY_CHANGED = 10;
50     public static final int SYNTHETIC_PROPERTY_CHANGED = 11;
51     public static final int EVENT_HANDLER_ADDED = 12;
52     public static final int EVENT_HANDLER_REMOVED = 13;
53     public static final int EVENT_HANDLER_RENAMED = 14;
54     public static final int OTHER_CHANGE = 15;
55
56     // data about the change
57
private int changeType;
58
59     private boolean createdDeleted;
60     private RADComponent component;
61     private ComponentContainer container;
62     private LayoutConstraints constraints;
63     private int componentIndex = -1;
64     private int[] reordering;
65     private String JavaDoc propertyName;
66     private Object JavaDoc oldPropertyValue;
67     private Object JavaDoc newPropertyValue;
68     private Event componentEvent;
69
70     private UndoableEdit undoableEdit;
71
72     // -----------
73

74     private FormModelEvent additionalEvent;
75     private static List interestList; // events interested in additional events
76

77     // -----------
78

79     FormModelEvent(FormModel source, int changeType) {
80         super(source);
81         this.changeType = changeType;
82         informInterestedEvents(this);
83     }
84
85     void setProperty(String JavaDoc propName, Object JavaDoc oldValue, Object JavaDoc newValue) {
86         propertyName = propName;
87         oldPropertyValue = oldValue;
88         newPropertyValue = newValue;
89     }
90
91     void setComponentAndContainer(RADComponent metacomp,
92                                   ComponentContainer metacont)
93     {
94         component = metacomp;
95         container = metacont != null ? metacont : deriveContainer(metacomp);
96     }
97
98     void setLayout(RADVisualContainer metacont,
99                    LayoutSupportDelegate oldLayoutSupp,
100                    LayoutSupportDelegate newLayoutSupp)
101     {
102         component = metacont;
103         container = metacont;
104         oldPropertyValue = oldLayoutSupp;
105         newPropertyValue = newLayoutSupp;
106     }
107
108     void setReordering(int[] perm) {
109         reordering = perm;
110     }
111
112     void setAddData(RADComponent metacomp,
113                     ComponentContainer metacont,
114                     boolean addedNew)
115     {
116         setComponentAndContainer(metacomp, metacont);
117         createdDeleted = addedNew;
118
119         if (component instanceof RADVisualComponent
120             && container instanceof RADVisualContainer)
121         {
122             componentIndex = container.getIndexOf(component);
123             if (componentIndex >= 0) {
124                 LayoutSupportManager laysup =
125                     ((RADVisualContainer)container).getLayoutSupport();
126                 if (laysup != null)
127                     constraints = laysup.getConstraints(componentIndex);
128             }
129         }
130     }
131
132     void setRemoveData(RADComponent metacomp,
133                        ComponentContainer metacont,
134                        int index,
135                        boolean removedFromModel)
136     {
137         component = metacomp;
138         container = metacont;
139         componentIndex = index;
140         createdDeleted = removedFromModel;
141
142         if (metacomp instanceof RADVisualComponent
143             && metacont instanceof RADVisualContainer)
144         {
145             LayoutSupportManager laysup =
146                 ((RADVisualContainer)metacont).getLayoutSupport();
147             constraints = laysup == null ? null :
148                 laysup.getStoredConstraints((RADVisualComponent)metacomp);
149         }
150     }
151
152     void setEvent(Event event, // may be null if the handler is just updated
153
String JavaDoc handler,
154                   String JavaDoc bodyText,
155                   boolean createdNew)
156     {
157         if (event != null)
158             component = event.getComponent();
159         componentEvent = event;
160         propertyName = handler;
161         newPropertyValue = bodyText;
162         createdDeleted = createdNew;
163     }
164
165     void setEvent(String JavaDoc oldHandlerName, String JavaDoc newHandlerName) {
166         oldPropertyValue = oldHandlerName;
167         propertyName = newHandlerName;
168         newPropertyValue = newHandlerName;
169     }
170
171     void setChangeType(int changeType) {
172         this.changeType = changeType;
173     }
174
175     private static ComponentContainer deriveContainer(RADComponent comp) {
176         if (comp == null)
177             return null;
178         if (comp.getParentComponent() instanceof ComponentContainer)
179             return (ComponentContainer) comp.getParentComponent();
180         else if (comp.getParentComponent() == null)
181             return comp.getFormModel().getModelContainer();
182         return null;
183     }
184
185     // -------
186

187     public final FormModel getFormModel() {
188         return (FormModel) getSource();
189     }
190
191     public final int getChangeType() {
192         return changeType;
193     }
194
195     public final boolean isModifying() {
196         return changeType != FORM_LOADED
197                && changeType != FORM_TO_BE_SAVED
198                && changeType != FORM_TO_BE_CLOSED
199                && (changeType != EVENT_HANDLER_ADDED || componentEvent != null);
200     }
201
202     public final boolean getCreatedDeleted() {
203         return createdDeleted;
204     }
205
206     public final ComponentContainer getContainer() {
207         return container;
208     }
209
210     public final RADComponent getComponent() {
211         return component;
212     }
213
214     public final LayoutConstraints getComponentLayoutConstraints() {
215         return constraints;
216     }
217
218     public final int getComponentIndex() {
219         return componentIndex;
220     }
221
222     public final String JavaDoc getPropertyName() {
223         return propertyName;
224     }
225
226     public final RADProperty getComponentProperty() {
227         return component != null && propertyName != null ?
228             component.getBeanProperty(propertyName) : null;
229     }
230
231     public final Object JavaDoc getOldPropertyValue() {
232         return oldPropertyValue;
233     }
234
235     public final Object JavaDoc getNewPropertyValue() {
236         return newPropertyValue;
237     }
238
239     public final LayoutSupportDelegate getOldLayoutSupport() {
240         return (LayoutSupportDelegate) oldPropertyValue;
241     }
242
243     public final LayoutSupportDelegate getNewLayoutSupport() {
244         return (LayoutSupportDelegate) newPropertyValue;
245     }
246
247     public final int[] getReordering() {
248         return reordering;
249     }
250
251     public final Event getComponentEvent() {
252         return componentEvent;
253     }
254
255     public final String JavaDoc getEventHandler() {
256         return propertyName;
257     }
258
259     public final String JavaDoc getOldEventHandler() {
260         return (String JavaDoc) oldPropertyValue;
261     }
262
263     public final String JavaDoc getNewEventHandler() {
264         return (String JavaDoc) newPropertyValue;
265     }
266
267     public final String JavaDoc getNewEventHandlerContent() {
268         return changeType == EVENT_HANDLER_ADDED
269                    || changeType == EVENT_HANDLER_REMOVED ?
270                (String JavaDoc) newPropertyValue : null;
271     }
272
273     public final String JavaDoc getOldEventHandlerContent() {
274         if (changeType == EVENT_HANDLER_ADDED
275             || changeType == EVENT_HANDLER_REMOVED)
276         {
277             if (additionalEvent != null) {
278                 if (additionalEvent.changeType == EVENT_HANDLER_REMOVED
279                     || additionalEvent.changeType == EVENT_HANDLER_ADDED)
280                 {
281                     oldPropertyValue = additionalEvent.oldPropertyValue;
282                 }
283                 additionalEvent = null;
284             }
285             return (String JavaDoc) oldPropertyValue;
286         }
287         return null;
288     }
289
290     public final void setOldEventHandlerContent(String JavaDoc text) {
291         if (changeType == EVENT_HANDLER_ADDED
292                 || changeType == EVENT_HANDLER_REMOVED)
293             oldPropertyValue = text;
294     }
295
296     // ----------
297

298     UndoableEdit getUndoableEdit() {
299         if (undoableEdit == null)
300             undoableEdit = new FormUndoableEdit();
301         return undoableEdit;
302     }
303
304     // ----------
305
// methods for events interested in additional events occured
306
// (used for undo/redo processing of event handlers)
307

308     private static void addToInterestList(FormModelEvent ev) {
309         if (interestList == null)
310             interestList = new ArrayList();
311         else
312             interestList.remove(ev);
313
314         interestList.add(ev);
315     }
316
317     private static void removeFromInterestList(FormModelEvent ev) {
318         if (interestList != null)
319             interestList.remove(ev);
320     }
321
322     private static void informInterestedEvents(FormModelEvent newEvent) {
323         if (interestList != null)
324             for (Iterator it=interestList.iterator(); it.hasNext(); )
325                 ((FormModelEvent)it.next()).newEventCreated(newEvent);
326     }
327
328     private void newEventCreated(FormModelEvent newEvent) {
329         additionalEvent = newEvent;
330     }
331
332     // ----------
333

334     private class FormUndoableEdit extends AbstractUndoableEdit {
335         public void undo() throws CannotUndoException {
336             super.undo();
337
338             // turn off undo/redo monitoring in FormModel while undoing!
339
boolean undoRedoOn = getFormModel().isUndoRedoRecording();
340             if (undoRedoOn)
341                 getFormModel().setUndoRedoRecording(false);
342
343             switch(changeType) {
344                 case CONTAINER_LAYOUT_EXCHANGED:
345                     FormModel.t("UNDO: container layout change"); // NOI18N
346
undoContainerLayoutExchange();
347                     break;
348                 case CONTAINER_LAYOUT_CHANGED:
349                     FormModel.t("UNDO: container layout property change"); // NOI18N
350
undoContainerLayoutChange();
351                     break;
352                 case COMPONENT_LAYOUT_CHANGED:
353                     FormModel.t("UNDO: component layout constraints change"); // NOI18N
354
undoComponentLayoutChange();
355                     break;
356                 case COMPONENTS_REORDERED:
357                     FormModel.t("UNDO: components reorder"); // NOI18N
358
undoComponentsReorder();
359                     break;
360                 case COMPONENT_ADDED:
361                     FormModel.t("UNDO: component addition"); // NOI18N
362
undoComponentAddition();
363                     break;
364                 case COMPONENT_REMOVED:
365                     FormModel.t("UNDO: component removal"); // NOI18N
366
undoComponentRemoval();
367                     break;
368                 case COMPONENT_PROPERTY_CHANGED:
369                     FormModel.t("UNDO: component property change"); // NOI18N
370
undoComponentPropertyChange();
371                     break;
372                 case SYNTHETIC_PROPERTY_CHANGED:
373                     FormModel.t("UNDO: synthetic property change"); // NOI18N
374
undoSyntheticPropertyChange();
375                     break;
376                 case EVENT_HANDLER_ADDED:
377                     FormModel.t("UNDO: event handler addition"); // NOI18N
378
undoEventHandlerAddition();
379                     break;
380                 case EVENT_HANDLER_REMOVED:
381                     FormModel.t("UNDO: event handler removal"); // NOI18N
382
undoEventHandlerRemoval();
383                     break;
384                 case EVENT_HANDLER_RENAMED:
385                     FormModel.t("UNDO: event handler renaming"); // NOI18N
386
undoEventHandlerRenaming();
387                     break;
388
389                 default: FormModel.t("UNDO: "+changeType); // NOI18N
390
break;
391             }
392
393             if (undoRedoOn) // turn on undo/redo monitoring again
394
getFormModel().setUndoRedoRecording(true);
395         }
396
397         public void redo() throws CannotRedoException {
398             super.redo();
399
400             // turn off undo/redo monitoring in FormModel while redoing!
401
boolean undoRedoOn = getFormModel().isUndoRedoRecording();
402             if (undoRedoOn)
403                 getFormModel().setUndoRedoRecording(false);
404
405             switch(changeType) {
406                 case CONTAINER_LAYOUT_EXCHANGED:
407                     FormModel.t("REDO: container layout change"); // NOI18N
408
redoContainerLayoutExchange();
409                     break;
410                 case CONTAINER_LAYOUT_CHANGED:
411                     FormModel.t("REDO: container layout property change"); // NOI18N
412
redoContainerLayoutChange();
413                     break;
414                 case COMPONENT_LAYOUT_CHANGED:
415                     FormModel.t("REDO: component layout constraints change"); // NOI18N
416
redoComponentLayoutChange();
417                     break;
418                 case COMPONENTS_REORDERED:
419                     FormModel.t("REDO: components reorder"); // NOI18N
420
redoComponentsReorder();
421                     break;
422                 case COMPONENT_ADDED:
423                     FormModel.t("REDO: component addition"); // NOI18N
424
redoComponentAddition();
425                     break;
426                 case COMPONENT_REMOVED:
427                     FormModel.t("REDO: component removal"); // NOI18N
428
redoComponentRemoval();
429                     break;
430                 case COMPONENT_PROPERTY_CHANGED:
431                     FormModel.t("REDO: component property change"); // NOI18N
432
redoComponentPropertyChange();
433                     break;
434                 case SYNTHETIC_PROPERTY_CHANGED:
435                     FormModel.t("REDO: synthetic property change"); // NOI18N
436
redoSyntheticPropertyChange();
437                     break;
438                 case EVENT_HANDLER_ADDED:
439                     FormModel.t("REDO: event handler addition"); // NOI18N
440
redoEventHandlerAddition();
441                     break;
442                 case EVENT_HANDLER_REMOVED:
443                     FormModel.t("REDO: event handler removal"); // NOI18N
444
redoEventHandlerRemoval();
445                     break;
446                 case EVENT_HANDLER_RENAMED:
447                     FormModel.t("REDO: event handler renaming"); // NOI18N
448
redoEventHandlerRenaming();
449                     break;
450
451                 default: FormModel.t("REDO: "+changeType); // NOI18N
452
break;
453             }
454
455             if (undoRedoOn) // turn on undo/redo monitoring again
456
getFormModel().setUndoRedoRecording(true);
457         }
458
459         public String JavaDoc getUndoPresentationName() {
460             return ""; // NOI18N
461
}
462         public String JavaDoc getRedoPresentationName() {
463             return ""; // NOI18N
464
}
465
466         // -------------
467

468         private void undoContainerLayoutExchange() {
469             try {
470                 LayoutSupportDelegate layoutDelegate = getOldLayoutSupport();
471                 if (layoutDelegate != null) {
472                     getFormModel().setContainerLayoutImpl(
473                             (RADVisualContainer)getContainer(),
474                             layoutDelegate,
475                             null);
476                 }
477                 else {
478                     getFormModel().setNaturalContainerLayoutImpl(
479                         (RADVisualContainer)getContainer());
480                 }
481             }
482             catch (Exception JavaDoc ex) {
483                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
484             }
485         }
486
487         private void redoContainerLayoutExchange() {
488             try {
489                 LayoutSupportDelegate layoutDelegate = getNewLayoutSupport();
490                 if (layoutDelegate != null) {
491                     getFormModel().setContainerLayoutImpl(
492                             (RADVisualContainer)getContainer(),
493                             layoutDelegate,
494                             null);
495                 }
496                 else {
497                     getFormModel().setNaturalContainerLayoutImpl(
498                             (RADVisualContainer)getContainer());
499                 }
500             }
501             catch (Exception JavaDoc ex) {
502                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
503             }
504         }
505
506         private void undoContainerLayoutChange() {
507             RADVisualContainer metacont = (RADVisualContainer) getComponent();
508             LayoutSupportManager laysup = metacont.getLayoutSupport();
509             if (laysup != null) {
510                 String JavaDoc propName = getPropertyName();
511                 if (propName != null) {
512                     Node.Property prop = laysup.getLayoutProperty(propName);
513                     if (prop != null) {
514                         try {
515                             prop.setValue(getOldPropertyValue());
516                         }
517                         catch (Exception JavaDoc ex) { // should not happen
518
ex.printStackTrace();
519                         }
520                     }
521                 }
522             }
523             else {
524                 getFormModel().fireContainerLayoutChanged(metacont, null, null, null);
525             }
526         }
527
528         private void redoContainerLayoutChange() {
529             RADVisualContainer metacont = (RADVisualContainer) getComponent();
530             LayoutSupportManager laysup = metacont.getLayoutSupport();
531             if (laysup != null) {
532                 String JavaDoc propName = getPropertyName();
533                 if (propName != null) {
534                     Node.Property prop = laysup.getLayoutProperty(propName);
535                     if (prop != null) {
536                         try {
537                             prop.setValue(getNewPropertyValue());
538                         }
539                         catch (Exception JavaDoc ex) { // should not happen
540
ex.printStackTrace();
541                         }
542                     }
543                 }
544             }
545             else {
546                 getFormModel().fireContainerLayoutChanged(metacont, null, null, null);
547             }
548         }
549
550         private void undoComponentLayoutChange() {
551             if (getComponent() instanceof RADVisualComponent) {
552                 ((RADVisualComponent)getComponent()).getConstraintsProperties();
553                 Node.Property prop =
554                     getComponent().getPropertyByName(getPropertyName());
555                 if (prop != null)
556                     try {
557                         prop.setValue(getOldPropertyValue());
558                     }
559                     catch (Exception JavaDoc ex) { // should not happen
560
ex.printStackTrace();
561                     }
562             }
563         }
564
565         private void redoComponentLayoutChange() {
566             if (getComponent() instanceof RADVisualComponent) {
567                 ((RADVisualComponent)getComponent()).getConstraintsProperties();
568                 Node.Property prop =
569                     getComponent().getPropertyByName(getPropertyName());
570                 if (prop != null)
571                     try {
572                         prop.setValue(getNewPropertyValue());
573                     }
574                     catch (Exception JavaDoc ex) { // should not happen
575
ex.printStackTrace();
576                     }
577             }
578         }
579
580         private void undoComponentAddition() {
581             removeComponent();
582         }
583
584         private void undoComponentRemoval() {
585             addComponent();
586         }
587
588         private void redoComponentAddition() {
589             addComponent();
590         }
591
592         private void redoComponentRemoval() {
593             removeComponent();
594         }
595
596         private void addComponent() {
597             RADComponent[] currentSubComps = getContainer().getSubBeans();
598             RADComponent[] undoneSubComps =
599                 new RADComponent[currentSubComps.length+1];
600
601             if (componentIndex < 0)
602                 componentIndex = currentSubComps.length;
603
604             for (int i=0,j=0; j < undoneSubComps.length; i++,j++) {
605                 if (i == componentIndex) {
606                     undoneSubComps[j] = getComponent();
607                     if (i == currentSubComps.length)
608                         break;
609                     j++;
610                 }
611                 undoneSubComps[j] = currentSubComps[i];
612             }
613
614             if (getCreatedDeleted())
615                 FormModel.setInModelRecursively(getComponent(), true);
616
617             getContainer().initSubComponents(undoneSubComps);
618
619             if (getContainer() instanceof RADVisualContainer
620                 && getComponent() instanceof RADVisualComponent)
621             {
622                 LayoutSupportManager layoutSupport =
623                     ((RADVisualContainer)getContainer()).getLayoutSupport();
624                 if (layoutSupport != null)
625                     layoutSupport.addComponents(
626                         new RADVisualComponent[] { (RADVisualComponent)getComponent() },
627                         new LayoutConstraints[] { getComponentLayoutConstraints() },
628                         componentIndex);
629             }
630
631             getFormModel().fireComponentAdded(getComponent(), getCreatedDeleted());
632         }
633
634         private void removeComponent() {
635             getFormModel().removeComponentImpl(getComponent(), getCreatedDeleted());
636         }
637
638         private void undoComponentsReorder() {
639             if (getContainer() != null && reordering != null) {
640                 int[] revPerm = new int[reordering.length];
641                 for (int i=0; i < reordering.length; i++)
642                     revPerm[reordering[i]] = i;
643
644                 getContainer().reorderSubComponents(revPerm);
645                 getFormModel().fireComponentsReordered(getContainer(), revPerm);
646             }
647         }
648
649         private void redoComponentsReorder() {
650             if (getContainer() != null && reordering != null) {
651                 getContainer().reorderSubComponents(reordering);
652                 getFormModel().fireComponentsReordered(getContainer(),
653                                                        reordering);
654             }
655         }
656
657         private void undoComponentPropertyChange() {
658             Node.Property prop =
659                 getComponent().getPropertyByName(getPropertyName());
660             if (prop != null)
661                 try {
662                     prop.setValue(getOldPropertyValue());
663                 }
664                 catch (Exception JavaDoc ex) { // should not happen
665
ex.printStackTrace();
666                 }
667         }
668
669         private void redoComponentPropertyChange() {
670             Node.Property prop =
671                 getComponent().getPropertyByName(getPropertyName());
672             if (prop != null)
673                 try {
674                     prop.setValue(getNewPropertyValue());
675                 }
676                 catch (Exception JavaDoc ex) { // should not happen
677
ex.printStackTrace();
678                 }
679         }
680
681         private void undoSyntheticPropertyChange() {
682             String JavaDoc propName = getPropertyName();
683             if (propName.startsWith(RADProperty.SYNTH_PREFIX)) {
684                 // special case - pre/post init code of a property
685
if (propName.startsWith(RADProperty.SYNTH_PRE_CODE)) {
686                     FormProperty prop = (FormProperty)
687                       getComponent().getPropertyByName(
688                         propName.substring(RADProperty.SYNTH_PRE_CODE.length()));
689                     prop.setPreCode((String JavaDoc)getOldPropertyValue());
690                 }
691                 else if (propName.startsWith(RADProperty.SYNTH_POST_CODE)) {
692                     FormProperty prop = (FormProperty)
693                       getComponent().getPropertyByName(
694                         propName.substring(RADProperty.SYNTH_POST_CODE.length()));
695                     prop.setPostCode((String JavaDoc)getOldPropertyValue());
696                 }
697             } else {
698                 Node.Property[] props;
699                 if (getComponent() == null) { // form synthetic property
700
FormEditor formEditor = FormEditor.getFormEditor(getFormModel());
701                     FormRootNode rootNode = (FormRootNode)formEditor.getFormRootNode();
702                     props = rootNode.getAllProperties();
703                 } else { // component synthetic property
704
props = getComponent().getSyntheticProperties();
705                 }
706                 for (int i=0; i < props.length; i++) {
707                     if (props[i].getName().equals(propName)) {
708                         try {
709                             props[i].setValue(getOldPropertyValue());
710                         }
711                         catch (Exception JavaDoc ex) { // should not happen
712
ex.printStackTrace();
713                         }
714                         break;
715                     }
716                 }
717             }
718         }
719
720         private void redoSyntheticPropertyChange() {
721             String JavaDoc propName = getPropertyName();
722             if (propName.startsWith(RADProperty.SYNTH_PREFIX)) {
723                 // special case - pre/post init code of a property
724
if (propName.startsWith(RADProperty.SYNTH_PRE_CODE)) {
725                     FormProperty prop = (FormProperty)
726                       getComponent().getPropertyByName(
727                         propName.substring(RADProperty.SYNTH_PRE_CODE.length()));
728                     prop.setPreCode((String JavaDoc)getNewPropertyValue());
729                 }
730                 else if (propName.startsWith(RADProperty.SYNTH_POST_CODE)) {
731                     FormProperty prop = (FormProperty)
732                       getComponent().getPropertyByName(
733                         propName.substring(RADProperty.SYNTH_POST_CODE.length()));
734                     prop.setPostCode((String JavaDoc)getNewPropertyValue());
735                 }
736             } else {
737                 Node.Property[] props;
738                 if (getComponent() == null) { // form synthetic property
739
FormEditor formEditor = FormEditor.getFormEditor(getFormModel());
740                     FormRootNode rootNode = (FormRootNode)formEditor.getFormRootNode();
741                     props = rootNode.getAllProperties();
742                 } else { // component synthetic property
743
props = getComponent().getSyntheticProperties();
744                 }
745                 for (int i=0; i < props.length; i++) {
746                     if (props[i].getName().equals(propName)) {
747                         try {
748                             props[i].setValue(getNewPropertyValue());
749                         }
750                         catch (Exception JavaDoc ex) { // should not happen
751
ex.printStackTrace();
752                         }
753                         break;
754                     }
755                 }
756             }
757         }
758
759         private void undoEventHandlerAddition() {
760             Event event = getComponentEvent();
761             if (event == null)
762                 return;
763
764             addToInterestList(FormModelEvent.this);
765
766             getFormModel().getFormEvents().detachEvent(event, getEventHandler());
767
768             removeFromInterestList(FormModelEvent.this);
769
770             // hack: reset the event property to update the property sheet
771
Node.Property prop = getComponent().getPropertyByName(event.getId());
772             if (prop != null) {
773                 try {
774                     if (getEventHandler().equals(prop.getValue()))
775                         prop.setValue(null);
776                 }
777                 catch (Exception JavaDoc ex) { // should not happen
778
ex.printStackTrace();
779                 }
780             }
781         }
782
783         private void redoEventHandlerAddition() {
784             Event event = getComponentEvent();
785             if (event == null)
786                 return;
787
788             getFormModel().getFormEvents().attachEvent(
789                 event,
790                 getEventHandler(),
791                 getOldEventHandlerContent()); //handlerText
792

793             // hack: set the event property to update the property sheet
794
Node.Property prop = getComponent().getPropertyByName(event.getId());
795             if (prop != null) {
796                 try {
797                     prop.setValue(getEventHandler());
798                 }
799                 catch (Exception JavaDoc ex) { // should not happen
800
ex.printStackTrace();
801                 }
802             }
803         }
804
805         private void undoEventHandlerRemoval() {
806             Event event = getComponentEvent();
807             if (event == null)
808                 return;
809
810             getFormModel().getFormEvents().attachEvent(
811                 event,
812                 getEventHandler(),
813                 getOldEventHandlerContent()); //handlerText
814

815             // hack: set the event property to update the property sheet
816
Node.Property prop = getComponent().getPropertyByName(event.getId());
817             if (prop != null) {
818                 try {
819                     prop.setValue(getEventHandler());
820                 }
821                 catch (Exception JavaDoc ex) { // should not happen
822
ex.printStackTrace();
823                 }
824             }
825         }
826
827         private void redoEventHandlerRemoval() {
828             Event event = getComponentEvent();
829             if (event == null)
830                 return;
831
832             addToInterestList(FormModelEvent.this);
833
834             getFormModel().getFormEvents().detachEvent(event, getEventHandler());
835
836             removeFromInterestList(FormModelEvent.this);
837
838             // hack: reset the event property to reflect the change in property sheet
839
Node.Property prop = getComponent().getPropertyByName(event.getId());
840             if (prop != null) {
841                 try {
842                     if (getEventHandler().equals(prop.getValue()))
843                         prop.setValue(null);
844                 }
845                 catch (Exception JavaDoc ex) { // should not happen
846
ex.printStackTrace();
847                 }
848             }
849         }
850
851         private void undoEventHandlerRenaming() {
852             FormEvents formEvents = getFormModel().getFormEvents();
853
854             formEvents.renameEventHandler(getNewEventHandler(),
855                                           getOldEventHandler());
856
857             Event[] events = formEvents.getEventsForHandler(
858                                             getOldEventHandler());
859             for (int i=0 ; i < events.length; i++) {
860                 Node.Property prop = events[i].getComponent()
861                                        .getPropertyByName(events[i].getId());
862                 if (prop != null) {
863                     try {
864                         prop.setValue(getOldEventHandler());
865                     }
866                     catch (Exception JavaDoc ex) { // should not happen
867
ex.printStackTrace();
868                     }
869                 }
870             }
871         }
872
873         private void redoEventHandlerRenaming() {
874             FormEvents formEvents = getFormModel().getFormEvents();
875
876             formEvents.renameEventHandler(getOldEventHandler(),
877                                           getNewEventHandler());
878
879             Event[] events = formEvents.getEventsForHandler(
880                                             getNewEventHandler());
881             for (int i=0 ; i < events.length; i++) {
882                 Node.Property prop = events[i].getComponent()
883                                        .getPropertyByName(events[i].getId());
884                 if (prop != null) {
885                     try {
886                         prop.setValue(getNewEventHandler());
887                     }
888                     catch (Exception JavaDoc ex) { // should not happen
889
ex.printStackTrace();
890                     }
891                 }
892             }
893         }
894     }
895 }
896
Popular Tags