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.httpquery.preferences;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import net.sourceforge.phpdt.httpquery.config.IConfiguration;
18 import net.sourceforge.phpdt.httpquery.config.IConfigurationWorkingCopy;
19 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
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;
44 public class ConfigurationComposite extends Composite {
45 protected Table table;
47 protected TableViewer tableViewer;
49 protected Button edit;
51 protected Button remove;
53 // protected Button start;
54 // protected Button stop;
56 protected List selection2;
58 public ConfigurationComposite(Composite parent, int style) {
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;
73 GridData data = new GridData(GridData.FILL_BOTH);
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));
81 label = new Label(this, SWT.NONE);
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);
88 data.heightHint = 300;
89 // WorkbenchHelp.setHelp(table, ContextIds.PREF_MONITORS);
91 table.setLayoutData(data);
92 table.setHeaderVisible(true);
93 table.setLinesVisible(true);
95 TableLayout tableLayout = new TableLayout();
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);
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);
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);
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);
117 table.setLayout(tableLayout);
119 tableViewer = new TableViewer(table);
120 tableViewer.setContentProvider(new ConfigurationContentProvider());
121 tableViewer.setLabelProvider(new ConfigurationTableLabelProvider());
122 tableViewer.setInput("root");
124 .addSelectionChangedListener(new ISelectionChangedListener() {
125 public void selectionChanged(SelectionChangedEvent event) {
126 setSelection(event.getSelection());
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);
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)
149 tableViewer.refresh();
151 List list = PHPHelpPlugin.getConfigurations();
152 Object configuration = list.get(list.size() - 1);
154 .setSelection(new StructuredSelection(configuration));
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();
165 ConfigurationDialog dialog = new ConfigurationDialog(
167 if (dialog.open() != Window.CANCEL) {
169 tableViewer.refresh(wc.save());
170 } catch (Exception ex) {
175 edit.setEnabled(false);
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();
186 } catch (Exception ex) {
188 tableViewer.remove(monitor);
190 List list = PHPHelpPlugin.getConfigurations();
191 Object monitor2 = list.get(list.size() - 1);
192 tableViewer.setSelection(new StructuredSelection(monitor2));
196 remove.setEnabled(false);
200 protected List getSelection() {
204 protected void setSelection(ISelection sel2) {
205 IStructuredSelection sel = (IStructuredSelection) sel2;
206 Iterator iterator = sel.iterator();
207 selection2 = new ArrayList();
209 while (iterator.hasNext()) {
210 Object obj = iterator.next();
211 if (obj instanceof IConfiguration)
215 if (!selection2.isEmpty()) {
216 remove.setEnabled(true);
218 edit.setEnabled(true);
220 edit.setEnabled(false);
221 remove.setEnabled(false);