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.*;
22 import org.eclipse.jface.window.Window;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.*;
29 import org.eclipse.ui.help.WorkbenchHelp;
30 import org.eclipse.jface.viewers.CheckStateChangedEvent;
31 import org.eclipse.jface.viewers.CheckboxTableViewer;
32 import org.eclipse.jface.viewers.ICheckStateListener;
36 public class BrowserTableComposite extends Composite {
37 protected Table table;
38 protected CheckboxTableViewer tableViewer;
39 protected Button edit;
40 protected Button remove;
41 protected Button search;
42 protected IWebBrowser selection;
44 protected Label location;
45 protected Label parameters;
47 public BrowserTableComposite(Composite parent, int style) {
52 protected void createWidgets() {
53 GridLayout layout = new GridLayout();
54 layout.horizontalSpacing = 5;
55 layout.verticalSpacing = 5;
56 layout.marginWidth = 0;
57 layout.marginHeight = 0;
58 layout.numColumns = 2;
61 GridData data = new GridData(GridData.FILL_BOTH);
64 Label label = new Label(this, SWT.NONE);
65 label.setText(WebBrowserUIPlugin.getResource("%browserList"));
66 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
67 data.horizontalSpan = 2;
68 label.setLayoutData(data);
70 table = new Table(this, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
71 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
73 table.setLayoutData(data);
74 table.setHeaderVisible(false);
75 table.setLinesVisible(false);
77 TableLayout tableLayout = new TableLayout();
78 new TableColumn(table, SWT.NONE);
80 tableLayout.addColumnData(new ColumnWeightData(100));
82 table.setLayout(tableLayout);
84 tableViewer = new CheckboxTableViewer(table);
86 tableViewer.setContentProvider(new BrowserContentProvider());
87 tableViewer.setLabelProvider(new BrowserTableLabelProvider());
89 tableViewer.setInput("root");
91 // uncheck any other elements that might be checked and leave only the element checked to
92 // remain checked since one can only chose one brower at a time to be current.
93 tableViewer.addCheckStateListener(new ICheckStateListener() {
94 public void checkStateChanged(CheckStateChangedEvent e) {
95 checkNewDefaultBrowser(e.getElement());
96 IWebBrowser browser = (IWebBrowser) e.getElement();
97 BrowserManager.getInstance().setCurrentWebBrowser(browser);
99 // if no other browsers are checked, don't allow the single one currently
100 // checked to become unchecked, and lose a current browser. That is, don't
101 // permit unchecking if no other item is checked which is supposed to be the case.
102 Object[] obj = tableViewer.getCheckedElements();
104 tableViewer.setChecked(e.getElement(), true);
108 // set a default, checked browser based on the current browser. If there is not a
109 // current browser, but the first item exists, use that instead.
110 // This will work currently until workbench shutdown, because current browser is not yet persisted.
111 IWebBrowser browser = BrowserManager.getInstance().getCurrentWebBrowser();
113 tableViewer.setChecked(browser, true);
115 Object obj = tableViewer.getElementAt(0);
117 tableViewer.setChecked(obj, true);
120 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
121 public void selectionChanged(SelectionChangedEvent event) {
122 Object obj = getSelection(event.getSelection());
124 if (obj instanceof IInternalWebBrowser) {
125 selection = (IInternalWebBrowser) obj;
126 remove.setEnabled(false);
127 edit.setEnabled(true);
128 } else if (obj instanceof IExternalWebBrowser) {
129 selection = (IExternalWebBrowser) obj;
130 remove.setEnabled(true);
131 edit.setEnabled(true);
135 if (selection == null) {
136 edit.setEnabled(false);
137 remove.setEnabled(false);
142 Composite buttonComp = new Composite(this, SWT.NONE);
143 layout = new GridLayout();
144 layout.horizontalSpacing = 0;
145 layout.verticalSpacing = 5;
146 layout.marginWidth = 0;
147 layout.marginHeight = 0;
148 layout.numColumns = 1;
149 buttonComp.setLayout(layout);
150 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_FILL);
151 buttonComp.setLayoutData(data);
153 Button add = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%add"));
154 add.addSelectionListener(new SelectionAdapter() {
155 public void widgetSelected(SelectionEvent e) {
156 ExternalBrowserDialog dialog = new ExternalBrowserDialog(getShell());
157 if (dialog.open() == Window.CANCEL)
159 tableViewer.refresh();
163 edit = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%edit"));
164 edit.addSelectionListener(new SelectionAdapter() {
165 public void widgetSelected(SelectionEvent e) {
166 IWebBrowser browser2 = getSelectedWebBrowser();
168 if (browser2 instanceof IInternalWebBrowser) {
169 IInternalWebBrowserWorkingCopy wc = ((IInternalWebBrowser) browser2).getWorkingCopy();
170 InternalBrowserDialog dialog = new InternalBrowserDialog(getShell(), wc);
171 if (dialog.open() != Window.CANCEL) {
173 tableViewer.refresh(wc.save());
174 } catch (Exception ex) { }
177 else if(browser2 instanceof IExternalWebBrowser) {
178 IExternalWebBrowserWorkingCopy wc = ((IExternalWebBrowser) browser2).getWorkingCopy();
179 ExternalBrowserDialog dialog = new ExternalBrowserDialog(getShell(), wc);
180 if (dialog.open() != Window.CANCEL) {
182 tableViewer.refresh(wc.save());
183 } catch (Exception ex) { }
188 edit.setEnabled(false);
190 remove = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%remove"));
191 remove.addSelectionListener(new SelectionAdapter() {
192 public void widgetSelected(SelectionEvent e) {
193 IWebBrowser browser2 = getSelectedWebBrowser();
195 if (browser2 instanceof IInternalWebBrowser){
196 remove.setEnabled(false);
197 return; // nothing else possible to do
199 else if(browser2 instanceof IExternalWebBrowser) {
200 remove.setEnabled(true);
201 ((IExternalWebBrowser) browser2).delete();
203 tableViewer.remove(browser2);
205 // need here to ensure that if the item deleted was checked, ie, was
206 // the current browser, that the new current browser will be the first in the
207 // list, typically, the internal browser, which cannot be deleted, and be current.
208 if(((IExternalWebBrowser) browser2) == BrowserManager.getInstance().getCurrentWebBrowser()){
209 Object obj = tableViewer.getElementAt(0);
211 BrowserManager.getInstance().setCurrentWebBrowser((InternalWebBrowser)obj);
212 tableViewer.setChecked(obj, true);
216 } catch (Exception ex) { }
219 remove.setEnabled(false);
221 search = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%search"));
222 search.addSelectionListener(new SelectionAdapter() {
223 public void widgetSelected(SelectionEvent e) {
224 java.util.List browsersToCreate = BrowserSearcher.search(getShell());
226 if (browsersToCreate == null) // cancelled
229 if (browsersToCreate.isEmpty()) { // no browsers found
230 WebBrowserUtil.openMessage(WebBrowserUIPlugin.getResource("%searchingNoneFound"));
234 Iterator iterator = browsersToCreate.iterator();
235 while (iterator.hasNext()) {
236 IExternalWebBrowserWorkingCopy browser2 = (IExternalWebBrowserWorkingCopy) iterator.next();
239 tableViewer.refresh();
242 WorkbenchHelp.setHelp(search, ContextIds.PREF_BROWSER_EXTERNAL_SEARCH);
244 tableViewer.addCheckStateListener(new ICheckStateListener() {
245 public void checkStateChanged(CheckStateChangedEvent e) {
246 checkNewDefaultBrowser(e.getElement());
247 IWebBrowser browser2 = (IWebBrowser) e.getElement();
248 BrowserManager.getInstance().setCurrentWebBrowser(browser2);
251 search.setEnabled(true);
254 public IWebBrowser getSelectedWebBrowser() {
258 protected Object getSelection(ISelection sel2) {
259 IStructuredSelection sel = (IStructuredSelection) sel2;
260 return sel.getFirstElement();
263 // Uncheck all the items except the current one that was just checked
264 protected void checkNewDefaultBrowser(Object browser) {
265 TableItem[] children = tableViewer.getTable().getItems();
266 for (int i = 0; i < children.length; i++) {
267 TableItem item = children[i];
269 if (!(item.getData().equals(browser)))
270 item.setChecked(false);