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
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package org.eclipse.webbrowser.internal;
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;
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;
47 interface StringModifyListener {
48 public void valueChanged(String s);
54 public ExternalBrowserDialog(Shell parentShell, IExternalWebBrowserWorkingCopy browser) {
56 this.browser = browser;
60 public ExternalBrowserDialog(Shell parentShell) {
62 browser = BrowserManager.getInstance().createExternalWebBrowser();
66 protected void configureShell(Shell shell) {
67 super.configureShell(shell);
70 shell.setText(WebBrowserUIPlugin.getResource("%editExternalBrowser"));
72 shell.setText(WebBrowserUIPlugin.getResource("%createBrowser"));
75 protected Text createText(Composite comp, String txt, final StringModifyListener listener) {
76 final Text text = new Text(comp, SWT.BORDER);
79 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
81 text.setLayoutData(data);
83 text.addModifyListener(new ModifyListener() {
84 public void modifyText(ModifyEvent e) {
85 listener.valueChanged(text.getText());
92 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
94 protected Control createDialogArea(Composite parent) {
95 Composite composite = (Composite) super.createDialogArea(parent);
96 ((GridLayout)composite.getLayout()).numColumns = 3;
99 WorkbenchHelp.setHelp(composite, ContextIds.PREF_BROWSER_EXTERNAL_EDIT);
101 WorkbenchHelp.setHelp(composite, ContextIds.PREF_BROWSER_EXTERNAL_ADD);
103 SWTUtil.createLabel(composite, WebBrowserUIPlugin.getResource("%name"));
104 browserNameTextfield = createText(composite, browser.getName(), new StringModifyListener() {
105 public void valueChanged(String s) {
111 new Label(composite, SWT.NONE);
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);
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"));
127 String fname = browserLocationTextfield.getText();
129 dialog.setFileName(fname);
130 fname = dialog.open();
133 browserLocationTextfield.setText(fname);
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);
144 new Label(composite, SWT.NONE);
146 new Label(composite, SWT.NONE);
147 Label urlLabel = new Label(composite, SWT.NONE);
148 urlLabel.setText(WebBrowserUIPlugin.getResource("%parametersMessage", WebBrowserPreference.URL_PARAMETER));
155 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
157 protected void okPressed() {
158 // do simple field validation to at least ensure target directory entered is valid pathname
160 File file = new File(browser.getLocation());
162 WebBrowserUtil.openError(WebBrowserUIPlugin.getResource("%locationInvalid"));
167 WebBrowserUtil.openError(WebBrowserUIPlugin.getResource("%locationInvalid"));
175 private void setOKButtonEnabled(boolean curIsEnabled) {
176 if (okButton == null)
177 okButton = getButton(IDialogConstants.OK_ID);
179 if (okButton != null)
180 okButton.setEnabled(curIsEnabled);
183 protected Control createButtonBar(Composite parent) {
184 Control buttonControl = super.createButtonBar(parent);
186 return buttonControl;
189 protected void validateFields() {
190 boolean valid = true;
192 String name = browserNameTextfield.getText();
193 if (name == null || name.trim().length() < 1)
196 String location = browserLocationTextfield.getText();
197 if (location == null || location.trim().length() < 1)
200 setOKButtonEnabled(valid);