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.uml.palette;
043:
044: import java.beans.PropertyChangeListener;
045: import java.beans.PropertyChangeEvent;
046:
047: import org.openide.nodes.Node;
048: import org.openide.ErrorManager;
049: import org.openide.cookies.InstanceCookie;
050: import org.openide.util.Lookup;
051: import org.netbeans.spi.palette.PaletteController;
052: import org.netbeans.modules.uml.palette.model.ModelingPaletteNodeDescriptor;
053: import org.netbeans.modules.uml.core.metamodel.diagrams.IDiagramKind;
054: import org.netbeans.modules.uml.core.metamodel.diagrams.IDrawingToolKind;
055: import org.netbeans.modules.uml.core.support.umlsupport.Log;
056: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaControl;
057: import org.netbeans.modules.uml.ui.swing.drawingarea.IDrawingAreaSelectStateEnteredSink;
058: import java.util.Enumeration;
059: import org.openide.nodes.Children;
060:
061: /**
062: *
063: * @author Thuy Nguyen
064: */
065: public class PaletteSupport {
066:
067: /**
068: * Creates a new instance of PaletteSupport
069: */
070: public PaletteSupport() {
071: }
072:
073: private PaletteController pController = null;
074: private PalettePropertyChangeListener propertyChangeListener = null;
075:
076: public PaletteController getPalette(
077: IDrawingAreaControl drawingAreaControl) {
078:
079: // get a string representing the diagram kind given a drawingAreaControl object
080: String paletteFolderName = findPaletteRepository(drawingAreaControl
081: .getDiagramKind());
082: pController = UMLPaletteFactory.getPalette(paletteFolderName);
083:
084: if (pController != null) {
085: // registers a PropertyChangeListner to the palette controller object
086: propertyChangeListener = new PalettePropertyChangeListener(
087: drawingAreaControl);
088: pController
089: .addPropertyChangeListener(propertyChangeListener);
090: initPaletteSettings();
091:
092: drawingAreaControl
093: .addDrawingAreaToolSelectionSink(new DrawingAreaSelectStateEnteredListener(
094: pController, propertyChangeListener));
095: }
096: return pController;
097: }
098:
099: public void unregisterListeners() {
100: if (pController != null) {
101: pController
102: .removePropertyChangeListener(propertyChangeListener);
103: }
104: if (propertyChangeListener != null) {
105: propertyChangeListener.setDrawingAreaControl(null);
106: propertyChangeListener = null;
107: }
108: }
109:
110: private String findPaletteRepository(int diagramType) {
111: String repository = null;
112:
113: switch (diagramType) {
114: case IDiagramKind.DK_CLASS_DIAGRAM:
115: repository = "UML/Palette/ClassDiagram"; // NOI18N
116: break;
117: case IDiagramKind.DK_ACTIVITY_DIAGRAM:
118: repository = "UML/Palette/ActivityDiagram"; // NOI18N
119: break;
120: case IDiagramKind.DK_COLLABORATION_DIAGRAM:
121: repository = "UML/Palette/CollaborationDiagram"; // NOI18N
122: break;
123: case IDiagramKind.DK_COMPONENT_DIAGRAM:
124: repository = "UML/Palette/ComponentDiagram"; // NOI18N
125: break;
126: case IDiagramKind.DK_DEPLOYMENT_DIAGRAM:
127: repository = "UML/Palette/DeploymentDiagram"; // NOI18N
128: break;
129: case IDiagramKind.DK_SEQUENCE_DIAGRAM:
130: repository = "UML/Palette/SequenceDiagram"; // NOI18N
131: break;
132: case IDiagramKind.DK_STATE_DIAGRAM:
133: repository = "UML/Palette/StateDiagram"; // NOI18N
134: break;
135: case IDiagramKind.DK_USECASE_DIAGRAM:
136: repository = "UML/Palette/UsecaseDiagram"; // NOI18N
137: break;
138: }
139: return repository;
140: }
141:
142: private void initPaletteSettings() {
143: Lookup nodeLkup = pController.getRoot();
144: Node rootNode = (Node) nodeLkup.lookup(Node.class);
145: if (rootNode == null)
146: return;
147: Children categories = rootNode.getChildren();
148: if (categories != null) {
149: Node cat = null;
150: Enumeration enumNodes = categories.nodes();
151: while (enumNodes.hasMoreElements()) {
152: cat = (Node) enumNodes.nextElement();
153: if (cat != null) {
154: // expand the category node initially
155: cat.setValue(PaletteController.ATTR_IS_EXPANDED,
156: true);
157: }
158: }
159: }
160: }
161:
162: private class PalettePropertyChangeListener implements
163: PropertyChangeListener {
164:
165: private IDrawingAreaControl drawingAreaContrl1;
166:
167: public PalettePropertyChangeListener(
168: IDrawingAreaControl daController) {
169: drawingAreaContrl1 = daController;
170: }
171:
172: public void setDrawingAreaControl(
173: IDrawingAreaControl daController) {
174: drawingAreaContrl1 = daController;
175: }
176:
177: public void propertyChange(PropertyChangeEvent event) {
178: String propName = event.getPropertyName();
179: Log
180: .out("PalettePropertyChangeListener():propertyChange(): event is on "
181: + propName); // NOI18N
182:
183: if (PaletteController.PROP_SELECTED_ITEM.equals(propName)) {
184:
185: Object newVal = event.getNewValue();
186: Log
187: .out("PalettePropertyChangeListener():propertyChange(): Current selection is "
188: + newVal); // NOI18N
189: //System.out.println("PalettePropertyChangeListener():propertyChange(): Current selection is " + newVal);
190:
191: if (newVal != null) {
192: if (newVal instanceof Lookup) {
193: Lookup lkup = (Lookup) newVal;
194:
195: Node selectedNode = (Node) lkup
196: .lookup(Node.class);
197:
198: if (selectedNode == null) { // when the same node is selected. i.e. the node is deselected
199: drawingAreaContrl1
200: .enterMode(IDrawingToolKind.DTK_SELECTION);
201: return;
202: }
203:
204: InstanceCookie ic = (InstanceCookie) selectedNode
205: .getCookie(InstanceCookie.class);
206: if (ic != null) {
207: Object obj = null;
208: try {
209: obj = ic.instanceCreate();
210: } catch (Exception ex) {
211: Log
212: .out("PalettePropertyChangeListener(): Error : "
213: + ex); // NOI18N
214: ErrorManager.getDefault().notify(
215: ErrorManager.INFORMATIONAL, ex);
216: }
217:
218: if (obj instanceof ModelingPaletteNodeDescriptor) {
219: ModelingPaletteNodeDescriptor umlPaletteNodeDescriptor = (ModelingPaletteNodeDescriptor) obj;
220: String paletteItemID = umlPaletteNodeDescriptor
221: .getButtonID();
222: Log
223: .out("Button ID is "
224: + paletteItemID); // NOI18N
225: //System.out.println("Button ID is " + paletteItemID);
226:
227: if (drawingAreaContrl1 != null) {
228: drawingAreaContrl1
229: .setSelectedPaletteButton(paletteItemID);
230: //System.out.println("PalettePropertyChangeListener(): paletteItemID="+drawingAreaContrl.getSelectedPaletteButton());
231: Log
232: .out("PalettePropertyChangeListener(): Calling entermodefromButton()"); // NOI18N
233: drawingAreaContrl1
234: .enterModeFromButton(paletteItemID);
235: }
236: }
237: }
238: }
239: } else { // No item is selected.
240: // set the state of the graph window to select
241: drawingAreaContrl1
242: .enterMode(IDrawingToolKind.DTK_SELECTION);
243: }
244: }
245: }
246: }
247:
248: private class DrawingAreaSelectStateEnteredListener implements
249: IDrawingAreaSelectStateEnteredSink {
250:
251: PaletteController paletteController;
252: PalettePropertyChangeListener listener;
253:
254: public DrawingAreaSelectStateEnteredListener(
255: PaletteController pController,
256: PalettePropertyChangeListener pListener) {
257: paletteController = pController;
258: listener = pListener;
259: }
260:
261: // Reset the palette so that no item is selected.
262: public void onSelectToolSelected() {
263: Log
264: .out("CreatePalette():DrawingAreaSelectStateEnteredListener(): resetting state");
265:
266: paletteController.removePropertyChangeListener(listener);
267: paletteController.setSelectedItem(null, null);
268: paletteController.addPropertyChangeListener(listener);
269: }
270: }
271: }
|