1 19 20 28 29 package org.netbeans.modules.xml.wsdl.ui.netbeans.module; 30 31 import java.awt.BorderLayout ; 32 import java.awt.Color ; 33 import java.io.IOException ; 34 import java.io.ObjectInput ; 35 import java.io.ObjectOutput ; 36 import java.util.Date ; 37 import javax.swing.Action ; 38 import javax.swing.ActionMap ; 39 import javax.swing.InputMap ; 40 import javax.swing.JComponent ; 41 import javax.swing.JToolBar ; 42 import javax.swing.KeyStroke ; 43 import javax.swing.UIManager ; 44 import javax.swing.text.DefaultEditorKit ; 45 import org.netbeans.core.spi.multiview.CloseOperationState; 46 import org.netbeans.core.spi.multiview.MultiViewElement; 47 import org.netbeans.core.spi.multiview.MultiViewElementCallback; 48 import org.netbeans.core.spi.multiview.MultiViewFactory; 49 import org.netbeans.modules.print.spi.PrintProvider; 50 import org.netbeans.modules.print.spi.PrintProviderCookie; 51 import org.netbeans.modules.xml.validation.ShowCookie; 52 import org.netbeans.modules.xml.validation.ValidateAction; 53 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 54 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.GraphView; 55 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.palette.WSDLPaletteFactory; 56 import org.netbeans.modules.xml.xam.Component; 57 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem; 58 import org.netbeans.modules.xml.xam.ui.multiview.ActivatedNodesMediator; 59 import org.netbeans.modules.xml.xam.ui.multiview.CookieProxyLookup; 60 import org.openide.actions.SaveAction; 61 import org.openide.awt.UndoRedo; 62 import org.openide.explorer.ExplorerManager; 63 import org.openide.explorer.ExplorerUtils; 64 import org.openide.nodes.Node; 65 import org.openide.util.HelpCtx; 66 import org.openide.util.Lookup; 67 import org.openide.util.NbBundle; 68 import org.openide.util.actions.SystemAction; 69 import org.openide.util.lookup.Lookups; 70 import org.openide.windows.TopComponent; 71 72 78 public class WSDLDesignMultiViewElement extends TopComponent 79 implements MultiViewElement, ExplorerManager.Provider { 80 private static final long serialVersionUID = -655912409997381426L; 81 private static final String ACTIVATED_NODES = "activatedNodes"; 83 private WSDLDataObject wsdlDataObject; 84 85 private ExplorerManager explorerManager; 86 87 private GraphView graphComponent; 88 private transient MultiViewElementCallback multiViewObserver; 89 private transient javax.swing.JLabel errorLabel = new javax.swing.JLabel (); 90 private transient JToolBar mToolbar; 91 92 public WSDLDesignMultiViewElement() { 93 super(); 94 } 95 96 public WSDLDesignMultiViewElement(WSDLDataObject dObj) { 97 wsdlDataObject = dObj; 98 initialize(); 99 } 100 101 private void initialize() { 102 setFocusable(true); 104 explorerManager = new ExplorerManager(); 109 ActionMap map = getActionMap(); 110 map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(explorerManager)); 111 map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(explorerManager)); 112 map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(explorerManager)); 113 map.put("delete", ExplorerUtils.actionDelete(explorerManager, false)); 114 115 SaveAction saveAction = (SaveAction) SystemAction.get(SaveAction.class); 117 map.put("save", saveAction); 118 InputMap keys = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 119 KeyStroke key = (KeyStroke ) saveAction.getValue(Action.ACCELERATOR_KEY); 120 if (key == null) { 121 key = KeyStroke.getKeyStroke("control S"); 122 } 123 keys.put(key, "save"); 124 125 ShowCookie showCookie = new ShowCookie() { 127 public void showComponent(Component component, 128 String errorMessage, String recommmendedCorrection) { 129 assert false:"ShowCookie.showComponent method is deprecated"; 130 } 131 132 public void show(ResultItem resultItem) { 133 Component component = resultItem.getComponents(); 134 graphComponent.showComponent(component); 135 } 136 }; 137 138 Node delegate = wsdlDataObject.getNodeDelegate(); 139 ActivatedNodesMediator nodesMediator = 140 new ActivatedNodesMediator(delegate); 141 nodesMediator.setExplorerManager(this); 142 CookieProxyLookup cpl = new CookieProxyLookup(new Lookup[] { 143 Lookups.fixed(new Object [] { 144 wsdlDataObject, 148 showCookie, 150 new DesignViewPrintProvider(), 152 WSDLPaletteFactory.getPalette(), 154 this, 156 }), 157 ExplorerUtils.createLookup(explorerManager, map), 159 nodesMediator.getLookup(), 160 delegate.getLookup(), 163 }, delegate); 164 associateLookup(cpl); 165 addPropertyChangeListener(ACTIVATED_NODES, nodesMediator); 166 addPropertyChangeListener(ACTIVATED_NODES, cpl); 167 setLayout(new BorderLayout ()); 168 initUI(); 169 } 170 171 public ExplorerManager getExplorerManager() { 172 return explorerManager; 173 } 174 175 @Override 176 public int getPersistenceType() { 177 return PERSISTENCE_ALWAYS; 179 } 180 181 public void setMultiViewCallback(final MultiViewElementCallback callback) { 182 multiViewObserver = callback; 183 } 184 185 public CloseOperationState canCloseElement() { 186 if (!WSDLEditorSupport.isLastView(multiViewObserver.getTopComponent())) { 188 return CloseOperationState.STATE_OK; 189 } 190 return MultiViewFactory.createUnsafeCloseState( 192 "ID_TEXT_CLOSING", MultiViewFactory.NOOP_CLOSE_ACTION, 194 MultiViewFactory.NOOP_CLOSE_ACTION); 195 } 196 197 @Override 198 public UndoRedo getUndoRedo() { 199 return wsdlDataObject.getWSDLEditorSupport().getUndoManager(); 200 } 201 202 203 @Override 204 public void componentHidden() { 205 super.componentHidden(); 206 } 207 208 @Override 209 public void componentClosed() { 210 super.componentClosed(); 211 } 212 213 @Override 214 public void componentOpened() { 215 super.componentOpened(); 216 } 217 218 @Override 219 public void componentActivated() { 220 super.componentActivated(); 221 ExplorerUtils.activateActions(explorerManager, true); 222 wsdlDataObject.getWSDLEditorSupport().syncModel(); 223 } 224 225 @Override 226 public void componentDeactivated() { 227 ExplorerUtils.activateActions(explorerManager, false); 228 super.componentDeactivated(); 229 } 230 231 @Override 232 public void componentShowing() { 233 super.componentShowing(); 234 initUI(); 235 } 236 237 @Override 238 protected String preferredID() { 239 return "WSDLDesignMultiViewElementTC"; } 241 242 245 private void initUI() { 246 WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport(); 247 WSDLModel wsdlModel = null; 248 String errorMessage = null; 249 try { 250 wsdlModel = editor.getModel(); 251 if (wsdlModel != null && 252 wsdlModel.getState() == WSDLModel.State.VALID) { 253 if (graphComponent == null) { 255 graphComponent = new GraphView(wsdlModel); 256 } 257 removeAll(); 258 add(graphComponent, BorderLayout.CENTER); 259 return; 260 } 261 } catch (IOException ex) { 262 errorMessage = ex.getMessage(); 263 } 264 265 if (wsdlModel == null || 267 wsdlModel.getState() == WSDLModel.State.NOT_WELL_FORMED) { 268 if (errorMessage == null) { 269 errorMessage = NbBundle.getMessage( 270 WSDLTreeViewMultiViewElement.class, 271 "MSG_NotWellformedWsdl"); 272 } 273 } 274 275 removeAll(); 277 errorLabel.setText("<" + errorMessage + ">"); 278 errorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 279 errorLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 280 errorLabel.setEnabled(false); 281 Color usualWindowBkg = UIManager.getColor("window"); errorLabel.setBackground(usualWindowBkg != null ? usualWindowBkg : 283 Color.white); 284 errorLabel.setOpaque(true); 285 add(errorLabel, BorderLayout.CENTER); 286 } 287 288 public javax.swing.JComponent getToolbarRepresentation() { 289 if (mToolbar == null) { 290 try { 291 WSDLModel model = wsdlDataObject.getWSDLEditorSupport().getModel(); 292 if (model != null && model.getState() == WSDLModel.State.VALID) { 293 mToolbar = new JToolBar (); 294 mToolbar.setFloatable(false); 295 mToolbar.addSeparator(); 296 graphComponent.addToolbarActions(mToolbar); 297 mToolbar.addSeparator(); 298 mToolbar.add(new ValidateAction(model)); 299 } 300 } catch (IOException e) { 301 } 303 } 304 return mToolbar; 305 } 306 307 public javax.swing.JComponent getVisualRepresentation() { 308 return this; 309 } 310 311 @SuppressWarnings ("deprecation") 312 public void requestFocus() { 313 super.requestFocus(); 314 if (graphComponent != null) { 317 graphComponent.requestFocus(); 318 } 319 } 320 321 @SuppressWarnings ("deprecation") 322 public boolean requestFocusInWindow() { 323 boolean retVal = super.requestFocusInWindow(); 324 if (graphComponent != null) { 327 return graphComponent.requestFocusInWindow(); 328 } 329 return retVal; 330 } 331 332 public HelpCtx getHelpCtx() { 333 return new HelpCtx(WSDLDesignMultiViewDesc.class); 334 } 335 336 public void readExternal(ObjectInput in) throws 337 IOException , ClassNotFoundException { 338 super.readExternal(in); 339 Object obj = in.readObject(); 340 if (obj instanceof WSDLDataObject) { 341 wsdlDataObject = (WSDLDataObject) obj; 342 initialize(); 343 } 344 try { 345 if (!in.readBoolean()) { 348 graphComponent.setCollaborationsVisible(false); 349 } 350 if (!in.readBoolean()) { 351 graphComponent.setMessagesVisible(false); 352 } 353 } catch (IOException ioe) { 354 } 356 } 357 358 public void writeExternal(ObjectOutput out) throws IOException { 359 super.writeExternal(out); 360 out.writeObject(wsdlDataObject); 361 out.writeBoolean(graphComponent.isCollaborationsShowing()); 363 out.writeBoolean(graphComponent.isMessagesShowing()); 364 } 365 366 370 private class DesignViewPrintProvider implements PrintProviderCookie { 371 372 public PrintProvider getPrintProvider() { 373 return new PrintProvider.Component() { 374 public String getName() { 375 return wsdlDataObject.getName(); 376 } 377 378 public Date getLastModifiedDate() { 379 return wsdlDataObject.getPrimaryFile().lastModified(); 380 } 381 382 public JComponent getComponent() { 383 return graphComponent.getContent(); 384 } 385 }; 386 } 387 } 388 } 389 | Popular Tags |