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 org.eclipse.webbrowser.internal;
13 import java.util.Iterator;
15 import org.eclipse.jface.viewers.*;
16 import org.eclipse.jface.window.Window;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.*;
23 import org.eclipse.ui.help.WorkbenchHelp;
24 import org.eclipse.webbrowser.IExternalWebBrowser;
25 import org.eclipse.webbrowser.IExternalWebBrowserWorkingCopy;
26 import org.eclipse.webbrowser.IInternalWebBrowser;
27 import org.eclipse.webbrowser.IInternalWebBrowserWorkingCopy;
28 import org.eclipse.webbrowser.IWebBrowser;
29 import org.eclipse.jface.viewers.CheckStateChangedEvent;
30 import org.eclipse.jface.viewers.CheckboxTableViewer;
31 import org.eclipse.jface.viewers.ICheckStateListener;
35 public class BrowserTableComposite extends Composite {
36 protected Table table;
37 protected CheckboxTableViewer tableViewer;
38 protected Button edit;
39 protected Button remove;
40 protected Button search;
41 protected IWebBrowser selection;
43 protected Label location;
44 protected Label parameters;
46 public BrowserTableComposite(Composite parent, int style) {
51 protected void createWidgets() {
52 GridLayout layout = new GridLayout();
53 layout.horizontalSpacing = 5;
54 layout.verticalSpacing = 5;
55 layout.marginWidth = 0;
56 layout.marginHeight = 0;
57 layout.numColumns = 2;
60 GridData data = new GridData(GridData.FILL_BOTH);
63 Label label = new Label(this, SWT.NONE);
64 label.setText(WebBrowserUIPlugin.getResource("%browserList"));
65 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
66 data.horizontalSpan = 2;
67 label.setLayoutData(data);
69 table = new Table(this, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
70 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
72 table.setLayoutData(data);
73 table.setHeaderVisible(false);
74 table.setLinesVisible(false);
76 TableLayout tableLayout = new TableLayout();
77 new TableColumn(table, SWT.NONE);
79 tableLayout.addColumnData(new ColumnWeightData(100));
81 table.setLayout(tableLayout);
83 tableViewer = new CheckboxTableViewer(table);
85 tableViewer.setContentProvider(new BrowserContentProvider());
86 tableViewer.setLabelProvider(new BrowserTableLabelProvider());
88 tableViewer.setInput("root");
90 // uncheck any other elements that might be checked and leave only the element checked to
91 // remain checked since one can only chose one brower at a time to be current.
92 tableViewer.addCheckStateListener(new ICheckStateListener() {
93 public void checkStateChanged(CheckStateChangedEvent e) {
94 checkNewDefaultBrowser(e.getElement());
95 IWebBrowser browser = (IWebBrowser) e.getElement();
96 BrowserManager.getInstance().setCurrentWebBrowser(browser);
98 // if no other browsers are checked, don't allow the single one currently
99 // checked to become unchecked, and lose a current browser. That is, don't
100 // permit unchecking if no other item is checked which is supposed to be the case.
101 Object[] obj = tableViewer.getCheckedElements();
103 tableViewer.setChecked(e.getElement(), true);
107 // set a default, checked browser based on the current browser. If there is not a
108 // current browser, but the first item exists, use that instead.
109 // This will work currently until workbench shutdown, because current browser is not yet persisted.
110 IWebBrowser browser = BrowserManager.getInstance().getCurrentWebBrowser();
112 tableViewer.setChecked(browser, true);
114 Object obj = tableViewer.getElementAt(0);
116 tableViewer.setChecked(obj, true);
119 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
120 public void selectionChanged(SelectionChangedEvent event) {
121 Object obj = getSelection(event.getSelection());
123 if (obj instanceof IInternalWebBrowser) {
124 selection = (IInternalWebBrowser) obj;
125 remove.setEnabled(false);
126 edit.setEnabled(true);
127 } else if (obj instanceof IExternalWebBrowser) {
128 selection = (IExternalWebBrowser) obj;
129 remove.setEnabled(true);
130 edit.setEnabled(true);
134 if (selection == null) {
135 edit.setEnabled(false);
136 remove.setEnabled(false);
141 Composite buttonComp = new Composite(this, SWT.NONE);
142 layout = new GridLayout();
143 layout.horizontalSpacing = 0;
144 layout.verticalSpacing = 5;
145 layout.marginWidth = 0;
146 layout.marginHeight = 0;
147 layout.numColumns = 1;
148 buttonComp.setLayout(layout);
149 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_FILL);
150 buttonComp.setLayoutData(data);
152 Button add = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%add"));
153 add.addSelectionListener(new SelectionAdapter() {
154 public void widgetSelected(SelectionEvent e) {
155 ExternalBrowserDialog dialog = new ExternalBrowserDialog(getShell());
156 if (dialog.open() == Window.CANCEL)
158 tableViewer.refresh();
162 edit = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%edit"));
163 edit.addSelectionListener(new SelectionAdapter() {
164 public void widgetSelected(SelectionEvent e) {
165 IWebBrowser browser2 = getSelectedWebBrowser();
167 if (browser2 instanceof IInternalWebBrowser) {
168 IInternalWebBrowserWorkingCopy wc = ((IInternalWebBrowser) browser2).getWorkingCopy();
169 InternalBrowserDialog dialog = new InternalBrowserDialog(getShell(), wc);
170 if (dialog.open() != Window.CANCEL) {
172 tableViewer.refresh(wc.save());
173 } catch (Exception ex) { }
176 else if(browser2 instanceof IExternalWebBrowser) {
177 IExternalWebBrowserWorkingCopy wc = ((IExternalWebBrowser) browser2).getWorkingCopy();
178 ExternalBrowserDialog dialog = new ExternalBrowserDialog(getShell(), wc);
179 if (dialog.open() != Window.CANCEL) {
181 tableViewer.refresh(wc.save());
182 } catch (Exception ex) { }
187 edit.setEnabled(false);
189 remove = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%remove"));
190 remove.addSelectionListener(new SelectionAdapter() {
191 public void widgetSelected(SelectionEvent e) {
192 IWebBrowser browser2 = getSelectedWebBrowser();
194 if (browser2 instanceof IInternalWebBrowser){
195 remove.setEnabled(false);
196 return; // nothing else possible to do
198 else if(browser2 instanceof IExternalWebBrowser) {
199 remove.setEnabled(true);
200 ((IExternalWebBrowser) browser2).delete();
202 tableViewer.remove(browser2);
204 // need here to ensure that if the item deleted was checked, ie, was
205 // the current browser, that the new current browser will be the first in the
206 // list, typically, the internal browser, which cannot be deleted, and be current.
207 if(((IExternalWebBrowser) browser2) == BrowserManager.getInstance().getCurrentWebBrowser()){
208 Object obj = tableViewer.getElementAt(0);
210 BrowserManager.getInstance().setCurrentWebBrowser((InternalWebBrowser)obj);
211 tableViewer.setChecked(obj, true);
215 } catch (Exception ex) { }
218 remove.setEnabled(false);
220 search = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%search"));
221 search.addSelectionListener(new SelectionAdapter() {
222 public void widgetSelected(SelectionEvent e) {
223 java.util.List browsersToCreate = BrowserSearcher.search(getShell());
225 if (browsersToCreate == null) // cancelled
228 if (browsersToCreate.isEmpty()) { // no browsers found
229 WebBrowserUtil.openMessage(WebBrowserUIPlugin.getResource("%searchingNoneFound"));
233 Iterator iterator = browsersToCreate.iterator();
234 while (iterator.hasNext()) {
235 IExternalWebBrowserWorkingCopy browser2 = (IExternalWebBrowserWorkingCopy) iterator.next();
238 tableViewer.refresh();
241 WorkbenchHelp.setHelp(search, ContextIds.PREF_BROWSER_EXTERNAL_SEARCH);
243 tableViewer.addCheckStateListener(new ICheckStateListener() {
244 public void checkStateChanged(CheckStateChangedEvent e) {
245 checkNewDefaultBrowser(e.getElement());
246 IWebBrowser browser2 = (IWebBrowser) e.getElement();
247 BrowserManager.getInstance().setCurrentWebBrowser(browser2);
250 search.setEnabled(true);
253 public IWebBrowser getSelectedWebBrowser() {
257 protected Object getSelection(ISelection sel2) {
258 IStructuredSelection sel = (IStructuredSelection) sel2;
259 return sel.getFirstElement();
262 // Uncheck all the items except the current one that was just checked
263 protected void checkNewDefaultBrowser(Object browser) {
264 TableItem[] children = tableViewer.getTable().getItems();
265 for (int i = 0; i < children.length; i++) {
266 TableItem item = children[i];
268 if (!(item.getData().equals(browser)))
269 item.setChecked(false);