Skip to content

Commit

Permalink
Add "New Dart file" & "New Dart project" to "New" context menu (#62)
Browse files Browse the repository at this point in the history
closes #6 

Signed-off-by: Lakshminarayana Nekkanti <narayana.nekkanti@gmail.com>
  • Loading branch information
lak-proddev authored and Jonas Hungershausen committed Jun 30, 2019
1 parent b291927 commit cf921e7
Show file tree
Hide file tree
Showing 13 changed files with 602 additions and 29 deletions.
3 changes: 2 additions & 1 deletion org.eclipse.dartboard/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Require-Bundle: org.eclipse.ui.genericeditor;bundle-version="1.0.0",
org.eclipse.ui.console,
ch.qos.logback.classic,
org.eclipse.core.expressions,
org.eclipse.core.resources
org.eclipse.core.resources,
org.eclipse.ui.ide
Import-Package: org.slf4j,
org.slf4j.spi
19 changes: 19 additions & 0 deletions org.eclipse.dartboard/assets/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,22 @@ ProjectNature_DeletePubspec_Message=Do you also want to delete the pubspec.yaml
Launch_NoConfigurationFound_Title=No launch configuration available
Launch_NoConfigurationFound_Body=No launch configuration was found for this project. \
Please create one first.

NewProject_WindowTitle=New Dart Project
NewProject_Title=Dart Project
NewProject_Description=Create a new Dart project.
NewProject_ErrorMessage=Creation Problems
NewProject_InternalError=Internal error: {0}
NewProject_CaseVariantExistsError=The underlying file system is case insensitive. There is an existing project or directory that conflicts with ''{0}''.

NewProject_Group_Label=Dart SDK
NewProject_SDK_Not_Found=Dart SDK not found. Please select a valid SDK location on the preference page.

NewFile_WindowTitle=New Dart File
NewFile_Title=Dart File
NewFile_Description=Create a new Dart file.
NewFile_Creating=Creating file {0}
NewFile_Container_Doesnot_Exist=Container {0} does not exist.
NewFile_OpeningFile=Opening file for editing...

ProjectUtil_NullCheck=Project shouldn't be a null.Unable to configure a dart project
Binary file added org.eclipse.dartboard/icons/dart_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added org.eclipse.dartboard/icons/dart_file_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions org.eclipse.dartboard/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,41 @@
</contextualLaunch>
</shortcut>
</extension>
<extension
point="org.eclipse.ui.newWizards">
<category
id="org.eclipse.dartboard.eclipse.wizards"
name="Dart"></category>
<wizard
category="org.eclipse.dartboard.eclipse.wizards"
class="org.eclipse.dartboard.project.DartProjectWizard"
icon="platform:/plugin/org.eclipse.ui.ide/icons/full/etool16/newprj_wiz.png"
id="org.eclipse.dartboard.dart.newproject"
name="Dart Project"
project="true">
<description>
Create a new Dart project.
</description>
</wizard>
<wizard
category="org.eclipse.dartboard.eclipse.wizards"
class="org.eclipse.dartboard.project.DartFileWizard"
icon="platform:/plugin/org.eclipse.ui.ide/icons/full/etool16/newfile_wiz.png"
id="org.eclipse.dartboard.dart.newfile"
name="Dart File">
<description>
Create a new Dart file
</description>
</wizard>
</extension>
<extension
point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension
targetID="*">
<newWizardShortcut
id="org.eclipse.dartboard.dart.newproject"/>
<newWizardShortcut
id="org.eclipse.dartboard.dart.newfile"/>
</perspectiveExtension>
</extension>
</plugin>
19 changes: 19 additions & 0 deletions org.eclipse.dartboard/src/org/eclipse/dartboard/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ public class Messages extends NLS {
public static String Launch_NoConfigurationFound_Title;
public static String Launch_NoConfigurationFound_Body;

public static String NewProject_WindowTitle;
public static String NewProject_Title;
public static String NewProject_Description;
public static String NewProject_ErrorMessage;
public static String NewProject_InternalError;
public static String NewProject_CaseVariantExistsError;

public static String NewProject_Group_Label;
public static String NewProject_SDK_Not_Found;

public static String NewFile_WindowTitle;
public static String NewFile_Title;
public static String NewFile_Description;
public static String NewFile_Creating;
public static String NewFile_Container_Doesnot_Exist;
public static String NewFile_OpeningFile;

public static String ProjectUtil_NullCheck;

static {
NLS.initializeMessages("assets.messages", Messages.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.dartboard.Constants;
import org.eclipse.dartboard.util.ProjectUtil;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;
Expand Down Expand Up @@ -56,28 +52,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
if (resourceAdapter != null) {
IResource resource = resourceAdapter;
IProject project = resource.getProject();
try {
IProjectDescription description = project.getDescription();
String[] natures = description.getNatureIds();
String[] newNatures = new String[natures.length + 1];
System.arraycopy(natures, 0, newNatures, 0, natures.length);

newNatures[natures.length] = Constants.NATURE_ID;

// validate the natures
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IStatus status = workspace.validateNatureSet(newNatures);

// only apply new nature, if the status is ok
if (status.getCode() == IStatus.OK) {
description.setNatureIds(newNatures);
project.setDescription(description, null);
}

return status;
} catch (CoreException e) {
throw new ExecutionException(e.getMessage(), e);
}
return ProjectUtil.addProjectNature(project, new NullProgressMonitor());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2019 vogella GmbH and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Lakshminarayana Nekkanti
*******************************************************************************/
package org.eclipse.dartboard.project;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

public class DartFilePage extends WizardNewFileCreationPage {

public DartFilePage(String pageName, IStructuredSelection selection) {
super(pageName, selection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*******************************************************************************
* Copyright (c) 2019 vogella GmbH and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Lakshminarayana Nekkanti
*******************************************************************************/
package org.eclipse.dartboard.project;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.dartboard.Messages;
import org.eclipse.dartboard.util.StatusUtil;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DartFileWizard extends Wizard implements INewWizard {
private static final Logger LOG = LoggerFactory.getLogger(DartFileWizard.class);

private DartFilePage dartFilePage;
private IStructuredSelection selection;

@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
this.selection = selection;
setWindowTitle(Messages.NewFile_WindowTitle);
setNeedsProgressMonitor(true);
}

@Override
public void addPages() {
dartFilePage = new DartFilePage(DartFilePage.class.getSimpleName(), selection);
dartFilePage.setTitle(Messages.NewFile_Title);
dartFilePage.setDescription(Messages.NewFile_Description);
dartFilePage.setFileExtension("dart"); //$NON-NLS-1$
addPage(dartFilePage);
}

@Override
public boolean performFinish() {
final String containerName = dartFilePage.getContainerFullPath().toOSString();
final String fileName = dartFilePage.getFileName();
IRunnableWithProgress op = monitor -> {
try {
doFinish(containerName, fileName, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
};
try {
getContainer().run(true, false, op);
} catch (InterruptedException e) {
return false;
} catch (InvocationTargetException e) {
StatusUtil.applyToStatusLine(dartFilePage, StatusUtil.createError(e.getTargetException()));
return false;
}
return true;
}

private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
// create a dart file
monitor.beginTask(NLS.bind(Messages.NewFile_Creating, fileName), 2);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource resource = root.findMember(new Path(containerName));
if (!resource.exists() || !(resource instanceof IContainer)) {
StatusUtil.throwCoreException(NLS.bind(Messages.NewFile_Container_Doesnot_Exist, containerName));
}
IContainer container = (IContainer) resource;
final IFile file = container.getFile(new Path(fileName));
try {
try (InputStream stream = new ByteArrayInputStream("".getBytes())) { //$NON-NLS-1$
if (file.exists()) {
file.setContents(stream, true, true, monitor);
} else {
file.create(stream, true, monitor);
}
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
monitor.worked(1);
monitor.setTaskName(Messages.NewFile_OpeningFile);
getShell().getDisplay().asyncExec(() -> {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditor(page, file, true);
} catch (PartInitException e) {
LOG.error(e.getMessage());
}
});
monitor.worked(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2019 vogella GmbH and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Lakshminarayana Nekkanti
*******************************************************************************/
package org.eclipse.dartboard.project;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.dartboard.Constants;
import org.eclipse.dartboard.Messages;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

public class DartProjectPage extends WizardNewProjectCreationPage {

private ScopedPreferenceStore preferences;

public DartProjectPage(String pageName) {
super(pageName);
preferences = new ScopedPreferenceStore(InstanceScope.INSTANCE, Constants.PLUGIN_ID);
}

@Override
public void createControl(Composite parent) {
super.createControl(parent);
createAdditionalControls((Composite) getControl());
}

private void createAdditionalControls(Composite parent) {
Group dartGroup = new Group(parent, SWT.NONE);
dartGroup.setFont(parent.getFont());
dartGroup.setText(Messages.NewProject_Group_Label);
dartGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
dartGroup.setLayout(new GridLayout(2, false));

Label labelSdkLocation = new Label(dartGroup, SWT.NONE);
labelSdkLocation.setText(Messages.Preference_SDKLocation);
GridDataFactory.swtDefaults().applyTo(labelSdkLocation);

Label sdkLocation = new Label(dartGroup, SWT.NONE);
GridDataFactory.fillDefaults().grab(true, false).applyTo(sdkLocation);
sdkLocation.setText(preferences.getString(Constants.PREFERENCES_SDK_LOCATION));
}

@Override
protected boolean validatePage() {
boolean isValid = super.validatePage();
if (isValid) {
if ("".equals(preferences.getString(Constants.PREFERENCES_SDK_LOCATION))) { //$NON-NLS-1$
setMessage(Messages.NewProject_SDK_Not_Found, IMessageProvider.WARNING);
// not making as invalid.Since its the temporary solution
}
}
return isValid;
}
}
Loading

0 comments on commit cf921e7

Please sign in to comment.