Skip to content

Commit 67f895e

Browse files
author
jantje
committed
#1163 simplify the threading
use WorkspaceModifyOperation instead of WorkspaceModifyOperation and ICoreRunnable together
1 parent e4bb01e commit 67f895e

File tree

2 files changed

+27
-49
lines changed

2 files changed

+27
-49
lines changed

io.sloeber.core/src/io/sloeber/core/api/BoardDescriptor.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sloeber.core.api;
22

33
import java.io.File;
4+
import java.lang.reflect.InvocationTargetException;
45
import java.net.URI;
56
import java.util.ArrayList;
67
import java.util.List;
@@ -39,8 +40,13 @@
3940
import org.eclipse.core.runtime.Path;
4041
import org.eclipse.core.runtime.Platform;
4142
import org.eclipse.core.runtime.Status;
43+
import org.eclipse.core.runtime.SubMonitor;
44+
import org.eclipse.core.runtime.jobs.IJobManager;
45+
import org.eclipse.core.runtime.jobs.Job;
4246
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
4347
import org.eclipse.core.runtime.preferences.InstanceScope;
48+
import org.eclipse.ui.PlatformUI;
49+
import org.eclipse.ui.actions.WorkspaceModifyOperation;
4450

4551
import io.sloeber.core.Activator;
4652
import io.sloeber.core.InternalBoardDescriptor;
@@ -573,18 +579,16 @@ public boolean configureProject(IProject project, IProgressMonitor monitor) {
573579
* Method to create a project based on the board
574580
*/
575581
public IProject createProject(String projectName, URI projectURI, CodeDescriptor codeDescription,
576-
CompileOptions compileOptions, IProgressMonitor monitor) throws Exception {
582+
CompileOptions compileOptions, IProgressMonitor monitor) {
577583

578584
String realProjectName = Common.MakeNameCompileSafe(projectName);
579585

580586
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
581587
final IProject newProjectHandle = root.getProject(realProjectName);
582588
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
583-
584-
ICoreRunnable runnable = new ICoreRunnable() {
589+
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
585590
@Override
586-
public void run(IProgressMonitor internalMonitor) throws CoreException {
587-
591+
protected void execute(IProgressMonitor internalMonitor) throws CoreException {
588592
try {
589593
// Create the base project
590594
IWorkspaceDescription workspaceDesc = workspace.getDescription();
@@ -636,32 +640,28 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
636640
compileOptions.save(curConfig);
637641
save(curConfig);
638642
}
643+
SubMonitor refreshMonitor = SubMonitor.convert(internalMonitor, 3);
644+
newProjectHandle.open(refreshMonitor);
645+
newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, refreshMonitor);
639646
cCorePlugin.setProjectDescription(newProjectHandle, prjCDesc, true, null);
640-
641-
ManagedBuildManager.getBuildInfo(newProjectHandle).setValid(true);
642647
} catch (Exception e) {
643648
Common.log(new Status(IStatus.INFO, io.sloeber.core.Activator.getId(),
644649
"Project creation failed: " + newProjectHandle.getName(), e)); //$NON-NLS-1$
645650
}
646-
651+
Common.log(new Status(Const.SLOEBER_STATUS_DEBUG, Activator.getId(),"internal creation of project is done: "+newProjectHandle.getName()));
647652
}
648653
};
649654

650655
try {
651-
// turn indexer off
652656
IndexerController.doNotIndex(newProjectHandle);
653-
workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, monitor);
654-
final IProgressMonitor refreshMonitor = new NullProgressMonitor();
655-
newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, refreshMonitor);
656-
// refreshMonitor.wait();
657-
IndexerController.Index(newProjectHandle);
658-
} catch (CoreException e2) {
657+
op.run(monitor);
658+
} catch (InvocationTargetException | InterruptedException e) {
659659
Common.log(new Status(IStatus.INFO, io.sloeber.core.Activator.getId(),
660-
"Project creation failed: " + newProjectHandle.getName(),e2)); //$NON-NLS-1$
660+
"Project creation failed: " + newProjectHandle.getName(),e)); //$NON-NLS-1$
661661
}
662662

663663
monitor.done();
664-
664+
IndexerController.Index(newProjectHandle);
665665
return newProjectHandle;
666666
}
667667

io.sloeber.ui/src/io/sloeber/ui/wizard/newsketch/NewSketchWizard.java

+10-32
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.eclipse.core.runtime.IExecutableExtension;
99
import org.eclipse.core.runtime.IProgressMonitor;
1010
import org.eclipse.core.runtime.IStatus;
11+
import org.eclipse.core.runtime.NullProgressMonitor;
1112
import org.eclipse.core.runtime.Status;
1213
import org.eclipse.jface.viewers.IStructuredSelection;
1314
import org.eclipse.jface.wizard.Wizard;
@@ -86,25 +87,20 @@ public boolean performFinish() {
8687
if (this.mProject != null) {
8788
return true;
8889
}
90+
BoardDescriptor boardID = this.mArduinoPage.getBoardID();
91+
CodeDescriptor codeDescription = this.mNewArduinoSketchWizardCodeSelectionPage.getCodeDescription();
92+
CompileOptions compileOptions = new CompileOptions(null);
93+
compileOptions.setEnableParallelBuild(MyPreferences.getEnableParallelBuildForNewProjects());
94+
this.mProject = boardID.createProject(this.mWizardPage.getProjectName(),
95+
(!this.mWizardPage.useDefaults()) ? this.mWizardPage.getLocationURI() : null, codeDescription,
96+
compileOptions, new NullProgressMonitor());
8997

90-
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
91-
@Override
92-
protected void execute(IProgressMonitor monitor) throws CoreException {
93-
createProjectWrapper(monitor);
94-
}
95-
};
96-
97-
try {
98-
getContainer().run(false, true, op);
99-
} catch (InvocationTargetException | InterruptedException e) {
98+
if (this.mProject == null) {
10099
Activator.log(new Status(IStatus.ERROR, Activator.getId(),
101-
Messages.ui_new_sketch_error_failed_to_create_project, e));
100+
Messages.ui_new_sketch_error_failed_to_create_project));
102101
return false;
103102
}
104103

105-
if (this.mProject == null) {
106-
return false;
107-
}
108104

109105
// so now we set Eclipse to the right perspective and switch to our just
110106
// created project
@@ -116,24 +112,6 @@ protected void execute(IProgressMonitor monitor) throws CoreException {
116112
return true;
117113
}
118114

119-
protected void createProjectWrapper(IProgressMonitor monitor) {
120-
121-
BoardDescriptor boardID = this.mArduinoPage.getBoardID();
122-
CodeDescriptor codeDescription = this.mNewArduinoSketchWizardCodeSelectionPage.getCodeDescription();
123-
try {
124-
CompileOptions compileOptions = new CompileOptions(null);
125-
compileOptions.setEnableParallelBuild(MyPreferences.getEnableParallelBuildForNewProjects());
126-
this.mProject = boardID.createProject(this.mWizardPage.getProjectName(),
127-
(!this.mWizardPage.useDefaults()) ? this.mWizardPage.getLocationURI() : null,
128-
codeDescription, compileOptions, monitor);
129-
130-
} catch (Exception e) {
131-
this.mProject = null;
132-
Activator.log(new Status(IStatus.ERROR, Activator.getId(),
133-
Messages.ui_new_sketch_error_failed_to_create_project, e));
134-
}
135-
}
136-
137115
@Override
138116
public void init(IWorkbench workbench, IStructuredSelection selection) {
139117
// nothing to do here

0 commit comments

Comments
 (0)