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.AdditionalBootJarClasses;
012: import com.terracottatech.config.DsoApplication;
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 BootClassesPanel extends XContainer implements
025: TableModelListener {
026: private DsoApplication m_dsoApp;
027: private AdditionalBootJarClasses m_bootClasses;
028: private XTable m_bootClassesTable;
029: private BootClassTableModel m_bootClassesTableModel;
030: private Button m_addButton;
031: private ActionListener m_addListener;
032: private Button m_removeButton;
033: private ActionListener m_removeListener;
034: private ListSelectionListener m_bootClassesListener;
035:
036: public BootClassesPanel() {
037: super ();
038: }
039:
040: public void load(ContainerResource containerRes) {
041: super .load(containerRes);
042:
043: m_bootClassesTable = (XTable) findComponent("BootClassesTable");
044: m_bootClassesTable
045: .setModel(m_bootClassesTableModel = new BootClassTableModel());
046: m_bootClassesListener = new ListSelectionListener() {
047: public void valueChanged(ListSelectionEvent lse) {
048: if (!lse.getValueIsAdjusting()) {
049: int[] sel = m_bootClassesTable.getSelectedRows();
050: m_removeButton.setEnabled(sel != null
051: && sel.length > 0);
052: }
053: }
054: };
055:
056: m_addButton = (Button) findComponent("AddBootClassButton");
057: m_addListener = new ActionListener() {
058: public void actionPerformed(ActionEvent ae) {
059: String bootType = JOptionPane
060: .showInputDialog("Boot class");
061:
062: if (bootType != null) {
063: bootType = bootType.trim();
064:
065: if (bootType != null && bootType.length() > 0) {
066: internalAddBootClass(bootType);
067: }
068: }
069: }
070: };
071:
072: m_removeButton = (Button) findComponent("RemoveBootClassButton");
073: m_removeListener = new ActionListener() {
074: public void actionPerformed(ActionEvent ae) {
075: AdditionalBootJarClasses abjc = ensureAdditionalBootClasses();
076: int[] selection = m_bootClassesTable.getSelectedRows();
077:
078: for (int i = selection.length - 1; i >= 0; i--) {
079: abjc.removeInclude(selection[i]);
080: }
081: m_bootClassesTableModel.removeRows(selection);
082: }
083: };
084: }
085:
086: public boolean hasAnySet() {
087: return m_bootClasses != null
088: && m_bootClasses.sizeOfIncludeArray() > 0;
089: }
090:
091: private AdditionalBootJarClasses ensureAdditionalBootClasses() {
092: if (m_bootClasses == null) {
093: ensureXmlObject();
094: }
095: return m_bootClasses;
096: }
097:
098: // make sure parent exists
099: public void ensureXmlObject() {
100: if (m_bootClasses == null) {
101: removeListeners();
102: m_bootClasses = m_dsoApp.addNewAdditionalBootJarClasses();
103: updateChildren();
104: addListeners();
105: }
106: }
107:
108: // write xml and remove if not needed
109: private void syncModel() {
110: if (!hasAnySet()
111: && m_dsoApp.getAdditionalBootJarClasses() != null) {
112: m_dsoApp.unsetAdditionalBootJarClasses();
113: m_bootClasses = null;
114: }
115:
116: SessionIntegratorFrame frame = (SessionIntegratorFrame) getAncestorOfClass(SessionIntegratorFrame.class);
117: frame.modelChanged();
118: }
119:
120: private void addListeners() {
121: m_bootClassesTableModel.addTableModelListener(this );
122: m_bootClassesTable.getSelectionModel()
123: .addListSelectionListener(m_bootClassesListener);
124: m_addButton.addActionListener(m_addListener);
125: m_removeButton.addActionListener(m_removeListener);
126: }
127:
128: private void removeListeners() {
129: m_bootClassesTableModel.removeTableModelListener(this );
130: m_bootClassesTable.getSelectionModel()
131: .removeListSelectionListener(m_bootClassesListener);
132: m_addButton.removeActionListener(m_addListener);
133: m_removeButton.removeActionListener(m_removeListener);
134: }
135:
136: // match table to xmlbeans
137: private void updateChildren() {
138: m_bootClassesTableModel.clear();
139:
140: if (m_bootClasses != null) {
141: String[] bootClasses = m_bootClasses.getIncludeArray();
142:
143: for (int i = 0; i < bootClasses.length; i++) {
144: m_bootClassesTableModel.addBootClass(bootClasses[i]);
145: }
146: }
147: }
148:
149: public void setup(DsoApplication dsoApp) {
150: setEnabled(true);
151: removeListeners();
152:
153: m_dsoApp = dsoApp;
154: m_bootClasses = m_dsoApp != null ? m_dsoApp
155: .getAdditionalBootJarClasses() : null;
156:
157: updateChildren();
158: addListeners();
159: }
160:
161: public void tearDown() {
162: removeListeners();
163:
164: m_dsoApp = null;
165: m_bootClasses = null;
166:
167: m_bootClassesTableModel.clear();
168:
169: setEnabled(false);
170: }
171:
172: class BootClassTableModel extends DefaultTableModel {
173: BootClassTableModel() {
174: super ();
175: setColumnIdentifiers(new String[] { "Boot Classes" });
176: }
177:
178: void clear() {
179: setRowCount(0);
180: }
181:
182: void setBootClasses(AdditionalBootJarClasses bootClasses) {
183: clear();
184:
185: if (bootClasses != null) {
186: int count = bootClasses.sizeOfIncludeArray();
187:
188: for (int i = 0; i < count; i++) {
189: addBootClass(bootClasses.getIncludeArray(i));
190: }
191: }
192: }
193:
194: void addBootClass(String typeName) {
195: addRow(new Object[] { typeName });
196: }
197:
198: int indexOf(String typeName) {
199: int count = getRowCount();
200:
201: for (int i = 0; i < count; i++) {
202: if (((String) getValueAt(i, 0)).equals(typeName)) {
203: return i;
204: }
205: }
206:
207: return -1;
208: }
209:
210: public void setValueAt(Object value, int row, int col) {
211: m_bootClasses.setIncludeArray(row, (String) value);
212: super .setValueAt(value, row, col);
213: }
214:
215: void removeRows(int[] rows) {
216: removeTableModelListener(BootClassesPanel.this );
217: for (int i = rows.length - 1; i >= 0; i--) {
218: removeRow(rows[i]);
219: }
220: addTableModelListener(BootClassesPanel.this );
221: syncModel();
222: }
223: }
224:
225: public void tableChanged(TableModelEvent tme) {
226: syncModel();
227: }
228:
229: private void internalAddBootClass(String typeName) {
230: ensureAdditionalBootClasses().addInclude(typeName);
231: m_bootClassesTableModel.addBootClass(typeName);
232:
233: int row = m_bootClassesTableModel.getRowCount() - 1;
234: m_bootClassesTable.setRowSelectionInterval(row, row);
235: }
236:
237: private void internalRemoveBootClass(String typeName) {
238: int row = m_bootClassesTableModel.indexOf(typeName);
239:
240: if (row >= 0) {
241: ensureAdditionalBootClasses().removeInclude(row);
242: m_bootClassesTableModel.removeRow(row);
243:
244: if (row > 0) {
245: row = Math.min(
246: m_bootClassesTableModel.getRowCount() - 1, row);
247: m_bootClassesTable.setRowSelectionInterval(row, row);
248: }
249: }
250: }
251:
252: public boolean isBootClass(String typeName) {
253: AdditionalBootJarClasses bootClasses = ensureAdditionalBootClasses();
254: int count = bootClasses.sizeOfIncludeArray();
255:
256: for (int i = 0; i < count; i++) {
257: if (typeName.equals(bootClasses.getIncludeArray(i))) {
258: return true;
259: }
260: }
261:
262: return false;
263: }
264:
265: public void ensureBootClass(String typeName) {
266: if (!isBootClass(typeName)) {
267: internalAddBootClass(typeName);
268: }
269: }
270:
271: public void ensureNotBootClass(String fieldName) {
272: if (isBootClass(fieldName)) {
273: internalRemoveBootClass(fieldName);
274: }
275: }
276: }
|