1 19 package org.netbeans.modules.web.jsf.navigation; 20 21 import org.netbeans.modules.web.jsf.navigation.graph.NavigationBridgeUtilities; 22 import java.awt.BorderLayout ; 23 import java.awt.Toolkit ; 24 import java.awt.datatransfer.Clipboard ; 25 import java.awt.datatransfer.DataFlavor ; 26 import java.awt.datatransfer.Transferable ; 27 import java.io.IOException ; 28 import java.lang.ref.WeakReference ; 29 import javax.swing.JComponent ; 30 import javax.swing.JPanel ; 31 import javax.swing.JScrollPane ; 32 import org.openide.ErrorManager; 33 import org.openide.actions.CopyAction; 34 import org.openide.actions.CutAction; 35 import org.openide.actions.DeleteAction; 36 import org.openide.actions.PasteAction; 37 import org.openide.filesystems.FileObject; 38 import org.openide.loaders.DataObject; 39 import org.openide.loaders.DataObjectNotFoundException; 40 import org.openide.text.CloneableEditorSupport; 41 import org.openide.util.HelpCtx; 42 import org.openide.util.Lookup; 43 import org.openide.util.NbBundle; 44 import org.openide.util.actions.ActionPerformer; 45 import org.openide.util.datatransfer.ClipboardEvent; 46 import org.openide.util.datatransfer.ClipboardListener; 47 import org.openide.util.datatransfer.ExClipboard; 48 import org.openide.util.datatransfer.PasteType; 49 import org.openide.util.lookup.Lookups; 50 import org.openide.util.lookup.ProxyLookup; 51 import org.openide.windows.CloneableTopComponent; 52 import org.openide.windows.TopComponent; 53 import org.netbeans.core.spi.multiview.CloseOperationState; 54 import org.netbeans.core.spi.multiview.MultiViewElement; 55 import org.netbeans.core.spi.multiview.MultiViewElementCallback; 56 import org.netbeans.core.spi.multiview.MultiViewFactory; 57 import org.netbeans.api.project.Project; 58 import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils; 59 import org.netbeans.modules.visualweb.insync.ParserAnnotation; 60 import org.netbeans.modules.visualweb.insync.markup.MarkupUnit; 61 import org.netbeans.modules.visualweb.insync.models.FacesModelSet; 62 import org.netbeans.modules.web.jsf.navigation.options.NavigationSettings; 63 import org.netbeans.api.project.FileOwnerQuery; 64 import org.netbeans.api.visual.graph.GraphPinScene; 65 import org.netbeans.modules.web.jsf.navigation.graph.NavigationGraphScene; 66 import org.openide.options.SystemOption; 67 import org.netbeans.modules.web.jsf.navigation.vwmodel.NavigationModel; 68 69 70 77 public class NavigationView extends TopComponent 78 implements MultiViewElement { 79 private NavigationModel navModel; 80 81 public static boolean newGraphView = true; 82 private GraphPinScene navigationScene; 84 private JComponent navigationGraphComponent; 85 private DataObject dataObject = null; 86 87 89 private NavigationView() { } 91 92 public NavigationView(FileObject fileObject) { 93 Project project = FileOwnerQuery.getOwner(fileObject); 94 try { 95 dataObject = DataObject.find(fileObject); 96 } catch (DataObjectNotFoundException ex) { 97 ErrorManager.getDefault().notify(ex); 98 } 99 100 101 if (project != null) { 102 FacesModelSet modelset = FacesModelSet.getInstance(project); 103 if(modelset != null) { 104 navModel = (NavigationModel) modelset.getConfigModel(fileObject); 105 if(navModel == null) { 106 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 107 new NullPointerException ("Retrieved model is null from modelSet=" + modelset)); } 109 }else{ 110 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 111 new NullPointerException ("Retrieved faces model set is null from project=" + project)); } 113 }else{ 114 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 115 new NullPointerException ("Retrieved project is null for navigation FileObject=" + fileObject)); } 117 initA11Y(); 118 } 119 120 private void initA11Y() { 121 getAccessibleContext().setAccessibleName(NbBundle.getMessage(NavigationView.class, "NavigationView")); 122 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(NavigationView.class, 123 "ACSD_NavigationView")); 124 } 125 126 127 private WeakReference lookupWRef = new WeakReference (null); 128 129 130 public Lookup getLookup() { 131 Lookup lookup = (Lookup)lookupWRef.get(); 132 133 if (lookup == null) { 134 Lookup superLookup = super.getLookup(); 135 136 DataObject dob = getDataObject(); 138 lookup = new ProxyLookup(new Lookup[] {superLookup, Lookups.fixed(new Object [] {dob})}); 139 lookupWRef = new WeakReference (lookup); 140 } 141 142 return lookup; 143 } 144 145 public void componentOpened() { 146 init(); 147 javax.swing.ActionMap am = getActionMap(); 148 149 if ( navigationScene != null ) { 150 153 155 } 157 158 160 if( showing ) { 161 162 showing = false; 163 componentShowing(); 164 } 165 166 167 } 168 169 public void componentClosed() { 170 super.componentClosed(); 171 } 172 173 public String getName() { 174 return NbBundle.getMessage(NavigationView.class, "NavigationView"); } 176 177 181 protected String iconResource() { 182 return "com/sun/rave/navigation/vwmodel/navigation.gif"; } 184 185 public HelpCtx getHelpCtx() { 186 return new HelpCtx("projrave_ui_elements_editors_about_navigation_editor"); 187 } 188 189 public void requestFocus() { 192 if ( navigationGraphComponent != null ) { 193 navigationGraphComponent.requestFocus(); 194 } else { 195 super.requestFocus(); 196 } 197 } 198 199 public boolean requestFocusInWindow() { 200 if ( navigationGraphComponent != null ) { 201 return navigationGraphComponent.requestFocusInWindow(); 202 } else { 203 return super.requestFocusInWindow(); 204 } 205 } 206 207 public void requestVisible() { 208 if (multiViewElementCallback != null) 209 multiViewElementCallback.requestVisible(); 210 else 211 super.requestVisible(); 212 } 213 214 public void requestActive() { 215 if (multiViewElementCallback != null) 216 multiViewElementCallback.requestActive(); 217 else 218 super.requestActive(); 219 } 220 221 private boolean showing = false; 222 223 public void componentShowing() { 225 if (showing) { 226 return; 227 } 228 showing = true; 229 230 try { 231 navModel.sync(); 232 } catch (Exception e) { 233 ErrorManager.getDefault().notify(e); 234 } 235 236 initGraph(true); 237 239 } 240 241 public void componentHidden() { 242 243 if (!showing) { 244 return; 247 } 248 showing = false; 249 250 252 254 MarkupUnit markupUnit = navModel.getMarkup(); 255 if ( markupUnit != null ) { 256 markupUnit.flush(); 257 } 258 } 259 260 private boolean initialized = false; 261 262 private void init() { 263 if (initialized) { 264 return; 265 } 266 initialized = true; 267 268 MarkupUnit markupUnit = navModel.getMarkup(); 269 270 assert markupUnit != null; 271 272 if (markupUnit.getState().isBusted()) { 273 System.out.println("The markup unit is invalid!"); 274 System.out.println("Errors are:"); 275 ParserAnnotation[] errors = markupUnit.getErrors(); 276 for (int i = 0; i < errors.length; i++) { 277 System.out.println("Next error = " + errors[i]); 278 return; 279 } 280 } 281 284 setLayout(new BorderLayout ()); 285 286 initGraph(false); 290 } 291 292 300 304 public void initGraph(boolean reset){ 305 if(reset){ 306 this.removeAll(); 307 } 308 navigationScene = new NavigationGraphScene(); 309 310 setDocument(reset); 311 navigationGraphComponent = navigationScene.createView(); 312 JScrollPane scrollPane = new JScrollPane (navigationGraphComponent); 313 add(scrollPane, BorderLayout.CENTER); 315 316 invalidate(); 317 validate(); 318 repaint(); 319 } 320 321 private void setDocument(boolean isListening){ 322 NavigationBridgeUtilities.getInstance().setDocument(navModel); 323 NavigationBridgeUtilities.getInstance().setGraphScene(navigationScene); 324 } 329 330 345 346 347 352 353 355 public void componentActivated() { 356 super.componentActivated(); 357 358 if( !navModel.equals(NavigationBridgeUtilities.getInstance().getNavigationModel())){ 359 setDocument(false); 360 } 361 364 if (cblistener == null) 366 cblistener = new CBListener(); 367 Clipboard c = getClipboard(); 368 if (c instanceof ExClipboard) { 369 ExClipboard clip = (ExClipboard)c; 370 clip.addClipboardListener(cblistener); 371 } 372 activateActions(); 373 } 374 375 377 public void componentDeactivated() { 378 Clipboard c = getClipboard(); 380 if (c instanceof ExClipboard) { 381 ExClipboard clip = (ExClipboard)c; 382 clip.removeClipboardListener(cblistener); 383 } 384 deactivateActions(); 385 386 391 393 } 394 395 396 private ActionPerformer copyActionPerformer; 397 398 private ActionPerformer cutActionPerformer; 399 400 private ActionPerformer deleteActionPerformer; 401 402 403 private static CopyAction copy; 404 private static CutAction cut; 405 private static DeleteAction delete; 406 private static PasteAction paste; 407 408 413 private void activateActions() { 414 415 enableCutCopyDelete(); 416 updatePasteAction(); 417 } 418 419 423 private void deactivateActions() { 424 if (copyActionPerformer != null) { 425 copy.setActionPerformer(null); 426 cut.setActionPerformer(null); 427 delete.setActionPerformer(null); 428 } 429 if (paste != null) { 430 paste.setPasteTypes(null); 431 } 432 } 433 434 438 void enableCutCopyDelete() { 439 if (copyActionPerformer == null) { 440 441 copy = (CopyAction) CopyAction.findObject(CopyAction.class, true); 442 cut = (CutAction) CutAction.findObject(CutAction.class, true); 443 delete = (DeleteAction) DeleteAction.findObject(DeleteAction.class, true); 444 } 445 446 Object perf; 448 458 perf = delete.getActionPerformer(); 459 if (perf != deleteActionPerformer) { 460 delete.setActionPerformer(deleteActionPerformer); 461 } 462 } 463 464 467 475 private class Paste extends PasteType { 476 public Transferable paste() throws IOException { 477 Clipboard clipboard = getClipboard(); 478 Transferable trans = clipboard.getContents(this); 479 if (trans != null) { 480 DataFlavor [] df = trans.getTransferDataFlavors(); 481 if (df == null) { 482 return null; 483 } 484 490 try { 491 Object [] objs = (Object [])trans.getTransferData(df[0]); 492 } catch (Exception e) { 495 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 496 } 497 } 498 return null; 499 } 501 } 502 503 508 private void updatePasteAction() { 509 boolean pasteEnabled = false; 511 if (!pasteEnabled) { 512 return; 513 } 514 515 if (paste == null) { 516 paste = (PasteAction) PasteAction.findObject(PasteAction.class, true); 517 } 518 519 Clipboard clipboard = getClipboard(); 520 Transferable trans = clipboard.getContents(this); 521 if (trans != null) { 522 DataFlavor [] df = trans.getTransferDataFlavors(); 523 int n = 0; 524 if (df != null) { 525 n = df.length; 526 } 527 for (int i = 0; i < n; i++) { 528 532 if (df[i].getMimeType().startsWith("application/x-rave-nav-dnd;")) { PasteType[] pasteTypes = new PasteType[] { new Paste() }; 535 paste.setPasteTypes(pasteTypes); 536 return; 537 } 538 } 539 } 540 paste.setPasteTypes(null); 541 } 542 543 562 590 591 private CBListener cblistener; 592 593 private class CBListener implements ClipboardListener { 594 597 public void clipboardChanged(ClipboardEvent ev) { 598 if (!ev.isConsumed()) { 599 updatePasteAction(); 600 } 601 } 602 } 603 604 607 private static Clipboard getClipboard() { 608 Clipboard c = (Clipboard )Lookup.getDefault().lookup(Clipboard .class); 609 if (c == null) { 610 c = Toolkit.getDefaultToolkit().getSystemClipboard(); 611 } 612 return c; 613 } 614 615 618 public NavigationModel getNavigationModel() { 619 return navModel; 620 } 621 622 public int getPersistenceType() { 624 return PERSISTENCE_NEVER; 625 } 626 627 private transient MultiViewElementCallback multiViewElementCallback; 629 630 public CloseOperationState canCloseElement() { 631 return MultiViewFactory.createUnsafeCloseState( 634 "ID_CLOSING_NAVIGATION", MultiViewFactory.NOOP_CLOSE_ACTION, 636 MultiViewFactory.NOOP_CLOSE_ACTION); 637 } 638 639 640 641 public interface NavigationEditorSupportAccessor { 642 public void setMultiView(CloneableTopComponent multiview); 643 } 644 645 public void setMultiViewCallback(MultiViewElementCallback callback) { 646 multiViewElementCallback = callback; 647 648 Project prj = navModel.getProject(); 651 if(prj == null) { 652 return; 653 } 654 FileObject navigationFile = JsfProjectUtils.getNavigationFile(prj); 655 if(navigationFile == null) { 656 return; 657 } 658 try { 659 DataObject dObj = DataObject.find(navigationFile); 660 CloneableEditorSupport ces = (CloneableEditorSupport)dObj.getLookup().lookup(CloneableEditorSupport.class); 661 if(ces instanceof NavigationEditorSupportAccessor) { 662 NavigationEditorSupportAccessor navigationEditorSupportAccessor = (NavigationEditorSupportAccessor)ces; 663 navigationEditorSupportAccessor.setMultiView( 666 (CloneableTopComponent)multiViewElementCallback.getTopComponent()); 667 } 668 } catch(DataObjectNotFoundException dnfe) { 669 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, dnfe); 670 } 671 } 672 673 public JComponent getToolbarRepresentation() { 674 return new JPanel (); 676 } 677 678 public JComponent getVisualRepresentation() { 679 return this; 680 } 681 683 689 693 700 704 712 731 737 756 781 protected DataObject getDataObject() { 782 return dataObject; 783 } 784 785 789 } 790 | Popular Tags |