View Javadoc

1   /*
2    * ColumnModelEditor.java
3    *
4    * Created on 10 janvier 2004, 21:38
5    */
6   
7   package org.jcreme.swing.table.columnmodel.editor;
8   
9   import java.awt.CardLayout;
10  import java.awt.Component;
11  import java.util.Enumeration;
12  import java.util.Hashtable;
13  
14  import javax.swing.DefaultListCellRenderer;
15  import javax.swing.DefaultListModel;
16  import javax.swing.JButton;
17  import javax.swing.JList;
18  import javax.swing.JPanel;
19  import javax.swing.JScrollPane;
20  import javax.swing.table.TableColumn;
21  
22  import org.jcreme.state.MementoException;
23  import org.jcreme.swing.table.columnmodel.EditableTableColumnModel;
24  import org.jcreme.swing.table.columnmodel.TableColumnType;
25  import org.jcreme.swing.table.columnmodel.state.EditableTableColumnModelMemento;
26  import org.jcreme.swing.table.state.TableColumnModelMemento;
27  
28  /***
29   * 
30   * @author dbregeon
31   */
32  public class ColumnModelEditor extends javax.swing.JDialog {
33      /***
34       * The TableColumnModel being edited.
35       */
36      private EditableTableColumnModel editedModel = null;
37  
38      /***
39       * The layout that displays the column editors.
40       */
41      private final CardLayout columnEditorPanelLayout = new CardLayout();
42  
43      /***
44       * Saves the Columns state before edition.
45       */
46      private final EditableTableColumnModelMemento memento = new EditableTableColumnModelMemento();
47  
48      /***
49       * The editors created to modify column.
50       */
51      private final Hashtable editorComponents = new Hashtable();
52  
53      /***
54       * The Editor currently displayed.
55       */
56      private TableColumnEditor displayedEditor = null;
57  
58      private final JPanel buttonPanel = new JPanel();
59  
60      private final JButton cancelButton = new JButton();
61  
62      private final JPanel columnEditorPanel = new JPanel();
63  
64      private final JList columnsList = new JList();
65  
66      private final JScrollPane columnsScrollPane = new JScrollPane();
67  
68      private final JButton okButton = new JButton();
69  
70      /***
71       * @see javax.swing.JDialog#JDialog(java.awt.Frame, boolean)
72       */
73      public ColumnModelEditor(java.awt.Frame parent, boolean modal) {
74          super(parent, modal);
75          initComponents();
76          this.columnEditorPanel.setLayout(this.columnEditorPanelLayout);
77          this.columnsList.setCellRenderer(new DefaultListCellRenderer() {
78              public Component getListCellRendererComponent(JList list,
79                      Object value, int index, boolean isSelected,
80                      boolean cellHasFocus) {
81                  Object displayValue = value;
82                  if (displayValue instanceof TableColumn) {
83                      displayValue = ((TableColumn) displayValue)
84                              .getHeaderValue();
85                  }
86                  return super.getListCellRendererComponent(list, displayValue,
87                          index, isSelected, cellHasFocus);
88              }
89          });
90      }
91  
92      /***
93       * 
94       * @param model
95       */
96      public void setEditedModel(EditableTableColumnModel model) {
97          this.editedModel = model;
98          if (this.editedModel != null) {
99              try {
100                 this.memento.updateMemento(this.editedModel);
101                 DefaultListModel listModel = new DefaultListModel();
102                 Enumeration enum = this.editedModel.getColumns();
103                 TableColumn[] hiddenColumns = model.getHiddenColumns();
104                 while (enum.hasMoreElements()) {
105                     addColumn((TableColumn) enum.nextElement(), listModel);
106                 }
107                 for (int j = 0; j < hiddenColumns.length; j++) {
108                     addColumn(hiddenColumns[j], listModel);
109                 }
110                 this.columnsList.setModel(listModel);
111             } catch (IllegalArgumentException e) {
112                 e.printStackTrace();
113             } catch (MementoException e) {
114             	e.printStackTrace();
115             }
116         }
117     }
118 
119     /***
120      * 
121      * @return the ColumnModel currently edited.
122      */
123     public EditableTableColumnModel getEditedModel() {
124         return this.editedModel;
125     }
126 
127     /***
128      * This method is called from within the constructor to initialize the form.
129      */
130     protected final void initComponents() {
131         addWindowListener(new java.awt.event.WindowAdapter() {
132             public void windowClosing(java.awt.event.WindowEvent evt) {
133                 closeDialog(evt);
134             }
135         });
136 
137         this.columnsList
138                 .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
139                     public void valueChanged(
140                             javax.swing.event.ListSelectionEvent evt) {
141                         columnsListValueChanged(evt);
142                     }
143                 });
144 
145         this.columnsScrollPane.setViewportView(this.columnsList);
146 
147         getContentPane()
148                 .add(this.columnsScrollPane, java.awt.BorderLayout.WEST);
149 
150         getContentPane().add(this.columnEditorPanel,
151                 java.awt.BorderLayout.CENTER);
152 
153         this.buttonPanel.setLayout(new java.awt.FlowLayout(
154                 java.awt.FlowLayout.RIGHT));
155 
156         this.okButton.setText("OK");
157         this.okButton.addActionListener(new java.awt.event.ActionListener() {
158             public void actionPerformed(java.awt.event.ActionEvent evt) {
159                 okButtonActionPerformed(evt);
160             }
161         });
162 
163         this.buttonPanel.add(this.okButton);
164 
165         this.cancelButton.setText("Cancel");
166         this.cancelButton
167                 .addActionListener(new java.awt.event.ActionListener() {
168                     public void actionPerformed(java.awt.event.ActionEvent evt) {
169                         cancel();
170                     }
171                 });
172 
173         this.buttonPanel.add(this.cancelButton);
174 
175         getContentPane().add(this.buttonPanel, java.awt.BorderLayout.SOUTH);
176 
177         pack();
178     }
179 
180     protected final void columnsListValueChanged(
181             javax.swing.event.ListSelectionEvent evt) {
182         if (this.displayedEditor != null) {
183             // Validates the current edition.
184             this.displayedEditor.getEditedColumn();
185         }
186         if (this.columnsList.getSelectedValue() != null) {
187             TableColumnType type = TableColumnType
188                     .getFromImplementation(this.columnsList.getSelectedValue()
189                             .getClass());
190             try {
191                 this.displayedEditor = (TableColumnEditor) this.editorComponents
192                         .get(type.getName());
193                 this.displayedEditor.setEditedColumn(
194                         (TableColumn) this.columnsList.getSelectedValue(),
195                         this.editedModel);
196                 this.columnEditorPanelLayout.show(this.columnEditorPanel, type
197                         .getName());
198             } catch (IllegalArgumentException e) {
199                 e.printStackTrace();
200             }
201         }
202     }
203 
204     protected final void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
205         if (this.displayedEditor != null) {
206             // Validates the current edition.
207             this.displayedEditor.getEditedColumn();
208         }
209         close();
210     }
211 
212     protected final void closeDialog(java.awt.event.WindowEvent evt) {
213         cancel();
214     }
215 
216     protected void cancel() {
217     	try {
218         this.editedModel = new EditableTableColumnModel();
219         this.memento
220                 .updateObject(this.editedModel);
221     	} catch (MementoException e) {
222     		e.printStackTrace();
223     	}
224         close();
225     }
226 
227     protected void close() {
228         setVisible(false);
229         dispose();
230     }
231 
232     protected void addColumn(TableColumn c, DefaultListModel listModel) {
233         TableColumnType type = TableColumnType.getFromImplementation(c
234                 .getClass());
235         if ((type != null) && (type.getTableColumnMementoClass() != null)
236                 && (type.getTableColumnEditorClass() != null)) {
237             try {
238                 if (this.editorComponents.get(type.getName()) == null) {
239                     TableColumnEditor editor = (TableColumnEditor) type
240                             .getTableColumnEditorClass().newInstance();
241                     this.editorComponents.put(type.getName(), editor);
242                     this.columnEditorPanel.add(editor, type.getName());
243                 }
244                 listModel.addElement(c);
245             } catch (InstantiationException e) {
246                 e.printStackTrace();
247             } catch (IllegalAccessException e) {
248                 e.printStackTrace();
249             }
250         }
251     }
252 }