6
6
import it .baeyens .arduino .tools .ArduinoHelpers ;
7
7
import it .baeyens .arduino .tools .ShouldHaveBeenInCDT ;
8
8
import it .baeyens .arduino .tools .Stream ;
9
+ import it .baeyens .arduino .tools .DiskStream ;
9
10
import it .baeyens .arduino .ui .BuildConfigurationsPage .ConfigurationDescriptor ;
10
11
11
12
import java .io .File ;
13
+ import java .io .InputStream ;
12
14
import java .lang .reflect .InvocationTargetException ;
13
15
import java .net .URI ;
14
16
import java .util .ArrayList ;
57
59
*/
58
60
public class NewArduinoSketchWizard extends Wizard implements INewWizard , IExecutableExtension {
59
61
60
- private WizardNewProjectCreationPage mWizardPage ;
61
- private ArduinoSettingsPage mArduinoPage ;
62
- private BuildConfigurationsPage mBuildCfgPage ;
62
+ private WizardNewProjectCreationPage mWizardPage ; // first page of the dialog
63
+ private SketchTemplatePage mSketchTemplatePage ; // add the folder for the templates
64
+ private ArduinoSettingsPage mArduinoPage ; // add Arduino board and comp port
65
+ private BuildConfigurationsPage mBuildCfgPage ; // build the configuration
63
66
private IConfigurationElement mConfig ;
64
67
private IProject mProject ;
65
68
@@ -68,42 +71,79 @@ public NewArduinoSketchWizard() {
68
71
}
69
72
70
73
@ Override
74
+ /**
75
+ * adds pages to the wizard. We are using the standard project wizard of Eclipse
76
+ */
71
77
public void addPages () {
78
+ //
72
79
// We assume everything is OK as it is tested in the handler
80
+ // create each page and fill in the title and description
81
+ // first page to fill in the project name
82
+ //
73
83
mWizardPage = new WizardNewProjectCreationPage ("New Arduino sketch" );
74
84
mWizardPage .setDescription ("Create a new Arduino sketch." );
75
85
mWizardPage .setTitle ("New Arduino sketch" );
76
-
86
+ //
87
+ // settings for template file location
88
+ //
89
+ mSketchTemplatePage = new SketchTemplatePage ("Sketch Template location" );
90
+ mSketchTemplatePage .setTitle ("Provide the sketch template folder" );
91
+ mSketchTemplatePage .setDescription ("The folder must contain a sketch.cpp and sketch.h" );
92
+ //
93
+ // settings for Arduino board etc
94
+ //
77
95
mArduinoPage = new ArduinoSettingsPage ("Arduino information" );
78
96
mArduinoPage .setTitle ("Provide the Arduino information." );
79
97
mArduinoPage .setDescription ("These settings can be changed later." );
80
-
98
+ //
99
+ // configuration page but I haven't seen it
100
+ //
81
101
mBuildCfgPage = new BuildConfigurationsPage ("Build configurations" );
82
102
mBuildCfgPage .setTitle ("Select additional build configurations for this project." );
83
103
mBuildCfgPage .setDescription ("If you are using additional tools you may want one or more of these extra configurations." );
84
-
104
+ //
105
+ // actually add the pages to the wizard
106
+ ///
85
107
addPage (mWizardPage );
108
+ addPage (mSketchTemplatePage );
86
109
addPage (mArduinoPage );
87
110
addPage (mBuildCfgPage );
88
-
89
111
}
90
-
112
+ /**
113
+ * this method is required by IWizard otherwise nothing will actually happen
114
+ */
91
115
@ Override
92
116
public boolean performFinish () {
93
-
117
+ //
118
+ // if the project is filled in then we are done
119
+ //
94
120
if (mProject != null ) {
95
121
return true ;
96
122
}
97
-
123
+ //
124
+ // get an IProject handle to our project
125
+ //
98
126
final IProject projectHandle = ResourcesPlugin .getWorkspace ().getRoot ().getProject (Common .MakeNameCompileSafe (mWizardPage .getProjectName ()));
127
+ //
128
+ // let's validate it
129
+ //
99
130
try {
100
-
131
+ //
132
+ // get the URL if it is filled in. This depends on the check box "use defaults" is checked
133
+ // or not
134
+ //
101
135
URI projectURI = (!mWizardPage .useDefaults ()) ? mWizardPage .getLocationURI () : null ;
102
-
136
+ //
137
+ // get the workspace name
138
+ //
103
139
IWorkspace workspace = ResourcesPlugin .getWorkspace ();
104
-
140
+ //
141
+ // the project descriptions is set equal to the name of the project
142
+ //
105
143
final IProjectDescription desc = workspace .newProjectDescription (projectHandle .getName ());
106
-
144
+ //
145
+ // get our workspace location
146
+ //
107
147
desc .setLocationURI (projectURI );
108
148
109
149
/*
@@ -112,6 +152,9 @@ public boolean performFinish() {
112
152
WorkspaceModifyOperation op = new WorkspaceModifyOperation () {
113
153
@ Override
114
154
protected void execute (IProgressMonitor monitor ) throws CoreException {
155
+ //
156
+ // actually create the project
157
+ //
115
158
createProject (desc , projectHandle , monitor );
116
159
}
117
160
};
@@ -127,13 +170,18 @@ protected void execute(IProgressMonitor monitor) throws CoreException {
127
170
MessageDialog .openError (getShell (), "Error" , realException .getMessage ());
128
171
return false ;
129
172
}
130
-
173
+ //
174
+ // so the project is created we can start
175
+ //
131
176
mProject = projectHandle ;
132
177
133
178
if (mProject == null ) {
134
179
return false ;
135
180
}
136
-
181
+ //
182
+ // so now we set Eclipse to the right perspective and switch to our just created
183
+ // project
184
+ //
137
185
BasicNewProjectResourceWizard .updatePerspective (mConfig );
138
186
IWorkbenchWindow TheWindow = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ();
139
187
BasicNewResourceWizard .selectAndReveal (mProject , TheWindow );
@@ -179,6 +227,7 @@ void createProject(IProjectDescription description, IProject project, IProgressM
179
227
180
228
for (int i = 0 ; i < cfgNamesAndTCIds .size (); i ++) {
181
229
ICConfigurationDescription configurationDescription = prjDesc .getConfigurationByName (cfgNamesAndTCIds .get (i ).Name );
230
+ mSketchTemplatePage .saveAllSelections (configurationDescription );
182
231
mArduinoPage .saveAllSelections (configurationDescription );
183
232
ArduinoHelpers .setTheEnvironmentVariables (project , configurationDescription , cfgNamesAndTCIds .get (i ).DebugCompilerSettings );
184
233
}
@@ -196,20 +245,45 @@ void createProject(IProjectDescription description, IProject project, IProgressM
196
245
// Insert The Arduino Code
197
246
// NOTE: Not duplicated for debug (the release reference is just to get at some environment variables)
198
247
ArduinoHelpers .addArduinoCodeToProject (project , defaultConfigDescription );
199
-
200
- /* Add the sketch source code file */
201
- ArduinoHelpers . addFileToProject ( container , new Path ( project . getName () + ".cpp" ),
202
- Stream . openContentStream ( project . getName (), "" , "templates/sketch.cpp" ), monitor );
203
-
204
- /* Add the sketch header file * /
248
+ //
249
+ // depending on the the Use Default selection we will either select the default
250
+ // templates or the ones specified in the Template Sketch folder
251
+ //
252
+ // first determine type of include due to Arduino version. Since version 1.0 we use Arduino.h
253
+ //
205
254
String Include = "WProgram.h" ;
206
255
if (ArduinoInstancePreferences .isArduinoIdeOne ()) // Arduino v1.0+
207
256
{
208
257
Include = "Arduino.h" ;
209
258
}
210
- ArduinoHelpers .addFileToProject (container , new Path (project .getName () + ".h" ),
211
- Stream .openContentStream (project .getName (), Include , "templates/sketch.h" ), monitor );
212
-
259
+ //
260
+ // Create the source files (sketch.cpp and sketch.h)
261
+ //
262
+ InputStream cppTemplateFile = null ;
263
+ InputStream hTemplateFile = null ;
264
+
265
+ if (ArduinoInstancePreferences .getLastUsedDefaultSketchSelection () == true ) {
266
+ //
267
+ // we will use the default sketch.cpp and sketch.h
268
+ //
269
+ cppTemplateFile = Stream .openContentStream (project .getName (), "" , "templates/sketch.cpp" );
270
+ hTemplateFile = Stream .openContentStream (project .getName (), Include , "templates/sketch.h" );
271
+ } else {
272
+ //
273
+ // we are using our own created sketch.cpp and sketch.h. We use a different streaming mechanism
274
+ // here. Standard used relative paths (Stream class). I created a different Class (DiskStream) to
275
+ // handle absolute paths
276
+ //
277
+ String folderName = ArduinoInstancePreferences .getLastTemplateFolderName ();
278
+ cppTemplateFile = DiskStream .openContentStream (project .getName (), "" , folderName + "\\ sketch.cpp" );
279
+ hTemplateFile = DiskStream .openContentStream (project .getName (), Include , folderName + "\\ sketch.h" );
280
+ }
281
+ //
282
+ // add both files to the project
283
+ //
284
+ ArduinoHelpers .addFileToProject (container , new Path (project .getName () + ".cpp" ), cppTemplateFile , monitor );
285
+ ArduinoHelpers .addFileToProject (container , new Path (project .getName () + ".h" ), hTemplateFile , monitor );
286
+
213
287
// exclude "Librarie/*/?xample from the build
214
288
// ICSourceEntry[] folder;
215
289
// folder = configurationDescription.getSourceEntries();
0 commit comments