#1365431 (toshihiro) - PHP Editor crashes Windows with JavaScript 'windows.close()'
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / ExternalBrowserDialog.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.io.File;
14
15 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowserWorkingCopy;
16
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.ModifyEvent;
21 import org.eclipse.swt.events.ModifyListener;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.FileDialog;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.PlatformUI;
34
35 /**
36  * 
37  */
38 public class ExternalBrowserDialog extends Dialog {
39         protected IExternalWebBrowserWorkingCopy browser;
40
41         protected boolean isEdit;
42
43         protected Button newPageCheckbox;
44
45         protected Button clearHistoryCheckbox;
46
47         protected Button browseButton;
48
49         protected Text browserNameTextfield;
50
51         protected Text browserLocationTextfield;
52
53         protected Text browserParametersTextfield;
54
55         private Button okButton;
56
57         interface StringModifyListener {
58                 public void valueChanged(String s);
59         }
60
61         /**
62          * @param parentShell
63          */
64         public ExternalBrowserDialog(Shell parentShell,
65                         IExternalWebBrowserWorkingCopy browser) {
66                 super(parentShell);
67                 this.browser = browser;
68                 isEdit = true;
69         }
70
71         public ExternalBrowserDialog(Shell parentShell) {
72                 super(parentShell);
73                 browser = BrowserManager.getInstance().createExternalWebBrowser();
74                 isEdit = false;
75         }
76
77         protected void configureShell(Shell shell) {
78                 super.configureShell(shell);
79
80                 if (isEdit)
81                         shell.setText(WebBrowserUIPlugin
82                                         .getResource("%editExternalBrowser"));
83                 else
84                         shell.setText(WebBrowserUIPlugin.getResource("%createBrowser"));
85         }
86
87         protected Text createText(Composite comp, String txt,
88                         final StringModifyListener listener) {
89                 final Text text = new Text(comp, SWT.BORDER);
90                 if (txt != null)
91                         text.setText(txt);
92                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
93                                 | GridData.VERTICAL_ALIGN_BEGINNING);
94                 data.widthHint = 250;
95                 text.setLayoutData(data);
96                 if (listener != null)
97                         text.addModifyListener(new ModifyListener() {
98                                 public void modifyText(ModifyEvent e) {
99                                         listener.valueChanged(text.getText());
100                                 }
101                         });
102                 return text;
103         }
104
105         /*
106          * (non-Javadoc)
107          * 
108          * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
109          */
110         protected Control createDialogArea(Composite parent) {
111                 Composite composite = (Composite) super.createDialogArea(parent);
112                 ((GridLayout) composite.getLayout()).numColumns = 3;
113
114                 if (isEdit)
115                         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
116                                         ContextIds.PREF_BROWSER_EXTERNAL_EDIT);
117                 else
118                         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
119                                         ContextIds.PREF_BROWSER_EXTERNAL_ADD);
120
121                 SWTUtil.createLabel(composite, WebBrowserUIPlugin.getResource("%name"));
122                 browserNameTextfield = createText(composite, browser.getName(),
123                                 new StringModifyListener() {
124                                         public void valueChanged(String s) {
125                                                 browser.setName(s);
126                                                 validateFields();
127                                         }
128                                 });
129
130                 new Label(composite, SWT.NONE);
131
132                 SWTUtil.createLabel(composite, WebBrowserUIPlugin
133                                 .getResource("%location"));
134                 browserLocationTextfield = createText(composite, browser.getLocation(),
135                                 new StringModifyListener() {
136                                         public void valueChanged(String s) {
137                                                 browser.setLocation(s);
138                                                 validateFields();
139                                         }
140                                 });
141
142                 browseButton = SWTUtil.createButton(composite, WebBrowserUIPlugin
143                                 .getResource("%browse"));
144                 browseButton.addSelectionListener(new SelectionAdapter() {
145                         public void widgetSelected(SelectionEvent e) {
146                                 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
147                                 dialog
148                                                 .setText(WebBrowserUIPlugin
149                                                                 .getResource("%browseMessage"));
150
151                                 String fname = browserLocationTextfield.getText();
152
153                                 dialog.setFileName(fname);
154                                 fname = dialog.open();
155
156                                 if (fname != null)
157                                         browserLocationTextfield.setText(fname);
158                         }
159                 });
160
161                 SWTUtil.createLabel(composite, WebBrowserUIPlugin
162                                 .getResource("%parameters"));
163                 browserParametersTextfield = createText(composite, browser
164                                 .getParameters(), new StringModifyListener() {
165                         public void valueChanged(String s) {
166                                 browser.setParameters(s);
167                         }
168                 });
169
170                 new Label(composite, SWT.NONE);
171
172                 new Label(composite, SWT.NONE);
173                 Label urlLabel = new Label(composite, SWT.NONE);
174                 urlLabel.setText(WebBrowserUIPlugin.getResource("%parametersMessage",
175                                 WebBrowserPreference.URL_PARAMETER));
176
177                 return composite;
178         }
179
180         /*
181          * (non-Javadoc)
182          * 
183          * @see org.eclipse.jface.dialogs.Dialog#okPressed()
184          */
185         protected void okPressed() {
186                 // do simple field validation to at least ensure target directory
187                 // entered is valid pathname
188                 try {
189                         File file = new File(browser.getLocation());
190                         if (!file.isFile()) {
191                                 WebBrowserUtil.openError(WebBrowserUIPlugin
192                                                 .getResource("%locationInvalid"));
193                                 return;
194                         }
195                 } catch (Exception e) {
196                         WebBrowserUtil.openError(WebBrowserUIPlugin
197                                         .getResource("%locationInvalid"));
198                         return;
199                 }
200
201                 browser.save();
202                 super.okPressed();
203         }
204
205         private void setOKButtonEnabled(boolean curIsEnabled) {
206                 if (okButton == null)
207                         okButton = getButton(IDialogConstants.OK_ID);
208
209                 if (okButton != null)
210                         okButton.setEnabled(curIsEnabled);
211         }
212
213         protected Control createButtonBar(Composite parent) {
214                 Control buttonControl = super.createButtonBar(parent);
215                 validateFields();
216                 return buttonControl;
217         }
218
219         protected void validateFields() {
220                 boolean valid = true;
221
222                 String name = browserNameTextfield.getText();
223                 if (name == null || name.trim().length() < 1)
224                         valid = false;
225
226                 String location = browserLocationTextfield.getText();
227                 if (location == null || location.trim().length() < 1)
228                         valid = false;
229
230                 setOKButtonEnabled(valid);
231         }
232 }