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 org.terracotta.dso.editors;
006:
007: import org.eclipse.jface.window.ApplicationWindow;
008: import org.eclipse.swt.SWT;
009: import org.eclipse.swt.events.MouseEvent;
010: import org.eclipse.swt.events.MouseMoveListener;
011: import org.eclipse.swt.events.SelectionAdapter;
012: import org.eclipse.swt.events.SelectionEvent;
013: import org.eclipse.swt.graphics.Point;
014: import org.eclipse.swt.layout.GridData;
015: import org.eclipse.swt.layout.GridLayout;
016: import org.eclipse.swt.widgets.Button;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Event;
019: import org.eclipse.swt.widgets.Label;
020: import org.eclipse.swt.widgets.Listener;
021: import org.eclipse.swt.widgets.Table;
022: import org.eclipse.swt.widgets.TableColumn;
023: import org.eclipse.swt.widgets.TableItem;
024: import org.eclipse.ui.IWorkbenchWindow;
025: import org.eclipse.ui.PlatformUI;
026: import org.terracotta.dso.ModuleInfo;
027: import org.terracotta.dso.ModulesConfiguration;
028: import org.terracotta.dso.TcPlugin;
029: import org.terracotta.dso.dialogs.NewAddModuleDialog;
030: import org.terracotta.dso.dialogs.RepoLocationDialog;
031: import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureChangeEvent;
032: import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureListener;
033: import org.terracotta.ui.util.SWTUtil;
034:
035: import com.terracottatech.config.Client;
036: import com.terracottatech.config.Module;
037: import com.terracottatech.config.Modules;
038:
039: import java.net.URL;
040:
041: public class ModulesPanel extends ConfigurationEditorPanel implements
042: XmlObjectStructureListener {
043: private Client m_dsoClient;
044: private Modules m_modules;
045:
046: private Layout m_layout;
047:
048: private TableSelectionListener m_tableSelectionListener;
049: private TableDataListener m_tableDataListener;
050: private MouseMoveListener m_moduleMouseMoveListener;
051: private AddModuleHandler m_addModuleHandler;
052: private RemoveModuleHandler m_removeModuleHandler;
053: private AddRepoHandler m_addRepoHandler;
054: private RemoveRepoHandler m_removeRepoHandler;
055:
056: private static final String MODULE_DECLARATION = "Module Declaration";
057: private static final String MODULE_REPO_LOCATION = "Repository Location (URL)";
058: private static final String REPO_DECLARATION = "Repository Declaration";
059:
060: private static final int MODULE_NAME_INDEX = 0;
061: private static final int MODULE_GROUP_ID_INDEX = 0;
062:
063: public ModulesPanel(Composite parent, int style) {
064: super (parent, style);
065: m_layout = new Layout(this );
066:
067: m_tableSelectionListener = new TableSelectionListener();
068: m_tableDataListener = new TableDataListener();
069: m_moduleMouseMoveListener = new ModuleMouseMoveListener();
070: m_addModuleHandler = new AddModuleHandler();
071: m_removeModuleHandler = new RemoveModuleHandler();
072: m_addRepoHandler = new AddRepoHandler();
073: m_removeRepoHandler = new RemoveRepoHandler();
074:
075: SWTUtil.setBGColorRecurse(this .getDisplay().getSystemColor(
076: SWT.COLOR_WHITE), this );
077: }
078:
079: private Modules ensureModules() {
080: if (m_modules == null) {
081: ensureXmlObject();
082: }
083: return m_modules;
084: }
085:
086: public void ensureXmlObject() {
087: super .ensureXmlObject();
088: if (m_modules == null) {
089: removeListeners();
090: m_modules = m_dsoClient.addNewModules();
091: updateChildren();
092: addListeners();
093: }
094: }
095:
096: private void updateChildren() {
097: initModuleRepositories();
098: initModules();
099: }
100:
101: public void setup(Client dsoClient) {
102: setEnabled(true);
103: removeListeners();
104: m_dsoClient = dsoClient;
105: m_modules = (m_dsoClient != null) ? m_dsoClient.getModules()
106: : null;
107: updateChildren();
108: addListeners();
109: }
110:
111: public void tearDown() {
112: removeListeners();
113: m_dsoClient = null;
114: setEnabled(false);
115: }
116:
117: private void addListeners() {
118: m_layout.m_moduleRepoTable
119: .addSelectionListener(m_tableSelectionListener);
120: m_layout.m_moduleRepoTable.addListener(SWT.SetData,
121: m_tableDataListener);
122: m_layout.m_moduleTable
123: .addSelectionListener(m_tableSelectionListener);
124: m_layout.m_moduleTable.addListener(SWT.SetData,
125: m_tableDataListener);
126: m_layout.m_moduleTable
127: .addMouseMoveListener(m_moduleMouseMoveListener);
128: m_layout.m_addModule.addSelectionListener(m_addModuleHandler);
129: m_layout.m_removeModule
130: .addSelectionListener(m_removeModuleHandler);
131: m_layout.m_addModuleRepo.addSelectionListener(m_addRepoHandler);
132: m_layout.m_removeModuleRepo
133: .addSelectionListener(m_removeRepoHandler);
134: }
135:
136: private void removeListeners() {
137: m_layout.m_moduleRepoTable
138: .removeSelectionListener(m_tableSelectionListener);
139: m_layout.m_moduleRepoTable.removeListener(SWT.SetData,
140: m_tableDataListener);
141: m_layout.m_moduleTable
142: .removeSelectionListener(m_tableSelectionListener);
143: m_layout.m_moduleTable.removeListener(SWT.SetData,
144: m_tableDataListener);
145: m_layout.m_moduleTable
146: .removeMouseMoveListener(m_moduleMouseMoveListener);
147: m_layout.m_addModule
148: .removeSelectionListener(m_addModuleHandler);
149: m_layout.m_removeModule
150: .removeSelectionListener(m_removeModuleHandler);
151: m_layout.m_addModuleRepo
152: .removeSelectionListener(m_addRepoHandler);
153: m_layout.m_removeModuleRepo
154: .removeSelectionListener(m_removeRepoHandler);
155: }
156:
157: private void testRemoveModules() {
158: if (!hasAnySet() && m_dsoClient.getModules() != null) {
159: m_dsoClient.unsetModules();
160: m_modules = null;
161: fireXmlObjectStructureChanged();
162: updateChildren();
163: }
164: }
165:
166: private void fireXmlObjectStructureChanged() {
167: fireXmlObjectStructureChanged(m_dsoClient);
168: }
169:
170: public void structureChanged(XmlObjectStructureChangeEvent e) {
171: testRemoveModules();
172: }
173:
174: public boolean hasAnySet() {
175: return m_modules != null
176: && (m_modules.sizeOfRepositoryArray() > 0 || m_modules
177: .sizeOfModuleArray() > 0);
178: }
179:
180: private void internalAddModule(String groupId, String name,
181: String version) {
182: Module module = ensureModules().addNewModule();
183: module.setName(name);
184: module.setGroupId(groupId);
185: module.setVersion(version);
186: createModuleTableItem(module);
187: }
188:
189: private void createModuleTableItem(Module module) {
190: TableItem item = new TableItem(m_layout.m_moduleTable, SWT.NONE);
191: String groupId = module.isSetGroupId() ? module.getGroupId()
192: : "org.terracotta.modules";
193: item.setText(new String[] { module.getName(), groupId,
194: module.getVersion() });
195: item.setData(module);
196: }
197:
198: private void initModules() {
199: m_layout.m_moduleTable.removeAll();
200: if (m_modules == null)
201: return;
202: Module[] modules = m_modules.getModuleArray();
203: for (int i = 0; i < modules.length; i++) {
204: createModuleTableItem(modules[i]);
205: }
206: if (modules.length > 0) {
207: m_layout.m_moduleTable.setSelection(0);
208: }
209: }
210:
211: private void internalAddModuleRepo(String location) {
212: ensureModules().addRepository(location);
213: createModuleRepoTableItem(location);
214: }
215:
216: private void createModuleRepoTableItem(String repo) {
217: TableItem item = new TableItem(m_layout.m_moduleRepoTable,
218: SWT.NONE);
219: item.setText(repo);
220: }
221:
222: private void initModuleRepositories() {
223: m_layout.m_moduleRepoTable.removeAll();
224: if (m_modules == null)
225: return;
226: String[] repos = m_modules.getRepositoryArray();
227: for (int i = 0; i < repos.length; i++) {
228: createModuleRepoTableItem(repos[i]);
229: }
230: if (repos.length > 0) {
231: m_layout.m_moduleRepoTable.setSelection(0);
232: }
233: }
234:
235: private void testEnableRemove() {
236: m_layout.m_removeModuleRepo
237: .setEnabled(m_layout.m_moduleRepoTable
238: .getSelectionCount() > 0);
239: m_layout.m_removeModule.setEnabled(m_layout.m_moduleTable
240: .getSelectionCount() > 0);
241: }
242:
243: private static class Layout {
244: private static final String MODULE_REPOSITORIES = "Module Repositories";
245: private static final String MODULES = "Modules";
246: private static final String LOCATION = "Location";
247: private static final String NAME = "Name";
248: private static final String GROUP_ID = "Group Identifier";
249: private static final String VERSION = "Version";
250: private static final String ADD = "Add...";
251: private static final String REMOVE = "Remove";
252:
253: private Table m_moduleRepoTable;
254: private Button m_addModuleRepo;
255: private Button m_removeModuleRepo;
256: private Table m_moduleTable;
257: private Button m_addModule;
258: private Button m_removeModule;
259:
260: public void reset() {
261: m_moduleRepoTable.removeAll();
262: m_removeModuleRepo.setEnabled(false);
263: m_moduleTable.removeAll();
264: m_removeModule.setEnabled(false);
265: }
266:
267: private Layout(Composite parent) {
268: parent.setLayout(new GridLayout());
269: createModuleRepositoriesPanel(parent);
270: createModulesPanel(parent);
271: }
272:
273: private void createModuleRepositoriesPanel(Composite parent) {
274: Composite comp = new Composite(parent, SWT.NONE);
275: GridLayout gridLayout = new GridLayout(2, false);
276: gridLayout.marginWidth = gridLayout.marginHeight = 0;
277: comp.setLayout(gridLayout);
278: GridData gridData = new GridData(GridData.FILL_BOTH);
279: gridData.horizontalSpan = 3;
280: comp.setLayoutData(gridData);
281:
282: Composite sidePanel = new Composite(comp, SWT.NONE);
283: gridLayout = new GridLayout();
284: gridLayout.marginWidth = gridLayout.marginHeight = 0;
285: sidePanel.setLayout(gridLayout);
286: sidePanel.setLayoutData(new GridData(GridData.FILL_BOTH));
287:
288: Label label = new Label(sidePanel, SWT.NONE);
289: label.setText(MODULE_REPOSITORIES);
290: label.setLayoutData(new GridData(
291: GridData.VERTICAL_ALIGN_BEGINNING));
292:
293: m_moduleRepoTable = new Table(sidePanel, SWT.BORDER
294: | SWT.MULTI | SWT.FULL_SELECTION | SWT.V_SCROLL);
295: m_moduleRepoTable.setHeaderVisible(true);
296: m_moduleRepoTable.setLinesVisible(true);
297: SWTUtil.makeTableColumnsResizeEqualWidth(sidePanel,
298: m_moduleRepoTable);
299: SWTUtil.makeTableColumnsEditable(m_moduleRepoTable,
300: new int[] { 0 });
301: gridData = new GridData(GridData.FILL_BOTH);
302: gridData.heightHint = SWTUtil.tableRowsToPixels(
303: m_moduleRepoTable, 0);
304: m_moduleRepoTable.setLayoutData(gridData);
305:
306: TableColumn column0 = new TableColumn(m_moduleRepoTable,
307: SWT.NONE);
308: column0.setResizable(true);
309: column0.setText(LOCATION);
310: column0.pack();
311:
312: Composite buttonPanel = new Composite(comp, SWT.NONE);
313: gridLayout = new GridLayout();
314: gridLayout.marginWidth = gridLayout.marginHeight = 0;
315: buttonPanel.setLayout(gridLayout);
316: buttonPanel.setLayoutData(new GridData(
317: GridData.VERTICAL_ALIGN_BEGINNING));
318:
319: new Label(buttonPanel, SWT.NONE); // filler
320:
321: m_addModuleRepo = new Button(buttonPanel, SWT.PUSH);
322: m_addModuleRepo.setText(ADD);
323: m_addModuleRepo.setLayoutData(new GridData(
324: GridData.VERTICAL_ALIGN_END));
325: SWTUtil.applyDefaultButtonSize(m_addModuleRepo);
326:
327: m_removeModuleRepo = new Button(buttonPanel, SWT.PUSH);
328: m_removeModuleRepo.setText(REMOVE);
329: m_removeModuleRepo.setEnabled(false);
330: m_removeModuleRepo.setLayoutData(new GridData(
331: GridData.VERTICAL_ALIGN_BEGINNING));
332: SWTUtil.applyDefaultButtonSize(m_removeModuleRepo);
333: }
334:
335: private void createModulesPanel(Composite parent) {
336: Composite comp = new Composite(parent, SWT.NONE);
337: GridLayout gridLayout = new GridLayout(2, false);
338: gridLayout.marginWidth = gridLayout.marginHeight = 0;
339: comp.setLayout(gridLayout);
340: GridData gridData = new GridData(GridData.FILL_BOTH);
341: gridData.horizontalSpan = 3;
342: comp.setLayoutData(gridData);
343:
344: Composite sidePanel = new Composite(comp, SWT.NONE);
345: gridLayout = new GridLayout();
346: gridLayout.marginWidth = gridLayout.marginHeight = 0;
347: sidePanel.setLayout(gridLayout);
348: sidePanel.setLayoutData(new GridData(GridData.FILL_BOTH));
349:
350: Label label = new Label(sidePanel, SWT.NONE);
351: label.setText(MODULES);
352: label.setLayoutData(new GridData(
353: GridData.VERTICAL_ALIGN_BEGINNING));
354:
355: m_moduleTable = new Table(sidePanel, SWT.BORDER | SWT.MULTI
356: | SWT.FULL_SELECTION | SWT.V_SCROLL);
357: m_moduleTable.setHeaderVisible(true);
358: m_moduleTable.setLinesVisible(true);
359: SWTUtil.makeTableColumnsResizeEqualWidth(sidePanel,
360: m_moduleTable);
361: SWTUtil.makeTableColumnsEditable(m_moduleTable, new int[] {
362: 0, 1, 2 });
363: gridData = new GridData(GridData.FILL_BOTH);
364: gridData.heightHint = SWTUtil.tableRowsToPixels(
365: m_moduleTable, 0);
366: m_moduleTable.setLayoutData(gridData);
367:
368: TableColumn column0 = new TableColumn(m_moduleTable,
369: SWT.NONE);
370: column0.setResizable(true);
371: column0.setText(NAME);
372: column0.pack();
373:
374: TableColumn column1 = new TableColumn(m_moduleTable,
375: SWT.NONE);
376: column1.setResizable(true);
377: column1.setText(GROUP_ID);
378: column1.pack();
379:
380: TableColumn column2 = new TableColumn(m_moduleTable,
381: SWT.NONE);
382: column2.setResizable(true);
383: column2.setText(VERSION);
384: column2.pack();
385:
386: Composite buttonPanel = new Composite(comp, SWT.NONE);
387: gridLayout = new GridLayout();
388: gridLayout.marginWidth = gridLayout.marginHeight = 0;
389: buttonPanel.setLayout(gridLayout);
390: buttonPanel.setLayoutData(new GridData(
391: GridData.VERTICAL_ALIGN_BEGINNING));
392:
393: new Label(buttonPanel, SWT.NONE); // filler
394:
395: m_addModule = new Button(buttonPanel, SWT.PUSH);
396: m_addModule.setText(ADD);
397: m_addModule.setLayoutData(new GridData(
398: GridData.VERTICAL_ALIGN_END));
399: SWTUtil.applyDefaultButtonSize(m_addModule);
400:
401: m_removeModule = new Button(buttonPanel, SWT.PUSH);
402: m_removeModule.setText(REMOVE);
403: m_removeModule.setEnabled(false);
404: m_removeModule.setLayoutData(new GridData(
405: GridData.VERTICAL_ALIGN_BEGINNING));
406: SWTUtil.applyDefaultButtonSize(m_removeModule);
407: }
408: }
409:
410: class TableSelectionListener extends SelectionAdapter {
411: public void widgetSelected(SelectionEvent e) {
412: Table table = (Table) e.widget;
413: boolean removeEnabled = table.getItemCount() > 0;
414: Button button = (table == m_layout.m_moduleRepoTable) ? m_layout.m_removeModuleRepo
415: : m_layout.m_removeModule;
416: button.setEnabled(removeEnabled);
417: }
418: }
419:
420: class TableDataListener implements Listener {
421: public void handleEvent(Event event) {
422: TableItem item = (TableItem) event.item;
423: String text = item.getText(event.index);
424: Table table = (Table) event.widget;
425: int index = table.getSelectionIndex();
426:
427: if (event.widget == m_layout.m_moduleRepoTable) {
428: m_modules.setRepositoryArray(index, text);
429: fireModuleRepoChanged(index);
430: } else {
431: Module module = (Module) item.getData();
432: if (event.index == MODULE_NAME_INDEX) {
433: module.setName(text);
434: } else if (event.index == MODULE_GROUP_ID_INDEX) {
435: module.setGroupId(text);
436: } else {
437: module.setVersion(text);
438: }
439: fireModuleChanged(index);
440: }
441: table.setSelection(index);
442: }
443: }
444:
445: class ModuleMouseMoveListener implements MouseMoveListener {
446: public void mouseMove(MouseEvent e) {
447: String tip = null;
448: TableItem item = m_layout.m_moduleTable.getItem(new Point(
449: e.x, e.y));
450: if (item != null) {
451: Module module = (Module) item.getData();
452: TcPlugin plugin = TcPlugin.getDefault();
453: ModulesConfiguration modulesConfig = plugin
454: .getModulesConfiguration(getProject());
455: if (modulesConfig != null) {
456: ModuleInfo moduleInfo = modulesConfig
457: .getModuleInfo(module);
458: if (moduleInfo != null) {
459: Exception error = moduleInfo.getError();
460: if (error != null) {
461: tip = error.getMessage();
462: } else {
463: URL loc = moduleInfo.getLocation();
464: if (loc != null) {
465: tip = loc.toString();
466: }
467: }
468: }
469: }
470: }
471:
472: final IWorkbenchWindow window = PlatformUI.getWorkbench()
473: .getActiveWorkbenchWindow();
474: if (window instanceof ApplicationWindow) {
475: ((ApplicationWindow) window).setStatus(tip);
476: }
477: }
478: }
479:
480: class AddModuleHandler extends SelectionAdapter {
481: public void widgetSelected(SelectionEvent e) {
482: NewAddModuleDialog dialog = new NewAddModuleDialog(
483: getShell(), MODULE_DECLARATION, MODULE_DECLARATION,
484: m_modules);
485: dialog
486: .addValueListener(new NewAddModuleDialog.ValueListener() {
487: public void setValue(Modules modules) {
488: m_dsoClient.setModules(modules);
489: fireModulesChanged();
490: fireModuleReposChanged();
491: }
492: });
493: dialog.open();
494: }
495: }
496:
497: class RemoveModuleHandler extends SelectionAdapter {
498: public void widgetSelected(SelectionEvent e) {
499: int[] selection = m_layout.m_moduleTable
500: .getSelectionIndices();
501: Modules modules = ensureModules();
502: for (int i = selection.length - 1; i >= 0; i--) {
503: modules.removeModule(selection[i]);
504: }
505: m_layout.m_moduleTable.remove(selection);
506: testEnableRemove();
507: testRemoveModules();
508: fireModulesChanged();
509: }
510: }
511:
512: class AddRepoHandler extends SelectionAdapter {
513: public void widgetSelected(SelectionEvent e) {
514: RepoLocationDialog dialog = new RepoLocationDialog(
515: getShell(), REPO_DECLARATION, MODULE_REPO_LOCATION);
516: dialog
517: .addValueListener(new RepoLocationDialog.ValueListener() {
518: public void setValues(String repoLocation) {
519: if (repoLocation != null
520: && !(repoLocation = repoLocation
521: .trim()).equals("")) {
522: internalAddModuleRepo(repoLocation);
523: testRemoveModules();
524: fireModuleReposChanged();
525: }
526: }
527: });
528: dialog.open();
529: }
530: }
531:
532: class RemoveRepoHandler extends SelectionAdapter {
533: public void widgetSelected(SelectionEvent e) {
534: int[] selection = m_layout.m_moduleRepoTable
535: .getSelectionIndices();
536: Modules modules = ensureModules();
537: for (int i = selection.length - 1; i >= 0; i--) {
538: modules.removeRepository(selection[i]);
539: }
540: m_layout.m_moduleRepoTable.remove(selection);
541: testEnableRemove();
542: testRemoveModules();
543: fireModuleReposChanged();
544: }
545: }
546: }
|