KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CompositorPanel.java
21  *
22  * Created on May 24, 2006, 5:28 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.xml.schema.abe;
29
30 import java.awt.BorderLayout JavaDoc;
31 import java.awt.Component JavaDoc;
32 import java.awt.Dimension JavaDoc;
33 import java.awt.Graphics JavaDoc;
34 import java.awt.Graphics2D JavaDoc;
35 import java.awt.event.ComponentEvent JavaDoc;
36 import java.awt.event.ComponentListener JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.LinkedList JavaDoc;
39 import java.util.List JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.SpringLayout JavaDoc;
42 import javax.swing.SwingConstants JavaDoc;
43 import javax.swing.SwingUtilities JavaDoc;
44 import org.netbeans.api.progress.ProgressHandle;
45 import org.netbeans.api.progress.ProgressHandleFactory;
46 import org.netbeans.modules.xml.axi.AXIComponent;
47 import org.netbeans.modules.xml.axi.AXIDocument;
48 import org.netbeans.modules.xml.axi.AnyAttribute;
49 import org.netbeans.modules.xml.axi.AnyElement;
50 import org.netbeans.modules.xml.axi.Attribute;
51 import org.netbeans.modules.xml.axi.Compositor;
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.axi.visitor.AXIVisitor;
56 import org.netbeans.modules.xml.schema.abe.palette.DnDHelper;
57 import org.openide.util.NbBundle;
58 import org.openide.util.RequestProcessor;
59
60 /**
61  *
62  * @author girix
63  */

64 public abstract class ContainerPanel extends AnnotatedBorderPanel implements AXIVisitor{
65     private static final long serialVersionUID = 7526472295622776147L;
66     private AXIComponent axiParent;
67     Component JavaDoc parentPanel;
68     private static final int INTER_PANEL_VERTICAL_SPACE = 0;
69     private int panelIndendation = InstanceDesignConstants.TAG_INDENT ;
70     private LinkedList JavaDoc<Component JavaDoc> childrenList = new LinkedList JavaDoc<Component JavaDoc>();
71     protected Component JavaDoc visitorResult = null;
72     boolean openByDefault = true;
73     
74     public ContainerPanel(InstanceUIContext context, AXIComponent axiParent,
75             Component JavaDoc parentPanel, boolean openByDefault) {
76         super(context);
77         this.setAXIParent(axiParent);
78         this.parentPanel = parentPanel;
79         this.openByDefault = openByDefault;
80         initialize();
81     }
82     
83     protected abstract void setupAXIComponentListener();
84     public abstract List JavaDoc<? extends AXIComponent> getAXIChildren();
85     
86     private void initialize(){
87         setOpaque(false);
88         setLayout(new BorderLayout JavaDoc());
89         addHeaderPanel();
90         initChildrenPanel();
91         if(openByDefault)
92             addAllChildren();
93         setupAXIComponentListener();
94     }
95     
96     
97     public void addHeaderPanel(){
98     }
99     
100     JPanel JavaDoc childrenPanel;
101     SpringLayout JavaDoc childrenPanelLayout;
102     Component JavaDoc childrenPanelLastComponent;
103     public void initChildrenPanel(){
104         //create a child panel that could expand and collapse
105
childrenPanelLayout = new SpringLayout JavaDoc();
106         childrenPanel = new JPanel JavaDoc(childrenPanelLayout);
107         childrenPanel.setOpaque(false);
108         
109         add(childrenPanel, BorderLayout.CENTER);
110         
111         
112         //add always a tweener panel to save drop logic in the compositorTypePanel
113
TweenerPanel tweener = new TweenerPanel(SwingConstants.HORIZONTAL, context);
114         //appendChild(tweener);
115
childrenPanel.add(tweener);
116         getChildrenList().add(tweener);
117         addTweenerListener(tweener);
118         
119         //always compensate the expand button of the compositor panel
120
childrenPanelLayout.putConstraint(SpringLayout.WEST, tweener,
121                 getChildrenIndent(), SpringLayout.WEST, childrenPanel);
122         childrenPanelLayout.putConstraint(SpringLayout.NORTH, tweener,
123                 getInterComponentVerticalSpacing(), SpringLayout.NORTH, childrenPanel);
124         adjustChildrenPanelSize();
125         addComponentEventListener(tweener);
126         childrenPanelLastComponent = tweener;
127         
128     }
129     
130     
131     public void verifyChildrenWithModel(){
132         List JavaDoc<Component JavaDoc> noTweenerList = new ArrayList JavaDoc<Component JavaDoc>();
133         for(Component JavaDoc comp : getChildrenList()){
134             if(comp instanceof TweenerPanel)
135                 continue;
136             noTweenerList.add(comp);
137         }
138         
139         if(noTweenerList.size() != getAXIChildren().size()){
140             //this will solve the count problem
141
removeAndAddAllChildren();
142             return;
143         }
144         
145         for(AXIComponent axiComponent : getAXIChildren()){
146             //ensure that all the children are in order and
147
if(isAlreadyAdded(axiComponent) == null){
148                 removeAndAddAllChildren();
149                 return;
150             }
151         }
152     }
153     
154     
155     public void removeAndAddAllChildren(){
156         this.remove(childrenPanel);
157         childrenPanel = null;
158         initChildrenPanel();
159         //childrenAdded = false;
160
addAllChildren();
161     }
162     
163     boolean childrenAdded = false;
164     public void addAllChildren(){
165         if(childrenAdded)
166             return;
167         childrenAdded = true;
168         final List JavaDoc<? extends AXIComponent> children = getAXIChildren();
169         if(children.size() < InstanceDesignerPanel.EXPAND_BY_DEFAULT_LIMIT){
170             addAllChildren(children) ;
171         }else{
172             RequestProcessor.getDefault().post(new Runnable JavaDoc() {
173                 public void run() {
174                     addAllChildren(children) ;
175                 }
176             });
177         }
178     }
179     
180     private void addAllChildren(List JavaDoc<? extends AXIComponent> children){
181         initProgress(children);
182         for(final AXIComponent axiComp: children){
183             showProgress();
184             visitorResult = null;
185             axiComp.accept(ContainerPanel.this);
186             final Component JavaDoc compToAdd = visitorResult;
187             if(visitorResult != null){
188                 appendChild(compToAdd, false);
189                 TweenerPanel tweener = new TweenerPanel(SwingConstants.HORIZONTAL, context);
190                 addTweenerListener(tweener);
191                 appendChild(tweener, false);
192             }
193         }
194         adjustChildrenPanelSize();
195         revalidate();
196         finishProgress();
197     }
198     
199     
200     ProgressHandle progressHandle = null;
201     int stepCount = 0;
202     private void initProgress(List JavaDoc<? extends AXIComponent> children) {
203         final int size = children.size();
204         if( size < InstanceDesignerPanel.EXPAND_BY_DEFAULT_LIMIT){
205             return;
206         }
207         UIUtilities.showBulbMessage(NbBundle.getMessage(ContainerPanel.class,
208                 "MSG_RENDERING_CHILDREN")+"...", this.context);
209         progressHandle= ProgressHandleFactory.createHandle(NbBundle.getMessage(
210                 ContainerPanel.class, "MSG_RENDERING_CHILDREN")+": ");
211         progressHandle.setInitialDelay(1);
212         progressHandle.start(size+10);
213         UIUtilities.setBusyCursor(this.context);
214         stepCount = 0;
215     }
216     
217     private void showProgress(){
218         if(progressHandle != null){
219             progressHandle.progress(stepCount++);
220         }
221     }
222     
223     private void finishProgress(){
224         if(progressHandle != null){
225             progressHandle.finish();
226             progressHandle = null;
227             UIUtilities.setDefaultCursor(this.context);
228             UIUtilities.hideGlassMessage();
229             stepCount = 0;
230         }
231     }
232     
233     public int getChildrenIndent(){
234         return getPanelIndendation()+InstanceDesignConstants.COMPOSITOR_CHILDREN_INDENT;//+ExpandCollapseButton.WIDTH;
235
}
236     
237     public void appendChild(Component JavaDoc component, boolean resize){
238         appendChild(component, 0, resize);
239     }
240     
241     public void appendChild(Component JavaDoc component, int vPadding, boolean resize){
242         childrenPanel.add(component);
243         getChildrenList().add(component);
244         //always compensate the expand button of the compositor panel
245
childrenPanelLayout.putConstraint(SpringLayout.WEST, component,
246                 getChildrenIndent(), SpringLayout.WEST, childrenPanel);
247         childrenPanelLayout.putConstraint(SpringLayout.NORTH, component,
248                 getInterComponentVerticalSpacing() + vPadding, SpringLayout.SOUTH, childrenPanelLastComponent);
249         
250         childrenPanelLastComponent = component;
251         if(resize){
252             adjustChildrenPanelSize();
253             revalidate();
254         }
255         addComponentEventListener(component);
256     }
257     
258     public boolean addChildAt(Component JavaDoc component, int index) {
259         childrenPanel.add(component);
260         Component JavaDoc cAbove = null;
261         Component JavaDoc cBelow = null;
262         try{
263             cAbove = getAboveAdjacentComponent(index);
264             cBelow = getBelowAdjacentComponent(index);
265         }catch(Exception JavaDoc e){
266             removeAndAddAllChildren();
267             return false;
268         }
269         if(cAbove == null){
270             //this usecase is invalid
271
return false;
272         }
273         //standard left spring
274
childrenPanelLayout.putConstraint(SpringLayout.WEST, component,
275                 getChildrenIndent(), SpringLayout.WEST, childrenPanel);
276         
277         childrenPanelLayout.putConstraint(SpringLayout.NORTH, component,
278                 getInterComponentVerticalSpacing(), SpringLayout.SOUTH, cAbove);
279         //add a tweener panel...always
280
TweenerPanel tweener = new TweenerPanel(SwingConstants.HORIZONTAL, context);
281         addTweenerListener(tweener);
282         childrenPanel.add(tweener);
283         childrenPanelLayout.putConstraint(SpringLayout.WEST, tweener,
284                 getChildrenIndent(), SpringLayout.WEST, childrenPanel);
285         
286         childrenPanelLayout.putConstraint(SpringLayout.NORTH, tweener,
287                 getInterComponentVerticalSpacing(), SpringLayout.SOUTH, component);
288         
289         if(cBelow != null){
290             //adjust the spring between tweener and below
291
childrenPanelLayout.putConstraint(SpringLayout.NORTH, cBelow,
292                     getInterComponentVerticalSpacing(), SpringLayout.SOUTH, tweener);
293         }
294         
295         //int cAboveIndex = getChildrenList().indexOf(cAbove);
296
if(getChildrenList().getLast() == cAbove){
297             //already last element so just append
298
getChildrenList().add(component);
299             getChildrenList().add(tweener);
300         }else{
301             getChildrenList().add(getChildrenList().indexOf(cAbove) + 1, component);
302             getChildrenList().add(getChildrenList().indexOf(component) + 1, tweener);
303         }
304         
305         adjustChildrenPanelSize();
306         revalidate();
307         //repaint();
308
addComponentEventListener(component);
309         addComponentEventListener(tweener);
310         return true;
311     }
312     
313     
314     public void removeComponent(Component JavaDoc component){
315         int cIndex = getChildrenList().indexOf(component);
316         //tweener panel index
317
int belowTIndex = cIndex + 1;
318         int aboveTIndex = cIndex - 1;
319         Component JavaDoc belowTweener = getChildrenList().get(belowTIndex);
320         Component JavaDoc aboveTweener = getChildrenList().get(aboveTIndex);
321         Component JavaDoc belowComponent = null;
322         if(getChildrenList().getLast() != belowTweener){
323             belowComponent = getChildrenList().get(belowTIndex + 1);
324         }
325         
326         childrenPanelLayout.removeLayoutComponent(component);
327         childrenPanelLayout.removeLayoutComponent(belowTweener);
328         childrenPanel.remove(component);
329         childrenPanel.remove(belowTweener);
330         
331         if(belowComponent != null){
332             childrenPanelLayout.putConstraint(SpringLayout.NORTH, belowComponent,
333                     getInterComponentVerticalSpacing(), SpringLayout.SOUTH, aboveTweener);
334         }
335         
336         getChildrenList().remove(component);
337         getChildrenList().remove(belowTweener);
338         
339         adjustChildrenPanelSize();
340         revalidate();
341         repaint();
342         
343     }
344     
345     
346     
347     private void addComponentEventListener(Component JavaDoc comp){
348         comp.addComponentListener(new ComponentListener JavaDoc() {
349             public void componentHidden(ComponentEvent JavaDoc e) {
350                 adjustChildrenPanelSize();
351             }
352             public void componentMoved(ComponentEvent JavaDoc e) {
353             }
354             int callCount = 1;
355             public void componentResized(ComponentEvent JavaDoc e) {
356                 if( (callCount <= 2) && ( (ContainerPanel.this instanceof GlobalComplextypeContainerPanel)
357                 ||(ContainerPanel.this instanceof GlobalElementsContainerPanel) ) ){
358                     //skip this event for the first time.
359
//If not done then there is a 10 sec delay in the UI after expanding
360
//many nodes in a huge schema.
361
callCount++;
362                     return;
363                 }
364                 adjustChildrenPanelSize();
365             }
366             public void componentShown(ComponentEvent JavaDoc e) {
367                 adjustChildrenPanelSize();
368             }
369         });
370     }
371     
372     public Component JavaDoc getAboveAdjacentComponent(int index){
373         return getChildrenList().get(index * 2);
374     }
375     
376     public Component JavaDoc getBelowAdjacentComponent(int index){
377         int ind = (index * 2) + 1;
378         if(ind >= getChildrenList().size())
379             return null;
380         return getChildrenList().get(ind);
381     }
382     
383     
384     public void adjustChildrenPanelSize() {
385         int width = 0;
386         int height = 0;
387         int indent = getChildrenIndent();
388         int spacing = getInterComponentVerticalSpacing();
389         for(Component JavaDoc child: childrenPanel.getComponents()){
390             if(!child.isVisible())
391                 break;
392             Dimension JavaDoc dim = child.getPreferredSize();
393             int curWidth = dim.width +
394                     indent;//+50;
395
if(curWidth > width)
396                 width = curWidth;
397             height += dim.height + spacing;
398         }
399         //add some fudge
400
width += 20;
401         Dimension JavaDoc dim = new Dimension JavaDoc(width, height+3);
402         
403         Dimension JavaDoc old = childrenPanel.getPreferredSize();
404         boolean revalidateChild = false;
405         if( (old.height != dim.height) || (old.width != dim.width) ){
406             childrenPanel.setPreferredSize(dim);
407             childrenPanel.setMinimumSize(dim);
408             revalidateChild = true;
409         }
410         
411         dim = _getMinimumSize();
412         old = getPreferredSize();
413         boolean revalidateMe = false;
414         if( (old.height != dim.height) || (old.width != dim.width) ){
415             setMinimumSize(dim);
416             setPreferredSize(dim);
417             revalidateMe = true;
418         }
419         if(revalidateChild)
420             childrenPanel.revalidate();
421         if(revalidateMe)
422             revalidate();
423         
424     }
425     
426 //Following set of methods needed for the tag size calculation and horizontal bar display logic
427
public Dimension JavaDoc getPreferredSize() {
428         return getMinimumSize();
429     }
430     
431     public Dimension JavaDoc _getMinimumSize() {
432         int width = 0;
433         int height = 0;
434         for(Component JavaDoc child: this.getComponents()){
435             if(!child.isVisible())
436                 break;
437             Dimension JavaDoc dim = child.getPreferredSize();
438             height += dim.getHeight();
439             int thisW = dim.width ;
440             width = width < thisW ? thisW : width;
441         }
442         return new Dimension JavaDoc(width, height);
443     }
444     
445     
446     
447     public ABEBaseDropPanel isAlreadyAdded(AXIComponent axiComp){
448         for(Component JavaDoc comp: getChildrenList()){
449             //check if this component is already added to the list. if yes return
450
if(comp instanceof ABEBaseDropPanel){
451                 if(((ABEBaseDropPanel)comp).getAXIComponent() == axiComp )
452                     return (ABEBaseDropPanel)comp;
453             }
454         }
455         return null;
456     }
457     
458     public boolean tweenerDragAccept(TweenerPanel tweener, DnDHelper.PaletteItem paletteItem) {
459         return true;
460     }
461     
462     public void tweenerDrop(TweenerPanel tweener, DnDHelper.PaletteItem paletteItem) {
463     }
464     
465     public void tweenerDragExited(TweenerPanel tweener) {
466     }
467     
468     public void tweenerDragEntered(TweenerPanel tweener, DnDHelper.PaletteItem paletteItem) {
469     }
470     
471     
472     protected void addTweenerListener(final TweenerPanel tweener) {
473         tweener.addTweenerListener(new TweenerListener(){
474             public boolean dragAccept(DnDHelper.PaletteItem paletteItem) {
475                 return ContainerPanel.this.tweenerDragAccept(tweener, paletteItem);
476             }
477             
478             public void drop(DnDHelper.PaletteItem paletteItem) {
479                 ContainerPanel.this.tweenerDrop(tweener, paletteItem);
480             }
481             
482             public void dragExited() {
483                 ContainerPanel.this.tweenerDragExited(tweener);
484             }
485             
486             public void dragEntered(DnDHelper.PaletteItem paletteItem) {
487                 ContainerPanel.this.tweenerDragEntered(tweener, paletteItem);
488             }
489         });
490     }
491     
492     
493     public int getInterComponentVerticalSpacing(){
494         return INTER_PANEL_VERTICAL_SPACE;
495     }
496     
497     
498     public void paintComponent(Graphics JavaDoc g){
499         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g;
500         super.paintComponent(g2d);
501     }
502     
503     public AXIComponent getAXIParent() {
504         return axiParent;
505     }
506     
507     public void setAXIParent(AXIComponent axiParent) {
508         this.axiParent = axiParent;
509     }
510     
511     
512     public int getPanelIndendation() {
513         return panelIndendation;
514     }
515     
516     public void setPanelIndendation(int panelIndendation) {
517         this.panelIndendation = panelIndendation;
518     }
519     
520     
521     
522     public LinkedList JavaDoc<Component JavaDoc> getChildrenList() {
523         return childrenList;
524     }
525     
526     public void setChildrenList(LinkedList JavaDoc<Component JavaDoc> childrenList) {
527         this.childrenList = childrenList;
528     }
529     
530     public void visit(AnyAttribute attribute) {
531     }
532     
533     public void visit(Element element) {
534     }
535     
536     public void visit(ContentModel element) {
537     }
538     
539     public void visit(Datatype datatype) {
540     }
541     
542     public void visit(AnyElement element) {
543     }
544     
545     public void visit(AXIDocument root) {
546     }
547     
548     public void visit(Attribute attribute) {
549     }
550     
551     public void visit(Compositor compositor) {
552     }
553     
554     public AXIComponent getAXIComponent() {
555         return getAXIParent();
556     }
557     
558     public ABEBaseDropPanel getChildUIComponentFor(AXIComponent axiComponent){
559         this.addAllChildren();
560         this.setVisible(true);
561         for(Component JavaDoc comp: getChildrenList()){
562             if(comp instanceof ABEBaseDropPanel){
563                 ABEBaseDropPanel uiComp = ((ABEBaseDropPanel)comp).getUIComponentFor(axiComponent);
564                 if( uiComp != null)
565                     return uiComp;
566             }
567         }
568         return null;
569     }
570     
571     public ABEBaseDropPanel getParentContainerPanel(){
572         return (ABEBaseDropPanel)this.parentPanel;
573     }
574 }
575
Popular Tags