Add property page that allows to add/remove TeaVM project nature

This commit is contained in:
konsoletyper 2014-09-14 00:45:10 +04:00
parent 58606d8aa6
commit 8351c48e25
6 changed files with 197 additions and 4 deletions

View File

@ -1,7 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: TeaVM plugin for Eclipse
Bundle-SymbolicName: teavm-eclipse-plugin;singleton:=true
Bundle-SymbolicName: org.teavm.eclipse;singleton:=true
Bundle-Version: 0.2.0.qualifer
Bundle-Vendor: Alexey Andreev <konsoletyper@gmail.com>
Bundle-RequiredExecutionEnvironment: JavaSE-1.7

View File

@ -45,4 +45,16 @@
id="org.teavm.eclipse.debugger">
</debugModelPresentation>
</extension>
<extension point="org.eclipse.core.resources.natures" id="nature" name="TeaVM nature">
<runtime>
<run class="org.teavm.eclipse.TeaVMProjectNature"/>
</runtime>
</extension>
<extension point="org.eclipse.ui.propertyPages">
<page id="org.teavm.eclipse.projectProperties" name="TeaVM"
objectClass="org.eclipse.core.resources.IProject"
class="org.teavm.eclipse.ui.TeaVMProjectPropertyPage">
</page>
</extension>
</plugin>

View File

@ -22,10 +22,13 @@ import org.eclipse.core.runtime.Plugin;
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TeaVMEclipsePlugin extends Plugin {
public static final String ID = "org.teavm.eclipse";
public static final String NATURE_ID = "org.teavm.eclipse.nature";
private static TeaVMEclipsePlugin defaultInstance;
static {
System.out.println();
}
public TeaVMEclipsePlugin() {
@ -35,6 +38,4 @@ public class TeaVMEclipsePlugin extends Plugin {
public static TeaVMEclipsePlugin getDefault() {
return defaultInstance;
}
public static final String ID = "org.teavm.eclipse";
}

View File

@ -0,0 +1,31 @@
package org.teavm.eclipse;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TeaVMProjectNature implements IProjectNature {
private IProject project;
@Override
public void configure() throws CoreException {
}
@Override
public void deconfigure() throws CoreException {
}
@Override
public IProject getProject() {
return project;
}
@Override
public void setProject(IProject project) {
this.project = project;
}
}

View File

@ -0,0 +1,28 @@
package org.teavm.eclipse.ui;
import org.eclipse.core.resources.IProject;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.ui.dialogs.PropertyPage;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TeaVMProjectPropertyPage extends PropertyPage implements IWorkbenchPropertyPage {
private TeaVMProjectPropertyWidget widget;
@Override
protected Control createContents(Composite parent) {
widget = new TeaVMProjectPropertyWidget(parent);
widget.load((IProject)getElement().getAdapter(IProject.class));
return widget;
}
@Override
public boolean performOk() {
widget.save((IProject)getElement().getAdapter(IProject.class));
return super.performOk();
}
}

View File

@ -0,0 +1,121 @@
package org.teavm.eclipse.ui;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.teavm.eclipse.TeaVMEclipsePlugin;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TeaVMProjectPropertyWidget extends Composite {
private Button natureButton;
public TeaVMProjectPropertyWidget(Composite parent) {
super(parent, SWT.NONE);
GridLayout layout = new GridLayout(1, false);
layout.verticalSpacing = 12;
layout.marginWidth = 12;
setLayout(layout);
natureButton = new Button(this, SWT.CHECK);
natureButton.setText("TeaVM build enabled");
}
public void load(IProject project) {
try {
natureButton.setSelection(project.hasNature(TeaVMEclipsePlugin.NATURE_ID));
} catch (CoreException e) {
reportError(e);
}
}
private void reportError(Throwable e) {
e.printStackTrace();
Status status = new Status(Status.ERROR, TeaVMEclipsePlugin.ID, getToolTipText(), e);
ErrorDialog.openError(getShell(), "Error occured", "Error occured", status);
}
public boolean save(IProject project) {
try {
if (natureButton.getSelection()) {
if (!project.hasNature(TeaVMEclipsePlugin.NATURE_ID)) {
addNature(project);
}
} else {
if (project.hasNature(TeaVMEclipsePlugin.NATURE_ID)) {
removeNature(project);
}
}
return true;
} catch (CoreException e) {
reportError(e);
return false;
}
}
private void addNature(final IProject project) {
ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(getShell());
try {
progressDialog.run(false, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
IProjectDescription projectDescription = project.getDescription();
String[] natureIds = projectDescription.getNatureIds();
natureIds = Arrays.copyOf(natureIds, natureIds.length + 1);
natureIds[natureIds.length - 1] = TeaVMEclipsePlugin.NATURE_ID;
projectDescription.setNatureIds(natureIds);
project.setDescription(projectDescription, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InterruptedException e) {
reportError(e);
} catch (InvocationTargetException e) {
reportError(e.getTargetException());
}
}
private void removeNature(final IProject project) {
ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(getShell());
try {
progressDialog.run(false, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
String[] natureIds = project.getDescription().getNatureIds();
String[] newNatureIds = new String[natureIds.length - 1];
for (int i = 0; i < natureIds.length; ++i) {
if (natureIds.equals(TeaVMEclipsePlugin.NATURE_ID)) {
System.arraycopy(natureIds, 0, newNatureIds, 0, i - 1);
System.arraycopy(natureIds, i + 1, newNatureIds, i, newNatureIds.length - i);
project.getDescription().setNatureIds(newNatureIds);
break;
}
}
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InterruptedException e) {
reportError(e);
} catch (InvocationTargetException e) {
reportError(e.getTargetException());
}
}
}