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:
042: package org.netbeans.modules.jmx.mbeanwizard.table;
043:
044: import java.awt.BorderLayout;
045: import javax.swing.JCheckBox;
046: import javax.swing.JTextField;
047: import javax.swing.table.AbstractTableModel;
048: import javax.swing.table.TableCellEditor;
049: import javax.swing.table.TableCellRenderer;
050: import org.netbeans.modules.jmx.mbeanwizard.renderer.CheckBoxRenderer;
051: import org.netbeans.modules.jmx.mbeanwizard.editor.JCheckBoxCellEditor;
052: import javax.swing.JTable;
053: import java.awt.event.ActionEvent;
054: import java.awt.event.ActionListener;
055: import javax.swing.DefaultCellEditor;
056: import javax.swing.JComboBox;
057: import javax.swing.JButton;
058: import javax.swing.JPanel;
059: import javax.swing.SwingConstants;
060: import javax.swing.table.TableColumn;
061: import javax.swing.table.TableColumnModel;
062: import org.netbeans.modules.jmx.WizardHelpers;
063: import org.netbeans.modules.jmx.mbeanwizard.MBeanOperationPanel.OperationWizardPanel;
064: import org.netbeans.modules.jmx.mbeanwizard.editor.JComboBoxCellEditor;
065: import org.netbeans.modules.jmx.mbeanwizard.listener.OperationTextFieldKeyListener;
066: import org.netbeans.modules.jmx.mbeanwizard.renderer.TextFieldRenderer;
067: import org.netbeans.modules.jmx.mbeanwizard.tablemodel.AbstractJMXTableModel;
068: import org.netbeans.modules.jmx.mbeanwizard.tablemodel.MBeanWrapperOperationTableModel;
069: import org.netbeans.modules.jmx.WizardConstants;
070: import org.netbeans.modules.jmx.mbeanwizard.editor.OperationExceptionPanelEditor;
071: import org.netbeans.modules.jmx.mbeanwizard.editor.OperationParameterPanelEditor;
072: import org.netbeans.modules.jmx.mbeanwizard.editor.JTextFieldCellEditor;
073: import org.netbeans.modules.jmx.mbeanwizard.popup.OperationExceptionPopup;
074: import org.netbeans.modules.jmx.mbeanwizard.popup.OperationParameterPopup;
075: import org.netbeans.modules.jmx.mbeanwizard.renderer.EmptyRenderer;
076: import org.netbeans.modules.jmx.mbeanwizard.renderer.OperationParameterPanelRenderer;
077: import org.netbeans.modules.jmx.mbeanwizard.renderer.OperationExceptionPanelRenderer;
078: import org.netbeans.modules.jmx.mbeanwizard.renderer.ComboBoxRenderer;
079: import org.netbeans.modules.jmx.mbeanwizard.popup.WrapperOperationExceptionPopup;
080: import org.netbeans.modules.jmx.mbeanwizard.popup.WrapperOperationParameterPopup;
081: import org.netbeans.modules.jmx.mbeanwizard.tablemodel.MBeanWrapperAttributeTableModel;
082:
083: /**
084: *
085: * @author an156382
086: */
087: public class WrapperOperationTable extends OperationTable {
088:
089: private static final int SELWIDTH = 55;
090:
091: final JTable table;
092: final OperationWizardPanel wiz;
093: private MBeanWrapperOperationTableModel model;
094:
095: /** Creates a new instance of WrapperOperationTable */
096: public WrapperOperationTable(JPanel ancestorPanel,
097: AbstractTableModel model, OperationWizardPanel wiz) {
098: super (ancestorPanel, model, wiz);
099: this .table = this ;
100: this .model = (MBeanWrapperOperationTableModel) model;
101: this .wiz = wiz;
102: ajustSelectionColumnWidth();
103: }
104:
105: private void ajustSelectionColumnWidth() {
106: TableColumnModel colModel = this .getColumnModel();
107: TableColumn tc = colModel
108: .getColumn(MBeanWrapperAttributeTableModel.IDX_ATTR_SELECTION);
109: tc.setMaxWidth(SELWIDTH);
110: tc.setMinWidth(SELWIDTH);
111: tc.setPreferredWidth(SELWIDTH);
112: }
113:
114: /**
115: * Returns the cell editor for the table according to the column
116: * @param row the row to be considered
117: * @param column the column to be considered
118: * @return TableCellEditor the cell editor
119: */
120: public TableCellEditor getCellEditor(final int row, final int column) {
121:
122: if (row >= getRowCount())
123: return null;
124:
125: int firstEditableRow = ((MBeanWrapperOperationTableModel) getModel())
126: .getFirstEditableRow();
127:
128: boolean selection = (Boolean) getModel().getValueAt(row, 0);
129:
130: if (column == 0) { //selection
131: final JCheckBox selBox = new JCheckBox();
132: selBox.setSelected((Boolean) getModel().getValueAt(row,
133: column));
134: selBox.setHorizontalAlignment(SwingConstants.CENTER);
135: selBox.setEnabled(true);
136: selBox.addActionListener(new ActionListener() {
137: public void actionPerformed(ActionEvent evt) {
138: getModel().setValueAt(selBox.isSelected(), row,
139: column);
140: ((AbstractJMXTableModel) getModel())
141: .fireTableDataChanged();
142: wiz.event();
143: }
144: });
145: //return new DefaultCellEditor(selBox);
146: return new JCheckBoxCellEditor(selBox, this );
147: } else if (column == 1) { //operation name
148: JTextField nameField = new JTextField();
149: String o = (String) getModel().getValueAt(row, column);
150: nameField.setText(o);
151: nameField
152: .addKeyListener(new OperationTextFieldKeyListener());
153: return new JTextFieldCellEditor(nameField, this );
154: } else if (column == 2) { //operation return type
155: if (row < firstEditableRow) {
156: JTextField typeField = new JTextField();
157: String o = (String) getModel().getValueAt(row, column);
158: typeField.setText(o);
159: return new DefaultCellEditor(typeField);
160: } else {
161: JComboBox jcb = WizardHelpers
162: .instanciateRetTypeJComboBox();
163: jcb.setName("methTypeBox");// NOI18N
164: return new JComboBoxCellEditor(jcb, this );
165: }
166: } else if (column == 3) { //parameter panel
167: final JTextField paramField = new JTextField();
168: paramField.setEditable(false);
169: paramField.setName("methParamTextField");// NOI18N
170: JButton paramButton = new JButton(
171: WizardConstants.MBEAN_POPUP_EDIT_BUTTON);
172: paramButton.setName("methAddParamButton");// NOI18N
173: paramButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
174: final int editedRow = row;
175:
176: int nbParam = model.getWrapperOperation(row)
177: .getParametersSize();
178: final boolean nonEditableParams = ((row < firstEditableRow) && (nbParam > 0));
179:
180: paramButton.addActionListener(new ActionListener() {
181: public void actionPerformed(ActionEvent evt) {
182: OperationParameterPopup methParamPopup;
183: if (nonEditableParams)
184: methParamPopup = new WrapperOperationParameterPopup(
185: ancestorPanel, model, paramField,
186: editedRow, wiz);
187: else
188: methParamPopup = new OperationParameterPopup(
189: ancestorPanel, model, paramField,
190: editedRow, wiz);
191: }
192: });
193:
194: boolean stateBut = (row >= firstEditableRow)
195: || ((row < firstEditableRow) && selection && (nbParam > 0));
196: paramButton.setEnabled(stateBut);
197:
198: JPanel panel = new JPanel(new BorderLayout());
199: panel.setName("methParamColJPanel");// NOI18N
200: panel.add(paramField, BorderLayout.CENTER);
201: panel.add(paramButton, BorderLayout.EAST);
202:
203: OperationParameterPanelEditor operationParamEditor = new OperationParameterPanelEditor(
204: model, panel, paramField, editedRow);
205:
206: //TODO Edit OperationParameterPanelEditor
207: //return new OperationParameterPanelEditor(panel,
208: //paramField, paramButton);
209: return operationParamEditor;
210: } else if (column == 4) { //operation exceptions
211: final JTextField excepField = new JTextField();
212: excepField.setEditable(false);
213: excepField.setName("methExcepTextField");// NOI18N
214: JButton excepButton = new JButton(
215: WizardConstants.MBEAN_POPUP_EDIT_BUTTON);
216: excepButton.setName("methAddExcepJButton");// NOI18N
217: excepButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
218: final int editedRow = row;
219:
220: int nbExcep = model.getWrapperOperation(row)
221: .getExceptionsSize();
222: final boolean nonEditableExcept = ((row < firstEditableRow) && (nbExcep > 0));
223: excepButton.addActionListener(new ActionListener() {
224: public void actionPerformed(ActionEvent evt) {
225: OperationExceptionPopup opExceptionPopup;
226: if (nonEditableExcept)
227: opExceptionPopup = new WrapperOperationExceptionPopup(
228: ancestorPanel, model, excepField,
229: editedRow);
230: else
231: opExceptionPopup = new OperationExceptionPopup(
232: ancestorPanel, model, excepField,
233: editedRow);
234: }
235: });
236:
237: boolean stateBut = (row >= firstEditableRow)
238: || ((row < firstEditableRow) && selection && (nbExcep > 0));
239: excepButton.setEnabled(stateBut);
240:
241: JPanel panel = new JPanel(new BorderLayout());
242: panel.add(excepField, BorderLayout.CENTER);
243: panel.add(excepButton, BorderLayout.EAST);
244:
245: OperationExceptionPanelEditor methExcepEditor = new OperationExceptionPanelEditor(
246: model, panel, excepField, row);
247:
248: //TODO Edit OperationExceptionPanelEditor
249: //return new OperationExceptionPanelEditor(panel,
250: //excepField, excepButton);
251: return methExcepEditor;
252: } else if (column == 5) {
253: JTextField descrField = new JTextField();
254: String o = (String) getModel().getValueAt(row, column);
255: descrField.setText(o);
256: descrField.setEnabled(true);
257: return new JTextFieldCellEditor(descrField, this );
258: }
259:
260: return super .getCellEditor(row, column);
261: }
262:
263: /**
264: * Returns the cell renderer for the table according to the column
265: * @param row the row to be considered
266: * @param column the column to be considered
267: * @return TableCellRenderer the cell renderer
268: */
269: public TableCellRenderer getCellRenderer(int row, int column) {
270:
271: if (row >= getRowCount())
272: return null;
273:
274: int editableRow = model.getFirstEditableRow();
275: boolean selection = (Boolean) getModel().getValueAt(row, 0);
276:
277: if (column == 0) { //selection
278: if (row < editableRow) {
279: JCheckBox cb = new JCheckBox();
280: cb.setEnabled(true);
281: return new CheckBoxRenderer(cb);
282: } else
283: return new EmptyRenderer(new JTextField());
284: } else if (column == 1) { //operation Name
285: boolean ok = (row < editableRow);
286: //if (ok)
287: //return new WrapperTextFieldRenderer(new JTextField(),true,false);
288: return new TextFieldRenderer(new JTextField(), true, !ok);
289: //else
290: // return new TextFieldRenderer(new JTextField(),true,true);
291: } else if (column == 2) {
292: if (row < editableRow)
293: //return new WrapperTextFieldRenderer(new JTextField(),true,false);
294: return new TextFieldRenderer(new JTextField(), true,
295: false);
296: else {
297: JComboBox jcb = WizardHelpers
298: .instanciateRetTypeJComboBox();
299: return new ComboBoxRenderer(jcb, true, true);
300: }
301: } else if (column == 3) {
302: JTextField paramField = new JTextField();
303: paramField.setEditable(false);
304: paramField.setName("methParamTextField");// NOI18N
305: JButton paramButton = new JButton(
306: WizardConstants.MBEAN_POPUP_EDIT_BUTTON);
307: paramButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
308: int nbParam = model.getWrapperOperation(row)
309: .getParametersSize();
310: boolean stateBut = (row >= editableRow)
311: || ((row < editableRow) && selection && (nbParam > 0));
312: paramButton.setEnabled(stateBut);
313: JPanel panel = new JPanel(new BorderLayout());
314: panel.add(paramField, BorderLayout.CENTER);
315: panel.add(paramButton, BorderLayout.EAST);
316: return new OperationParameterPanelRenderer(panel,
317: paramField);
318: } else if (column == 4) {
319: JTextField excepField = new JTextField();
320: excepField.setEditable(false);
321: excepField.setName("methExcepTextField");// NOI18N
322: JButton excepButton = new JButton(
323: WizardConstants.MBEAN_POPUP_EDIT_BUTTON);
324: excepButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
325:
326: int nbExcep = model.getWrapperOperation(row)
327: .getExceptionsSize();
328: boolean stateBut = (row >= editableRow)
329: || ((row < editableRow) && selection && (nbExcep > 0));
330: excepButton.setEnabled(stateBut);
331:
332: JPanel panel = new JPanel(new BorderLayout());
333: panel.add(excepField, BorderLayout.CENTER);
334: panel.add(excepButton, BorderLayout.EAST);
335: return new OperationExceptionPanelRenderer(panel,
336: excepField);
337: } else if (column == 5) {
338: JTextField txt = new JTextField();
339:
340: return new TextFieldRenderer(txt, true, selection);
341: } else
342: return super .getCellRenderer(row, column);
343: }
344:
345: public boolean isCellEditable(int row, int col) {
346:
347: int editableRow = ((MBeanWrapperOperationTableModel) getModel())
348: .getFirstEditableRow();
349: boolean isChecked = (Boolean) getModel().getValueAt(row, 0);
350:
351: if (row < editableRow) {
352: if (isChecked)
353: return ((col == 0) || (col == 3) || (col == 4) || (col == 5));
354: else
355: return (col == 0);
356: } else
357: return (col != 0);
358: }
359: }
|