Initial version from the webtools project; sligthly modified for phpeclipse
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / org / eclipse / 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 org.eclipse.webbrowser.internal;
12
13 import java.util.Iterator;
14
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;
32 /**
33  * 
34  */
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;
42         
43         protected Label location;
44         protected Label parameters;
45         
46         public BrowserTableComposite(Composite parent, int style) {
47                 super(parent, style);
48                 createWidgets();
49         }
50
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;
58                 setLayout(layout);
59
60                 GridData data = new GridData(GridData.FILL_BOTH);
61                 setLayoutData(data);
62                 
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);
68                 
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);
71                 data.widthHint = 300;
72                 table.setLayoutData(data);
73                 table.setHeaderVisible(false);
74                 table.setLinesVisible(false);
75                 
76                 TableLayout tableLayout = new TableLayout();
77                 new TableColumn(table, SWT.NONE);
78                 
79                 tableLayout.addColumnData(new ColumnWeightData(100));
80
81                 table.setLayout(tableLayout);
82                 
83                 tableViewer = new CheckboxTableViewer(table);
84                 
85                 tableViewer.setContentProvider(new BrowserContentProvider());
86                 tableViewer.setLabelProvider(new BrowserTableLabelProvider());
87         
88                 tableViewer.setInput("root");
89
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);
97                                 
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();
102                            if (obj.length == 0)
103                                 tableViewer.setChecked(e.getElement(), true);
104                         }
105                 });
106                 
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();
111                 if (browser != null)
112                         tableViewer.setChecked(browser, true);
113                 else {
114                         Object obj = tableViewer.getElementAt(0);
115                         if (obj != null)
116                                 tableViewer.setChecked(obj, true);
117                 }
118                 
119                 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
120                         public void selectionChanged(SelectionChangedEvent event) {
121                                 Object obj = getSelection(event.getSelection());
122                                 
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);
131                                 } else
132                                         selection = null;
133                                 
134                                 if (selection == null) {
135                                         edit.setEnabled(false);
136                                         remove.setEnabled(false);
137                                 }
138                         }
139                 });
140                 
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);
151                 
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)
157                                         return;
158                                 tableViewer.refresh();
159                         }
160                 });
161                 
162                 edit = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%edit"));
163                 edit.addSelectionListener(new SelectionAdapter() {
164                         public void widgetSelected(SelectionEvent e) {
165                                 IWebBrowser browser2 = getSelectedWebBrowser();
166
167                                 if (browser2 instanceof IInternalWebBrowser) {
168                                         IInternalWebBrowserWorkingCopy wc = ((IInternalWebBrowser) browser2).getWorkingCopy();
169                                         InternalBrowserDialog dialog = new InternalBrowserDialog(getShell(), wc);
170                                         if (dialog.open() != Window.CANCEL) {
171                                                 try {
172                                                         tableViewer.refresh(wc.save());
173                                                 } catch (Exception ex) { }
174                                         }
175                                 }                               
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) {
180                                                 try {
181                                                         tableViewer.refresh(wc.save());
182                                                 } catch (Exception ex) { }
183                                         }
184                                 }
185                    }                    
186                 });
187                 edit.setEnabled(false);
188
189                 remove = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%remove"));
190                 remove.addSelectionListener(new SelectionAdapter() {
191                         public void widgetSelected(SelectionEvent e) {
192                                 IWebBrowser browser2 = getSelectedWebBrowser();
193                                 try {
194                                         if (browser2 instanceof IInternalWebBrowser){
195                                                 remove.setEnabled(false);
196                                                 return; // nothing else possible to do
197                     } 
198                                         else if(browser2 instanceof IExternalWebBrowser) {
199                                                 remove.setEnabled(true);
200                        ((IExternalWebBrowser) browser2).delete();
201                        
202                                                 tableViewer.remove(browser2);
203                                                 
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);
209                                                                 if(obj != null){
210                                                                         BrowserManager.getInstance().setCurrentWebBrowser((InternalWebBrowser)obj);
211                                                                         tableViewer.setChecked(obj, true);
212                                                                 }
213                                                 }
214                                         }
215                                 } catch (Exception ex) { }
216                         }
217                 });
218                 remove.setEnabled(false);
219                 
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());
224                                 
225                                 if (browsersToCreate == null) // cancelled
226                                         return;
227                                 
228                                 if (browsersToCreate.isEmpty()) { // no browsers found
229                                         WebBrowserUtil.openMessage(WebBrowserUIPlugin.getResource("%searchingNoneFound"));
230                                         return;
231                                 }
232                                 
233                                 Iterator iterator = browsersToCreate.iterator();
234                                 while (iterator.hasNext()) {
235                                         IExternalWebBrowserWorkingCopy browser2 = (IExternalWebBrowserWorkingCopy) iterator.next();
236                                         browser2.save();
237                                 }
238                                 tableViewer.refresh();
239                         }
240                 });
241                 WorkbenchHelp.setHelp(search, ContextIds.PREF_BROWSER_EXTERNAL_SEARCH);
242                 
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);
248                         }
249                 });
250                 search.setEnabled(true);
251         }
252         
253         public IWebBrowser getSelectedWebBrowser() {
254                 return selection;
255         }
256         
257         protected Object getSelection(ISelection sel2) {
258                 IStructuredSelection sel = (IStructuredSelection) sel2;
259                 return sel.getFirstElement();
260         }
261         
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];
267                         
268                         if (!(item.getData().equals(browser))) 
269                                 item.setChecked(false);
270                 }
271         }
272 }