Organized imports
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.ui / src / net / sourceforge / phpdt / monitor / ui / internal / MonitorComposite.java
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.ui.internal;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import net.sourceforge.phpdt.monitor.core.IMonitor;
18 import net.sourceforge.phpdt.monitor.core.IMonitorWorkingCopy;
19 import net.sourceforge.phpdt.monitor.core.MonitorCore;
20
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;
43 /**
44  * 
45  */
46 public class MonitorComposite extends Composite {
47         protected Table table;
48         protected TableViewer tableViewer;
49         
50         protected Button edit;
51         protected Button remove;
52         protected Button start;
53         protected Button stop;
54         
55         protected List selection2;
56         
57         public MonitorComposite(Composite parent, int style) {
58                 super(parent, style);
59                 
60                 createWidgets();
61         }
62         
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;
70                 setLayout(layout);
71
72                 GridData data = new GridData(GridData.FILL_BOTH);
73                 setLayoutData(data);
74                 
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));
78                 
79                 label = new Label(this, SWT.NONE);
80
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);
83                 data.widthHint = 300;
84                 WorkbenchHelp.setHelp(table, ContextIds.PREF_MONITORS);
85                 
86                 table.setLayoutData(data);
87                 table.setHeaderVisible(true);
88                 table.setLinesVisible(true);
89                 
90                 TableLayout tableLayout = new TableLayout();
91                 
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);
96                 
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);
101                 
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);
106                 
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);
111                 
112                 table.setLayout(tableLayout);
113                 
114                 tableViewer = new TableViewer(table);
115                 tableViewer.setContentProvider(new MonitorContentProvider());
116                 tableViewer.setLabelProvider(new MonitorTableLabelProvider());
117                 tableViewer.setInput("root");
118                 
119                 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
120                         public void selectionChanged(SelectionChangedEvent event) {
121                                 setSelection(event.getSelection());
122                         }
123                 });
124                 
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);
135                 
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)
141                                         return;
142                                 tableViewer.refresh();
143                                 
144                                 List list = MonitorCore.getMonitors();
145                                 Object monitor = list.get(list.size() - 1);
146                                 tableViewer.setSelection(new StructuredSelection(monitor));
147                         }
148                 });             
149                 
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();
155                                 
156                                 MonitorDialog dialog = new MonitorDialog(getShell(), wc);
157                                 if (dialog.open() != Window.CANCEL) {
158                                         try {
159                                                 tableViewer.refresh(wc.save());
160                                         } catch (Exception ex) { }
161                                 }
162                         }
163                 });
164                 edit.setEnabled(false);
165                 
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();
172                                         try {
173                                                 monitor.delete();
174                                         } catch (Exception ex) { }
175                                         tableViewer.remove(monitor);
176                                         
177                                         List list = MonitorCore.getMonitors();
178                                         Object monitor2 = list.get(list.size() - 1);
179                                         tableViewer.setSelection(new StructuredSelection(monitor2));
180                                 }
181                         }
182                 });
183                 remove.setEnabled(false);
184                 
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();
191                                         try {
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());
197                                         }
198                                         tableViewer.refresh(monitor, true);
199                                 }
200                                 tableViewer.setSelection(tableViewer.getSelection());
201                         }
202                 });
203                 start.setEnabled(false);
204                 
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();
211                                         try {
212                                                 MonitorCore.stopMonitor(monitor);
213                                         } catch (Exception ex) { }
214                                         tableViewer.refresh(monitor, true);
215                                 }
216                                 tableViewer.setSelection(tableViewer.getSelection());
217                         }
218                 });
219                 stop.setEnabled(false);
220         }
221
222         protected List getSelection() {
223                 return selection2;
224         }
225
226         protected void setSelection(ISelection sel2) {
227                 IStructuredSelection sel = (IStructuredSelection) sel2;
228                 Iterator iterator = sel.iterator();
229                 selection2 = new ArrayList();
230                 
231                 while (iterator.hasNext()) {
232                         Object obj = iterator.next();
233                         if (obj instanceof IMonitor)
234                                 selection2.add(obj);
235                 }
236                 
237                 if (!selection2.isEmpty()) {
238                         remove.setEnabled(true);
239                         
240                         boolean allStopped = true;
241                         boolean allStarted = true;
242                         
243                         iterator = selection2.iterator();
244                         while (iterator.hasNext()) {
245                                 IMonitor monitor = (IMonitor) iterator.next();
246                                 if (monitor.isRunning())
247                                         allStopped = false;
248                                 else
249                                         allStarted = false;
250                         }
251                         start.setEnabled(allStopped);
252                         stop.setEnabled(allStarted);
253                         edit.setEnabled(selection2.size() == 1 && allStopped);
254                 } else {
255                         edit.setEnabled(false);
256                         remove.setEnabled(false);
257                         start.setEnabled(false);
258                         stop.setEnabled(false);
259                 }
260         }
261 }