001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.schema.abe;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Color;
046: import java.awt.Component;
047: import java.awt.Cursor;
048: import java.awt.dnd.DropTargetDragEvent;
049: import java.awt.dnd.DropTargetDropEvent;
050: import java.awt.dnd.DropTargetEvent;
051: import java.awt.event.MouseAdapter;
052: import java.awt.event.MouseEvent;
053: import java.beans.PropertyChangeEvent;
054: import java.beans.PropertyChangeListener;
055: import java.io.IOException;
056: import java.lang.ref.WeakReference;
057: import java.util.ArrayList;
058: import java.util.LinkedList;
059: import java.util.List;
060: import javax.swing.Action;
061: import javax.swing.JPanel;
062: import javax.swing.JScrollPane;
063: import javax.swing.SwingUtilities;
064: import org.netbeans.modules.xml.axi.AXIComponent;
065: import org.netbeans.modules.xml.axi.AXIDocument;
066: import org.netbeans.modules.xml.axi.AXIModel;
067: import org.netbeans.modules.xml.axi.ContentModel;
068: import org.netbeans.modules.xml.axi.Element;
069: import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode;
070: import org.netbeans.modules.xml.schema.model.SchemaComponent;
071: import org.netbeans.modules.xml.schema.model.SchemaModel;
072: import org.netbeans.spi.palette.PaletteActions;
073: import org.netbeans.spi.palette.PaletteController;
074: import org.netbeans.spi.palette.PaletteFactory;
075: import org.openide.ErrorManager;
076: import org.openide.loaders.DataObject;
077: import org.openide.util.Lookup;
078: import org.openide.util.NbBundle;
079: import org.openide.windows.TopComponent;
080:
081: /**
082: *
083: * @author Todd Fast, todd.fast@sun.com
084: */
085: public class InstanceDesignerPanel extends ABEBaseDropPanel {
086: public static final int EXPAND_BY_DEFAULT_LIMIT = 50;
087: private static final long serialVersionUID = 7526472295622776147L;
088:
089: /**
090: *
091: *
092: */
093: public InstanceDesignerPanel(AXIModel axiModel,
094: DataObject schemaDataObject, TopComponent tc) {
095: this (axiModel, schemaDataObject, tc, new InstanceUIContext());
096: }
097:
098: private InstanceDesignerPanel(AXIModel axiModel,
099: DataObject schemaDataObject, TopComponent tc,
100: InstanceUIContext context) {
101: super (context);
102: this .axiModel = axiModel;
103: context.initialize(tc, schemaDataObject, this ,
104: getPaletteController());
105: initComponents();
106: initialize();
107: initMouseListener();
108: }
109:
110: /**
111: *
112: *
113: */
114: private void initialize() {
115: initComponents();
116: JPanel wrapperPanel = new JPanel(new BorderLayout());
117: wrapperPanel.setOpaque(true);
118: wrapperPanel.setBackground(new Color(252, 250, 245));
119:
120: /*wrapperPanel.addMouseListener(new MouseAdapter() {
121: public void mouseClicked(MouseEvent e) {
122: weakThis.get().dispatchEvent(e);
123: }
124: public void mousePressed(MouseEvent e) {
125: weakThis.get().dispatchEvent(e);
126: }
127: public void mouseReleased(MouseEvent e) {
128: weakThis.get().dispatchEvent(e);
129: }
130: });*/
131:
132: namespacePanel = new NamespacePanel(context);
133: add(getNamespacePanel(), BorderLayout.NORTH);
134: //wrapperPanel.add(namespacePanel, BorderLayout.NORTH);
135:
136: boolean expand = getAXIModel().getRoot().getElements().size() > EXPAND_BY_DEFAULT_LIMIT ? false
137: : true;
138: globalElementsChildrenPanel = new GlobalElementsContainerPanel(
139: getUIContext(), getAXIModel().getRoot(), expand);
140: TitleWrapperPanel geWrapper = new TitleWrapperPanel(
141: globalElementsChildrenPanel, globalElementsStr,
142: getAXIModel().getRoot(), expand, context) {
143: private static final long serialVersionUID = 7526472295622776147L;
144:
145: public int getChildrenItemsCount() {
146: return getAXIModel().getRoot().getElements().size();
147: }
148: };
149: wrapperPanel.add(geWrapper, BorderLayout.NORTH);
150:
151: expand = getAXIModel().getRoot().getContentModels().size() > 0 ? false
152: : true;
153: globalComplextypeChildrenPanel = new GlobalComplextypeContainerPanel(
154: getUIContext(), getAXIModel().getRoot(), expand);
155: TitleWrapperPanel gcWrapper = new TitleWrapperPanel(
156: globalComplextypeChildrenPanel, globalComplexTypesStr,
157: getAXIModel().getRoot(), expand, context) {
158: private static final long serialVersionUID = 7526472295622776147L;
159:
160: public int getChildrenItemsCount() {
161: int count = 0;
162: for (ContentModel cm : getAXIModel().getRoot()
163: .getContentModels()) {
164: if (cm.getType() == cm.getType().COMPLEX_TYPE)
165: count++;
166: }
167: return count;
168: }
169: };
170: wrapperPanel.add(gcWrapper, BorderLayout.CENTER);
171:
172: JScrollPane scrollPane = new JScrollPane(wrapperPanel);
173: context.setInstanceDesignerScrollPane(scrollPane);
174: scrollPane.getVerticalScrollBar().setUnitIncrement(20);
175: scrollPane.setBorder(null);
176: scrollPane.setViewportBorder(null);
177: scrollPane.setAutoscrolls(true);
178: add(scrollPane, BorderLayout.CENTER);
179:
180: context.getComponentSelectionManager().setSelectedComponent(
181: namespacePanel);
182: context.setFocusTraversalManager(new FocusTraversalManager(
183: context));
184:
185: // vlv: print
186: wrapperPanel.putClientProperty(java.awt.print.Printable.class,
187: ""); // NOI18N
188: }
189:
190: private static String globalElementsStr = NbBundle.getMessage(
191: InstanceDesignerPanel.class, "" + "LBL_GLOBAL_ELEMENTS");
192: private static String globalComplexTypesStr = NbBundle
193: .getMessage(InstanceDesignerPanel.class, ""
194: + "LBL_GLOBAL_COMPLEXTYPES");
195:
196: /**
197: *
198: *
199: */
200: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
201: private void initComponents() {
202:
203: setLayout(new java.awt.BorderLayout());
204:
205: setBackground(java.awt.Color.white);
206: }// </editor-fold>//GEN-END:initComponents
207:
208: /**
209: *
210: *
211: */
212: public SchemaModel getSchemaModel() {
213: return schemaModel;
214: }
215:
216: /**
217: *
218: *
219: */
220: public InstanceUIContext getUIContext() {
221: return context;
222: }
223:
224: /**
225: *
226: *
227: */
228: public synchronized PaletteController getPaletteController() {
229: if (paletteController != null)
230: return paletteController;
231:
232: PaletteActions actions = new PaletteActionsImpl();
233:
234: try {
235: paletteController = PaletteFactory.createPalette(
236: "xmlschema-abe-palette", actions);
237: } catch (IOException e) {
238: ErrorManager.getDefault().notify(e);
239: }
240: return paletteController;
241: }
242:
243: public AXIModel getAXIModel() {
244: return axiModel;
245: }
246:
247: public NamespacePanel getNamespacePanel() {
248: return namespacePanel;
249: }
250:
251: public void selectUIComponent(AXIComponent axiComponent) {
252: this .setCursor(new Cursor(Cursor.WAIT_CURSOR));
253: try {
254: ABEBaseDropPanel uiComp = null;
255: if (axiComponent == null) {
256: uiComp = namespacePanel;
257: } else {
258: //get the path from the root to the child.
259: LinkedList<AXIComponent> path = new LinkedList<AXIComponent>();
260: AXIComponent current = axiComponent;
261: path.addFirst(current);
262: while ((current != null)
263: && !(current.getParent() instanceof AXIDocument)) {
264: current = current.getParent();
265: path.addFirst(current);
266: }
267: //start opening up the UI components and reach the leaf
268: current = path.getFirst();
269:
270: if (current instanceof Element) {
271: //then the root is global element
272: uiComp = globalElementsChildrenPanel
273: .getChildUIComponentFor(current);
274: } else if (current instanceof ContentModel) {
275: //need to try with global complex type
276: uiComp = globalComplextypeChildrenPanel
277: .getChildUIComponentFor(current);
278: }
279: if (uiComp == null)
280: return;
281: path.removeFirst();
282: for (AXIComponent comp : path) {
283: ABEBaseDropPanel cur = uiComp
284: .getChildUIComponentFor(comp);
285: if (cur == null)
286: return;
287: uiComp = cur;
288: }
289: }
290: context.getComponentSelectionManager()
291: .setSelectedComponent(uiComp);
292: final ABEBaseDropPanel ftemp = uiComp;
293: SwingUtilities.invokeLater(new Runnable() {
294: public void run() {
295: UIUtilities.scrollViewTo(ftemp, context);
296: }
297: });
298: } finally {
299: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
300: }
301: }
302:
303: public void selectUIComponent(
304: org.netbeans.modules.xml.xam.Component component) {
305: if (component == null) {
306: selectUIComponent(null);
307: return;
308: }
309: if (component instanceof AXIComponent)
310: selectUIComponent((AXIComponent) component);
311: else {
312: AXIComponent axiComp = null;
313: try {
314: axiComp = UIUtilities
315: .findMatchingAXIComponent((SchemaComponent) component);
316: } catch (Exception e) {
317: }
318: selectUIComponent(axiComp);
319: }
320: }
321:
322: public GlobalElementsContainerPanel getGlobalElementsPanel() {
323: return globalElementsChildrenPanel;
324: }
325:
326: public GlobalComplextypeContainerPanel getGlobalComplextypePanel() {
327: return globalComplextypeChildrenPanel;
328: }
329:
330: /*void setUserInducedEventMode(boolean mode) {
331: if(this.userInducedEventMode == mode)
332: return;
333: Boolean oldValue = Boolean.valueOf(userInducedEventMode);
334: this.userInducedEventMode = mode;
335: firePropertyChange(PROP_USER_INDUCED_EVENT_MODE, oldValue,
336: Boolean.valueOf(mode));
337: }*/
338:
339: public void accept(UIVisitor visitor) {
340: visitor.visit(context.getNamespacePanel());
341: }
342:
343: public ABEAbstractNode getNBNode() {
344: //just return the namespace panel node
345: return context.getNamespacePanel().getNBNode();
346: }
347:
348: protected void initMouseListener() {
349: addMouseListener(new MouseAdapter() {
350: public void mouseReleased(MouseEvent e) {
351: mouseClickedActionHandler(e);
352: }
353:
354: public void mouseClicked(MouseEvent e) {
355: mouseClickedActionHandler(e);
356: }
357:
358: public void mousePressed(MouseEvent e) {
359: mouseClickedActionHandler(e);
360: }
361:
362: });
363: }
364:
365: protected void mouseClickedActionHandler(MouseEvent e) {
366: if (e.getClickCount() == 1) {
367: if (e.isPopupTrigger()) {
368: context.getMultiComponentActionManager().showPopupMenu(
369: e, this );
370: return;
371: }
372: }
373: //the tag is selected
374: context.getComponentSelectionManager().setSelectedComponent(
375: this );
376: }
377:
378: public void drop(DropTargetDropEvent event) {
379: getNamespacePanel().drop(event);
380: }
381:
382: public void dragExit(DropTargetEvent event) {
383: getNamespacePanel().dragExit(event);
384: }
385:
386: public void dragEnter(DropTargetDragEvent event) {
387: getNamespacePanel().dragEnter(event);
388: }
389:
390: public void dragOver(DropTargetDragEvent event) {
391: getNamespacePanel().dragOver(event);
392: }
393:
394: public void shutdown() {
395: context.shutdown();
396: context = null;
397: }
398:
399: ////////////////////////////////////////////////////////////////////////////
400: // Instance variables
401: ////////////////////////////////////////////////////////////////////////////
402:
403: // Variables declaration - do not modify//GEN-BEGIN:variables
404: // End of variables declaration//GEN-END:variables
405:
406: private SchemaModel schemaModel;
407: private AXIModel axiModel;
408: private PaletteController paletteController;
409: GlobalElementsContainerPanel globalElementsChildrenPanel;
410: List<Component> childrenList = new ArrayList<Component>();
411: NamespacePanel namespacePanel;
412: GlobalComplextypeContainerPanel globalComplextypeChildrenPanel;
413: //private boolean userInducedEventMode;
414: //public static final String PROP_USER_INDUCED_EVENT_MODE = "user_induced_event_mode";
415: }
416:
417: class PaletteActionsImpl extends PaletteActions {
418: public Action[] getImportActions() {
419: return new Action[0];
420: }
421:
422: public Action[] getCustomPaletteActions() {
423: return new Action[0];
424: }
425:
426: public Action[] getCustomCategoryActions(Lookup lookup) {
427: return new Action[0];
428: }
429:
430: public Action[] getCustomItemActions(Lookup lookup) {
431: return new Action[0];
432: }
433:
434: public Action getPreferredAction(Lookup lookup) {
435: return null;
436: }
437:
438: }
|