Refactory: replaced internal copy of Assert class with org.eclipse.core.runtime.Assert
[phpeclipse.git] / net.sourceforge.phpeclipse.phphelp / src / net / sourceforge / phpdt / httpquery / preferences / 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.phpdt.httpquery.preferences;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import net.sourceforge.phpdt.httpquery.config.IConfiguration;
18 import net.sourceforge.phpdt.httpquery.config.IConfigurationWorkingCopy;
19 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
20
21 import org.eclipse.jface.viewers.ColumnWeightData;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.viewers.TableLayout;
28 import org.eclipse.jface.viewers.TableViewer;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Table;
39 import org.eclipse.swt.widgets.TableColumn;
40
41 /**
42  * 
43  */
44 public class ConfigurationComposite extends Composite {
45         protected Table table;
46
47         protected TableViewer tableViewer;
48
49         protected Button edit;
50
51         protected Button remove;
52
53         // protected Button start;
54         // protected Button stop;
55
56         protected List selection2;
57
58         public ConfigurationComposite(Composite parent, int style) {
59                 super(parent, style);
60
61                 createWidgets();
62         }
63
64         protected void createWidgets() {
65                 GridLayout layout = new GridLayout();
66                 layout.horizontalSpacing = 6;
67                 layout.verticalSpacing = 6;
68                 layout.marginWidth = 0;
69                 layout.marginHeight = 0;
70                 layout.numColumns = 2;
71                 setLayout(layout);
72
73                 GridData data = new GridData(GridData.FILL_BOTH);
74                 setLayoutData(data);
75
76                 Label label = new Label(this, SWT.WRAP);
77                 label.setText(PHPHelpPlugin.getResource("%configurationsList"));
78                 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
79                                 | GridData.VERTICAL_ALIGN_CENTER));
80
81                 label = new Label(this, SWT.NONE);
82
83                 table = new Table(this, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL
84                                 | SWT.MULTI | SWT.FULL_SELECTION);
85                 data = new GridData(GridData.FILL_HORIZONTAL
86                                 | GridData.VERTICAL_ALIGN_FILL);
87                 data.widthHint = 300;
88                 data.heightHint = 300;
89                 // WorkbenchHelp.setHelp(table, ContextIds.PREF_MONITORS);
90
91                 table.setLayoutData(data);
92                 table.setHeaderVisible(true);
93                 table.setLinesVisible(true);
94
95                 TableLayout tableLayout = new TableLayout();
96
97                 TableColumn statusColumn = new TableColumn(table, SWT.NONE);
98                 statusColumn.setText(PHPHelpPlugin.getResource("%columnName"));
99                 ColumnWeightData colData = new ColumnWeightData(5, 30, true);
100                 tableLayout.addColumnData(colData);
101
102                 TableColumn typeColumn = new TableColumn(table, SWT.NONE);
103                 typeColumn.setText(PHPHelpPlugin.getResource("%columnType"));
104                 colData = new ColumnWeightData(5, 30, true);
105                 tableLayout.addColumnData(colData);
106
107                 // TableColumn urlColumn = new TableColumn(table, SWT.NONE);
108                 // urlColumn.setText(PHPHelpPlugin.getResource("%columnUser"));
109                 // colData = new ColumnWeightData(5, 30, true);
110                 // tableLayout.addColumnData(colData);
111
112                 TableColumn localColumn = new TableColumn(table, SWT.NONE);
113                 localColumn.setText(PHPHelpPlugin.getResource("%columnURL"));
114                 colData = new ColumnWeightData(5, 150, true);
115                 tableLayout.addColumnData(colData);
116
117                 table.setLayout(tableLayout);
118
119                 tableViewer = new TableViewer(table);
120                 tableViewer.setContentProvider(new ConfigurationContentProvider());
121                 tableViewer.setLabelProvider(new ConfigurationTableLabelProvider());
122                 tableViewer.setInput("root");
123                 tableViewer
124                                 .addSelectionChangedListener(new ISelectionChangedListener() {
125                                         public void selectionChanged(SelectionChangedEvent event) {
126                                                 setSelection(event.getSelection());
127                                         }
128                                 });
129
130                 Composite buttonComp = new Composite(this, SWT.NONE);
131                 layout = new GridLayout();
132                 layout.horizontalSpacing = 0;
133                 layout.verticalSpacing = 8;
134                 layout.marginWidth = 0;
135                 layout.marginHeight = 0;
136                 layout.numColumns = 1;
137                 buttonComp.setLayout(layout);
138                 data = new GridData(GridData.HORIZONTAL_ALIGN_END
139                                 | GridData.VERTICAL_ALIGN_FILL);
140                 buttonComp.setLayoutData(data);
141
142                 Button add = SWTUtil.createButton(buttonComp, PHPHelpPlugin
143                                 .getResource("%add"));
144                 add.addSelectionListener(new SelectionAdapter() {
145                         public void widgetSelected(SelectionEvent e) {
146                                 ConfigurationDialog dialog = new ConfigurationDialog(getShell());
147                                 if (dialog.open() == Window.CANCEL)
148                                         return;
149                                 tableViewer.refresh();
150
151                                 List list = PHPHelpPlugin.getConfigurations();
152                                 Object configuration = list.get(list.size() - 1);
153                                 tableViewer
154                                                 .setSelection(new StructuredSelection(configuration));
155                         }
156                 });
157
158                 edit = SWTUtil.createButton(buttonComp, PHPHelpPlugin
159                                 .getResource("%edit"));
160                 edit.addSelectionListener(new SelectionAdapter() {
161                         public void widgetSelected(SelectionEvent e) {
162                                 IConfiguration monitor = (IConfiguration) getSelection().get(0);
163                                 IConfigurationWorkingCopy wc = monitor.getWorkingCopy();
164
165                                 ConfigurationDialog dialog = new ConfigurationDialog(
166                                                 getShell(), wc);
167                                 if (dialog.open() != Window.CANCEL) {
168                                         try {
169                                                 tableViewer.refresh(wc.save());
170                                         } catch (Exception ex) {
171                                         }
172                                 }
173                         }
174                 });
175                 edit.setEnabled(false);
176
177                 remove = SWTUtil.createButton(buttonComp, PHPHelpPlugin
178                                 .getResource("%remove"));
179                 remove.addSelectionListener(new SelectionAdapter() {
180                         public void widgetSelected(SelectionEvent e) {
181                                 Iterator iterator = getSelection().iterator();
182                                 while (iterator.hasNext()) {
183                                         IConfiguration monitor = (IConfiguration) iterator.next();
184                                         try {
185                                                 monitor.delete();
186                                         } catch (Exception ex) {
187                                         }
188                                         tableViewer.remove(monitor);
189
190                                         List list = PHPHelpPlugin.getConfigurations();
191                                         Object monitor2 = list.get(list.size() - 1);
192                                         tableViewer.setSelection(new StructuredSelection(monitor2));
193                                 }
194                         }
195                 });
196                 remove.setEnabled(false);
197
198         }
199
200         protected List getSelection() {
201                 return selection2;
202         }
203
204         protected void setSelection(ISelection sel2) {
205                 IStructuredSelection sel = (IStructuredSelection) sel2;
206                 Iterator iterator = sel.iterator();
207                 selection2 = new ArrayList();
208
209                 while (iterator.hasNext()) {
210                         Object obj = iterator.next();
211                         if (obj instanceof IConfiguration)
212                                 selection2.add(obj);
213                 }
214
215                 if (!selection2.isEmpty()) {
216                         remove.setEnabled(true);
217
218                         edit.setEnabled(true);
219                 } else {
220                         edit.setEnabled(false);
221                         remove.setEnabled(false);
222                 }
223         }
224 }