KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > navigation > graph > NavigationBridgeUtilities


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  * NavigationGraphUtilities.java
21  *
22  * Created on April 14, 2006, 7:33 PM
23  *
24  */

25
26 package org.netbeans.modules.web.jsf.navigation.graph;
27
28 import org.netbeans.modules.web.jsf.navigation.graph.NavigationGraphScene;
29 import org.netbeans.modules.web.jsf.navigation.vwmodel.Link;
30 import org.netbeans.modules.web.jsf.navigation.vwmodel.NavigableComponent;
31 import org.netbeans.modules.web.jsf.navigation.vwmodel.Page;
32 import org.netbeans.modules.web.jsf.navigation.vwmodel.Pin;
33 import org.netbeans.modules.web.jsf.navigation.vwmodel.NavigationModel;
34 import java.awt.Image JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Set JavaDoc;
38 import org.netbeans.api.visual.graph.GraphPinScene;
39 import org.netbeans.api.visual.layout.LayoutFactory;
40 import org.netbeans.api.visual.vmd.VMDNodeWidget;
41 import org.netbeans.api.visual.vmd.VMDPinWidget;
42 import org.netbeans.api.visual.widget.ConnectionWidget;
43 import org.netbeans.api.visual.widget.LabelWidget;
44 import org.netbeans.api.visual.widget.Widget;
45 import org.openide.DialogDisplayer;
46 import org.openide.NotifyDescriptor;
47 import org.openide.cookies.OpenCookie;
48 import org.openide.util.Utilities;
49
50 /**
51  * Bridge utilities between Navigation Graph and Scene model.
52  * @author Joelle Lam
53  * @Version 1.0
54  */

55 public class NavigationBridgeUtilities {
56     
57     
58     private static final Image JavaDoc IMAGE_LIST = null;
59     private static final Image JavaDoc IMAGE_CANVAS = Utilities.loadImage("org/netbeans/modules/web/jsf/navigation/graph//resources/custom_displayable_32.png"); // NOI18N
60
private static final Image JavaDoc IMAGE_COMMAND = Utilities.loadImage("org/netbeans/modules/web/jsf/navigation/graph/resources/command_16.png"); // NOI18N
61
private static final Image JavaDoc IMAGE_ITEM = Utilities.loadImage("org/netbeans/modules/web/jsf/navigation/graph/resources/item_16.png"); // NOI18N
62
private static final Image JavaDoc GLYPH_PRE_CODE = Utilities.loadImage("org/netbeans/modules/web/jsf/navigation/graph/resources/preCodeGlyph.png"); // NOI18N
63
private static final Image JavaDoc GLYPH_POST_CODE = Utilities.loadImage("org/netbeans/modules/web/jsf/navigation/graph/resources/postCodeGlyph.png"); // NOI18N
64
private static final Image JavaDoc GLYPH_CANCEL = Utilities.loadImage("org/netbeans/modules/web/jsf/navigation/graph/resources/cancelGlyph.png"); // NOI18N
65

66     private static int nodeID = 1;
67     private static int edgeID = 1;
68     
69     
70     private static NavigationBridgeUtilities instance;
71 // private NavigationGraphDocument graphDocument;
72

73 // private NavigationGraphScene graphScene;
74
private GraphPinScene graphScene;
75     
76     private NavigationModel navModel = null;
77     
78     public static final String JavaDoc PAGE = "page";
79     public static final String JavaDoc LINK = "link";
80     public static final int BUTTON = Page.BUTTON;
81     public static final int HYPERLINK = Page.HYPERLINK;
82     public static final int IMAGE_HYPERLINK = Page.IMAGE_HYPERLINK;
83     
84     /** Don't allow to instantiate. This is a singleton class */
85     private NavigationBridgeUtilities() {
86         
87     }
88     
89     // Accessor to get the instance. Singleton pattern
90
public static NavigationBridgeUtilities getInstance(){
91         if (instance == null){
92             instance = new NavigationBridgeUtilities();
93         }
94         return instance;
95     }
96     
97     public void setDocument(NavigationModel doc){
98         navModel = doc;
99     }
100     
101     public NavigationModel getNavigationModel(){
102         return navModel;
103     }
104     
105 // public NavigationGraphDocument getGraphDocument() {
106
// return this.graphDocument;
107
// }
108
//
109
// public void setGraphDocument(NavigationGraphDocument graphDocument) {
110
// this.graphDocument = graphDocument;
111
// }
112

113 // public NavigationGraphScene getGraphScene() {
114
public GraphPinScene getGraphScene() {
115         return this.graphScene;
116     }
117     
118     private void loadPageComponents(Page page){
119         List JavaDoc<NavigableComponent> navComponents = null;
120         navComponents = page.getBeans();
121         for( NavigableComponent navComponent : navComponents ){
122             createPin(graphScene, page, navComponent);
123         }
124     }
125     
126 // public void setGraphScene(NavigationGraphScene graphScene) {
127
public void setGraphScene(GraphPinScene graphScene) {
128         this.graphScene = graphScene;
129         
130         
131         List JavaDoc<Page> pages = navModel.getPages();
132         for (Page page: pages){
133             createNode(graphScene, IMAGE_LIST, page, null, null);
134             navModel.updateBeans(page);
135             loadPageComponents(page);
136         }
137         
138         List JavaDoc<Link> links = navModel.getLinks();
139         for( Link link : links ){
140             Page fromPage = link.getFromPage();
141             Page toPage = link.getToPage();
142             
143             createEdge(graphScene, link);
144 // createEdge(graphScene, link.getFromPage().getName() + "_" + link.)
145
}
146         
147 // //Testing
148
// Link link;
149
//// Link link = navModel.addLink(pages.get(0), pages.get(1), null);
150
//// createEdge(graphScene, link);
151
// if( navComponents != null){
152
// link = navModel.addLink( pages.get(0), pages.get(1), navComponents.get(0));
153
// createEdge(graphScene,link);
154
// }
155

156
157         
158 // VMDNodeWidget widget = (VMDNodeWidget) graphScene.findWidget (menu);
159
// HashMap<String, List<Widget>> categories = new HashMap<String, List<Widget>> ();
160
// categories.put ("Elements", Arrays.asList (graphScene.findWidget ("game"), graphScene.findWidget ("options"), graphScene.findWidget ("help"), graphScene.findWidget ("exit")));
161
// categories.put ("Commands", Arrays.asList (graphScene.findWidget ("listCommand1"), graphScene.findWidget ("listCommand2")));
162
// widget.sortPins (categories);
163

164         if( graphScene instanceof NavigationGraphScene )
165         {
166             ((NavigationGraphScene)graphScene).layoutScene();
167         }
168         
169 // graphScene.getActions ().addAction (ActionFactory.createEditAction (new EditProvider() {
170
// public void edit (Widget widget) {
171
// graphScene.layoutScene ();
172
// }
173
// }));
174
}
175     
176     /* Creates a webpage in both the model and the graph */
177     public void createWebPage() {
178         Page page = navModel.createWebPage();
179 // graphScene.addNode(page);
180
graphScene.addNode(page);
181         graphScene.validate();
182     }
183     
184 // /**
185
// *Create the GraphNode from Page and return
186
// */
187
// public VMDNodeWidget addPage(Page page) {
188
//
189
// VMDNodeWidget node = (VMDNodeWidget) graphScene.findWidget(page);
190
// if (node == null){
191
// node = createNode(graphScene, IMAGE_LIST, page, "Page", null );
192
// }
193
// return node;
194
//
195
// }
196

197     /**
198      * Open the Page corresponding to the node in the web form designer
199      */

200     public void openPage(Widget node) {
201         Page page = (Page) graphScene.findObject(node);
202         OpenCookie ec = (OpenCookie)page.getDataObject().getCookie(OpenCookie.class);
203         if (ec != null) {
204             ec.open();
205         }
206         return;
207     }
208     
209     public void removeNode(Page page){
210         navModel.removePage(page);
211 // graphScene.removeNode(page);
212
// graphScene.removeNodeWithEdges(page);
213
graphScene.removeNodeWithEdges(page);
214         graphScene.validate();
215 // graphScene.validate();
216
//
217
// List<Widget> widgets = node.getChildren();
218
//
219
// for (Widget widget : widgets) {
220
// if( widget instanceof VMDConnectionWidget ){
221
// Link link = (Link)graphScene.findObject(widget);
222
// navModel.removeLink(link);
223
// }
224
// }
225

226     }
227     
228     public void removePin(Pin pin) {
229         navModel.removeNavigableComponent(pin.getNavComp());
230 // graphScene.removePin(pin);
231
graphScene.removePin(pin);
232         graphScene.validate();
233     }
234     
235     
236     
237     
238     /**
239      * Add the Link to the navigation navModel and then create Graph Link
240      */

241     public Link createLink(Page source, Page target, NavigableComponent navComp) {
242         
243         Link link = navModel.addLink(source, target, navComp);
244         //It seems the navComponent actions are not updated in create Edge
245
createEdge(graphScene, link);
246        
247         
248         return link;
249         
250     }
251     
252     /**
253      * Rename the navigation case label and graph link name as it
254      * depends on it
255      */

256 // public void renameLink(IGraphLink graphLink, String newName){
257
// NavigationGraphLink navGraphLink = (NavigationGraphLink)graphLink;
258
// Link link = navGraphLink.getLink();
259
// navGraphLink.renameId(newName);
260
// navModel.setOutcome(link, newName.trim(), true);
261
// }
262

263     /**
264      * Remove the link from the Page navigation navModel and Graph
265      */

266     public void removeLink(Link link) {
267         navModel.removeLink(link);
268         graphScene.removeEdge(link);
269 // graphScene.removeEdge(link);
270
graphScene.validate();
271     }
272     
273     
274     public boolean confirm(String JavaDoc message, String JavaDoc title){
275         NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message, title, NotifyDescriptor.YES_NO_OPTION);
276         return NotifyDescriptor.YES_OPTION.equals(DialogDisplayer.getDefault().notify(desc));
277     }
278     
279     public void addPageBean(Page page, int type) {
280         NavigableComponent navComp = null;
281         if ( page != null ) {
282             navComp = navModel.addPageBean(page, type);
283             if (navComp != null) {
284                 this.createPin(graphScene, page, navComp);
285             }
286         }
287         graphScene.validate();
288     }
289     
290     public void openPage(Page page) {
291         page.getModel().openDefaultHandler();
292     }
293     
294 // NavigationGraphNode graphNode = (NavigationGraphNode) port.getNode(); //Not sure if I can do this cast
295
// Page page = graphNode.getPage();
296
// NavigableComponent pagebean = port.getPageBean();
297
//
298
// /*NavigableComponent bean = getBeanAt(page, e.getPoint());*/
299
// if (pagebean != null) {
300
// // Open default event handler instead
301
// page.getModel().openDefaultHandler(pagebean.getBean());
302
// return;
303
// }
304
//
305
// return;
306

307     
308     
309     private VMDNodeWidget createNode(GraphPinScene graphScene, Image JavaDoc image, Page page, String JavaDoc type, List JavaDoc<Image JavaDoc> glyphs) {
310 // System.err.println("\nPage : " + page);
311
VMDNodeWidget widget = (VMDNodeWidget) graphScene.addNode(page);
312 // VMDNodeWidget widget = (VMDNodeWidget) graphScene.addNode(page);
313
// widget.setPreferredLocation(new Point(x, y));
314
widget.setNodeProperties(image, page.getName(), type, glyphs);
315 // graphScene.addPin(page, new Pin( page ));
316
graphScene.addPin(page, new Pin(page));
317         return widget;
318     }
319     
320     private VMDPinWidget createPin(GraphPinScene graphScene, Page page, NavigableComponent navComp) {
321         Pin pin = new Pin(page, navComp);
322         VMDPinWidget widget = (VMDPinWidget) graphScene.addPin(page, pin);
323 // VMDPinWidget widget = (VMDPinWidget) graphScene.addPin(page, pin);
324
if( navComp != null ){
325             widget.setProperties(navComp.getName(), Arrays.asList(navComp.getBufferedIcon()));
326         }
327         return widget;
328     }
329     
330     private void createEdge(GraphPinScene graphScene, Link link) {
331         Page fromPage = link.getFromPage();
332         Page toPage = link.getToPage();
333         NavigableComponent navComp = link.getFromNavigableComponent();
334         Set JavaDoc<Pin> pins = (Set JavaDoc) graphScene.getPins();
335         Pin sourcePin = null;
336         for ( Pin pin : pins ){
337             if ( pin.equals(new Pin( fromPage, navComp))) {
338                 sourcePin = pin;
339                 break;
340             }
341         }
342         
343         Pin targetPin = null;
344         for (Pin pin : pins ){
345             if (pin.equals(new Pin(toPage))) {
346                 targetPin = pin;
347                 break;
348             }
349         }
350         
351         ConnectionWidget widget = (ConnectionWidget)graphScene.addEdge(link);
352 // ConnectionWidget widget = (ConnectionWidget)graphScene.addEdge(link);
353
String JavaDoc navCase = link.getFromOutcome();
354         
355         LabelWidget label = new LabelWidget(graphScene, navCase);
356         label.setOpaque(true);
357         widget.addChild(label);
358         widget.setConstraint(label, LayoutFactory.ConnectionWidgetLayoutAlignment.TOP_RIGHT, 10);
359         
360         graphScene.setEdgeSource(link, targetPin);
361         graphScene.setEdgeTarget(link, targetPin);
362         
363 // graphScene.setEdgeSource(link, sourcePin);
364
// graphScene.setEdgeTarget(link, targetPin);
365

366         
367     }
368     
369     
370     
371     
372 }
373
Popular Tags