KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > BrowserDescriptorDialog


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.browser;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.graphics.Font;
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 public class BrowserDescriptorDialog extends Dialog {
38     protected IBrowserDescriptorWorkingCopy 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 JavaDoc s);
50     }
51     
52     /**
53      * @param parentShell
54      */

55     public BrowserDescriptorDialog(Shell parentShell, IBrowserDescriptorWorkingCopy browser) {
56         super(parentShell);
57         this.browser = browser;
58         isEdit = true;
59     }
60
61     public BrowserDescriptorDialog(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(Messages.editExternalBrowser);
72         else
73             shell.setText(Messages.createBrowser);
74     }
75
76     protected Text createText(Composite comp, String JavaDoc 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         Font font = parent.getFont();
97         Composite composite = (Composite) super.createDialogArea(parent);
98         ((GridLayout)composite.getLayout()).numColumns = 3;
99         composite.setFont(font);
100         
101         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ContextIds.PREF_BROWSER_DIALOG);
102         
103         SWTUtil.createLabel(composite, Messages.name).setFont(font);
104         browserNameTextfield = createText(composite, browser.getName(), new StringModifyListener() {
105             public void valueChanged(String JavaDoc s) {
106                 browser.setName(s);
107                 validateFields();
108             }
109         });
110         browserNameTextfield.setFont(font);
111         
112         new Label(composite, SWT.NONE);
113     
114         SWTUtil.createLabel(composite, Messages.location).setFont(font);
115         browserLocationTextfield = createText(composite, browser.getLocation(), new StringModifyListener() {
116             public void valueChanged(String JavaDoc s) {
117                 browser.setLocation(s);
118                 validateFields();
119             }
120         });
121         browserLocationTextfield.setFont(font);
122         
123         browseButton = SWTUtil.createButton(composite, Messages.browse);
124         browseButton.setFont(font);
125         browseButton.addSelectionListener(new SelectionAdapter() {
126             public void widgetSelected(SelectionEvent e) {
127                 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
128                 dialog.setText(Messages.browseMessage);
129                 
130                 String JavaDoc fname = browserLocationTextfield.getText();
131                 
132                 dialog.setFileName(fname);
133                 fname = dialog.open();
134                 
135                 if (fname != null)
136                     browserLocationTextfield.setText(fname);
137             }
138         });
139         
140         SWTUtil.createLabel(composite, Messages.parameters).setFont(font);
141         browserParametersTextfield = createText(composite, browser.getParameters(), new StringModifyListener() {
142             public void valueChanged(String JavaDoc s) {
143                 browser.setParameters(s);
144             }
145         });
146         browserParametersTextfield.setFont(font);
147
148         new Label(composite, SWT.NONE);
149         
150         new Label(composite, SWT.NONE);
151         Label urlLabel = new Label(composite, SWT.NONE);
152         urlLabel.setText(NLS.bind(Messages.parametersMessage, IBrowserDescriptor.URL_PARAMETER));
153         urlLabel.setFont(font);
154         
155         return composite;
156     }
157
158     /* (non-Javadoc)
159      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
160      */

161     protected void okPressed() {
162         // do simple field validation to at least ensure target directory entered is valid pathname
163
try {
164         File JavaDoc file = new File JavaDoc(browser.getLocation());
165             if(!file.isFile()){
166                 WebBrowserUtil.openError(Messages.locationInvalid);
167                 return;
168             }
169         }
170         catch(Exception JavaDoc e){
171             WebBrowserUtil.openError(Messages.locationInvalid);
172             return;
173         }
174         
175         browser.save();
176         super.okPressed();
177     }
178     
179     private void setOKButtonEnabled(boolean curIsEnabled) {
180         if (okButton == null)
181             okButton = getButton(IDialogConstants.OK_ID);
182         
183         if (okButton != null)
184             okButton.setEnabled(curIsEnabled);
185     }
186     
187     protected Control createButtonBar(Composite parent) {
188         Control buttonControl = super.createButtonBar(parent);
189         validateFields();
190         return buttonControl;
191     }
192     
193     protected void validateFields() {
194         boolean valid = true;
195         
196         String JavaDoc name = browserNameTextfield.getText();
197         if (name == null || name.trim().length() < 1)
198             valid = false;
199         
200         String JavaDoc location = browserLocationTextfield.getText();
201         if (location == null || location.trim().length() < 1)
202             valid = false;
203         
204         setOKButtonEnabled(valid);
205     }
206 }
207
Popular Tags