001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc;
006:
007: import org.apache.xmlbeans.XmlAnyURI;
008: import org.dijon.Button;
009: import org.dijon.ContainerResource;
010:
011: import com.tc.admin.common.XContainer;
012: import com.tc.admin.common.XObjectTableModel;
013: import com.tc.admin.common.XTable;
014: import com.terracottatech.config.Client;
015: import com.terracottatech.config.Module;
016: import com.terracottatech.config.Modules;
017:
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020:
021: import javax.swing.JOptionPane;
022: import javax.swing.event.ListSelectionEvent;
023: import javax.swing.event.ListSelectionListener;
024: import javax.swing.event.TableModelEvent;
025: import javax.swing.event.TableModelListener;
026:
027: // TODO: validate that the repo and modules actually exist (this may require a background thread due to request timeouts
028: public class ModulesPanel extends XContainer implements
029: TableModelListener {
030:
031: private Modules m_modules;
032: private Client m_dsoClient;
033: private XTable m_repositoriesTable;
034: private XTable m_modulesTable;
035: private AddModuleDialog m_addModulesDialog;
036: private RepositoriesTableModel m_repositoriesTableModel;
037: private ModulesTableModel m_modulesTableModel;
038: private Button m_repoAddButton;
039: private Button m_repoRemoveButton;
040: private Button m_moduleAddButton;
041: private Button m_moduleRemoveButton;
042: private ActionListener m_repoAddListener;
043: private ActionListener m_repoRemoveListener;
044: private ActionListener m_moduleAddListener;
045: private ActionListener m_moduleRemoveListener;
046: private ListSelectionListener m_repoSelectionListener;
047: private ListSelectionListener m_moduleSelectionListener;
048:
049: public ModulesPanel() {
050: super ();
051: }
052:
053: public void load(ContainerResource containerRes) {
054: super .load(containerRes);
055:
056: m_repositoriesTable = (XTable) findComponent("RepositoriesTable");
057: m_repositoriesTable
058: .setModel(m_repositoriesTableModel = new RepositoriesTableModel());
059:
060: m_modulesTable = (XTable) findComponent("ModulesTable");
061: m_modulesTable
062: .setModel(m_modulesTableModel = new ModulesTableModel());
063: m_addModulesDialog = new AddModuleDialog();
064: m_moduleAddListener = new ActionListener() {
065: public void actionPerformed(ActionEvent ae) {
066: String[] module = m_addModulesDialog.prompt();
067: if (module != null) {
068: internalAddModule(module[0], module[1]);
069: }
070: }
071: };
072:
073: m_moduleRemoveButton = (Button) findComponent("RemoveModuleButton");
074: m_moduleRemoveListener = new ActionListener() {
075: public void actionPerformed(ActionEvent ae) {
076: Modules modules = ensureModulesElement();
077: int[] selection = m_modulesTable.getSelectedRows();
078: for (int i = selection.length - 1; i >= 0; i--) {
079: modules.removeModule(selection[i]);
080: }
081: m_modulesTableModel.removeRows(selection);
082: }
083: };
084:
085: m_repoAddButton = (Button) findComponent("AddRepositoryButton");
086: m_repoAddListener = new ActionListener() {
087: public void actionPerformed(ActionEvent ae) {
088: String repoLocation = JOptionPane
089: .showInputDialog("Repository Location (URL)");
090: if (repoLocation != null
091: && !(repoLocation = repoLocation.trim())
092: .equals("")) {
093: internalAddRepositoryLocation(repoLocation);
094: }
095: }
096: };
097:
098: m_repoRemoveButton = (Button) findComponent("RemoveRepositoryButton");
099: m_repoRemoveListener = new ActionListener() {
100: public void actionPerformed(ActionEvent ae) {
101: Modules modules = ensureModulesElement();
102: int[] selection = m_repositoriesTable.getSelectedRows();
103: for (int i = selection.length - 1; i >= 0; i--) {
104: modules.removeRepository(selection[i]);
105: }
106: m_repositoriesTableModel.removeRows(selection);
107: }
108: };
109:
110: m_repoSelectionListener = new ListSelectionListener() {
111: public void valueChanged(ListSelectionEvent lse) {
112: if (!lse.getValueIsAdjusting()) {
113: int[] sel = m_repositoriesTable.getSelectedRows();
114: m_repoRemoveButton.setEnabled(sel != null
115: && sel.length > 0);
116: }
117: }
118: };
119: m_moduleSelectionListener = new ListSelectionListener() {
120: public void valueChanged(ListSelectionEvent lse) {
121: if (!lse.getValueIsAdjusting()) {
122: int[] sel = m_modulesTable.getSelectedRows();
123: m_moduleRemoveButton.setEnabled(sel != null
124: && sel.length > 0);
125: }
126: }
127: };
128:
129: m_moduleAddButton = (Button) findComponent("AddModuleButton");
130: }
131:
132: private Modules ensureModulesElement() {
133: if (m_modules == null) {
134: ensureXmlObject();
135: }
136: return m_modules;
137: }
138:
139: // make sure parent exists
140: public void ensureXmlObject() {
141: if (m_modules == null) {
142: removeListeners();
143: m_modules = m_dsoClient.addNewModules();
144: updateChildren();
145: addListeners();
146: }
147: }
148:
149: // match table to xmlbeans
150: private void updateChildren() {
151: m_modulesTableModel.clear();
152: m_repositoriesTableModel.clear();
153: if (m_modules != null) {
154: //m_repositoriesTableModel.set(m_modules.xgetRepositoryArray());
155: m_repositoriesTableModel
156: .set(m_modules.getRepositoryArray());
157: m_modulesTableModel.set(m_modules.getModuleArray());
158: }
159: }
160:
161: public void tableChanged(TableModelEvent e) {
162: syncModel();
163: }
164:
165: public void setup(Client dsoClient) {
166: setEnabled(true);
167: removeListeners();
168: m_dsoClient = dsoClient;
169: m_modules = (m_dsoClient != null) ? m_dsoClient.getModules()
170: : null;
171: updateChildren();
172: addListeners();
173: }
174:
175: public void tearDown() {
176: removeListeners();
177: m_dsoClient = null;
178: m_repositoriesTableModel.clear();
179: setEnabled(false);
180: }
181:
182: private void removeListeners() {
183: m_modulesTableModel.removeTableModelListener(this );
184: m_repositoriesTableModel.removeTableModelListener(this );
185: m_repositoriesTable.getSelectionModel()
186: .removeListSelectionListener(m_repoSelectionListener);
187: m_repoAddButton.removeActionListener(m_repoAddListener);
188: m_repoRemoveButton.removeActionListener(m_repoRemoveListener);
189: m_modulesTable.getSelectionModel().removeListSelectionListener(
190: m_moduleSelectionListener);
191: m_moduleAddButton.removeActionListener(m_moduleAddListener);
192: m_moduleRemoveButton
193: .removeActionListener(m_moduleRemoveListener);
194: }
195:
196: private void addListeners() {
197: m_modulesTableModel.addTableModelListener(this );
198: m_repositoriesTableModel.addTableModelListener(this );
199: m_repositoriesTable.getSelectionModel()
200: .addListSelectionListener(m_repoSelectionListener);
201: m_repoAddButton.addActionListener(m_repoAddListener);
202: m_repoRemoveButton.addActionListener(m_repoRemoveListener);
203: m_modulesTable.getSelectionModel().addListSelectionListener(
204: m_moduleSelectionListener);
205: m_moduleAddButton.addActionListener(m_moduleAddListener);
206: m_moduleRemoveButton.addActionListener(m_moduleRemoveListener);
207: }
208:
209: private void internalAddRepositoryLocation(String repoLocation) {
210: XmlAnyURI elem = ensureModulesElement().addNewRepository();
211: elem.setStringValue(repoLocation);
212: m_repositoriesTableModel.addRepoLocation(elem);
213: int row = m_repositoriesTableModel.getRowCount() - 1;
214: m_repositoriesTable.setRowSelectionInterval(row, row);
215: }
216:
217: private void internalAddModule(String name, String version) {
218: Module module = ensureModulesElement().addNewModule();
219: module.setName(name);
220: module.setVersion(version);
221: m_modulesTableModel.addModule(module);
222: int row = m_modulesTableModel.getRowCount() - 1;
223: m_modulesTable.setRowSelectionInterval(row, row);
224: }
225:
226: private void syncModel() {
227: if (!hasAnySet() && m_dsoClient.getModules() != null) {
228: m_dsoClient.unsetModules();
229: m_modules = null;
230: }
231: SessionIntegratorFrame frame = (SessionIntegratorFrame) getAncestorOfClass(SessionIntegratorFrame.class);
232: frame.modelChanged();
233: }
234:
235: public boolean hasAnySet() {
236: return m_modules != null
237: && (m_modules.sizeOfRepositoryArray() > 0 || m_modules
238: .sizeOfModuleArray() > 0);
239: }
240:
241: // --------------------------------------------------------------------------------
242:
243: class RepositoriesTableModel extends XObjectTableModel {
244: RepositoriesTableModel() {
245: super (XmlAnyURI.class, new String[] { "StringValue" },
246: new String[] { "Location" });
247: }
248:
249: void addRepoLocation(XmlAnyURI repoLocation) {
250: add(repoLocation);
251: int row = getRowCount();
252: fireTableRowsInserted(row, row);
253: }
254:
255: public void setValueAt(Object value, int row, int col) {
256: m_modules.setRepositoryArray(row, (String) value);
257: super .setValueAt(value, row, col);
258: }
259:
260: void removeRows(int[] rows) {
261: removeTableModelListener(ModulesPanel.this );
262: for (int i = rows.length - 1; i >= 0; i--) {
263: remove(rows[i]);
264: }
265: addTableModelListener(ModulesPanel.this );
266: syncModel();
267: }
268: }
269:
270: // --------------------------------------------------------------------------------
271:
272: private static final String[] MODULE_FIELDS = { "Name", "Version" };
273:
274: class ModulesTableModel extends XObjectTableModel {
275: ModulesTableModel() {
276: super (Module.class, MODULE_FIELDS, MODULE_FIELDS);
277: }
278:
279: void addModule(Module module) {
280: add(module);
281: int row = getRowCount();
282: fireTableRowsInserted(row, row);
283: }
284:
285: void removeRows(int[] rows) {
286: removeTableModelListener(ModulesPanel.this );
287: for (int i = rows.length - 1; i >= 0; i--) {
288: remove(rows[i]);
289: }
290: addTableModelListener(ModulesPanel.this);
291: syncModel();
292: }
293: }
294: }
|