KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Cursor JavaDoc;
26 import java.awt.dnd.DropTargetDragEvent JavaDoc;
27 import java.awt.dnd.DropTargetDropEvent JavaDoc;
28 import java.awt.dnd.DropTargetEvent JavaDoc;
29 import java.awt.event.MouseAdapter JavaDoc;
30 import java.awt.event.MouseEvent JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.lang.ref.WeakReference JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.LinkedList JavaDoc;
37 import java.util.List JavaDoc;
38 import javax.swing.Action JavaDoc;
39 import javax.swing.JPanel JavaDoc;
40 import javax.swing.JScrollPane JavaDoc;
41 import javax.swing.SwingUtilities JavaDoc;
42 import org.netbeans.modules.xml.axi.AXIComponent;
43 import org.netbeans.modules.xml.axi.AXIDocument;
44 import org.netbeans.modules.xml.axi.AXIModel;
45 import org.netbeans.modules.xml.axi.ContentModel;
46 import org.netbeans.modules.xml.axi.Element;
47 import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode;
48 import org.netbeans.modules.xml.schema.model.SchemaComponent;
49 import org.netbeans.modules.xml.schema.model.SchemaModel;
50 import org.netbeans.spi.palette.PaletteActions;
51 import org.netbeans.spi.palette.PaletteController;
52 import org.netbeans.spi.palette.PaletteFactory;
53 import org.openide.ErrorManager;
54 import org.openide.loaders.DataObject;
55 import org.openide.util.Lookup;
56 import org.openide.util.NbBundle;
57 import org.openide.windows.TopComponent;
58
59 /**
60  *
61  * @author Todd Fast, todd.fast@sun.com
62  */

63 public class InstanceDesignerPanel extends ABEBaseDropPanel {
64     public static final int EXPAND_BY_DEFAULT_LIMIT = 50;
65     private static final long serialVersionUID = 7526472295622776147L;
66     /**
67      *
68      *
69      */

70     public InstanceDesignerPanel(AXIModel axiModel, DataObject schemaDataObject, TopComponent tc) {
71         this(axiModel, schemaDataObject, tc, new InstanceUIContext());
72     }
73     
74     private InstanceDesignerPanel(AXIModel axiModel, DataObject schemaDataObject, TopComponent tc, InstanceUIContext context) {
75         super(context);
76         this.axiModel = axiModel;
77         context.initialize(tc, schemaDataObject, this, getPaletteController());
78         initComponents();
79         initialize();
80         initMouseListener();
81     }
82     
83     /**
84      *
85      *
86      */

87     private void initialize() {
88         initComponents();
89         JPanel JavaDoc wrapperPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
90         wrapperPanel.setOpaque(true);
91         wrapperPanel.setBackground(new Color JavaDoc(252, 250, 245));
92         
93         /*wrapperPanel.addMouseListener(new MouseAdapter() {
94             public void mouseClicked(MouseEvent e) {
95                 weakThis.get().dispatchEvent(e);
96             }
97             public void mousePressed(MouseEvent e) {
98                 weakThis.get().dispatchEvent(e);
99             }
100             public void mouseReleased(MouseEvent e) {
101                 weakThis.get().dispatchEvent(e);
102             }
103         });*/

104         
105         namespacePanel = new NamespacePanel(context);
106         add(getNamespacePanel(), BorderLayout.NORTH);
107         //wrapperPanel.add(namespacePanel, BorderLayout.NORTH);
108

109         boolean expand = getAXIModel().getRoot().getElements().size() > EXPAND_BY_DEFAULT_LIMIT ? false : true;
110         globalElementsChildrenPanel = new GlobalElementsContainerPanel(
111                 getUIContext(), getAXIModel().getRoot(), expand);
112         TitleWrapperPanel geWrapper = new TitleWrapperPanel(globalElementsChildrenPanel,
113                 globalElementsStr, getAXIModel().getRoot(), expand, context){
114             private static final long serialVersionUID = 7526472295622776147L;
115             public int getChildrenItemsCount() {
116                 return getAXIModel().getRoot().getElements().size();
117             }
118         };
119         wrapperPanel.add(geWrapper, BorderLayout.NORTH);
120         
121         expand = getAXIModel().getRoot().getContentModels().size() > 0 ? false : true;
122         globalComplextypeChildrenPanel = new GlobalComplextypeContainerPanel(
123                 getUIContext(), getAXIModel().getRoot(), expand);
124         TitleWrapperPanel gcWrapper = new TitleWrapperPanel(globalComplextypeChildrenPanel,
125                 globalComplexTypesStr, getAXIModel().getRoot(), expand, context){
126             private static final long serialVersionUID = 7526472295622776147L;
127             public int getChildrenItemsCount() {
128                 int count = 0;
129                 for(ContentModel cm: getAXIModel().getRoot().getContentModels()){
130                     if(cm.getType() == cm.getType().COMPLEX_TYPE)
131                         count++;
132                 }
133                 return count;
134             }
135         };
136         wrapperPanel.add(gcWrapper, BorderLayout.CENTER);
137         
138         JScrollPane JavaDoc scrollPane=new JScrollPane JavaDoc(wrapperPanel);
139         context.setInstanceDesignerScrollPane(scrollPane);
140         scrollPane.getVerticalScrollBar().setUnitIncrement(20);
141         scrollPane.setBorder(null);
142         scrollPane.setViewportBorder(null);
143         scrollPane.setAutoscrolls(true);
144         add(scrollPane,BorderLayout.CENTER);
145         
146         
147         context.getComponentSelectionManager().setSelectedComponent(namespacePanel);
148         
149         context.setFocusTraversalManager(new FocusTraversalManager(context));
150     }
151     
152     
153     private static String JavaDoc globalElementsStr = NbBundle.getMessage(InstanceDesignerPanel.class,"" +
154             "LBL_GLOBAL_ELEMENTS");
155     private static String JavaDoc globalComplexTypesStr = NbBundle.getMessage(InstanceDesignerPanel.class,"" +
156             "LBL_GLOBAL_COMPLEXTYPES");
157     
158     
159     
160     /**
161      *
162      *
163      */

164     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
165
private void initComponents() {
166
167         setLayout(new java.awt.BorderLayout JavaDoc());
168
169         setBackground(java.awt.Color.white);
170     }// </editor-fold>//GEN-END:initComponents
171

172     
173     /**
174      *
175      *
176      */

177     public SchemaModel getSchemaModel() {
178         return schemaModel;
179     }
180     
181     
182     /**
183      *
184      *
185      */

186     public InstanceUIContext getUIContext() {
187         return context;
188     }
189     
190     
191     /**
192      *
193      *
194      */

195     public synchronized PaletteController getPaletteController() {
196         if (paletteController!=null)
197             return paletteController;
198         
199         PaletteActions actions=
200                 new PaletteActionsImpl();
201         
202         try {
203             paletteController=
204                     PaletteFactory.createPalette("xmlschema-abe-palette",actions);
205         } catch (IOException JavaDoc e) {
206             ErrorManager.getDefault().notify(e);
207         }
208         return paletteController;
209     }
210     
211     public AXIModel getAXIModel() {
212         return axiModel;
213     }
214     
215     public NamespacePanel getNamespacePanel() {
216         return namespacePanel;
217     }
218     
219     
220     
221     public void selectUIComponent(AXIComponent axiComponent){
222         this.setCursor(new Cursor JavaDoc(Cursor.WAIT_CURSOR));
223         try{
224             ABEBaseDropPanel uiComp = null;
225             if(axiComponent == null){
226                 uiComp = namespacePanel;
227             }else{
228                 //get the path from the root to the child.
229
LinkedList JavaDoc<AXIComponent> path = new LinkedList JavaDoc<AXIComponent>();
230                 AXIComponent current = axiComponent;
231                 path.addFirst(current);
232                 while( (current != null) && !(current.getParent() instanceof AXIDocument) ){
233                     current = current.getParent();
234                     path.addFirst(current);
235                 }
236                 //start opening up the UI components and reach the leaf
237
current = path.getFirst();
238                 
239                 if(current instanceof Element){
240                     //then the root is global element
241
uiComp = globalElementsChildrenPanel.getChildUIComponentFor(current);
242                 }else if(current instanceof ContentModel){
243                     //need to try with global complex type
244
uiComp = globalComplextypeChildrenPanel.getChildUIComponentFor(current);
245                 }
246                 if(uiComp == null)
247                     return;
248                 path.removeFirst();
249                 for(AXIComponent comp: path){
250                     ABEBaseDropPanel cur = uiComp.getChildUIComponentFor(comp);
251                     if(cur == null)
252                         return;
253                     uiComp = cur;
254                 }
255             }
256             context.getComponentSelectionManager().setSelectedComponent(uiComp);
257             final ABEBaseDropPanel ftemp = uiComp;
258             SwingUtilities.invokeLater(new Runnable JavaDoc(){
259                 public void run() {
260                     UIUtilities.scrollViewTo(ftemp, context);
261                 }
262             });
263         }finally{
264             this.setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
265         }
266     }
267     
268     
269     public void selectUIComponent(org.netbeans.modules.xml.xam.Component component) {
270         if(component == null){
271             selectUIComponent(null);
272             return;
273         }
274         if(component instanceof AXIComponent)
275             selectUIComponent((AXIComponent)component);
276         else {
277             AXIComponent axiComp = null;
278             try{
279                 axiComp = UIUtilities.findMatchingAXIComponent((SchemaComponent) component);
280             }catch (Exception JavaDoc e){}
281             selectUIComponent(axiComp);
282         }
283     }
284     
285     public GlobalElementsContainerPanel getGlobalElementsPanel() {
286         return globalElementsChildrenPanel;
287     }
288     
289     public GlobalComplextypeContainerPanel getGlobalComplextypePanel() {
290         return globalComplextypeChildrenPanel;
291     }
292     
293     /*void setUserInducedEventMode(boolean mode) {
294         if(this.userInducedEventMode == mode)
295             return;
296         Boolean oldValue = Boolean.valueOf(userInducedEventMode);
297         this.userInducedEventMode = mode;
298         firePropertyChange(PROP_USER_INDUCED_EVENT_MODE, oldValue,
299                 Boolean.valueOf(mode));
300     }*/

301     
302     public void accept(UIVisitor visitor) {
303         //do nothing
304
}
305     
306     public ABEAbstractNode getNBNode() {
307         //just return the namespace panel node
308
return context.getNamespacePanel().getNBNode();
309     }
310     
311     
312     
313     protected void initMouseListener(){
314         addMouseListener(new MouseAdapter JavaDoc() {
315             public void mouseReleased(MouseEvent JavaDoc e) {
316                 mouseClickedActionHandler(e);
317             }
318             public void mouseClicked(MouseEvent JavaDoc e){
319                 mouseClickedActionHandler(e);
320             }
321             
322             public void mousePressed(MouseEvent JavaDoc e) {
323                 mouseClickedActionHandler(e);
324             }
325             
326         });
327     }
328     
329     
330     protected void mouseClickedActionHandler(MouseEvent JavaDoc e){
331         if(e.getClickCount() == 1){
332             if(e.isPopupTrigger()){
333                 context.getMultiComponentActionManager().showPopupMenu(e, this);
334                 return;
335             }
336         }
337         //the tag is selected
338
context.getComponentSelectionManager().setSelectedComponent(this);
339     }
340     
341     public void drop(DropTargetDropEvent JavaDoc event) {
342         getNamespacePanel().drop(event);
343     }
344     
345     public void dragExit(DropTargetEvent JavaDoc event) {
346         getNamespacePanel().dragExit(event);
347     }
348     
349     public void dragEnter(DropTargetDragEvent JavaDoc event) {
350         getNamespacePanel().dragEnter(event);
351     }
352     
353     public void dragOver(DropTargetDragEvent JavaDoc event) {
354         getNamespacePanel().dragOver(event);
355     }
356     
357     public void shutdown() {
358         context.shutdown();
359         context = null;
360     }
361     
362     
363 ////////////////////////////////////////////////////////////////////////////
364
// Instance variables
365
////////////////////////////////////////////////////////////////////////////
366

367     // Variables declaration - do not modify//GEN-BEGIN:variables
368
// End of variables declaration//GEN-END:variables
369

370     private SchemaModel schemaModel;
371     private AXIModel axiModel;
372     private PaletteController paletteController;
373     GlobalElementsContainerPanel globalElementsChildrenPanel;
374     List JavaDoc<Component JavaDoc> childrenList = new ArrayList JavaDoc<Component JavaDoc>();
375     NamespacePanel namespacePanel;
376     GlobalComplextypeContainerPanel globalComplextypeChildrenPanel;
377 //private boolean userInducedEventMode;
378
//public static final String PROP_USER_INDUCED_EVENT_MODE = "user_induced_event_mode";
379
}
380
381 class PaletteActionsImpl extends PaletteActions {
382     public Action JavaDoc[] getImportActions() {
383         return new Action JavaDoc[0];
384     }
385     
386     public Action JavaDoc[] getCustomPaletteActions() {
387         return new Action JavaDoc[0];
388     }
389     
390     public Action JavaDoc[] getCustomCategoryActions(Lookup lookup) {
391         return new Action JavaDoc[0];
392     }
393     
394     public Action JavaDoc[] getCustomItemActions(Lookup lookup) {
395         return new Action JavaDoc[0];
396     }
397     
398     public Action JavaDoc getPreferredAction(Lookup lookup) {
399         return null;
400     }
401     
402 }
403
404
405
Popular Tags