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