3
3
import java .io .File ;
4
4
import java .net .URI ;
5
5
import java .util .ArrayList ;
6
+ import java .util .List ;
6
7
import java .util .Map ;
7
8
import java .util .Map .Entry ;
8
9
import java .util .Set ;
34
35
import org .eclipse .core .runtime .NullProgressMonitor ;
35
36
import org .eclipse .core .runtime .OperationCanceledException ;
36
37
import org .eclipse .core .runtime .Path ;
38
+ import org .eclipse .core .runtime .Platform ;
37
39
import org .eclipse .core .runtime .Status ;
38
40
import org .eclipse .core .runtime .preferences .IEclipsePreferences ;
39
41
import org .eclipse .core .runtime .preferences .InstanceScope ;
40
42
41
43
import io .sloeber .core .Activator ;
42
44
import io .sloeber .core .InternalBoardDescriptor ;
43
45
import io .sloeber .core .common .Common ;
46
+ import io .sloeber .core .common .ConfigurationPreferences ;
44
47
import io .sloeber .core .common .Const ;
45
48
import io .sloeber .core .tools .Helpers ;
46
49
import io .sloeber .core .tools .Programmers ;
47
50
import io .sloeber .core .tools .ShouldHaveBeenInCDT ;
48
51
import io .sloeber .core .tools .TxtFile ;
49
52
53
+ @ SuppressWarnings ("nls" )
50
54
public class BoardDescriptor {
51
55
56
+ @ Override
57
+ public String toString () {
58
+ return getBoardsFile () + " \" " + getBoardName () + "\" " + getUploadPort (); //$NON-NLS-2$
59
+ }
60
+
52
61
// preference nodes
53
62
public static final String NODE_ARDUINO = Activator .NODE_ARDUINO ;
54
- /**
55
- *
56
- */
57
63
64
+ /*
65
+ * This is the basic info contained in the descriptor
66
+ */
58
67
private String myUploadPort ;
59
68
private String myUploadProtocol ;
60
69
private String myBoardID ;
61
- private String myProjectName = new String ();
62
70
private Map <String , String > myOptions ;
71
+
72
+ /*
73
+ * the following data is stored to detect changes that will make the equal
74
+ * fail so os changes, workspace changes, eclipse install changes will force
75
+ * a update on the stored data
76
+ */
77
+ private String myProjectName = new String ();
78
+ private String myOSName = Platform .getOS ();
79
+ private String myWorkSpaceLocation = Common .getWorkspaceRoot ().toString ();
80
+ private String myWorkEclipseLocation = ConfigurationPreferences .getEclipseHome ().toString ();
81
+
82
+ /*
83
+ * Stuff to make things work
84
+ */
63
85
private File myBoardsFile ;
64
86
protected TxtFile myTxtFile ;
65
87
private ChangeListener myChangeListeners = null ;
66
88
private static final IEclipsePreferences myStorageNode = InstanceScope .INSTANCE .getNode (NODE_ARDUINO );
67
89
68
- private static final String KEY_LAST_USED_BOARD = "Last used Board" ; //$NON-NLS-1$
69
- private static final String KEY_LAST_USED_UPLOAD_PORT = "Last Used Upload port" ; //$NON-NLS-1$
70
- private static final String KEY_LAST_USED_UPLOAD_PROTOCOL = "last Used upload Protocol" ; //$NON-NLS-1$
71
- private static final String KEY_LAST_USED_BOARDS_FILE = "Last used Boards file" ; //$NON-NLS-1$
72
- private static final String KEY_LAST_USED_BOARD_MENU_OPTIONS = "last used Board custom option selections" ; //$NON-NLS-1$
73
- private static final String MENUSELECTION = Const .ENV_KEY_JANTJE_START + "MENU." ; //$NON-NLS-1$
90
+ /*
91
+ * Some constants
92
+ */
93
+ private static final String KEY_LAST_USED_BOARD = "Last used Board" ;
94
+ private static final String KEY_LAST_USED_UPLOAD_PORT = "Last Used Upload port" ;
95
+ private static final String KEY_LAST_USED_UPLOAD_PROTOCOL = "last Used upload Protocol" ;
96
+ private static final String KEY_LAST_USED_BOARDS_FILE = "Last used Boards file" ;
97
+ private static final String KEY_LAST_USED_BOARD_MENU_OPTIONS = "last used Board custom option selections" ;
98
+ private static final String MENUSELECTION = Const .ENV_KEY_JANTJE_START + "MENU." ;
99
+ private static final String ENV_KEY_JANTJE_UPLOAD_PORT = Const .ENV_KEY_JANTJE_START + "COM_PORT" ;
100
+ private static final String ENV_KEY_JANTJE_BOARD_NAME = Const .ENV_KEY_JANTJE_START + "BOARD_NAME" ;
101
+ private static final String ENV_KEY_JANTJE_PROJECT_NAME = Const .ENV_KEY_JANTJE_START + "PROJECT_NAME" ;
102
+ private static final String ENV_KEY_JANTJE_OS = Const .ENV_KEY_JANTJE_START + "OS_NAME" ;
103
+ private static final String ENV_KEY_JANTJE_WORKSPACE_LOCATION = Const .ENV_KEY_JANTJE_START + "WORKSPACE_LOCATION" ;
104
+ private static final String ENV_KEY_JANTJE_ECLIPSE_LOCATION = Const .ENV_KEY_JANTJE_START + "ECLIPSE_LOCATION" ;
74
105
106
+ /**
107
+ * Compare 2 descriptors and return true is they are equal. This method
108
+ * detects - OS changes - project name changes - moves of workspace -
109
+ * changed runtine eclipse install
110
+ *
111
+ * @param obj
112
+ * @return true if equal otherwise false
113
+ */
75
114
public boolean equals (BoardDescriptor obj ) {
76
115
if (!this .getUploadPort ().equals (obj .getUploadPort ())) {
77
116
return false ;
@@ -91,13 +130,18 @@ public boolean equals(BoardDescriptor obj) {
91
130
if (!this .getProjectName ().equals (obj .getProjectName ())) {
92
131
return false ;
93
132
}
133
+ if (!this .getMyOSName ().equals (obj .getMyOSName ())) {
134
+ return false ;
135
+ }
136
+ if (!this .getMyWorkEclipseLocation ().equals (obj .getMyWorkEclipseLocation ())) {
137
+ return false ;
138
+ }
139
+ if (!this .getMyWorkSpaceLocation ().equals (obj .getMyWorkSpaceLocation ())) {
140
+ return false ;
141
+ }
94
142
return true ;
95
143
}
96
144
97
- public String getProjectName () {
98
- return this .myProjectName ;
99
- }
100
-
101
145
/*
102
146
* Create a sketchProject. This class does not really create a sketch
103
147
* object. Nor does it look for existing (mapping) sketch projects This
@@ -115,7 +159,6 @@ protected BoardDescriptor() {
115
159
116
160
}
117
161
118
- @ SuppressWarnings ("nls" )
119
162
protected BoardDescriptor (ICConfigurationDescription confdesc ) {
120
163
if (confdesc == null ) {
121
164
this .myBoardsFile = new File (myStorageNode .get (KEY_LAST_USED_BOARDS_FILE , "" ));
@@ -127,14 +170,19 @@ protected BoardDescriptor(ICConfigurationDescription confdesc) {
127
170
menuOptionsFromString (myStorageNode .get (KEY_LAST_USED_BOARD_MENU_OPTIONS , new String ()));
128
171
129
172
} else {
130
- this .myUploadPort = Common .getBuildEnvironmentVariable (confdesc , Const . ENV_KEY_JANTJE_UPLOAD_PORT , "" );
173
+ this .myUploadPort = Common .getBuildEnvironmentVariable (confdesc , ENV_KEY_JANTJE_UPLOAD_PORT , "" );
131
174
this .myUploadProtocol = Common .getBuildEnvironmentVariable (confdesc ,
132
175
Common .get_Jantje_KEY_PROTOCOL (Const .ACTION_UPLOAD ), "" );
133
176
this .myBoardsFile = new File (
134
177
Common .getBuildEnvironmentVariable (confdesc , Const .ENV_KEY_JANTJE_BOARDS_FILE , "" ));
135
178
this .myBoardID = Common .getBuildEnvironmentVariable (confdesc , Const .ENV_KEY_JANTJE_BOARD_ID , "" );
136
- this .myProjectName = Common .getBuildEnvironmentVariable (confdesc , Const . ENV_KEY_JANTJE_PROJECT_NAME , "" );
179
+ this .myProjectName = Common .getBuildEnvironmentVariable (confdesc , ENV_KEY_JANTJE_PROJECT_NAME , "" );
137
180
this .myTxtFile = new TxtFile (this .myBoardsFile );
181
+ this .myOSName = Common .getBuildEnvironmentVariable (confdesc , ENV_KEY_JANTJE_OS , "" );
182
+ this .myWorkSpaceLocation = Common .getBuildEnvironmentVariable (confdesc , ENV_KEY_JANTJE_WORKSPACE_LOCATION ,
183
+ "" );
184
+ this .myWorkEclipseLocation = Common .getBuildEnvironmentVariable (confdesc , ENV_KEY_JANTJE_ECLIPSE_LOCATION ,
185
+ "" );
138
186
139
187
this .myOptions = new TreeMap <>(String .CASE_INSENSITIVE_ORDER );
140
188
IEnvironmentVariableManager envManager = CCorePlugin .getDefault ().getBuildEnvironmentManager ();
@@ -152,14 +200,53 @@ public static BoardDescriptor makeBoardDescriptor(File boardsFile, String boardI
152
200
return new InternalBoardDescriptor (boardsFile , boardID , options );
153
201
}
154
202
203
+ /**
204
+ * make a board descriptor for each board in the board.txt file with the
205
+ * default options
206
+ *
207
+ * @param boardFile
208
+ * @return a list of board descriptors
209
+ */
210
+ public static List <BoardDescriptor > makeBoardDescriptors (File boardFile ) {
211
+ TxtFile txtFile = new TxtFile (boardFile );
212
+ List <BoardDescriptor > boards = new ArrayList <>();
213
+ for (String curboardName : txtFile .getAllNames ()) {
214
+ Map <String , String > boardSection = txtFile .getSection (curboardName );
215
+ if (!"true" .equalsIgnoreCase (boardSection .get ("hide" ))) {
216
+ boards .add (makeBoardDescriptor (boardFile , txtFile .getBoardIDFromBoardName (curboardName ), null ));
217
+ }
218
+ }
219
+ return boards ;
220
+ }
221
+
222
+ /**
223
+ * create a board descriptor
224
+ *
225
+ * @param boardsFile
226
+ * @param boardID
227
+ * @param options
228
+ * if null default options are taken
229
+ */
155
230
protected BoardDescriptor (File boardsFile , String boardID , Map <String , String > options ) {
156
- this .myUploadPort = Const . EMPTY_STRING ;
231
+ this .myUploadPort = new String () ;
157
232
this .myUploadProtocol = Defaults .getDefaultUploadProtocol ();
158
233
this .myBoardID = boardID ;
159
234
this .myOptions = new TreeMap <>(String .CASE_INSENSITIVE_ORDER );
160
- this .myOptions .putAll (options );
161
235
this .myBoardsFile = boardsFile ;
162
236
this .myTxtFile = new TxtFile (this .myBoardsFile );
237
+ if (options != null ) {
238
+ this .myOptions .putAll (options );
239
+ } else {
240
+ TreeMap <String , String > allOptions = this .myTxtFile .getMenus ();
241
+ for (Map .Entry <String , String > curoption : allOptions .entrySet ()) {
242
+ if (!this .myOptions .containsKey (curoption .getKey ())) {
243
+ String [] menuOptions = this .myTxtFile .getMenuItemIDsFromMenuID (curoption .getKey (), boardID );
244
+ if (menuOptions .length > 0 ) {
245
+ this .myOptions .put (curoption .getKey (), menuOptions [0 ]);
246
+ }
247
+ }
248
+ }
249
+ }
163
250
}
164
251
165
252
/**
@@ -177,9 +264,8 @@ public boolean configureProject(IProject project, IProgressMonitor monitor) {
177
264
// prjCDesc.setActiveConfiguration(configurationDescription);
178
265
CoreModel .getDefault ().getProjectDescriptionManager ().setProjectDescription (project , prjCDesc , true , null );
179
266
} catch (Exception e ) {
180
- // TODO Auto-generated catch block
181
267
e .printStackTrace ();
182
- Common .log (new Status (IStatus .ERROR , io .sloeber .core .Activator .getId (), "failed to save the board settings" , //$NON-NLS-1$
268
+ Common .log (new Status (IStatus .ERROR , io .sloeber .core .Activator .getId (), "failed to save the board settings" ,
183
269
e ));
184
270
return false ;
185
271
}
@@ -235,19 +321,18 @@ public IProject createProject(String projectName, URI projectURI,
235
321
ICConfigurationDescription defaultConfigDescription = prjCDesc
236
322
.getConfigurationByName (cfgNamesAndTCIds .get (0 ).configName );
237
323
238
- ICResourceDescription cfgd = defaultConfigDescription .getResourceDescription (new Path (Const .EMPTY_STRING ),
239
- true );
324
+ ICResourceDescription cfgd = defaultConfigDescription .getResourceDescription (new Path (new String ()), true );
240
325
ICExclusionPatternPathEntry [] entries = cfgd .getConfiguration ().getSourceEntries ();
241
326
if (entries .length == 1 ) {
242
327
Path exclusionPath [] = new Path [8 ];
243
- exclusionPath [0 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/**/?xamples/**" ); //$NON-NLS-1$
244
- exclusionPath [1 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/**/?xtras/**" ); //$NON-NLS-1$
245
- exclusionPath [2 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/**/test*/**" ); //$NON-NLS-1$
246
- exclusionPath [3 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/**/third-party/**" ); //$NON-NLS-1$
247
- exclusionPath [4 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "**/._*" ); //$NON-NLS-1$
248
- exclusionPath [5 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/c*/?*" ); //$NON-NLS-1$
249
- exclusionPath [6 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/d*/?*" ); //$NON-NLS-1$
250
- exclusionPath [7 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/D*/?*" ); //$NON-NLS-1$
328
+ exclusionPath [0 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/**/?xamples/**" );
329
+ exclusionPath [1 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/**/?xtras/**" );
330
+ exclusionPath [2 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/**/test*/**" );
331
+ exclusionPath [3 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/**/third-party/**" );
332
+ exclusionPath [4 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "**/._*" );
333
+ exclusionPath [5 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/c*/?*" );
334
+ exclusionPath [6 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/d*/?*" );
335
+ exclusionPath [7 ] = new Path (Const .LIBRARY_PATH_SUFFIX + "/?*/D*/?*" );
251
336
252
337
ICExclusionPatternPathEntry newSourceEntry = new CSourceEntry (entries [0 ].getFullPath (), exclusionPath ,
253
338
ICSettingEntry .VALUE_WORKSPACE_PATH );
@@ -275,7 +360,7 @@ public IProject createProject(String projectName, URI projectURI,
275
360
}
276
361
277
362
public void save (ICConfigurationDescription confdesc ) throws Exception {
278
- saveConfiguration (confdesc );
363
+ saveConfiguration (confdesc , null );
279
364
if (confdesc != null ) {
280
365
IProject project = confdesc .getProjectDescription ().getProject ();
281
366
@@ -289,25 +374,38 @@ public void save(ICConfigurationDescription confdesc) throws Exception {
289
374
}
290
375
291
376
public void saveConfiguration () {
292
- saveConfiguration (null );
377
+ saveConfiguration (null , null );
293
378
}
294
379
295
- public void saveConfiguration (ICConfigurationDescription confdesc ) {
296
- if (confdesc != null ) {
297
-
298
- Common .setBuildEnvironmentVariable (confdesc , Const .ENV_KEY_JANTJE_PLATFORM_FILE , getPlatformFile ());
299
- Common .setBuildEnvironmentVariable (confdesc , "JANTJE.SELECTED.PLATFORM" , getPlatformPath ().toString ()); //$NON-NLS-1$
300
- Common .setBuildEnvironmentVariable (confdesc , Const .ENV_KEY_JANTJE_BOARD_NAME , getBoardName ());
301
- Common .setBuildEnvironmentVariable (confdesc , Const .ENV_KEY_JANTJE_BOARDS_FILE , getBoardsFile ());
302
- Common .setBuildEnvironmentVariable (confdesc , Const .ENV_KEY_JANTJE_BOARD_ID , this .myBoardID );
303
- Common .setBuildEnvironmentVariable (confdesc , Const .ENV_KEY_JANTJE_ARCITECTURE_ID , getArchitecture ());
304
- Common .setBuildEnvironmentVariable (confdesc , Const .ENV_KEY_JANTJE_PACKAGE_ID , getPackage ());
305
- Common .setBuildEnvironmentVariable (confdesc , Const .ENV_KEY_JANTJE_UPLOAD_PORT , this .myUploadPort );
306
- Common .setBuildEnvironmentVariable (confdesc , Common .get_Jantje_KEY_PROTOCOL (Const .ACTION_UPLOAD ),
380
+ public void saveConfiguration (ICConfigurationDescription confDesc , IContributedEnvironment contribEnvIn ) {
381
+ if (confDesc != null ) {
382
+ IContributedEnvironment contribEnv = contribEnvIn ;
383
+ if (contribEnv == null ) {
384
+ IEnvironmentVariableManager envManager = CCorePlugin .getDefault ().getBuildEnvironmentManager ();
385
+ contribEnv = envManager .getContributedEnvironment ();
386
+ }
387
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , Const .ENV_KEY_JANTJE_PLATFORM_FILE ,
388
+ getPlatformFile ());
389
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , "JANTJE.SELECTED.PLATFORM" ,
390
+ getPlatformPath ().toString ());
391
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , ENV_KEY_JANTJE_BOARD_NAME , getBoardName ());
392
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , Const .ENV_KEY_JANTJE_BOARDS_FILE , getBoardsFile ());
393
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , Const .ENV_KEY_JANTJE_BOARD_ID , this .myBoardID );
394
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , Const .ENV_KEY_JANTJE_ARCITECTURE_ID ,
395
+ getArchitecture ());
396
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , Const .ENV_KEY_JANTJE_PACKAGE_ID , getPackage ());
397
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , ENV_KEY_JANTJE_UPLOAD_PORT , this .myUploadPort );
398
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , ENV_KEY_JANTJE_PROJECT_NAME , getProjectName ());
399
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , ENV_KEY_JANTJE_OS , this .myOSName );
400
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , ENV_KEY_JANTJE_WORKSPACE_LOCATION ,
401
+ this .myWorkSpaceLocation );
402
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , ENV_KEY_JANTJE_ECLIPSE_LOCATION ,
403
+ this .myWorkEclipseLocation );
404
+ Common .setBuildEnvironmentVariable (confDesc , Common .get_Jantje_KEY_PROTOCOL (Const .ACTION_UPLOAD ),
307
405
this .myUploadProtocol );
308
406
if (this .myOptions != null ) {
309
407
for (Map .Entry <String , String > curoption : this .myOptions .entrySet ()) {
310
- Common .setBuildEnvironmentVariable (confdesc , MENUSELECTION + curoption .getKey (),
408
+ Common .setBuildEnvironmentVariable (contribEnv , confDesc , MENUSELECTION + curoption .getKey (),
311
409
curoption .getValue ());
312
410
}
313
411
}
@@ -468,28 +566,43 @@ public String getMenuIdFromMenuName(String menuName) {
468
566
return this .myTxtFile .getMenuIDFromMenuName (menuName );
469
567
}
470
568
569
+ public String getMenuNameFromMenuID (String id ) {
570
+ return this .myTxtFile .getMenuNameFromID (id );
571
+ }
572
+
471
573
public String getMenuItemNamedFromMenuItemID (String menuItemID , String menuID ) {
472
574
return this .myTxtFile .getMenuItemNameFromMenuItemID (this .myBoardID , menuID , menuItemID );
473
575
}
474
576
577
+ /**
578
+ * convert the options to a string so it can be stored
579
+ *
580
+ * @return a string representation of the options
581
+ */
475
582
private String menuOptionsToString () {
476
583
String ret = new String ();
477
584
String concat = new String ();
478
585
if (this .myOptions != null ) {
479
586
for (Entry <String , String > curOption : this .myOptions .entrySet ()) {
480
587
ret += concat + curOption .getKey () + '=' + curOption .getValue ();
481
- concat = "\n " ; //$NON-NLS-1$
588
+ concat = "\n " ;
482
589
}
483
590
}
484
591
return ret ;
485
592
}
486
593
594
+ /**
595
+ * convert a string to a options so it can be read from a string based
596
+ * storage
597
+ *
598
+ * @param options
599
+ */
487
600
private void menuOptionsFromString (String options ) {
488
601
this .myOptions = new TreeMap <>(String .CASE_INSENSITIVE_ORDER );
489
602
if (options != null ) {
490
- String [] lines = options .split ("\n " ); //$NON-NLS-1$
603
+ String [] lines = options .split ("\n " );
491
604
for (String curLine : lines ) {
492
- String [] values = curLine .split ("=" , 2 ); //$NON-NLS-1$
605
+ String [] values = curLine .split ("=" , 2 );
493
606
if (values .length == 2 ) {
494
607
this .myOptions .put (values [0 ], values [1 ]);
495
608
}
@@ -501,4 +614,28 @@ public String getMenuItemIDFromMenuItemName(String menuItemName, String menuID)
501
614
return this .myTxtFile .getMenuItemIDFromMenuItemName (this .myBoardID , menuID , menuItemName );
502
615
}
503
616
617
+ public static String getUploadPort (IProject project ) {
618
+ return Common .getBuildEnvironmentVariable (project , ENV_KEY_JANTJE_UPLOAD_PORT , new String ());
619
+ }
620
+
621
+ public static void storeUploadPort (IProject project , String uploadPort ) {
622
+ Common .setBuildEnvironmentVariable (project , ENV_KEY_JANTJE_UPLOAD_PORT , uploadPort );
623
+ }
624
+
625
+ public String getMyOSName () {
626
+ return this .myOSName ;
627
+ }
628
+
629
+ public String getMyWorkSpaceLocation () {
630
+ return this .myWorkSpaceLocation ;
631
+ }
632
+
633
+ public String getMyWorkEclipseLocation () {
634
+ return this .myWorkEclipseLocation ;
635
+ }
636
+
637
+ public String getProjectName () {
638
+ return this .myProjectName ;
639
+ }
640
+
504
641
}
0 commit comments