KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > navigation > PageFlowView


1 /*
2  * JSFTopComponent.java
3  *
4  * Created on February 8, 2007, 12:40 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.web.jsf.navigation;
11
12 import java.awt.BorderLayout JavaDoc;
13 import java.awt.Dimension JavaDoc;
14 import java.awt.Image JavaDoc;
15 import java.awt.Rectangle JavaDoc;
16 import java.awt.event.ItemEvent JavaDoc;
17 import java.awt.event.ItemListener JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.lang.ref.WeakReference JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.List JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.JComboBox JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JScrollPane JavaDoc;
27 import javax.swing.JToolBar JavaDoc;
28 import javax.swing.border.EmptyBorder JavaDoc;
29 import org.netbeans.api.visual.layout.LayoutFactory;
30 import org.netbeans.api.visual.vmd.VMDNodeWidget;
31 import org.netbeans.api.visual.vmd.VMDPinWidget;
32 import org.netbeans.api.visual.widget.ConnectionWidget;
33 import org.netbeans.api.visual.widget.LabelWidget;
34 import org.netbeans.modules.web.jsf.api.editor.JSFConfigEditorContext;
35 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModel;
36 import org.netbeans.modules.web.jsf.api.facesmodel.NavigationCase;
37 import org.netbeans.modules.web.jsf.api.facesmodel.NavigationRule;
38 import org.netbeans.modules.web.jsf.navigation.graph.PageFlowScene;
39 import org.netbeans.spi.palette.PaletteActions;
40 import org.netbeans.spi.palette.PaletteController;
41 import org.netbeans.spi.palette.PaletteFactory;
42 import org.openide.loaders.DataNode;
43 import org.openide.loaders.DataObject;
44 import org.openide.nodes.AbstractNode;
45 import org.openide.nodes.Node;
46 import org.openide.util.Lookup;
47 import org.openide.util.lookup.Lookups;
48 import org.openide.util.lookup.ProxyLookup;
49 import org.openide.windows.TopComponent;
50
51 /**
52  *
53  * @author Joelle Lam
54  */

55 public class PageFlowView extends TopComponent implements Lookup.Provider {
56     private JSFConfigEditorContext context;
57     private PageFlowScene scene;
58     private JSFConfigModel configModel;
59     private PageFlowController pfc;
60     
61     PageFlowView(JSFConfigEditorContext context){
62         init();
63         pfc = new PageFlowController( context, this );
64         
65         // this(context, new InstanceContent());
66
}
67     
68     
69     /**
70      *
71      * @return PageFlowController
72      */

73     public PageFlowController getPageFlowController() {
74         return pfc;
75     }
76     
77     // PageFlowView(JSFConfigEditorContext context, InstanceContent ic ){
78
// super( new AbstractLookup( ic ) );
79
// ic.add( initializePalette() );
80
// this.context = context;
81
//
82
//
83
// }
84
/** Weak reference to the lookup. */
85     private WeakReference JavaDoc lookupWRef = new WeakReference JavaDoc(null);
86     
87     public Lookup getLookup() {
88         Lookup lookup = (Lookup)lookupWRef.get();
89         
90         if (lookup == null) {
91             Lookup superLookup = super.getLookup();
92             
93             // XXX Needed in order to close the component automatically by project close.
94
/* This is currently done at the MultiViewElement level all though we can easily add it here */
95             // DataObject jspDataObject = webform.getJspDataObject();
96
// DataObject jspDataObject = null;
97
// try {
98
// jspDataObject = DataObject.find(context.getFacesConfigFile());
99
// } catch ( DataObjectNotFoundException donfe) {
100
// donfe.printStackTrace();
101
// }
102
PaletteController paletteController = getPaletteController();
103             if (paletteController == null) {
104                 lookup = new ProxyLookup(new Lookup[] {superLookup});
105             } else {
106                 lookup = new ProxyLookup(new Lookup[] {superLookup, Lookups.fixed(new Object JavaDoc[] { paletteController})});
107             }
108             
109             lookupWRef = new WeakReference JavaDoc(lookup);
110         }
111         
112         return lookup;
113         
114     }
115     
116     
117     /*
118      * Initializes the Panel and the graph
119      **/

120     private void init(){
121         setLayout(new BorderLayout JavaDoc());
122         
123         scene = new PageFlowScene(this);
124         
125         JScrollPane JavaDoc pane = new JScrollPane JavaDoc(scene.createView());
126         pane.setVisible(true);
127         
128 // Dimension viewDim = pane.getViewportBorderBounds().getSize();
129
// scene.setPreferredBounds(pane.getViewportBorderBounds());
130
// scene.setPreferredSize(pane.getViewportBorderBounds().getSize());
131
add(pane, BorderLayout.CENTER);
132         
133         try{
134             Node node = DataObject.find(context.getFacesConfigFile()).getNodeDelegate();
135             setActivatedNodes(new Node[] { node });
136         } catch (Exception JavaDoc e){}
137         
138     }
139     
140
141     
142     // private static final Image IMAGE_LIST = Utilities.loadImage("org/netbeans/modules/web/jsf/navigation/graph/resources/list_32.png"); // NOI18N
143
private static final Image JavaDoc IMAGE_LIST = null; // NOI18N
144

145     /**
146      *
147      */

148     public void clearGraph() {
149 // scene.removeChildren();
150

151         //Temporarily Wrapping Collection because of http://www.netbeans.org/issues/show_bug.cgi?id=97496
152
Collection JavaDoc<AbstractNode> nodes = new HashSet JavaDoc<AbstractNode>(scene.getNodes());
153         for( AbstractNode node : nodes ){
154             scene.removeNodeWithEdges(node);
155         }
156         scene.validate();
157     }
158     
159     /**
160      *
161      */

162     public void validateGraph() {
163         scene.validate();
164         scene.layoutScene();
165     }
166     
167  
168     
169     /**
170      * Creates a PageFlowScene node from a pageNode. The PageNode will generally be some type of DataObject unless
171      * there is no true file to represent it. In that case a abstractNode should be passed
172      * @param pageNode the node that represents a dataobject or empty object
173      * @param type
174      * @param glyphs
175      * @return
176      */

177     protected VMDNodeWidget createNode( AbstractNode pageNode, String JavaDoc type, List JavaDoc<Image JavaDoc> glyphs) {
178         VMDNodeWidget widget = (VMDNodeWidget) scene.addNode(pageNode);
179         String JavaDoc pageName = pageNode.getName();
180         if( pageNode instanceof DataNode ){
181             pageName = ((DataNode)pageNode).getDataObject().getPrimaryFile().getNameExt();
182         }
183         
184         widget.setNodeProperties(IMAGE_LIST, pageName, type, glyphs);
185         System.out.println("Page Node: " + pageName);
186         
187         scene.addPin(pageNode, pageName +"pin");
188         
189         return widget;
190     }
191     
192     /**
193      * Creates a PageFlowScene pin from a pageNode and pin name String.
194      * In general a pin represents a NavigasbleComponent orginally designed for VWP.
195      * @param pageNode
196      * @param navComp
197      * @return
198      */

199     protected VMDPinWidget createPin( AbstractNode pageNode, String JavaDoc navComp) {
200         // Pin pin = new Pin(page, navComp);
201
VMDPinWidget widget = (VMDPinWidget) scene.addPin(pageNode, navComp);
202         // VMDPinWidget widget = (VMDPinWidget) graphScene.addPin(page, pin);
203
// if( navComp != null ){
204
// widget.setProperties(navComp, Arrays.asList(navComp.getBufferedIcon()));
205
// }
206
return widget;
207     }
208     
209     /**
210      * Creates an Edge or Connection in the Graph Scene
211      * @param rule
212      * @param navCase
213      */

214     protected void createEdge( NavigationRule rule, NavigationCase navCase) {
215         
216         String JavaDoc toPage = navCase.getToViewId();
217         String JavaDoc caseName = navCase.getFromOutcome();
218         String JavaDoc action = navCase.getFromAction();
219         String JavaDoc fromPage = rule.getFromViewId();
220         
221         ConnectionWidget widget = (ConnectionWidget)scene.addEdge(navCase);
222         
223         LabelWidget label = new LabelWidget(scene, caseName);
224         label.setOpaque(true);
225         widget.addChild(label);
226         widget.setConstraint(label, LayoutFactory.ConnectionWidgetLayoutAlignment.TOP_RIGHT, 10);
227         
228         // graphScene.setEdgeSource(navCase, label);
229

230         //I need to remove extension so it matches the DataNode's pins.
231
scene.setEdgeSource(navCase, fromPage+"pin");
232         scene.setEdgeTarget(navCase, toPage+"pin");
233         
234         // Collection<String> pins = graphScene.getPins();
235
// String targetPin = null;
236
// String sourcePin = null;
237
// for (String pin : pins ){
238
// if (pin.equals(toPage)) {
239
// sourcePin = pin;
240
// if( targetPin != null ) {
241
// break;
242
// } else {
243
// continue;
244
// }
245
// } else if (pin.equals(fromPage)) {
246
// targetPin = fromPage;
247
// if( sourcePin != null ) {
248
// break;
249
// } else {
250
// continue;
251
// }
252
// }
253
// }
254
//
255
// graphScene.setEdgeTarget(navCase, targetPin);
256
// graphScene.setEdgeSource(navCase, sourcePin);
257

258         
259     }
260     
261
262     private static final String JavaDoc PATH_TOOLBAR_FOLDER = "PageFlowEditor/Toolbars"; // NOI18N
263

264     
265     /**
266      *
267      * @return
268      */

269     public JComponent JavaDoc getToolbarRepresentation() {
270         
271         PageFlowUtilities pfu = PageFlowUtilities.getInstance();
272         // TODO -- Look at NbEditorToolBar in the editor - it does stuff
273
// with the UI to get better Aqua and Linux toolbar
274
JToolBar JavaDoc toolbar = new JToolBar JavaDoc();
275         toolbar.setFloatable(false);
276         toolbar.setRollover(true);
277         toolbar.setBorder(new EmptyBorder JavaDoc(0, 0, 0, 0));
278         
279         // ToolbarListener listener = new ToolbarListener();
280

281         toolbar.addSeparator();
282         
283         JComboBox JavaDoc comboBox = new JComboBox JavaDoc();
284         comboBox.addItem(PageFlowUtilities.LBL_SCOPE_FACESCONFIG);
285         comboBox.addItem(PageFlowUtilities.LBL_SCOPE_PROJECT);
286         
287         //Set the appropriate size of the combo box so it doesn't take up the whole page.
288
Dimension JavaDoc prefSize = comboBox.getPreferredSize();
289         comboBox.setMinimumSize(prefSize);
290         comboBox.setMaximumSize(prefSize);
291         
292         comboBox.setSelectedItem(pfu.getCurrentScope());
293         
294         comboBox.addItemListener( new ItemListener JavaDoc() {
295             public void itemStateChanged(ItemEvent JavaDoc event) {
296                 PageFlowUtilities pfu = PageFlowUtilities.getInstance();
297                 if ( event.getStateChange() == ItemEvent.SELECTED ) {
298                     pfu.setCurrentScope((String JavaDoc)event.getItem());
299                     pfc.setupGraph();
300                 }
301             }
302         });
303         
304         toolbar.add(comboBox);
305         
306         return toolbar;
307         
308     }
309     
310     
311     
312     private static final String JavaDoc PATH_PALETTE_FOLDER = "PageFlowEditor/Palette"; // NOI18N
313

314     /**
315      * Get's the Palette Controller for the related Palette.
316      * @return the Palette Controller.
317      */

318     public PaletteController getPaletteController() {
319         try {
320             return PaletteFactory.createPalette( PATH_PALETTE_FOLDER, new PaletteActions() {
321                 public Action JavaDoc[] getCustomCategoryActions(Lookup lookup) {
322                     return new Action JavaDoc[0];
323                 }
324                 public Action JavaDoc[] getCustomItemActions(Lookup lookup) {
325                     return new Action JavaDoc[0];
326                 }
327                 public Action JavaDoc[] getCustomPaletteActions() {
328                     return new Action JavaDoc[0];
329                 }
330                 public Action JavaDoc[] getImportActions() {
331                     return new Action JavaDoc[0];
332                 }
333                 public Action JavaDoc getPreferredAction(Lookup lookup) {
334                     return null; //TODO
335
}
336             });
337         } catch (IOException JavaDoc ex) {
338             ex.printStackTrace();
339             
340         }
341         return null;
342     }
343     
344     
345     
346     
347     
348 }
349
Popular Tags