1 19 20 package org.netbeans.modules.xml.wsdl.ui.netbeans.module; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.List ; 31 32 import javax.swing.text.StyledDocument ; 33 34 import org.netbeans.modules.xml.schema.model.Schema; 35 import org.netbeans.modules.xml.schema.model.SchemaComponent; 36 import org.netbeans.modules.xml.schema.ui.nodes.SchemaComponentNode; 37 import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategoryNode; 38 import org.netbeans.modules.xml.wsdl.model.Definitions; 39 import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent; 40 import org.netbeans.modules.xml.wsdl.model.Types; 41 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 42 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 43 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema; 44 import org.netbeans.modules.xml.wsdl.ui.validation.ValidationAnnotation; 45 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.FolderNode; 46 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.WSDLElementNode; 47 import org.netbeans.modules.xml.xam.ModelSource; 48 import org.netbeans.modules.xml.xam.dom.DocumentComponent; 49 import org.netbeans.modules.xml.xam.ui.customizer.Customizer; 50 import org.openide.DialogDescriptor; 51 import org.openide.cookies.EditCookie; 52 import org.openide.cookies.LineCookie; 53 import org.openide.nodes.Node; 54 import org.openide.text.Line; 55 import org.openide.text.NbDocument; 56 import org.openide.util.Lookup; 57 import org.openide.util.NbBundle; 58 59 63 public class UIUtilities { 64 65 66 private UIUtilities() { 67 } 68 69 72 public static List <Node> findPathFromRoot(Node root, WSDLComponent sc) { 73 if (sc.getModel() == null) { 74 return Collections.emptyList(); 75 } 76 ArrayList <WSDLComponent> path = new ArrayList <WSDLComponent>(); 77 WSDLComponent parent = sc; 78 while (parent != null) { 79 path.add(0,parent); 80 parent = parent.getParent(); 81 } 82 if (path.get(0) != sc.getModel().getDefinitions()) { 83 return Collections.emptyList(); 84 } 85 WSDLElementNode rootSCN = (WSDLElementNode) root.getCookie( 86 WSDLElementNode.class); 87 if (rootSCN == null || !rootSCN.isSameAsMyWSDLElement(path.get(0))) { 88 return Collections.emptyList(); 89 } 90 ArrayList <Node> selectionPath = new ArrayList <Node>(); 91 selectionPath.add(root); 92 path.remove(0); 93 Node parentNode = root; 94 for (int ii = 0; ii < path.size(); ii++) { 95 WSDLComponent comp = path.get(ii); 96 List <Node> subPath = null; 97 Node[] children = parentNode.getChildren().getNodes(); 98 if (children == null || children.length < 0) { 99 return selectionPath; 100 } 101 for (Node n : children) { 102 WSDLElementNode node = (WSDLElementNode) n.getCookie( 103 WSDLElementNode.class); 104 if (node != null) { 105 WSDLComponent scomp = node.getWSDLComponent(); 106 int idx = path.indexOf(scomp); 107 if (idx >= ii) { 108 subPath = Collections.singletonList(n); 109 parentNode = n; 110 ii=idx; 111 break; 112 } 113 } 114 } 115 if (subPath == null) { 116 for (Node n : children) { 117 FolderNode cNode = (FolderNode) n.getCookie(FolderNode.class); 118 if (cNode != null && cNode.getChildType().isInstance(comp)) { 119 Node[] nChildren = n.getChildren().getNodes(); 120 if (nChildren != null && nChildren.length > 0) { 121 for (Node nChild : nChildren) { 122 WSDLElementNode node = (WSDLElementNode) nChild. 123 getCookie(WSDLElementNode.class); 124 if (node != null) { 125 WSDLComponent scomp = node.getWSDLComponent(); 126 int idx = path.indexOf(scomp); 127 if (idx >= ii) { 128 subPath = new ArrayList <Node>(); 129 subPath.add(n); 130 subPath.add(nChild); 131 parentNode = nChild; 132 ii = idx; 133 break; 134 } 135 } 136 } 137 } 138 break; 139 } 140 } 141 } 142 if (subPath == null) { 143 break; 144 } 145 selectionPath.addAll(subPath); 146 } 147 return selectionPath; 148 } 149 150 151 162 public static List <Node> findPathFromRoot(Node root, SchemaComponent sc, WSDLModel model) 163 { 164 if (sc.getModel() == null) { 165 return Collections.emptyList(); 166 } 167 168 ArrayList <SchemaComponent> path = new ArrayList <SchemaComponent>(); 169 SchemaComponent parent = sc; 170 while(parent!=null) 171 { 172 path.add(0, parent); 173 parent = parent.getParent(); 174 } 175 176 if (!(path.get(0) instanceof Schema)) return Collections.emptyList(); 178 179 Types types = model.getDefinitions().getTypes(); 180 Collection <WSDLSchema> schemas = types.getExtensibilityElements(WSDLSchema.class); 181 182 WSDLSchema wsdlSchema = null; 183 for (WSDLSchema schema : schemas) { 184 if (path.get(0) == schema.getSchemaModel().getSchema()) { 185 wsdlSchema = schema; 186 break; 187 } 188 } 189 190 if (wsdlSchema == null) return Collections.emptyList(); 192 193 List <Node> wsdlPathTillTypes = findPathFromRoot(root, types); 195 196 if (wsdlPathTillTypes == null || wsdlPathTillTypes.isEmpty()) { 197 return Collections.emptyList(); 198 } 199 200 WSDLElementNode rootNode = (WSDLElementNode) root.getCookie(WSDLElementNode.class); 201 if (rootNode == null || rootNode != wsdlPathTillTypes.get(0)) { 202 return Collections.emptyList(); 203 } 204 205 206 Node typesNode = wsdlPathTillTypes.get(wsdlPathTillTypes.size() - 1); 207 Node[] schemaNodes = typesNode.getChildren().getNodes(); 208 Node schemaRootNode = null; 209 if (schemaNodes != null) { 210 for (Node schemaNode : schemaNodes) { 211 SchemaComponentNode schemaCNode = (SchemaComponentNode) schemaNode.getCookie(SchemaComponentNode.class); 212 if (schemaCNode.getReference().get().equals(wsdlSchema.getSchemaModel().getSchema())) { 213 schemaRootNode = schemaNode; break; 215 } 216 } 217 } 218 219 ArrayList <Node> selectionPath = new ArrayList <Node>(); 220 selectionPath.add(schemaRootNode); 221 Class <? extends SchemaComponent> categoryType = null; 222 if(path.size() > 1) categoryType = path.get(1).getComponentType(); 223 Node parentNode = schemaRootNode; 224 for(int i = 0; i < path.size(); i++) 225 { 226 SchemaComponent comp = path.get(i); 227 List <Node> subPath = null; 228 Node[] children = parentNode.getChildren().getNodes(); 229 if (children == null || children.length < 0) 230 { 231 return selectionPath; 232 } 233 if (subPath==null) 234 { 235 for (Node n : children) 236 { 237 CategoryNode cNode = (CategoryNode) n.getCookie(CategoryNode.class); 238 if (cNode != null && cNode.getChildType()==categoryType) 239 { 240 Node[] nChildren = n.getChildren().getNodes(); 241 if (nChildren != null && nChildren.length > 0) 242 { 243 for (Node nChild : nChildren) 244 { 245 SchemaComponentNode node = (SchemaComponentNode) nChild. 246 getCookie(SchemaComponentNode.class); 247 if (node != null) 248 { 249 SchemaComponent scomp = node.getReference().get(); 250 int idx = path.indexOf(scomp); 251 if (idx>=i) 252 { 253 subPath = new ArrayList <Node>(); 254 subPath.add(n); 255 subPath.add(nChild); 256 parentNode = nChild; 257 i=idx; 258 if(i!=0&&i<path.size()) categoryType = 259 path.get(i).getComponentType(); 260 break; 261 } 262 } 263 } 264 } 265 break; 266 } 267 } 268 } 269 if (subPath==null) 270 { 271 for (Node n : children) 272 { 273 SchemaComponentNode node = (SchemaComponentNode) n.getCookie( 274 SchemaComponentNode.class); 275 if (node != null) 276 { 277 SchemaComponent scomp = node.getReference().get(); 278 int idx = path.indexOf(scomp); 279 if (idx>=i) 280 { 281 subPath = Collections.singletonList(n); 282 parentNode = n; 283 i=idx; 284 if(i!=0&&i<path.size()) categoryType = 285 path.get(i).getComponentType(); 286 break; 287 } 288 } 289 } 290 } 291 if (subPath==null) break; 292 selectionPath.addAll(subPath); 293 } 294 wsdlPathTillTypes.addAll(selectionPath); 295 return wsdlPathTillTypes; 296 } 297 298 302 public static Node findNode(Node parent, ReferenceableWSDLComponent sc, WSDLModel currentModel) { 303 if (parent == null) return null; 304 Node[] children = parent.getChildren().getNodes(); 305 if(children == null || children.length<0) return null; 306 boolean externalRef = sc.getModel()!=currentModel; 309 for (Node n:children) { 310 WSDLElementNode node =(WSDLElementNode)n. 311 getCookie(WSDLElementNode.class); 312 WSDLComponent scomp = node==null?null:node.getWSDLComponent(); 313 if (scomp == sc) { 314 return n; 315 } 316 if(externalRef) { 324 if(scomp instanceof Definitions && scomp.getModel() == sc.getModel()) { 325 return findNode(n,sc,scomp.getModel()); 326 } 327 } 333 } 348 return null; 349 } 350 351 private static WSDLElementNode findWSDLComponentNode(Node node) { 352 WSDLElementNode scn =(WSDLElementNode)node 353 .getCookie(WSDLElementNode.class); 354 if(scn!=null) return scn; 355 Node parent = node; 356 while((parent=parent.getParentNode())!=null) { 357 scn = (WSDLElementNode)parent 358 .getCookie(WSDLElementNode.class); 359 if(scn!=null) return scn; 360 } 361 return null; 362 } 363 364 public static <T extends ReferenceableWSDLComponent> T findReferenceable(Node node, 365 Class <T> type) { 366 WSDLElementNode scn =findWSDLComponentNode(node); 367 if(scn==null) return null; 368 WSDLComponent component = 369 scn.getWSDLComponent(); 370 if(type.isInstance(component)) 371 return type.cast(component); 372 while((component=component.getParent())!=null) 373 if(type.isInstance(component)) 374 return type.cast(component); 375 return null; 376 } 377 378 385 public static DialogDescriptor getCreatorDialog( 386 Customizer customizer, String title, boolean editable) { 387 title = NbBundle.getMessage(UIUtilities.class, 388 "TITLE_WSDLElementNode_Creator", title); 389 return getCustomizerDialog0(customizer, title, editable); 390 } 391 392 399 public static DialogDescriptor getCustomizerDialog( 400 Customizer customizer, String title, boolean editable) { 401 title = NbBundle.getMessage(UIUtilities.class, 402 "TITLE_WSDLElementNode_Customizer", title); 403 return getCustomizerDialog0(customizer, title, editable); 404 } 405 406 private static DialogDescriptor getCustomizerDialog0( 407 final Customizer customizer, final String title, final boolean editable) { 408 java.awt.Component component = customizer.getComponent(); 409 final DialogDescriptor descriptor = new DialogDescriptor(component, 410 title, true, null); 411 descriptor.setHelpCtx(customizer.getHelpCtx()); 412 if(editable) { 413 final PropertyChangeListener pcl = new PropertyChangeListener () { 415 public void propertyChange(PropertyChangeEvent evt) { 416 if(evt.getSource()==customizer && evt.getPropertyName(). 417 equals(Customizer.PROP_ACTION_APPLY)) { 418 descriptor.setValid(((Boolean ) evt.getNewValue()).booleanValue()); 419 } 420 } 421 }; 422 customizer.addPropertyChangeListener(pcl); 423 ActionListener al = new ActionListener () { 425 public void actionPerformed(ActionEvent evt) { 426 if (evt.getSource().equals(DialogDescriptor.OK_OPTION) || 427 evt.getSource().equals(DialogDescriptor.CANCEL_OPTION) || 428 evt.getSource().equals(DialogDescriptor.CLOSED_OPTION)) { 429 customizer.removePropertyChangeListener(pcl); 430 } 431 if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) { 432 try { 433 customizer.apply(); 434 } catch (IOException ioe) { 435 } 436 } 437 } 438 }; 439 descriptor.setButtonListener(al); 440 } 441 return descriptor; 442 } 443 444 452 public static void annotateSourceView(WSDLDataObject dobj, DocumentComponent wsdlComp, String errorMessage, boolean shouldShowSource) { 453 LineCookie lc = (LineCookie) dobj.getCookie(LineCookie.class); 454 EditCookie ec = (EditCookie) dobj.getCookie(EditCookie.class); 455 if (lc == null || ec == null) { 456 return; 457 } 458 ec.edit(); 459 ValidationAnnotation.clearAll(); 460 int lineNum = getLineNumber(wsdlComp); 461 if (lineNum < 1) { 462 return; 463 } 464 465 Line l = lc.getLineSet().getCurrent(lineNum); 466 if (errorMessage != null) { 467 ValidationAnnotation annotation = ValidationAnnotation.getNewInstance(); 468 annotation.setErrorMessage(errorMessage); 469 annotation.attach( l ); 470 l.addPropertyChangeListener( annotation ); 471 } 472 473 if (shouldShowSource) { 474 l.show(Line.SHOW_GOTO); 475 } 476 } 477 478 public static int getLineNumber(DocumentComponent comp) { 479 int position = comp.findPosition(); 480 ModelSource modelSource = comp.getModel().getModelSource(); 481 assert modelSource != null; 482 Lookup lookup = modelSource.getLookup(); 483 484 StyledDocument document = (StyledDocument )lookup.lookup(StyledDocument .class); 485 if (document == null) { 486 return -1; 487 } 488 return NbDocument.findLineNumber(document,position); 489 } 490 } 491 | Popular Tags |