KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > TagPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.abe;
21
22 import java.awt.BasicStroke JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.GradientPaint JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.Graphics2D JavaDoc;
29 import java.awt.Point JavaDoc;
30 import java.awt.Polygon JavaDoc;
31 import java.awt.Rectangle JavaDoc;
32 import java.awt.Shape JavaDoc;
33 import java.awt.Stroke JavaDoc;
34 import java.awt.dnd.DropTargetDragEvent JavaDoc;
35 import java.awt.dnd.DropTargetDropEvent JavaDoc;
36 import java.awt.dnd.DropTargetEvent JavaDoc;
37 import java.awt.event.ComponentEvent JavaDoc;
38 import java.awt.event.ComponentListener JavaDoc;
39 import java.awt.event.ContainerEvent JavaDoc;
40 import java.awt.event.ContainerListener JavaDoc;
41 import java.awt.event.MouseAdapter JavaDoc;
42 import java.awt.event.MouseEvent JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import javax.swing.JLabel JavaDoc;
45 import javax.swing.SwingUtilities JavaDoc;
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 /**
53  *
54  *
55  * @author Todd Fast, todd.fast@sun.com
56  */

57 public abstract class TagPanel extends ABEBaseDropPanel implements ComponentListener JavaDoc{
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 JavaDoc tagName;
68     private boolean readonlyTag;
69     
70     private static final int TAG_HEIGHT = 29;
71     
72     private int XFUDGE = 3;
73     
74     protected ArrayList JavaDoc<Component JavaDoc> excludePaintComponentList = new ArrayList JavaDoc<Component JavaDoc>();
75     
76     
77     /**
78      *
79      *
80      */

81     public TagPanel(ElementPanel elementPanel, InstanceUIContext context) {
82         super(context);
83         this.elementPanel=elementPanel;
84         initialize();
85     }
86     
87     /**
88      *
89      *
90      */

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 JavaDoc(100,height));
97 // setMinimumSize(new Dimension(100,height));
98
initMouseListener();
99         addContainerListener(new ContainerListener JavaDoc() {
100             public void componentAdded(ContainerEvent JavaDoc e) {
101                 e.getComponent().addComponentListener(TagPanel.this);
102                 forceSizeRecalculate();
103             }
104             public void componentRemoved(ContainerEvent JavaDoc 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 JavaDoc() {
119             public void mouseReleased(MouseEvent JavaDoc e) {
120                 mouseClickedActionHandler(e, true);
121             }
122             public void mouseClicked(MouseEvent JavaDoc e) {
123                 mouseClickedActionHandler(e, false);
124             }
125             public void mousePressed(MouseEvent JavaDoc e) {
126                 mouseClickedActionHandler(e, true);
127             }
128         });
129     }
130     
131     protected void mouseClickedActionHandler(MouseEvent JavaDoc 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             //the tag is selected
140
if(e.isControlDown())
141                 context.getComponentSelectionManager().addToSelectedComponents(this);
142             else
143                 context.getComponentSelectionManager().setSelectedComponent(this);
144         }
145     }
146     
147     
148     
149     ////////////////////////////////////////////////////////////////////////////
150
// Accessors and mutators
151
////////////////////////////////////////////////////////////////////////////
152

153     /**
154      *
155      *
156      */

157     public ElementPanel getElementPanel() {
158         return elementPanel;
159     }
160     
161     
162     /**
163      *
164      *
165      */

166     /*pkg*/ boolean isHover() {
167         return hover;
168     }
169     
170     
171     /**
172      *
173      *
174      */

175     /*pkg*/ void setHover(boolean value) {
176         boolean oldHover=hover;
177         if (oldHover!=value) {
178             hover=value;
179             repaint();
180         }
181     }
182     
183     
184     /**
185      *
186      *
187      */

188     public String JavaDoc getTagName() {
189         return tagName;
190     }
191     
192     
193     /**
194      *
195      *
196      */

197     public void setTagName(String JavaDoc value) {
198         tagName=value;
199     }
200     
201     
202     Color JavaDoc fillTopColor = InstanceDesignConstants.TAG_BG_NORMAL_TOP_GRADIENT_COLOR;
203     Color JavaDoc fillBottomColor=InstanceDesignConstants.TAG_BG_NORMAL_BOTTOM_GRADIENT_COLOR;
204     
205     ////////////////////////////////////////////////////////////////////////////
206
// Paint methods
207
////////////////////////////////////////////////////////////////////////////
208

209     /**
210      *
211      *
212      */

213     public void paintComponent(Graphics JavaDoc g) {
214         Graphics2D JavaDoc g2d=(Graphics2D JavaDoc)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         //set proper colors
224
if (isHover()) {
225             fillTopColor=InstanceDesignConstants.DARK_BLUE;//Color.WHITE;
226
fillBottomColor=InstanceDesignConstants.DARK_BLUE;//InstanceDesignConstants.XP_ORANGE;
227
}else if(context.getComponentSelectionManager().isSelected(this)){
228             selected = true;
229         }
230         
231         Shape JavaDoc tag = getTagShape();
232         Rectangle JavaDoc tagBounds = tag.getBounds();
233         
234         // Draw shadow
235
g.translate(X_SHADOW_OFFSET,Y_SHADOW_OFFSET);
236         g2d.setColor(new Color JavaDoc(240,240,240));
237         g2d.fill(tag);
238         
239         g.translate(-1,-1);
240         g2d.setColor(new Color JavaDoc(224,224,224));
241         g2d.fill(tag);
242         
243         g.translate(-1,-1);
244         g2d.setColor(new Color JavaDoc(192,192,192));
245         g2d.fill(tag);
246         
247         // Draw main tag body
248

249         
250         g.translate(2,2);
251         g.translate(-X_SHADOW_OFFSET,-Y_SHADOW_OFFSET);
252         // fill tag
253
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 JavaDoc fill = new GradientPaint JavaDoc(x1, y1, fillTopColor, x2, y2, fillBottomColor, false);
260             g2d.setPaint(fill);
261             g2d.fill(tag);
262         }
263         
264         //draw row separation lines
265
int lineY = (int) tagBounds.getY();
266         g2d.setColor(fillBottomColor);
267         Stroke JavaDoc stroke = new BasicStroke JavaDoc(2f);
268         Stroke JavaDoc 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         // draw Outline
278
g2d.setColor(InstanceDesignConstants.TAG_OUTLINE_COLOR);
279         oldstroke = null;
280         if(selected){
281             oldstroke = g2d.getStroke();
282             stroke = new BasicStroke JavaDoc(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     /**
300      *
301      *
302      */

303     protected int getA() {
304         // Height could be zero before the component is laid out, so use
305
// the preferred height (which should be the final height)
306
/*int h = getHeight();
307         if (h == 0)
308             h = getPreferredSize().height;*/

309         int h = TAG_HEIGHT;
310         return (h - BOTTOM_PAD)/2;
311     }
312     
313     
314     /**
315      *
316      *
317      */

318    /* protected Shape getTagShape() {
319         Graphics g=getGraphics();
320         return getTagShape();
321     }*/

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     /**
332      *
333      *
334      */

335     private Shape JavaDoc _getTagShape(Graphics JavaDoc 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(); // ym-yo;
341
int w = getAbsoluteWidth();//(int) getPreferredSize().getWidth() + getA()) - 2*xo;
342

343         tagNosePointOffset = ym;
344         
345         /*
346         Starting from the left vertical middle point and moving clockwise:
347          
348              /-----------------\
349             / \
350             \ /
351              \-----------------/
352          
353             xo, ym
354             xo+a, yo
355             w-xo-a, yo
356             w-xo, ym
357             w-xo-a, h-yo
358             xo+a, h-yo
359          */

360         
361         return new Polygon JavaDoc(
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 JavaDoc leftBottomPoint;
369     private Point JavaDoc rightBottomPoint;
370     private Point JavaDoc rightNosePoint;
371     private Point JavaDoc leftNosePoint;
372     public Shape JavaDoc getTagShape() {
373         Rectangle JavaDoc rect = getChildrenAreaUnion();
374         //Rectangle realRect = g.getClipBounds();
375
int h = TAG_HEIGHT - BOTTOM_PAD;
376         int a = getA(); // ym-yo;
377
int x = rect.x;//rect.x;
378
int y = rect.y - 2;
379         int H = this.getHeight() - BOTTOM_PAD;
380         int W = rect.width;
381         tagNosePointOffset = h/2;
382         /*
383         Starting from the left vertical middle point and moving clockwise:
384          
385          (x,y) /--------------------| (x+W, y)
386   (x-a,y+h/2) / |
387                  \ |
388 (x, y+h),(x, y+h) \ |
389                   | \----| (x+w1, y+H-h), (x+W, y+H-h)
390                   | \
391                   | / (x+w1+a, y+H-(h/2))
392           (x, y+H)|---------------/((x+w1, y+H);
393          
394          
395          */

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 JavaDoc(x, y+h);
407         rightBottomPoint = new Point JavaDoc(x+w1, y+H);
408         rightNosePoint = new Point JavaDoc(x+w1+a, y+H-(h/2));
409         leftNosePoint = new Point JavaDoc(x-a, y+h/2);
410         return new Polygon JavaDoc(
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 JavaDoc 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 JavaDoc getChildrenAreaUnion(){
430         Component JavaDoc children[] = getComponents();
431         if(children.length <= 0)
432             return null;
433         Rectangle JavaDoc current;
434         current = children[1].getBounds();
435         for(Component JavaDoc child: children){
436             if(excludePaintComponentList.contains(child))// || !child.isVisible())
437
continue;
438             current = SwingUtilities.computeUnion(current.x, current.y, current.width,
439                     current.height, child.getBounds());
440         }
441         return current;
442     }
443     
444     
445     //sub-class to override
446
public int getInterComponentSpacing(){
447         return 0;
448     }
449     
450     //Following set of methods needed for the tag size calculation and horizontal bar display logic
451
public Dimension JavaDoc _getPreferredSize() {
452         int width = 0;
453         int maxWidth = 0;
454         int propsWidth = 0;
455         for(Component JavaDoc child: this.getComponents()){
456             if(!child.isVisible())
457                 continue;
458             Dimension JavaDoc 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 JavaDoc(width, ((TAG_HEIGHT - BOTTOM_PAD) * getRowCount())+BOTTOM_PAD );
474     }
475     
476     boolean recalculateRequired = true;
477     Dimension JavaDoc myDim;
478     public Dimension JavaDoc getPreferredSize() {
479         synchronized(this){
480             if(recalculateRequired){
481                 myDim = _getPreferredSize();
482                 recalculateRequired = false;
483             }
484             return myDim;
485         }
486     }
487     
488     public Dimension JavaDoc getMinimumSize() {
489         return getPreferredSize();
490     }
491     
492     public Dimension JavaDoc getMaximumSize() {
493         return getPreferredSize();
494     }
495     
496     
497     public static int getTagHeight(){
498         return TAG_HEIGHT;
499     }
500     
501     
502     //DND events
503
public void drop(DropTargetDropEvent JavaDoc event) {
504         setHover(false);
505     }
506     
507     public void dragExit(DropTargetEvent JavaDoc event) {
508         setHover(false);
509     }
510     
511     public void dragOver(DropTargetDragEvent JavaDoc event) {
512         setHover(true);
513     }
514     
515     public void dragEnter(DropTargetDragEvent JavaDoc 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 JavaDoc g2d) {
540         AXIContainer acon = getElementPanel().getAXIContainer();
541         //ContentModel cm = acon.getContentModel();
542
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 JavaDoc g2d) {
557     }
558     
559     
560     public Point JavaDoc getLeftBottomPoint() {
561         return leftBottomPoint;
562     }
563     
564     public Point JavaDoc getRightBottomPoint() {
565         return rightBottomPoint;
566     }
567     
568     public Point JavaDoc getRightNosePoint() {
569         return rightNosePoint;
570     }
571     
572     public Point JavaDoc getLeftNosePoint(){
573         return leftNosePoint;
574     }
575     
576     public JLabel JavaDoc getEndSlash() {
577         return null;
578     }
579     
580     public void componentShown(ComponentEvent JavaDoc e) {
581         forceSizeRecalculate();
582     }
583     
584     public void componentResized(ComponentEvent JavaDoc e) {
585         forceSizeRecalculate();
586     }
587     
588     public void componentMoved(ComponentEvent JavaDoc e) {
589         forceSizeRecalculate();
590     }
591     
592     public void componentHidden(ComponentEvent JavaDoc e) {
593         forceSizeRecalculate();
594     }
595     
596     public void forceSizeRecalculate(){
597         recalculateRequired = true;
598     }
599 }
600
Popular Tags