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