1 19 20 package org.netbeans.modules.xml.refactoring.ui.util; 21 22 import java.awt.Color ; 23 import java.awt.Image ; 24 import java.beans.BeanInfo ; 25 import java.io.IOException ; 26 import java.util.Enumeration ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import javax.swing.tree.TreeModel ; 30 import org.netbeans.modules.refactoring.spi.ui.UI; 31 import org.netbeans.modules.xml.nbprefuse.AnalysisConstants; 32 import org.netbeans.modules.xml.refactoring.CannotRefactorException; 33 import org.netbeans.modules.xml.refactoring.DeleteRequest; 34 import org.netbeans.modules.xml.refactoring.FileRenameRequest; 35 import org.netbeans.modules.xml.refactoring.RefactorRequest; 36 import org.netbeans.modules.xml.refactoring.RefactoringManager; 37 import org.netbeans.modules.xml.refactoring.RenameRequest; 38 import org.netbeans.modules.xml.refactoring.spi.UIHelper; 39 import org.netbeans.modules.xml.refactoring.ui.ModelProvider; 40 import org.netbeans.modules.xml.refactoring.ui.j.spi.ui.RenameRefactoringUI; 41 import org.netbeans.modules.xml.refactoring.ui.j.ui.CheckNode; 42 import org.netbeans.modules.xml.refactoring.ui.ReferenceableProvider; 43 import org.netbeans.modules.xml.refactoring.ui.j.spi.ui.DeleteRefactoringUI; 44 import org.netbeans.modules.xml.refactoring.ui.j.spi.ui.FileRenameRefactoringUI; 45 import org.netbeans.modules.xml.refactoring.ui.j.spi.ui.RefactoringUI; 46 import org.netbeans.modules.xml.refactoring.ui.j.ui.RefactoringPanel; 47 import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView; 48 import org.netbeans.modules.xml.xam.Component; 49 import org.netbeans.modules.xml.xam.Model; 50 import org.netbeans.modules.xml.xam.Nameable; 51 import org.netbeans.modules.xml.xam.Named; 52 import org.netbeans.modules.xml.xam.Referenceable; 53 import org.openide.DialogDisplayer; 54 import org.openide.ErrorManager; 55 import org.openide.NotifyDescriptor; 56 import org.openide.filesystems.FileObject; 57 import org.openide.filesystems.FileUtil; 58 import org.openide.loaders.DataObject; 59 import org.openide.loaders.DataObjectNotFoundException; 60 import org.openide.text.CloneableEditorSupport; 61 import org.openide.util.NbBundle; 62 import org.openide.windows.TopComponent; 63 import prefuse.Visualization; 64 import prefuse.data.Edge; 65 import prefuse.data.Graph; 66 import prefuse.data.Node; 67 import prefuse.data.expression.Predicate; 68 import prefuse.data.expression.parser.ExpressionParser; 69 import prefuse.util.ColorLib; 70 import prefuse.visual.EdgeItem; 71 import prefuse.visual.NodeItem; 72 73 77 public abstract class AnalysisUtilities { 78 79 80 public static final String WSDL_MIME_TYPE = "text/xml-wsdl"; 82 87 public static Image getImage(FileObject fobj){ 88 try { 89 return DataObject.find(fobj).getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16); 90 } catch (DataObjectNotFoundException ex) { 91 ErrorManager.getDefault().notify(ex); 92 } 93 return null; 94 } 95 96 97 98 102 public static FileObject getFileObject(final Component xamComp){ 103 return (FileObject)xamComp.getModel().getModelSource().getLookup().lookup(FileObject.class); 104 } 105 106 114 public static CheckNode findCheckNode(TreeModel model, Object userObject){ 115 if (model == null || userObject == null){ 116 return null; 117 } 118 CheckNode root = (CheckNode)model.getRoot(); 119 if (root.getUserObject() == userObject){ 120 return root; 121 } 122 return processChildren(root, userObject); 123 124 } 125 126 private static CheckNode processChildren(CheckNode node, Object userObject){ 127 Enumeration en = node.children(); 128 while (en.hasMoreElements()){ 129 CheckNode child = (CheckNode)en.nextElement(); 130 Object currUserObject = child.getUserObject(); 131 if (currUserObject == userObject){ 132 return child; 133 } 134 return processChildren(child, userObject); 135 } 136 return null; 137 } 138 139 148 public static int[] getHSBPalette(int size, float s, float b, 149 AnalysisConstants.HSBHues[] huesToExclude) { 150 int[] cm = new int[size]; 151 int igen = 0; 152 for ( int i=0; i<size; i++ ) { 153 float h = 0; 154 boolean goodHue = false; 155 while(goodHue == false){ 156 h = ((float)igen++)/(size); 157 if (huesToExclude != null) { 158 for (AnalysisConstants.HSBHues exH:huesToExclude){ 159 goodHue = !isInColorFamily(h, exH); 160 if (goodHue == false){ 161 break; 162 } 163 } 164 } else { 165 goodHue = true; 166 } 167 } 168 cm[i] = ColorLib.hsb(h,s,b); 169 } 170 return cm; 171 } 172 173 private static boolean isInColorFamily(float h, AnalysisConstants.HSBHues hue){ 174 if (hue == null){ 175 return false; 176 } 177 return (h >= hue.low() && h <= hue.high()); 178 179 } 180 181 194 public static void expandCollapseFileNodes(List <NodeItem> fileNodes){ 195 Predicate p = null; 196 if (fileNodes == null || fileNodes.size() < 1 || fileNodes.get(0) == null){ 197 return; 198 } 199 Visualization vis = fileNodes.get(0).getVisualization(); 200 201 if (fileNodes.size() == 1){ 202 NodeItem n = fileNodes.get(0); 203 n.setBoolean(AnalysisConstants.IS_EXPANDED, true); 204 p = (Predicate) 205 ExpressionParser.parse("["+AnalysisConstants.FILE_GROUP+"] = " + n.getInt(AnalysisConstants.FILE_NODE_FILE_GROUP)); 207 vis.setVisible(AnalysisConstants.GRAPH_GROUP, p, true); 208 setFileEdgeVisible(n, false); 209 210 } else { 211 for(NodeItem n:fileNodes){ 212 n.setBoolean(AnalysisConstants.IS_EXPANDED, false); 213 p = (Predicate) 214 ExpressionParser.parse("["+AnalysisConstants.FILE_GROUP+"] = " + n.getInt(AnalysisConstants.FILE_NODE_FILE_GROUP)); 216 vis.setVisible(AnalysisConstants.GRAPH_GROUP, p, false); 217 setFileEdgeVisible(n, true); 218 } 219 } 220 } 221 222 228 private static void setFileEdgeVisible(final NodeItem fileNode, boolean visible){ 229 Iterator outEdges = fileNode.outEdges(); 230 while(outEdges.hasNext()){ 231 EdgeItem edge = EdgeItem.class.cast(outEdges.next()); 232 if (edge.getString(AnalysisConstants.EDGE_TYPE).equals( 233 AnalysisConstants.FILE_EDGE_TYPE)){ 234 edge.setVisible(visible); 235 } 236 } 237 } 238 239 258 259 public static String createHTMLToolTip(ToolTipLine[] lines){ 260 if (lines == null){ 261 return null; 262 } 263 StringBuffer tooltip = new StringBuffer (); 264 tooltip.append("<html><head></head><body>"); 265 for (ToolTipLine l:lines) { 266 if (l == null){ 267 continue; 268 } 269 tooltip.append("<p style='color: rgb"); 270 tooltip.append(l.getColorAsRGBString()); 271 tooltip.append(";text-align:"); 272 tooltip.append(l.getHorizontalAlignmentAsString()); 273 tooltip.append(";font-size:"); 274 tooltip.append(l.getFontSizePercentageAsString()); 275 tooltip.append("%'>"); 276 tooltip.append(l.getText()); 277 tooltip.append("</p>"); 278 } 279 tooltip.append("</body></html>"); 280 return tooltip.toString(); 281 } 282 283 284 289 public static void dumpGraph(Graph graph){ 290 if (graph == null){ 291 return; 292 } 293 294 Iterator nodeIt = graph.nodes(); 295 Iterator edgeIt = graph.edges(); 296 System.out.println("NODES:"); 297 while (nodeIt.hasNext()){ 298 Node node = Node.class.cast(nodeIt.next()); 299 System.out.println(" "); 300 System.out.println(node.toString()); 301 Iterator nodeEdgesIt = node.edges(); 309 while(nodeEdgesIt.hasNext()){ 310 Edge edge = Edge.class.cast(nodeEdgesIt.next()); 311 System.out.println("First node: " + edge.getSourceNode().toString() 312 + " Second node: " + edge.getTargetNode().toString() 313 + " isDirected: " + edge.isDirected() 314 ); 315 } 316 } 317 System.out.println("EDGES:"); 318 while (edgeIt.hasNext()){ 319 Edge edge = Edge.class.cast(edgeIt.next()); 320 System.out.println(" "); 321 System.out.println(edge.toString()); 322 System.out.println("First Node: " + edge.getSourceNode().toString()); 330 System.out.println("Second Node: " + edge.getTargetNode().toString()); 331 } 332 333 } 334 335 public static String getXmlFileType(FileObject fobj){ 336 if (fobj.getExt().equals(AnalysisConstants.SCHEMA_FILE_EXTENSION)){ 337 return AnalysisConstants.SCHEMA_FILE_TYPE; 338 } 339 if ( WSDL_MIME_TYPE.equals(FileUtil.getMIMEType(fobj))) { 340 return AnalysisConstants.WSDL_FILE_TYPE; 341 } 342 if (fobj.getExt().equals(AnalysisConstants.BPEL_FILE_EXTENSION)){ 343 return AnalysisConstants.BPEL_FILE_TYPE; 344 } 345 return ""; } 347 348 public static String getXmlFileTypeDisplayName(String fileType){ 349 if (fileType.equals(AnalysisConstants.SCHEMA_FILE_TYPE)){ 350 return NbBundle.getMessage(AnalysisUtilities.class, "LBL_Schema"); 351 } 352 353 if (fileType.equals(AnalysisConstants.WSDL_FILE_TYPE)){ 354 return NbBundle.getMessage(AnalysisUtilities.class, "LBL_WSDL"); 355 } 356 357 if (fileType.equals(AnalysisConstants.BPEL_FILE_TYPE)){ 358 return NbBundle.getMessage(AnalysisUtilities.class, "LBL_BPEL"); 359 } 360 return ""; 362 } 363 364 365 371 public static Referenceable getReferenceable(final org.openide.nodes.Node[] nodes) { 372 Referenceable referenceable = null; 373 ReferenceableProvider provider = 374 (ReferenceableProvider)nodes[0].getCookie(ReferenceableProvider.class); 375 if (provider != null){ 376 referenceable = provider.getReferenceable(); 377 } else { 378 ModelProvider modelProvider = (ModelProvider)nodes[0].getCookie(ModelProvider.class); 379 if (modelProvider != null) { 380 referenceable = modelProvider.getModel(); 381 } 382 } 383 return referenceable; 384 } 385 386 394 public static class ToolTipLine { 395 public enum HorizontalAlignment { 396 CENTER("center"), LEFT("left"), RIGHT("right"); 400 private final String name; 401 HorizontalAlignment(String name) { 402 this.name = name; 403 } 404 public String toString() { return name; } 405 } 406 private String text =""; private int fontSizePercentage = 100; 408 private int rgbColor = Color.BLACK.getRGB(); 409 private HorizontalAlignment hAlign = HorizontalAlignment.CENTER; 410 public ToolTipLine(String text, int fontSizePercentage, int rgbColor, HorizontalAlignment hAlign){ 411 412 this.text = text; 413 this.fontSizePercentage = fontSizePercentage; 414 this.rgbColor = rgbColor; 415 this.hAlign = hAlign; 416 } 417 418 419 public ToolTipLine(String text, int rgbColor){ 420 this.text = text; 421 this.rgbColor = rgbColor; 422 } 423 424 425 public ToolTipLine(String text){ 426 this.text = text; 427 } 428 429 public String getHorizontalAlignmentAsString() { 430 return this.hAlign.toString(); 431 } 432 433 public String getText() { 434 return " " + this.text + " "; } 436 437 public String getFontSizePercentageAsString() { 438 return String.valueOf(fontSizePercentage); 439 } 440 441 442 public String getColorAsRGBString() { 444 Color color = new Color (rgbColor); 445 StringBuilder str = new StringBuilder (); 446 str.append("("); str.append(String.valueOf(color.getRed())); 448 str.append(","); str.append(String.valueOf(color.getGreen())); 450 str.append(","); str.append(String.valueOf(color.getBlue())); 452 str.append(")"); return str.toString(); 454 } 455 456 457 } 458 459 public static String getName(Referenceable target) { 460 if (target instanceof Model ) { 461 FileObject fo = (FileObject) ((Model )target).getModelSource().getLookup().lookup(FileObject.class); 462 assert fo != null : "Target model does not contain FileObject in lookkup"; 463 return fo.getName(); 464 } else { 465 return ((Named)target).getName(); 466 } 467 } 468 469 public static org.openide.nodes.Node getDisplayNode(Referenceable ref) { 470 if (ref instanceof Model ) { 471 return getUIHelper(ref).getDisplayNode((Model )ref); 472 } else { 473 return getUIHelper(ref).getDisplayNode((Component)ref); 474 } 475 } 476 477 public static UIHelper getUIHelper(Referenceable component) { 478 return RefactoringManager.getInstance().getTargetComponentUIHelper(component); 479 } 480 481 public static Object [] getChangeValues(RefactorRequest request) { 482 if (request instanceof RenameRequest) { 483 RenameRequest rr = (RenameRequest) request; 484 return new Object [] { rr.getOldName(), rr.getNewName() }; 485 } else if (request instanceof FileRenameRequest) { 486 FileRenameRequest rr = (FileRenameRequest) request; 487 return new Object [] { rr.getOldFileName(), rr.getNewFileName() }; 488 } 489 return new String [0]; 490 } 491 492 495 public static void locallyRenameRefactor(Nameable target, String newName) { 496 RenameRequest request = new RenameRequest(target, newName); 497 request.setScopeLocal(); 498 doQuietRefactor(request); 499 } 500 501 505 public static void doQuietRefactor(RefactorRequest request) { 506 try { 507 RefactoringManager.getInstance().execute(request, false); 508 } catch(IOException ex) { 509 String msg = ex.getMessage(); 510 NotifyDescriptor nd = new NotifyDescriptor.Message( 511 msg, NotifyDescriptor.ERROR_MESSAGE); 512 DialogDisplayer.getDefault().notify(nd); 513 } catch (CannotRefactorException cre) { 514 showRefactoringUI(request); 515 } 516 } 517 518 public static void showRefactoringUI(RefactorRequest request) { 519 WhereUsedView wuv = new WhereUsedView(request.getTarget()); 520 org.netbeans.modules.refactoring.spi.ui.RefactoringUI ui = null; 521 if (request instanceof FileRenameRequest) { 522 ui = new FileRenameRefactoringUI(wuv, (FileRenameRequest)request); 523 } else if (request instanceof RenameRequest) { 524 ui = new RenameRefactoringUI(wuv, (RenameRequest)request); 525 } else if (request instanceof DeleteRequest) { 526 ui = new DeleteRefactoringUI(wuv, (DeleteRequest) request); 527 } else { 528 throw new IllegalArgumentException ("Unknown refactoring type!"); } 530 TopComponent activetc = TopComponent.getRegistry().getActivated(); 531 if (activetc instanceof CloneableEditorSupport.Pane) { 532 UI.openRefactoringUI(ui, activetc); 534 } else { 535 UI.openRefactoringUI(ui); 537 } 538 } 539 } 540 541 542 | Popular Tags |