Supress following exception
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / BrowserTableComposite.java
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
7  �*
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpeclipse.webbrowser.internal;
12
13 import java.util.Iterator;
14
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;
20
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;
43
44 /**
45  * 
46  */
47 public class BrowserTableComposite extends Composite {
48         protected Table table;
49
50         protected CheckboxTableViewer tableViewer;
51
52         protected Button edit;
53
54         protected Button remove;
55
56         protected Button search;
57
58         protected IWebBrowser selection;
59
60         protected Label location;
61
62         protected Label parameters;
63
64         public BrowserTableComposite(Composite parent, int style) {
65                 super(parent, style);
66                 createWidgets();
67         }
68
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;
76                 setLayout(layout);
77
78                 GridData data = new GridData(GridData.FILL_BOTH);
79                 setLayoutData(data);
80
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);
87
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);
92                 data.widthHint = 300;
93                 table.setLayoutData(data);
94                 table.setHeaderVisible(false);
95                 table.setLinesVisible(false);
96
97                 TableLayout tableLayout = new TableLayout();
98                 new TableColumn(table, SWT.NONE);
99
100                 tableLayout.addColumnData(new ColumnWeightData(100));
101
102                 table.setLayout(tableLayout);
103
104                 tableViewer = new CheckboxTableViewer(table);
105
106                 tableViewer.setContentProvider(new BrowserContentProvider());
107                 tableViewer.setLabelProvider(new BrowserTableLabelProvider());
108
109                 tableViewer.setInput("root");
110
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
114                 // current.
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);
120
121                                 // if no other browsers are checked, don't allow the single one
122                                 // currently
123                                 // checked to become unchecked, and lose a current browser. That
124                                 // is, don't
125                                 // permit unchecking if no other item is checked which is
126                                 // supposed to be the case.
127                                 Object[] obj = tableViewer.getCheckedElements();
128                                 if (obj.length == 0)
129                                         tableViewer.setChecked(e.getElement(), true);
130                         }
131                 });
132
133                 // set a default, checked browser based on the current browser. If there
134                 // is not a
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();
140                 if (browser != null)
141                         tableViewer.setChecked(browser, true);
142                 else {
143                         Object obj = tableViewer.getElementAt(0);
144                         if (obj != null)
145                                 tableViewer.setChecked(obj, true);
146                 }
147
148                 tableViewer
149                                 .addSelectionChangedListener(new ISelectionChangedListener() {
150                                         public void selectionChanged(SelectionChangedEvent event) {
151                                                 Object obj = getSelection(event.getSelection());
152
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);
161                                                 } else
162                                                         selection = null;
163
164                                                 if (selection == null) {
165                                                         edit.setEnabled(false);
166                                                         remove.setEnabled(false);
167                                                 }
168                                         }
169                                 });
170
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);
182
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(
188                                                 getShell());
189                                 if (dialog.open() == Window.CANCEL)
190                                         return;
191                                 tableViewer.refresh();
192                         }
193                 });
194
195                 edit = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin
196                                 .getResource("%edit"));
197                 edit.addSelectionListener(new SelectionAdapter() {
198                         public void widgetSelected(SelectionEvent e) {
199                                 IWebBrowser browser2 = getSelectedWebBrowser();
200
201                                 if (browser2 instanceof IInternalWebBrowser) {
202                                         IInternalWebBrowserWorkingCopy wc = ((IInternalWebBrowser) browser2)
203                                                         .getWorkingCopy();
204                                         InternalBrowserDialog dialog = new InternalBrowserDialog(
205                                                         getShell(), wc);
206                                         if (dialog.open() != Window.CANCEL) {
207                                                 try {
208                                                         tableViewer.refresh(wc.save());
209                                                 } catch (Exception ex) {
210                                                 }
211                                         }
212                                 } else if (browser2 instanceof IExternalWebBrowser) {
213                                         IExternalWebBrowserWorkingCopy wc = ((IExternalWebBrowser) browser2)
214                                                         .getWorkingCopy();
215                                         ExternalBrowserDialog dialog = new ExternalBrowserDialog(
216                                                         getShell(), wc);
217                                         if (dialog.open() != Window.CANCEL) {
218                                                 try {
219                                                         tableViewer.refresh(wc.save());
220                                                 } catch (Exception ex) {
221                                                 }
222                                         }
223                                 }
224                         }
225                 });
226                 edit.setEnabled(false);
227
228                 remove = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin
229                                 .getResource("%remove"));
230                 remove.addSelectionListener(new SelectionAdapter() {
231                         public void widgetSelected(SelectionEvent e) {
232                                 IWebBrowser browser2 = getSelectedWebBrowser();
233                                 try {
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();
240
241                                                 tableViewer.remove(browser2);
242
243                                                 // need here to ensure that if the item deleted was
244                                                 // checked, ie, 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);
252                                                         if (obj != null) {
253                                                                 BrowserManager.getInstance()
254                                                                                 .setCurrentWebBrowser(
255                                                                                                 (InternalWebBrowser) obj);
256                                                                 tableViewer.setChecked(obj, true);
257                                                         }
258                                                 }
259                                         }
260                                 } catch (Exception ex) {
261                                 }
262                         }
263                 });
264                 remove.setEnabled(false);
265
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
271                                                 .search(getShell());
272
273                                 if (browsersToCreate == null) // cancelled
274                                         return;
275
276                                 if (browsersToCreate.isEmpty()) { // no browsers found
277                                         WebBrowserUtil.openMessage(WebBrowserUIPlugin
278                                                         .getResource("%searchingNoneFound"));
279                                         return;
280                                 }
281
282                                 Iterator iterator = browsersToCreate.iterator();
283                                 while (iterator.hasNext()) {
284                                         IExternalWebBrowserWorkingCopy browser2 = (IExternalWebBrowserWorkingCopy) iterator
285                                                         .next();
286                                         browser2.save();
287                                 }
288                                 tableViewer.refresh();
289                         }
290                 });
291                 PlatformUI.getWorkbench().getHelpSystem().setHelp(search,
292                                 ContextIds.PREF_BROWSER_EXTERNAL_SEARCH);
293
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);
299                         }
300                 });
301                 search.setEnabled(true);
302         }
303
304         public IWebBrowser getSelectedWebBrowser() {
305                 return selection;
306         }
307
308         protected Object getSelection(ISelection sel2) {
309                 IStructuredSelection sel = (IStructuredSelection) sel2;
310                 return sel.getFirstElement();
311         }
312
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];
318
319                         if (!(item.getData().equals(browser)))
320                                 item.setChecked(false);
321                 }
322         }
323 }