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.phpdt.monitor.ui.internal;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import net.sourceforge.phpdt.monitor.core.IMonitor;
18 import net.sourceforge.phpdt.monitor.core.IMonitorWorkingCopy;
19 import net.sourceforge.phpdt.monitor.core.MonitorCore;
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;
42 import org.eclipse.ui.help.WorkbenchHelp;
46 public class MonitorComposite extends Composite {
47 protected Table table;
48 protected TableViewer tableViewer;
50 protected Button edit;
51 protected Button remove;
52 protected Button start;
53 protected Button stop;
55 protected List selection2;
57 public MonitorComposite(Composite parent, int style) {
63 protected void createWidgets() {
64 GridLayout layout = new GridLayout();
65 layout.horizontalSpacing = 6;
66 layout.verticalSpacing = 6;
67 layout.marginWidth = 0;
68 layout.marginHeight = 0;
69 layout.numColumns = 2;
72 GridData data = new GridData(GridData.FILL_BOTH);
75 Label label = new Label(this, SWT.WRAP);
76 label.setText(MonitorUIPlugin.getResource("%monitorList"));
77 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER));
79 label = new Label(this, SWT.NONE);
81 table = new Table(this, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
82 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
84 WorkbenchHelp.setHelp(table, ContextIds.PREF_MONITORS);
86 table.setLayoutData(data);
87 table.setHeaderVisible(true);
88 table.setLinesVisible(true);
90 TableLayout tableLayout = new TableLayout();
92 TableColumn statusColumn = new TableColumn(table, SWT.NONE);
93 statusColumn.setText(MonitorUIPlugin.getResource("%columnStatus"));
94 ColumnWeightData colData = new ColumnWeightData(6, 60, true);
95 tableLayout.addColumnData(colData);
97 TableColumn remoteColumn = new TableColumn(table, SWT.NONE);
98 remoteColumn.setText(MonitorUIPlugin.getResource("%columnRemote"));
99 colData = new ColumnWeightData(12, 120, true);
100 tableLayout.addColumnData(colData);
102 TableColumn httpColumn = new TableColumn(table, SWT.NONE);
103 httpColumn.setText(MonitorUIPlugin.getResource("%columnType"));
104 colData = new ColumnWeightData(5, 55, true);
105 tableLayout.addColumnData(colData);
107 TableColumn localColumn = new TableColumn(table, SWT.NONE);
108 localColumn.setText(MonitorUIPlugin.getResource("%columnLocal"));
109 colData = new ColumnWeightData(5, 50, true);
110 tableLayout.addColumnData(colData);
112 table.setLayout(tableLayout);
114 tableViewer = new TableViewer(table);
115 tableViewer.setContentProvider(new MonitorContentProvider());
116 tableViewer.setLabelProvider(new MonitorTableLabelProvider());
117 tableViewer.setInput("root");
119 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
120 public void selectionChanged(SelectionChangedEvent event) {
121 setSelection(event.getSelection());
125 Composite buttonComp = new Composite(this, SWT.NONE);
126 layout = new GridLayout();
127 layout.horizontalSpacing = 0;
128 layout.verticalSpacing = 8;
129 layout.marginWidth = 0;
130 layout.marginHeight = 0;
131 layout.numColumns = 1;
132 buttonComp.setLayout(layout);
133 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_FILL);
134 buttonComp.setLayoutData(data);
136 Button add = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%add"));
137 add.addSelectionListener(new SelectionAdapter() {
138 public void widgetSelected(SelectionEvent e) {
139 MonitorDialog dialog = new MonitorDialog(getShell());
140 if (dialog.open() == Window.CANCEL)
142 tableViewer.refresh();
144 List list = MonitorCore.getMonitors();
145 Object monitor = list.get(list.size() - 1);
146 tableViewer.setSelection(new StructuredSelection(monitor));
150 edit = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%edit"));
151 edit.addSelectionListener(new SelectionAdapter() {
152 public void widgetSelected(SelectionEvent e) {
153 IMonitor monitor = (IMonitor) getSelection().get(0);
154 IMonitorWorkingCopy wc = monitor.getWorkingCopy();
156 MonitorDialog dialog = new MonitorDialog(getShell(), wc);
157 if (dialog.open() != Window.CANCEL) {
159 tableViewer.refresh(wc.save());
160 } catch (Exception ex) { }
164 edit.setEnabled(false);
166 remove = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%remove"));
167 remove.addSelectionListener(new SelectionAdapter() {
168 public void widgetSelected(SelectionEvent e) {
169 Iterator iterator = getSelection().iterator();
170 while (iterator.hasNext()) {
171 IMonitor monitor = (IMonitor) iterator.next();
174 } catch (Exception ex) { }
175 tableViewer.remove(monitor);
177 List list = MonitorCore.getMonitors();
178 Object monitor2 = list.get(list.size() - 1);
179 tableViewer.setSelection(new StructuredSelection(monitor2));
183 remove.setEnabled(false);
185 start = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%start"));
186 start.addSelectionListener(new SelectionAdapter() {
187 public void widgetSelected(SelectionEvent e) {
188 Iterator iterator = getSelection().iterator();
189 while (iterator.hasNext()) {
190 IMonitor monitor = (IMonitor) iterator.next();
192 MonitorCore.startMonitor(monitor);
193 } catch (CoreException ce) {
194 MessageDialog.openError(getShell(), MonitorUIPlugin.getResource("%errorDialogTitle"), ce.getStatus().getMessage());
195 } catch (Exception ce) {
196 MessageDialog.openError(getShell(), MonitorUIPlugin.getResource("%errorDialogTitle"), ce.getMessage());
198 tableViewer.refresh(monitor, true);
200 tableViewer.setSelection(tableViewer.getSelection());
203 start.setEnabled(false);
205 stop = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%stop"));
206 stop.addSelectionListener(new SelectionAdapter() {
207 public void widgetSelected(SelectionEvent e) {
208 Iterator iterator = getSelection().iterator();
209 while (iterator.hasNext()) {
210 IMonitor monitor = (IMonitor) iterator.next();
212 MonitorCore.stopMonitor(monitor);
213 } catch (Exception ex) { }
214 tableViewer.refresh(monitor, true);
216 tableViewer.setSelection(tableViewer.getSelection());
219 stop.setEnabled(false);
222 protected List getSelection() {
226 protected void setSelection(ISelection sel2) {
227 IStructuredSelection sel = (IStructuredSelection) sel2;
228 Iterator iterator = sel.iterator();
229 selection2 = new ArrayList();
231 while (iterator.hasNext()) {
232 Object obj = iterator.next();
233 if (obj instanceof IMonitor)
237 if (!selection2.isEmpty()) {
238 remove.setEnabled(true);
240 boolean allStopped = true;
241 boolean allStarted = true;
243 iterator = selection2.iterator();
244 while (iterator.hasNext()) {
245 IMonitor monitor = (IMonitor) iterator.next();
246 if (monitor.isRunning())
251 start.setEnabled(allStopped);
252 stop.setEnabled(allStarted);
253 edit.setEnabled(selection2.size() == 1 && allStopped);
255 edit.setEnabled(false);
256 remove.setEnabled(false);
257 start.setEnabled(false);
258 stop.setEnabled(false);