View Javadoc

1   /***
2    * TableColumnEditor.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2003 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.swing.table.columnmodel.editor;
27  
28  import java.awt.event.ActionEvent;
29  import java.awt.event.ActionListener;
30  
31  import javax.swing.BoxLayout;
32  import javax.swing.JCheckBox;
33  import javax.swing.JComponent;
34  import javax.swing.JLabel;
35  import javax.swing.JPanel;
36  import javax.swing.JSpinner;
37  import javax.swing.JTextField;
38  import javax.swing.SpinnerNumberModel;
39  import javax.swing.table.TableColumn;
40  
41  import org.jcreme.swing.table.columnmodel.EditableTableColumnModel;
42  
43  /***
44   * This is the base class for TableColumnEditors. The TableColumnEditor
45   * instances directly edit the TableColumn they are associated to.
46   * 
47   * @author $Author: dbregeon $
48   * @version $Revision: 1.1 $
49   */
50  public class TableColumnEditor extends JComponent {
51      /***
52       * The Panel that contains the edition elements.
53       */
54      private final JPanel editorPanel = new JPanel();
55  
56      /***
57       * The CheckBox that signals whether the column is resizable.
58       */
59      private final JCheckBox resizableCheckBox = new JCheckBox();
60  
61      /***
62       * The Panel that holds the display position elements.
63       */
64      private final JPanel displayPanel = new JPanel();
65  
66      /***
67       * The CheckBox that signals whether the column is displayed.
68       */
69      private final JCheckBox displayCheckBox = new JCheckBox();
70  
71      /***
72       * The Label for the display position.
73       */
74      private final JLabel displayPositionLabel = new JLabel();
75  
76      /***
77       * The Spinner that enables to change the display position.
78       */
79      private final JSpinner displayPositionSpinner = new JSpinner();
80  
81      /***
82       * The Panel that contains the header value related fields.
83       */
84      private final JPanel headerValuePanel = new JPanel();
85  
86      /***
87       * The label for the header value.
88       */
89      private final JLabel headerValueLabel = new JLabel();
90  
91      /***
92       * The TextField that enables to change the header value.
93       */
94      private final JTextField headerValueTextField = new JTextField();
95  
96      /***
97       * Listener that updates the Editor when the displayCheckBox gets
98       * selected/deselected.
99       */
100     private final ActionListener displayCheckBoxListener = new ActionListener() {
101         public void actionPerformed(ActionEvent e) {
102             updateDisplayPosition();
103         }
104     };
105 
106     /***
107      * The TableColumn currently edited.
108      */
109     private TableColumn editedColumn = null;
110 
111     /***
112      * The TableColummnModel to which the editedColumn belongs.
113      */
114     private EditableTableColumnModel editedModel = null;
115 
116     /***
117      * The columnIndex when the column edition started.
118      */
119     private int initialColumnIndex = -1;
120 
121     /***
122      * 
123      *  
124      */
125     public TableColumnEditor() {
126         init();
127     }
128 
129     /***
130      * 
131      * @param column
132      * @param model
133      * @throws IllegalArgumentException
134      */
135     public void setEditedColumn(TableColumn column,
136             EditableTableColumnModel model) throws IllegalArgumentException {
137         if (column == null) {
138             throw new IllegalArgumentException(
139                     "Null is not an editable TableColumn.");
140         }
141         if (model == null) {
142             throw new IllegalArgumentException(
143                     "Null is not an editable TableColumnModel.");
144         }
145         this.editedModel = model;
146         this.editedColumn = column;
147         int max = this.editedModel.getColumnCount();
148         if (this.initialColumnIndex >= 0) {
149             max--;
150         }
151         ((SpinnerNumberModel) this.displayPositionSpinner.getModel())
152                 .setMaximum(new Integer(max));
153         this.resizableCheckBox.setSelected(this.editedColumn.getResizable());
154         this.initialColumnIndex = this.editedModel
155                 .getColumnIndex(this.editedColumn.getIdentifier());
156         this.displayCheckBox.setSelected(this.initialColumnIndex >= 0);
157 
158         this.displayPositionLabel.setEnabled(this.displayCheckBox.isSelected());
159         this.displayPositionSpinner.setEnabled(this.displayCheckBox
160                 .isSelected());
161         if (this.displayPositionSpinner.isEnabled()) {
162             this.displayPositionSpinner.setValue(new Integer(
163                     this.initialColumnIndex));
164         } else {
165             this.displayPositionSpinner.setValue(new Integer(max));
166         }
167         this.headerValueTextField.setText(this.editedColumn.getHeaderValue()
168                 .toString());
169     }
170 
171     /***
172      * 
173      * @return the currently edited column.
174      */
175     public TableColumn getEditedColumn() {
176         if (this.displayCheckBox.isSelected()) {
177             if (this.initialColumnIndex == -1) {
178                 this.initialColumnIndex = this.editedModel.getColumnCount();
179                 this.editedModel.showColumn(this.editedColumn);
180             }
181             this.editedModel.moveColumn(this.initialColumnIndex,
182                     ((Integer) this.displayPositionSpinner.getValue())
183                             .intValue());
184         } else {
185             this.editedModel.hideColumn(this.editedColumn);
186         }
187         this.editedColumn
188                 .setHeaderValue((this.headerValueTextField.getText() != null ? this.headerValueTextField
189                         .getText().trim()
190                         : this.editedColumn.getIdentifier()));
191         this.editedColumn.setResizable(this.resizableCheckBox.isSelected());
192         return this.editedColumn;
193     }
194 
195     protected final void init() {
196         setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER));
197 
198         this.resizableCheckBox.setText("Resizable");
199 
200         this.displayCheckBox.setText("Displayed");
201         this.displayPositionLabel.setText("at");
202         SpinnerNumberModel model = new SpinnerNumberModel();
203         model.setMinimum(new Integer(0));
204         model.setStepSize(new Integer(1));
205         this.displayPositionSpinner.setModel(model);
206 
207         this.displayPanel.setLayout(new BoxLayout(this.displayPanel,
208                 BoxLayout.Y_AXIS));
209         this.displayPanel.add(this.displayCheckBox);
210         this.displayPanel.add(this.displayPositionLabel);
211         this.displayPanel.add(this.displayPositionSpinner);
212 
213         this.headerValueLabel.setText("Column Title");
214 
215         this.headerValuePanel.setLayout(new BoxLayout(this.headerValuePanel,
216                 BoxLayout.Y_AXIS));
217         this.headerValuePanel.add(this.headerValueLabel);
218         this.headerValuePanel.add(this.headerValueTextField);
219 
220         this.editorPanel.setLayout(new BoxLayout(this.editorPanel,
221                 BoxLayout.Y_AXIS));
222         this.editorPanel.add(this.resizableCheckBox);
223         this.editorPanel.add(this.displayPanel);
224         this.editorPanel.add(this.headerValuePanel);
225         add(this.editorPanel);
226 
227         this.displayCheckBox.addActionListener(this.displayCheckBoxListener);
228     }
229 
230     protected final void updateDisplayPosition() {
231         this.displayPositionLabel.setEnabled(this.displayCheckBox.isSelected());
232         this.displayPositionSpinner.setEnabled(this.displayCheckBox
233                 .isSelected());
234     }
235 }