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