1 19 20 package org.netbeans.modules.xml.schema.refactoring.ui; 21 22 import java.awt.Color ; 23 import java.awt.Image ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 import javax.swing.text.StyledDocument ; 31 import org.netbeans.api.project.FileOwnerQuery; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectUtils; 34 import org.netbeans.api.project.SourceGroup; 35 import org.netbeans.modules.xml.nbprefuse.AnalysisConstants; 36 import org.netbeans.modules.xml.schema.model.Schema; 37 import org.netbeans.modules.xml.schema.model.SchemaComponent; 38 import org.netbeans.modules.xml.schema.model.SchemaModel; 39 import org.netbeans.modules.xml.schema.model.visitor.FindUsageVisitor; 40 import org.netbeans.modules.xml.schema.model.visitor.Preview; 41 import org.netbeans.modules.xml.xam.Component; 42 import org.netbeans.modules.xml.xam.Model; 43 import org.netbeans.modules.xml.xam.NamedReferenceable; 44 import org.openide.ErrorManager; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.Repository; 47 import org.openide.loaders.DataObject; 48 import org.openide.loaders.DataObjectNotFoundException; 49 import org.openide.nodes.AbstractNode; 50 import org.openide.nodes.Children; 51 import org.openide.text.CloneableEditorSupport; 52 import org.openide.text.Line; 53 import org.openide.text.NbDocument; 54 import org.openide.util.NbBundle; 55 import prefuse.data.Edge; 56 import prefuse.data.Graph; 57 import prefuse.data.Node; 58 import prefuse.util.ColorLib; 59 60 64 public abstract class QueryUtilities { 65 66 74 75 public static List <SourceGroup> getProjectSourceGroups(final SchemaModel model, 76 final String sourceGroupName){ 77 if (model == null || sourceGroupName == null){ 78 return null; 79 } 80 List <SourceGroup>result = new ArrayList <SourceGroup>(); 81 Project proj = getProject(model); 82 83 List <String >sourceGroupTypeList = new ArrayList <String >(); 84 sourceGroupTypeList.add(sourceGroupName); 85 86 for(String type: sourceGroupTypeList){ 87 SourceGroup[] srcGrps = ProjectUtils.getSources(proj).getSourceGroups(type); 88 if(srcGrps != null){ 89 for(SourceGroup srcGrp : srcGrps) { 90 result.add(srcGrp); 91 } 92 } 93 } 94 return result; 95 } 96 97 101 private static Project getProject(final Model model){ 102 if (model == null ){ 103 return null; 104 } 105 FileObject fileObj = (FileObject) model.getModelSource().getLookup().lookup(FileObject.class); 106 return FileOwnerQuery.getOwner(fileObj); 107 108 } 109 110 111 112 113 117 118 @SuppressWarnings ("unchecked") 119 public static Preview getUsagesPreview(Schema schema, NamedReferenceable ref){ 120 FindUsageVisitor usage = new FindUsageVisitor(); 121 return usage.findUsages(Collections.singletonList 122 (schema), ref); 123 124 } 125 126 134 public static AbstractNode createCategoryNode(AnalysisConstants.GlobalTypes type) { 135 Children.SortedArray children = new Children.SortedArray(); 136 children.setComparator(new NodeComparator()); 137 AbstractNode catNode = new AbstractNode(children) { 138 org.openide.nodes.Node node = null; 139 private org.openide.nodes.Node getDelegateNode() { 140 if(node==null) { 141 try { 142 node = DataObject.find(Repository.getDefault(). 143 getDefaultFileSystem().getRoot()).getNodeDelegate(); 144 } catch (DataObjectNotFoundException ex) { 145 } 146 } 147 return node; 148 }; 149 public Image getIcon(int type) { 150 org.openide.nodes.Node n = getDelegateNode(); 151 return n!=null?n.getIcon(type):super.getIcon(type); 152 } 153 public Image getOpenedIcon(int type) { 154 org.openide.nodes.Node n = getDelegateNode(); 155 return n!=null?n.getOpenedIcon(type):super.getOpenedIcon(type); 156 } 157 }; 158 String label = null; 159 switch(type){ 160 case COMPLEX_TYPE: 161 label = NbBundle.getMessage(QueryUtilities.class,"LBL_GlobalComplexTypes"); 162 break; 163 case SIMPLE_TYPE: 164 label = NbBundle.getMessage(QueryUtilities.class,"LBL_GlobalSimpleTypes"); 165 break; 166 case PRIMITIVE: 167 label = NbBundle.getMessage(QueryUtilities.class,"LBL_Primitives"); 168 break; 169 case ELEMENT: 170 label = NbBundle.getMessage(QueryUtilities.class,"LBL_GlobalElements"); 171 break; 172 case GROUP: 173 label = NbBundle.getMessage(QueryUtilities.class,"LBL_GlobalGroups"); 174 break; 175 case ATTRIBUTE: 176 label = NbBundle.getMessage(QueryUtilities.class,"LBL_GlobalAttributes"); 177 break; 178 case ATTRIBUTE_GROUP: 179 label = NbBundle.getMessage(QueryUtilities.class,"LBL_GlobalAttributeGroups"); 180 break; 181 case BASE_COMPLEX_TYPES: 182 label = NbBundle.getMessage(QueryUtilities.class,"LBL_Global_ComplexTypes_With_Derivations"); 183 break; 184 } 185 186 catNode.setName(label); 187 return catNode; 188 } 189 190 191 200 public static int[] getHSBPalette(int size, float s, float b, 201 AnalysisConstants.HSBHues[] huesToExclude) { 202 int[] cm = new int[size]; 203 int igen = 0; 204 for ( int i=0; i<size; i++ ) { 205 float h = 0; 206 boolean goodHue = false; 207 while(goodHue == false){ 208 h = ((float)igen++)/(size); 209 if (huesToExclude != null) { 210 for (AnalysisConstants.HSBHues exH:huesToExclude){ 211 goodHue = !isInColorFamily(h, exH); 212 if (goodHue == false){ 213 break; 214 } 215 } 216 } else { 217 goodHue = true; 218 } 219 } 220 cm[i] = ColorLib.hsb(h,s,b); 221 } 222 return cm; 223 } 224 225 private static boolean isInColorFamily(float h, AnalysisConstants.HSBHues hue){ 226 if (hue == null){ 227 return false; 228 } 229 return (h >= hue.low() && h <= hue.high()); 230 231 } 232 233 238 public static void dumpGraph(Graph graph){ 239 } 283 284 289 public static String getTextForSchemaComponent(final Component comp){ 290 SchemaComponent sc = null; 291 if (comp instanceof SchemaComponent){ 292 sc = SchemaComponent.class.cast(comp); 293 } 294 if (sc == null){ 295 return ""; } 297 DataObject dobj = null; 299 try { 300 dobj = DataObject.find((FileObject) sc.getModel().getModelSource().getLookup().lookup(FileObject.class)); 301 } catch (DataObjectNotFoundException ex) { 302 ErrorManager.getDefault().notify(ex); 303 } 304 CloneableEditorSupport editor = (CloneableEditorSupport)dobj.getCookie(org.openide.cookies.EditorCookie.class); 305 Line.Set s =editor.getLineSet(); 306 StyledDocument doc = editor.getDocument(); 307 308 int position = (int)sc.findPosition(); 309 int line = NbDocument.findLineNumber(doc, position); 310 int col = NbDocument.findLineColumn(doc, position); 311 Line xmlLine = s.getCurrent(line); 312 String nodeLabel = xmlLine.getText().trim(); 313 Pattern lt = Pattern.compile("<"); Matcher mlt = lt.matcher(nodeLabel); 316 nodeLabel = mlt.replaceAll("<"); Pattern gt = Pattern.compile(">"); Matcher mgt = gt.matcher(nodeLabel); 319 nodeLabel = mgt.replaceAll(">"); return boldenRefOrType(nodeLabel); 321 } 322 323 335 private static String boldenRefOrType(String label){ 336 int it = label.indexOf(" type"); if (it < 0){ 344 it = label.indexOf(" ref"); } 346 if (it < 0){ 347 return label; 349 } 350 int iq1 = label.indexOf('"',it); 351 if (iq1 < it){ 352 return label; 354 } 355 int ic = label.indexOf(':',iq1); 356 int iq2 = label.indexOf('"', iq1+1); 360 if (iq2 < iq1 || ic > iq2){ 361 return label; 363 } 364 int ib1 = -1; 365 if (ic > -1){ 366 ib1 = ic+1; 367 } else { 368 ib1 = iq1+1; 369 } 370 StringBuffer l = new StringBuffer (label); 371 l.insert(ib1,"<b>"); 372 l.insert(iq2+3,"</b>"); 374 return l.toString(); 375 376 } 377 378 379 387 public static class ToolTipLine { 388 public enum HorizontalAlignment { 389 CENTER("center"), LEFT("left"), RIGHT("right"); 393 private final String name; 394 HorizontalAlignment(String name) { 395 this.name = name; 396 } 397 public String toString() { return name; } 398 } 399 private String text =""; private int fontSizePercentage = 100; 401 private int rgbColor = Color.BLACK.getRGB(); 402 private HorizontalAlignment hAlign = HorizontalAlignment.CENTER; 403 public ToolTipLine(String text, int fontSizePercentage, int rgbColor, HorizontalAlignment hAlign){ 404 405 this.text = text; 406 this.fontSizePercentage = fontSizePercentage; 407 this.rgbColor = rgbColor; 408 this.hAlign = hAlign; 409 } 410 411 412 public ToolTipLine(String text, int rgbColor){ 413 this.text = text; 414 this.rgbColor = rgbColor; 415 } 416 417 418 public ToolTipLine(String text){ 419 this.text = text; 420 } 421 422 public String getHorizontalAlignmentAsString() { 423 return this.hAlign.toString(); 424 } 425 426 public String getText() { 427 return " " + this.text + " "; } 429 430 public String getFontSizePercentageAsString() { 431 return String.valueOf(fontSizePercentage); 432 } 433 434 435 public String getColorAsRGBString() { 437 Color color = new Color (rgbColor); 438 StringBuffer str = new StringBuffer (); 439 str.append("("); str.append(String.valueOf(color.getRed())); 441 str.append(","); str.append(String.valueOf(color.getGreen())); 443 str.append(","); str.append(String.valueOf(color.getBlue())); 445 str.append(")"); return str.toString(); 447 } 448 449 450 } 451 } 452 453 | Popular Tags |