KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > grapheditor > widget > ExScene


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.JLabel JavaDoc;
29 import org.netbeans.api.visual.action.ActionFactory;
30 import org.netbeans.api.visual.action.SelectProvider;
31 import org.netbeans.api.visual.action.WidgetAction;
32 import org.netbeans.api.visual.model.ObjectScene;
33 import org.netbeans.api.visual.widget.Widget;
34 import org.netbeans.modules.xml.wsdl.model.Definitions;
35 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
36 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
37 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.DragOverSceneLayer;
38 import org.netbeans.modules.xml.xam.ComponentEvent;
39 import org.netbeans.modules.xml.xam.ComponentListener;
40
41 /**
42  *
43  * @author anjeleevich
44  */

45 public class ExScene extends ObjectScene implements ComponentListener {
46     private ButtonAction buttonAction;
47     private WidgetAction selectAction;
48     private DnDAction dndAction;
49     private WSDLModel model;
50     private MessagesWidget messagesWidget;
51     private CollaborationsWidget collaborationsWidget;
52     private DragOverSceneLayer dragOverLayer;
53     private Font JavaDoc defaultFont = new JLabel JavaDoc().getFont();
54     
55     public ExScene(WSDLModel model) {
56         super();
57         
58         this.model = model;
59         model.addComponentListener(this);
60
61         buttonAction = new ButtonAction();
62         selectAction = ActionFactory.createSelectAction(
63                 new ObjectSelectProvider());
64         dndAction = new DnDAction();
65         
66         getActions().addAction(selectAction);
67         getActions().addAction(buttonAction);
68         getActions().addAction(dndAction);
69     }
70     
71     
72     public ButtonAction getButtonAction() {
73         return buttonAction;
74     }
75
76     
77     public WidgetAction getSelectAction() {
78         return selectAction;
79     }
80     
81     
82     public WidgetAction getDnDAction() {
83         return dndAction;
84     }
85
86     
87     public MessagesWidget getMessagesWidget() {
88         if (messagesWidget == null) {
89              messagesWidget = new MessagesWidget(this, model);
90         }
91         
92         return messagesWidget;
93     }
94     
95     
96     public CollaborationsWidget getCollaborationsWidget() {
97         if (collaborationsWidget == null) {
98             collaborationsWidget = new CollaborationsWidget(this, model);
99         }
100         
101         return collaborationsWidget;
102     }
103     
104     public DragOverSceneLayer getDragOverLayer() {
105         if (dragOverLayer == null) {
106             dragOverLayer = new DragOverSceneLayer(this);
107         }
108         return dragOverLayer;
109     }
110
111     /**
112      * Scan the scene's object-to-widget mapping for components that are
113      * no longer in the component model, and remove them if so.
114      */

115     private void pruneStaleBindings() {
116         // Create a new set to avoid concurrent modification exceptions.
117
Set JavaDoc<Object JavaDoc> objects = new HashSet JavaDoc<Object JavaDoc>(getObjects());
118         for (Object JavaDoc object : objects) {
119             if (object instanceof WSDLComponent &&
120                     !isInModel((WSDLComponent) object)) {
121                 removeObject(object);
122             }
123         }
124     }
125
126     /**
127      * Determine if the component is in the model or not.
128      *
129      * @param component the component to query.
130      * @return true if component is in model, false otherwise.
131      */

132     private boolean isInModel(WSDLComponent component) {
133         WSDLComponent root = model.getRootComponent();
134         for (WSDLComponent c = component; c != null; c = c.getParent()) {
135             if (c == root) {
136                 return true;
137             }
138         }
139         return false;
140     }
141
142     public void valueChanged(ComponentEvent componentEvent) {
143         updateContent(componentEvent);
144     }
145
146     public void childrenAdded(ComponentEvent componentEvent) {
147         updateContent(componentEvent);
148     }
149
150     public void childrenDeleted(ComponentEvent componentEvent) {
151         updateContent(componentEvent);
152         // Perform the pruning in one place, as opposed to in the
153
// AbstractWidget class, which would be invoked many times.
154
if (EventQueue.isDispatchThread()) {
155             pruneStaleBindings();
156         } else {
157             EventQueue.invokeLater(new Runnable JavaDoc() {
158                 public void run() {
159                     pruneStaleBindings();
160                 }
161             });
162         }
163     }
164
165     /**
166      * If the event source is the model root, update the top-level widgets
167      * manually, since they are not registered as model listeners.
168      */

169     private void updateContent(ComponentEvent componentEvent) {
170         if (componentEvent.getSource() instanceof Definitions) {
171             Runnable JavaDoc updater = new Runnable JavaDoc() {
172                 public void run() {
173                     getMessagesWidget().updateContent();
174                     getCollaborationsWidget().updateContent();
175                     // Validate the scene after making changes.
176
validate();
177                 }
178             };
179             if (EventQueue.isDispatchThread()) {
180                 updater.run();
181             } else {
182                 EventQueue.invokeLater(updater);
183             }
184         }
185     }
186
187     public Font JavaDoc getDefaultFont() {
188         return defaultFont;
189     }
190
191     private class ObjectSelectProvider implements SelectProvider {
192         public boolean isAimingAllowed(Widget widget, Point JavaDoc localLocation,
193                 boolean invertSelection)
194         {
195             return false;
196         }
197         
198         public boolean isSelectionAllowed(Widget widget, Point JavaDoc localLocation,
199                 boolean invertSelection)
200         {
201             return true;
202 // Object object = findObject(widget);
203
// return object != null && (invertSelection
204
// || ! getSelectedObjects().contains(object));
205
}
206         
207
208         public void select(Widget widget, Point JavaDoc localLocation,
209                 boolean invertSelection)
210         {
211             if (widget instanceof ButtonWidget) {
212                 if (!((ButtonWidget) widget).isParenSelectionAllowed()) {
213                     // do nothing
214
return;
215                 }
216             }
217             
218             Object JavaDoc object = findObject(widget);
219             
220 // System.out.println("Object=" + object);
221
// System.out.println("getFocusedObject=" + getFocusedObject());
222

223             // setFocusedObject(object);
224
if (object != null) {
225                 if (getSelectedObjects().contains(object))
226                     return;
227                 userSelectionSuggested(Collections.singleton(object),
228                         invertSelection);
229             } else {
230                 userSelectionSuggested(Collections.emptySet(),
231                         invertSelection);
232             }
233         }
234     }
235 }
236
Popular Tags