KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > WizardNewLinkPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog
11  * font should be activated and used by other components.
12  *******************************************************************************/

13
14 package org.eclipse.ui.dialogs;
15
16 import org.eclipse.core.filesystem.IFileStore;
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IPathVariableManager;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspace;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.wizard.WizardPage;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.ModifyEvent;
29 import org.eclipse.swt.events.ModifyListener;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.events.SelectionListener;
33 import org.eclipse.swt.graphics.Font;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.DirectoryDialog;
39 import org.eclipse.swt.widgets.FileDialog;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.ide.dialogs.PathVariableSelectionDialog;
44 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
45 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
46 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
47 import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils;
48
49 /**
50  * Standard resource link page for a wizard that creates a file or
51  * folder resource.
52  * <p>
53  * This class may be instantiated; it is not intended to be subclassed.
54  * </p>
55  * @since 2.1
56  */

57 public class WizardNewLinkPage extends WizardPage {
58     private String JavaDoc initialLinkTarget;
59
60     private int type;
61
62     private boolean createLink = false;
63
64     private IContainer container;
65
66     // widgets
67
private Text linkTargetField;
68
69     private Button browseButton;
70
71     private Button variablesButton;
72
73     /**
74      * Creates a new resource link wizard page.
75      *
76      * @param pageName the name of the page
77      * @param type specifies the type of resource to link to.
78      * <code>IResource.FILE</code> or <code>IResource.FOLDER</code>
79      */

80     public WizardNewLinkPage(String JavaDoc pageName, int type) {
81         super(pageName);
82         this.type = type;
83         setPageComplete(true);
84     }
85
86     /* (non-Javadoc)
87      * Method declared on IDialogPage.
88      */

89     public void createControl(Composite parent) {
90         Font font = parent.getFont();
91         initializeDialogUnits(parent);
92         // top level group
93
Composite topLevel = new Composite(parent, SWT.NONE);
94         GridLayout layout = new GridLayout();
95         layout.numColumns = 3;
96         topLevel.setLayout(layout);
97         topLevel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
98                 | GridData.HORIZONTAL_ALIGN_FILL));
99         topLevel.setFont(font);
100         PlatformUI.getWorkbench().getHelpSystem().setHelp(topLevel,
101                 IIDEHelpContextIds.NEW_LINK_WIZARD_PAGE);
102
103         final Button createLinkButton = new Button(topLevel, SWT.CHECK);
104         if (type == IResource.FILE) {
105             createLinkButton.setText(IDEWorkbenchMessages.WizardNewLinkPage_linkFileButton);
106         } else {
107             createLinkButton.setText(IDEWorkbenchMessages.WizardNewLinkPage_linkFolderButton);
108         }
109         createLinkButton.setSelection(createLink);
110         GridData data = new GridData();
111         data.horizontalSpan = 3;
112         createLinkButton.setLayoutData(data);
113         createLinkButton.setFont(font);
114         SelectionListener listener = new SelectionAdapter() {
115             public void widgetSelected(SelectionEvent e) {
116                 createLink = createLinkButton.getSelection();
117                 browseButton.setEnabled(createLink);
118                 variablesButton.setEnabled(createLink);
119                 linkTargetField.setEnabled(createLink);
120                 setPageComplete(validatePage());
121             }
122         };
123         createLinkButton.addSelectionListener(listener);
124
125         createLinkLocationGroup(topLevel, createLink);
126         validatePage();
127
128         setErrorMessage(null);
129         setMessage(null);
130         setControl(topLevel);
131     }
132
133     /**
134      * Creates the link target location widgets.
135      *
136      * @param locationGroup the parent composite
137      * @param enabled sets the initial enabled state of the widgets
138      */

139     private void createLinkLocationGroup(Composite locationGroup,
140             boolean enabled) {
141         Font font = locationGroup.getFont();
142         Label fill = new Label(locationGroup, SWT.NONE);
143         GridData data = new GridData();
144         Button button = new Button(locationGroup, SWT.CHECK);
145         data.widthHint = button.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
146         button.dispose();
147         fill.setLayoutData(data);
148
149         // link target location entry field
150
linkTargetField = new Text(locationGroup, SWT.BORDER);
151         data = new GridData(GridData.FILL_HORIZONTAL);
152         linkTargetField.setLayoutData(data);
153         linkTargetField.setFont(font);
154         linkTargetField.setEnabled(enabled);
155         linkTargetField.addModifyListener(new ModifyListener() {
156             public void modifyText(ModifyEvent e) {
157                 setPageComplete(validatePage());
158             }
159         });
160         if (initialLinkTarget != null) {
161             linkTargetField.setText(initialLinkTarget);
162         }
163
164         // browse button
165
browseButton = new Button(locationGroup, SWT.PUSH);
166         setButtonLayoutData(browseButton);
167         browseButton.setFont(font);
168         browseButton.setText(IDEWorkbenchMessages.WizardNewLinkPage_browseButton);
169         browseButton.addSelectionListener(new SelectionAdapter() {
170             public void widgetSelected(SelectionEvent event) {
171                 handleLinkTargetBrowseButtonPressed();
172             }
173         });
174         browseButton.setEnabled(enabled);
175
176         fill = new Label(locationGroup, SWT.NONE);
177         data = new GridData();
178         data.horizontalSpan = 2;
179         fill.setLayoutData(data);
180
181         // variables button
182
variablesButton = new Button(locationGroup, SWT.PUSH);
183         setButtonLayoutData(variablesButton);
184         variablesButton.setFont(font);
185         variablesButton.setText(IDEWorkbenchMessages.WizardNewLinkPage_variablesButton);
186         variablesButton.addSelectionListener(new SelectionAdapter() {
187             public void widgetSelected(SelectionEvent event) {
188                 handleVariablesButtonPressed();
189             }
190         });
191         variablesButton.setEnabled(enabled);
192     }
193
194     /**
195      * Returns the link target location entered by the user.
196      *
197      * @return the link target location entered by the user. null if the user
198      * choose not to create a link.
199      */

200     public String JavaDoc getLinkTarget() {
201         if (createLink && linkTargetField != null
202                 && linkTargetField.isDisposed() == false) {
203             return linkTargetField.getText();
204         }
205         return null;
206     }
207
208     /**
209      * Opens a file or directory browser depending on the link type.
210      */

211     private void handleLinkTargetBrowseButtonPressed() {
212         String JavaDoc linkTargetName = linkTargetField.getText();
213          String JavaDoc selection = null;
214          IFileStore store = null;
215         if (linkTargetName.length() > 0) {
216             store = IDEResourceInfoUtils.getFileStore(linkTargetName);
217             if (store == null || !store.fetchInfo().exists()) {
218                 store = null;
219             }
220         }
221         if (type == IResource.FILE) {
222             FileDialog dialog = new FileDialog(getShell());
223             if (store != null) {
224                 if (store.fetchInfo().isDirectory()) {
225                     dialog.setFilterPath(linkTargetName);
226                 } else {
227                     dialog.setFileName(linkTargetName);
228                 }
229             }
230             selection = dialog.open();
231         } else {
232             DirectoryDialog dialog = new DirectoryDialog(getShell());
233             if (store != null) {
234                 if (!store.fetchInfo().isDirectory()) {
235                     linkTargetName = store.getParent().getName();
236                 }
237                 if (linkTargetName != null) {
238                     dialog.setFilterPath(linkTargetName);
239                 }
240             }
241             dialog.setMessage(IDEWorkbenchMessages.WizardNewLinkPage_targetSelectionLabel);
242             selection = dialog.open();
243         }
244         if (selection != null) {
245             linkTargetField.setText(selection);
246         }
247     }
248
249     /**
250      * Opens a path variable selection dialog
251      */

252     private void handleVariablesButtonPressed() {
253         PathVariableSelectionDialog dialog = new PathVariableSelectionDialog(
254                 getShell(), type);
255
256         if (dialog.open() == IDialogConstants.OK_ID) {
257             String JavaDoc[] variableNames = (String JavaDoc[]) dialog.getResult();
258
259             if (variableNames != null) {
260                 IPathVariableManager pathVariableManager = ResourcesPlugin
261                         .getWorkspace().getPathVariableManager();
262                 IPath path = pathVariableManager.getValue(variableNames[0]);
263
264                 if (path != null) {
265                     linkTargetField.setText(path.toOSString());
266                 }
267             }
268         }
269     }
270
271     /**
272      * Sets the container to use for link validation.
273      * This should be the parent of the new resource that is being
274      * linked.
275      *
276      * @param container the container to use for link validation.
277      */

278     public void setContainer(IContainer container) {
279         this.container = container;
280     }
281
282     /**
283      * Sets the value of the link target field
284      *
285      * @param target the value of the link target field
286      */

287     public void setLinkTarget(String JavaDoc target) {
288         initialLinkTarget = target;
289         if (linkTargetField != null && linkTargetField.isDisposed() == false) {
290             linkTargetField.setText(target);
291         }
292     }
293
294     /**
295      * Validates the type of the given file against the link type specified
296      * during page creation.
297      *
298      * @param linkTargetStore file to validate
299      * @return boolean <code>true</code> if the link target type is valid
300      * and <code>false</code> otherwise.
301      */

302     private boolean validateFileType(IFileStore linkTargetStore) {
303         boolean valid = true;
304
305         if (type == IResource.FILE && linkTargetStore.fetchInfo().isDirectory()) {
306             setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetNotFile);
307             valid = false;
308         } else if (type == IResource.FOLDER
309                 && !linkTargetStore.fetchInfo().isDirectory()) {
310             setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetNotFolder);
311             valid = false;
312         }
313         return valid;
314     }
315
316     /**
317      * Validates the name of the link target
318      *
319      * @param linkTargetName link target name to validate
320      * @return boolean <code>true</code> if the link target name is valid
321      * and <code>false</code> otherwise.
322      */

323     private boolean validateLinkTargetName(String JavaDoc linkTargetName) {
324         boolean valid = true;
325
326         if ("".equals(linkTargetName)) {//$NON-NLS-1$
327
setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetEmpty);
328             valid = false;
329         } else {
330             IPath path = new Path("");//$NON-NLS-1$
331
if (path.isValidPath(linkTargetName) == false) {
332                 setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetInvalid);
333                 valid = false;
334             }
335         }
336         return valid;
337     }
338
339     /**
340      * Returns whether this page's controls currently all contain valid
341      * values.
342      *
343      * @return <code>true</code> if all controls are valid, and
344      * <code>false</code> if at least one is invalid
345      */

346     private boolean validatePage() {
347         boolean valid = true;
348         IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
349
350         if (createLink) {
351             String JavaDoc linkTargetName = linkTargetField.getText();
352
353             valid = validateLinkTargetName(linkTargetName);
354             if (valid) {
355                 IFileStore linkTargetFile = IDEResourceInfoUtils.getFileStore(linkTargetName);
356                 if (linkTargetFile == null || !linkTargetFile.fetchInfo().exists()) {
357                     setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetNonExistent);
358                     valid = false;
359                 } else {
360                     IStatus locationStatus = workspace.validateLinkLocation(
361                             container, new Path(linkTargetName));
362
363                     if (locationStatus.isOK() == false) {
364                         setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetLocationInvalid);
365                         valid = false;
366                     } else {
367                         valid = validateFileType(linkTargetFile);
368                     }
369                 }
370             }
371         }
372         // Avoid draw flicker by clearing error message
373
// if all is valid.
374
if (valid) {
375             setMessage(null);
376             setErrorMessage(null);
377         }
378         return valid;
379     }
380 }
381
Popular Tags