Package: VdmNewFileWizard

VdmNewFileWizard

nameinstructionbranchcomplexitylinemethod
VdmNewFileWizard()
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
addPages()
M: 31 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
applyTemplate(IFile, String)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
canFinish()
M: 11 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getFileTemplate(String)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
init(IWorkbench, IStructuredSelection)
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%
performFinish()
M: 78 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 26 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.wizard;
23:
24: import java.io.ByteArrayInputStream;
25: import java.io.IOException;
26: import java.io.InputStream;
27:
28: import org.eclipse.core.resources.IFile;
29: import org.eclipse.core.resources.IResource;
30: import org.eclipse.core.runtime.CoreException;
31: import org.eclipse.jface.viewers.IStructuredSelection;
32: import org.eclipse.jface.wizard.Wizard;
33: import org.eclipse.ui.IWorkbench;
34: import org.eclipse.ui.IWorkbenchWizard;
35: import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
36: import org.eclipse.ui.ide.IDE;
37: import org.eclipse.ui.PlatformUI;
38: import org.overture.ide.ui.VdmUIPlugin;
39:
40: public abstract class VdmNewFileWizard extends Wizard implements
41:                 IWorkbenchWizard
42: {
43:
44:         private static final String WIZARD_NAME = "VDM New File Wizard";
45:
46:         private WizardNewFileCreationPage _pageOne;
47:         private String fPageName;
48:         private String fPageTitle;
49:         private String fPageDescription;
50:         private IStructuredSelection fStructuredSelection;
51:
52:         public VdmNewFileWizard()
53:         {
54:                 setWindowTitle(WIZARD_NAME);
55:                 this.fPageName = getPageName();
56:                 this.fPageTitle = getPageTitle();
57:                 this.fPageDescription = getPageDescription();
58:         }
59:
60:         @Override
61:         public void addPages()
62:         {
63:                 super.addPages();
64:                 _pageOne = new WizardNewFileCreationPage(this.fPageName, this.fStructuredSelection);
65:                 _pageOne.setFileExtension(getFileExtension());
66:                 _pageOne.setTitle(this.fPageTitle);
67:                 _pageOne.setDescription(this.fPageDescription);
68:
69:                 addPage(_pageOne);
70:
71:         }
72:
73:         /*
74:          * Gets the main page name
75:          */
76:         protected abstract String getPageName();
77:
78:         /*
79:          * Gets the main page title to be displayed
80:          */
81:         protected abstract String getPageTitle();
82:
83:         /*
84:          * Gets the main page description
85:          */
86:         protected abstract String getPageDescription();
87:
88:         /*
89:          * Gets the file extension of the file to create
90:          */
91:         protected abstract String getFileExtension();
92:
93:         /*
94:          * Gets the file template or null if none is provided
95:          */
96:         protected String getFileTemplate(String fileName)
97:         {
98:                 return null;
99:         }
100:
101:         @Override
102:         public boolean canFinish()
103:         {
104:•                return super.canFinish() && _pageOne.getErrorMessage() == null;
105:         }
106:
107:         @Override
108:         public boolean performFinish()
109:         {
110:                 IFile file = _pageOne.createNewFile();
111:•                if (file.exists())
112:                 {
113:                         String fileName = file.getName();
114:•                        if (fileName.contains("."))
115:                         {
116:                                 fileName = fileName.substring(0, fileName.indexOf("."));
117:                         }
118:
119:                         boolean isClean = false;
120:                         InputStream in =null;
121:                         try
122:                         {
123:                                 in= file.getContents();
124:•                                if (file.getContents().read() == -1)
125:                                 {
126:                                         isClean = true;
127:                                 }
128:                         } catch (IOException e)
129:                         {
130:                         } catch (CoreException e)
131:                         {
132:                         }finally
133:                         {
134:•                                if(in!=null)
135:                                 {
136:                                         try
137:                                         {
138:                                                 in.close();
139:                                         } catch (IOException e)
140:                                         {
141:                                         }
142:                                 }
143:                         }
144:
145:•                        if (isClean)
146:                         {
147:                                 String fileTemplate = getFileTemplate(fileName);
148:•                                if (fileTemplate != null)
149:                                 {
150:                                         applyTemplate(file, fileTemplate);
151:                                 }
152:                         }
153:
154:                 }
155:                 try
156:                 {
157:                         IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file, true);
158:                         file.touch(null);
159:                         file.refreshLocal(IResource.DEPTH_ONE, null);
160:                 } catch (CoreException e)
161:                 {
162:•                        if (VdmUIPlugin.DEBUG)
163:                         {
164:                                 e.printStackTrace();
165:                         }
166:                 }
167:                 return true;
168:         }
169:
170:         private void applyTemplate(IFile file, String fileTemplate)
171:         {
172:                 InputStream stream;
173:                 try
174:                 {
175:                         stream = new ByteArrayInputStream(fileTemplate.getBytes());
176:                         file.setContents(stream, IFile.FORCE, null);
177:                 } catch (CoreException e)
178:                 {
179:•                        if (VdmUIPlugin.DEBUG)
180:                         {
181:                                 e.printStackTrace();
182:                         }
183:
184:                 }
185:
186:         }
187:
188:         public void init(IWorkbench workbench, IStructuredSelection selection)
189:         {
190:                 this.fStructuredSelection = selection;
191:         }
192:
193: }