1 5 package org.terracotta.dso.editors; 6 7 import org.dijon.Button; 8 import org.dijon.ContainerResource; 9 import org.eclipse.swt.widgets.Shell; 10 import org.terracotta.dso.TcPlugin; 11 12 import com.tc.admin.common.XContainer; 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 ; 19 import java.awt.event.ActionListener ; 20 21 import javax.swing.event.ListSelectionEvent ; 22 import javax.swing.event.ListSelectionListener ; 23 import javax.swing.event.TableModelEvent ; 24 import javax.swing.event.TableModelListener ; 25 import javax.swing.table.DefaultTableModel ; 26 27 public class ModulesPanel extends XContainer implements TableModelListener { 29 30 private Modules m_modules; 31 private Client m_dsoClient; 32 private XTable m_repositoriesTable; 33 private XTable m_modulesTable; 34 private RepositoriesTableModel m_repositoriesTableModel; 35 private ModulesTableModel m_modulesTableModel; 36 private Button m_repoAddButton; 37 private Button m_repoRemoveButton; 38 private Button m_moduleAddButton; 39 private Button m_moduleRemoveButton; 40 private ActionListener m_repoAddListener; 41 private ActionListener m_repoRemoveListener; 42 private ActionListener m_moduleAddListener; 43 private ActionListener m_moduleRemoveListener; 44 private ListSelectionListener m_repoSelectionListener; 45 private ListSelectionListener m_moduleSelectionListener; 46 private ModulesEventListener m_changeListener; 47 private ModulesEventListener m_setDirtyListener; 48 49 public ModulesPanel() { 50 super(); 51 } 52 53 public void load(ContainerResource containerRes) { 54 super.load(containerRes); 55 56 m_repositoriesTable = (XTable) findComponent("RepositoriesTable"); 57 m_repositoriesTable.setModel(m_repositoriesTableModel = new RepositoriesTableModel()); 58 59 m_modulesTable = (XTable) findComponent("ModulesTable"); 60 m_modulesTable.setModel(m_modulesTableModel = new ModulesTableModel()); 61 62 m_moduleAddListener = new ActionListener () { 63 public void actionPerformed(ActionEvent ae) { 64 TcPlugin.getStandardDisplay().asyncExec(new Runnable () { 65 public void run() { 66 Shell shell = TcPlugin.getStandardDisplay().getActiveShell(); 67 AddModuleDialog dialog = new AddModuleDialog(shell, "Module Declaration", "Module Declaration"); 68 dialog.addValueListener(new AddModuleDialog.ValueListener() { 69 public void setValues(String name, String version) { 70 if (!name.trim().equals("") || !version.trim().equals("")) { 71 internalAddModule(name, version); 72 } 73 } 74 }); 75 dialog.open(); 76 } 77 }); 78 } 79 }; 80 81 m_moduleRemoveButton = (Button) findComponent("RemoveModuleButton"); 82 m_moduleRemoveListener = new ActionListener () { 83 public void actionPerformed(ActionEvent ae) { 84 Modules modules = ensureModulesElement(); 85 int[] selection = m_modulesTable.getSelectedRows(); 86 for (int i = selection.length - 1; i >= 0; i--) { 87 modules.removeModule(selection[i]); 88 } 89 m_modulesTableModel.removeRows(selection); 90 } 91 }; 92 93 m_repoAddButton = (Button) findComponent("AddRepositoryButton"); 94 m_repoAddListener = new ActionListener () { 95 public void actionPerformed(ActionEvent ae) { 96 TcPlugin.getStandardDisplay().asyncExec(new Runnable () { 97 public void run() { 98 Shell shell = TcPlugin.getStandardDisplay().getActiveShell(); 99 RepoLocationDialog dialog = new RepoLocationDialog(shell, "Repository Declaration", 100 "Repository Location (URL)"); 101 dialog.addValueListener(new RepoLocationDialog.ValueListener() { 102 public void setValues(String repoLocation) { 103 if (repoLocation != null && !(repoLocation = repoLocation.trim()).equals("")) { 104 internalAddRepositoryLocation(repoLocation); 105 } 106 } 107 }); 108 dialog.open(); 109 } 110 }); 111 } 112 }; 113 114 m_repoRemoveButton = (Button) findComponent("RemoveRepositoryButton"); 115 m_repoRemoveListener = new ActionListener () { 116 public void actionPerformed(ActionEvent ae) { 117 Modules modules = ensureModulesElement(); 118 int[] selection = m_repositoriesTable.getSelectedRows(); 119 for (int i = selection.length - 1; i >= 0; i--) { 120 modules.removeRepository(selection[i]); 121 } 122 m_repositoriesTableModel.removeRows(selection); 123 } 124 }; 125 126 m_repoSelectionListener = new ListSelectionListener () { 127 public void valueChanged(ListSelectionEvent lse) { 128 if (!lse.getValueIsAdjusting()) { 129 int[] sel = m_repositoriesTable.getSelectedRows(); 130 m_repoRemoveButton.setEnabled(sel != null && sel.length > 0); 131 } 132 } 133 }; 134 m_moduleSelectionListener = new ListSelectionListener () { 135 public void valueChanged(ListSelectionEvent lse) { 136 if (!lse.getValueIsAdjusting()) { 137 int[] sel = m_modulesTable.getSelectedRows(); 138 m_moduleRemoveButton.setEnabled(sel != null && sel.length > 0); 139 } 140 } 141 }; 142 143 m_moduleAddButton = (Button) findComponent("AddModuleButton"); 144 } 145 146 private Modules ensureModulesElement() { 147 if (m_modules == null) { 148 ensureXmlObject(); 149 } 150 return m_modules; 151 } 152 153 public void ensureXmlObject() { 155 if (m_modules == null) { 156 removeListeners(); 157 m_modules = m_dsoClient.addNewModules(); 158 updateChildren(); 159 addListeners(); 160 } 161 } 162 163 private void updateChildren() { 165 m_modulesTableModel.clear(); 166 m_repositoriesTableModel.clear(); 167 168 if (m_modules != null) { 169 String [] repos = m_modules.getRepositoryArray(); 170 for (int i = 0; i < repos.length; i++) { 171 m_repositoriesTableModel.addRepoLocation(repos[i]); 172 } 173 Module[] modules = m_modules.getModuleArray(); 174 for (int i = 0; i < modules.length; i++) { 175 m_modulesTableModel.addModule(modules[i].getName(), modules[i].getVersion()); 176 } 177 } else { 178 m_modulesTableModel.fireTableDataChanged(); 179 m_repositoriesTableModel.fireTableDataChanged(); 180 } 181 } 182 183 public void tableChanged(TableModelEvent e) { 184 syncModel(); 185 } 186 187 public void setup(Client dsoClient) { 188 setEnabled(true); 189 removeListeners(); 190 m_dsoClient = dsoClient; 191 m_modules = (m_dsoClient != null) ? m_dsoClient.getModules() : null; 192 updateChildren(); 193 addListeners(); 194 } 195 196 public void tearDown() { 197 removeListeners(); 198 m_dsoClient = null; 199 m_repositoriesTableModel.clear(); 200 setEnabled(false); 201 } 202 203 private void removeListeners() { 204 m_modulesTableModel.removeTableModelListener(this); 205 m_repositoriesTableModel.removeTableModelListener(this); 206 m_repositoriesTable.getSelectionModel().removeListSelectionListener(m_repoSelectionListener); 207 m_repoAddButton.removeActionListener(m_repoAddListener); 208 m_repoRemoveButton.removeActionListener(m_repoRemoveListener); 209 m_modulesTable.getSelectionModel().removeListSelectionListener(m_moduleSelectionListener); 210 m_moduleAddButton.removeActionListener(m_moduleAddListener); 211 m_moduleRemoveButton.removeActionListener(m_moduleRemoveListener); 212 } 213 214 private void addListeners() { 215 m_modulesTableModel.addTableModelListener(this); 216 m_repositoriesTableModel.addTableModelListener(this); 217 m_repositoriesTable.getSelectionModel().addListSelectionListener(m_repoSelectionListener); 218 m_repoAddButton.addActionListener(m_repoAddListener); 219 m_repoRemoveButton.addActionListener(m_repoRemoveListener); 220 m_modulesTable.getSelectionModel().addListSelectionListener(m_moduleSelectionListener); 221 m_moduleAddButton.addActionListener(m_moduleAddListener); 222 m_moduleRemoveButton.addActionListener(m_moduleRemoveListener); 223 } 224 225 private void internalAddRepositoryLocation(String repoLocation) { 226 ensureModulesElement().addRepository(repoLocation); 227 m_repositoriesTableModel.addRepoLocation(repoLocation); 228 int row = m_repositoriesTableModel.getRowCount() - 1; 229 m_repositoriesTable.setRowSelectionInterval(row, row); 230 } 231 232 private void internalAddModule(String name, String version) { 233 Module module = ensureModulesElement().addNewModule(); 234 module.setName(name); 235 module.setVersion(version); 236 m_modulesTableModel.addModule(name, version); 237 int row = m_modulesTableModel.getRowCount() - 1; 238 m_modulesTable.setRowSelectionInterval(row, row); 239 } 240 241 private void syncModel() { 242 if (!hasAnySet() && m_dsoClient.getModules() != null) { 243 m_dsoClient.unsetModules(); 244 m_modules = null; 245 fireChangeEvent(); 246 updateChildren(); 247 } else { 248 fireSetDirtyEvent(); 249 } 250 } 251 252 synchronized void addChangeListener(ModulesEventListener listener) { 253 m_changeListener = listener; 254 } 255 256 synchronized void addSetDirtyListener(ModulesEventListener listener) { 257 m_setDirtyListener = listener; 258 } 259 260 private synchronized void fireChangeEvent() { 261 if (m_changeListener != null) m_changeListener.handleEvent(); 262 } 263 264 private synchronized void fireSetDirtyEvent() { 265 if (m_setDirtyListener != null) m_setDirtyListener.handleEvent(); 266 } 267 268 public boolean hasAnySet() { 269 return m_modules != null && (m_modules.sizeOfRepositoryArray() > 0 || m_modules.sizeOfModuleArray() > 0); 270 } 271 272 274 class RepositoriesTableModel extends DefaultTableModel { 275 RepositoriesTableModel() { 276 super(); 277 setColumnIdentifiers(new String [] { "Location" }); 278 } 279 280 void clear() { 281 setRowCount(0); 282 } 283 284 void addRepoLocation(String typeName) { 285 addRow(new Object [] { typeName }); 286 } 287 288 int indexOf(String typeName) { 289 int count = getRowCount(); 290 for (int i = 0; i < count; i++) { 291 if (((String ) getValueAt(i, 0)).equals(typeName)) { return i; } 292 } 293 return -1; 294 } 295 296 public void setValueAt(Object value, int row, int col) { 297 m_modules.addRepository((String ) value); 298 super.setValueAt(value, row, col); 299 } 300 301 void removeRows(int[] rows) { 302 removeTableModelListener(ModulesPanel.this); 303 for (int i = rows.length - 1; i >= 0; i--) { 304 removeRow(rows[i]); 305 } 306 addTableModelListener(ModulesPanel.this); 307 syncModel(); 308 } 309 } 310 311 313 class ModulesTableModel extends DefaultTableModel { 314 ModulesTableModel() { 315 super(); 316 setColumnIdentifiers(new String [] { "Name", "Version" }); 317 } 318 319 void clear() { 320 setRowCount(0); 321 } 322 323 void addModule(String name, String version) { 324 addRow(new Object [] { name, version }); 325 } 326 327 int indexOf(String typeName) { 328 int count = getRowCount(); 329 for (int i = 0; i < count; i++) { 330 if (((String ) getValueAt(i, 0)).equals(typeName)) { return i; } 331 } 332 return -1; 333 } 334 335 public void setValueAt(Object value, int row, int col) { 336 Module module = m_modules.addNewModule(); 337 if (col == 0) switch (col) { 338 case 0: 339 module.setName((String ) value); 340 break; 341 case 1: 342 module.setVersion((String ) value); 343 break; 344 345 default: 346 break; 347 } 348 super.setValueAt(value, row, col); 349 } 350 351 void removeRows(int[] rows) { 352 removeTableModelListener(ModulesPanel.this); 353 for (int i = rows.length - 1; i >= 0; i--) { 354 removeRow(rows[i]); 355 } 356 addTableModelListener(ModulesPanel.this); 357 syncModel(); 358 } 359 } 360 361 363 public static interface ModulesEventListener { 364 void handleEvent(); 365 } 366 } 367 | Popular Tags |