KickJava   Java API By Example, From Geeks To Geeks.

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


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;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.EventQueue JavaDoc;
25 import java.awt.Image JavaDoc;
26 import java.awt.Point JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.MouseWheelListener JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.AbstractAction JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.ImageIcon JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.JScrollPane JavaDoc;
37 import javax.swing.JToggleButton JavaDoc;
38 import javax.swing.JToolBar JavaDoc;
39 import javax.swing.UIManager JavaDoc;
40 import javax.swing.border.Border JavaDoc;
41 import org.netbeans.api.visual.layout.LayoutFactory;
42 import org.netbeans.api.visual.layout.LayoutFactory.SerialAlignment;
43 import org.netbeans.api.visual.widget.LayerWidget;
44 import org.netbeans.api.visual.widget.Widget;
45 import org.netbeans.modules.visual.border.EmptyBorder;
46 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
47 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget.CollaborationsWidget;
48 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget.PartnerScene;
49 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget.MessagesWidget;
50 import org.netbeans.modules.xml.xam.Component;
51 import org.openide.util.NbBundle;
52 import org.openide.util.Utilities;
53
54 /**
55  * Contains the scrollable view that displays the widgets which represent
56  * the partner link types view (e.g. collaborations and messages).
57  *
58  * @author radval
59  * @author Nathan Fiedler
60  */

61 public class GraphView extends JPanel JavaDoc {
62     /** Manages the state of the widgets and corresponding objects. */
63     private PartnerScene scene;
64     /** Layer containing the visible widgets. */
65     private Widget mMainLayer;
66     /** The component model. */
67     private WSDLModel mModel;
68     /** Layer for drag and drop actions. */
69     private DragOverSceneLayer mDragLayer;
70     /** Manages the zoom level. */
71     private ZoomManager zoomer;
72     /** The widget showing the collaborations. */
73     private CollaborationsWidget collaborationsWidget;
74     /** The widget showing the messages. */
75     private MessagesWidget messagesWidget;
76     /** That which contains the collaborations and messages widgets. */
77     private Widget contentWidget;
78
79     /**
80      * Creates a new instance of GraphView.
81      */

82     public GraphView(WSDLModel model) {
83         super(new BorderLayout JavaDoc());
84         this.mModel = model;
85
86         scene = new PartnerScene(mModel);
87         JComponent JavaDoc sceneView = scene.createView();
88         scene.setBackground(Color.WHITE);
89         zoomer = new ZoomManager(scene);
90
91         // dirty hack to fix issue 93508
92
if (sceneView instanceof MouseWheelListener JavaDoc) {
93             sceneView.removeMouseWheelListener((MouseWheelListener JavaDoc) sceneView);
94         }
95         
96         collaborationsWidget = scene.getCollaborationsWidget();
97         messagesWidget = scene.getMessagesWidget();
98         // Note that the arrangement of collaborationsWidget and
99
// messagesWidget is also controlled by the View actions below.
100
contentWidget = new Widget(scene);
101         contentWidget.setBorder(new EmptyBorder(24, 24, 24, 24, false));
102         contentWidget.setLayout(LayoutFactory.createVerticalLayout(
103                 SerialAlignment.JUSTIFY, 16));
104         contentWidget.addChild(collaborationsWidget);
105         contentWidget.addChild(messagesWidget);
106
107         mMainLayer = new LayerWidget(scene);
108         mMainLayer.addChild(contentWidget);
109         mMainLayer.setPreferredLocation(new Point JavaDoc(0, 0));
110         mMainLayer.setBackground(Color.WHITE);
111
112         mDragLayer = scene.getDragOverLayer();
113
114         scene.addChild(mMainLayer);
115         scene.addChild(mDragLayer);
116
117         JScrollPane JavaDoc panel = new JScrollPane JavaDoc(sceneView);
118         panel.getVerticalScrollBar().setUnitIncrement(16);
119         panel.getHorizontalScrollBar().setUnitIncrement(16);
120         panel.setBorder(null);
121         add(panel, BorderLayout.CENTER);
122     }
123
124     /**
125      * Adds the graph actions to the given toolbar (no separators are
126      * added to either the beginning or end).
127      *
128      * @param toolbar to which the actions are added.
129      */

130     public void addToolbarActions(JToolBar JavaDoc toolbar) {
131         zoomer.addToolbarActions(toolbar);
132         toolbar.addSeparator();
133         Border JavaDoc border = UIManager.getBorder("nb.tabbutton.border"); //NOI18N
134
Action JavaDoc[] actions = new Action JavaDoc[] {
135             new ViewCollaborationsAction(),
136             new ViewMessagesAction(),
137         };
138         boolean[] visible = new boolean[] {
139             isCollaborationsShowing(),
140             isMessagesShowing(),
141         };
142         for (int ii = 0; ii < actions.length; ii++) {
143             Action JavaDoc action = actions[ii];
144             JToggleButton JavaDoc button = new JToggleButton JavaDoc(action);
145             // Action has a name for accessibility purposes, but we do
146
// not want that to appear in the button label.
147
button.setText(null);
148             button.setRolloverEnabled(true);
149             if (border != null) {
150                 button.setBorder(border);
151             }
152             button.setSelected(visible[ii]);
153             toolbar.add(button);
154         }
155     }
156
157     /**
158      * Return the view content, suitable for printing (i.e. without a
159      * scroll pane, which would result in the scroll bars being printed).
160      *
161      * @return the view content, sans scroll pane.
162      */

163     public JComponent JavaDoc getContent() {
164         return scene.getView();
165     }
166
167     /**
168      * Return the ZoomManager for this GraphView instance.
169      *
170      * @return the zoom manager.
171      */

172     public ZoomManager getZoomManager() {
173         return zoomer;
174     }
175
176     /**
177      * Indicates if the collaborations container widget is visible or not.
178      *
179      * @return true if collaborations is showing, false otherwise.
180      */

181     public boolean isCollaborationsShowing() {
182         return collaborationsWidget.getParentWidget() != null;
183     }
184
185     /**
186      * Indicates if the messages container widget is visible or not.
187      *
188      * @return true if messages is showing, false otherwise.
189      */

190     public boolean isMessagesShowing() {
191         return messagesWidget.getParentWidget() != null;
192     }
193
194     public void requestFocus() {
195         super.requestFocus();
196         // Ensure the graph widgets have the focus.
197
scene.getView().requestFocus();
198     }
199
200     public boolean requestFocusInWindow() {
201         super.requestFocusInWindow();
202         // Ensure the graph widgets have the focus.
203
return scene.getView().requestFocusInWindow();
204     }
205
206     /**
207      * Change the visibility of the collaborations container widget.
208      *
209      * @param visible true to make visible, false to hide.
210      */

211     public void setCollaborationsVisible(boolean visible) {
212         if (visible) {
213             assert !isCollaborationsShowing() : "collaborations already showing!";
214             // Ensure that collaborations appears before messages.
215
List JavaDoc<Widget> children = contentWidget.getChildren();
216             int index = children.indexOf(messagesWidget);
217             if (index < 0) {
218                 index = 0;
219             }
220             contentWidget.addChild(index, collaborationsWidget);
221         } else {
222             assert isCollaborationsShowing() : "collaborations already hidden!";
223             contentWidget.removeChild(collaborationsWidget);
224         }
225         scene.validate();
226     }
227
228     /**
229      * Change the visibility of the messages container widget.
230      *
231      * @param visible true to make visible, false to hide.
232      */

233     public void setMessagesVisible(boolean visible) {
234         if (visible) {
235             assert !isMessagesShowing() : "messages already showing!";
236             // Messages should come after collaborations.
237
contentWidget.addChild(messagesWidget);
238         } else {
239             assert isMessagesShowing() : "messages already hidden!";
240             contentWidget.removeChild(messagesWidget);
241         }
242         scene.validate();
243     }
244
245     /**
246      * Attempt to show the corresponding widget for the given component.
247      *
248      * @param comp model component to show.
249      */

250     public void showComponent(Component JavaDoc comp) {
251         Widget widget = scene.findWidget(comp);
252         if (widget != null) {
253             // Make the widget visible and select it (our select provider
254
// will make the widget visible within the scroll pane).
255
// Reset whatever the current selection is first.
256
scene.setSelectedObjects(Collections.emptySet());
257             scene.getSelectProvider().select(widget, widget.getLocation(), true);
258         }
259     }
260
261     /**
262      * Toggles the visibility of the collaborations widget.
263      */

264     private class ViewCollaborationsAction extends AbstractAction JavaDoc
265             implements Runnable JavaDoc {
266
267         /**
268          * Creates a new instance of ViewCollaborationsAction.
269          */

270         public ViewCollaborationsAction() {
271             String JavaDoc path = NbBundle.getMessage(ViewCollaborationsAction.class,
272                     "IMG_ViewCollaborationsAction");
273             Image JavaDoc img = Utilities.loadImage(path);
274             if (img != null) {
275                 putValue(Action.SMALL_ICON, new ImageIcon JavaDoc(img));
276             }
277             String JavaDoc name = NbBundle.getMessage(ViewCollaborationsAction.class,
278                     "ACSN_ViewCollaborationsAction");
279             putValue(Action.NAME, name); // for accessibility
280
putValue(Action.SHORT_DESCRIPTION, NbBundle.getMessage(
281                     ViewCollaborationsAction.class, "LBL_ViewCollaborationsAction"));
282         }
283
284         public void actionPerformed(ActionEvent JavaDoc e) {
285             EventQueue.invokeLater(this);
286         }
287
288         public void run() {
289             setCollaborationsVisible(!isCollaborationsShowing());
290         }
291     }
292
293     /**
294      * Toggles the visibility of the messages widget.
295      */

296     private class ViewMessagesAction extends AbstractAction JavaDoc
297             implements Runnable JavaDoc {
298
299         /**
300          * Creates a new instance of ViewMessagesAction.
301          */

302         public ViewMessagesAction() {
303             String JavaDoc path = NbBundle.getMessage(ViewMessagesAction.class,
304                     "IMG_ViewMessagesAction");
305             Image JavaDoc img = Utilities.loadImage(path);
306             if (img != null) {
307                 putValue(Action.SMALL_ICON, new ImageIcon JavaDoc(img));
308             }
309             String JavaDoc name = NbBundle.getMessage(ViewCollaborationsAction.class,
310                     "ACSN_ViewMessagesAction");
311             putValue(Action.NAME, name); // for accessibility
312
putValue(Action.SHORT_DESCRIPTION, NbBundle.getMessage(
313                     ViewMessagesAction.class, "LBL_ViewMessagesAction"));
314         }
315
316         public void actionPerformed(ActionEvent JavaDoc e) {
317             EventQueue.invokeLater(this);
318         }
319
320         public void run() {
321             setMessagesVisible(!isMessagesShowing());
322         }
323     }
324 }
325
Popular Tags