KickJava   Java API By Example, From Geeks To Geeks.

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


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.Insets JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.Shape JavaDoc;
26 import java.awt.geom.Rectangle2D JavaDoc;
27 import java.util.EnumSet JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ListIterator JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.JTextField JavaDoc;
32 import org.netbeans.api.visual.action.ActionFactory;
33 import org.netbeans.api.visual.action.InplaceEditorProvider;
34 import org.netbeans.api.visual.action.TextFieldInplaceEditor;
35 import org.netbeans.api.visual.border.Border;
36 import org.netbeans.api.visual.layout.Layout;
37 import org.netbeans.api.visual.model.ObjectState;
38 import org.netbeans.api.visual.widget.LabelWidget;
39 import org.netbeans.api.visual.widget.Scene;
40 import org.netbeans.api.visual.widget.Widget;
41 import org.netbeans.modules.visual.action.TextFieldInplaceEditorProvider;
42 import org.netbeans.modules.xml.refactoring.ui.util.AnalysisUtilities;
43 import org.netbeans.modules.xml.wsdl.model.Part;
44 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.border.FilledBorder;
45 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.layout.TableLayout;
46 import org.netbeans.modules.xml.xam.ui.XAMUtils;
47 import org.openide.actions.NewAction;
48 import org.openide.util.Lookup;
49 import org.openide.util.NbBundle;
50
51
52 public class PartWidget extends AbstractWidget<Part> {
53     
54     private LabelWidget nameWidget;
55     private PartTypeChooserWidget typeWidget;
56     
57     public PartWidget(Scene scene, Part part, Lookup lookup) {
58         super(scene, part, lookup);
59
60         setLayout(ROW_LAYOUT);
61         setBorder(BORDER);
62         
63         createContent();
64     }
65     
66     
67     private void removeContent() {
68         removeChildren();
69     }
70     
71     PartTypeChooserWidget getPartChooserWidget() {
72         return typeWidget;
73     }
74     
75     private void createContent() {
76         Part part = getWSDLComponent();
77         String JavaDoc name = part.getName();
78         
79         if (name == null) {
80             name = NbBundle.getMessage(PartWidget.class, "LBL_Undefined"); // NOI18N
81
} else if (name.trim().equals("")) { // NOI18N
82
name = NbBundle.getMessage(PartWidget.class, "LBL_Empty"); // NOI18N
83
}
84
85         nameWidget = createLabelWidget(getScene(), name);
86         typeWidget = new PartTypeChooserWidget(getScene(), part);
87         
88         addChild(nameWidget);
89         addChild(typeWidget);
90     }
91     
92     
93     public void updateContent() {
94         removeContent();
95         createContent();
96     }
97     
98     
99     private LabelWidget createLabelWidget(Scene scene, String JavaDoc text) {
100         LabelWidget result = new LabelWidget(scene, text);
101         result.setBorder(CELL_BORDER);
102         result.setFont(scene.getDefaultFont());
103         result.setAlignment(LabelWidget.Alignment.LEFT);
104         result.setVerticalAlignment(LabelWidget.VerticalAlignment.CENTER);
105         result.getActions().addAction(ActionFactory.createInplaceEditorAction(
106                 (InplaceEditorProvider<JTextField JavaDoc>) new PartNameInplaceEditorProvider()));
107         return result;
108     }
109
110
111     protected void notifyStateChanged(ObjectState previousState, ObjectState state) {
112         super.notifyStateChanged(previousState, state);
113
114         if (state.isSelected()) {
115             MessageWidget messageWidget = getMessageWidget();
116             if (messageWidget != null) {
117                 messageWidget.updateButtonState();
118             }
119         } else {
120             MessageWidget messageWidget = getMessageWidget();
121             if (messageWidget != null) {
122                 messageWidget.updateButtonState();
123             }
124         }
125     }
126     
127
128     private MessageWidget getMessageWidget() {
129         for (Widget w = this; w != null; w = w.getParentWidget()) {
130             if (w instanceof MessageWidget) return (MessageWidget) w;
131         }
132         return null;
133     }
134     
135     
136     public static final Layout ROW_LAYOUT = new TableLayout(2, 1, 0, 100);
137     public static final Border CELL_BORDER = new FilledBorder(0, 0, 1, 8, null,
138             Color.WHITE);
139
140     
141     private static class PartNameInplaceEditorProvider implements
142             InplaceEditorProvider<JTextField JavaDoc>,
143             TextFieldInplaceEditor
144     {
145         private TextFieldInplaceEditorProvider editorProvider;
146         
147         public PartNameInplaceEditorProvider() {
148             this.editorProvider = new TextFieldInplaceEditorProvider(this,
149                     null);
150         }
151
152         
153         public boolean isEnabled(Widget widget) {
154             Part part = getPart(widget);
155             if (part != null) {
156                 return XAMUtils.isWritable(part.getModel());
157             }
158             return false;
159         }
160
161         
162         public String JavaDoc getText(Widget widget) {
163             Part part = getPart(widget);
164             String JavaDoc name = (part != null) ? part.getName() : null;
165             return (name == null) ? "" : name; // NOI18N
166
}
167
168         
169         public void setText(Widget widget, String JavaDoc text) {
170             Part part = getPart(widget);
171             if (part != null) {
172                 // try rename silent and locally
173
AnalysisUtilities.locallyRenameRefactor(part, text);
174             }
175         }
176         
177         
178         private Part getPart(Widget widget) {
179             PartWidget partWidget = getPartWidget(widget);
180             return (partWidget == null) ? null : partWidget.getWSDLComponent();
181         }
182         
183         
184         private PartWidget getPartWidget(Widget widget) {
185             for (Widget w = widget; w != null; w = w.getParentWidget()) {
186                 if (w instanceof PartWidget) {
187                     return (PartWidget) w;
188                 }
189             }
190             return null;
191         }
192         
193         
194         public void notifyOpened(
195                 InplaceEditorProvider.EditorController controller,
196                 Widget widget, JTextField JavaDoc component)
197         {
198             editorProvider.notifyOpened(controller, widget, component);
199         }
200
201         
202         public void notifyClosing(
203                 InplaceEditorProvider.EditorController controller,
204                 Widget widget,
205                 JTextField JavaDoc component,
206                 boolean commit)
207         {
208             editorProvider.notifyClosing(controller, widget, component, commit);
209         }
210
211         
212         public JTextField JavaDoc createEditorComponent(
213                 InplaceEditorProvider.EditorController controller,
214                 Widget widget)
215         {
216             return editorProvider.createEditorComponent(controller, widget);
217         }
218         
219
220         public Rectangle JavaDoc getInitialEditorComponentBounds(
221                 InplaceEditorProvider.EditorController controller,
222                 Widget widget,
223                 JTextField JavaDoc component,
224                 Rectangle JavaDoc bounds)
225         {
226             double k = widget.getScene().getZoomFactor();
227             
228             Widget parent = widget.getParentWidget();
229             
230             Rectangle JavaDoc widgetBounds = widget.convertLocalToScene(widget.getBounds());
231             Rectangle JavaDoc parentBounds = parent.convertLocalToScene(parent.getBounds());
232             
233             int x1 = parentBounds.x + 24;
234             int y1 = parentBounds.y + 24;
235             
236             int x2 = x1 + widgetBounds.width + 1;
237             int y2 = y1 + parentBounds.height + 1;
238             
239             int x = (int) Math.floor(k * x1);
240             int y = (int) Math.floor(k * y1);
241             
242             int w = (int) Math.ceil(k * x2) - x;
243             int h = (int) Math.ceil(k * y2) - y;
244             
245             int preferredHeight = component.getPreferredSize().height;
246             
247             if (preferredHeight > h) {
248                 y -= (preferredHeight - h) / 2;
249                 h = preferredHeight;
250             }
251             
252             return new Rectangle JavaDoc(x, y, w, h);
253         }
254
255         
256         public EnumSet JavaDoc<InplaceEditorProvider.ExpansionDirection>
257                 getExpansionDirections(
258                         InplaceEditorProvider.EditorController controller,
259                         Widget widget,
260                         JTextField JavaDoc component)
261         {
262             return editorProvider.getExpansionDirections(controller, widget,
263                     component);
264         }
265     }
266     
267
268     private static final Border BORDER = new FilledBorder(
269             new Insets JavaDoc(1, 0, 0, 0), new Insets JavaDoc(0, 0, 0, 0),
270             new Color JavaDoc(0x999999), null);
271     
272
273     protected Shape JavaDoc createSelectionShape() {
274         Rectangle JavaDoc rect = getBounds();
275         return new Rectangle2D.Double JavaDoc(rect.x + 1, rect.y + 2, rect.width - 2,
276                 rect.height - 3);
277     }
278
279     @Override JavaDoc
280     protected void updateActions(List JavaDoc<Action JavaDoc> actions) {
281         super.updateActions(actions);
282         ListIterator JavaDoc<Action JavaDoc> liter = actions.listIterator();
283         while (liter.hasNext()) {
284             Action JavaDoc action = liter.next();
285             if (action instanceof NewAction) {
286                 liter.remove();
287             }
288         }
289     }
290 }
291
Popular Tags