Skip to content

Package: EMFFormsNewRuleRepositoryWizardPage$3

EMFFormsNewRuleRepositoryWizardPage$3

nameinstructionbranchcomplexitylinemethod
modifyText(ModifyEvent)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.rulerepository.tooling.wizard;
15:
16: import org.eclipse.core.resources.IContainer;
17: import org.eclipse.core.resources.IResource;
18: import org.eclipse.core.resources.ResourcesPlugin;
19: import org.eclipse.core.runtime.Path;
20: import org.eclipse.jface.viewers.ISelection;
21: import org.eclipse.jface.viewers.IStructuredSelection;
22: import org.eclipse.jface.window.Window;
23: import org.eclipse.jface.wizard.WizardPage;
24: import org.eclipse.swt.SWT;
25: import org.eclipse.swt.events.ModifyEvent;
26: import org.eclipse.swt.events.ModifyListener;
27: import org.eclipse.swt.events.SelectionAdapter;
28: import org.eclipse.swt.events.SelectionEvent;
29: import org.eclipse.swt.layout.GridData;
30: import org.eclipse.swt.layout.GridLayout;
31: import org.eclipse.swt.widgets.Button;
32: import org.eclipse.swt.widgets.Composite;
33: import org.eclipse.swt.widgets.Label;
34: import org.eclipse.swt.widgets.Text;
35: import org.eclipse.ui.dialogs.ContainerSelectionDialog;
36:
37: /**
38: * The "New" wizard page allows setting the container for the new file as well
39: * as the file name. The page will only accept file name without the extension
40: * OR with the extension that matches the expected one (rulerepository).
41: */
42:
43: public class EMFFormsNewRuleRepositoryWizardPage extends WizardPage {
44:         private Text containerText;
45:
46:         private Text fileText;
47:
48:         private final ISelection selection;
49:
50:         /**
51:          * Constructor for SampleNewWizardPage.
52:          *
53:          * @param selection the current {@link ISelection}
54:          */
55:         public EMFFormsNewRuleRepositoryWizardPage(ISelection selection) {
56:                 super("wizardPage"); //$NON-NLS-1$
57:                 setTitle(Messages.EMFFormsRuleRepositoryWizardPage_title);
58:                 setDescription(Messages.EMFFormsRuleRepositoryWizardPage_description);
59:                 this.selection = selection;
60:         }
61:
62:         @Override
63:         public void createControl(Composite parent) {
64:                 final Composite container = new Composite(parent, SWT.NULL);
65:                 final GridLayout layout = new GridLayout();
66:                 container.setLayout(layout);
67:                 layout.numColumns = 3;
68:                 layout.verticalSpacing = 9;
69:                 Label label = new Label(container, SWT.NULL);
70:                 label.setText(Messages.EMFFormsRuleRepositoryWizardPage_containerSelection);
71:
72:                 containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
73:                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
74:                 containerText.setLayoutData(gd);
75:                 containerText.addModifyListener(new ModifyListener() {
76:                         @Override
77:                         public void modifyText(ModifyEvent e) {
78:                                 dialogChanged();
79:                         }
80:                 });
81:
82:                 final Button button = new Button(container, SWT.PUSH);
83:                 button.setText(Messages.EMFFormsRuleRepositoryWizardPage_browseContainer);
84:                 button.addSelectionListener(new SelectionAdapter() {
85:                         @Override
86:                         public void widgetSelected(SelectionEvent e) {
87:                                 handleBrowse();
88:                         }
89:                 });
90:                 label = new Label(container, SWT.NULL);
91:                 label.setText(Messages.EMFFormsRuleRepositoryWizardPage_fileSelection);
92:
93:                 fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
94:                 gd = new GridData(GridData.FILL_HORIZONTAL);
95:                 fileText.setLayoutData(gd);
96:                 fileText.addModifyListener(new ModifyListener() {
97:                         @Override
98:                         public void modifyText(ModifyEvent e) {
99:                                 dialogChanged();
100:                         }
101:                 });
102:                 initialize();
103:                 dialogChanged();
104:                 setControl(container);
105:         }
106:
107:         /**
108:          * Tests if the current workbench selection is a suitable container to use.
109:          */
110:
111:         private void initialize() {
112:                 if (selection != null && !selection.isEmpty()
113:                         && selection instanceof IStructuredSelection) {
114:                         final IStructuredSelection ssel = (IStructuredSelection) selection;
115:                         if (ssel.size() > 1) {
116:                                 return;
117:                         }
118:                         final Object obj = ssel.getFirstElement();
119:                         if (obj instanceof IResource) {
120:                                 IContainer container;
121:                                 if (obj instanceof IContainer) {
122:                                         container = (IContainer) obj;
123:                                 } else {
124:                                         container = ((IResource) obj).getParent();
125:                                 }
126:                                 containerText.setText(container.getFullPath().toString());
127:                         }
128:                 }
129:                 fileText.setText("new_file.rulerepository"); //$NON-NLS-1$
130:         }
131:
132:         /**
133:          * Uses the standard container selection dialog to choose the new value for
134:          * the container field.
135:          */
136:
137:         private void handleBrowse() {
138:                 final ContainerSelectionDialog dialog = new ContainerSelectionDialog(
139:                         getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
140:                         Messages.EMFFormsRuleRepositoryWizardPage_browseFile);
141:                 if (dialog.open() == Window.OK) {
142:                         final Object[] result = dialog.getResult();
143:                         if (result.length == 1) {
144:                                 containerText.setText(((Path) result[0]).toString());
145:                         }
146:                 }
147:         }
148:
149:         /**
150:          * Validates the container and file.
151:          */
152:
153:         private void dialogChanged() {
154:                 if (getContainerName().length() == 0) {
155:                         updateStatus(Messages.EMFFormsRuleRepositoryWizardPage_errorNoContainer);
156:                         return;
157:                 }
158:
159:                 final IResource container = ResourcesPlugin.getWorkspace().getRoot()
160:                         .findMember(new Path(getContainerName()));
161:
162:                 final String message = getContainerErrorMessage(container);
163:
164:                 if (message != null) {
165:                         updateStatus(message);
166:                         return;
167:                 }
168:
169:                 final String fileName = getFileName();
170:
171:                 if (fileName.length() == 0) {
172:                         updateStatus(Messages.EMFFormsRuleRepositoryWizardPage_errorNoFilename);
173:                         return;
174:                 }
175:                 if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
176:                         updateStatus(Messages.EMFFormsRuleRepositoryWizardPage_errorInvalidFilename);
177:                         return;
178:                 }
179:
180:                 if (ResourcesPlugin.getWorkspace().getRoot().getFile(container.getFullPath().append(fileName)).exists()) {
181:                         updateStatus(String.format(Messages.EMFFormsRuleRepositoryWizardPage_FileAlreadyExist, fileName));
182:                         return;
183:                 }
184:                 final int dotLoc = fileName.lastIndexOf('.');
185:                 if (dotLoc != -1) {
186:                         final String ext = fileName.substring(dotLoc + 1);
187:                         if (!ext.equalsIgnoreCase("rulerepository")) { //$NON-NLS-1$
188:                                 updateStatus(
189:                                         String.format(Messages.EMFFormsRuleRepositoryWizardPage_errorWrongFileExtension, "rulerepository")); //$NON-NLS-1$
190:                                 return;
191:                         }
192:                 }
193:                 updateStatus(null);
194:         }
195:
196:         /**
197:          * Return the error message for a selected {@link IResource} if
198:          * 1. The container does not exists
199:          * 2. If the container is project or folder
200:          * 3. If the container is read-only
201:          * Otherwise (no error) Null is returned.
202:          *
203:          * @param container The {@link IResource} to check
204:          * @return the error message or null if the {@link IResource} is valid
205:          */
206:         public static String getContainerErrorMessage(IResource container) {
207:                 if (container == null
208:                         || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
209:                         return Messages.EMFFormsRuleRepositoryWizardPage_errorContainerNotExists;
210:                 }
211:                 if (!container.isAccessible()) {
212:                         return Messages.EMFFormsRuleRepositoryWizardPage_errorProjectReadOnly;
213:                 }
214:                 return null;
215:         }
216:
217:         private void updateStatus(String message) {
218:                 setErrorMessage(message);
219:                 setPageComplete(message == null);
220:         }
221:
222:         /**
223:          * The container name.
224:          *
225:          * @return the name of the container
226:          */
227:         public String getContainerName() {
228:                 return containerText.getText();
229:         }
230:
231:         /**
232:          * The file name.
233:          *
234:          * @return the name of the file
235:          */
236:         public String getFileName() {
237:                 return fileText.getText();
238:         }
239: }