Skip to content

Package: EMFFormsNewTemplateWizardPage$3

EMFFormsNewTemplateWizardPage$3

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

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 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.emf.ecp.view.template.tooling.wizards;
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.emf.ecp.view.template.internal.tooling.Messages;
21: import org.eclipse.jface.viewers.ISelection;
22: import org.eclipse.jface.viewers.IStructuredSelection;
23: import org.eclipse.jface.window.Window;
24: import org.eclipse.jface.wizard.WizardPage;
25: import org.eclipse.swt.SWT;
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.Label;
35: import org.eclipse.swt.widgets.Text;
36: import org.eclipse.ui.dialogs.ContainerSelectionDialog;
37:
38: /**
39: * The "New" wizard page allows setting the container for the new file as well
40: * as the file name. The page will only accept file name without the extension
41: * OR with the extension that matches the expected one (template).
42: */
43:
44: public class EMFFormsNewTemplateWizardPage extends WizardPage {
45:         private Text containerText;
46:
47:         private Text fileText;
48:
49:         private final ISelection selection;
50:
51:         /**
52:          * Constructor for SampleNewWizardPage.
53:          *
54:          * @param selection the current {@link ISelection}
55:          */
56:         public EMFFormsNewTemplateWizardPage(ISelection selection) {
57:                 super("wizardPage"); //$NON-NLS-1$
58:                 setTitle(Messages.EMFFormsTemplateWizardPage_title);
59:                 setDescription(Messages.EMFFormsTemplateWizardPage_description);
60:                 this.selection = selection;
61:         }
62:
63:         @Override
64:         public void createControl(Composite parent) {
65:                 final Composite container = new Composite(parent, SWT.NULL);
66:                 final GridLayout layout = new GridLayout();
67:                 container.setLayout(layout);
68:                 layout.numColumns = 3;
69:                 layout.verticalSpacing = 9;
70:                 Label label = new Label(container, SWT.NULL);
71:                 label.setText(Messages.EMFFormsTemplateWizardPage_containerSelection);
72:
73:                 containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
74:                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
75:                 containerText.setLayoutData(gd);
76:                 containerText.addModifyListener(new ModifyListener() {
77:                         @Override
78:                         public void modifyText(ModifyEvent e) {
79:                                 dialogChanged();
80:                         }
81:                 });
82:
83:                 final Button button = new Button(container, SWT.PUSH);
84:                 button.setText(Messages.EMFFormsTemplateWizardPage_browseContainer);
85:                 button.addSelectionListener(new SelectionAdapter() {
86:                         @Override
87:                         public void widgetSelected(SelectionEvent e) {
88:                                 handleBrowse();
89:                         }
90:                 });
91:                 label = new Label(container, SWT.NULL);
92:                 label.setText(Messages.EMFFormsTemplateWizardPage_fileSelection);
93:
94:                 fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
95:                 gd = new GridData(GridData.FILL_HORIZONTAL);
96:                 fileText.setLayoutData(gd);
97:                 fileText.addModifyListener(new ModifyListener() {
98:                         @Override
99:                         public void modifyText(ModifyEvent e) {
100:                                 dialogChanged();
101:                         }
102:                 });
103:                 initialize();
104:                 dialogChanged();
105:                 setControl(container);
106:         }
107:
108:         /**
109:          * Tests if the current workbench selection is a suitable container to use.
110:          */
111:
112:         private void initialize() {
113:                 if (selection != null && !selection.isEmpty()
114:                         && selection instanceof IStructuredSelection) {
115:                         final IStructuredSelection ssel = (IStructuredSelection) selection;
116:                         if (ssel.size() > 1) {
117:                                 return;
118:                         }
119:                         final Object obj = ssel.getFirstElement();
120:                         if (obj instanceof IResource) {
121:                                 IContainer container;
122:                                 if (obj instanceof IContainer) {
123:                                         container = (IContainer) obj;
124:                                 } else {
125:                                         container = ((IResource) obj).getParent();
126:                                 }
127:                                 containerText.setText(container.getFullPath().toString());
128:                         }
129:                 }
130:                 fileText.setText("new_file.template"); //$NON-NLS-1$
131:         }
132:
133:         /**
134:          * Uses the standard container selection dialog to choose the new value for
135:          * the container field.
136:          */
137:
138:         private void handleBrowse() {
139:                 final ContainerSelectionDialog dialog = new ContainerSelectionDialog(
140:                         getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
141:                         Messages.EMFFormsTemplateWizardPage_browseFile);
142:                 if (dialog.open() == Window.OK) {
143:                         final Object[] result = dialog.getResult();
144:                         if (result.length == 1) {
145:                                 containerText.setText(((Path) result[0]).toString());
146:                         }
147:                 }
148:         }
149:
150:         /**
151:          * Ensures that both text fields are set.
152:          */
153:
154:         private void dialogChanged() {
155:                 final IResource container = ResourcesPlugin.getWorkspace().getRoot()
156:                         .findMember(new Path(getContainerName()));
157:                 final String fileName = getFileName();
158:
159:                 if (getContainerName().length() == 0) {
160:                         updateStatus(Messages.EMFFormsTemplateWizardPage_errorNoContainer);
161:                         return;
162:                 }
163:                 if (container == null
164:                         || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
165:                         updateStatus(Messages.EMFFormsTemplateWizardPage_errorContainerNotExists);
166:                         return;
167:                 }
168:                 if (!container.isAccessible()) {
169:                         updateStatus(Messages.EMFFormsTemplateWizardPage_errorProjectReadOnly);
170:                         return;
171:                 }
172:                 if (fileName.length() == 0) {
173:                         updateStatus(Messages.EMFFormsTemplateWizardPage_errorNoFilename);
174:                         return;
175:                 }
176:                 if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
177:                         updateStatus(Messages.EMFFormsTemplateWizardPage_errorInvalidFilename);
178:                         return;
179:                 }
180:
181:                 if (ResourcesPlugin.getWorkspace().getRoot().getFile(container.getFullPath().append(fileName)).exists()) {
182:                         updateStatus(String.format(Messages.EMFFormsTemplateWizardPage_FileAlreadyExist, fileName));
183:                         return;
184:                 }
185:                 final int dotLoc = fileName.lastIndexOf('.');
186:                 if (dotLoc != -1) {
187:                         final String ext = fileName.substring(dotLoc + 1);
188:                         if (!ext.equalsIgnoreCase("template")) { //$NON-NLS-1$
189:                                 updateStatus(String.format(Messages.EMFFormsTemplateWizardPage_errorWrongFileExtension, "template")); //$NON-NLS-1$
190:                                 return;
191:                         }
192:                 }
193:                 updateStatus(null);
194:         }
195:
196:         private void updateStatus(String message) {
197:                 setErrorMessage(message);
198:                 setPageComplete(message == null);
199:         }
200:
201:         /**
202:          * The container name.
203:          *
204:          * @return the name of the container
205:          */
206:         public String getContainerName() {
207:                 return containerText.getText();
208:         }
209:
210:         /**
211:          * The file name.
212:          *
213:          * @return the name of the file
214:          */
215:         public String getFileName() {
216:                 return fileText.getText();
217:         }
218: }