KickJava   Java API By Example, From Geeks To Geeks.

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


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

28
29 package org.netbeans.modules.xml.schema.abe;
30
31 import java.awt.Component JavaDoc;
32 import java.beans.PropertyChangeEvent JavaDoc;
33 import java.beans.PropertyChangeListener JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.List JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import org.netbeans.modules.xml.axi.AXIComponent;
38 import org.netbeans.modules.xml.axi.AXIContainer;
39 import org.netbeans.modules.xml.axi.AXIModel;
40 import org.netbeans.modules.xml.axi.AbstractElement;
41 import org.netbeans.modules.xml.axi.AnyElement;
42 import org.netbeans.modules.xml.axi.Element;
43 import org.netbeans.modules.xml.schema.abe.palette.DnDHelper;
44 import org.openide.util.NbBundle;
45
46 /**
47  *
48  * @author girix
49  */

50 public abstract class ElementsContainerPanel extends ContainerPanel{
51     private static final long serialVersionUID = 7526472295622776147L;
52     /** Creates a new instance of ElementsContainerPanel */
53     public ElementsContainerPanel(InstanceUIContext context, AXIComponent axiComponent,
54             Component JavaDoc parentPanel, boolean openByDefault) {
55         super(context, axiComponent, parentPanel, openByDefault);
56     }
57     
58     
59     protected void setupAXIComponentListener() {
60         getAXIParent().addPropertyChangeListener(new ModelEventMediator(this, getAXIParent()) {
61             public void _propertyChange(PropertyChangeEvent JavaDoc evt) {
62                 if(evt.getPropertyName().startsWith(Element.PROP_ELEMENT)){
63                     if(context.isUserInducedEventMode())
64                         setVisible(true);
65                     //event is for child element
66
if((evt.getOldValue() == null) && (evt.getNewValue() != null)){
67                         //new element added
68
addElement((AbstractElement)evt.getNewValue());
69                     }else if((evt.getNewValue() == null) && (evt.getOldValue() != null)){
70                         //old element removed
71
removeElement((AbstractElement)evt.getOldValue());
72                     }
73
74                 }
75             }
76         });
77     }
78     
79     public void visit(Element element) {
80         super.visit(element);
81         visitorResult = new ElementPanel(context, element,
82                 ElementsContainerPanel.this);
83     }
84     
85     public void visit(AnyElement element){
86         super.visit(element);
87         visitorResult = new ElementPanel(context, element,
88                 ElementsContainerPanel.this);
89     }
90     
91     public List JavaDoc<? extends AXIComponent> getAXIChildren() {
92         return getAXIParent().getChildElements();
93     }
94     
95     
96     
97     public void addElement(AXIContainer element){
98         //look in to the children list find out where the child was added
99
//create a new ElementPanel and add @ that index. Adjust the layout accordingly
100
int index = getAXIChildren().indexOf(element);
101         ABEBaseDropPanel temp = null;
102         if( (temp = isAlreadyAdded(element)) != null){
103             //if already added the just ignore
104
showNameEditorIfNeeded(temp);
105             return;
106         }
107         
108         visitorResult = null;
109         element.accept(this);
110         final ElementPanel ep = (ElementPanel) visitorResult;
111         if(ep == null)
112             return;
113         
114         if(!addChildAt(ep, index)){
115             //then some problem happened which caused a redraw. So, ignore rest of the steps.
116
return;
117         }
118         showNameEditorIfNeeded(ep);
119     }
120     
121     
122     public void showNameEditorIfNeeded(ABEBaseDropPanel comp){
123         final ElementPanel ep;
124         if(comp instanceof ElementPanel)
125             ep = (ElementPanel) comp;
126         else
127             return;
128         if(context.isUserInducedEventMode() &&
129                 ( ((getParentContainerPanel() instanceof ElementPanel) &&
130                 (((ElementPanel)getParentContainerPanel()).getStartTagPanel()
131                 == context.getUserActedComponent()) )
132                 || ( (getParentContainerPanel() == null) && (context.getUserActedComponent() == context.getNamespacePanel() )
133                 ) ) ){
134             
135             //show the tag name editor after
136
SwingUtilities.invokeLater(new Runnable JavaDoc(){
137                 public void run() {
138                     //skipping this threadtime is needed to let the UI refresh before the
139
//tag editor is shown. If I do it in the same threadtime the editor
140
//appears in a wrong position.
141
SwingUtilities.invokeLater(new Runnable JavaDoc(){
142                         public void run() {
143                             UIUtilities.scrollViewTo(ep.getStartTagPanel(), context);
144                             context.getComponentSelectionManager().setSelectedComponent(ep.getStartTagPanel());
145                             ep.showNameEditor(true);
146                             context.setUserInducedEventMode(false);
147                             context.resetUserActedComponent();
148                         }
149                     });
150                 }
151             });
152         }
153     }
154     
155     public void removeElement(AXIContainer element){
156         //look in to the children list find out where the child was present
157
//remove the ElementPanel @ that index. Adjust the layout accordingly
158
Component JavaDoc rmComp = null;
159         for(Component JavaDoc component: getChildrenList()){
160             if(component instanceof ElementPanel){
161                 if( ((ElementPanel)component).getAXIContainer() == element){
162                     //dont call removeComponent() from here as it affects getChildrenList()
163
rmComp = component;
164                     break;
165                 }
166             }
167         }
168         if(rmComp != null){
169             removeComponent(rmComp);
170         }
171         
172     }
173     
174     private void addNewElementAt(TweenerPanel tweener){
175         int index = getChildrenList().indexOf(tweener);
176         if(index == -1){
177             //must not happen
178
return;
179         }
180         index = index/2;
181         AXIModel model = getAXIParent().getModel();
182         model.startTransaction();
183         try{
184             Element elm = model.getComponentFactory().createElement();
185             String JavaDoc str = UIUtilities.getUniqueName(
186                     InstanceDesignConstants.NEW_ELEMENT_NAME, getAXIParent());
187             elm.setName(str);
188             getAXIParent().addChildAtIndex(elm, index);
189         }finally{
190             model.endTransaction();
191         }
192     }
193     
194     public void tweenerDrop(TweenerPanel tweener, DnDHelper.PaletteItem paletteItem) {
195         super.tweenerDrop(tweener, paletteItem);
196         if(paletteItem == paletteItem.ELEMENT){
197             ABEBaseDropPanel comp = null;
198             if(getParentContainerPanel() instanceof ElementPanel)
199                 comp = ((ElementPanel)getParentContainerPanel()).getStartTagPanel();
200             else if(getParentContainerPanel() == null)
201                 comp = context.getNamespacePanel();
202             context.setUserInducedEventMode(true, comp);
203             addNewElementAt(tweener);
204         }
205     }
206     
207     public void tweenerDragEntered(TweenerPanel tweener, DnDHelper.PaletteItem paletteItem) {
208         super.tweenerDragEntered(tweener, paletteItem);
209         if(tweenerDragAccept(tweener, paletteItem)){
210             String JavaDoc locDropMsgAccept = NbBundle.getMessage(GlobalComplextypeContainerPanel.class,
211                     "MSG_GEP_DROP_ACCEPT");
212             tweener.setDropInfoText(locDropMsgAccept);
213         }
214     }
215     
216     public boolean tweenerDragAccept(TweenerPanel tweener, DnDHelper.PaletteItem paletteItem) {
217         if(paletteItem != paletteItem.ELEMENT){
218             String JavaDoc locDropMsgAccept = NbBundle.getMessage(GlobalComplextypeContainerPanel.class,
219                     "MSG_GEP_DROP_REJECT");
220             UIUtilities.showErrorMessageFor(locDropMsgAccept, context, tweener);
221             return false;
222         }
223         return true;
224     }
225     
226     public void tweenerDragExited(TweenerPanel tweener) {
227         super.tweenerDragExited(tweener);
228         UIUtilities.hideGlassMessage();
229     }
230     
231 }
232
Popular Tags