1 19 20 package org.netbeans.modules.xml.schema.abe; 21 22 import java.awt.BasicStroke ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Dimension ; 26 import java.awt.GradientPaint ; 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.Shape ; 33 import java.awt.Stroke ; 34 import java.awt.dnd.DropTargetDragEvent ; 35 import java.awt.dnd.DropTargetDropEvent ; 36 import java.awt.dnd.DropTargetEvent ; 37 import java.awt.event.ComponentEvent ; 38 import java.awt.event.ComponentListener ; 39 import java.awt.event.ContainerEvent ; 40 import java.awt.event.ContainerListener ; 41 import java.awt.event.MouseAdapter ; 42 import java.awt.event.MouseEvent ; 43 import java.util.ArrayList ; 44 import javax.swing.JLabel ; 45 import javax.swing.SwingUtilities ; 46 import org.netbeans.modules.xml.axi.AXIComponent; 47 import org.netbeans.modules.xml.axi.AXIContainer; 48 import org.netbeans.modules.xml.axi.ContentModel; 49 import org.netbeans.modules.xml.axi.Element; 50 import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode; 51 52 57 public abstract class TagPanel extends ABEBaseDropPanel implements ComponentListener { 58 59 private static final int X_OFFSET = 0; 60 private static final int Y_OFFSET = 0; 61 private static final int X_SHADOW_OFFSET = 4; 62 private static final int Y_SHADOW_OFFSET = 4; 63 public static final int BOTTOM_PAD = Y_SHADOW_OFFSET+1; 64 65 private ElementPanel elementPanel; 66 private boolean hover; 67 private String tagName; 68 private boolean readonlyTag; 69 70 private static final int TAG_HEIGHT = 29; 71 72 private int XFUDGE = 3; 73 74 protected ArrayList <Component > excludePaintComponentList = new ArrayList <Component >(); 75 76 77 81 public TagPanel(ElementPanel elementPanel, InstanceUIContext context) { 82 super(context); 83 this.elementPanel=elementPanel; 84 initialize(); 85 } 86 87 91 private void initialize() { 92 setOpaque(false); 93 setBackground(Color.WHITE); 94 95 int height=InstanceDesignConstants.TAG_FONT.getSize()*2+BOTTOM_PAD; 96 setPreferredSize(new Dimension (100,height)); 97 initMouseListener(); 99 addContainerListener(new ContainerListener () { 100 public void componentAdded(ContainerEvent e) { 101 e.getComponent().addComponentListener(TagPanel.this); 102 forceSizeRecalculate(); 103 } 104 public void componentRemoved(ContainerEvent e) { 105 e.getComponent().removeComponentListener(TagPanel.this); 106 forceSizeRecalculate(); 107 } 108 }); 109 } 110 111 112 public void removeElement(){ 113 if(getElementPanel().getParent() != null) 114 getElementPanel().removeElement(); 115 } 116 117 protected void initMouseListener(){ 118 addMouseListener(new MouseAdapter () { 119 public void mouseReleased(MouseEvent e) { 120 mouseClickedActionHandler(e, true); 121 } 122 public void mouseClicked(MouseEvent e) { 123 mouseClickedActionHandler(e, false); 124 } 125 public void mousePressed(MouseEvent e) { 126 mouseClickedActionHandler(e, true); 127 } 128 }); 129 } 130 131 protected void mouseClickedActionHandler(MouseEvent e, boolean handelPopupOnly){ 132 if(e.getClickCount() == 1){ 133 if(e.isPopupTrigger()){ 134 context.getMultiComponentActionManager().showPopupMenu(e, this); 135 return; 136 } 137 if(handelPopupOnly) 138 return; 139 if(e.isControlDown()) 141 context.getComponentSelectionManager().addToSelectedComponents(this); 142 else 143 context.getComponentSelectionManager().setSelectedComponent(this); 144 } 145 } 146 147 148 149 153 157 public ElementPanel getElementPanel() { 158 return elementPanel; 159 } 160 161 162 166 boolean isHover() { 167 return hover; 168 } 169 170 171 175 void setHover(boolean value) { 176 boolean oldHover=hover; 177 if (oldHover!=value) { 178 hover=value; 179 repaint(); 180 } 181 } 182 183 184 188 public String getTagName() { 189 return tagName; 190 } 191 192 193 197 public void setTagName(String value) { 198 tagName=value; 199 } 200 201 202 Color fillTopColor = InstanceDesignConstants.TAG_BG_NORMAL_TOP_GRADIENT_COLOR; 203 Color fillBottomColor=InstanceDesignConstants.TAG_BG_NORMAL_BOTTOM_GRADIENT_COLOR; 204 205 209 213 public void paintComponent(Graphics g) { 214 Graphics2D g2d=(Graphics2D )g; 215 216 super.paintComponent(g2d); 217 218 fillTopColor = InstanceDesignConstants.TAG_BG_NORMAL_TOP_GRADIENT_COLOR; 219 fillBottomColor=InstanceDesignConstants.TAG_BG_NORMAL_BOTTOM_GRADIENT_COLOR; 220 221 setDrawParamsForSharedElement(g2d); 222 boolean selected = false; 223 if (isHover()) { 225 fillTopColor=InstanceDesignConstants.DARK_BLUE; fillBottomColor=InstanceDesignConstants.DARK_BLUE; }else if(context.getComponentSelectionManager().isSelected(this)){ 228 selected = true; 229 } 230 231 Shape tag = getTagShape(); 232 Rectangle tagBounds = tag.getBounds(); 233 234 g.translate(X_SHADOW_OFFSET,Y_SHADOW_OFFSET); 236 g2d.setColor(new Color (240,240,240)); 237 g2d.fill(tag); 238 239 g.translate(-1,-1); 240 g2d.setColor(new Color (224,224,224)); 241 g2d.fill(tag); 242 243 g.translate(-1,-1); 244 g2d.setColor(new Color (192,192,192)); 245 g2d.fill(tag); 246 247 249 250 g.translate(2,2); 251 g.translate(-X_SHADOW_OFFSET,-Y_SHADOW_OFFSET); 252 float x1, y1, x2, y2; 254 x1 = x2 = (float) tagBounds.getX(); 255 y2 = (float) tagBounds.getY(); 256 for(int i = 1; i<= getRowCount(); i++){ 257 y1 = y2; 258 y2 = y2 + getTagHeight() - BOTTOM_PAD; 259 GradientPaint fill = new GradientPaint (x1, y1, fillTopColor, x2, y2, fillBottomColor, false); 260 g2d.setPaint(fill); 261 g2d.fill(tag); 262 } 263 264 int lineY = (int) tagBounds.getY(); 266 g2d.setColor(fillBottomColor); 267 Stroke stroke = new BasicStroke (2f); 268 Stroke oldstroke = null; 269 oldstroke = g2d.getStroke(); 270 g2d.setStroke(stroke); 271 for(int i = 1; i< getRowCount(); i++){ 272 lineY = lineY + getTagHeight() - BOTTOM_PAD; 273 g2d.drawLine(extremeLeftX+1, lineY, extremeRightX-1, lineY); 274 } 275 g2d.setStroke(oldstroke); 276 277 g2d.setColor(InstanceDesignConstants.TAG_OUTLINE_COLOR); 279 oldstroke = null; 280 if(selected){ 281 oldstroke = g2d.getStroke(); 282 stroke = new BasicStroke (2f); 283 g2d.setStroke(stroke); 284 g2d.setColor(InstanceDesignConstants.XP_ORANGE); 285 } 286 287 g2d.draw(tag); 288 289 if(selected && (oldstroke != null)){ 290 g2d.setStroke(oldstroke); 291 } 292 293 294 295 resetDrawParamsForSharedElement(g2d); 296 } 297 298 299 303 protected int getA() { 304 309 int h = TAG_HEIGHT; 310 return (h - BOTTOM_PAD)/2; 311 } 312 313 314 318 322 323 int tagNosePointOffset; 324 public int getTagNosePointOffset(){ 325 int h = TAG_HEIGHT - BOTTOM_PAD; 326 tagNosePointOffset = h/2; 327 return tagNosePointOffset; 328 } 329 330 331 335 private Shape _getTagShape(Graphics g) { 336 int h = TAG_HEIGHT - BOTTOM_PAD; 337 int xo = X_OFFSET; 338 int yo = Y_OFFSET; 339 int ym = h/2; 340 int a = getA(); int w = getAbsoluteWidth(); 343 tagNosePointOffset = ym; 344 345 360 361 return new Polygon ( 362 new int[] { xo, xo+a, w-xo-a, w-xo, w-xo-a, xo+a }, 363 new int[] { ym, yo, yo, ym, h-yo, h-yo }, 6); 364 } 365 366 private int extremeLeftX; 367 private int extremeRightX; 368 private Point leftBottomPoint; 369 private Point rightBottomPoint; 370 private Point rightNosePoint; 371 private Point leftNosePoint; 372 public Shape getTagShape() { 373 Rectangle rect = getChildrenAreaUnion(); 374 int h = TAG_HEIGHT - BOTTOM_PAD; 376 int a = getA(); int x = rect.x; int y = rect.y - 2; 379 int H = this.getHeight() - BOTTOM_PAD; 380 int W = rect.width; 381 tagNosePointOffset = h/2; 382 396 397 if(getRowCount() > 1) 398 W += 8; 399 400 int w1 = W; 401 if(getEndSlash() != null){ 402 w1 = getEndSlash().getBounds().x; 403 } 404 extremeLeftX = x; 405 extremeRightX = x+W; 406 leftBottomPoint = new Point (x, y+h); 407 rightBottomPoint = new Point (x+w1, y+H); 408 rightNosePoint = new Point (x+w1+a, y+H-(h/2)); 409 leftNosePoint = new Point (x-a, y+h/2); 410 return new Polygon ( 411 new int[] { x-a , x, x+W, x+W , x+w1 , x+w1+a , x+w1, x , x , x }, 412 new int[] { y+h/2, y, y , y+H-h , y+H-h, y+H-(h/2), y+H , y+H , y+h, y+h }, 10); 413 } 414 415 private int getAbsoluteWidth(){ 416 int width = 0; 417 int finalX = 0; 418 for(Component child: getComponents()){ 419 int x; 420 x = child.getBounds().x; 421 x += child.getPreferredSize().width; 422 if(x > finalX) 423 finalX = x; 424 } 425 finalX += getA(); 426 return finalX; 427 } 428 429 private Rectangle getChildrenAreaUnion(){ 430 Component children[] = getComponents(); 431 if(children.length <= 0) 432 return null; 433 Rectangle current; 434 current = children[1].getBounds(); 435 for(Component child: children){ 436 if(excludePaintComponentList.contains(child)) continue; 438 current = SwingUtilities.computeUnion(current.x, current.y, current.width, 439 current.height, child.getBounds()); 440 } 441 return current; 442 } 443 444 445 public int getInterComponentSpacing(){ 447 return 0; 448 } 449 450 public Dimension _getPreferredSize() { 452 int width = 0; 453 int maxWidth = 0; 454 int propsWidth = 0; 455 for(Component child: this.getComponents()){ 456 if(!child.isVisible()) 457 continue; 458 Dimension dim = child.getPreferredSize(); 459 width += dim.width + getInterComponentSpacing(); 460 maxWidth = maxWidth > dim.width ? maxWidth : dim.width; 461 if(child instanceof ElementPropertiesPanel) 462 propsWidth = dim.width; 463 } 464 if(getRowCount() > 1){ 465 width = maxWidth * StartTagPanel.NO_OF_ATTRS_PER_ROW + 466 getInterComponentSpacing() * StartTagPanel.NO_OF_ATTRS_PER_ROW * 2 467 + getA() *2 + propsWidth + 25; 468 } else{ 469 width += maxWidth + getA() * 2 + 20; 470 width += (getElementPanel().getAXIContainer().getName().length() < 3) ? 471 10 : 0; 472 } 473 return new Dimension (width, ((TAG_HEIGHT - BOTTOM_PAD) * getRowCount())+BOTTOM_PAD ); 474 } 475 476 boolean recalculateRequired = true; 477 Dimension myDim; 478 public Dimension getPreferredSize() { 479 synchronized(this){ 480 if(recalculateRequired){ 481 myDim = _getPreferredSize(); 482 recalculateRequired = false; 483 } 484 return myDim; 485 } 486 } 487 488 public Dimension getMinimumSize() { 489 return getPreferredSize(); 490 } 491 492 public Dimension getMaximumSize() { 493 return getPreferredSize(); 494 } 495 496 497 public static int getTagHeight(){ 498 return TAG_HEIGHT; 499 } 500 501 502 public void drop(DropTargetDropEvent event) { 504 setHover(false); 505 } 506 507 public void dragExit(DropTargetEvent event) { 508 setHover(false); 509 } 510 511 public void dragOver(DropTargetDragEvent event) { 512 setHover(true); 513 } 514 515 public void dragEnter(DropTargetDragEvent event) { 516 setHover(true); 517 } 518 519 public ABEAbstractNode getNBNode(){ 520 return getElementPanel().getNBNode(); 521 } 522 523 public AXIComponent getAXIComponent() { 524 return getElementPanel().getAXIContainer(); 525 } 526 527 public int getRowCount() { 528 return 1; 529 } 530 531 public boolean isReadonlyTag() { 532 return readonlyTag; 533 } 534 535 public void setReadonlyTag(boolean readonlyTag) { 536 this.readonlyTag = readonlyTag; 537 } 538 539 private void setDrawParamsForSharedElement(Graphics2D g2d) { 540 AXIContainer acon = getElementPanel().getAXIContainer(); 541 boolean reference = false; 543 if(acon instanceof Element) 544 reference = ((Element) acon).isReference(); 545 if(acon.isShared() || reference) { 546 fillTopColor = InstanceDesignConstants.TAG_BG_SHARED_TOP_GRADIENT_COLOR; 547 fillBottomColor = InstanceDesignConstants.TAG_BG_SHARED_BOTTOM_GRADIENT_COLOR; 548 } 549 if(acon.isReadOnly()){ 550 fillTopColor = InstanceDesignConstants.TAG_BG_READONLY_TOP_GRADIENT_COLOR; 551 fillBottomColor = InstanceDesignConstants.TAG_BG_READONLY_BOTTOM_GRADIENT_COLOR; 552 } 553 554 } 555 556 private void resetDrawParamsForSharedElement(Graphics2D g2d) { 557 } 558 559 560 public Point getLeftBottomPoint() { 561 return leftBottomPoint; 562 } 563 564 public Point getRightBottomPoint() { 565 return rightBottomPoint; 566 } 567 568 public Point getRightNosePoint() { 569 return rightNosePoint; 570 } 571 572 public Point getLeftNosePoint(){ 573 return leftNosePoint; 574 } 575 576 public JLabel getEndSlash() { 577 return null; 578 } 579 580 public void componentShown(ComponentEvent e) { 581 forceSizeRecalculate(); 582 } 583 584 public void componentResized(ComponentEvent e) { 585 forceSizeRecalculate(); 586 } 587 588 public void componentMoved(ComponentEvent e) { 589 forceSizeRecalculate(); 590 } 591 592 public void componentHidden(ComponentEvent e) { 593 forceSizeRecalculate(); 594 } 595 596 public void forceSizeRecalculate(){ 597 recalculateRequired = true; 598 } 599 } 600 | Popular Tags |