1 9 10 package org.netbeans.modules.web.jsf.navigation; 11 12 import java.awt.BorderLayout ; 13 import java.awt.Dimension ; 14 import java.awt.Image ; 15 import java.awt.Rectangle ; 16 import java.awt.event.ItemEvent ; 17 import java.awt.event.ItemListener ; 18 import java.io.IOException ; 19 import java.lang.ref.WeakReference ; 20 import java.util.Collection ; 21 import java.util.HashSet ; 22 import java.util.List ; 23 import javax.swing.Action ; 24 import javax.swing.JComboBox ; 25 import javax.swing.JComponent ; 26 import javax.swing.JScrollPane ; 27 import javax.swing.JToolBar ; 28 import javax.swing.border.EmptyBorder ; 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 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 } 67 68 69 73 public PageFlowController getPageFlowController() { 74 return pfc; 75 } 76 77 85 private WeakReference lookupWRef = new WeakReference (null); 86 87 public Lookup getLookup() { 88 Lookup lookup = (Lookup)lookupWRef.get(); 89 90 if (lookup == null) { 91 Lookup superLookup = super.getLookup(); 92 93 95 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 [] { paletteController})}); 107 } 108 109 lookupWRef = new WeakReference (lookup); 110 } 111 112 return lookup; 113 114 } 115 116 117 120 private void init(){ 121 setLayout(new BorderLayout ()); 122 123 scene = new PageFlowScene(this); 124 125 JScrollPane pane = new JScrollPane (scene.createView()); 126 pane.setVisible(true); 127 128 add(pane, BorderLayout.CENTER); 132 133 try{ 134 Node node = DataObject.find(context.getFacesConfigFile()).getNodeDelegate(); 135 setActivatedNodes(new Node[] { node }); 136 } catch (Exception e){} 137 138 } 139 140 141 142 private static final Image IMAGE_LIST = null; 145 148 public void clearGraph() { 149 151 Collection <AbstractNode> nodes = new HashSet <AbstractNode>(scene.getNodes()); 153 for( AbstractNode node : nodes ){ 154 scene.removeNodeWithEdges(node); 155 } 156 scene.validate(); 157 } 158 159 162 public void validateGraph() { 163 scene.validate(); 164 scene.layoutScene(); 165 } 166 167 168 169 177 protected VMDNodeWidget createNode( AbstractNode pageNode, String type, List <Image > glyphs) { 178 VMDNodeWidget widget = (VMDNodeWidget) scene.addNode(pageNode); 179 String 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 199 protected VMDPinWidget createPin( AbstractNode pageNode, String navComp) { 200 VMDPinWidget widget = (VMDPinWidget) scene.addPin(pageNode, navComp); 202 return widget; 207 } 208 209 214 protected void createEdge( NavigationRule rule, NavigationCase navCase) { 215 216 String toPage = navCase.getToViewId(); 217 String caseName = navCase.getFromOutcome(); 218 String action = navCase.getFromAction(); 219 String 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 230 scene.setEdgeSource(navCase, fromPage+"pin"); 232 scene.setEdgeTarget(navCase, toPage+"pin"); 233 234 258 259 } 260 261 262 private static final String PATH_TOOLBAR_FOLDER = "PageFlowEditor/Toolbars"; 264 265 269 public JComponent getToolbarRepresentation() { 270 271 PageFlowUtilities pfu = PageFlowUtilities.getInstance(); 272 JToolBar toolbar = new JToolBar (); 275 toolbar.setFloatable(false); 276 toolbar.setRollover(true); 277 toolbar.setBorder(new EmptyBorder (0, 0, 0, 0)); 278 279 281 toolbar.addSeparator(); 282 283 JComboBox comboBox = new JComboBox (); 284 comboBox.addItem(PageFlowUtilities.LBL_SCOPE_FACESCONFIG); 285 comboBox.addItem(PageFlowUtilities.LBL_SCOPE_PROJECT); 286 287 Dimension prefSize = comboBox.getPreferredSize(); 289 comboBox.setMinimumSize(prefSize); 290 comboBox.setMaximumSize(prefSize); 291 292 comboBox.setSelectedItem(pfu.getCurrentScope()); 293 294 comboBox.addItemListener( new ItemListener () { 295 public void itemStateChanged(ItemEvent event) { 296 PageFlowUtilities pfu = PageFlowUtilities.getInstance(); 297 if ( event.getStateChange() == ItemEvent.SELECTED ) { 298 pfu.setCurrentScope((String )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 PATH_PALETTE_FOLDER = "PageFlowEditor/Palette"; 314 318 public PaletteController getPaletteController() { 319 try { 320 return PaletteFactory.createPalette( PATH_PALETTE_FOLDER, new PaletteActions() { 321 public Action [] getCustomCategoryActions(Lookup lookup) { 322 return new Action [0]; 323 } 324 public Action [] getCustomItemActions(Lookup lookup) { 325 return new Action [0]; 326 } 327 public Action [] getCustomPaletteActions() { 328 return new Action [0]; 329 } 330 public Action [] getImportActions() { 331 return new Action [0]; 332 } 333 public Action getPreferredAction(Lookup lookup) { 334 return null; } 336 }); 337 } catch (IOException ex) { 338 ex.printStackTrace(); 339 340 } 341 return null; 342 } 343 344 345 346 347 348 } 349 | Popular Tags |