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 2004-2005 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: package org.netbeans.modules.jmx.mbeanwizard.table;
042:
043: import org.netbeans.modules.jmx.mbeanwizard.editor.JComboBoxCellEditor;
044: import javax.swing.JTable;
045: import javax.swing.table.AbstractTableModel;
046: import javax.swing.JComboBox;
047: import javax.swing.JTextField;
048: import javax.swing.table.TableCellEditor;
049: import org.netbeans.modules.jmx.mbeanwizard.editor.JTextFieldCellEditor;
050: import org.netbeans.modules.jmx.mbeanwizard.renderer.ComboBoxRenderer;
051: import java.awt.Dimension;
052: import java.awt.event.KeyEvent;
053: import java.awt.event.KeyListener;
054: import javax.swing.ListSelectionModel;
055: import javax.swing.table.TableCellRenderer;
056: import org.netbeans.modules.jmx.WizardHelpers;
057:
058: /**
059: * Class responsible for the parameter table in the operation parameter popup
060: *
061: */
062: public class OperationParameterPopupTable extends JTable {
063:
064: /*******************************************************************/
065: // here we use raw model calls (i.e getValueAt and setValueAt) to
066: // access the model data because the inheritance pattern
067: // makes it hard to type these calls and to use the object model
068: /********************************************************************/
069:
070: /**
071: * Constructor
072: * @param model the table model of this table
073: */
074: public OperationParameterPopupTable(AbstractTableModel model) {
075: super (model);
076:
077: this .setRowHeight(25);
078: this .setPreferredScrollableViewportSize(new Dimension(250, 70));
079: this .setRowSelectionAllowed(true);
080: this .setColumnSelectionAllowed(false);
081: this .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
082: }
083:
084: /**
085: * Returns the cell editor for the table according to the column
086: * @param row the row to be considered
087: * @param column the column to be considered
088: * @return TableCellEditor the cell editor
089: */
090: public TableCellEditor getCellEditor(int row, int column) {
091:
092: if (row >= getRowCount())
093: return null;
094:
095: if (column == 0) { //parameter name
096: final JTextField nameField = new JTextField();
097: String o = ((String) getModel().getValueAt(row, column));
098: nameField.setText(o);
099: nameField.addKeyListener(new KeyListener() {
100: public void keyPressed(KeyEvent e) {
101: }
102:
103: public void keyReleased(KeyEvent e) {
104: }
105:
106: public void keyTyped(KeyEvent e) {
107: String txt = nameField.getText();
108: int selectionStart = nameField.getSelectionStart();
109: int selectionEnd = nameField.getSelectionEnd();
110: char typedKey = e.getKeyChar();
111: boolean acceptedKey = false;
112: if (selectionStart == 0) {
113: acceptedKey = Character
114: .isJavaIdentifierStart(typedKey);
115: } else {
116: acceptedKey = Character
117: .isJavaIdentifierPart(typedKey);
118: }
119: if (acceptedKey) {
120: if ((typedKey != KeyEvent.VK_BACK_SPACE)
121: && (typedKey != KeyEvent.VK_DELETE)) {
122: txt = txt.substring(0, selectionStart)
123: + typedKey
124: + txt.substring(selectionEnd);
125: } else if (typedKey == KeyEvent.VK_DELETE) {
126: txt = txt.substring(0, selectionStart)
127: + txt.substring(selectionEnd);
128: } else {
129: txt = txt.substring(0, selectionStart)
130: + txt.substring(selectionEnd);
131: }
132: } else {
133: getToolkit().beep();
134: }
135: //txt = WizardHelpers.capitalizeFirstLetter(txt);
136: nameField.setText(txt);
137: if ((typedKey == KeyEvent.VK_BACK_SPACE)
138: || (typedKey == KeyEvent.VK_DELETE))
139: nameField.setCaretPosition(selectionStart);
140: else {
141: if (acceptedKey) {
142: nameField
143: .setCaretPosition(selectionStart + 1);
144: } else {
145: nameField.setCaretPosition(selectionStart);
146: }
147: }
148:
149: e.consume();
150: }
151: });
152: return new JTextFieldCellEditor(nameField, this );
153: } else {
154: if (column == 1) { //parameter type
155: JComboBox typeBox = WizardHelpers
156: .instanciateTypeJComboBox();
157: Object o = getModel().getValueAt(row, column);
158: typeBox.setSelectedItem(o);
159: return new JComboBoxCellEditor(typeBox, this );
160: } else {
161: if (column == 2) { //parameter description
162: JTextField descrField = new JTextField();
163: String o = ((String) getModel().getValueAt(row,
164: column));
165: descrField.setText(o);
166: return new JTextFieldCellEditor(descrField, this );
167: }
168: }
169: return super .getCellEditor(row, column);
170: }
171: }
172:
173: /**
174: * Returns the cell renderer for the table according to the column
175: * @param row the row to be considered
176: * @param column the column to be considered
177: * @return TableCellRenderer the cell renderer
178: */
179: public TableCellRenderer getCellRenderer(int row, int column) {
180:
181: if (row >= getRowCount())
182: return null;
183:
184: if (column == 1) {
185: JComboBox typeBox = WizardHelpers
186: .instanciateTypeJComboBox();
187: Object o = getModel().getValueAt(row, column);
188: typeBox.setSelectedItem(o);
189: return new ComboBoxRenderer(typeBox, true, true);
190: }
191:
192: return super.getCellRenderer(row, column);
193: }
194: }
|