1 /**********************************************************************
2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpeclipse.wiki.ui.internal;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
18 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
19 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.ColumnWeightData;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.jface.viewers.TableLayout;
30 import org.eclipse.jface.viewers.TableViewer;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Table;
41 import org.eclipse.swt.widgets.TableColumn;
45 public class ConfigurationComposite extends Composite {
46 protected Table table;
47 protected TableViewer tableViewer;
49 protected Button edit;
50 protected Button remove;
51 protected Button start;
52 protected Button stop;
54 protected List selection2;
56 public ConfigurationComposite(Composite parent, int style) {
62 protected void createWidgets() {
63 GridLayout layout = new GridLayout();
64 layout.horizontalSpacing = 6;
65 layout.verticalSpacing = 6;
66 layout.marginWidth = 0;
67 layout.marginHeight = 0;
68 layout.numColumns = 2;
71 GridData data = new GridData(GridData.FILL_BOTH);
74 Label label = new Label(this, SWT.WRAP);
75 label.setText(WikiEditorPlugin.getResource("%configurationsList"));
76 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER));
78 label = new Label(this, SWT.NONE);
80 table = new Table(this, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
81 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
83 // WorkbenchHelp.setHelp(table, ContextIds.PREF_MONITORS);
85 table.setLayoutData(data);
86 table.setHeaderVisible(true);
87 table.setLinesVisible(true);
89 TableLayout tableLayout = new TableLayout();
91 TableColumn statusColumn = new TableColumn(table, SWT.NONE);
92 statusColumn.setText(WikiEditorPlugin.getResource("%columnName"));
93 ColumnWeightData colData = new ColumnWeightData(5, 30, true);
94 tableLayout.addColumnData(colData);
96 TableColumn typeColumn = new TableColumn(table, SWT.NONE);
97 typeColumn.setText(WikiEditorPlugin.getResource("%columnType"));
98 colData = new ColumnWeightData(5, 30, true);
99 tableLayout.addColumnData(colData);
101 TableColumn urlColumn = new TableColumn(table, SWT.NONE);
102 urlColumn.setText(WikiEditorPlugin.getResource("%columnUser"));
103 colData = new ColumnWeightData(5, 30, true);
104 tableLayout.addColumnData(colData);
106 TableColumn localColumn = new TableColumn(table, SWT.NONE);
107 localColumn.setText(WikiEditorPlugin.getResource("%columnURL"));
108 colData = new ColumnWeightData(5, 150, true);
109 tableLayout.addColumnData(colData);
111 table.setLayout(tableLayout);
113 tableViewer = new TableViewer(table);
114 tableViewer.setContentProvider(new ConfigurationContentProvider());
115 tableViewer.setLabelProvider(new ConfigurationTableLabelProvider());
116 tableViewer.setInput("root");
118 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
119 public void selectionChanged(SelectionChangedEvent event) {
120 setSelection(event.getSelection());
124 Composite buttonComp = new Composite(this, SWT.NONE);
125 layout = new GridLayout();
126 layout.horizontalSpacing = 0;
127 layout.verticalSpacing = 8;
128 layout.marginWidth = 0;
129 layout.marginHeight = 0;
130 layout.numColumns = 1;
131 buttonComp.setLayout(layout);
132 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_FILL);
133 buttonComp.setLayoutData(data);
135 Button add = SWTUtil.createButton(buttonComp, WikiEditorPlugin.getResource("%add"));
136 add.addSelectionListener(new SelectionAdapter() {
137 public void widgetSelected(SelectionEvent e) {
138 ConfigurationDialog dialog = new ConfigurationDialog(getShell());
139 if (dialog.open() == Window.CANCEL)
141 tableViewer.refresh();
143 List list = WikiEditorPlugin.getConfigurations();
144 Object configuration = list.get(list.size() - 1);
145 tableViewer.setSelection(new StructuredSelection(configuration));
149 edit = SWTUtil.createButton(buttonComp, WikiEditorPlugin.getResource("%edit"));
150 edit.addSelectionListener(new SelectionAdapter() {
151 public void widgetSelected(SelectionEvent e) {
152 IConfiguration monitor = (IConfiguration) getSelection().get(0);
153 IConfigurationWorkingCopy wc = monitor.getWorkingCopy();
155 ConfigurationDialog dialog = new ConfigurationDialog(getShell(), wc);
156 if (dialog.open() != Window.CANCEL) {
158 tableViewer.refresh(wc.save());
159 } catch (Exception ex) { }
163 edit.setEnabled(false);
165 remove = SWTUtil.createButton(buttonComp, WikiEditorPlugin.getResource("%remove"));
166 remove.addSelectionListener(new SelectionAdapter() {
167 public void widgetSelected(SelectionEvent e) {
168 Iterator iterator = getSelection().iterator();
169 while (iterator.hasNext()) {
170 IConfiguration monitor = (IConfiguration) iterator.next();
173 } catch (Exception ex) { }
174 tableViewer.remove(monitor);
176 List list = WikiEditorPlugin.getConfigurations();
177 Object monitor2 = list.get(list.size() - 1);
178 tableViewer.setSelection(new StructuredSelection(monitor2));
182 remove.setEnabled(false);
184 start = SWTUtil.createButton(buttonComp, WikiEditorPlugin.getResource("%start"));
185 start.addSelectionListener(new SelectionAdapter() {
186 public void widgetSelected(SelectionEvent e) {
187 Iterator iterator = getSelection().iterator();
188 while (iterator.hasNext()) {
189 IConfiguration configuration = (IConfiguration) iterator.next();
191 // WikiEditorPlugin.startMonitor(monitor);
192 // } catch (CoreException ce) {
193 // MessageDialog.openError(getShell(), WikiEditorPlugin.getResource("%errorDialogTitle"), ce.getStatus().getMessage());
194 // } catch (Exception ce) {
195 // MessageDialog.openError(getShell(), WikiEditorPlugin.getResource("%errorDialogTitle"), ce.getMessage());
197 tableViewer.refresh(configuration, true);
199 tableViewer.setSelection(tableViewer.getSelection());
202 start.setEnabled(false);
204 stop = SWTUtil.createButton(buttonComp, WikiEditorPlugin.getResource("%stop"));
205 stop.addSelectionListener(new SelectionAdapter() {
206 public void widgetSelected(SelectionEvent e) {
207 Iterator iterator = getSelection().iterator();
208 while (iterator.hasNext()) {
209 IConfiguration monitor = (IConfiguration) iterator.next();
211 // WikiEditorPlugin.stopMonitor(monitor);
212 } catch (Exception ex) { }
213 tableViewer.refresh(monitor, true);
215 tableViewer.setSelection(tableViewer.getSelection());
218 stop.setEnabled(false);
221 protected List getSelection() {
225 protected void setSelection(ISelection sel2) {
226 IStructuredSelection sel = (IStructuredSelection) sel2;
227 Iterator iterator = sel.iterator();
228 selection2 = new ArrayList();
230 while (iterator.hasNext()) {
231 Object obj = iterator.next();
232 if (obj instanceof IConfiguration)
236 if (!selection2.isEmpty()) {
237 remove.setEnabled(true);
239 boolean allStopped = true;
240 boolean allStarted = true;
242 iterator = selection2.iterator();
243 while (iterator.hasNext()) {
244 IConfiguration monitor = (IConfiguration) iterator.next();
245 if (monitor.isActive())
250 start.setEnabled(allStopped);
251 stop.setEnabled(allStarted);
252 edit.setEnabled(selection2.size() == 1 && allStopped);
254 edit.setEnabled(false);
255 remove.setEnabled(false);
256 start.setEnabled(false);
257 stop.setEnabled(false);