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: /*
043: * Created on Jun 18, 2003
044: *
045: */
046: package org.netbeans.modules.uml.ui.swing.treetable;
047:
048: import java.awt.GridBagLayout;
049: import java.awt.event.ActionEvent;
050: import java.awt.event.ActionListener;
051: import java.awt.event.KeyEvent;
052: import java.awt.event.KeyListener;
053: import java.awt.event.MouseEvent;
054: import java.awt.event.MouseListener;
055:
056: import javax.swing.JMenuItem;
057: import javax.swing.JPopupMenu;
058: import javax.swing.ToolTipManager;
059: import javax.swing.event.MouseInputAdapter;
060: import javax.swing.table.TableColumnModel;
061: import javax.swing.tree.TreePath;
062:
063: import org.netbeans.modules.uml.common.ETSystem;
064: import org.netbeans.modules.uml.core.support.umlutils.IPropertyDefinition;
065: import org.netbeans.modules.uml.core.support.umlutils.IPropertyElement;
066: import org.netbeans.modules.uml.ui.swing.propertyeditor.PropertyEditor;
067: import org.netbeans.modules.uml.ui.swing.propertyeditor.PropertyEditorResources;
068:
069: /**
070: * @author sumitabhk
071: *
072: */
073: public class JPropertyTreeTable extends JTreeTable implements
074: ActionListener {
075: private JPopupMenu m_popup = null;
076: private PropertyEditor m_editor = null;
077:
078: //protected TreeTableCellRenderer tree = null;
079:
080: private int m_CurRow = 0;
081: private IPropertyElement m_CurElement = null;
082:
083: /**
084: *
085: */
086: public JPropertyTreeTable(TreeTableModel treeTableModel,
087: PropertyEditor editor) {
088: super (treeTableModel);
089: m_editor = editor;
090:
091: m_popup = new JPopupMenu();
092:
093: GridBagLayout gbl = new GridBagLayout();
094: double[] vals = { 0.0, 0.5, 0.5 };
095: gbl.columnWeights = vals;
096: setLayout(gbl);
097: gbl.invalidateLayout(this );
098: doLayout();
099:
100: TableColumnModel colMod = getColumnModel();
101: TreeTableCellEditor cellEditor = new TreeTableCellEditor(tree,
102: editor);
103: colMod.getColumn(1).setCellEditor(cellEditor);
104: colMod.getColumn(2).setCellEditor(cellEditor);
105: colMod.getColumn(0).setCellEditor(cellEditor);
106:
107: colMod.getColumn(0).setMinWidth(20);
108: colMod.getColumn(0).setMaxWidth(20);
109:
110: PropertyValueCellRenderer valueRenderer = new PropertyValueCellRenderer();
111: colMod.getColumn(1).setCellRenderer(tree);
112: colMod.getColumn(2).setCellRenderer(valueRenderer);
113:
114: MouseListener popupListener = new TreeTablePopupListener();
115:
116: this .addMouseListener(popupListener);
117: getTree().addMouseListener(new TreeMouseHandler());
118:
119: ToolTipManager.sharedInstance().registerComponent(this );
120: setShowVerticalLines(true);
121:
122: this .addKeyListener(new KeyListener() {
123:
124: public void keyTyped(KeyEvent e) {
125: e.consume();
126: }
127:
128: public void keyPressed(KeyEvent e) {
129: if (e.getKeyCode() == KeyEvent.VK_RIGHT
130: || e.getKeyCode() == KeyEvent.VK_LEFT
131: || e.getKeyCode() == KeyEvent.VK_INSERT
132: || e.getKeyCode() == KeyEvent.VK_DELETE
133: || e.getKeyCode() == KeyEvent.VK_ENTER) {
134: m_editor.onKeyDownGrid(e);
135: e.consume();
136: } else if (e.getKeyCode() == KeyEvent.VK_TAB) {
137: m_editor.onKeyDownGrid(e);
138: if (e.isShiftDown()) {
139: m_CurRow--;
140: } else {
141: m_CurRow++;
142: }
143: e.consume();
144: } else if (e.getKeyCode() == KeyEvent.VK_UP) {
145: m_editor.onKeyDownGrid(e);
146: m_CurRow--;
147: e.consume();
148: } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
149: m_editor.onKeyDownGrid(e);
150: m_CurRow++;
151: e.consume();
152: }
153: }
154:
155: public void keyReleased(KeyEvent e) {
156: e.consume();
157: }
158: });
159: }
160:
161: protected void processMouseMotionEvent(MouseEvent e) {
162: //we do not want the columns/cells to be dragged, so just consume this event for Propety Editor.
163: e.consume();
164: }
165:
166: /**
167: *
168: * Allows a user to start editing the specified row.
169: */
170: public void startEditingRow(int row) {
171: m_editor.startEditingRowAt(row);
172: m_CurRow = row;
173: }
174:
175: public void handlePopupDisplay(MouseEvent e) {
176: m_popup.removeAll();
177: //pass this on to the PropertyEditor, passing in the location of
178: // mouse click.
179: int row = rowAtPoint(e.getPoint());
180: //TreePath path = getTree().getPathForLocation(e.getX(), e.getY());
181: TreePath path = getTree().getPathForRow(row);
182:
183: if (path != null) {
184: m_editor.handleSave(true, null);
185: m_editor.setEditingComponent(null);
186:
187: Object obj = path.getLastPathComponent();
188: ETSystem.out.println(obj.getClass());
189: JDefaultMutableTreeNode node = (JDefaultMutableTreeNode) obj;
190: IPropertyElement ele = (IPropertyElement) node
191: .getUserObject();
192: if (ele != null) {
193: //set the current row and selected element to get popup menu to work.
194: m_CurRow = row;
195: m_CurElement = ele;
196:
197: IPropertyDefinition def = ele.getPropertyDefinition();
198: String[] strs = m_editor.showMenuBasedOnDefinition(def,
199: ele);
200: m_editor.setRightClickRow(row);
201: if (strs != null) {
202: int count = strs.length;
203: for (int i = 0; i < count; i++) {
204: JMenuItem menuItem = new JMenuItem(strs[i]);
205: menuItem.addActionListener(this );
206: m_popup.add(menuItem);
207: }
208: }
209: }
210: }
211: if (m_popup != null) {
212: m_popup.show(e.getComponent(), e.getX(), e.getY());
213: }
214:
215: }
216:
217: public PropertyEditor getPropertyEditor() {
218: return m_editor;
219: }
220:
221: public void actionPerformed(ActionEvent e) {
222: JMenuItem source = (JMenuItem) (e.getSource());
223: String srcText = source.getText();
224: if (srcText.equals(PropertyEditorResources
225: .getString("PropertyEditor.Create_Menu"))) {
226: m_editor.onPopupCreate(m_CurRow, m_CurElement);
227: } else if (srcText.equals(PropertyEditorResources
228: .getString("PropertyEditor.Delete_Menu"))) {
229: m_editor.onPopupDelete();
230: } else if (srcText.equals(PropertyEditorResources
231: .getString("PropertyEditor.Navigate_Menu"))) {
232: m_editor.onPopupNavigate();
233: } else if (srcText.equals(PropertyEditorResources
234: .getString("PropertyEditor.Fill_Down_Menu"))) {
235: m_editor.onPopupFill();
236: }
237: }
238:
239: public class TreeMouseHandler extends MouseInputAdapter {
240: public void mousePressed(MouseEvent e) {
241: int selRow = getTree()
242: .getRowForLocation(e.getX(), e.getY());
243: TreePath selPath = getTree().getPathForLocation(e.getX(),
244: e.getY());
245: if (selRow != -1) {
246: if (e.getClickCount() == 2) {
247: m_editor.handleDoubleClick(selRow, selPath);
248: m_editor.refresh();
249: } else if (e.getClickCount() == 1) {
250: Object obj = selPath.getLastPathComponent();
251: if (obj instanceof JDefaultMutableTreeNode) {
252: //getTree().getUI().
253: // if(selPath != null && !getModel().isLeaf(selPath.getLastPathComponent())){
254: // int boxWidth;
255: // java.awt.Insets i = tree.getInsets();
256: //
257: // if(getExpandedIcon() != null)
258: // boxWidth = getExpandedIcon().getIconWidth();
259: // else
260: // boxWidth = 8;
261: //
262: // int boxLeftX = (i != null) ? i.left : 0;
263: //
264: // if (leftToRight) {
265: // boxLeftX += (((path.getPathCount() + depthOffset - 2) *
266: // totalChildIndent) + getLeftChildIndent()) -
267: // boxWidth / 2;
268: // }
269: // else {
270: // boxLeftX += lastWidth - 1 -
271: // ((path.getPathCount() - 2 + depthOffset) *
272: // totalChildIndent) - getLeftChildIndent() -
273: // boxWidth / 2;
274: // }
275: // int boxRightX = boxLeftX + boxWidth;
276: //
277: // return mouseX >= boxLeftX && mouseX <= boxRightX;
278: // }
279: // return false;
280: }
281: }
282: }
283: }
284:
285: }
286:
287: }
|