Skip to content

Commit ebc7c4a

Browse files
author
jantje
committed
Fixed exception with regex and system.separator
Also the "old values" were not properly populated so I fixed that to
1 parent 88e8d30 commit ebc7c4a

File tree

1 file changed

+33
-53
lines changed

1 file changed

+33
-53
lines changed

io.sloeber.ui/src/io/sloeber/ui/project/properties/BoardSelectionPage.java

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package io.sloeber.ui.project.properties;
22

33
import java.io.File;
4-
import java.util.ArrayList;
54
import java.util.HashMap;
65
import java.util.Map;
7-
import java.util.StringJoiner;
86
import java.util.TreeMap;
97

108
import org.eclipse.cdt.core.parser.util.ArrayUtil;
@@ -13,7 +11,9 @@
1311
import org.eclipse.cdt.ui.newui.AbstractCPropertyTab;
1412
import org.eclipse.cdt.ui.newui.AbstractPage;
1513
import org.eclipse.cdt.ui.newui.ICPropertyProvider;
14+
import org.eclipse.core.runtime.IPath;
1615
import org.eclipse.core.runtime.IStatus;
16+
import org.eclipse.core.runtime.Path;
1717
import org.eclipse.core.runtime.Status;
1818
import org.eclipse.jface.dialogs.Dialog;
1919
import org.eclipse.swt.SWT;
@@ -130,11 +130,9 @@ public void handleEvent(Event e) {
130130
}
131131
};
132132

133-
private int mNumBoardsFiles;
134-
135133
private Composite mComposite;
136134

137-
private String[] mAllBoardsFileNames;
135+
private TreeMap<String, String> mAllBoardsFiles = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
138136

139137
@Override
140138
public void createControls(Composite parent, ICPropertyProvider provider) {
@@ -167,39 +165,36 @@ public void draw(Composite composite) {
167165
this.myBoardID = BoardDescriptor.makeBoardDescriptor(getConfdesc());
168166
}
169167
ICConfigurationDescription confdesc = getConfdesc();
168+
169+
String[] allBoardsFileNames = BoardsManager.getAllBoardsFiles();
170+
for (String curBoardFile : allBoardsFileNames) {
171+
this.mAllBoardsFiles.put(tidyUpLength(curBoardFile), curBoardFile);
172+
}
173+
170174
this.mComposite = composite;
171175

172176
GridLayout theGridLayout = new GridLayout();
173177
theGridLayout.numColumns = this.ncol;
174178
composite.setLayout(theGridLayout);
175179

176180
GridData theGriddata;
177-
this.mAllBoardsFileNames = BoardsManager.getAllBoardsFiles();
178-
this.mNumBoardsFiles = this.mAllBoardsFileNames.length;
179-
if (this.mAllBoardsFileNames.length == 0) {
180-
Activator.log(new Status(IStatus.ERROR, Activator.getId(),
181-
"ArduinoHelpers.getBoardsFiles() returns null.\nThis should not happen.\nIt looks like the download of the boards failed.")); //$NON-NLS-1$
182-
}
183181

184-
switch (this.mAllBoardsFileNames.length) {
185-
case 0:
182+
if (this.mAllBoardsFiles.isEmpty()) {
183+
186184
Activator.log(new Status(IStatus.ERROR, Activator.getId(), Messages.error_no_platform_files_found, null));
187-
break;
188-
default: {
189-
// create a combo to select the boards
190-
createLabel(composite, this.ncol, "The boards.txt file you want to use"); //$NON-NLS-1$
191-
new Label(composite, SWT.NONE).setText("Boards.txt file:"); //$NON-NLS-1$
192185
}
193186

194-
}
187+
// create a combo to select the boards
188+
createLabel(composite, this.ncol, "The platform you want to use"); //$NON-NLS-1$
189+
new Label(composite, SWT.NONE).setText("Platform folder:"); //$NON-NLS-1$
195190

196191
this.mControlBoardsTxtFile = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
197192
theGriddata = new GridData();
198193
theGriddata.horizontalAlignment = SWT.FILL;
199194
theGriddata.horizontalSpan = (this.ncol - 1);
200195
this.mControlBoardsTxtFile.setLayoutData(theGriddata);
201196
this.mControlBoardsTxtFile.setEnabled(false);
202-
this.mControlBoardsTxtFile.setItems(tidyUpLength(this.mAllBoardsFileNames));
197+
this.mControlBoardsTxtFile.setItems(this.mAllBoardsFiles.keySet().toArray(new String[0]));
203198

204199
createLine(composite, this.ncol);
205200
// -------
@@ -252,43 +247,25 @@ public void draw(Composite composite) {
252247
this.mFeedbackControl.setLayoutData(theGriddata);
253248
// End of special controls
254249

255-
this.mControlBoardsTxtFile.select(0);
256-
this.boardFileModifyListener.handleEvent(null);
257250
setValues(confdesc);
258251

259252
this.mcontrolBoardName.addListener(SWT.Modify, this.boardModifyListener);
260253
this.mControlBoardsTxtFile.addListener(SWT.Modify, this.boardFileModifyListener);
261254

262-
// force update to first
263-
264255
enableControls();
265256
Dialog.applyDialogFont(composite);
266257
}
267258

268-
private static String[] tidyUpLength(String[] pLongNames) {
269-
final String separator = "/"; //$NON-NLS-1$
270-
ArrayList<String> shortNames = new ArrayList<>();
271-
for (String longName : pLongNames) {
272-
String[] pathParts = longName.split(separator);
273-
if (pathParts.length > 10) {
274-
StringJoiner sj = new StringJoiner(separator);
275-
sj.add(pathParts[0]);
276-
sj.add(pathParts[1]);
277-
sj.add(pathParts[2]);
278-
sj.add(pathParts[3]);
279-
sj.add("..."); //$NON-NLS-1$
280-
sj.add(pathParts[pathParts.length - 5]);
281-
sj.add(pathParts[pathParts.length - 4]);
282-
sj.add(pathParts[pathParts.length - 3]);
283-
sj.add(pathParts[pathParts.length - 2]);
284-
sj.add(pathParts[pathParts.length - 1]);
285-
shortNames.add(sj.toString());
286-
} else {
287-
shortNames.add(longName);
288-
}
259+
private static String tidyUpLength(String longName) {
260+
IPath longPath = new Path(longName).removeLastSegments(1);
261+
IPath tidyPath = longPath;
262+
int segments = longPath.segmentCount();
263+
if (segments > 7) {
264+
tidyPath = longPath.removeLastSegments(segments - 2);
265+
tidyPath = tidyPath.append("..."); //$NON-NLS-1$
266+
tidyPath = tidyPath.append(longPath.removeFirstSegments(segments - 4));
289267
}
290-
291-
return shortNames.toArray(new String[0]);
268+
return tidyPath.toString();
292269
}
293270

294271
public boolean isPageComplete() {
@@ -320,7 +297,7 @@ protected void enableControls() {
320297
this.mcontrolBoardName.setEnabled(true);
321298
this.mControlUploadPort.setEnabled(true);
322299
this.mControlUploadProtocol.setEnabled(true);
323-
this.mControlBoardsTxtFile.setEnabled((this.mNumBoardsFiles > 1));
300+
this.mControlBoardsTxtFile.setEnabled(true);
324301
for (LabelCombo curLabelCombo : this.mBoardOptionCombos) {
325302
curLabelCombo.setVisible(true);
326303
}
@@ -356,8 +333,8 @@ protected void updateButtons() {
356333

357334
private void setValues(ICConfigurationDescription confdesc) {
358335

359-
this.mControlBoardsTxtFile.setText(this.myBoardID.getBoardsFile());
360-
// // if no boards file is selected select the first
336+
this.mControlBoardsTxtFile.setText(tidyUpLength(this.myBoardID.getBoardsFile()));
337+
// if no boards file is selected select the first
361338
// if (this.mControlBoardsTxtFile.getText().isEmpty()) {
362339
// this.mControlBoardsTxtFile.setText(this.mControlBoardsTxtFile.getItem(0));
363340
// this.myBoardID.setBoardsFile(this.mAllBoardsFiles[0]);
@@ -451,9 +428,12 @@ protected File getSelectedBoardsFile() {
451428
if (this.mControlBoardsTxtFile == null) {
452429
return null;
453430
}
454-
int index = this.mControlBoardsTxtFile.getSelectionIndex();
455-
return new File(this.mAllBoardsFileNames[index]);
456-
// return new File(this.mControlBoardsTxtFile.getText().trim());
431+
String selectedText = this.mControlBoardsTxtFile.getText().trim();
432+
String longText = this.mAllBoardsFiles.get(selectedText);
433+
if (longText == null) {
434+
return null;// this should not happen
435+
}
436+
return new File(longText);
457437
}
458438

459439
private String getUpLoadPort() {

0 commit comments

Comments
 (0)