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.ui.support.drawingproperties.FontColorDialogs;
043:
044: import java.awt.Component;
045: import java.awt.event.MouseEvent;
046: import java.util.EventObject;
047:
048: import javax.swing.JLabel;
049: import javax.swing.JTable;
050: import javax.swing.event.MouseInputAdapter;
051: import javax.swing.table.TableColumnModel;
052: import javax.swing.tree.DefaultMutableTreeNode;
053: import javax.swing.tree.TreeModel;
054: import javax.swing.tree.TreePath;
055: import javax.swing.tree.TreeSelectionModel;
056:
057: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
058: import org.netbeans.modules.uml.core.metamodel.diagrams.IProxyDiagram;
059: import org.netbeans.modules.uml.ui.support.DiagramBuilder;
060: import org.netbeans.modules.uml.ui.support.commonresources.CommonResourceManager;
061: import org.netbeans.modules.uml.ui.support.diagramsupport.IPresentationTarget;
062: import org.netbeans.modules.uml.ui.swing.commondialogs.SwingNavigationDialog;
063: import org.netbeans.modules.uml.ui.swing.treetable.JTreeTable;
064: import org.netbeans.modules.uml.ui.swing.treetable.PropertyValueCellRenderer;
065: import org.netbeans.modules.uml.ui.swing.treetable.TreeTableModel;
066:
067: /**
068: * @author sumitabhk
069: *
070: *
071: */
072: public class FontColorTreeTable extends JTreeTable {
073: private TreePath m_Selected = null;
074: private CommonResourceManager m_Manager = CommonResourceManager
075: .instance();
076: private BasicColorsAndFontsDialog m_Parent = null;
077:
078: /**
079: *
080: */
081: public FontColorTreeTable(TreeTableModel model) {
082: this (model, (TreeTableCellRenderer) null);
083: }
084:
085: public FontColorTreeTable(TreeTableModel model,
086: BasicColorsAndFontsDialog parent) {
087: this (model, (TreeTableCellRenderer) null);
088: m_Parent = parent;
089: }
090:
091: public FontColorTreeTable(TreeTableModel model,
092: TreeTableCellRenderer renderer) {
093: super (model, renderer);
094:
095: tree.setShowsRootHandles(true);
096: tree.setRootVisible(false);
097: tree.getSelectionModel().setSelectionMode(
098: TreeSelectionModel.SINGLE_TREE_SELECTION);
099: tree.setEditable(true);
100:
101: TableColumnModel colMod = getColumnModel();
102:
103: DrawingPropertyCellRenderer valueRenderer = new DrawingPropertyCellRenderer();
104: colMod.getColumn(0).setCellRenderer(tree);
105:
106: colMod.getColumn(1).setCellRenderer(valueRenderer);
107: FontColorCellEditor editor = new FontColorCellEditor(
108: valueRenderer);
109: colMod.getColumn(1).setCellEditor(editor);
110: colMod.getColumn(0).setCellEditor(editor);
111:
112: // colMod.getColumn(2).setCellRenderer(valueRenderer);
113: // colMod.getColumn(3).setCellRenderer(valueRenderer);
114:
115: addMouseListener(new TreeMouseHandler());
116:
117: }
118:
119: public BasicColorsAndFontsDialog getParentDlg() {
120: return m_Parent;
121: }
122:
123: public boolean editCellAt(int row, int column, EventObject e) {
124: boolean retVal = super .editCellAt(row, column, e);
125: if (column == 1) {
126: retVal = true;
127: } else if (column == 0) {
128: retVal = false;
129: }
130: return retVal;
131: }
132:
133: public TreePath getSelectedPath() {
134: return m_Selected;
135: }
136:
137: public void expandFirstLevelNodes() {
138: TreeModel model = getTree().getModel();
139: Object obj = model.getRoot();
140: if (obj instanceof DefaultMutableTreeNode) {
141: DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj;
142: int count = node.getChildCount();
143: for (int i = count - 1; i >= 0; i--) {
144: tree.expandNode(i, true);
145: }
146: }
147: }
148:
149: public void handleNavigation(TreePath path) {
150: //we want to navigate to the double clicked element.
151: if (path != null) {
152: Object obj = path.getLastPathComponent();
153: if (obj != null && obj instanceof DefaultMutableTreeNode) {
154: Object userObj = ((DefaultMutableTreeNode) obj)
155: .getUserObject();
156: if (userObj != null) {
157: DiagramBuilder builder = new DiagramBuilder();
158: if (userObj instanceof IProxyDiagram) {
159: IProxyDiagram dia = (IProxyDiagram) userObj;
160: String filename = dia.getFilename();
161: String presId = dia.getXMIID();
162: if (m_Parent != null) {
163: //m_Parent.setTargetXMIID(presId);
164: }
165: if (filename != null && filename.length() > 0) {
166: builder.navigateToDiagram(filename, presId,
167: "", "");
168: }
169: } else if (userObj instanceof IPresentationTarget) {
170: IPresentationTarget target = (IPresentationTarget) userObj;
171: String id = target.getPresentationID();
172: if (m_Parent != null) {
173: //m_Parent.setTargetXMIID(id);
174: }
175: builder.navigateToTarget(target);
176: } else if (userObj instanceof IElement) {
177: IElement elem = (IElement) userObj;
178: String id = elem.getXMIID();
179: if (m_Parent != null) {
180: //m_Parent.setTargetXMIID(id);
181: }
182: builder.navigateToElementInTree(elem);
183: }
184: }
185: }
186: }
187: }
188:
189: public class TreeMouseHandler extends MouseInputAdapter {
190: public void mousePressed(MouseEvent e) {
191: int selRow = rowAtPoint(e.getPoint());
192: if (selRow >= 0) {
193: Object source = e.getSource();
194: if (source instanceof FontColorTreeTable) {
195: ((FontColorTreeTable) source).editCellAt(selRow, 1);
196: }
197: }
198: // TreePath selPath = getTree().getPathForRow(selRow);
199: // //TreePath selPath = getTree().getPathForLocation(e.getX(), e.getY());
200: // if(selRow != -1)
201: // {
202: // if (e.getClickCount() == 1)
203: // {
204: // m_Selected = selPath;
205: //
206: // //if parent is not null, then set element type on that
207: // if (m_Parent != null)
208: // {
209: // Object obj = selPath.getLastPathComponent();
210: // if (obj != null && obj instanceof DefaultMutableTreeNode)
211: // {
212: // Object userObj = ((DefaultMutableTreeNode)obj).getUserObject();
213: // if (userObj != null)
214: // {
215: // if (userObj instanceof IProxyDiagram)
216: // {
217: // m_Parent.setIsDiagram(true);
218: // }
219: // else
220: // {
221: // m_Parent.setIsDiagram(false);
222: // }
223: // }
224: // }
225: // }
226: //
227: // if (!tree.isExpanded(selPath))
228: // {
229: // getTree().expandPath(selPath);
230: // }
231: // else
232: // {
233: // getTree().collapsePath(selPath);
234: // }
235: // }
236: // else if (e.getClickCount() == 2)
237: // {
238: // handleNavigation(selPath);
239: // }
240: // }
241: }
242:
243: }
244:
245: public class FontColorCellRenderer extends TreeTableCellRenderer {
246: public FontColorCellRenderer(TreeTableCellRenderer renderer) {
247: super ();
248: tree = renderer;
249: }
250:
251: public Component getTableCellRendererComponent(JTable table,
252: Object value, boolean isSelected, boolean hasFocus,
253: int row, int column) {
254: Component retObj = super .getTableCellRendererComponent(
255: table, value, isSelected, hasFocus, row, column);
256: if (column == 0) {
257: TreePath path = tree.getPathForRow(row);
258: Object tempNode = path.getLastPathComponent();
259: if (tempNode != null
260: && tempNode instanceof DefaultMutableTreeNode) {
261: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tempNode;
262: Object obj = node.getUserObject();
263: if (obj != null) {
264: if (obj instanceof IPresentationTarget) {
265: JLabel retObj1 = new JLabel();
266: retObj1.setIcon(m_Manager
267: .getIconForDisp(obj));
268: return retObj1;
269: } else if (obj instanceof IProxyDiagram) {
270: JLabel retObj1 = new JLabel();
271: retObj1.setIcon(m_Manager
272: .getIconForDisp(obj));
273: return retObj1;
274: } else if (obj instanceof IElement) {
275: JLabel retObj1 = new JLabel();
276: retObj1.setIcon(m_Manager
277: .getIconForDisp(obj));
278: return retObj1;
279: }
280: }
281: }
282: }
283:
284: return retObj;
285: }
286: }
287: }
|