Lines 14-34
Link Here
|
14 |
import java.text.MessageFormat; |
14 |
import java.text.MessageFormat; |
15 |
|
15 |
|
16 |
import org.eclipse.core.resources.IFile; |
16 |
import org.eclipse.core.resources.IFile; |
|
|
17 |
import org.eclipse.core.resources.IResource; |
18 |
import org.eclipse.core.resources.IWorkspace; |
19 |
import org.eclipse.core.resources.IWorkspaceRoot; |
17 |
import org.eclipse.core.resources.ResourcesPlugin; |
20 |
import org.eclipse.core.resources.ResourcesPlugin; |
|
|
21 |
import org.eclipse.core.runtime.IPath; |
22 |
import org.eclipse.core.runtime.IStatus; |
18 |
import org.eclipse.core.runtime.Path; |
23 |
import org.eclipse.core.runtime.Path; |
|
|
24 |
import org.eclipse.jface.dialogs.IMessageProvider; |
19 |
import org.eclipse.jface.viewers.IStructuredSelection; |
25 |
import org.eclipse.jface.viewers.IStructuredSelection; |
|
|
26 |
import org.eclipse.osgi.util.NLS; |
27 |
import org.eclipse.swt.widgets.Composite; |
20 |
import org.eclipse.ui.dialogs.WizardNewFileCreationPage; |
28 |
import org.eclipse.ui.dialogs.WizardNewFileCreationPage; |
21 |
|
29 |
|
22 |
public class GMFGenNewFileCreationPage extends WizardNewFileCreationPage { |
30 |
public class GMFGenNewFileCreationPage extends WizardNewFileCreationPage { |
23 |
|
31 |
|
24 |
static final String EXT_GMFGEN = "gmfgen"; //$NON-NLS-1$ |
32 |
static final String EXT_GMFGEN = "gmfgen"; //$NON-NLS-1$ |
|
|
33 |
|
34 |
private ResourceGroupEmulator resourceGroupEmulator; |
25 |
|
35 |
|
26 |
public GMFGenNewFileCreationPage(String pageId, IStructuredSelection selection) { |
36 |
public GMFGenNewFileCreationPage(String pageId, IStructuredSelection selection) { |
27 |
super(pageId, selection); |
37 |
super(pageId, selection); |
28 |
} |
38 |
} |
|
|
39 |
|
40 |
/* (non-Javadoc) |
41 |
* @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createControl(org.eclipse.swt.widgets.Composite) |
42 |
*/ |
43 |
@Override |
44 |
public void createControl(Composite parent) { |
45 |
resourceGroupEmulator = new ResourceGroupEmulator(); |
46 |
super.createControl(parent); |
47 |
} |
29 |
|
48 |
|
30 |
protected boolean validatePage() { |
49 |
protected boolean validatePage() { |
31 |
if (super.validatePage()) { |
50 |
if (emulateValidate()) { |
32 |
// Make sure the file ends in ".gmfgen". |
51 |
// Make sure the file ends in ".gmfgen". |
33 |
// |
52 |
// |
34 |
String enteredExt = new Path(getFileName()).getFileExtension(); |
53 |
String enteredExt = new Path(getFileName()).getFileExtension(); |
Lines 38-43
Link Here
|
38 |
return false; |
57 |
return false; |
39 |
} |
58 |
} |
40 |
else { |
59 |
else { |
|
|
60 |
IPath resourcePath = getContainerFullPath().append( |
61 |
getFileName()); |
62 |
if (isFileExists(resourcePath)) { |
63 |
setMessage(Messages.ResourceGroup_nameExists, IMessageProvider.WARNING); |
64 |
} |
41 |
return true; |
65 |
return true; |
42 |
} |
66 |
} |
43 |
} |
67 |
} |
Lines 49-52
Link Here
|
49 |
public IFile getModelFile() { |
73 |
public IFile getModelFile() { |
50 |
return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())); |
74 |
return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())); |
51 |
} |
75 |
} |
|
|
76 |
|
77 |
private boolean emulateValidate() { |
78 |
boolean valid = true; |
79 |
|
80 |
if (!resourceGroupEmulator.validateControls()) { |
81 |
// if blank name then fail silently |
82 |
if (resourceGroupEmulator.getProblemType() == ResourceGroupEmulator.PROBLEM_RESOURCE_EMPTY |
83 |
|| resourceGroupEmulator.getProblemType() == ResourceGroupEmulator.PROBLEM_CONTAINER_EMPTY) { |
84 |
setMessage(resourceGroupEmulator.getProblemMessage()); |
85 |
setErrorMessage(null); |
86 |
} else { |
87 |
setErrorMessage(resourceGroupEmulator.getProblemMessage()); |
88 |
} |
89 |
valid = false; |
90 |
} |
91 |
|
92 |
String resourceName = getFileName(); |
93 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
94 |
IStatus result = workspace.validateName(resourceName, IResource.FILE); |
95 |
if (!result.isOK()) { |
96 |
setErrorMessage(result.getMessage()); |
97 |
return false; |
98 |
} |
99 |
|
100 |
IStatus linkedResourceStatus = null; |
101 |
if (valid) { |
102 |
linkedResourceStatus = validateLinkedResource(); |
103 |
if (linkedResourceStatus.getSeverity() == IStatus.ERROR) { |
104 |
valid = false; |
105 |
} |
106 |
} |
107 |
// validateLinkedResource sets messages itself |
108 |
if (valid |
109 |
&& (linkedResourceStatus == null || linkedResourceStatus.isOK())) { |
110 |
setMessage(null); |
111 |
setErrorMessage(null); |
112 |
} |
113 |
return valid; |
114 |
} |
115 |
|
116 |
boolean isFileExists(IPath resourcePath) { |
117 |
if (resourcePath == null) { |
118 |
return false; |
119 |
} |
120 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
121 |
return (workspace.getRoot().getFolder(resourcePath).exists() || workspace |
122 |
.getRoot().getFile(resourcePath).exists()); |
123 |
} |
124 |
|
125 |
//XXX: remove after fix of https://bugs.eclipse.org/bugs/show_bug.cgi?id=172000 |
126 |
private class ResourceGroupEmulator { |
127 |
|
128 |
static final int PROBLEM_NONE = 0; |
129 |
static final int PROBLEM_RESOURCE_EMPTY = 1; |
130 |
static final int PROBLEM_RESOURCE_EXIST = 2; |
131 |
static final int PROBLEM_PATH_INVALID = 4; |
132 |
static final int PROBLEM_CONTAINER_EMPTY = 5; |
133 |
static final int PROBLEM_PROJECT_DOES_NOT_EXIST = 6; |
134 |
static final int PROBLEM_NAME_INVALID = 7; |
135 |
static final int PROBLEM_PATH_OCCUPIED = 8; |
136 |
|
137 |
private boolean allowExistingResources = true; |
138 |
private String problemMessage = "";//$NON-NLS-1$ |
139 |
private int problemType = PROBLEM_NONE; |
140 |
|
141 |
|
142 |
ResourceGroupEmulator() { |
143 |
//just create |
144 |
} |
145 |
|
146 |
boolean validateControls() { |
147 |
// don't attempt to validate controls until they have been created |
148 |
try { |
149 |
getFileName(); |
150 |
} catch (Exception e) { |
151 |
return false; |
152 |
} |
153 |
problemType = PROBLEM_NONE; |
154 |
problemMessage = "";//$NON-NLS-1$ |
155 |
|
156 |
if (!validateContainer() || !validateResourceName()) { |
157 |
return false; |
158 |
} |
159 |
|
160 |
IPath path = getContainerFullPath().append( |
161 |
getFileName()); |
162 |
return validateFullResourcePath(path); |
163 |
} |
164 |
|
165 |
private boolean validateContainer() { |
166 |
IPath path = getContainerFullPath(); |
167 |
if (path == null) { |
168 |
problemType = PROBLEM_CONTAINER_EMPTY; |
169 |
problemMessage = Messages.ResourceGroup_folderEmpty; |
170 |
return false; |
171 |
} |
172 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
173 |
String projectName = path.segment(0); |
174 |
if (projectName == null |
175 |
|| !workspace.getRoot().getProject(projectName).exists()) { |
176 |
problemType = PROBLEM_PROJECT_DOES_NOT_EXIST; |
177 |
problemMessage = Messages.ResourceGroup_noProject; |
178 |
return false; |
179 |
} |
180 |
//path is invalid if any prefix is occupied by a file |
181 |
IWorkspaceRoot root = workspace.getRoot(); |
182 |
while (path.segmentCount() > 1) { |
183 |
if (root.getFile(path).exists()) { |
184 |
problemType = PROBLEM_PATH_OCCUPIED; |
185 |
problemMessage = NLS.bind(Messages.ResourceGroup_pathOccupied, path.makeRelative()); |
186 |
return false; |
187 |
} |
188 |
path = path.removeLastSegments(1); |
189 |
} |
190 |
return true; |
191 |
} |
192 |
|
193 |
private boolean validateResourceName() { |
194 |
String resourceName = getFileName(); |
195 |
|
196 |
if (resourceName.length() == 0) { |
197 |
problemType = PROBLEM_RESOURCE_EMPTY; |
198 |
problemMessage = NLS.bind(Messages.ResourceGroup_emptyName, Messages.WizardNewFileCreationPage_file); |
199 |
return false; |
200 |
} |
201 |
|
202 |
if (!Path.ROOT.isValidPath(resourceName)) { |
203 |
problemType = PROBLEM_NAME_INVALID; |
204 |
problemMessage = NLS.bind(Messages.ResourceGroup_invalidFilename, resourceName); |
205 |
return false; |
206 |
} |
207 |
return true; |
208 |
} |
209 |
|
210 |
private boolean validateFullResourcePath(IPath resourcePath) { |
211 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
212 |
|
213 |
IStatus result = workspace.validatePath(resourcePath.toString(), |
214 |
IResource.FOLDER); |
215 |
if (!result.isOK()) { |
216 |
problemType = PROBLEM_PATH_INVALID; |
217 |
problemMessage = result.getMessage(); |
218 |
return false; |
219 |
} |
220 |
|
221 |
if (!allowExistingResources |
222 |
&& isFileExists(resourcePath)) { |
223 |
problemType = PROBLEM_RESOURCE_EXIST; |
224 |
problemMessage = Messages.ResourceGroup_nameExists; |
225 |
return false; |
226 |
} |
227 |
return true; |
228 |
} |
229 |
|
230 |
String getProblemMessage() { |
231 |
return problemMessage; |
232 |
} |
233 |
|
234 |
int getProblemType() { |
235 |
return problemType; |
236 |
} |
237 |
} |
52 |
} |
238 |
} |