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 javax.swing.JTable;
044: import javax.swing.table.AbstractTableModel;
045: import javax.swing.JTextField;
046: import javax.swing.table.TableCellEditor;
047: import org.netbeans.modules.jmx.mbeanwizard.editor.JTextFieldCellEditor;
048: import java.awt.Dimension;
049: import java.awt.event.KeyEvent;
050: import java.awt.event.KeyListener;
051: import javax.swing.ListSelectionModel;
052:
053: /**
054: * Class responsible for the exception table in the operation exception popup
055: *
056: */
057: public class OperationExceptionPopupTable extends JTable {
058:
059: /*******************************************************************/
060: // here we use raw model calls (i.e getValueAt and setValueAt) to
061: // access the model data because the inheritance pattern
062: // makes it hard to type these calls and to use the object model
063: /********************************************************************/
064:
065: /**
066: * Constructor
067: * @param model the table model of this table
068: */
069: public OperationExceptionPopupTable(AbstractTableModel model) {
070: super (model);
071: this .setRowHeight(25);
072: this .setPreferredScrollableViewportSize(new Dimension(250, 70));
073: this .setRowSelectionAllowed(true);
074: this .setColumnSelectionAllowed(false);
075: this .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
076: }
077:
078: /**
079: * Returns the cell editor for the table according to the column
080: * @param row the row to be considered
081: * @param column the column to be considered
082: * @return TableCellEditor the cell editor
083: */
084: public TableCellEditor getCellEditor(int row, int column) {
085:
086: if (row >= getRowCount())
087: return null;
088:
089: final JTextField genericField = new JTextField();
090: if (column == 1)
091: return new JTextFieldCellEditor(genericField, this );
092:
093: // here, one text field for the two columns of the popup is enough
094: final int col = column;
095: String o = ((String) getModel().getValueAt(row, column));
096: genericField.setText(o);
097: genericField.addKeyListener(new KeyListener() {
098: public void keyPressed(KeyEvent e) {
099: }
100:
101: public void keyReleased(KeyEvent e) {
102: }
103:
104: public void keyTyped(KeyEvent e) {
105: String txt = genericField.getText();
106: int selectionStart = genericField.getSelectionStart();
107: int selectionEnd = genericField.getSelectionEnd();
108: char typedKey = e.getKeyChar();
109: boolean acceptedKey = false;
110: if (selectionStart == 0) {
111: acceptedKey = Character
112: .isJavaIdentifierStart(typedKey);
113: } else {
114: acceptedKey = Character
115: .isJavaIdentifierPart(typedKey);
116: }
117: if ((!acceptedKey) && (typedKey == '.')) {
118: acceptedKey = true;
119: }
120: if (acceptedKey) {
121: if ((typedKey != KeyEvent.VK_BACK_SPACE)
122: && (typedKey != KeyEvent.VK_DELETE)) {
123: txt = txt.substring(0, selectionStart)
124: + typedKey
125: + txt.substring(selectionEnd);
126: } else if (typedKey == KeyEvent.VK_DELETE) {
127: txt = txt.substring(0, selectionStart)
128: + txt.substring(selectionEnd);
129: } else {
130: txt = txt.substring(0, selectionStart)
131: + txt.substring(selectionEnd);
132: }
133: } else {
134: getToolkit().beep();
135: }
136: if (txt.indexOf("..") == -1) { // NOI18N
137: genericField.setText(txt);
138: if ((typedKey == KeyEvent.VK_BACK_SPACE)
139: || (typedKey == KeyEvent.VK_DELETE))
140: genericField.setCaretPosition(selectionStart);
141: else {
142: if (acceptedKey) {
143: genericField
144: .setCaretPosition(selectionStart + 1);
145: } else {
146: genericField
147: .setCaretPosition(selectionStart);
148: }
149: }
150: } else {
151: getToolkit().beep();
152: }
153: e.consume();
154: }
155: });
156: return new JTextFieldCellEditor(genericField, this);
157: }
158:
159: }
|