1436190 - Test additions & refactoring
[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 public class ConfigurationComposite extends Composite {
44         protected Table table;
45         protected TableViewer tableViewer;
46
47         protected Button edit;
48         protected Button remove;
49 //      protected Button start;
50 //      protected Button stop;
51
52         protected List selection2;
53
54         public ConfigurationComposite(Composite parent, int style) {
55                 super(parent, style);
56
57                 createWidgets();
58         }
59
60         protected void createWidgets() {
61                 GridLayout layout = new GridLayout();
62                 layout.horizontalSpacing = 6;
63                 layout.verticalSpacing = 6;
64                 layout.marginWidth = 0;
65                 layout.marginHeight = 0;
66                 layout.numColumns = 2;
67                 setLayout(layout);
68
69                 GridData data = new GridData(GridData.FILL_BOTH);
70                 setLayoutData(data);
71
72                 Label label = new Label(this, SWT.WRAP);
73                 label.setText(PHPHelpPlugin.getResource("%configurationsList"));
74                 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER));
75
76                 label = new Label(this, SWT.NONE);
77
78                 table = new Table(this, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
79                 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
80                 data.widthHint = 300;
81                 data.heightHint = 300;
82 //              WorkbenchHelp.setHelp(table, ContextIds.PREF_MONITORS);
83
84                 table.setLayoutData(data);
85                 table.setHeaderVisible(true);
86                 table.setLinesVisible(true);
87
88                 TableLayout tableLayout = new TableLayout();
89
90                 TableColumn statusColumn = new TableColumn(table, SWT.NONE);
91                 statusColumn.setText(PHPHelpPlugin.getResource("%columnName"));
92                 ColumnWeightData colData = new ColumnWeightData(5, 30, true);
93                 tableLayout.addColumnData(colData);
94
95                 TableColumn typeColumn = new TableColumn(table, SWT.NONE);
96                 typeColumn.setText(PHPHelpPlugin.getResource("%columnType"));
97                 colData = new ColumnWeightData(5, 30, true);
98                 tableLayout.addColumnData(colData);
99
100 //              TableColumn urlColumn = new TableColumn(table, SWT.NONE);
101 //              urlColumn.setText(PHPHelpPlugin.getResource("%columnUser"));
102 //              colData = new ColumnWeightData(5, 30, true);
103 //              tableLayout.addColumnData(colData);
104
105                 TableColumn localColumn = new TableColumn(table, SWT.NONE);
106                 localColumn.setText(PHPHelpPlugin.getResource("%columnURL"));
107                 colData = new ColumnWeightData(5, 150, true);
108                 tableLayout.addColumnData(colData);
109
110                 table.setLayout(tableLayout);
111
112                 tableViewer = new TableViewer(table);
113                 tableViewer.setContentProvider(new ConfigurationContentProvider());
114                 tableViewer.setLabelProvider(new ConfigurationTableLabelProvider());
115                 tableViewer.setInput("root");
116                 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
117                         public void selectionChanged(SelectionChangedEvent event) {
118                                 setSelection(event.getSelection());
119                         }
120                 });
121
122                 Composite buttonComp = new Composite(this, SWT.NONE);
123                 layout = new GridLayout();
124                 layout.horizontalSpacing = 0;
125                 layout.verticalSpacing = 8;
126                 layout.marginWidth = 0;
127                 layout.marginHeight = 0;
128                 layout.numColumns = 1;
129                 buttonComp.setLayout(layout);
130                 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_FILL);
131                 buttonComp.setLayoutData(data);
132
133                 Button add = SWTUtil.createButton(buttonComp, PHPHelpPlugin.getResource("%add"));
134                 add.addSelectionListener(new SelectionAdapter() {
135                         public void widgetSelected(SelectionEvent e) {
136                                 ConfigurationDialog dialog = new ConfigurationDialog(getShell());
137                                 if (dialog.open() == Window.CANCEL)
138                                         return;
139                                 tableViewer.refresh();
140
141                                 List list = PHPHelpPlugin.getConfigurations();
142                                 Object configuration = list.get(list.size() - 1);
143                                 tableViewer.setSelection(new StructuredSelection(configuration));
144                         }
145                 });
146
147                 edit = SWTUtil.createButton(buttonComp, PHPHelpPlugin.getResource("%edit"));
148                 edit.addSelectionListener(new SelectionAdapter() {
149                         public void widgetSelected(SelectionEvent e) {
150                                 IConfiguration monitor = (IConfiguration) getSelection().get(0);
151                                 IConfigurationWorkingCopy wc = monitor.getWorkingCopy();
152
153                                 ConfigurationDialog dialog = new ConfigurationDialog(getShell(), wc);
154                                 if (dialog.open() != Window.CANCEL) {
155                                         try {
156                                                 tableViewer.refresh(wc.save());
157                                         } catch (Exception ex) { }
158                                 }
159                         }
160                 });
161                 edit.setEnabled(false);
162
163                 remove = SWTUtil.createButton(buttonComp, PHPHelpPlugin.getResource("%remove"));
164                 remove.addSelectionListener(new SelectionAdapter() {
165                         public void widgetSelected(SelectionEvent e) {
166                                 Iterator iterator = getSelection().iterator();
167                                 while (iterator.hasNext()) {
168                                         IConfiguration monitor = (IConfiguration) iterator.next();
169                                         try {
170                                                 monitor.delete();
171                                         } catch (Exception ex) { }
172                                         tableViewer.remove(monitor);
173
174                                         List list = PHPHelpPlugin.getConfigurations();
175                                         Object monitor2 = list.get(list.size() - 1);
176                                         tableViewer.setSelection(new StructuredSelection(monitor2));
177                                 }
178                         }
179                 });
180                 remove.setEnabled(false);
181
182         }
183
184         protected List getSelection() {
185                 return selection2;
186         }
187
188         protected void setSelection(ISelection sel2) {
189                 IStructuredSelection sel = (IStructuredSelection) sel2;
190                 Iterator iterator = sel.iterator();
191                 selection2 = new ArrayList();
192
193                 while (iterator.hasNext()) {
194                         Object obj = iterator.next();
195                         if (obj instanceof IConfiguration)
196                                 selection2.add(obj);
197                 }
198
199                 if (!selection2.isEmpty()) {
200                         remove.setEnabled(true);
201
202                         edit.setEnabled(true);
203                 } else {
204                         edit.setEnabled(false);
205                         remove.setEnabled(false);
206                 }
207         }
208 }