KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.awt.datatransfer.DataFlavor JavaDoc;
27 import java.awt.datatransfer.Transferable JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ListIterator JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.BorderFactory JavaDoc;
32 import org.netbeans.api.visual.action.ActionFactory;
33 import org.netbeans.api.visual.action.TextFieldInplaceEditor;
34 import org.netbeans.api.visual.action.WidgetAction.WidgetDropTargetDragEvent;
35 import org.netbeans.api.visual.action.WidgetAction.WidgetDropTargetDropEvent;
36 import org.netbeans.api.visual.layout.Layout;
37 import org.netbeans.api.visual.layout.LayoutFactory;
38 import org.netbeans.api.visual.layout.LayoutFactory.SerialAlignment;
39 import org.netbeans.api.visual.widget.Scene;
40 import org.netbeans.api.visual.widget.Widget;
41 import org.netbeans.modules.xml.wsdl.model.Definitions;
42 import org.netbeans.modules.xml.wsdl.model.PortType;
43 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
44 import org.netbeans.modules.xml.wsdl.model.extensions.bpel.BPELQName;
45 import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType;
46 import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role;
47 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.actions.HoverActionProvider;
48 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.PortTypeNode;
49 import org.netbeans.modules.xml.xam.ComponentEvent;
50 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
51 import org.netbeans.modules.xml.xam.ui.XAMUtils;
52 import org.openide.actions.NewAction;
53 import org.openide.nodes.Node;
54 import org.openide.util.Lookup;
55 import org.openide.util.NbBundle;
56
57 /**
58  * Represents a role in the WSDL model.
59  */

60 public class RoleWidget extends AbstractWidget<Role> implements DnDHandler{
61     private PortTypeWidget mPortTypeWidget;
62     private CenteredLabelWidget mLabelWidget;
63     private PartnerLinkType mPartnerLinkType;
64     private boolean leftSided;
65
66     /**
67      * Creates a new instance of RoleWidget.
68      *
69      * @param scene the Scene to contain this widget.
70      * @param role the WSDL component.
71      * @param lookup the Lookup for this widget.
72      */

73     public RoleWidget(Scene scene, Role role, Lookup lookup) {
74         super(scene, role, lookup);
75         mPartnerLinkType = (PartnerLinkType) getLookup().lookup(PartnerLinkType.class);
76         init();
77     }
78
79     private void init() {
80         setLayout(new RoleWidgetLayout(10));
81         
82         setOpaque(true);
83         updateContent();
84         if (getWSDLComponent() != null)
85             getActions().addAction(((PartnerScene)getScene()).getDnDAction());
86         //setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
87
}
88     
89     private String JavaDoc getName() {
90         Role role = getWSDLComponent();
91         if (role == null) {
92             return NbBundle.getMessage(RoleWidget.class, "ROLEWIDGET_PLACE_HOLDER_ROLENAME");
93         }
94         
95         return role.getName();
96     }
97
98     protected void setLeftSided(boolean isLeftSided) {
99         leftSided = isLeftSided;
100     }
101     
102     protected boolean isLeftSided() {
103         return leftSided;
104     }
105     
106     public void showHotSpot(boolean show) {
107         if (show) {
108             mPortTypeWidget.showHotSpot();
109         } else {
110             mPortTypeWidget.clearHotSpot();
111         }
112     }
113
114     @Override JavaDoc
115     public void updateContent() {
116         refreshPortTypeColumn();
117         getScene().validate();
118     }
119
120     @Override JavaDoc
121     public void childrenDeleted(ComponentEvent event) {
122         super.childrenDeleted(event);
123         // Ignore whether this event is for our component or not, since it
124
// may be for a PortType shared between multiple roles.
125
if (event.getSource() instanceof Definitions) {
126             // Check if the port type is no longer in the model.
127
PortType portType = getPortType();
128             PortType widgetPT = mPortTypeWidget.getWSDLComponent();
129             if (widgetPT != null && portType == null) {
130                 if (EventQueue.isDispatchThread()) {
131                     updateContent();
132                 } else {
133                     EventQueue.invokeLater(new Runnable JavaDoc(){
134                         public void run() {
135                             updateContent();
136                         }
137                     });
138                 }
139             }
140         }
141     }
142
143     /**
144      * Retrieve the PortType for this RoleWidget.
145      *
146      * @return PortType, or null if not available.
147      */

148     private PortType getPortType() {
149         try {
150             Role role = getWSDLComponent();
151             if (role != null) {
152                 NamedComponentReference<PortType> ptref = role.getPortType();
153                 if (ptref != null) {
154                     return ptref.get();
155                 }
156             }
157         } catch (IllegalStateException JavaDoc ise) {
158             // Indicates the referencing component is no longer in the model.
159
// Fall through and return null.
160
}
161         return null;
162     }
163
164     private void refreshPortTypeColumn() {
165         removeChildren();
166         if (getWSDLComponent() == null) {
167             mLabelWidget = new CenteredLabelWidget(getScene(), getName(), WidgetConstants.DISABLED_GRAY);
168             mLabelWidget.setToolTipText(NbBundle.getMessage(RoleWidget.class, "RoleWidget_DBL_CLICK_CREATE_NEW_ROLE_TT"));
169         } else {
170             mLabelWidget = new CenteredLabelWidget(getScene(), getName(), Color.WHITE);
171             mLabelWidget.setToolTipText(null);
172         }
173         addChild(mLabelWidget);
174         //mLabelWidget.setBackground(Color.WHITE);
175
mLabelWidget.setBorder(BorderFactory.createLineBorder(Color.GRAY));
176         mLabelWidget.getActions().addAction(ActionFactory.createInplaceEditorAction(new TextFieldInplaceEditor() {
177
178             public void setText(Widget widget, String JavaDoc text) {
179                 WSDLModel model = mPartnerLinkType.getModel();
180                 boolean newRoleCreated = false;
181                 try {
182                     if (model.startTransaction()) {
183                         Role role = getWSDLComponent();
184                         if (role == null) {
185                             role = (Role) mPartnerLinkType.getModel().
186                                     getFactory().create(mPartnerLinkType,
187                                     BPELQName.ROLE.getQName());
188                             if (mPartnerLinkType.getRole1() == null) {
189                                 mPartnerLinkType.setRole1(role);
190                             } else if (mPartnerLinkType.getRole2() == null) {
191                                 mPartnerLinkType.setRole2(role);
192                             }
193                             newRoleCreated = true;
194                         }
195                         role.setName(text);
196                     }
197                 } finally {
198                     model.endTransaction();
199                 }
200                 if (newRoleCreated) {
201                     updateContent();
202                 }
203             }
204
205             public boolean isEnabled(Widget widget) {
206                 if (mPartnerLinkType != null) {
207                     return XAMUtils.isWritable(mPartnerLinkType.getModel());
208                 }
209                 return false;
210             }
211
212             public String JavaDoc getText(Widget widget) {
213                 Role role = getWSDLComponent();
214                 if (role == null) {
215                     String JavaDoc name = mPartnerLinkType.getName() + "Role"; //generate a new name;
216
if (mPartnerLinkType.getRole1() != null && mPartnerLinkType.getRole1().getName().equals(name)
217                             || mPartnerLinkType.getRole2() != null && mPartnerLinkType.getRole2().getName().equals(name)) {
218                         name = name + "1";
219                     }
220                     
221                     return name;
222                 }
223                 return role.getName();
224             }
225
226         }, null));
227         mLabelWidget.getActions().addAction(HoverActionProvider.getDefault(
228                 getScene()).getHoverAction());
229         WidgetFactory factory = WidgetFactory.getInstance();
230         PortType portType = getPortType();
231         if (portType != null) {
232             mPortTypeWidget = (PortTypeWidget) factory.createWidget(
233                     getScene(), portType, getLookup());
234         } else {
235             mPortTypeWidget = (PortTypeWidget) factory.createWidget(
236                     getScene(), PortType.class, getLookup());
237         }
238         
239         addChild(mPortTypeWidget);
240     }
241     
242     /**
243      * This layout makes the second widget (which is the port type widget) to fill the
244      * remaining height of the parent widget, after the first widget is placed.
245      *
246      * @author skini
247      *
248      */

249     class RoleWidgetLayout implements Layout {
250         
251         private Layout vertLayout;
252         private int mGap = 0;
253         
254         public RoleWidgetLayout(int gap) {
255             mGap = gap;
256             vertLayout = LayoutFactory.createVerticalLayout(SerialAlignment.JUSTIFY, gap);
257         }
258         
259         public void justify(Widget widget) {
260             List JavaDoc<Widget> children = widget.getChildren();
261             
262             if (children.size() < 2) return;
263             
264             Rectangle JavaDoc parentBounds = widget.getClientArea();
265             
266             Widget nameWidget = children.get(0);
267             Widget portTypeWidget = children.get(1);
268             
269             Point JavaDoc nameWLocation = nameWidget.getLocation();
270             Rectangle JavaDoc nameBounds = nameWidget.getBounds();
271             
272             int parentX1 = parentBounds.x;
273             int parentX2 = parentX1 + parentBounds.width;
274             int nameX1 = nameWLocation.x + nameBounds.x;
275             int nameX2 = nameX1 + nameBounds.width;
276             
277
278             nameBounds.x = Math.min (parentX1, nameX1);
279             nameBounds.width = Math.max (parentX2, nameX2) - nameBounds.x;
280             nameBounds.x -= nameWLocation.x;
281             nameWidget.resolveBounds (nameWLocation, nameBounds);
282             
283             
284             Point JavaDoc portTypeWLocation = portTypeWidget.getLocation();
285             Rectangle JavaDoc portTypeBounds = portTypeWidget.getBounds();
286             
287             int portTypeX1 = portTypeWLocation.x + portTypeBounds.x;
288             int portTypeX2 = portTypeX1 + portTypeBounds.width;
289             
290
291             portTypeBounds.x = Math.min (parentX1, portTypeX1);
292             portTypeBounds.width = Math.max (parentX2, portTypeX2) - portTypeBounds.x;
293             portTypeBounds.x -= portTypeWLocation.x;
294             portTypeBounds.height = parentBounds.height - (nameBounds.height + mGap);
295             
296             portTypeWidget.resolveBounds (portTypeWLocation, portTypeBounds);
297         }
298
299         public void layout(Widget widget) {
300             vertLayout.layout(widget);
301         }
302
303         public boolean requiresJustification(Widget widget) {
304             return true;
305         }
306         
307     }
308
309     public void dragExit() {
310         mPortTypeWidget.setBorder(BorderFactory.createEmptyBorder());
311         
312     }
313
314     public boolean dragOver(Point JavaDoc scenePoint, WidgetDropTargetDragEvent event) {
315         Transferable JavaDoc transferable = event.getTransferable();
316
317         try {
318             if (transferable != null) {
319                 for (DataFlavor JavaDoc flavor : transferable.getTransferDataFlavors()) {
320                     Class JavaDoc repClass = flavor.getRepresentationClass();
321                     if (Node.class.isAssignableFrom(repClass)) {
322                         Node node = Node.class.cast(transferable.getTransferData(flavor));
323                         if (node instanceof PortTypeNode) {
324                             mPortTypeWidget.setBorder(BorderFactory.createLineBorder(WidgetConstants.HIT_POINT_BORDER, 2));
325                             return true;
326                         }
327                     }
328                 }
329             }
330         } catch (Exception JavaDoc ex) {
331             //do nothing
332
}
333         return false;
334     }
335
336     public boolean drop(Point JavaDoc scenePoint, WidgetDropTargetDropEvent event) {
337         Transferable JavaDoc transferable = event.getTransferable();
338         try {
339             if (transferable != null) {
340                 for (DataFlavor JavaDoc flavor : transferable.getTransferDataFlavors()) {
341                     Class JavaDoc repClass = flavor.getRepresentationClass();
342                     Object JavaDoc data = transferable.getTransferData(flavor);
343                     if (Node.class.isAssignableFrom(repClass)) {
344                         Node node = (Node) data;
345                         if (node instanceof PortTypeNode) {
346                             mPortTypeWidget.setBorder(BorderFactory.createEmptyBorder());
347                             setPortType((PortTypeNode)node);
348                         }
349                         return true;
350                     }
351                 }
352             }
353         } catch (Exception JavaDoc ex) {
354             //do nothing
355
}
356         return false;
357     }
358
359     private void setPortType(PortTypeNode node) {
360         PortType pt = (PortType) node.getWSDLComponent();
361         if (getWSDLComponent().getModel().startTransaction()) {
362             try {
363                 getWSDLComponent().setPortType(getWSDLComponent().createReferenceTo(pt, PortType.class));
364             } finally {
365                 getWSDLComponent().getModel().endTransaction();
366             }
367         }
368     }
369
370     public void expandForDragAndDrop() {}
371
372     public boolean isCollapsed() {
373         return false;
374     }
375
376     @Override JavaDoc
377     protected void updateActions(List JavaDoc<Action JavaDoc> actions) {
378         super.updateActions(actions);
379         ListIterator JavaDoc<Action JavaDoc> liter = actions.listIterator();
380         while (liter.hasNext()) {
381             Action JavaDoc action = liter.next();
382             if (action instanceof NewAction) {
383                 liter.remove();
384             }
385         }
386     }
387 }
388
Popular Tags