KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > ModulesPanel


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

5 package com.tc;
6
7 import org.apache.xmlbeans.XmlAnyURI;
8 import org.dijon.Button;
9 import org.dijon.ContainerResource;
10
11 import com.tc.admin.common.XContainer;
12 import com.tc.admin.common.XObjectTableModel;
13 import com.tc.admin.common.XTable;
14 import com.terracottatech.config.Client;
15 import com.terracottatech.config.Module;
16 import com.terracottatech.config.Modules;
17
18 import java.awt.event.ActionEvent JavaDoc;
19 import java.awt.event.ActionListener JavaDoc;
20
21 import javax.swing.JOptionPane JavaDoc;
22 import javax.swing.event.ListSelectionEvent JavaDoc;
23 import javax.swing.event.ListSelectionListener JavaDoc;
24 import javax.swing.event.TableModelEvent JavaDoc;
25 import javax.swing.event.TableModelListener JavaDoc;
26
27 // TODO: validate that the repo and modules actually exist (this may require a background thread due to request timeouts
28
public class ModulesPanel extends XContainer implements TableModelListener JavaDoc {
29
30   private Modules m_modules;
31   private Client m_dsoClient;
32   private XTable m_repositoriesTable;
33   private XTable m_modulesTable;
34   private AddModuleDialog m_addModulesDialog;
35   private RepositoriesTableModel m_repositoriesTableModel;
36   private ModulesTableModel m_modulesTableModel;
37   private Button m_repoAddButton;
38   private Button m_repoRemoveButton;
39   private Button m_moduleAddButton;
40   private Button m_moduleRemoveButton;
41   private ActionListener JavaDoc m_repoAddListener;
42   private ActionListener JavaDoc m_repoRemoveListener;
43   private ActionListener JavaDoc m_moduleAddListener;
44   private ActionListener JavaDoc m_moduleRemoveListener;
45   private ListSelectionListener JavaDoc m_repoSelectionListener;
46   private ListSelectionListener JavaDoc m_moduleSelectionListener;
47
48   public ModulesPanel() {
49     super();
50   }
51
52   public void load(ContainerResource containerRes) {
53     super.load(containerRes);
54
55     m_repositoriesTable = (XTable) findComponent("RepositoriesTable");
56     m_repositoriesTable.setModel(m_repositoriesTableModel = new RepositoriesTableModel());
57
58     m_modulesTable = (XTable) findComponent("ModulesTable");
59     m_modulesTable.setModel(m_modulesTableModel = new ModulesTableModel());
60     m_addModulesDialog = new AddModuleDialog();
61     m_moduleAddListener = new ActionListener JavaDoc() {
62       public void actionPerformed(ActionEvent JavaDoc ae) {
63         String JavaDoc[] module = m_addModulesDialog.prompt();
64         if (module != null) {
65           internalAddModule(module[0], module[1]);
66         }
67       }
68     };
69
70     m_moduleRemoveButton = (Button) findComponent("RemoveModuleButton");
71     m_moduleRemoveListener = new ActionListener JavaDoc() {
72       public void actionPerformed(ActionEvent JavaDoc ae) {
73         Modules modules = ensureModulesElement();
74         int[] selection = m_modulesTable.getSelectedRows();
75         for (int i = selection.length - 1; i >= 0; i--) {
76           modules.removeModule(selection[i]);
77         }
78         m_modulesTableModel.removeRows(selection);
79       }
80     };
81
82     m_repoAddButton = (Button) findComponent("AddRepositoryButton");
83     m_repoAddListener = new ActionListener JavaDoc() {
84       public void actionPerformed(ActionEvent JavaDoc ae) {
85         String JavaDoc repoLocation = JOptionPane.showInputDialog("Repository Location (URL)");
86         if (repoLocation != null && !(repoLocation = repoLocation.trim()).equals("")) {
87           internalAddRepositoryLocation(repoLocation);
88         }
89       }
90     };
91
92     m_repoRemoveButton = (Button) findComponent("RemoveRepositoryButton");
93     m_repoRemoveListener = new ActionListener JavaDoc() {
94       public void actionPerformed(ActionEvent JavaDoc ae) {
95         Modules modules = ensureModulesElement();
96         int[] selection = m_repositoriesTable.getSelectedRows();
97         for (int i = selection.length - 1; i >= 0; i--) {
98           modules.removeRepository(selection[i]);
99         }
100         m_repositoriesTableModel.removeRows(selection);
101       }
102     };
103
104     m_repoSelectionListener = new ListSelectionListener JavaDoc() {
105       public void valueChanged(ListSelectionEvent JavaDoc lse) {
106         if (!lse.getValueIsAdjusting()) {
107           int[] sel = m_repositoriesTable.getSelectedRows();
108           m_repoRemoveButton.setEnabled(sel != null && sel.length > 0);
109         }
110       }
111     };
112     m_moduleSelectionListener = new ListSelectionListener JavaDoc() {
113       public void valueChanged(ListSelectionEvent JavaDoc lse) {
114         if (!lse.getValueIsAdjusting()) {
115           int[] sel = m_modulesTable.getSelectedRows();
116           m_moduleRemoveButton.setEnabled(sel != null && sel.length > 0);
117         }
118       }
119     };
120
121     m_moduleAddButton = (Button) findComponent("AddModuleButton");
122   }
123
124   private Modules ensureModulesElement() {
125     if (m_modules == null) {
126       ensureXmlObject();
127     }
128     return m_modules;
129   }
130
131   // make sure parent exists
132
public void ensureXmlObject() {
133     if (m_modules == null) {
134       removeListeners();
135       m_modules = m_dsoClient.addNewModules();
136       updateChildren();
137       addListeners();
138     }
139   }
140
141   // match table to xmlbeans
142
private void updateChildren() {
143     m_modulesTableModel.clear();
144     m_repositoriesTableModel.clear();
145     if (m_modules != null) {
146       m_repositoriesTableModel.set(m_modules.xgetRepositoryArray());
147       m_modulesTableModel.set(m_modules.getModuleArray());
148     }
149   }
150
151   public void tableChanged(TableModelEvent JavaDoc e) {
152     syncModel();
153   }
154
155   public void setup(Client dsoClient) {
156     setEnabled(true);
157     removeListeners();
158     m_dsoClient = dsoClient;
159     m_modules = (m_dsoClient != null) ? m_dsoClient.getModules() : null;
160     updateChildren();
161     addListeners();
162   }
163
164   public void tearDown() {
165     removeListeners();
166     m_dsoClient = null;
167     m_repositoriesTableModel.clear();
168     setEnabled(false);
169   }
170
171   private void removeListeners() {
172     m_modulesTableModel.removeTableModelListener(this);
173     m_repositoriesTableModel.removeTableModelListener(this);
174     m_repositoriesTable.getSelectionModel().removeListSelectionListener(m_repoSelectionListener);
175     m_repoAddButton.removeActionListener(m_repoAddListener);
176     m_repoRemoveButton.removeActionListener(m_repoRemoveListener);
177     m_modulesTable.getSelectionModel().removeListSelectionListener(m_moduleSelectionListener);
178     m_moduleAddButton.removeActionListener(m_moduleAddListener);
179     m_moduleRemoveButton.removeActionListener(m_moduleRemoveListener);
180   }
181
182   private void addListeners() {
183     m_modulesTableModel.addTableModelListener(this);
184     m_repositoriesTableModel.addTableModelListener(this);
185     m_repositoriesTable.getSelectionModel().addListSelectionListener(m_repoSelectionListener);
186     m_repoAddButton.addActionListener(m_repoAddListener);
187     m_repoRemoveButton.addActionListener(m_repoRemoveListener);
188     m_modulesTable.getSelectionModel().addListSelectionListener(m_moduleSelectionListener);
189     m_moduleAddButton.addActionListener(m_moduleAddListener);
190     m_moduleRemoveButton.addActionListener(m_moduleRemoveListener);
191   }
192
193   private void internalAddRepositoryLocation(String JavaDoc repoLocation) {
194     XmlAnyURI elem = ensureModulesElement().addNewRepository();
195     elem.setStringValue(repoLocation);
196     m_repositoriesTableModel.addRepoLocation(elem);
197     int row = m_repositoriesTableModel.getRowCount() - 1;
198     m_repositoriesTable.setRowSelectionInterval(row, row);
199   }
200
201   private void internalAddModule(String JavaDoc name, String JavaDoc version) {
202     Module module = ensureModulesElement().addNewModule();
203     module.setName(name);
204     module.setVersion(version);
205     m_modulesTableModel.addModule(module);
206     int row = m_modulesTableModel.getRowCount() - 1;
207     m_modulesTable.setRowSelectionInterval(row, row);
208   }
209
210   private void syncModel() {
211     if (!hasAnySet() && m_dsoClient.getModules() != null) {
212       m_dsoClient.unsetModules();
213       m_modules = null;
214     }
215     SessionIntegratorFrame frame = (SessionIntegratorFrame) getAncestorOfClass(SessionIntegratorFrame.class);
216     frame.modelChanged();
217   }
218
219   public boolean hasAnySet() {
220     return m_modules != null && (m_modules.sizeOfRepositoryArray() > 0 || m_modules.sizeOfModuleArray() > 0);
221   }
222
223   // --------------------------------------------------------------------------------
224

225   class RepositoriesTableModel extends XObjectTableModel {
226     RepositoriesTableModel() {
227       super(XmlAnyURI.class,
228             new String JavaDoc[] {"StringValue"},
229             new String JavaDoc[] {"Location"});
230     }
231
232     void addRepoLocation(XmlAnyURI repoLocation) {
233       add(repoLocation);
234       int row = getRowCount();
235       fireTableRowsInserted(row, row);
236     }
237
238     public void setValueAt(Object JavaDoc value, int row, int col) {
239       m_modules.setRepositoryArray(row, (String JavaDoc) value);
240       super.setValueAt(value, row, col);
241     }
242
243     void removeRows(int[] rows) {
244       removeTableModelListener(ModulesPanel.this);
245       for (int i = rows.length - 1; i >= 0; i--) {
246         remove(rows[i]);
247       }
248       addTableModelListener(ModulesPanel.this);
249       syncModel();
250     }
251   }
252
253   // --------------------------------------------------------------------------------
254

255   private static final String JavaDoc[] MODULE_FIELDS = {"Name", "Version"};
256   
257   class ModulesTableModel extends XObjectTableModel {
258     ModulesTableModel() {
259       super(Module.class, MODULE_FIELDS, MODULE_FIELDS);
260     }
261
262     void addModule(Module module) {
263       add(module);
264       int row = getRowCount();
265       fireTableRowsInserted(row, row);
266     }
267
268     void removeRows(int[] rows) {
269       removeTableModelListener(ModulesPanel.this);
270       for (int i = rows.length - 1; i >= 0; i--) {
271         remove(rows[i]);
272       }
273       addTableModelListener(ModulesPanel.this);
274       syncModel();
275     }
276   }
277 }
278
Popular Tags