KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > TransientFieldsPanel


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc;
5
6 import org.dijon.Button;
7 import org.dijon.ContainerResource;
8
9 import com.tc.admin.common.XContainer;
10 import com.tc.admin.common.XTable;
11 import com.terracottatech.config.DsoApplication;
12 import com.terracottatech.config.TransientFields;
13
14 import java.awt.event.ActionEvent JavaDoc;
15 import java.awt.event.ActionListener JavaDoc;
16
17 import javax.swing.JOptionPane JavaDoc;
18 import javax.swing.event.ListSelectionEvent JavaDoc;
19 import javax.swing.event.ListSelectionListener JavaDoc;
20 import javax.swing.event.TableModelEvent JavaDoc;
21 import javax.swing.event.TableModelListener JavaDoc;
22 import javax.swing.table.DefaultTableModel JavaDoc;
23
24 public class TransientFieldsPanel extends XContainer
25   implements TableModelListener JavaDoc
26 {
27   private DsoApplication m_dsoApp;
28   private TransientFields m_transientFields;
29   private XTable m_transientTable;
30   private TransientTableModel m_transientTableModel;
31   private Button m_addButton;
32   private ActionListener JavaDoc m_addListener;
33   private Button m_removeButton;
34   private ActionListener JavaDoc m_removeListener;
35   private ListSelectionListener JavaDoc m_transientsListener;
36   
37   public TransientFieldsPanel() {
38     super();
39   }
40   
41   public void load(ContainerResource containerRes) {
42     super.load(containerRes);
43
44     m_transientTable = (XTable)findComponent("TransientFieldTable");
45     m_transientTable.setModel(m_transientTableModel = new TransientTableModel());
46     m_transientsListener = new ListSelectionListener JavaDoc() {
47       public void valueChanged(ListSelectionEvent JavaDoc lse) {
48         if(!lse.getValueIsAdjusting()) {
49           int[] sel = m_transientTable.getSelectedRows();
50           m_removeButton.setEnabled(sel != null && sel.length > 0);
51         }
52       }
53     };
54     
55     m_addButton = (Button)findComponent("AddTransientButton");
56     m_addListener = new ActionListener JavaDoc() {
57       public void actionPerformed(ActionEvent JavaDoc ae) {
58         String JavaDoc field = JOptionPane.showInputDialog("Transient field", "");
59         
60         if(field != null) {
61           field = field.trim();
62           
63           if(field != null && field.length() > 0 && !isTransient(field)) {
64             internalAddTransient(field);
65           }
66         }
67       }
68     };
69
70     m_removeButton = (Button)findComponent("RemoveTransientButton");
71     m_removeListener = new ActionListener JavaDoc() {
72       public void actionPerformed(ActionEvent JavaDoc ae) {
73         TransientFields tf = ensureTransientFields();
74         int[] selection = m_transientTable.getSelectedRows();
75         
76         for(int i = selection.length-1; i >= 0; i--) {
77           tf.removeFieldName(selection[i]);
78         }
79         m_transientTableModel.removeRows(selection);
80       }
81     };
82   }
83
84   public boolean hasAnySet() {
85     return m_transientFields != null &&
86            m_transientFields.sizeOfFieldNameArray() > 0;
87   }
88   
89   private TransientFields ensureTransientFields() {
90     if(m_transientFields == null) {
91       ensureXmlObject();
92     }
93     return m_transientFields;
94   }
95   
96   public void ensureXmlObject() {
97     if(m_transientFields == null) {
98       removeListeners();
99       m_transientFields = m_dsoApp.addNewTransientFields();
100       updateChildren();
101       addListeners();
102     }
103   }
104   
105   private void syncModel() {
106     if(!hasAnySet() && m_dsoApp.getTransientFields() != null){
107       m_dsoApp.unsetTransientFields();
108       m_transientFields = null;
109     }
110
111     SessionIntegratorFrame frame =
112       (SessionIntegratorFrame)getAncestorOfClass(SessionIntegratorFrame.class);
113     frame.modelChanged();
114   }
115   
116   private void addListeners() {
117     m_transientTableModel.addTableModelListener(this);
118     m_transientTable.getSelectionModel().addListSelectionListener(m_transientsListener);
119     m_addButton.addActionListener(m_addListener);
120     m_removeButton.addActionListener(m_removeListener);
121   }
122   
123   private void removeListeners() {
124     m_transientTableModel.removeTableModelListener(this);
125     m_transientTable.getSelectionModel().removeListSelectionListener(m_transientsListener);
126     m_addButton.removeActionListener(m_addListener);
127     m_removeButton.removeActionListener(m_removeListener);
128   }
129   
130   private void updateChildren() {
131     m_transientTableModel.clear();
132
133     if(m_transientFields != null) {
134       String JavaDoc[] transients = m_transientFields.getFieldNameArray();
135   
136       for(int i = 0; i < transients.length; i++) {
137         m_transientTableModel.addField(transients[i]);
138       }
139     }
140   }
141
142   public void setup(DsoApplication dsoApp) {
143     setEnabled(true);
144     removeListeners();
145
146     m_dsoApp = dsoApp;
147     m_transientFields = m_dsoApp != null ?
148                         m_dsoApp.getTransientFields() :
149                         null;
150
151     updateChildren();
152     addListeners();
153   }
154   
155   public void tearDown() {
156     removeListeners();
157     
158     m_dsoApp = null;
159     m_transientFields = null;
160
161     m_transientTableModel.clear();
162     
163     setEnabled(false);
164   }
165   
166   class TransientTableModel extends DefaultTableModel JavaDoc {
167     TransientTableModel() {
168       super();
169       setColumnIdentifiers(new String JavaDoc[]{"Transient fields"});
170     }
171     
172     void clear() {
173       setRowCount(0);
174     }
175     
176     void setTransientFields(TransientFields fields) {
177       clear();
178       
179       if(fields != null) {
180         int count = fields.sizeOfFieldNameArray();
181         
182         for(int i = 0; i < count; i++) {
183           addField(fields.getFieldNameArray(i));
184         }
185       }
186     }
187       
188     void addField(String JavaDoc fieldName) {
189       addRow(new Object JavaDoc[] {fieldName});
190     }
191     
192     int indexOf(String JavaDoc fieldName) {
193       int count = getRowCount();
194       
195       for(int i = 0; i < count; i++) {
196         if(((String JavaDoc)getValueAt(i, 0)).equals(fieldName)) {
197           return i;
198         }
199       }
200       
201       return -1;
202     }
203     
204     public void setValueAt(Object JavaDoc value, int row, int col) {
205       m_transientFields.setFieldNameArray(row, (String JavaDoc)value);
206       super.setValueAt(value, row, col);
207     }
208
209     void removeRows(int[] rows) {
210       removeTableModelListener(TransientFieldsPanel.this);
211       for(int i = rows.length-1; i >= 0; i--) {
212         removeRow(rows[i]);
213       }
214       addTableModelListener(TransientFieldsPanel.this);
215       syncModel();
216     }
217   }
218   
219   public void tableChanged(TableModelEvent JavaDoc tme) {
220     syncModel();
221   }
222   
223   private void internalAddTransient(String JavaDoc fieldName) {
224     ensureTransientFields().addFieldName(fieldName);
225     m_transientTableModel.addField(fieldName);
226     
227     int row = m_transientTableModel.getRowCount()-1;
228     m_transientTable.setRowSelectionInterval(row, row);
229   }
230
231   private void internalRemoveTransient(String JavaDoc fieldName) {
232     int row = m_transientTableModel.indexOf(fieldName);
233     
234     if(row >= 0) {
235       ensureTransientFields().removeFieldName(row);
236       m_transientTableModel.removeRow(row);
237       
238       if(row > 0) {
239         row = Math.min(m_transientTableModel.getRowCount()-1, row);
240         m_transientTable.setRowSelectionInterval(row, row);
241       }
242     }
243   }
244   
245   public boolean isTransient(String JavaDoc fieldName) {
246     TransientFields transients = ensureTransientFields();
247     int count = transients.sizeOfFieldNameArray();
248     
249     for(int i = 0; i < count; i++) {
250       if(fieldName.equals(transients.getFieldNameArray(i))) {
251         return true;
252       }
253     }
254     
255     return false;
256   }
257   
258   public void ensureTransient(String JavaDoc fieldName) {
259     if(!isTransient(fieldName)) {
260       internalAddTransient(fieldName);
261     }
262   }
263   
264   public void ensureNotTransient(String JavaDoc fieldName) {
265     if(isTransient(fieldName)) {
266       internalRemoveTransient(fieldName);
267     }
268   }
269 }
270
Popular Tags