1 19 27 28 package org.netbeans.modules.xml.schema.abe; 29 30 import java.awt.Color ; 31 import java.awt.Component ; 32 import java.awt.Cursor ; 33 import java.awt.Dimension ; 34 import java.awt.Font ; 35 import java.awt.Point ; 36 import java.awt.Rectangle ; 37 import java.util.ArrayList ; 38 import java.util.List ; 39 import javax.swing.Icon ; 40 import javax.swing.JFrame ; 41 import javax.swing.JLabel ; 42 import javax.swing.JPanel ; 43 import javax.swing.JViewport ; 44 import javax.swing.SwingConstants ; 45 import javax.swing.SwingUtilities ; 46 import org.netbeans.modules.xml.axi.AXIComponent; 47 import org.netbeans.modules.xml.axi.AXIModel; 48 import org.netbeans.modules.xml.axi.AXIModelFactory; 49 import org.netbeans.modules.xml.axi.AXIType; 50 import org.netbeans.modules.xml.axi.AbstractElement; 51 import org.netbeans.modules.xml.axi.Attribute; 52 import org.netbeans.modules.xml.axi.ContentModel; 53 import org.netbeans.modules.xml.axi.Element; 54 import org.netbeans.modules.xml.axi.datatype.Datatype; 55 import org.netbeans.modules.xml.schema.model.SchemaComponent; 56 import org.netbeans.modules.xml.xam.Model; 57 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie; 58 import org.openide.filesystems.FileObject; 59 import org.openide.loaders.DataObject; 60 import org.openide.loaders.DataObjectNotFoundException; 61 import org.openide.util.NbBundle; 62 63 67 public class UIUtilities { 68 69 public static String standardizeString(String str){ 70 str = str.toLowerCase(); 71 StringBuffer strbuff = new StringBuffer (str); 72 strbuff.replace(0, 1, str.substring(0,1).toUpperCase()); 73 return new String (strbuff); 74 } 75 76 public static String getConstraintsString(String minOccurs, String maxOccurs) { 77 int min; 78 int max; 79 80 if(minOccurs == null){ 81 min = 1; 82 }else{ 83 try{ 84 min = Integer.parseInt(minOccurs); 85 }catch(NumberFormatException e){ 86 min = 1; 87 } 88 } 89 90 if(maxOccurs == null){ 91 max = 1; 92 }else if(maxOccurs.equalsIgnoreCase("unbounded")){ max = Integer.MAX_VALUE; 94 }else{ 95 try{ 96 max = Integer.parseInt(maxOccurs); 97 }catch(NumberFormatException e){ 98 max = 1; 99 } 100 } 101 102 String str; 103 if(min == max){ 104 if(max == 1){ 105 return ""; 106 } 107 return "["+min+".."+max+"]"; 108 } 109 if(min < max){ 110 String maxStr; 111 if(max == Integer.MAX_VALUE) 112 maxStr = "*"; else 114 maxStr = ""+max; 115 return "["+min+".."+maxStr+"]"; 116 } 117 return null; 119 } 120 121 122 123 private static final String LOC_SIMPLE_TYPE = NbBundle.getMessage(UIUtilities.class, "LBL_SIMPLE_TYPE"); 124 private static final String LOC_COMPLEX_TYPE = NbBundle.getMessage(UIUtilities.class, "LBL_COMPLEX_TYPE"); 125 private static final String LOC_FROM = NbBundle.getMessage(UIUtilities.class, "LBL_FROM"); 126 private static final String LOC_BELONGS_TO = NbBundle.getMessage(UIUtilities.class, "LBL_INSTANCE_OF"); 127 128 public static JLabel getContentModelInfoLabel(AXIComponent component, boolean simpleTypeOnly, boolean withIcon, final InstanceUIContext context) { 129 HyperlinkLabel contentModelInfoLabel = new HyperlinkLabel(); 130 contentModelInfoLabel.setForeground(new Color (139, 139, 139)); 131 132 Font font = contentModelInfoLabel.getFont(); 133 font = new Font (font.getFontName(), Font.PLAIN, 134 InstanceDesignConstants.PROPS_FONT_SIZE); 135 contentModelInfoLabel.setFont(font); 136 137 if(component instanceof Element){ 138 Element element = (Element) component; 139 AXIType dt = element.getType(); 140 141 if(element.isReference()){ 142 try{ 144 Element aelm = ((Element)getSuperDefn(element)); 145 while(aelm.isReference()) 146 aelm = (Element)aelm.getReferent(); 147 contentModelInfoLabel.setText(aelm.getName()); 148 contentModelInfoLabel.setIcon(getImageIcon("element.png")); 149 contentModelInfoLabel.setToolTipText(NbBundle.getMessage(UIUtilities.class, 150 "TTP_REFERENCE_TO_GE", element)); 151 final AbstractElement tmpaelm = aelm; 152 contentModelInfoLabel.setHyperlinkClickHandler(new HyperlinkLabel.HyperlinkClickHandler(){ 153 public void handleClick() { 154 showDefinition(context, tmpaelm, false); 155 } 156 157 }); 158 return contentModelInfoLabel; 159 }catch (Throwable e){ 160 return null; 162 } 163 } 164 165 if(dt == null) 166 return null; 167 String value = dt.getName(); 168 if(value == null) { 169 contentModelInfoLabel.setText(NbBundle.getMessage(UIUtilities.class, "LBL_ANONYMOUS_TYPE")); 170 return contentModelInfoLabel; 171 } 172 173 contentModelInfoLabel.setText(value); 174 String typeStr = null; 175 typeStr = (dt instanceof Datatype) ? LOC_SIMPLE_TYPE : LOC_COMPLEX_TYPE; 176 if(withIcon){ 177 String iconStr = (dt instanceof Datatype) ? "simpletype.png" : 178 "complextype.png"; 179 contentModelInfoLabel.setIcon(getImageIcon(iconStr)); 180 } 181 String infoStr = " "+LOC_BELONGS_TO+" "+typeStr+": "+value+" "; 182 contentModelInfoLabel.setToolTipText(infoStr); 183 if(dt instanceof ContentModel){ 184 final ContentModel cm = (ContentModel) dt; 185 contentModelInfoLabel. 186 setHyperlinkClickHandler(new HyperlinkLabel.HyperlinkClickHandler(){ 187 public void handleClick() { 188 showDefinition(context, cm, false); 189 } 190 191 }); 192 } 193 return contentModelInfoLabel; 194 } 195 196 if(!simpleTypeOnly){ 197 final ContentModel cm = component.getContentModel(); 198 if(cm != null){ 199 String type = getContentModelTypeString(cm.getType()); 200 String locFromStr = (component instanceof AbstractElement) ? LOC_BELONGS_TO : LOC_FROM; 201 String infoStr = " "+locFromStr+" "+type+": "+cm.getName()+" "; 202 contentModelInfoLabel.setToolTipText(infoStr); 203 contentModelInfoLabel.setText(cm.getName()); 204 if(withIcon) 205 contentModelInfoLabel.setIcon(getContentModelTypeIcon(cm.getType())); 206 contentModelInfoLabel. 207 setHyperlinkClickHandler(new HyperlinkLabel.HyperlinkClickHandler(){ 208 public void handleClick() { 209 showDefinition(context, cm, false); 210 } 211 212 }); 213 return contentModelInfoLabel; 214 } 215 } 216 return null; 217 } 218 219 public static org.netbeans.modules.xml.xam.Component getSuperDefn(AXIComponent axiComponent) { 220 AXIComponent original = axiComponent.getOriginal(); 221 if(original == axiComponent){ 222 if(original instanceof Element){ 223 Element elm = (Element)original; 224 if(elm.isReference()) 225 return elm.getReferent(); 226 } 227 if(original instanceof Attribute){ 228 Attribute attr = (Attribute)original; 229 if(attr.isReference()) 230 return attr.getReferent(); 231 } 232 return null; 233 } 234 return original; 235 } 236 237 238 public static void showDefinition(InstanceUIContext context, 239 AXIComponent axiComponent, boolean showSuper){ 240 if(context != null){ 241 setBusyCursor(context); 242 try{ 243 FileObject fo = (FileObject) axiComponent.getModel() 244 .getSchemaModel().getModelSource().getLookup() 245 .lookup(FileObject.class); 246 DataObject sdo = null; 247 try { 248 sdo = DataObject.find(fo); 249 } catch (DataObjectNotFoundException ex) { 250 return; 251 } 252 if(sdo == null) 253 return; 254 ViewComponentCookie vcc = (ViewComponentCookie) sdo 255 .getCookie(ViewComponentCookie.class); 256 if(vcc != null){ 257 AXIComponent tmp = axiComponent; 258 if(showSuper) 259 tmp = (AXIComponent) getSuperDefn(axiComponent); 260 vcc.view(ViewComponentCookie.View.SUPER, tmp); 261 } 262 }finally{ 263 setDefaultCursor(context); 264 } 265 } 266 } 267 268 269 public static String getContentModelTypeString(ContentModel.ContentModelType cmType) { 270 switch(cmType){ 271 case COMPLEX_TYPE: 272 return NbBundle.getMessage(UIUtilities.class, "LBL_COMPLEX_TYPE"); case GROUP: 274 return NbBundle.getMessage(UIUtilities.class, "LBL_GROUP"); case ATTRIBUTE_GROUP: 276 return NbBundle.getMessage(UIUtilities.class, "LBL_ATTRIBUTE_GROUP"); } 278 return ""; 279 } 280 281 public static Icon getContentModelTypeIcon(ContentModel.ContentModelType cmType) { 282 switch(cmType){ 283 case COMPLEX_TYPE: 284 return new javax.swing.ImageIcon (UIUtilities.class. 285 getResource("/org/netbeans/modules/xml/schema/abe/resources/complextype.png")); 286 case GROUP: 287 return new javax.swing.ImageIcon (UIUtilities.class. 288 getResource("/org/netbeans/modules/xml/schema/abe/resources/group.png")); 289 case ATTRIBUTE_GROUP: 290 return new javax.swing.ImageIcon (UIUtilities.class. 291 getResource("/org/netbeans/modules/xml/schema/abe/resources/attributeGroup.png")); 292 } 293 return null; 294 } 295 296 public static Icon getImageIcon(String iconFileName) { 297 return new javax.swing.ImageIcon (UIUtilities.class. 298 getResource("/org/netbeans/modules/xml/schema/abe/resources/" 299 +iconFileName)); 300 } 301 302 public static void scrollViewTo(Component comp, InstanceUIContext context){ 303 if(!context.getTopComponent().isShowing()) 304 return; 305 JViewport viewPort = context.getInstanceDesignerScrollPane().getViewport(); 306 Rectangle rect = comp.getBounds(); 307 rect = SwingUtilities.convertRectangle(comp.getParent(), rect, viewPort); 308 rect.height += 100; 309 viewPort.scrollRectToVisible(rect); 311 } 312 313 public static AXIComponent findMatchingAXIComponent(SchemaComponent c) { 314 AXIComponent matchingComponent = null; 315 AXIModel model = AXIModelFactory.getDefault().getModel(c.getModel()); 316 assert model != null; 317 if (model.getState() == Model.State.VALID) { 318 List <SchemaComponent> pathFromRoot = findSchemaComponentsFromRoot(c); 319 matchingComponent = findClosestAXIComponent(pathFromRoot, model); 320 } 321 return matchingComponent; 322 } 323 324 private static List <SchemaComponent> findSchemaComponentsFromRoot(SchemaComponent s) { 325 List <SchemaComponent> components = new ArrayList <SchemaComponent>(); 326 components.add(0,s); 327 SchemaComponent currentSC = s; 328 while (currentSC.getParent() != currentSC.getModel().getRootComponent()) { 329 currentSC = currentSC.getParent(); 330 components.add(0,currentSC); 331 } 332 return components; 333 } 334 335 private static AXIComponent findClosestAXIComponent( 336 List <SchemaComponent> components, AXIModel model) { 337 assert !components.isEmpty(); 338 SchemaComponent root = components.remove(0); 339 AXIComponent lastFound = findChildComponent(root, model.getRoot()); 340 if (lastFound != null) { 341 for (SchemaComponent sc: components) { 342 AXIComponent newRoot = findChildComponent(sc, lastFound); 343 if (newRoot != null) { 344 lastFound = newRoot; 345 } 346 } 347 } 348 return lastFound; 349 } 350 351 private static AXIComponent findChildComponent(SchemaComponent component, 352 AXIComponent searchFrom) { 353 AXIComponent found = null; 354 for (AXIComponent axiComponent:searchFrom.getChildren()) { 355 if (axiComponent.getPeer() == component) { 356 found = axiComponent; 357 } 358 } 359 return found; 360 } 361 362 public static void showBulbMessage(String message, InstanceUIContext context){ 363 showBulbMessageFor(message, context, null); 364 } 365 366 public static void showBulbMessageFor(String message, InstanceUIContext context, 367 Component messageForComp){ 368 showMessage(UIUtilities.getImageIcon("bulb.png"), null, 369 null, message, 370 context, null, messageForComp); 371 } 372 373 public static void showErrorMessage(String message, InstanceUIContext context){ 374 showErrorMessageFor(message, context, null); 375 } 376 377 public static void showErrorMessageFor(String message, InstanceUIContext context, 378 Component messageForComp){ 379 showMessage(UIUtilities.getImageIcon("error.png"), Color.RED, 380 null, message, 381 context, null, messageForComp); 382 } 383 384 public static void showErrorMessage(String message, InstanceUIContext context, JPanel glass){ 385 showErrorMessageFor(message, context, glass, null); 386 } 387 388 public static void showErrorMessageFor(String message, 389 InstanceUIContext context, JPanel glass, Component messageForComp){ 390 showMessage(UIUtilities.getImageIcon("error.png"), 391 Color.RED, null, message, 392 context, glass, messageForComp); 393 } 394 395 public static void showMessage(Icon icon, Color color, String message, InstanceUIContext context){ 396 showMessageFor(icon, color, message, context, null); 397 } 398 399 public static void showMessageFor(Icon icon, Color color, String message, 400 InstanceUIContext context, Component messageForComp){ 401 showMessage(icon, color, null, message, context, null, null); 402 } 403 404 405 406 static JLabel infoLabel ; 407 static JPanel glassReference; 408 protected static void showMessage(Icon icon, Color foreGroundColor, Color backgroundColor, 409 String message, InstanceUIContext context, JPanel glass, 410 Component messageForComp){ 411 if(message == null) 412 return; 413 if( (infoLabel != null) && (infoLabel.getParent() != null) ){ 414 if(infoLabel.getText().equals(message)) 415 return; 416 } 417 infoLabel = null; 418 if(icon != null) 419 infoLabel = new TranslucentLabel(icon , SwingConstants.LEFT); 420 else 421 infoLabel = new TranslucentLabel(" ",SwingConstants.LEFT); 422 423 Font font = infoLabel.getFont(); 424 font = new Font (font.getName(), Font.BOLD, font.getSize()); 425 if(foreGroundColor != null) 426 infoLabel.setForeground(foreGroundColor); 427 else 428 infoLabel.setForeground(Color.black); 429 430 if(backgroundColor != null) 431 infoLabel.setBackground(backgroundColor); 432 else 433 infoLabel.setBackground(InstanceDesignConstants.LIGHT_YELLOW); 434 435 infoLabel.setFont(font); 436 infoLabel.setText(message); 437 Component panel = (messageForComp != null) ? messageForComp : 438 context.getNamespacePanel(); 439 Rectangle rect = panel.getBounds(); 440 if(glass == null) 441 glass = NBGlassPaneAccessSupport.getNBGlassPane( 442 context.getInstanceDesignerPanel()); 443 else 444 glassReference = glass; 445 if(glass == null) 446 return; 447 rect = SwingUtilities.convertRectangle(panel.getParent(), rect, glass); 448 glass.add(infoLabel); 449 infoLabel.setOpaque(false); 450 if(messageForComp != null){ 451 rect.y -= 20; 452 Dimension dim = infoLabel.getPreferredSize(); 453 rect.width = dim.width; 454 rect.height = dim.height; 455 } 456 infoLabel.setBounds(rect); 457 glass.setVisible(true); 458 } 459 460 public static void hideGlassMessage(boolean disposeGlass){ 461 if((infoLabel != null) && (glassReference != null)){ 462 glassReference.remove(infoLabel); 463 glassReference.revalidate(); 464 glassReference.repaint(); 465 } 466 467 if(disposeGlass) 468 NBGlassPaneAccessSupport.forceDisposeNBGlassPane(); 469 } 470 471 public static void hideGlassMessage(){ 472 hideGlassMessage(true); 473 } 474 475 public static List <Point > getBrokenTapePoints(Point start, int end, 476 int xgap, int ygap, boolean rightHanded){ 477 List <Point > result = new ArrayList <Point >(); 478 int begin = start.y; 479 int stop = end; 480 int current = begin; 481 boolean sharpeEdge = true; 482 result.add(new Point (start.x, current)); 483 current += ygap; 484 while(current < stop){ 485 if(sharpeEdge){ 486 if(rightHanded) 487 result.add(new Point ((start.x + xgap), current)); 488 else 489 result.add(new Point ((start.x - xgap), current)); 490 sharpeEdge = false; 491 }else{ 492 if(rightHanded) 493 result.add(new Point ((start.x - xgap), current)); 494 else 495 result.add(new Point ((start.x + xgap), current)); 496 sharpeEdge = true; 498 } 499 current += ygap; 500 } 501 result.add(new Point (start.x, end)); 502 return result; 503 } 504 505 public static String getUniqueName(String name, AXIComponent elm) { 506 int count = 1; 507 String result = null; 508 String nowName = name; 509 if(elm.getChildren().size() < 0) 510 return name; 511 while(result == null){ 512 boolean gotResult = true; 513 for(AXIComponent child : elm.getChildren()){ 514 if(child.toString().indexOf(nowName) != -1){ 515 nowName = name + count++; 516 gotResult = false; 517 break; 518 } 519 } 520 if(gotResult){ 521 result = nowName; 522 break; 523 } 524 } 525 return result; 526 } 527 528 public static void setBusyCursor(InstanceUIContext context){ 529 if(context == null) 530 return; 531 JFrame NBFRAME = NBGlassPaneAccessSupport.getNBFRAME(context.getTopComponent()); 532 if(NBFRAME == null) 533 return; 534 NBFRAME.setCursor(new Cursor (Cursor.WAIT_CURSOR)); 535 } 536 537 public static void setDefaultCursor(InstanceUIContext context){ 538 if(context == null) 539 return; 540 JFrame NBFRAME = NBGlassPaneAccessSupport.getNBFRAME(context.getTopComponent()); 541 if(NBFRAME == null) 542 return; 543 NBFRAME.setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 544 } 545 546 } 547 | Popular Tags |