-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "New Dart file" & "New Dart project" to "New" context menu (#62)
closes #6 Signed-off-by: Lakshminarayana Nekkanti <narayana.nekkanti@gmail.com>
- Loading branch information
1 parent
b291927
commit cf921e7
Showing
13 changed files
with
602 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartFilePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartFileWizard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartProjectPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.