001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc;
005:
006: import org.dijon.Button;
007: import org.dijon.ContainerResource;
008:
009: import com.tc.admin.common.XContainer;
010: import com.tc.admin.common.XTable;
011: import com.terracottatech.config.DsoApplication;
012: import com.terracottatech.config.TransientFields;
013:
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016:
017: import javax.swing.JOptionPane;
018: import javax.swing.event.ListSelectionEvent;
019: import javax.swing.event.ListSelectionListener;
020: import javax.swing.event.TableModelEvent;
021: import javax.swing.event.TableModelListener;
022: import javax.swing.table.DefaultTableModel;
023:
024: public class TransientFieldsPanel extends XContainer implements
025: TableModelListener {
026: private DsoApplication m_dsoApp;
027: private TransientFields m_transientFields;
028: private XTable m_transientTable;
029: private TransientTableModel m_transientTableModel;
030: private Button m_addButton;
031: private ActionListener m_addListener;
032: private Button m_removeButton;
033: private ActionListener m_removeListener;
034: private ListSelectionListener m_transientsListener;
035:
036: public TransientFieldsPanel() {
037: super ();
038: }
039:
040: public void load(ContainerResource containerRes) {
041: super .load(containerRes);
042:
043: m_transientTable = (XTable) findComponent("TransientFieldTable");
044: m_transientTable
045: .setModel(m_transientTableModel = new TransientTableModel());
046: m_transientsListener = new ListSelectionListener() {
047: public void valueChanged(ListSelectionEvent lse) {
048: if (!lse.getValueIsAdjusting()) {
049: int[] sel = m_transientTable.getSelectedRows();
050: m_removeButton.setEnabled(sel != null
051: && sel.length > 0);
052: }
053: }
054: };
055:
056: m_addButton = (Button) findComponent("AddTransientButton");
057: m_addListener = new ActionListener() {
058: public void actionPerformed(ActionEvent ae) {
059: String field = JOptionPane.showInputDialog(
060: "Transient field", "");
061:
062: if (field != null) {
063: field = field.trim();
064:
065: if (field != null && field.length() > 0
066: && !isTransient(field)) {
067: internalAddTransient(field);
068: }
069: }
070: }
071: };
072:
073: m_removeButton = (Button) findComponent("RemoveTransientButton");
074: m_removeListener = new ActionListener() {
075: public void actionPerformed(ActionEvent ae) {
076: TransientFields tf = ensureTransientFields();
077: int[] selection = m_transientTable.getSelectedRows();
078:
079: for (int i = selection.length - 1; i >= 0; i--) {
080: tf.removeFieldName(selection[i]);
081: }
082: m_transientTableModel.removeRows(selection);
083: }
084: };
085: }
086:
087: public boolean hasAnySet() {
088: return m_transientFields != null
089: && m_transientFields.sizeOfFieldNameArray() > 0;
090: }
091:
092: private TransientFields ensureTransientFields() {
093: if (m_transientFields == null) {
094: ensureXmlObject();
095: }
096: return m_transientFields;
097: }
098:
099: public void ensureXmlObject() {
100: if (m_transientFields == null) {
101: removeListeners();
102: m_transientFields = m_dsoApp.addNewTransientFields();
103: updateChildren();
104: addListeners();
105: }
106: }
107:
108: private void syncModel() {
109: if (!hasAnySet() && m_dsoApp.getTransientFields() != null) {
110: m_dsoApp.unsetTransientFields();
111: m_transientFields = null;
112: }
113:
114: SessionIntegratorFrame frame = (SessionIntegratorFrame) getAncestorOfClass(SessionIntegratorFrame.class);
115: frame.modelChanged();
116: }
117:
118: private void addListeners() {
119: m_transientTableModel.addTableModelListener(this );
120: m_transientTable.getSelectionModel().addListSelectionListener(
121: m_transientsListener);
122: m_addButton.addActionListener(m_addListener);
123: m_removeButton.addActionListener(m_removeListener);
124: }
125:
126: private void removeListeners() {
127: m_transientTableModel.removeTableModelListener(this );
128: m_transientTable.getSelectionModel()
129: .removeListSelectionListener(m_transientsListener);
130: m_addButton.removeActionListener(m_addListener);
131: m_removeButton.removeActionListener(m_removeListener);
132: }
133:
134: private void updateChildren() {
135: m_transientTableModel.clear();
136:
137: if (m_transientFields != null) {
138: String[] transients = m_transientFields.getFieldNameArray();
139:
140: for (int i = 0; i < transients.length; i++) {
141: m_transientTableModel.addField(transients[i]);
142: }
143: }
144: }
145:
146: public void setup(DsoApplication dsoApp) {
147: setEnabled(true);
148: removeListeners();
149:
150: m_dsoApp = dsoApp;
151: m_transientFields = m_dsoApp != null ? m_dsoApp
152: .getTransientFields() : null;
153:
154: updateChildren();
155: addListeners();
156: }
157:
158: public void tearDown() {
159: removeListeners();
160:
161: m_dsoApp = null;
162: m_transientFields = null;
163:
164: m_transientTableModel.clear();
165:
166: setEnabled(false);
167: }
168:
169: class TransientTableModel extends DefaultTableModel {
170: TransientTableModel() {
171: super ();
172: setColumnIdentifiers(new String[] { "Transient fields" });
173: }
174:
175: void clear() {
176: setRowCount(0);
177: }
178:
179: void setTransientFields(TransientFields fields) {
180: clear();
181:
182: if (fields != null) {
183: int count = fields.sizeOfFieldNameArray();
184:
185: for (int i = 0; i < count; i++) {
186: addField(fields.getFieldNameArray(i));
187: }
188: }
189: }
190:
191: void addField(String fieldName) {
192: addRow(new Object[] { fieldName });
193: }
194:
195: int indexOf(String fieldName) {
196: int count = getRowCount();
197:
198: for (int i = 0; i < count; i++) {
199: if (((String) getValueAt(i, 0)).equals(fieldName)) {
200: return i;
201: }
202: }
203:
204: return -1;
205: }
206:
207: public void setValueAt(Object value, int row, int col) {
208: m_transientFields.setFieldNameArray(row, (String) value);
209: super .setValueAt(value, row, col);
210: }
211:
212: void removeRows(int[] rows) {
213: removeTableModelListener(TransientFieldsPanel.this );
214: for (int i = rows.length - 1; i >= 0; i--) {
215: removeRow(rows[i]);
216: }
217: addTableModelListener(TransientFieldsPanel.this );
218: syncModel();
219: }
220: }
221:
222: public void tableChanged(TableModelEvent tme) {
223: syncModel();
224: }
225:
226: private void internalAddTransient(String fieldName) {
227: ensureTransientFields().addFieldName(fieldName);
228: m_transientTableModel.addField(fieldName);
229:
230: int row = m_transientTableModel.getRowCount() - 1;
231: m_transientTable.setRowSelectionInterval(row, row);
232: }
233:
234: private void internalRemoveTransient(String fieldName) {
235: int row = m_transientTableModel.indexOf(fieldName);
236:
237: if (row >= 0) {
238: ensureTransientFields().removeFieldName(row);
239: m_transientTableModel.removeRow(row);
240:
241: if (row > 0) {
242: row = Math.min(m_transientTableModel.getRowCount() - 1,
243: row);
244: m_transientTable.setRowSelectionInterval(row, row);
245: }
246: }
247: }
248:
249: public boolean isTransient(String fieldName) {
250: TransientFields transients = ensureTransientFields();
251: int count = transients.sizeOfFieldNameArray();
252:
253: for (int i = 0; i < count; i++) {
254: if (fieldName.equals(transients.getFieldNameArray(i))) {
255: return true;
256: }
257: }
258:
259: return false;
260: }
261:
262: public void ensureTransient(String fieldName) {
263: if (!isTransient(fieldName)) {
264: internalAddTransient(fieldName);
265: }
266: }
267:
268: public void ensureNotTransient(String fieldName) {
269: if (isTransient(fieldName)) {
270: internalRemoveTransient(fieldName);
271: }
272: }
273: }
|