All prefs for xampp administrated in external tools plugin.
[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.help.WorkbenchHelp;
43 /**
44  * 
45  */
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;
53         
54         protected Label location;
55         protected Label parameters;
56         
57         public BrowserTableComposite(Composite parent, int style) {
58                 super(parent, style);
59                 createWidgets();
60         }
61
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;
69                 setLayout(layout);
70
71                 GridData data = new GridData(GridData.FILL_BOTH);
72                 setLayoutData(data);
73                 
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);
79                 
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);
82                 data.widthHint = 300;
83                 table.setLayoutData(data);
84                 table.setHeaderVisible(false);
85                 table.setLinesVisible(false);
86                 
87                 TableLayout tableLayout = new TableLayout();
88                 new TableColumn(table, SWT.NONE);
89                 
90                 tableLayout.addColumnData(new ColumnWeightData(100));
91
92                 table.setLayout(tableLayout);
93                 
94                 tableViewer = new CheckboxTableViewer(table);
95                 
96                 tableViewer.setContentProvider(new BrowserContentProvider());
97                 tableViewer.setLabelProvider(new BrowserTableLabelProvider());
98         
99                 tableViewer.setInput("root");
100
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);
108                                 
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();
113                            if (obj.length == 0)
114                                 tableViewer.setChecked(e.getElement(), true);
115                         }
116                 });
117                 
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();
122                 if (browser != null)
123                         tableViewer.setChecked(browser, true);
124                 else {
125                         Object obj = tableViewer.getElementAt(0);
126                         if (obj != null)
127                                 tableViewer.setChecked(obj, true);
128                 }
129                 
130                 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
131                         public void selectionChanged(SelectionChangedEvent event) {
132                                 Object obj = getSelection(event.getSelection());
133                                 
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);
142                                 } else
143                                         selection = null;
144                                 
145                                 if (selection == null) {
146                                         edit.setEnabled(false);
147                                         remove.setEnabled(false);
148                                 }
149                         }
150                 });
151                 
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);
162                 
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)
168                                         return;
169                                 tableViewer.refresh();
170                         }
171                 });
172                 
173                 edit = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%edit"));
174                 edit.addSelectionListener(new SelectionAdapter() {
175                         public void widgetSelected(SelectionEvent e) {
176                                 IWebBrowser browser2 = getSelectedWebBrowser();
177
178                                 if (browser2 instanceof IInternalWebBrowser) {
179                                         IInternalWebBrowserWorkingCopy wc = ((IInternalWebBrowser) browser2).getWorkingCopy();
180                                         InternalBrowserDialog dialog = new InternalBrowserDialog(getShell(), wc);
181                                         if (dialog.open() != Window.CANCEL) {
182                                                 try {
183                                                         tableViewer.refresh(wc.save());
184                                                 } catch (Exception ex) { }
185                                         }
186                                 }                               
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) {
191                                                 try {
192                                                         tableViewer.refresh(wc.save());
193                                                 } catch (Exception ex) { }
194                                         }
195                                 }
196                    }                    
197                 });
198                 edit.setEnabled(false);
199
200                 remove = SWTUtil.createButton(buttonComp, WebBrowserUIPlugin.getResource("%remove"));
201                 remove.addSelectionListener(new SelectionAdapter() {
202                         public void widgetSelected(SelectionEvent e) {
203                                 IWebBrowser browser2 = getSelectedWebBrowser();
204                                 try {
205                                         if (browser2 instanceof IInternalWebBrowser){
206                                                 remove.setEnabled(false);
207                                                 return; // nothing else possible to do
208                     } 
209                                         else if(browser2 instanceof IExternalWebBrowser) {
210                                                 remove.setEnabled(true);
211                        ((IExternalWebBrowser) browser2).delete();
212                        
213                                                 tableViewer.remove(browser2);
214                                                 
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);
220                                                                 if(obj != null){
221                                                                         BrowserManager.getInstance().setCurrentWebBrowser((InternalWebBrowser)obj);
222                                                                         tableViewer.setChecked(obj, true);
223                                                                 }
224                                                 }
225                                         }
226                                 } catch (Exception ex) { }
227                         }
228                 });
229                 remove.setEnabled(false);
230                 
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());
235                                 
236                                 if (browsersToCreate == null) // cancelled
237                                         return;
238                                 
239                                 if (browsersToCreate.isEmpty()) { // no browsers found
240                                         WebBrowserUtil.openMessage(WebBrowserUIPlugin.getResource("%searchingNoneFound"));
241                                         return;
242                                 }
243                                 
244                                 Iterator iterator = browsersToCreate.iterator();
245                                 while (iterator.hasNext()) {
246                                         IExternalWebBrowserWorkingCopy browser2 = (IExternalWebBrowserWorkingCopy) iterator.next();
247                                         browser2.save();
248                                 }
249                                 tableViewer.refresh();
250                         }
251                 });
252                 WorkbenchHelp.setHelp(search, ContextIds.PREF_BROWSER_EXTERNAL_SEARCH);
253                 
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);
259                         }
260                 });
261                 search.setEnabled(true);
262         }
263         
264         public IWebBrowser getSelectedWebBrowser() {
265                 return selection;
266         }
267         
268         protected Object getSelection(ISelection sel2) {
269                 IStructuredSelection sel = (IStructuredSelection) sel2;
270                 return sel.getFirstElement();
271         }
272         
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];
278                         
279                         if (!(item.getData().equals(browser))) 
280                                 item.setChecked(false);
281                 }
282         }
283 }