KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > scripting > RefactoringScriptLocationControl


1 /*******************************************************************************
2  * Copyright (c) 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.ltk.internal.ui.refactoring.scripting;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URI JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.ltk.internal.ui.refactoring.RefactoringLocationControl;
20 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin;
21 import org.eclipse.ltk.internal.ui.refactoring.util.SWTUtil;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.DisposeEvent;
25 import org.eclipse.swt.events.DisposeListener;
26 import org.eclipse.swt.events.ModifyEvent;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.FileDialog;
35
36 import org.eclipse.jface.dialogs.IDialogSettings;
37 import org.eclipse.jface.wizard.IWizard;
38
39 /**
40  * Control to specify the location of a refactoring script.
41  *
42  * @since 3.2
43  */

44 public class RefactoringScriptLocationControl extends Composite {
45
46     /** The clipboard dialog setting */
47     protected static final String JavaDoc SETTING_CLIPBOARD= "org.eclipse.ltk.ui.refactoring.useClipboard"; //$NON-NLS-1$
48

49     /** The history dialog setting */
50     protected static final String JavaDoc SETTING_HISTORY= "org.eclipse.ltk.ui.refactoring.scriptHistory"; //$NON-NLS-1$
51

52     /** The external browse button */
53     protected Button fExternalBrowseButton= null;
54
55     /** The location control */
56     protected RefactoringLocationControl fExternalLocationControl= null;
57
58     /** The from clipboard button */
59     protected Button fFromClipboardButton= null;
60
61     /** The from external location button */
62     protected Button fFromExternalLocationButton= null;
63
64     /** The script location, or <code>null</code> */
65     protected URI JavaDoc fScriptLocation= null;
66
67     /** The parent wizard */
68     protected final IWizard fWizard;
69
70     /**
71      * Creates a new refactoring script location control.
72      *
73      * @param wizard
74      * the parent wizard
75      * @param parent
76      * the parent control
77      */

78     public RefactoringScriptLocationControl(final IWizard wizard, final Composite parent) {
79         super(parent, SWT.NONE);
80         Assert.isNotNull(wizard);
81         fWizard= wizard;
82         setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 6, 0));
83         setLayout(new GridLayout(3, false));
84         boolean clipboard= false;
85         final IDialogSettings settings= fWizard.getDialogSettings();
86         if (settings != null)
87             clipboard= settings.getBoolean(SETTING_CLIPBOARD);
88         fFromClipboardButton= new Button(this, SWT.RADIO);
89         fFromClipboardButton.setText(ScriptingMessages.ScriptLocationControl_clipboard_label);
90         fFromClipboardButton.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_BEGINNING, 3, 0));
91         fFromClipboardButton.setSelection(clipboard);
92         fFromClipboardButton.addSelectionListener(new SelectionAdapter() {
93
94             public final void widgetSelected(final SelectionEvent event) {
95                 final boolean selection= fFromClipboardButton.getSelection();
96                 fExternalLocationControl.setEnabled(!selection);
97                 fExternalBrowseButton.setEnabled(!selection);
98                 handleClipboardScriptChanged();
99             }
100         });
101         fFromExternalLocationButton= new Button(this, SWT.RADIO);
102         fFromExternalLocationButton.setText(ScriptingMessages.ScriptLocationControl_location_label);
103         fFromExternalLocationButton.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_BEGINNING, 1, 0));
104         fFromExternalLocationButton.setSelection(!clipboard);
105         fFromExternalLocationButton.addSelectionListener(new SelectionAdapter() {
106
107             public final void widgetSelected(final SelectionEvent event) {
108                 handleExternalLocationChanged();
109             }
110         });
111         fExternalLocationControl= new RefactoringLocationControl(fWizard, this, SETTING_HISTORY);
112         fExternalLocationControl.setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 1, 0));
113         fExternalLocationControl.setEnabled(!clipboard);
114         fExternalLocationControl.getControl().addModifyListener(new ModifyListener() {
115
116             public final void modifyText(final ModifyEvent event) {
117                 handleExternalLocationChanged();
118             }
119         });
120         fExternalLocationControl.getControl().addSelectionListener(new SelectionAdapter() {
121
122             public void widgetSelected(final SelectionEvent event) {
123                 handleExternalLocationChanged();
124             }
125         });
126         if (!clipboard)
127             fExternalLocationControl.setFocus();
128         fExternalBrowseButton= new Button(this, SWT.PUSH);
129         fExternalBrowseButton.setText(ScriptingMessages.ScriptLocationControl_browse_label);
130         fExternalBrowseButton.setEnabled(!clipboard);
131         fExternalBrowseButton.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 1, 0));
132         SWTUtil.setButtonDimensionHint(fExternalBrowseButton);
133         fExternalBrowseButton.addSelectionListener(new SelectionAdapter() {
134
135             public final void widgetSelected(final SelectionEvent event) {
136                 handleBrowseExternalLocation();
137             }
138         });
139         addDisposeListener(new DisposeListener() {
140
141             public final void widgetDisposed(final DisposeEvent event) {
142                 if (settings != null)
143                     settings.put(SETTING_CLIPBOARD, fFromClipboardButton.getSelection());
144             }
145         });
146     }
147
148     /**
149      * Creates a new grid data.
150      *
151      * @param flag
152      * the flags to use
153      * @param hspan
154      * the horizontal span
155      * @param indent
156      * the indent
157      * @return the grid data
158      */

159     protected GridData createGridData(final int flag, final int hspan, final int indent) {
160         final GridData data= new GridData(flag);
161         data.horizontalIndent= indent;
162         data.horizontalSpan= hspan;
163         return data;
164     }
165
166     /**
167      * Returns the chosen refactoring script location.
168      *
169      * @return the refactoring script location, or <code>null</code> if no
170      * refactoring script has been chosen, or the refactoring script
171      * should be taken from the clipboard
172      */

173     public URI JavaDoc getRefactoringScript() {
174         return fScriptLocation;
175     }
176
177     /**
178      * Handles the browse external location event.
179      */

180     protected void handleBrowseExternalLocation() {
181         final FileDialog file= new FileDialog(getShell(), SWT.OPEN);
182         file.setText(ScriptingMessages.ScriptLocationControl_browse_caption);
183         file.setFilterNames(new String JavaDoc[] { ScriptingMessages.ScriptLocationControl_filter_name_script, ScriptingMessages.ScriptLocationControl_filter_name_wildcard});
184         file.setFilterExtensions(new String JavaDoc[] { ScriptingMessages.ScriptLocationControl_filter_extension_script, ScriptingMessages.ScriptLocationControl_filter_extension_wildcard});
185         final String JavaDoc path= file.open();
186         if (path != null)
187             fExternalLocationControl.setText(path);
188     }
189
190     /**
191      * Handles the clipboard script changed event.
192      */

193     protected void handleClipboardScriptChanged() {
194         // Do nothing
195
}
196
197     /**
198      * Handles the external location changed event.
199      */

200     protected void handleExternalLocationChanged() {
201         final String JavaDoc text= fExternalLocationControl.getText();
202         if (text != null && !"".equals(text)) //$NON-NLS-1$
203
fScriptLocation= new File JavaDoc(text).toURI();
204         else
205             fScriptLocation= null;
206     }
207
208     /**
209      * Loads the history of this control.
210      */

211     public void loadHistory() {
212         fExternalLocationControl.loadHistory();
213     }
214
215     /**
216      * Saves the history of this control.
217      */

218     public void saveHistory() {
219         fExternalLocationControl.saveHistory();
220     }
221
222     /**
223      * Sets the refactoring script location to choose.
224      *
225      * @param uri
226      * the refactoring script location, or <code>null</code> if no
227      * refactoring script has been chosen, or the refactoring script
228      * should be taken from the clipboard
229      */

230     public void setRefactoringScript(final URI JavaDoc uri) {
231         if (fExternalLocationControl != null)
232             fExternalLocationControl.setEnabled(true);
233         if (fExternalBrowseButton != null)
234             fExternalBrowseButton.setEnabled(true);
235         if (uri == null)
236             fExternalLocationControl.setText(""); //$NON-NLS-1$
237
else {
238             try {
239                 final String JavaDoc path= new File JavaDoc(uri).getCanonicalPath();
240                 if (path != null && !"".equals(path)) //$NON-NLS-1$
241
fExternalLocationControl.setText(path);
242                 handleExternalLocationChanged();
243             } catch (IOException JavaDoc exception) {
244                 RefactoringUIPlugin.log(exception);
245             }
246         }
247     }
248 }
Popular Tags