KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.Component JavaDoc;
26 import java.awt.Cursor JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.awt.Graphics JavaDoc;
29 import java.awt.Graphics2D JavaDoc;
30 import java.awt.Point JavaDoc;
31 import java.awt.Rectangle JavaDoc;
32 import java.awt.Stroke JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.event.ActionListener JavaDoc;
35 import java.awt.event.ComponentAdapter JavaDoc;
36 import java.awt.event.ComponentEvent JavaDoc;
37 import java.beans.PropertyChangeEvent JavaDoc;
38 import java.beans.PropertyChangeListener JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.List JavaDoc;
42 import javax.swing.Box JavaDoc;
43 import javax.swing.JPanel JavaDoc;
44 import javax.swing.SpringLayout 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.AXIModel;
49 import org.netbeans.modules.xml.axi.AbstractAttribute;
50 import org.netbeans.modules.xml.axi.AbstractElement;
51 import org.netbeans.modules.xml.axi.AnyElement;
52 import org.netbeans.modules.xml.axi.Compositor;
53 import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode;
54 import org.netbeans.modules.xml.schema.abe.nodes.AnyElementNode;
55 import org.netbeans.modules.xml.schema.abe.nodes.ElementNode;
56 import org.openide.nodes.Node;
57
58 public class ElementPanel extends ABEBaseDropPanel{
59     private static final long serialVersionUID = 7526472295622776147L;
60     
61     public ElementPanel(InstanceUIContext context,
62             AXIContainer element, ContainerPanel parentCompositorPanel) {
63         super(context);
64         this.parentCompositorPanel=parentCompositorPanel;
65         this.axiContainer=element;
66         //setBorder(new LineBorder(Color.RED));
67
initialize();
68         
69         _setName(element.getName());
70         
71         addElementListener();
72         
73         makeNBNode();
74     }
75     
76     
77     private void initialize() {
78         initButton();
79         setLayout(new BorderLayout JavaDoc());
80         setOpaque(false);
81         
82         startTag = getNewStartTagPanel(this, context);
83         add(startTag,BorderLayout.NORTH);
84         
85         //dont show expand button if not needed
86
if(axiContainer.getCompositor() == null){
87             expandButton.setVisible(false);
88         }
89     }
90     
91     private void addElementListener(){
92         axiContainer.addPropertyChangeListener(new ModelEventMediator(this, axiContainer){
93             public void _propertyChange(PropertyChangeEvent JavaDoc evt) {
94                 if(evt.getPropertyName().equals(Compositor.PROP_COMPOSITOR)){
95                     if(evt.getPropertyName().equals(Compositor.PROP_COMPOSITOR)){
96                         //a compositor event
97
if((evt.getOldValue() == null) && (evt.getNewValue() != null)){
98                             //new Compositor added
99
addCompositor((Compositor) evt.getNewValue());
100                         }else if((evt.getNewValue() == null) && (evt.getOldValue() != null)){
101                             //old Compositor removed
102
removeCompositor((Compositor) evt.getOldValue());
103                         }
104                     }
105                 }else if(evt.getPropertyName().equals(
106                         org.netbeans.modules.xml.axi.Element.PROP_TYPE)){
107                     startTag.updateAttributes();
108                 }
109             }
110         });
111     }
112     
113     private void initButton(){
114         expandButton = new ExpandCollapseButton("+");
115         expandButton.addActionListener(new ActionListener JavaDoc() {
116             public void actionPerformed(ActionEvent JavaDoc e) {
117                 handleExpandOrCollapse();
118             }
119         });
120     }
121     
122     
123     private void handleExpandOrCollapse(){
124         if(!expandButton.isExpanded()){
125             //expand
126
expandChild();
127         }else{
128             //collapse
129
collapseChild();
130         }
131     }
132     
133     public void expandChild(){
134         if(expandButton.isExpanded())
135             expandButton.setText("-");
136         setCursor(new Cursor JavaDoc(Cursor.WAIT_CURSOR));
137         try{
138             if(fadeinPanel != null){
139                 //children already added just show
140
fadeinPanel.setVisible(true);
141             }else{
142                 //children not added create them and then show
143
createChild();
144                 setExpanded(true);
145             }
146         }finally{
147             //always reset back
148
setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
149         }
150         revalidate();
151         repaint();
152     }
153     
154     public void collapseChild(){
155         if(fadeinPanel != null){
156             //children already added just hide
157
fadeinPanel.setVisible(false);
158             revalidate();
159             repaint();
160         }
161     }
162     
163     private void createChild(){
164         for(AXIComponent axiComp: axiContainer.getChildren()){
165             if(axiComp instanceof Compositor){
166                 Compositor compositor = (Compositor) axiComp;
167                 CompositorPanel child = new CompositorPanel(getUIContext(),
168                         compositor, this);
169                 append(child);
170             }
171         }
172     }
173     
174     Component JavaDoc lastComponent = null;
175     SpringLayout JavaDoc childCompositorPanelLayout;
176     public void append(ContainerPanel child) {
177         if (childCompositorPanel==null) {
178             fadeinPanel = new JPanel JavaDoc();
179             expandButton.setWatchForComponent(fadeinPanel);
180             fadeinPanel.setOpaque(false);
181             fadeinPanel.setLayout(new BorderLayout JavaDoc());
182             //add a horizontal strut for compensating the expand collapse button
183
fadeinPanel.add(Box.createHorizontalStrut((int)
184             expandButton.getPreferredSize().getWidth()), BorderLayout.WEST);
185             childCompositorPanel = new AutoSizingPanel(context);
186             childCompositorPanel.setVerticalScaling(true);
187             childCompositorPanel.setOpaque(false);
188             childCompositorPanelLayout = new SpringLayout JavaDoc();
189             childCompositorPanel.setLayout(childCompositorPanelLayout);
190             fadeinPanel.add(childCompositorPanel, BorderLayout.CENTER);
191             
192             childCompositorPanel.add(child);
193             childCompositorPanelLayout.putConstraint(SpringLayout.NORTH, child, 0,
194                     SpringLayout.NORTH, childCompositorPanel);
195             childCompositorPanelLayout.putConstraint(SpringLayout.WEST, child, 0,
196                     SpringLayout.WEST, childCompositorPanel);
197             
198             add(fadeinPanel,BorderLayout.CENTER);
199             lastComponent = child;
200         }else{
201             childCompositorPanel.add(child);
202             childCompositorPanelLayout.putConstraint(SpringLayout.NORTH, child, 0,
203                     SpringLayout.SOUTH, lastComponent);
204             childCompositorPanelLayout.putConstraint(SpringLayout.WEST, child, 0,
205                     SpringLayout.WEST, childCompositorPanel);
206             lastComponent = child;
207         }
208     }
209     
210     private void removeChild(CompositorPanel component){
211         if(childCompositorPanel != null){
212             childCompositorPanel.remove(component);
213             if(childCompositorPanel.getComponents().length <= 0){
214                 remove(fadeinPanel);
215                 expandButton.setVisible(false);
216                 childCompositorPanel = null;
217             }
218             revalidate();
219             repaint();
220         }
221     }
222     
223     
224     
225     private void addCompositor(Compositor compositor){
226         createChild();
227         expandButton.setVisible(true);
228         setExpanded(false);
229     }
230     
231     private void removeCompositor(Compositor compositor){
232         if(childCompositorPanel == null)
233             return;
234         for(Component JavaDoc comp: childCompositorPanel.getComponents()){
235             CompositorPanel cp = (CompositorPanel) comp;
236             if(cp.getAXIParent() == compositor){
237                 removeChild(cp);
238             }
239         }
240     }
241     
242     
243     public void removeElement() {
244         AXIContainer axiContainer = getAXIContainer();
245         AXIModel model = axiContainer.getModel();
246         if(model != null){
247             model.startTransaction();
248             try{
249                 getAXIContainer().getParent().removeChild(getAXIContainer());
250             }finally{
251                 model.endTransaction();
252             }
253         }
254     }
255     
256     public boolean isExpanded() {
257         return fadeinPanel !=null ? fadeinPanel.isVisible() : false;
258     }
259     
260     
261     public void setExpanded(boolean value) {
262         if (fadeinPanel != null) {
263             fadeinPanel.setVisible(value);
264         }
265     }
266     
267     public String JavaDoc getName() {
268         return name;
269     }
270     
271     
272     public void setName(String JavaDoc value) {
273         _setName(value);
274     }
275     
276     
277     private void _setName(String JavaDoc value) {
278         name=value;
279         repaint();
280     }
281     
282     
283     public AXIContainer getAXIContainer() {
284         return axiContainer;
285     }
286     
287     public String JavaDoc getAnnotation() {
288         return annotation;
289     }
290     
291     public void setAnnotation(String JavaDoc value) {
292         _setAnnotation(value);
293     }
294     
295     private void _setAnnotation(String JavaDoc value) {
296         annotation=value;
297     }
298     
299     public TagPanel getStartTagPanel() {
300         return startTag;
301     }
302     
303     
304     TweenerPanel getTweenerPanel() {
305         return tweenerPanel;
306     }
307     
308     
309     //Following set of methods needed for the tag size calculation and horizontal bar display logic
310
public Dimension JavaDoc getPreferredSize() {
311         return getMinimumSize();
312     }
313     
314     public Dimension JavaDoc getMinimumSize() {
315         int width = 0;
316         int height = 0;
317         for(Component JavaDoc child: this.getComponents()){
318             if(!child.isVisible())
319                 continue;
320             Dimension JavaDoc dim = child.getPreferredSize();
321             height += dim.height;// + getInterComponentSpacing();
322
int thisW = dim.width ;
323             width = width < thisW ? thisW : width;
324         }
325         if( (fadeinPanel != null) && fadeinPanel.isVisible() ){
326             height +=4;
327             width += 10;
328         }
329         return new Dimension JavaDoc(width, height);
330     }
331     
332     
333     List JavaDoc<String JavaDoc> getAttributes() {
334         return attributes;
335     }
336     
337     
338     void addAttribute(String JavaDoc value) {
339         attributes.add(value);
340         repaint();
341     }
342     
343     public void paintComponent(Graphics JavaDoc g){
344         final Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g;
345         super.paintComponent(g2d);
346 // SwingUtilities.invokeLater(new Runnable(){
347
// public void run() {
348
if(context.getComponentSelectionManager().getSelectedComponentList().contains(startTag))
349             drawBoundingBox(g2d);
350 // }
351
// });
352

353     }
354     
355     public InstanceUIContext getUIContext() {
356         return context;
357     }
358     
359     void showNameEditor(boolean firstTime) {
360         startTag.showTagNameEditor(firstTime);
361     }
362     
363     protected void makeNBNode() {
364         if(getAXIContainer() instanceof AnyElement)
365             elementNode = new AnyElementNode((AnyElement) getAXIContainer(), context);
366         else
367             elementNode = new ElementNode((AbstractElement) getAXIContainer(), context);
368         /*if(getAXIContainer().isReadOnly(getAXIContainer().getOriginal().getModel()))
369             ((ABEAbstractNode)elementNode).setReadOnly(true);*/

370     }
371     
372     public ABEAbstractNode getNBNode() {
373         return elementNode;
374     }
375     
376     protected StartTagPanel getNewStartTagPanel(ElementPanel elementPanel, InstanceUIContext context) {
377         return new StartTagPanel(elementPanel, context);
378     }
379     
380     
381     ExpandCollapseButton getExpandButton() {
382         return expandButton;
383     }
384     
385     private void drawBoundingBox(Graphics2D JavaDoc g2d) {
386         if( (fadeinPanel == null) || !fadeinPanel.isVisible() )
387             return;
388         if( (childCompositorPanel == null) ||
389                 (childCompositorPanel.getComponents().length < 1))
390             return;
391         //this refreshes the point detail of the tag
392
startTag.getTagShape();
393         //Point rightBottomPoint = startTag.getRightBottomPoint();
394
Point JavaDoc leftBottomPoint = startTag.getLeftNosePoint();
395         Point JavaDoc rightNosePoint = startTag.getRightNosePoint();
396         
397         Stroke JavaDoc oldStroke = g2d.getStroke();
398         Color JavaDoc oldColor = g2d.getColor();
399         g2d.setColor(InstanceDesignConstants.XP_ORANGE);
400         g2d.setStroke(new BasicStroke JavaDoc(1));
401         
402         Rectangle JavaDoc rect = fadeinPanel.getBounds();
403         Rectangle JavaDoc myrect = getBounds();
404         
405         int x1, y1, x2, y2;
406         //left Top
407
x1 = leftBottomPoint.x;
408         y1 = leftBottomPoint.y;
409         //left Bottom
410
x2 = leftBottomPoint.x;
411         y2 = y1 + myrect.height - leftBottomPoint.y - 2;
412         /*y2 = y1 + (rect.y > leftBottomPoint.y ? rect.y - leftBottomPoint.y
413                 : leftBottomPoint.y - rect.y) + rect.height;*/

414         g2d.drawLine(x1, y1, x2, y2);
415         
416         //left Bottom
417
x1 = x2; y1 = y2;
418         //right Bottom
419
x2 = (rect.x + rect.width) > rightNosePoint.x ? (rect.x + rect.width): rightNosePoint.x + 5;
420         y2 = y1;
421         g2d.drawLine(x1, y1, x2, y2);
422         
423         //right Bottom
424
x1 = x2; y1 = y2;
425         //right Top
426
x2 = x1;
427         y2 = rightNosePoint.y;
428         g2d.drawLine(x1-1, y1, x2-1, y2);
429         
430         //right Top
431
x1 = x2; y1 = y2;
432         //end Point
433
x2 = rightNosePoint.x; y2 = rightNosePoint.y;
434         g2d.drawLine(x1, y1, x2, y2);
435         
436         g2d.setStroke(oldStroke);
437         g2d.setColor(oldColor);
438     }
439     
440     
441     
442     
443     public ABEBaseDropPanel getUIComponentFor(AXIComponent axiComponent) {
444         ABEBaseDropPanel comp = super.getUIComponentFor(axiComponent);
445         if(comp == this){
446             //startTag panel is everything so just return that.
447
return startTag;
448         }
449         return null;
450     }
451     
452     public ABEBaseDropPanel getChildUIComponentFor(AXIComponent axiComponent){
453         //search myself
454
ABEBaseDropPanel result = null;
455         //search for attribute
456
if(axiComponent instanceof AbstractAttribute){
457             //pass on the call to start tag panel
458
result = startTag.getChildUIComponentFor(axiComponent);
459         }else{
460             //expand before analysing the content
461
expandChild();
462             //must be element or seq
463
if(childCompositorPanel == null)
464                 return null;
465             
466             expandButton.setText("-");
467             for(Component JavaDoc comp : childCompositorPanel.getComponents()){
468                 if(comp instanceof CompositorPanel){
469                     
470                     result = ((ABEBaseDropPanel)comp).getUIComponentFor(axiComponent);
471                     if(result != null)
472                         break;
473                 }
474             }
475         }
476         return result;
477     }
478     
479     public AXIComponent getAXIComponent() {
480         return getAXIContainer();
481     }
482     
483     public void accept(UIVisitor visitor) {
484         visitor.visit(this);
485     }
486     
487     public ContainerPanel getParentContainerPanel(){
488         return this.parentCompositorPanel;
489     }
490     
491 ////////////////////////////////////////////////////////////////////////////
492
// Instance members
493
////////////////////////////////////////////////////////////////////////////
494

495     private ContainerPanel parentCompositorPanel;
496     private AutoSizingPanel childCompositorPanel;
497     private JPanel JavaDoc fadeinPanel;
498     private AXIContainer axiContainer;
499     private String JavaDoc name;
500     private String JavaDoc annotation;
501     private StartTagPanel startTag;
502     private JPanel JavaDoc startTagPanel;
503     private TweenerPanel tweenerPanel;
504     private List JavaDoc<String JavaDoc> attributes=new ArrayList JavaDoc<String JavaDoc>();
505     private ExpandCollapseButton expandButton;
506     private ABEAbstractNode elementNode;
507 }
508
Popular Tags