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