KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > NewContainerDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.wizards.buildpaths;
12
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.core.runtime.IStatus;
15
16 import org.eclipse.core.resources.IFolder;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IWorkspace;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Shell;
26
27 import org.eclipse.jface.dialogs.StatusDialog;
28
29 import org.eclipse.ui.PlatformUI;
30
31 import org.eclipse.jdt.internal.corext.util.Messages;
32
33 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
34 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
35 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
36 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
37 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
38 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
39 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
40
41 public class NewContainerDialog extends StatusDialog {
42     
43     private StringDialogField fContainerDialogField;
44     private StatusInfo fContainerFieldStatus;
45     
46     private IFolder fFolder;
47     private IPath[] fExistingFolders;
48     private IProject fCurrProject;
49         
50     public NewContainerDialog(Shell parent, String JavaDoc title, IProject project, IPath[] existingFolders, CPListElement entryToEdit) {
51         super(parent);
52         setTitle(title);
53         
54         fContainerFieldStatus= new StatusInfo();
55         
56         SourceContainerAdapter adapter= new SourceContainerAdapter();
57         fContainerDialogField= new StringDialogField();
58         fContainerDialogField.setDialogFieldListener(adapter);
59         
60         fFolder= null;
61         fExistingFolders= existingFolders;
62         fCurrProject= project;
63         
64         if (entryToEdit == null) {
65             fContainerDialogField.setText(""); //$NON-NLS-1$
66
} else {
67             fContainerDialogField.setText(entryToEdit.getPath().removeFirstSegments(1).toString());
68         }
69     }
70     
71     public void setMessage(String JavaDoc message) {
72         fContainerDialogField.setLabelText(message);
73     }
74     
75     protected Control createDialogArea(Composite parent) {
76         Composite composite= (Composite)super.createDialogArea(parent);
77         
78         int widthHint= convertWidthInCharsToPixels(80);
79         
80         Composite inner= new Composite(composite, SWT.NONE);
81         GridLayout layout= new GridLayout();
82         layout.marginHeight= 0;
83         layout.marginWidth= 0;
84         layout.numColumns= 1;
85         inner.setLayout(layout);
86         
87         fContainerDialogField.doFillIntoGrid(inner, 2);
88         
89         LayoutUtil.setWidthHint(fContainerDialogField.getLabelControl(null), widthHint);
90         LayoutUtil.setWidthHint(fContainerDialogField.getTextControl(null), widthHint);
91         LayoutUtil.setHorizontalGrabbing(fContainerDialogField.getTextControl(null));
92                 
93         fContainerDialogField.postSetFocusOnDialogField(parent.getDisplay());
94         applyDialogFont(composite);
95         return composite;
96     }
97
98         
99     // -------- SourceContainerAdapter --------
100

101     private class SourceContainerAdapter implements IDialogFieldListener {
102         
103         // -------- IDialogFieldListener
104

105         public void dialogFieldChanged(DialogField field) {
106             doStatusLineUpdate();
107         }
108     }
109     
110     protected void doStatusLineUpdate() {
111         checkIfPathValid();
112         updateStatus(fContainerFieldStatus);
113     }
114     
115     protected void checkIfPathValid() {
116         fFolder= null;
117         
118         String JavaDoc pathStr= fContainerDialogField.getText();
119         if (pathStr.length() == 0) {
120             fContainerFieldStatus.setError(NewWizardMessages.NewContainerDialog_error_enterpath);
121             return;
122         }
123         IPath path= fCurrProject.getFullPath().append(pathStr);
124         IWorkspace workspace= fCurrProject.getWorkspace();
125         
126         IStatus pathValidation= workspace.validatePath(path.toString(), IResource.FOLDER);
127         if (!pathValidation.isOK()) {
128             fContainerFieldStatus.setError(Messages.format(NewWizardMessages.NewContainerDialog_error_invalidpath, pathValidation.getMessage()));
129             return;
130         }
131         IFolder folder= fCurrProject.getFolder(pathStr);
132         if (isFolderExisting(folder)) {
133             fContainerFieldStatus.setError(NewWizardMessages.NewContainerDialog_error_pathexists);
134             return;
135         }
136         fContainerFieldStatus.setOK();
137         fFolder= folder;
138     }
139     
140     private boolean isFolderExisting(IFolder folder) {
141         for (int i= 0; i < fExistingFolders.length; i++) {
142             if (folder.getFullPath().equals(fExistingFolders[i])) {
143                 return true;
144             }
145         }
146         return false;
147     }
148         
149     
150         
151     public IFolder getFolder() {
152         return fFolder;
153     }
154         
155     /*
156      * @see org.eclipse.jface.window.Window#configureShell(Shell)
157      */

158     protected void configureShell(Shell newShell) {
159         super.configureShell(newShell);
160         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.NEW_CONTAINER_DIALOG);
161     }
162
163
164 }
165
Popular Tags