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 java.awt.event.ActionEvent;
044: import org.netbeans.modules.jmx.WizardHelpers;
045: import org.netbeans.modules.jmx.mbeanwizard.renderer.ComboBoxRenderer;
046: import java.awt.Dimension;
047: import java.awt.event.ActionListener;
048: import javax.swing.JTable;
049: import javax.swing.table.AbstractTableModel;
050: import javax.swing.JComboBox;
051: import javax.swing.JTextField;
052: import javax.swing.ListSelectionModel;
053: import javax.swing.table.TableCellEditor;
054: import javax.swing.table.TableCellRenderer;
055: import javax.swing.table.TableColumn;
056: import javax.swing.table.TableColumnModel;
057: import org.netbeans.modules.jmx.FireEvent;
058: import org.netbeans.modules.jmx.mbeanwizard.listener.AttributeTextFieldKeyListener;
059: import org.netbeans.modules.jmx.mbeanwizard.editor.JComboBoxCellEditor;
060: import org.netbeans.modules.jmx.mbeanwizard.tablemodel.MBeanAttributeTableModel;
061: import org.netbeans.modules.jmx.mbeanwizard.editor.JTextFieldCellEditor;
062:
063: /**
064: * Class responsible for the attribute table in the Method and Attribute Panel
065: *
066: */
067: public class AttributeTable extends JTable implements ActionListener {
068:
069: /*******************************************************************/
070: // here we use raw model calls (i.e getValueAt and setValueAt) to
071: // access the model data because the inheritance pattern
072: // makes it hard to type these calls and to use the object model
073: /********************************************************************/
074:
075: protected FireEvent wiz;
076: public static final int ACCESSWIDTH = 95;
077:
078: /**
079: * Constructor
080: * @param model the table model of this table
081: * @param wiz the wizard panel
082: */
083: public AttributeTable(AbstractTableModel model, FireEvent wiz) {
084: super (model);
085: this .wiz = wiz;
086: this .setColumnSelectionAllowed(false);
087: this .setCellSelectionEnabled(true);
088: this .setRowHeight(25);
089: this .setPreferredScrollableViewportSize(new Dimension(500, 70));
090: this .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
091: this .setRowSelectionAllowed(true);
092: this .setColumnSelectionAllowed(false);
093: ajustAccessColumnWidth();
094: }
095:
096: protected void ajustAccessColumnWidth() {
097: TableColumnModel colModel = this .getColumnModel();
098: TableColumn tc = colModel
099: .getColumn(MBeanAttributeTableModel.IDX_ATTR_ACCESS);
100: tc.setMaxWidth(ACCESSWIDTH);
101: tc.setMinWidth(ACCESSWIDTH);
102: tc.setPreferredWidth(ACCESSWIDTH);
103: }
104:
105: /**
106: * Returns the cell editor for the table according to the column
107: * @param row the row to be considered
108: * @param column the column to be considered
109: * @return TableCellEditor the cell editor
110: */
111: public TableCellEditor getCellEditor(int row, int column) {
112: if (row >= getRowCount())
113: return null;
114: if (column == 0) { // attribute name
115: final JTextField nameField = new JTextField();
116: String o = (String) getModel().getValueAt(row, column);
117: nameField.setText(o);
118: nameField
119: .addKeyListener(new AttributeTextFieldKeyListener());
120:
121: /* OLD
122: nameField.addKeyListener(new KeyListener() {
123: public void keyPressed(KeyEvent e) {}
124: public void keyReleased(KeyEvent e) {}
125: public void keyTyped(KeyEvent e) {
126: String txt = nameField.getText();
127: int selectionStart = nameField.getSelectionStart();
128: int selectionEnd = nameField.getSelectionEnd();
129: char typedKey = e.getKeyChar();
130: boolean acceptedKey = false;
131: if (selectionStart == 0) {
132: acceptedKey = Character.isJavaIdentifierStart(typedKey);
133: } else {
134: acceptedKey = Character.isJavaIdentifierPart(typedKey);
135: }
136: if (acceptedKey) {
137: if ((typedKey != KeyEvent.VK_BACK_SPACE) &&
138: (typedKey != KeyEvent.VK_DELETE)) {
139: txt = txt.substring(0, selectionStart) +
140: typedKey +
141: txt.substring(selectionEnd);
142: } else if (typedKey == KeyEvent.VK_DELETE) {
143: txt = txt.substring(0, selectionStart) +
144: txt.substring(selectionEnd);
145: } else {
146: txt = txt.substring(0, selectionStart) +
147: txt.substring(selectionEnd);
148: }
149: } else {
150: getToolkit().beep();
151: }
152: txt = WizardHelpers.capitalizeFirstLetter(txt);
153: nameField.setText(txt);
154: if ((typedKey == KeyEvent.VK_BACK_SPACE) ||
155: (typedKey == KeyEvent.VK_DELETE))
156: nameField.setCaretPosition(selectionStart);
157: else {
158: if (acceptedKey) {
159: nameField.setCaretPosition(selectionStart + 1);
160: } else {
161: nameField.setCaretPosition(selectionStart);
162: }
163: }
164:
165: e.consume();
166: }
167: });*/
168:
169: return new JTextFieldCellEditor(nameField, this );
170: } else {
171: if (column == 1) { //attribute type
172: JComboBox typeBox = WizardHelpers
173: .instanciateTypeJComboBox();
174: Object o = getModel().getValueAt(row, column);
175: typeBox.setSelectedItem(o);
176: typeBox.addActionListener(this );
177: return new JComboBoxCellEditor(typeBox, this );
178: } else {
179: if (column == 2) { //access mode
180: JComboBox accessBox = WizardHelpers
181: .instanciateAccessJComboBox();
182: accessBox.setName("attrAccessBox");// NOI18N
183: Object o = getModel().getValueAt(row, column);
184: accessBox.setSelectedItem(o);
185: return new JComboBoxCellEditor(accessBox, this );
186: } else {
187: if (column == 3) { //attribute description
188: JTextField descrField = new JTextField();
189: String o = (String) getModel().getValueAt(row,
190: column);
191: descrField.setText(o);
192: return new JTextFieldCellEditor(descrField,
193: this );
194: }
195: }
196: }
197: return super .getCellEditor(row, column);
198: }
199: }
200:
201: /**
202: * Returns the cell renderer for the table according to the column
203: * @param row the row to be considered
204: * @param column the column to be considered
205: * @return TableCellRenderer the cell renderer
206: */
207: public TableCellRenderer getCellRenderer(int row, int column) {
208: if (row >= getRowCount())
209: return null;
210:
211: if (column == 1) {
212: JComboBox typeBox = WizardHelpers
213: .instanciateTypeJComboBox();
214: return new ComboBoxRenderer(typeBox, true, true);
215: } else {
216: if (column == 2) {
217: JComboBox accessBox = WizardHelpers
218: .instanciateAccessJComboBox();
219: return new ComboBoxRenderer(accessBox);
220: }
221: }
222: return super .getCellRenderer(row, column);
223: }
224:
225: /**
226: * Returns the wizard panel
227: * @return AttributesWizardPanel the wizard panel
228: */
229: public FireEvent getWiz() {
230:
231: return this .wiz;
232: }
233:
234: public void actionPerformed(ActionEvent arg0) {
235: wiz.event();
236: }
237: }
|