1 19 20 package org.netbeans.modules.xml.schema.abe; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Cursor ; 25 import java.awt.Dimension ; 26 import java.awt.Font ; 27 import java.awt.Graphics ; 28 import java.awt.Graphics2D ; 29 import java.awt.Point ; 30 import java.awt.Polygon ; 31 import java.awt.Rectangle ; 32 import java.awt.dnd.DropTargetDragEvent ; 33 import java.awt.dnd.DropTargetDropEvent ; 34 import java.awt.dnd.DropTargetEvent ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.ActionListener ; 37 import java.awt.event.KeyEvent ; 38 import java.awt.event.KeyListener ; 39 import java.awt.event.MouseAdapter ; 40 import java.awt.event.MouseEvent ; 41 import java.beans.PropertyChangeEvent ; 42 import java.beans.PropertyChangeListener ; 43 import java.io.IOException ; 44 import java.util.ArrayList ; 45 import java.util.Collections ; 46 import java.util.List ; 47 import javax.swing.Box ; 48 import javax.swing.JLabel ; 49 import javax.swing.JPanel ; 50 import javax.swing.JTextField ; 51 import javax.swing.SpringLayout ; 52 import javax.swing.SwingUtilities ; 53 import org.netbeans.modules.xml.axi.AXIComponent; 54 import org.netbeans.modules.xml.axi.AXIContainer; 55 import org.netbeans.modules.xml.axi.AXIModel; 56 import org.netbeans.modules.xml.axi.AXIType; 57 import org.netbeans.modules.xml.axi.AbstractAttribute; 58 import org.netbeans.modules.xml.axi.AbstractElement; 59 import org.netbeans.modules.xml.axi.AnyElement; 60 import org.netbeans.modules.xml.axi.Attribute; 61 import org.netbeans.modules.xml.axi.Compositor; 62 import org.netbeans.modules.xml.axi.Element; 63 import org.netbeans.modules.xml.axi.datatype.Datatype; 64 import org.netbeans.modules.xml.schema.abe.action.AttributeOnElementNewType; 65 import org.netbeans.modules.xml.schema.abe.action.ElementOnElementNewType; 66 import org.netbeans.modules.xml.schema.abe.palette.DnDHelper; 67 import org.openide.util.NbBundle; 68 import org.openide.util.datatransfer.NewType; 69 70 74 public class StartTagPanel extends TagPanel { 75 protected static final long serialVersionUID = 7526472295622776147L; 76 77 private ExpandCollapseButton expandButton; 78 public StartTagPanel(final ElementPanel elementPanel, InstanceUIContext context) { 79 super(elementPanel, context); 80 elementPanel.getAXIContainer().addPropertyChangeListener(new ModelEventMediator(this, elementPanel.getAXIContainer()) { 82 public void _propertyChange(PropertyChangeEvent evt) { 83 if(evt.getPropertyName().equals(Element.PROP_NAME)){ 84 setTagName(getElementPanel().getAXIContainer().getName()); 85 forceSizeRecalculate(); 86 revalidate(); 87 repaint(); 88 }else if(evt.getPropertyName().startsWith(Attribute.PROP_ATTRIBUTE)){ 89 updateAttributes(); 90 if(evt.getNewValue() != null){ 91 if((StartTagPanel.super.context.getUserActedComponent() == StartTagPanel.this)){ 93 showAttributes(); 94 showAttributeEditFor((Attribute) evt.getNewValue()); 95 StartTagPanel.super.context.resetUserActedComponent(); 96 } 97 } 98 }else if(evt.getPropertyName().equals(Element.PROP_TYPE)){ 99 updateAttributes(); 101 } 102 } 103 }); 104 105 initialize(); 106 initKeyListener(); 107 } 108 109 110 protected void initKeyListener(){ 111 addKeyListener(new KeyListener () { 112 public void keyPressed(KeyEvent e) { 113 if( e.getKeyCode() == e.VK_F2 ){ 114 tagNameLabel.showEditor(); 115 } 116 if(context.getFocusTraversalManager().isFocusChangeEvent(e)) 117 context.getFocusTraversalManager().handleEvent(e, StartTagPanel.this); 118 } 119 public void keyReleased(KeyEvent e) { 120 } 121 public void keyTyped(KeyEvent e) { 122 if(e.getKeyChar() == e.VK_SPACE){ 123 tagNameLabel.showEditor(); 124 }else if(e.getKeyChar() == e.VK_E){ 125 for(NewType nt : getNBNode().getNewTypes()){ 127 if(nt instanceof ElementOnElementNewType){ 128 try { nt.create(); } catch (IOException ex) {} 129 return; 130 } 131 } 132 }else if(e.getKeyChar() == e.VK_A){ 133 for(NewType nt : getNBNode().getNewTypes()){ 134 if(nt instanceof AttributeOnElementNewType){ 136 try { nt.create(); } catch (IOException ex) {} 137 return; 138 } 139 } 140 } 141 } 142 }); 143 } 144 145 146 boolean initialized = false; 147 SpringLayout startTagPanelLayout; 148 Color tagNameLabelColor = InstanceDesignConstants.TAG_NAME_COLOR; 149 private void initialize() { 150 initialized = true; 151 setOpaque(false); 152 startTagPanelLayout = new SpringLayout (); 153 setLayout(startTagPanelLayout); 154 155 attributeCollapseButton.setToolTipText(NbBundle.getMessage(StartTagPanel.class, "TTP_ATTR_EXPAND_COLLAPSE_BUTTON")); 156 attributeCountLabel.setForeground(InstanceDesignConstants.ITEM_COUNT_COLOR); 157 158 159 expandButton = getElementPanel().getExpandButton(); 160 add(expandButton); 161 startTagPanelLayout.putConstraint(SpringLayout.WEST, expandButton, 162 getA(), SpringLayout.WEST, this); 163 startTagPanelLayout.putConstraint(SpringLayout.NORTH, expandButton, 164 LABEL_HEAD_ROOM_SPACE+5, SpringLayout.NORTH, this); 165 167 168 tagNameLabel = new InplaceEditableLabel(); 169 tagNameLabel.setToolTipText(NbBundle.getMessage(StartTagPanel.class, "TTP_ELEMENT_NAME_LABEL")); 170 171 tagNameLabelColor = InstanceDesignConstants.TAG_NAME_COLOR; 172 173 if(getElementPanel().getAXIContainer().isReadOnly() || 174 (getElementPanel().getAXIContainer() instanceof AnyElement)){ 175 tagNameLabelColor = InstanceDesignConstants.TAG_NAME_READONLY_COLOR; 176 tagNameLabel.setToolTipText(NbBundle.getMessage(StartTagPanel.class, "TTP_ELEMENT_NAME_LABEL_READONLY")); 177 }else{ 178 if(getElementPanel().getAXIContainer().isShared()){ 179 tagNameLabelColor = InstanceDesignConstants.TAG_NAME_SHARED_COLOR; 180 tagNameLabel.setToolTipText(NbBundle.getMessage(StartTagPanel.class, "TTP_ELEMENT_NAME_LABEL_SHARED")); 181 } 182 } 183 184 187 tagNameLabel.setForeground(tagNameLabelColor); 188 189 getEndSlash().setForeground(tagNameLabel.getForeground()); 190 initTagEditListener(); 191 192 193 194 Component hgap = Box.createHorizontalStrut(5); 195 add(hgap); 196 startTagPanelLayout.putConstraint(SpringLayout.WEST, hgap, 197 getA()+INTER_PANEL_SPACE+8, SpringLayout.WEST, this); 198 startTagPanelLayout.putConstraint(SpringLayout.NORTH, hgap, 199 LABEL_HEAD_ROOM_SPACE, SpringLayout.NORTH, this); 200 201 add(tagNameLabel); 203 startTagPanelLayout.putConstraint(SpringLayout.WEST, tagNameLabel, 204 0, SpringLayout.EAST, hgap); 205 startTagPanelLayout.putConstraint(SpringLayout.NORTH, tagNameLabel, 206 LABEL_HEAD_ROOM_SPACE, SpringLayout.NORTH, this); 207 208 hgap = Box.createHorizontalStrut(5); 209 add(hgap); 210 startTagPanelLayout.putConstraint(SpringLayout.WEST, hgap, 211 0, SpringLayout.EAST, tagNameLabel); 212 startTagPanelLayout.putConstraint(SpringLayout.NORTH, hgap, 213 LABEL_HEAD_ROOM_SPACE, SpringLayout.NORTH, this); 214 215 firstRowLastComp = hgap; 216 218 updateTagName(); 219 addNonAttributeComponents(); 220 updateAttributes(); 221 addSelectionListener(); 222 } 223 224 Component lastNonAtribComponent; 225 Component lastAtribComponent; 226 227 228 229 protected void initTagEditListener(){ 230 231 tagNameLabel.addCtrlClickHandler(new InplaceEditableLabel.CtrlClickHandler(){ 232 public void handleCtrlClick() { 233 getNBNode().showSuperDefinition(); 234 } 235 }); 236 237 tagNameLabel.setInputValidator(new InputValidator(){ 238 public boolean isStringValid(String input) { 239 return org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(input); 240 } 241 }, NbBundle.getMessage(StartTagPanel.class, "MSG_NOT_A_NCNAME")); 242 tagNameLabel.addPropertyChangeListener(new PropertyChangeListener () { 243 public void propertyChange(PropertyChangeEvent evt) { 244 if(evt.getPropertyName().equals(tagNameLabel.PROPERTY_MODE_CHANGE)){ 245 if(evt.getNewValue() == InplaceEditableLabel.Mode.EDIT){ 246 final JTextField field = new JTextField (getElementPanel().getAXIContainer().getName()); 249 field.select(0, field.getText().length()); 250 field.addActionListener(new ActionListener (){ 251 public void actionPerformed(ActionEvent e) { 252 String newName = field.getText(); 253 if(getElementPanel().getAXIContainer().getName().equals(newName)) 254 return; 255 if(org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(newName)){ 257 field.setCursor(new Cursor (Cursor.WAIT_CURSOR)); 258 try{ 259 if(firstTimeRename) 260 getNBNode().setNameInModel(newName); 261 else 262 setTagNameInModel(newName); 263 firstTimeRename = false; 264 }finally{ 265 field.setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 266 } 267 } 268 } 269 }); 270 if(getElementPanel().getAXIContainer().isShared()){ 271 String str = NbBundle.getMessage(StartTagPanel.class, "MSG_SHARED_ELEMENT_EDIT"); 272 tagNameLabel.setEditInfoText(str, context); 273 } 274 if(!getElementPanel().getAXIContainer().isReadOnly()){ 275 if(getElementPanel().getAXIContainer() instanceof AnyElement){ 276 String str = NbBundle.getMessage(StartTagPanel.class, "MSG_ANY_ELEMENT_EDIT"); 277 tagNameLabel.setEditInfoText(str, context); 278 }else{ 279 tagNameLabel.setInlineEditorComponent(field); 280 } 281 }else{ 282 String str = NbBundle.getMessage(StartTagPanel.class, "MSG_READONLY_ELEMENT_EDIT"); 283 tagNameLabel.setEditInfoText(str, context); 284 } 285 } 286 } 287 } 288 }); 289 } 290 291 292 private void setTagNameInModel(String name){ 293 getNBNode().setName(name); 294 } 295 296 297 301 public void setTagName(String value) { 302 String tagName=value; 303 tagNameLabel.setText(tagName); 304 } 305 306 307 311 public void updateTagName() { 312 setTagName(getElementPanel().getAXIContainer().getName()); 313 } 314 315 316 317 private void showAttributeEditFor(Attribute attr){ 318 if(componentList != null){ 319 for(Component child: componentList){ 320 if(child instanceof AttributePanel){ 321 final AttributePanel attrP = (AttributePanel) child; 322 if(attrP.getAttribute() == attr){ 323 context.getComponentSelectionManager().setSelectedComponent(attrP); 324 SwingUtilities.invokeLater(new Runnable (){ 325 public void run() { 326 UIUtilities.scrollViewTo(attrP, context); 327 attrP.showEditorForName(true); 328 } 329 }); 330 return; 331 } 332 } 333 } 334 } 335 } 336 337 338 342 Component firstRowLastComp; 343 public void updateAttributes() { 344 updateAttributeCountLabel(); 345 if(componentList != null){ 347 for (Component panel: componentList){ 348 if(panel != firstTweener){ 350 startTagPanelLayout.removeLayoutComponent(panel); 351 remove(panel); 352 } 353 } 354 } 355 356 componentList = new ArrayList <Component >(); 357 componentList.add(firstTweener); 359 360 int attrCount = getElementPanel().getAXIContainer().getAttributes().size(); 361 if(attrCount > 0){ 362 if(!attributeCollapseButton.isVisible()){ 363 attributeCollapseButton.setVisible(true); 364 attributeCountLabel.setVisible(true); 365 if(context.isUserInducedEventMode()) 367 attributeCollapseButton.setText("-"); 368 } 369 }else{ 370 if(attributeCollapseButton.isVisible()){ 371 attributeCollapseButton.setVisible(false); 372 attributeCountLabel.setVisible(false); 373 } 374 } 375 376 lastAtribComponent = firstTweener; 377 firstRowLastComp = null; 378 if(attributesAreShown){ 379 addAttributes(); 380 if(firstRowLastComp == null){ 381 firstRowLastComp = getEndSlash(); 383 } 384 }else{ 385 hgap.setPreferredSize(new Dimension (0,0)); 387 firstRowLastComp = getEndSlash(); 388 } 389 390 391 addEndSlashLabel(); 392 addElementPropertiesPanel(); 393 revalidate(); 394 getElementPanel().repaint(); 396 } 397 398 static final String locHidden = NbBundle.getMessage(StartTagPanel.class, "LBL_HIDDEN"); 399 static final String locAttr = NbBundle.getMessage(StartTagPanel.class, "LBL_ATTRIBUTE"); 400 static final String locAttrs = NbBundle.getMessage(StartTagPanel.class, "LBL_ATTRIBUTES"); 401 402 protected void addAttributeCountLabel(){ 403 if(attributesAreShown) 404 return; 406 int count = getElementPanel().getAXIContainer().getAttributes().size(); 407 if(count > 0){ 408 String hidden = (attributesAreShown) ? "" : " "+locHidden; 409 String attributeStr = ""; 410 if(!attributesAreShown) 411 attributeStr = (count == 1) ? " "+locAttr : " "+locAttrs; 412 413 String str = "["+count+attributeStr+hidden+"]"; 414 String countStr = "["+count+"]"; 415 attributeCountLabel.setText(countStr); 416 attributeCountLabel.setToolTipText(str); 417 add(attributeCountLabel); 418 startTagPanelLayout.putConstraint(SpringLayout.WEST, attributeCountLabel, 419 0, SpringLayout.EAST, lastAtribComponent); 420 startTagPanelLayout.putConstraint(SpringLayout.NORTH, attributeCountLabel, 421 (getRowCount() - 1) * getTagHeight() + LABEL_HEAD_ROOM_SPACE, SpringLayout.NORTH, this); 422 lastAtribComponent = attributeCountLabel; 424 } 425 } 426 427 private void updateAttributeCountLabel(){ 428 int count = getElementPanel().getAXIContainer().getAttributes().size(); 429 if(count > 0){ 430 String hidden = (attributesAreShown) ? "" : " "+locHidden; 431 String attributeStr = ""; 432 if(!attributesAreShown) 433 attributeStr = (count == 1) ? " "+locAttr : " "+locAttrs; 434 435 String str = "["+count+attributeStr+hidden+"]"; 436 String countStr = "["+count+"]"; 437 attributeCountLabel.setText(countStr); 438 attributeCountLabel.setToolTipText(str); 439 }else{ 440 attributeCountLabel.setText(""); 441 attributeCountLabel.setToolTipText(""); 442 } 443 } 444 445 protected void addEndSlashLabel() { 446 add(getEndSlash()); 447 startTagPanelLayout.putConstraint(SpringLayout.WEST, getEndSlash(), 448 INTER_PANEL_SPACE, SpringLayout.EAST, lastAtribComponent); 449 startTagPanelLayout.putConstraint(SpringLayout.NORTH, getEndSlash(), 450 getLastRowComponentsHeadRoom()+5 , SpringLayout.NORTH, this); 451 componentList.add(getEndSlash()); 452 lastAtribComponent = getEndSlash(); 453 } 454 455 private int getLastRowComponentsHeadRoom(){ 456 return (getRowCount() - 1) * (getTagHeight() - TAG_HEIGHT_ADJUSTMENT); 457 } 458 private void addElementPropertiesPanel() { 459 if(elementPropertiesPanel != null) 460 excludePaintComponentList.remove(elementPropertiesPanel); 461 elementPropertiesPanel = getNewAXIContainerPropertiesPanel(); 462 add(elementPropertiesPanel); 463 startTagPanelLayout.putConstraint(SpringLayout.WEST, elementPropertiesPanel, 464 getA()+INTER_PANEL_SPACE+6, SpringLayout.EAST, getEndSlash()); 465 startTagPanelLayout.putConstraint(SpringLayout.NORTH, elementPropertiesPanel, 466 getLastRowComponentsHeadRoom(), SpringLayout.NORTH, this); 467 elementPropertiesPanel.addMouseListener(new MouseAdapter (){ 468 public void mouseClicked(MouseEvent e) { 469 StartTagPanel.this.dispatchEvent(e); 470 } 471 public void mouseReleased(MouseEvent e) { 472 StartTagPanel.this.dispatchEvent(e); 473 } 474 public void mousePressed(MouseEvent e) { 475 StartTagPanel.this.dispatchEvent(e); 476 } 477 }); 478 componentList.add(elementPropertiesPanel); 479 excludePaintComponentList.add(elementPropertiesPanel); 480 } 481 482 483 protected void addNonAttributeComponents() { 484 add(attributeCollapseButton); 485 attributeCollapseButton.setVisible(false); 486 startTagPanelLayout.putConstraint(SpringLayout.WEST, attributeCollapseButton, 487 INTER_PANEL_SPACE, SpringLayout.EAST, firstRowLastComp); 488 startTagPanelLayout.putConstraint(SpringLayout.NORTH, attributeCollapseButton, 489 ATTR_HEAD_ROOM_SPACE+2, SpringLayout.NORTH, this); 490 attributeCollapseButton.addActionListener(new ActionListener () { 491 public void actionPerformed(ActionEvent e) { 492 showOrHideAttrs(); 493 } 494 }); 495 496 lastAtribComponent = attributeCollapseButton; 497 addAttributeCountLabel(); 498 499 500 506 507 508 firstTweener = Box.createHorizontalStrut(0); 512 add(firstTweener); 513 startTagPanelLayout.putConstraint(SpringLayout.WEST, firstTweener, 514 INTER_PANEL_SPACE, SpringLayout.EAST, lastAtribComponent); 515 startTagPanelLayout.putConstraint(SpringLayout.NORTH, firstTweener, 516 ATTR_HEAD_ROOM_SPACE, SpringLayout.NORTH, this); 517 } 518 519 private void showOrHideAttrs(){ 520 if(attributesAreShown){ 521 hideAttributes(); 522 }else{ 523 showAttributes(); 524 } 525 526 } 527 528 public void showAttributes(){ 529 if(attributesAreShown) 530 return; 531 attributesAreShown = true; 532 attributeCollapseButton.setText("-"); 533 attributeCountLabel.setVisible(false); 534 updateAttributes(); 535 536 } 537 538 public void hideAttributes(){ 539 if(!attributesAreShown) 540 return; 541 attributesAreShown = false; 542 attributeCollapseButton.setText("+"); 543 attributeCountLabel.setVisible(true); 544 updateAttributes(); 545 } 546 int TAG_HEIGHT_ADJUSTMENT = 5; 547 protected void addAttributes() { 548 int rowCount = 0; 549 int attrPosition = 0; 550 Component tweener; 551 int tagHeight = getTagHeight() - TAG_HEIGHT_ADJUSTMENT; 552 List <AbstractAttribute> attributeList = getElementPanel().getAXIContainer().getAttributes(); 553 for (int i = 0; i< attributeList.size(); i++) { 554 if(i < NO_OF_FIRST_ROW_ATTRS){ 556 rowCount = 0; 558 attrPosition = i % NO_OF_FIRST_ROW_ATTRS; 559 } else{ 560 rowCount = (int) Math.ceil((double)(i+1 - NO_OF_FIRST_ROW_ATTRS) / (double)NO_OF_ATTRS_PER_ROW); 562 attrPosition = (i+1 - NO_OF_FIRST_ROW_ATTRS) % (NO_OF_ATTRS_PER_ROW); 563 attrPosition--; 564 if(attrPosition == -1) 565 attrPosition = NO_OF_ATTRS_PER_ROW - 1; 566 } 567 568 AttributePanel panel=new AttributePanel(this,attributeList.get(i), context); 569 add(panel); 570 componentList.add(panel); 571 if((attrPosition != 0) || (rowCount == 0)){ 572 startTagPanelLayout.putConstraint(SpringLayout.WEST, panel, 573 0, SpringLayout.EAST, lastAtribComponent); 574 }else{ 575 startTagPanelLayout.putConstraint(SpringLayout.WEST, panel, 577 firstAttrPos, SpringLayout.WEST, this); 578 } 579 startTagPanelLayout.putConstraint(SpringLayout.NORTH, panel, 580 rowCount * tagHeight+ATTR_HEAD_ROOM_SPACE + 1, SpringLayout.NORTH, this); 581 582 583 tweener = Box.createHorizontalStrut(10); 586 add(tweener); 587 componentList.add(tweener); 588 startTagPanelLayout.putConstraint(SpringLayout.WEST, tweener, 589 0, SpringLayout.EAST, panel); 590 startTagPanelLayout.putConstraint(SpringLayout.NORTH, tweener, 591 rowCount * tagHeight+ATTR_HEAD_ROOM_SPACE, SpringLayout.NORTH, this); 592 593 lastAtribComponent = tweener; 594 if((i + 1) == NO_OF_FIRST_ROW_ATTRS && ((i+1) != attributeList.size())){ 595 firstRowLastComp = lastAtribComponent; 597 } 598 } 599 } 600 601 public int getRowCount(){ 602 int tmprowCount = 0; 603 if(!attributesAreShown){ 604 return 1; 605 } 606 int atrCount = getElementPanel().getAXIContainer().getAttributes().size(); 607 if(atrCount < NO_OF_FIRST_ROW_ATTRS){ 608 return 1; 609 }else{ 610 atrCount -= NO_OF_FIRST_ROW_ATTRS; 611 return (int) Math.ceil((double) atrCount / (double) NO_OF_ATTRS_PER_ROW) + 1; 612 } 613 } 614 615 private void addNewAttributeAt(TweenerPanel tweener){ 616 int index = componentList.indexOf(tweener); 617 if(index == -1){ 618 return; 620 } 621 index = index/2; 622 AXIContainer element = getElementPanel().getAXIContainer(); 623 AXIModel model = element.getModel(); 624 model.startTransaction(); 625 try{ 626 Attribute attr = model.getComponentFactory().createAttribute(); 627 String str = UIUtilities.getUniqueName( 628 InstanceDesignConstants.NEW_ATTRIBUTE_NAME, element); 629 attr.setName(str); 630 element.addChildAtIndex(attr, index); 631 }finally{ 632 model.endTransaction(); 633 } 634 } 635 636 private void addTweenerListener(final TweenerPanel tweener){ 637 tweener.addTweenerListener(new TweenerListener(){ 638 public boolean dragAccept(DnDHelper.PaletteItem paletteItem) { 639 if(paletteItem != DnDHelper.PaletteItem.ATTRIBUTE){ 641 String str = NbBundle.getMessage(StartTagPanel.class, 642 "MSG_ATTRIBUTE_TWEENER_DROP_REJECT"); 643 UIUtilities.showErrorMessageFor(str, context, StartTagPanel.this); 644 return false; 645 } 646 String str = NbBundle.getMessage(StartTagPanel.class, 647 "MSG_ATTRIBUTE_TWEENER_DROP_ACCEPT", 648 getElementPanel().getAXIContainer().getName()); 649 UIUtilities.showBulbMessageFor(str, context, StartTagPanel.this); 650 return true; 651 } 652 653 public void drop(DnDHelper.PaletteItem paletteItem) { 654 UIUtilities.hideGlassMessage(); 655 addNewAttributeAt(tweener); 656 } 657 658 public void dragEntered(DnDHelper.PaletteItem paletteItem) { 659 } 660 661 public void dragExited() { 662 UIUtilities.hideGlassMessage(); 663 } 664 }); 665 } 666 667 668 public int getInterComponentSpacing() { 669 return INTER_PANEL_SPACE; 670 } 671 672 public void addElement(){ 673 if(context.isUserInducedEventMode()){ 674 SwingUtilities.invokeLater(new Runnable (){ 675 public void run() { 676 getElementPanel().setExpanded(true); 677 } 678 }); 679 } 680 AXIContainer elm = getElementPanel().getAXIContainer(); 681 AXIModel model = elm.getModel(); 682 Compositor comp = elm.getCompositor(); 683 boolean addCompositorFirst =false; 684 if(comp == null){ 687 comp = model.getComponentFactory().createSequence(); 688 addCompositorFirst = true; 689 } 690 Element nelm = model.getComponentFactory().createElement(); 691 String str = UIUtilities.getUniqueName(InstanceDesignConstants.NEW_ELEMENT_NAME, comp); 692 nelm.setName(str); 693 try{ 694 model.startTransaction(); 695 if(addCompositorFirst) 696 elm.addCompositor(comp); 697 if(comp.getParent() != null && comp.getModel() != null) 698 comp.addElement(nelm); 699 }finally{ 700 model.endTransaction(); 701 } 702 } 703 704 705 public void addCompositor(Compositor.CompositorType compType){ 706 AXIContainer elm = getElementPanel().getAXIContainer(); 707 AXIModel model = elm.getModel(); 708 Compositor comp = null; 709 switch(compType){ 710 case SEQUENCE: 711 comp = model.getComponentFactory().createSequence(); 712 break; 713 case CHOICE: 714 comp = model.getComponentFactory().createChoice(); 715 break; 716 case ALL: 717 comp = model.getComponentFactory().createAll(); 718 break; 719 } 720 addCompositor(comp); 721 } 722 723 724 public void addCompositor(DnDHelper.PaletteItem compType){ 725 AXIContainer elm = getElementPanel().getAXIContainer(); 726 AXIModel model = elm.getModel(); 727 Compositor comp = null; 728 switch(compType){ 729 case SEQUENCE: 730 comp = model.getComponentFactory().createSequence(); 731 break; 732 case CHOICE: 733 comp = model.getComponentFactory().createChoice(); 734 break; 735 case ALL: 736 comp = model.getComponentFactory().createAll(); 737 break; 738 } 739 addCompositor(comp); 740 } 741 742 public void addCompositor(Compositor comp){ 743 if(comp == null) 744 return; 745 AXIContainer elm = getElementPanel().getAXIContainer(); 746 AXIModel model = elm.getModel(); 747 748 model.startTransaction(); 749 try{ 750 elm.addCompositor(comp); 751 }finally{ 752 model.endTransaction(); 753 } 754 } 755 756 public void addAttribute(){ 757 AXIContainer elm = getElementPanel().getAXIContainer(); 758 AXIModel model = elm.getModel(); 759 model.startTransaction(); 760 try{ 761 Attribute attr = model.getComponentFactory().createAttribute(); 762 String str = UIUtilities.getUniqueName( 763 InstanceDesignConstants.NEW_ATTRIBUTE_NAME, elm); 764 attr.setName(str); 765 elm.addAttribute(attr); 766 }finally{ 767 model.endTransaction(); 768 } 769 } 770 771 772 public void drop(DropTargetDropEvent event) { 773 UIUtilities.hideGlassMessage(); 775 if(getElementPanel().getAXIContainer().isReadOnly()){ 776 event.rejectDrop(); 777 return; 778 } 779 setDragMode(false); 780 context.setUserInducedEventMode(true, this); 781 try{ 782 super.drop(event); 783 if(DnDHelper.getDraggedPaletteItem(event) == DnDHelper.PaletteItem.ELEMENT){ 784 addElement(); 786 } 787 if(DnDHelper.isCompositor(DnDHelper.getDraggedPaletteItem(event))){ 788 if(getElementPanel().getAXIContainer().getCompositor() != null){ 789 event.rejectDrop(); 791 return; 792 }else{ 793 addCompositor(DnDHelper.getDraggedPaletteItem(event)); 795 } 796 } 797 if(DnDHelper.getDraggedPaletteItem(event) == DnDHelper.PaletteItem.ATTRIBUTE){ 798 addAttribute(); 799 } 800 }finally{ 801 context.setUserInducedEventMode(false); 802 } 803 } 804 805 public void dragOver(DropTargetDragEvent event) { 806 super.dragOver(event); 807 if(getElementPanel().getAXIContainer().isReadOnly()){ 808 String str = NbBundle.getMessage(StartTagPanel.class, 809 "MSG_READONLY_COMPOSITOR_DROP", getElementPanel().getAXIContainer().getName()); 810 UIUtilities.showErrorMessageFor(str, context, this); 811 event.rejectDrag(); 812 return; 813 } 814 DnDHelper.PaletteItem item = DnDHelper.getDraggedPaletteItem(event); 815 String messageKey = "MSG_ELEMENT_DROP_ACCEPT"; 816 switch(item){ 817 case ELEMENT: 818 if(getElementPanel().getAXIContainer() instanceof Element){ 819 AXIType dt = ((Element)getElementPanel().getAXIContainer()).getType(); 820 if(dt != null){ 821 messageKey = (dt instanceof Datatype) ? "MSG_SIMPLE2COMPLEX_WARNING" : 822 messageKey; 823 } 824 } 825 case ATTRIBUTE: 826 String [] options = new String []{ 827 item.toString().toLowerCase() 828 , getElementPanel().getAXIContainer().getName() 829 }; 830 String str = NbBundle.getMessage(StartTagPanel.class, 831 messageKey, options ); 832 UIUtilities.showBulbMessageFor(str, context, this); 833 return; 834 case CHOICE: 835 case SEQUENCE: 836 case ALL: 837 if(getElementPanel().getAXIContainer().getCompositor() != null){ 838 options = new String []{ 839 getElementPanel().getAXIContainer().getCompositor().getType().getName() 840 , getElementPanel().getAXIContainer().getName() 841 }; 842 str = NbBundle.getMessage(StartTagPanel.class, 844 "MSG_ELEMENT_COMPOSITOR_DROP_REJECT", options ); 845 UIUtilities.showErrorMessageFor(str, context, this); 846 event.rejectDrag(); 847 return; 848 }else{ 849 options = new String []{ 850 item.toString().toLowerCase() 851 , getElementPanel().getAXIContainer().getName() 852 }; 853 str = NbBundle.getMessage(StartTagPanel.class, 854 "MSG_ELEMENT_DROP_ACCEPT", options ); 855 UIUtilities.showBulbMessageFor(str, context, this); 856 } 857 return; 858 default: 859 options = new String []{ 860 getElementPanel().getAXIContainer().getName() 861 }; 862 str = NbBundle.getMessage(StartTagPanel.class, 863 "MSG_ELEMENT_DROP_REJECT", options ); 864 UIUtilities.showErrorMessageFor(str, context, this); 865 event.rejectDrag(); 866 return; 867 } 868 } 869 870 public void dragEnter(DropTargetDragEvent event) { 871 super.dragEnter(event); 872 setDragMode(true); 873 dragOver(event); 874 } 875 876 public void dragExit(DropTargetEvent event) { 877 super.dragExit(event); 878 setDragMode(false); 879 UIUtilities.hideGlassMessage(); 880 } 881 882 private void setDragMode(boolean dragMode){ 883 if(dragMode){ 884 tagNameLabel.setForeground(Color.WHITE); 885 expandButton.setDragMode(true); 886 attributeCollapseButton.setDragMode(true); 887 }else{ 888 tagNameLabel.setForeground(tagNameLabelColor); 889 expandButton.setDragMode(false); 890 attributeCollapseButton.setDragMode(false); 891 } 892 893 } 894 895 public JLabel getEndSlash() { 896 endSlash.setVisible(false); 897 return endSlash; 898 } 899 900 901 public void paintComponent(Graphics g){ 902 Graphics2D g2d = (Graphics2D ) g; 903 super.paintComponent(g2d); 904 905 chainAttributeGroups(g2d); 906 } 907 908 public JPanel getNewAXIContainerPropertiesPanel(){ 909 return new ElementPropertiesPanel((AbstractElement) getElementPanel().getAXIContainer(), context); 910 } 911 912 void showTagNameEditor(boolean firstTime) { 913 this.firstTimeRename = firstTime; 914 if(tagNameLabel != null) 915 tagNameLabel.showEditor(); 916 } 917 918 private void addSelectionListener() { 919 addPropertyChangeListener(new PropertyChangeListener (){ 920 public void propertyChange(PropertyChangeEvent evt) { 921 if(evt.getPropertyName().equals(PROP_SELECTED)){ 922 if(((Boolean )evt.getNewValue()).booleanValue()){ 923 Font font = tagNameLabel.getFont(); 926 font = new Font (font.getName(), Font.BOLD, font.getSize()); 927 tagNameLabel.setFont(font); 928 getElementPanel().repaint(); 929 }else{ 930 939 Font font = tagNameLabel.getFont(); 940 font = new Font (font.getName(), Font.PLAIN, font.getSize()); 941 tagNameLabel.setFont(font); 942 getElementPanel().repaint(); 943 } 944 getEndSlash().setForeground(tagNameLabel.getForeground()); 945 getEndSlash().setFont(tagNameLabel.getFont()); 946 } 947 } 948 }); 949 } 950 951 protected void chainAttributeGroups(Graphics2D g2d) { 952 List <AttributePanel> attrPanels = new ArrayList <AttributePanel>(); 953 for(Component comp: componentList){ 954 if(comp instanceof AttributePanel) 955 attrPanels.add((AttributePanel) comp); 956 } 957 int i = 0; 958 while(i < attrPanels.size()){ 959 if(i+1 < attrPanels.size()){ 960 chainIfNeeded(attrPanels.get(i), attrPanels.get(i+1), g2d); 961 } 962 i++; 963 } 964 } 965 966 public ABEBaseDropPanel getChildUIComponentFor(AXIComponent axiComponent) { 967 if(!(axiComponent instanceof AbstractAttribute)){ 968 return getElementPanel().getChildUIComponentFor(axiComponent); 970 } 971 if(componentList == null) 972 return null; 973 showAttributes(); 974 for(Component comp: componentList){ 975 if(comp instanceof AttributePanel){ 976 if( ((AttributePanel)comp).getUIComponentFor(axiComponent) != null){ 977 return (AttributePanel) comp; 978 } 979 } 980 } 981 return null; 982 } 983 984 985 public List <AttributePanel> getAttributePanels(){ 986 if(componentList == null) 987 return Collections.EMPTY_LIST; 988 showAttributes(); 989 List <AttributePanel> result = new ArrayList <AttributePanel>(); 990 for(Component comp: componentList){ 991 if(comp instanceof AttributePanel){ 992 result.add((AttributePanel) comp); 993 } 994 } 995 return result; 996 } 997 998 private void chainIfNeeded(AttributePanel attr1, AttributePanel attr2, Graphics2D g2d) { 999 final int L_H_BOX_W = 5; 1000 final int R_H_BOX_W = 10; 1001 final int chainWidth = AttributePanel.getAttributePanelHeight(); 1002 1003 AbstractAttribute at1 = (AbstractAttribute) attr1.getAXIComponent(); 1004 AbstractAttribute at2 = (AbstractAttribute) attr2.getAXIComponent(); 1005 1006 if(at1.getContentModel() != at2.getContentModel()) 1007 return; 1008 1009 1010 1011 if(at1.getContentModel() == null){ 1012 return; 1014 } 1015 1016 if(!at1.isShared()){ 1017 return; 1019 } 1020 1021 Color fillColor = InstanceDesignConstants.ATTR_BG_SHARED_COLOR; 1022 1023 if(at1.isReadOnly()) 1024 fillColor = InstanceDesignConstants.ATTR_BG_READONLY_COLOR; 1025 1026 Rectangle r1 = attr1.getBounds(); 1027 Rectangle r2 = attr2.getBounds(); 1028 1029 Color oldColor = g2d.getColor(); 1030 g2d.setColor(fillColor); 1031 1032 if(r1.y == r2.y){ 1033 int x, y, width, height; 1036 x = r1.x + r1.width; 1037 y = r1.y + (r1.height - chainWidth)/2; 1038 width = r2.x - x; 1039 height = chainWidth; 1040 g2d.fillRect( x, y, width, height); 1042 }else{ 1043 1045 int xfudge = 8; 1047 1048 Point pt = new Point (r1.x+r1.width+xfudge, r1.y); 1049 List <Point > plist = UIUtilities.getBrokenTapePoints(pt, r1.y+r1.height-1, 1050 5, 4, true); 1051 Polygon rightTape = new Polygon (); 1052 for(Point tpt: plist){ 1053 rightTape.addPoint(tpt.x, tpt.y); 1054 } 1055 rightTape.addPoint(r1.x+r1.width, r1.y+r1.height-1); 1056 rightTape.addPoint(r1.x+r1.width, r1.y); 1057 g2d.draw(rightTape); 1058 g2d.fill(rightTape); 1059 1060 xfudge -= 3; 1062 pt = new Point (r2.x - xfudge, r2.y); 1063 plist = UIUtilities.getBrokenTapePoints(pt, r2.y+r2.height-1, 1064 5, 4, false); 1065 Polygon leftTape = new Polygon (); 1066 for(Point tpt: plist){ 1067 leftTape.addPoint(tpt.x, tpt.y); 1068 } 1069 leftTape.addPoint(r2.x, r2.y+r2.height-1); 1070 leftTape.addPoint(r2.x, r2.y); 1071 g2d.draw(leftTape); 1072 g2d.fill(leftTape); 1073 } 1074 g2d.setColor(oldColor); 1075 } 1076 1077 public void accept(UIVisitor visitor) { 1078 visitor.visit(this); 1079 } 1080 1081 1082 1086 private InplaceEditableLabel tagNameLabel; 1087 1088 List <Component > componentList; 1089 private Component firstTweener; 1090 1091 private static final int ATTR_HEAD_ROOM_SPACE = 3; 1092 private static final int LABEL_HEAD_ROOM_SPACE = (TagPanel.getTagHeight() / 2) -9; 1093 private static final int INTER_PANEL_SPACE = 2; 1094 1095 private final JLabel endSlash = new JLabel (""); 1096 private JPanel elementPropertiesPanel; 1097 RoundExpandCollapseButton attributeCollapseButton = new RoundExpandCollapseButton("+", false); 1098 boolean attributesAreShown = false; 1099 JLabel attributeCountLabel = new JLabel (); 1100 public static final int NO_OF_FIRST_ROW_ATTRS = 5; 1101 public static final int NO_OF_ATTRS_PER_ROW = 6; 1102 int firstAttrPos = getA() + INTER_PANEL_SPACE + 12; 1103 JLabel hgap = new JLabel (); 1104} 1105 | Popular Tags |