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 9, 2003
044: *
045: */
046: package org.netbeans.modules.uml.ui.swing.treetable;
047:
048: import java.awt.Component;
049: import java.awt.event.FocusEvent;
050: import java.awt.event.FocusListener;
051: import java.awt.event.MouseEvent;
052: import java.util.EventObject;
053: import java.util.StringTokenizer;
054:
055: import javax.swing.AbstractCellEditor;
056: import javax.swing.JComboBox;
057: import javax.swing.JTable;
058: import javax.swing.JTextField;
059: import javax.swing.table.TableCellEditor;
060: import javax.swing.tree.TreeModel;
061: import javax.swing.tree.TreePath;
062:
063: import org.netbeans.modules.uml.common.ETSystem;
064: import org.netbeans.modules.uml.core.configstringframework.ConfigStringTranslator;
065: import org.netbeans.modules.uml.core.support.umlutils.IPropertyDefinition;
066: import org.netbeans.modules.uml.core.support.umlutils.IPropertyElement;
067:
068: //import org.netbeans.modules.uml.ui.addins.webreport.WebRPTFormatDlg;
069:
070: public class RPTWizTreeTableCellEditor extends AbstractCellEditor
071: implements TableCellEditor {
072: private JTreeTable.TreeTableCellRenderer tree = null;
073:
074: //private WebRPTFormatDlg m_Editor = null;
075:
076: /**
077: *
078: */
079: public RPTWizTreeTableCellEditor() {
080: super ();
081: }
082:
083: public RPTWizTreeTableCellEditor(JTreeTable.TreeTableCellRenderer tr) {
084: super ();
085: tree = tr;
086: }
087:
088: // public RPTWizTreeTableCellEditor(JTreeTable.TreeTableCellRenderer tr, PropertyEditor editor)
089: // {
090: // super();
091: // tree = tr;
092: // m_Editor = editor;
093: // }
094:
095: public JTreeTable.TreeTableCellRenderer getTree() {
096: return tree;
097: }
098:
099: /**
100: * TreeTableCellEditor implementation. Component returned is the
101: * JTree.
102: */
103: public Component getTableCellEditorComponent(JTable table,
104: Object value, boolean isSelected, int r, int c) {
105: //PropertyEditor.instance().setCurrentRow(r);
106:
107: Component retObj = null;
108:
109: //set the current row on property editor
110: // m_Editor.setCurrentRow(r);
111:
112: boolean isEditable = true;
113:
114: if (c == 2) {
115: TreePath path = getTree().getPathForRow(r);
116: if (path != null) {
117: Object obj = path.getLastPathComponent();
118: if (obj instanceof JDefaultMutableTreeNode) {
119: JDefaultMutableTreeNode node = (JDefaultMutableTreeNode) obj;
120: final IPropertyElement ele = (IPropertyElement) node
121: .getUserObject();
122: IPropertyDefinition def = ele
123: .getPropertyDefinition();
124: long mult = def.getMultiplicity();
125: String values = def.getValidValues();
126: ETSystem.out.println(values);
127: if (values != null) {
128: if (values.equals("FormatString")) {
129: //This node is non-editable, I need to format string to show.
130: } else if (values.equals("#DataTypeList")) {
131: String buffer = "buffer buffer"; //m_Editor.buildDataTypeList(ele);
132: JDescribeComboBox temp = new JDescribeComboBox(/*m_Editor*/);
133: temp.setBorder(null);
134: StringTokenizer tokenizer = new StringTokenizer(
135: buffer, "|");
136: while (tokenizer.hasMoreTokens()) {
137: String transVal = tokenizer.nextToken();
138: temp.addItem(transVal);
139: }
140: temp.setSelectedItem(value);
141: retObj = temp;
142: } else {
143: String cType = def.getControlType();
144: if (cType != null
145: && cType.equals("read-only")) {
146: JTextField temp = new JTextField();
147: temp.setEditable(false);
148: isEditable = false;
149: temp.setBorder(null);
150: if (value != null) {
151: ConfigStringTranslator translator = new ConfigStringTranslator();
152: temp.setText(translator.translate(
153: def, value.toString()));
154: }
155: retObj = temp;
156: } else {
157: ConfigStringTranslator translator = new ConfigStringTranslator();
158: JComboBox temp = new JComboBox();
159: temp.setBorder(null);
160: //temp.addFocusListener(new EditorFocusListener());
161: //cannot make is editable as the focus listener will stop working
162: //temp.setEditable(true);
163: temp.setPopupVisible(false);
164: StringTokenizer tokenizer = new StringTokenizer(
165: values, "|");
166: while (tokenizer.hasMoreTokens()) {
167: String transVal = translator
168: .translate(def, tokenizer
169: .nextToken());
170: temp.addItem(transVal);
171: }
172: temp.setSelectedItem(value);
173:
174: retObj = temp;
175: }
176: }
177: } else {
178: String cType = def.getControlType();
179: if (mult > 1
180: && (cType == null || !cType
181: .equals("read-only"))) {
182: if (table instanceof JPropertyTreeTable) {
183: //WebRPTFormatDlg editor = ((RPTWizPropertyTreeTable)table).getPropertyEditor();
184: JDescribeButton btn = new JDescribeButton(
185: r/*, editor*/);
186: btn.setBorder(null);
187: retObj = btn;
188:
189: //retObj.addFocusListener(new EditorFocusListener());
190: //mark the component as non-editable, so that
191: //we do not add property listener to it.
192: isEditable = false;
193: }
194: } else {
195: JTextField temp = new JTextField();
196: temp.setBorder(null);
197: if (value != null) {
198: temp.setText(value.toString());
199: // temp.addActionListener(new ActionListener() {
200: //
201: // public void actionPerformed(ActionEvent e)
202: // {
203: // ele.setModified(true);
204: // }
205: // });
206: }
207: if (cType == null
208: || cType.equals("read-only")) {
209: temp.setEditable(false);
210:
211: //mark the component as non-editable, so that
212: //we do not add property listener to it.
213: isEditable = false;
214: }
215: retObj = temp;
216: }
217: }
218: //ETSystem.out.println("in getTableCellRen... = " + mult + values);
219: }
220: }
221: } else if (c == 1) {
222: TreePath path = getTree().getPathForRow(r);
223: if (path != null) {
224: Object obj = path.getLastPathComponent();
225: if (obj instanceof JDefaultMutableTreeNode) {
226: JDefaultMutableTreeNode node = (JDefaultMutableTreeNode) obj;
227: if (node.getChildCount() > 0) {
228: retObj = null;//tree;
229: } else {
230: return null;
231: }
232: }
233: }
234: } else if (c == 0) {
235: //ETSystem.out.println("First column selected for images filter");
236: // m_Editor.sortPropertyEditorElements(r);
237: return null;
238: }
239:
240: //I want to add property change listener only if the component is
241: // in values column and is editable.
242: if (retObj != null && c == 2 && isEditable) {
243: // retObj.addPropertyChangeListener(new PropertyChangeListener()
244: // {
245: //
246: // public void propertyChange(PropertyChangeEvent arg0)
247: // {
248: // m_Editor.columnValueChanged(arg0);
249: // }
250: // }
251: // );
252: }
253:
254: if (retObj != null && c == 2) {
255: retObj.addFocusListener(new FocusListener() {
256:
257: public void focusGained(FocusEvent e) {
258: if (!e.isTemporary()) {
259: // m_Editor.handleFocusGainedOnCellEvent(e);
260: }
261: //ETSystem.out.println("Focus gained on " + obj.getClass());
262: }
263:
264: public void focusLost(FocusEvent e) {
265: if (!e.isTemporary()) {
266: // m_Editor.handleFocusLostOnCellEvent(e);
267: }
268: //ETSystem.out.println("Focus lost on " + obj.getClass() + " new value = " + text);
269: }
270: });
271: }
272: return retObj;
273: }
274:
275: /**
276: * Overridden to return false, and if the event is a mouse event
277: * it is forwarded to the tree.<p>
278: * The behavior for this is debatable, and should really be offered
279: * as a property. By returning false, all keyboard actions are
280: * implemented in terms of the table. By returning true, the
281: * tree would get a chance to do something with the keyboard
282: * events. For the most part this is ok. But for certain keys,
283: * such as left/right, the tree will expand/collapse where as
284: * the table focus should really move to a different column. Page
285: * up/down should also be implemented in terms of the table.
286: * By returning false this also has the added benefit that clicking
287: * outside of the bounds of the tree node, but still in the tree
288: * column will select the row, whereas if this returned true
289: * that wouldn't be the case.
290: * <p>By returning false we are also enforcing the policy that
291: * the tree will never be editable (at least by a key sequence).
292: */
293: public boolean isCellEditable(EventObject e) {
294: if (e instanceof MouseEvent) {
295: TreeModel model = tree.getModel();
296: if (model instanceof TreeTableModel) {
297: TreeTableModel tModel = (TreeTableModel) model;
298: for (int counter = tModel.getColumnCount() - 1; counter >= 0; counter--) {
299: if (tModel.getColumnClass(counter).equals(
300: PropertyTreeTableModel.class))
301: //if (counter == 1)
302: {
303: MouseEvent me = (MouseEvent) e;
304: if (me.getClickCount() == 2) {
305: //the element was double clicked, need to expand/collapse
306: // m_Editor.handleDoubleClick();
307: } else {
308: //need to pass on this mouse event to the tree.
309: MouseEvent newME = new MouseEvent(tree, me
310: .getID(), me.getWhen(), me
311: .getModifiers(), me.getX(), me
312: .getY(), me.getClickCount(), me
313: .isPopupTrigger());
314: tree.dispatchEvent(newME);
315: break;
316: }
317: }
318: }
319: }
320: }
321: return true;
322: }
323:
324: /* (non-Javadoc)
325: * @see javax.swing.CellEditor#getCellEditorValue()
326: */
327: public Object getCellEditorValue() {
328: // TODO Auto-generated method stub
329: return tree;
330: }
331:
332: private class EditorFocusListener implements FocusListener {
333: public void focusGained(FocusEvent e) {
334: //m_Editor.handleFocusGainedOnCellEvent(e);
335: ETSystem.out.println("Focus gained on ");
336: }
337:
338: public void focusLost(FocusEvent e) {
339: //m_Editor.handleFocusLostOnCellEvent(e);
340: ETSystem.out.println("Focus lost on ");
341: }
342: }
343: }
|