View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.models;
17  
18  import java.awt.Component;
19  
20  import javax.swing.AbstractCellEditor;
21  import javax.swing.JTable;
22  import javax.swing.JTextField;
23  import javax.swing.table.TableCellEditor;
24  
25  import org.xulux.api.gui.IWidget;
26  
27  /**
28   * A table cell editor for Xulux
29   *
30   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
31   * @version $Id: NyxTableCellEditor.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
32   */
33  public class NyxTableCellEditor extends AbstractCellEditor implements TableCellEditor {
34  
35      /**
36       * The widget
37       */
38      private IWidget widget;
39  
40      /**
41       * @param widget the widget
42       */
43      public NyxTableCellEditor(IWidget widget) {
44          this.widget = widget;
45      }
46      /**
47       * @see javax.swing.CellEditor#getCellEditorValue()
48       */
49      public Object getCellEditorValue() {
50          return widget.getGuiValue();
51      }
52      /**
53       * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
54       */
55      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
56          return (Component) this.widget.getNativeWidget();
57      }
58  
59      /**
60       * Stops the editing of cells.
61       * @param table the table
62       */
63      public void stopCellEditing(JTable table) {
64          Component component = table.getEditorComponent();
65          if (component instanceof JTextField) {
66              table.getModel().setValueAt(((JTextField) component).getText(), table.getEditingRow(), table.getEditingColumn());
67              stopCellEditing();
68          }
69      }
70  
71      /**
72       * @return the widget that is the editor.
73       */
74      public IWidget getWidget() {
75          return this.widget;
76      }
77  
78      /**
79       * Destroy the editor.
80       */
81      public void destroy() {
82          this.widget = null;
83      }
84  
85  }