1 19 20 package org.netbeans.modules.xml.wsdl.ui.netbeans.module; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.event.ActionEvent ; 25 import java.io.IOException ; 26 import java.util.Iterator ; 27 import javax.swing.AbstractAction ; 28 import javax.swing.Action ; 29 import javax.swing.ActionMap ; 30 import javax.swing.InputMap ; 31 import javax.swing.JComponent ; 32 import javax.swing.JToolBar ; 33 import javax.swing.KeyStroke ; 34 import javax.swing.UIManager ; 35 import javax.swing.text.DefaultEditorKit ; 36 import org.netbeans.core.api.multiview.MultiViewHandler; 37 import org.netbeans.core.api.multiview.MultiViewPerspective; 38 import org.netbeans.core.api.multiview.MultiViews; 39 import org.netbeans.core.spi.multiview.CloseOperationState; 40 import org.netbeans.core.spi.multiview.MultiViewElement; 41 import org.netbeans.core.spi.multiview.MultiViewElementCallback; 42 import org.netbeans.core.spi.multiview.MultiViewFactory; 43 import org.netbeans.modules.xml.validation.ShowCookie; 44 import org.netbeans.modules.xml.validation.ValidateAction; 45 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 46 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.WSDLSettings.ViewMode; 47 import org.netbeans.modules.xml.xam.Component; 48 import org.netbeans.modules.xml.xam.dom.DocumentComponent; 49 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem; 50 import org.netbeans.modules.xml.xam.ui.category.Category; 51 import org.netbeans.modules.xml.xam.ui.category.CategoryPane; 52 import org.netbeans.modules.xml.xam.ui.category.DefaultCategoryPane; 53 import org.netbeans.modules.xml.xam.ui.multiview.ActivatedNodesMediator; 54 import org.netbeans.modules.xml.xam.ui.multiview.CookieProxyLookup; 55 import org.openide.actions.FindAction; 56 import org.openide.awt.UndoRedo; 57 import org.openide.explorer.ExplorerManager; 58 import org.openide.explorer.ExplorerUtils; 59 import org.openide.nodes.Node; 60 import org.openide.util.HelpCtx; 61 import org.openide.util.Lookup; 62 import org.openide.util.NbBundle; 63 import org.openide.util.actions.CallbackSystemAction; 64 import org.openide.util.actions.SystemAction; 65 import org.openide.util.lookup.Lookups; 66 import org.openide.windows.Mode; 67 import org.openide.windows.TopComponent; 68 import org.openide.windows.TopComponentGroup; 69 import org.openide.windows.WindowManager; 70 71 77 public class WSDLTreeViewMultiViewElement extends TopComponent 78 implements MultiViewElement, ExplorerManager.Provider { 79 private static final long serialVersionUID = -655912409997381426L; 80 private static final String ACTIVATED_NODES = "activatedNodes"; private ExplorerManager manager; 82 private WSDLDataObject mObj; 83 private CategoryPane categoryPane; 84 private transient MultiViewElementCallback multiViewObserver; 85 private transient javax.swing.JLabel errorLabel = new javax.swing.JLabel (); 86 private transient JToolBar mToolbar; 87 88 public WSDLTreeViewMultiViewElement() { 89 super(); 90 } 91 92 public WSDLTreeViewMultiViewElement(WSDLDataObject dObj) { 93 super(); 94 this.mObj = dObj; 95 initialize(); 96 } 97 98 private void initialize() { 99 manager = new ExplorerManager(); 100 CallbackSystemAction globalFindAction = 102 (CallbackSystemAction) SystemAction.get(FindAction.class); 103 Object mapKey = globalFindAction.getActionMapKey(); 104 Action action = new WSDLFindAction(); 105 ActionMap map = getActionMap(); 106 map.put(mapKey, action); 107 map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager)); 108 map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager)); 109 map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager)); 110 map.put("delete", ExplorerUtils.actionDelete(manager, false)); 111 112 InputMap keys = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 114 KeyStroke key = (KeyStroke ) globalFindAction.getValue(Action.ACCELERATOR_KEY); 115 if (key == null) { 116 key = KeyStroke.getKeyStroke("control F"); 117 } 118 keys.put(key, mapKey); 119 120 ShowCookie showCookie = new ShowCookie() { 122 123 public void show(ResultItem resultItem) { 124 Component component = resultItem.getComponents(); 125 if (categoryPane != null && component instanceof DocumentComponent) { 126 categoryPane.getCategory().showComponent(component); 127 } 128 } 129 }; 130 131 Node delegate = mObj.getNodeDelegate(); 132 ActivatedNodesMediator nodesMediator = 133 new ActivatedNodesMediator(delegate); 134 nodesMediator.setExplorerManager(this); 135 CookieProxyLookup cpl = new CookieProxyLookup(new Lookup[] { 136 Lookups.fixed(new Object [] { 137 map, 139 mObj, 143 showCookie 145 }), 146 nodesMediator.getLookup(), 147 delegate.getLookup(), 150 }, delegate); 151 associateLookup(cpl); 152 addPropertyChangeListener(ACTIVATED_NODES, nodesMediator); 153 addPropertyChangeListener(ACTIVATED_NODES, cpl); 154 155 setLayout(new BorderLayout ()); 156 157 initUI(); 158 } 159 160 public ExplorerManager getExplorerManager() { 161 return manager; 162 } 163 164 @Override 165 public int getPersistenceType() { 166 return PERSISTENCE_NEVER; 167 } 168 169 public void setMultiViewCallback(final MultiViewElementCallback callback) { 170 multiViewObserver = callback; 171 } 172 173 public CloseOperationState canCloseElement() { 174 if (!WSDLEditorSupport.isLastView(multiViewObserver.getTopComponent())) { 176 return CloseOperationState.STATE_OK; 177 } 178 return MultiViewFactory.createUnsafeCloseState( 180 "ID_TEXT_CLOSING", MultiViewFactory.NOOP_CLOSE_ACTION, 182 MultiViewFactory.NOOP_CLOSE_ACTION); 183 } 184 185 @Override 186 public UndoRedo getUndoRedo() { 187 return mObj.getWSDLEditorSupport().getUndoManager(); 188 } 189 190 @Override 191 public void componentHidden() { 192 super.componentHidden(); 193 if (categoryPane != null) { 194 Category cat = categoryPane.getCategory(); 195 if (cat != null) { 196 cat.componentHidden(); 197 } 198 } 199 } 200 201 @Override 202 public void componentClosed() { 203 super.componentClosed(); 204 } 205 206 @Override 207 public void componentOpened() { 208 super.componentOpened(); 209 } 210 211 @Override 212 public void componentActivated() { 213 super.componentActivated(); 214 ExplorerUtils.activateActions(manager, true); 215 mObj.getWSDLEditorSupport().syncModel(); 216 updateGroupVisibility(false); 217 } 218 219 @Override 220 public void componentDeactivated() { 221 ExplorerUtils.activateActions(manager, false); 222 super.componentDeactivated(); 223 updateGroupVisibility(true); 224 } 225 226 @Override 227 public void componentShowing() { 228 super.componentShowing(); 229 initUI(); 230 231 if (categoryPane != null) { 232 Category cat = categoryPane.getCategory(); 233 if (cat != null) { 234 cat.componentShown(); 235 } 236 } 237 } 238 239 @SuppressWarnings ("deprecation") 240 public void requestFocus() { 241 super.requestFocus(); 242 if (categoryPane != null) { 244 categoryPane.getComponent().requestFocus(); 245 } 246 } 247 248 @SuppressWarnings ("deprecation") 249 public boolean requestFocusInWindow() { 250 boolean retVal = super.requestFocusInWindow(); 251 if (categoryPane != null) { 253 return categoryPane.getComponent().requestFocusInWindow(); 254 } 255 return retVal; 256 } 257 258 public HelpCtx getHelpCtx() { 259 return new HelpCtx(WSDLTreeViewMultiViewDesc.class); 260 } 261 262 private static Boolean groupVisible = null; 263 264 private void updateGroupVisibility(boolean closeGroup) { 265 WindowManager wm = WindowManager.getDefault(); 266 final TopComponentGroup group = wm.findTopComponentGroup("wsdl_ui"); if (group == null) { 268 return; } 270 boolean isWSDLViewSelected = false; 272 Iterator it = wm.getModes().iterator(); 273 while (it.hasNext()) { 274 Mode mode = (Mode) it.next(); 275 TopComponent selected = mode.getSelectedTopComponent(); 276 if (selected != null) { 277 MultiViewHandler mvh = MultiViews.findMultiViewHandler(selected); 278 if (mvh != null) { 279 MultiViewPerspective mvp = mvh.getSelectedPerspective(); 280 if (mvp != null) { 281 String id = mvp.preferredID(); 282 if (WSDLTreeViewMultiViewDesc.PREFERRED_ID.equals(id)) { 283 isWSDLViewSelected = true; 284 break; 285 } 286 } 287 } 288 } 289 } 290 if (isWSDLViewSelected && !Boolean.TRUE.equals(groupVisible)) { 292 group.open(); 293 } else if (!isWSDLViewSelected && !Boolean.FALSE.equals(groupVisible)) { 294 group.close(); 295 } 296 groupVisible = isWSDLViewSelected ? Boolean.TRUE : Boolean.FALSE; 298 299 } 300 301 @Override 302 protected String preferredID() { 303 return "WSDLTreeViewMultiViewElementTC"; } 305 306 309 private void initUI() { 310 WSDLEditorSupport editor = mObj.getWSDLEditorSupport(); 311 WSDLModel wsdlModel = null; 312 String errorMessage = null; 313 try { 314 wsdlModel = editor.getModel(); 315 if (wsdlModel != null && 316 wsdlModel.getState() == WSDLModel.State.VALID) { 317 if (categoryPane == null) { 319 Lookup lookup = getLookup(); 320 categoryPane = new DefaultCategoryPane(); 321 Category tree = new WSDLTreeCategory(wsdlModel, lookup); 322 categoryPane.addCategory(tree); 323 Category columns = new WSDLColumnsCategory(wsdlModel, lookup); 324 categoryPane.addCategory(columns); 325 ViewMode mode = WSDLSettings.getDefault().getViewMode(); 327 switch (mode) { 328 case COLUMN: 329 categoryPane.setCategory(columns); 330 break; 331 case TREE: 332 categoryPane.setCategory(tree); 333 break; 334 } 335 } 336 removeAll(); 337 add(categoryPane.getComponent(), BorderLayout.CENTER); 338 return; 339 } 340 } catch (IOException ex) { 341 errorMessage = ex.getMessage(); 342 } 343 344 if (wsdlModel == null || 346 wsdlModel.getState() == WSDLModel.State.NOT_WELL_FORMED) { 347 if (errorMessage == null) { 348 errorMessage = NbBundle.getMessage( 349 WSDLTreeViewMultiViewElement.class, 350 "MSG_NotWellformedWsdl"); 351 } 352 } 353 354 removeAll(); 356 errorLabel.setText("<" + errorMessage + ">"); 357 errorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 358 errorLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 359 errorLabel.setEnabled(false); 360 Color usualWindowBkg = UIManager.getColor("window"); errorLabel.setBackground(usualWindowBkg != null ? usualWindowBkg : 362 Color.white); 363 errorLabel.setOpaque(true); 364 add(errorLabel, BorderLayout.CENTER); 365 } 366 367 public javax.swing.JComponent getToolbarRepresentation() { 368 if (mToolbar == null) { 369 try { 370 WSDLModel model = mObj.getWSDLEditorSupport().getModel(); 371 if (model != null && model.getState() == WSDLModel.State.VALID) { 372 mToolbar = new JToolBar (); 373 mToolbar.setFloatable(false); 374 if (categoryPane != null) { 375 mToolbar.addSeparator(); 376 categoryPane.populateToolbar(mToolbar); 377 } 378 mToolbar.addSeparator(); 379 mToolbar.add(new ValidateAction(model)); 380 } 381 } catch (IOException e) { 382 } 384 } 385 return mToolbar; 386 } 387 388 public javax.swing.JComponent getVisualRepresentation() { 389 return this; 390 } 391 392 397 private class WSDLFindAction extends AbstractAction { 398 399 private static final long serialVersionUID = 1L; 400 401 404 public WSDLFindAction() { 405 } 406 407 public void actionPerformed(ActionEvent event) { 408 WSDLTreeViewMultiViewElement parent = 409 WSDLTreeViewMultiViewElement.this; 410 if (parent.categoryPane != null) { 411 CategoryPane pane = parent.categoryPane; 412 pane.getSearchComponent().showComponent(); 413 } 414 } 415 } 416 } 417 | Popular Tags |