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;
46 public class BrowserTableComposite extends Composite {
47 protected Table table;
48 protected CheckboxTableViewer tableViewer;
49 protected Button edit;
50 protected Button remove;
51 protected Button search;
52 protected IWebBrowser selection;
54 protected Label location;
55 protected Label parameters;
57 public BrowserTableComposite(Composite parent, int style) {
62 protected void createWidgets() {
63 GridLayout layout = new GridLayout();
64 layout.horizontalSpacing = 5;
65 layout.verticalSpacing = 5;
66 layout.marginWidth = 0;
67 layout.marginHeight = 0;
68 layout.numColumns = 2;
71 GridData data = new GridData(GridData.FILL_BOTH);
74 Label label = new Label(this, SWT.NONE);
75 label.setText(WebBrowserUIPlugin.getResource("%browserList"));
76 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
77 data.horizontalSpan = 2;
78 label.setLayoutData(data);
80 table = new Table(this, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
81 data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
83 table.setLayoutData(data);
84 table.setHeaderVisible(false);
85 table.setLinesVisible(false);
87 TableLayout tableLayout = new TableLayout();
88 new TableColumn(table, SWT.NONE);
90 tableLayout.addColumnData(new ColumnWeightData(100));
92 table.setLayout(tableLayout);
94 tableViewer = new CheckboxTableViewer(table);
96 tableViewer.setContentProvider(new BrowserContentProvider());
97 tableViewer.setLabelProvider(new BrowserTableLabelProvider());
99 tableViewer.setInput("root");
101 // uncheck any other elements that might be checked and leave only the element checked to
102 // remain checked since one can only chose one brower at a time to be current.
103 tableViewer.addCheckStateListener(new ICheckStateListener() {
104 public void checkStateChanged(CheckStateChangedEvent e) {
105 checkNewDefaultBrowser(e.getElement());
106 IWebBrowser browser = (IWebBrowser) e.getElement();
107 BrowserManager.getInstance().setCurrentWebBrowser(browser);
109 // if no other browsers are checked, don't allow the single one currently
110 // checked to become unchecked, and lose a current browser. That is, don't
111 // permit unchecking if no other item is checked which is supposed to be the case.
112 Object[] obj = tableViewer.getCheckedElements();
114 tableViewer.setChecked(e.getElement(), true);
118 // set a default, checked browser based on the current browser. If there is not a
119 // current browser, but the first item exists, use that instead.
120 // This will work currently until workbench shutdown, because current browser is not yet persisted.
121 IWebBrowser browser = BrowserManager.getInstance().getCurrentWebBrowser();
123 tableViewer.setChecked(browser, true);
125 Object obj = tableViewer.getElementAt(0);
127 tableViewer.setChecked(obj, true);
130 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
131 public void selectionChanged(SelectionChangedEvent event) {
132 Object obj = getSelection(event.getSelection());
134 if (obj instanceof IInternalWebBrowser) {
135 selection = (IInternalWebBrowser) obj;
136 remove.setEnabled(false);
137 edit.setEnabled(true);
138 } else if (obj instanceof IExternalWebBrowser) {
139 selection = (IExternalWebBrowser) obj;
140 remove.setEnabled(true);
141 edit.setEnabled(true);
145 if (selection == null) {
146 edit.setEnabled(false);
147 remove.setEnabled(false);
152 Composite buttonComp = new Composite(this, SWT.NONE);
153 layout = new GridLayout();
154 layout.horizontalSpacing = 0;
155 layout.verticalSpacing = 5;
156 layout.marginWidth = 0;
157 layout.marginHeight = 0;
158 layout.numColumns = 1;
159 buttonComp.setLayout(layout);
160 data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_FILL);
161 buttonComp.setLayoutData(data);
163 Button add = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%add"));
164 add.addSelectionListener(new SelectionAdapter() {
165 public void widgetSelected(SelectionEvent e) {
166 ExternalBrowserDialog dialog = new ExternalBrowserDialog(getShell());
167 if (dialog.open() == Window.CANCEL)
169 tableViewer.refresh();
173 edit = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%edit"));
174 edit.addSelectionListener(new SelectionAdapter() {
175 public void widgetSelected(SelectionEvent e) {
176 IWebBrowser browser2 = getSelectedWebBrowser();
178 if (browser2 instanceof IInternalWebBrowser) {
179 IInternalWebBrowserWorkingCopy wc = ((IInternalWebBrowser) browser2).getWorkingCopy();
180 InternalBrowserDialog dialog = new InternalBrowserDialog(getShell(), wc);
181 if (dialog.open() != Window.CANCEL) {
183 tableViewer.refresh(wc.save());
184 } catch (Exception ex) { }
187 else if(browser2 instanceof IExternalWebBrowser) {
188 IExternalWebBrowserWorkingCopy wc = ((IExternalWebBrowser) browser2).getWorkingCopy();
189 ExternalBrowserDialog dialog = new ExternalBrowserDialog(getShell(), wc);
190 if (dialog.open() != Window.CANCEL) {
192 tableViewer.refresh(wc.save());
193 } catch (Exception ex) { }
198 edit.setEnabled(false);
200 remove = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%remove"));
201 remove.addSelectionListener(new SelectionAdapter() {
202 public void widgetSelected(SelectionEvent e) {
203 IWebBrowser browser2 = getSelectedWebBrowser();
205 if (browser2 instanceof IInternalWebBrowser){
206 remove.setEnabled(false);
207 return; // nothing else possible to do
209 else if(browser2 instanceof IExternalWebBrowser) {
210 remove.setEnabled(true);
211 ((IExternalWebBrowser) browser2).delete();
213 tableViewer.remove(browser2);
215 // need here to ensure that if the item deleted was checked, ie, was
216 // the current browser, that the new current browser will be the first in the
217 // list, typically, the internal browser, which cannot be deleted, and be current.
218 if(((IExternalWebBrowser) browser2) == BrowserManager.getInstance().getCurrentWebBrowser()){
219 Object obj = tableViewer.getElementAt(0);
221 BrowserManager.getInstance().setCurrentWebBrowser((InternalWebBrowser)obj);
222 tableViewer.setChecked(obj, true);
226 } catch (Exception ex) { }
229 remove.setEnabled(false);
231 search = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%search"));
232 search.addSelectionListener(new SelectionAdapter() {
233 public void widgetSelected(SelectionEvent e) {
234 java.util.List browsersToCreate = BrowserSearcher.search(getShell());
236 if (browsersToCreate == null) // cancelled
239 if (browsersToCreate.isEmpty()) { // no browsers found
240 WebBrowserUtil.openMessage(WebBrowserUIPlugin.getResource("%searchingNoneFound"));
244 Iterator iterator = browsersToCreate.iterator();
245 while (iterator.hasNext()) {
246 IExternalWebBrowserWorkingCopy browser2 = (IExternalWebBrowserWorkingCopy) iterator.next();
249 tableViewer.refresh();
252 PlatformUI.getWorkbench().getHelpSystem().setHelp(search, ContextIds.PREF_BROWSER_EXTERNAL_SEARCH);
254 tableViewer.addCheckStateListener(new ICheckStateListener() {
255 public void checkStateChanged(CheckStateChangedEvent e) {
256 checkNewDefaultBrowser(e.getElement());
257 IWebBrowser browser2 = (IWebBrowser) e.getElement();
258 BrowserManager.getInstance().setCurrentWebBrowser(browser2);
261 search.setEnabled(true);
264 public IWebBrowser getSelectedWebBrowser() {
268 protected Object getSelection(ISelection sel2) {
269 IStructuredSelection sel = (IStructuredSelection) sel2;
270 return sel.getFirstElement();
273 // Uncheck all the items except the current one that was just checked
274 protected void checkNewDefaultBrowser(Object browser) {
275 TableItem[] children = tableViewer.getTable().getItems();
276 for (int i = 0; i < children.length; i++) {
277 TableItem item = children[i];
279 if (!(item.getData().equals(browser)))
280 item.setChecked(false);