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.*;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Table;
34 import org.eclipse.swt.widgets.TableColumn;
35 import org.eclipse.ui.help.WorkbenchHelp;
39 public class MonitorComposite extends Composite {
40 protected Table table;
41 protected TableViewer tableViewer;
43 protected Button edit;
44 protected Button remove;
45 protected Button start;
46 protected Button stop;
48 protected List selection2;
50 public MonitorComposite(Composite parent, int style) {
56 protected void createWidgets() {
57 GridLayout layout = new GridLayout();
58 layout.horizontalSpacing = 6;
59 layout.verticalSpacing = 6;
60 layout.marginWidth = 0;
61 layout.marginHeight = 0;
62 layout.numColumns = 2;
65 GridData data = new GridData(GridData.FILL_BOTH);
68 Label label = new Label(this, SWT.WRAP);
69 label.setText(MonitorUIPlugin.getResource("%monitorList"));
70 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER));
72 label = new Label(this, SWT.NONE);
74 table = new Table(this, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
75 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
77 WorkbenchHelp.setHelp(table, ContextIds.PREF_MONITORS);
79 table.setLayoutData(data);
80 table.setHeaderVisible(true);
81 table.setLinesVisible(true);
83 TableLayout tableLayout = new TableLayout();
85 TableColumn statusColumn = new TableColumn(table, SWT.NONE);
86 statusColumn.setText(MonitorUIPlugin.getResource("%columnStatus"));
87 ColumnWeightData colData = new ColumnWeightData(6, 60, true);
88 tableLayout.addColumnData(colData);
90 TableColumn remoteColumn = new TableColumn(table, SWT.NONE);
91 remoteColumn.setText(MonitorUIPlugin.getResource("%columnRemote"));
92 colData = new ColumnWeightData(12, 120, true);
93 tableLayout.addColumnData(colData);
95 TableColumn httpColumn = new TableColumn(table, SWT.NONE);
96 httpColumn.setText(MonitorUIPlugin.getResource("%columnType"));
97 colData = new ColumnWeightData(5, 55, true);
98 tableLayout.addColumnData(colData);
100 TableColumn localColumn = new TableColumn(table, SWT.NONE);
101 localColumn.setText(MonitorUIPlugin.getResource("%columnLocal"));
102 colData = new ColumnWeightData(5, 50, true);
103 tableLayout.addColumnData(colData);
105 table.setLayout(tableLayout);
107 tableViewer = new TableViewer(table);
108 tableViewer.setContentProvider(new MonitorContentProvider());
109 tableViewer.setLabelProvider(new MonitorTableLabelProvider());
110 tableViewer.setInput("root");
112 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
113 public void selectionChanged(SelectionChangedEvent event) {
114 setSelection(event.getSelection());
118 Composite buttonComp = new Composite(this, SWT.NONE);
119 layout = new GridLayout();
120 layout.horizontalSpacing = 0;
121 layout.verticalSpacing = 8;
122 layout.marginWidth = 0;
123 layout.marginHeight = 0;
124 layout.numColumns = 1;
125 buttonComp.setLayout(layout);
126 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_FILL);
127 buttonComp.setLayoutData(data);
129 Button add = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%add"));
130 add.addSelectionListener(new SelectionAdapter() {
131 public void widgetSelected(SelectionEvent e) {
132 MonitorDialog dialog = new MonitorDialog(getShell());
133 if (dialog.open() == Window.CANCEL)
135 tableViewer.refresh();
137 List list = MonitorCore.getMonitors();
138 Object monitor = list.get(list.size() - 1);
139 tableViewer.setSelection(new StructuredSelection(monitor));
143 edit = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%edit"));
144 edit.addSelectionListener(new SelectionAdapter() {
145 public void widgetSelected(SelectionEvent e) {
146 IMonitor monitor = (IMonitor) getSelection().get(0);
147 IMonitorWorkingCopy wc = monitor.getWorkingCopy();
149 MonitorDialog dialog = new MonitorDialog(getShell(), wc);
150 if (dialog.open() != Window.CANCEL) {
152 tableViewer.refresh(wc.save());
153 } catch (Exception ex) { }
157 edit.setEnabled(false);
159 remove = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%remove"));
160 remove.addSelectionListener(new SelectionAdapter() {
161 public void widgetSelected(SelectionEvent e) {
162 Iterator iterator = getSelection().iterator();
163 while (iterator.hasNext()) {
164 IMonitor monitor = (IMonitor) iterator.next();
167 } catch (Exception ex) { }
168 tableViewer.remove(monitor);
170 List list = MonitorCore.getMonitors();
171 Object monitor2 = list.get(list.size() - 1);
172 tableViewer.setSelection(new StructuredSelection(monitor2));
176 remove.setEnabled(false);
178 start = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%start"));
179 start.addSelectionListener(new SelectionAdapter() {
180 public void widgetSelected(SelectionEvent e) {
181 Iterator iterator = getSelection().iterator();
182 while (iterator.hasNext()) {
183 IMonitor monitor = (IMonitor) iterator.next();
185 MonitorCore.startMonitor(monitor);
186 } catch (CoreException ce) {
187 MessageDialog.openError(getShell(), MonitorUIPlugin.getResource("%errorDialogTitle"), ce.getStatus().getMessage());
188 } catch (Exception ce) {
189 MessageDialog.openError(getShell(), MonitorUIPlugin.getResource("%errorDialogTitle"), ce.getMessage());
191 tableViewer.refresh(monitor, true);
193 tableViewer.setSelection(tableViewer.getSelection());
196 start.setEnabled(false);
198 stop = SWTUtil.createButton(buttonComp, MonitorUIPlugin.getResource("%stop"));
199 stop.addSelectionListener(new SelectionAdapter() {
200 public void widgetSelected(SelectionEvent e) {
201 Iterator iterator = getSelection().iterator();
202 while (iterator.hasNext()) {
203 IMonitor monitor = (IMonitor) iterator.next();
205 MonitorCore.stopMonitor(monitor);
206 } catch (Exception ex) { }
207 tableViewer.refresh(monitor, true);
209 tableViewer.setSelection(tableViewer.getSelection());
212 stop.setEnabled(false);
215 protected List getSelection() {
219 protected void setSelection(ISelection sel2) {
220 IStructuredSelection sel = (IStructuredSelection) sel2;
221 Iterator iterator = sel.iterator();
222 selection2 = new ArrayList();
224 while (iterator.hasNext()) {
225 Object obj = iterator.next();
226 if (obj instanceof IMonitor)
230 if (!selection2.isEmpty()) {
231 remove.setEnabled(true);
233 boolean allStopped = true;
234 boolean allStarted = true;
236 iterator = selection2.iterator();
237 while (iterator.hasNext()) {
238 IMonitor monitor = (IMonitor) iterator.next();
239 if (monitor.isRunning())
244 start.setEnabled(allStopped);
245 stop.setEnabled(allStarted);
246 edit.setEnabled(selection2.size() == 1 && allStopped);
248 edit.setEnabled(false);
249 remove.setEnabled(false);
250 start.setEnabled(false);
251 stop.setEnabled(false);