1 19 20 package org.netbeans.modules.xml.wsdl.ui.netbeans.module; 21 22 import java.awt.EventQueue ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import org.netbeans.core.api.multiview.MultiViewHandler; 27 import org.netbeans.core.api.multiview.MultiViewPerspective; 28 import org.netbeans.core.api.multiview.MultiViews; 29 import org.netbeans.modules.print.spi.PrintProvider; 30 import org.netbeans.modules.print.spi.PrintProviderCookie; 31 import org.netbeans.modules.xml.schema.model.SchemaComponent; 32 import org.netbeans.modules.xml.validation.ShowCookie; 33 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 34 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 35 import org.netbeans.modules.xml.xam.Component; 36 import org.netbeans.modules.xml.xam.dom.DocumentComponent; 37 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem; 38 import org.netbeans.modules.xml.xam.ui.cookies.GetComponentCookie; 39 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie; 40 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie.View; 41 import org.openide.loaders.DataObject; 42 import org.openide.nodes.Node; 43 import org.openide.util.Lookup; 44 import org.openide.windows.TopComponent; 45 import org.openide.windows.WindowManager; 46 47 55 public class WSDLMultiViewSupport implements ViewComponentCookie, ShowCookie, 56 PrintProviderCookie { 57 58 private WSDLDataObject dobj; 59 60 private static final Class [] DESIGNABLE_COMPONENTS = new Class [] { 61 org.netbeans.modules.xml.wsdl.model.Message.class, 62 org.netbeans.modules.xml.wsdl.model.Operation.class, 64 org.netbeans.modules.xml.wsdl.model.OperationParameter.class, 66 org.netbeans.modules.xml.wsdl.model.Part.class, 67 org.netbeans.modules.xml.wsdl.model.PortType.class, 68 org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType.class, 69 org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role.class, 70 }; 71 72 77 public WSDLMultiViewSupport(WSDLDataObject dobj) { 78 this.dobj = dobj; 79 } 80 81 public void view(final View view, final Component component, 82 final Object ... parameters) { 83 if (!EventQueue.isDispatchThread()) { 84 EventQueue.invokeLater(new Runnable () { 85 public void run() { 86 viewInSwingThread(view, component, parameters); 87 } 88 }); 89 } else { 90 viewInSwingThread(view, component, parameters); 91 } 92 } 93 94 private void viewInSwingThread(View view, Component component, 96 Object ... parameters) { 97 if (canView(view,component)) { 98 WSDLEditorSupport editor = dobj.getWSDLEditorSupport(); 99 editor.open(); 100 if (view != null) { 101 switch (view) { 102 case SOURCE: 103 WSDLMultiViewFactory.requestMultiviewActive( 104 WSDLSourceMultiviewDesc.PREFERRED_ID); 105 break; 106 case STRUCTURE: 107 WSDLMultiViewFactory.requestMultiviewActive( 108 WSDLTreeViewMultiViewDesc.PREFERRED_ID); 109 break; 110 case DESIGN: 111 WSDLMultiViewFactory.requestMultiviewActive( 112 WSDLDesignMultiViewDesc.PREFERRED_ID); 113 break; 114 } 115 } 116 TopComponent activeTC = TopComponent.getRegistry().getActivated(); 117 ShowCookie showCookie = (ShowCookie) activeTC.getLookup().lookup( 118 ShowCookie.class); 119 ResultItem resultItem = null; 120 if (parameters != null && parameters.length != 0) { 121 for (Object o : parameters) { 122 if (o instanceof ResultItem) { 123 resultItem = (ResultItem) o; 124 break; 125 } 126 } 127 } 128 if (showCookie != null) { 129 if (resultItem == null) { 130 resultItem = new ResultItem(null, null, component, null); 131 } 132 showCookie.show(resultItem); 133 } 134 } 135 } 136 137 public boolean canView(ViewComponentCookie.View view, Component component) { 139 if (view != null && component != null) { 140 switch (view) { 141 case SOURCE: 142 if (!WSDLSourceMultiviewDesc.PREFERRED_ID.equals( 143 getMultiviewActive()) || 144 !getActiveComponents().contains(component)) { 145 return true; 146 } 147 break; 148 case STRUCTURE: 149 if (!(component instanceof SchemaComponent || 150 component instanceof WSDLComponent)) { 151 return false; 152 } 153 if (WSDLTreeViewMultiViewDesc.PREFERRED_ID.equals( 154 getMultiviewActive()) && 155 getActiveComponents().contains(component)) { 156 TopComponent activeTC = TopComponent.getRegistry().getActivated(); 157 WSDLDataObject wdobj = (WSDLDataObject) activeTC.getLookup(). 158 lookup(WSDLDataObject.class); 159 return wdobj != dobj; 160 } 161 return true; 162 case CURRENT: 163 case SUPER: 164 return true; 165 case DESIGN: 166 if (WSDLDesignMultiViewDesc.PREFERRED_ID.equals( 167 getMultiviewActive())) { 168 return false; 169 } 170 boolean okay = false; 173 for (Class type : DESIGNABLE_COMPONENTS) { 174 if (type.isInstance(component)) { 175 return true; 176 } 177 } 178 break; 179 } 180 } 181 return false; 182 } 183 184 public void show(ResultItem resultItem) { 185 View view = View.STRUCTURE; 186 Component component = resultItem.getComponents(); 187 if (component == null || component.getModel() == null || 188 component.getModel().getState() == WSDLModel.State.NOT_WELL_FORMED) { 189 view(View.SOURCE,component,resultItem); 190 } else { 191 if (component instanceof DocumentComponent) { 192 UIUtilities.annotateSourceView(dobj, (DocumentComponent) component, 193 resultItem.getDescription(), false); 194 } 195 196 TopComponent tc = WindowManager.getDefault().getRegistry().getActivated(); 197 MultiViewHandler mvh = MultiViews.findMultiViewHandler(tc); 198 199 if (mvh == null) { 200 return; 201 } 202 203 MultiViewPerspective mvp = mvh.getSelectedPerspective(); 204 if (mvp.preferredID().equals(WSDLTreeViewMultiViewDesc.PREFERRED_ID)) { 205 view(View.STRUCTURE, component, resultItem); 206 } else if (mvp.preferredID().equals(WSDLSourceMultiviewDesc.PREFERRED_ID)) { 207 view(View.SOURCE, component, resultItem); 208 } else if (mvp.preferredID().equals(WSDLDesignMultiViewDesc.PREFERRED_ID)) { 209 view(View.DESIGN, component, resultItem); 210 } 211 } 212 } 213 214 public PrintProvider getPrintProvider() { 215 TopComponent component = TopComponent.getRegistry().getActivated(); 216 Lookup lookup = component.getLookup(); 217 DataObject dobj = (DataObject) lookup.lookup(DataObject.class); 218 if (dobj == this.dobj) { 219 PrintProviderCookie cookie = (PrintProviderCookie) lookup.lookup( 220 PrintProviderCookie.class); 221 if (cookie != null && cookie != this) { 224 return cookie.getPrintProvider(); 225 } 226 } 227 return null; 228 } 229 230 236 private static String getMultiviewActive() { 237 TopComponent activeTC = TopComponent.getRegistry().getActivated(); 238 MultiViewHandler handler = MultiViews.findMultiViewHandler(activeTC); 239 if (handler != null) { 240 return handler.getSelectedPerspective().preferredID(); 241 } 242 return null; 243 } 244 245 250 private static Collection <Component> getActiveComponents() { 251 TopComponent activeTC = TopComponent.getRegistry().getActivated(); 252 Collection <Component> activeComponents = Collections.emptySet(); 253 for (Node node : activeTC.getActivatedNodes()) { 254 GetComponentCookie cake = (GetComponentCookie) node. 255 getCookie(GetComponentCookie.class); 256 Component component = null; 257 if (cake != null) { 258 component = cake.getComponent(); 259 } 260 if (component == null) { 261 component = (Component) node.getLookup().lookup(Component.class); 262 } 263 if (component != null) { 264 if (activeComponents.isEmpty()) { 265 activeComponents = new HashSet <Component>(); 266 } 267 activeComponents.add(component); 268 } 269 } 270 return activeComponents; 271 } 272 } 273 | Popular Tags |