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.phpeclipse.webbrowser.internal;
13 import java.util.Iterator;
15 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser;
16 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowserWorkingCopy;
17 import net.sourceforge.phpeclipse.webbrowser.IInternalWebBrowser;
18 import net.sourceforge.phpeclipse.webbrowser.IInternalWebBrowserWorkingCopy;
19 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
21 import org.eclipse.jface.viewers.CheckStateChangedEvent;
22 import org.eclipse.jface.viewers.CheckboxTableViewer;
23 import org.eclipse.jface.viewers.ColumnWeightData;
24 import org.eclipse.jface.viewers.ICheckStateListener;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionChangedListener;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.SelectionChangedEvent;
29 import org.eclipse.jface.viewers.TableLayout;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Table;
40 import org.eclipse.swt.widgets.TableColumn;
41 import org.eclipse.swt.widgets.TableItem;
42 import org.eclipse.ui.PlatformUI;
47 public class BrowserTableComposite extends Composite {
48 protected Table table;
50 protected CheckboxTableViewer tableViewer;
52 protected Button edit;
54 protected Button remove;
56 protected Button search;
58 protected IWebBrowser selection;
60 protected Label location;
62 protected Label parameters;
64 public BrowserTableComposite(Composite parent, int style) {
69 protected void createWidgets() {
70 GridLayout layout = new GridLayout();
71 layout.horizontalSpacing = 5;
72 layout.verticalSpacing = 5;
73 layout.marginWidth = 0;
74 layout.marginHeight = 0;
75 layout.numColumns = 2;
78 GridData data = new GridData(GridData.FILL_BOTH);
81 Label label = new Label(this, SWT.NONE);
82 label.setText(WebBrowserUIPlugin.getResource("%browserList"));
83 data = new GridData(GridData.FILL_HORIZONTAL
84 | GridData.VERTICAL_ALIGN_CENTER);
85 data.horizontalSpan = 2;
86 label.setLayoutData(data);
88 table = new Table(this, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL
89 | SWT.H_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
90 data = new GridData(GridData.FILL_HORIZONTAL
91 | GridData.VERTICAL_ALIGN_FILL);
93 table.setLayoutData(data);
94 table.setHeaderVisible(false);
95 table.setLinesVisible(false);
97 TableLayout tableLayout = new TableLayout();
98 new TableColumn(table, SWT.NONE);
100 tableLayout.addColumnData(new ColumnWeightData(100));
102 table.setLayout(tableLayout);
104 tableViewer = new CheckboxTableViewer(table);
106 tableViewer.setContentProvider(new BrowserContentProvider());
107 tableViewer.setLabelProvider(new BrowserTableLabelProvider());
109 tableViewer.setInput("root");
111 // uncheck any other elements that might be checked and leave only the
112 // element checked to
113 // remain checked since one can only chose one brower at a time to be
115 tableViewer.addCheckStateListener(new ICheckStateListener() {
116 public void checkStateChanged(CheckStateChangedEvent e) {
117 checkNewDefaultBrowser(e.getElement());
118 IWebBrowser browser = (IWebBrowser) e.getElement();
119 BrowserManager.getInstance().setCurrentWebBrowser(browser);
121 // if no other browsers are checked, don't allow the single one
123 // checked to become unchecked, and lose a current browser. That
125 // permit unchecking if no other item is checked which is
126 // supposed to be the case.
127 Object[] obj = tableViewer.getCheckedElements();
129 tableViewer.setChecked(e.getElement(), true);
133 // set a default, checked browser based on the current browser. If there
135 // current browser, but the first item exists, use that instead.
136 // This will work currently until workbench shutdown, because current
137 // browser is not yet persisted.
138 IWebBrowser browser = BrowserManager.getInstance()
139 .getCurrentWebBrowser();
141 tableViewer.setChecked(browser, true);
143 Object obj = tableViewer.getElementAt(0);
145 tableViewer.setChecked(obj, true);
149 .addSelectionChangedListener(new ISelectionChangedListener() {
150 public void selectionChanged(SelectionChangedEvent event) {
151 Object obj = getSelection(event.getSelection());
153 if (obj instanceof IInternalWebBrowser) {
154 selection = (IInternalWebBrowser) obj;
155 remove.setEnabled(false);
156 edit.setEnabled(true);
157 } else if (obj instanceof IExternalWebBrowser) {
158 selection = (IExternalWebBrowser) obj;
159 remove.setEnabled(true);
160 edit.setEnabled(true);
164 if (selection == null) {
165 edit.setEnabled(false);
166 remove.setEnabled(false);
171 Composite buttonComp = new Composite(this, SWT.NONE);
172 layout = new GridLayout();
173 layout.horizontalSpacing = 0;
174 layout.verticalSpacing = 5;
175 layout.marginWidth = 0;
176 layout.marginHeight = 0;
177 layout.numColumns = 1;
178 buttonComp.setLayout(layout);
179 data = new GridData(GridData.HORIZONTAL_ALIGN_END
180 | GridData.VERTICAL_ALIGN_FILL);
181 buttonComp.setLayoutData(data);
183 Button add = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin
184 .getResource("%add"));
185 add.addSelectionListener(new SelectionAdapter() {
186 public void widgetSelected(SelectionEvent e) {
187 ExternalBrowserDialog dialog = new ExternalBrowserDialog(
189 if (dialog.open() == Window.CANCEL)
191 tableViewer.refresh();
195 edit = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin
196 .getResource("%edit"));
197 edit.addSelectionListener(new SelectionAdapter() {
198 public void widgetSelected(SelectionEvent e) {
199 IWebBrowser browser2 = getSelectedWebBrowser();
201 if (browser2 instanceof IInternalWebBrowser) {
202 IInternalWebBrowserWorkingCopy wc = ((IInternalWebBrowser) browser2)
204 InternalBrowserDialog dialog = new InternalBrowserDialog(
206 if (dialog.open() != Window.CANCEL) {
208 tableViewer.refresh(wc.save());
209 } catch (Exception ex) {
212 } else if (browser2 instanceof IExternalWebBrowser) {
213 IExternalWebBrowserWorkingCopy wc = ((IExternalWebBrowser) browser2)
215 ExternalBrowserDialog dialog = new ExternalBrowserDialog(
217 if (dialog.open() != Window.CANCEL) {
219 tableViewer.refresh(wc.save());
220 } catch (Exception ex) {
226 edit.setEnabled(false);
228 remove = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin
229 .getResource("%remove"));
230 remove.addSelectionListener(new SelectionAdapter() {
231 public void widgetSelected(SelectionEvent e) {
232 IWebBrowser browser2 = getSelectedWebBrowser();
234 if (browser2 instanceof IInternalWebBrowser) {
235 remove.setEnabled(false);
236 return; // nothing else possible to do
237 } else if (browser2 instanceof IExternalWebBrowser) {
238 remove.setEnabled(true);
239 ((IExternalWebBrowser) browser2).delete();
241 tableViewer.remove(browser2);
243 // need here to ensure that if the item deleted was
245 // the current browser, that the new current browser
246 // will be the first in the
247 // list, typically, the internal browser, which cannot
248 // be deleted, and be current.
249 if (((IExternalWebBrowser) browser2) == BrowserManager
250 .getInstance().getCurrentWebBrowser()) {
251 Object obj = tableViewer.getElementAt(0);
253 BrowserManager.getInstance()
254 .setCurrentWebBrowser(
255 (InternalWebBrowser) obj);
256 tableViewer.setChecked(obj, true);
260 } catch (Exception ex) {
264 remove.setEnabled(false);
266 search = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin
267 .getResource("%search"));
268 search.addSelectionListener(new SelectionAdapter() {
269 public void widgetSelected(SelectionEvent e) {
270 java.util.List browsersToCreate = BrowserSearcher
273 if (browsersToCreate == null) // cancelled
276 if (browsersToCreate.isEmpty()) { // no browsers found
277 WebBrowserUtil.openMessage(WebBrowserUIPlugin
278 .getResource("%searchingNoneFound"));
282 Iterator iterator = browsersToCreate.iterator();
283 while (iterator.hasNext()) {
284 IExternalWebBrowserWorkingCopy browser2 = (IExternalWebBrowserWorkingCopy) iterator
288 tableViewer.refresh();
291 PlatformUI.getWorkbench().getHelpSystem().setHelp(search,
292 ContextIds.PREF_BROWSER_EXTERNAL_SEARCH);
294 tableViewer.addCheckStateListener(new ICheckStateListener() {
295 public void checkStateChanged(CheckStateChangedEvent e) {
296 checkNewDefaultBrowser(e.getElement());
297 IWebBrowser browser2 = (IWebBrowser) e.getElement();
298 BrowserManager.getInstance().setCurrentWebBrowser(browser2);
301 search.setEnabled(true);
304 public IWebBrowser getSelectedWebBrowser() {
308 protected Object getSelection(ISelection sel2) {
309 IStructuredSelection sel = (IStructuredSelection) sel2;
310 return sel.getFirstElement();
313 // Uncheck all the items except the current one that was just checked
314 protected void checkNewDefaultBrowser(Object browser) {
315 TableItem[] children = tableViewer.getTable().getItems();
316 for (int i = 0; i < children.length; i++) {
317 TableItem item = children[i];
319 if (!(item.getData().equals(browser)))
320 item.setChecked(false);