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;
43 public class ConfigurationComposite extends Composite {
44 protected Table table;
45 protected TableViewer tableViewer;
47 protected Button edit;
48 protected Button remove;
49 // protected Button start;
50 // protected Button stop;
52 protected List selection2;
54 public ConfigurationComposite(Composite parent, int style) {
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;
69 GridData data = new GridData(GridData.FILL_BOTH);
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));
76 label = new Label(this, SWT.NONE);
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);
81 data.heightHint = 300;
82 // WorkbenchHelp.setHelp(table, ContextIds.PREF_MONITORS);
84 table.setLayoutData(data);
85 table.setHeaderVisible(true);
86 table.setLinesVisible(true);
88 TableLayout tableLayout = new TableLayout();
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);
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);
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);
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);
110 table.setLayout(tableLayout);
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());
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);
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)
139 tableViewer.refresh();
141 List list = PHPHelpPlugin.getConfigurations();
142 Object configuration = list.get(list.size() - 1);
143 tableViewer.setSelection(new StructuredSelection(configuration));
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();
153 ConfigurationDialog dialog = new ConfigurationDialog(getShell(), wc);
154 if (dialog.open() != Window.CANCEL) {
156 tableViewer.refresh(wc.save());
157 } catch (Exception ex) { }
161 edit.setEnabled(false);
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();
171 } catch (Exception ex) { }
172 tableViewer.remove(monitor);
174 List list = PHPHelpPlugin.getConfigurations();
175 Object monitor2 = list.get(list.size() - 1);
176 tableViewer.setSelection(new StructuredSelection(monitor2));
180 remove.setEnabled(false);
184 protected List getSelection() {
188 protected void setSelection(ISelection sel2) {
189 IStructuredSelection sel = (IStructuredSelection) sel2;
190 Iterator iterator = sel.iterator();
191 selection2 = new ArrayList();
193 while (iterator.hasNext()) {
194 Object obj = iterator.next();
195 if (obj instanceof IConfiguration)
199 if (!selection2.isEmpty()) {
200 remove.setEnabled(true);
202 edit.setEnabled(true);
204 edit.setEnabled(false);
205 remove.setEnabled(false);