diff --git a/org.eclipse.dartboard/META-INF/MANIFEST.MF b/org.eclipse.dartboard/META-INF/MANIFEST.MF index 12e2a56..ccb33d6 100644 --- a/org.eclipse.dartboard/META-INF/MANIFEST.MF +++ b/org.eclipse.dartboard/META-INF/MANIFEST.MF @@ -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 diff --git a/org.eclipse.dartboard/assets/messages.properties b/org.eclipse.dartboard/assets/messages.properties index 2c8ec2c..d844bc0 100644 --- a/org.eclipse.dartboard/assets/messages.properties +++ b/org.eclipse.dartboard/assets/messages.properties @@ -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 \ No newline at end of file diff --git a/org.eclipse.dartboard/icons/dart_16.png b/org.eclipse.dartboard/icons/dart_16.png new file mode 100644 index 0000000..c879b36 Binary files /dev/null and b/org.eclipse.dartboard/icons/dart_16.png differ diff --git a/org.eclipse.dartboard/icons/dart_file_16.png b/org.eclipse.dartboard/icons/dart_file_16.png new file mode 100644 index 0000000..39e6537 Binary files /dev/null and b/org.eclipse.dartboard/icons/dart_file_16.png differ diff --git a/org.eclipse.dartboard/plugin.xml b/org.eclipse.dartboard/plugin.xml index 846e127..214db77 100644 --- a/org.eclipse.dartboard/plugin.xml +++ b/org.eclipse.dartboard/plugin.xml @@ -113,4 +113,41 @@ + + + + + Create a new Dart project. + + + + + Create a new Dart file + + + + + + + + + diff --git a/org.eclipse.dartboard/src/org/eclipse/dartboard/Messages.java b/org.eclipse.dartboard/src/org/eclipse/dartboard/Messages.java index 534d5fc..1067bca 100644 --- a/org.eclipse.dartboard/src/org/eclipse/dartboard/Messages.java +++ b/org.eclipse.dartboard/src/org/eclipse/dartboard/Messages.java @@ -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); } diff --git a/org.eclipse.dartboard/src/org/eclipse/dartboard/nature/DartProjectNatureHandler.java b/org.eclipse.dartboard/src/org/eclipse/dartboard/nature/DartProjectNatureHandler.java index 38f4e05..17fd7bd 100644 --- a/org.eclipse.dartboard/src/org/eclipse/dartboard/nature/DartProjectNatureHandler.java +++ b/org.eclipse.dartboard/src/org/eclipse/dartboard/nature/DartProjectNatureHandler.java @@ -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; @@ -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()); } } diff --git a/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartFilePage.java b/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartFilePage.java new file mode 100644 index 0000000..0f30d1b --- /dev/null +++ b/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartFilePage.java @@ -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); + } +} diff --git a/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartFileWizard.java b/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartFileWizard.java new file mode 100644 index 0000000..1f2136a --- /dev/null +++ b/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartFileWizard.java @@ -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); + } +} diff --git a/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartProjectPage.java b/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartProjectPage.java new file mode 100644 index 0000000..e607986 --- /dev/null +++ b/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartProjectPage.java @@ -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; + } +} diff --git a/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartProjectWizard.java b/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartProjectWizard.java new file mode 100644 index 0000000..a7da941 --- /dev/null +++ b/org.eclipse.dartboard/src/org/eclipse/dartboard/project/DartProjectWizard.java @@ -0,0 +1,144 @@ +/******************************************************************************* + * 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.lang.reflect.InvocationTargetException; + +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IResourceStatus; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.dartboard.Constants; +import org.eclipse.dartboard.Messages; +import org.eclipse.dartboard.util.ProjectUtil; +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.ide.undo.CreateProjectOperation; +import org.eclipse.ui.ide.undo.WorkspaceUndoUtil; +import org.eclipse.ui.statushandlers.IStatusAdapterConstants; +import org.eclipse.ui.statushandlers.StatusAdapter; +import org.eclipse.ui.statushandlers.StatusManager; +import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DartProjectWizard extends Wizard implements INewWizard { + private static final Logger LOG = LoggerFactory.getLogger(DartProjectWizard.class); + + private DartProjectPage dartProjectPage; + private IProject newProject; + private IWorkbench workbench; + private IStructuredSelection selection; + + @Override + public void init(IWorkbench workbench, IStructuredSelection selection) { + this.workbench = workbench; + this.selection = selection; + setWindowTitle(Messages.NewProject_WindowTitle); + setNeedsProgressMonitor(true); + } + + @Override + public void addPages() { + dartProjectPage = new DartProjectPage(DartProjectPage.class.getSimpleName()); + dartProjectPage.setTitle(Messages.NewProject_Title); + dartProjectPage.setDescription(Messages.NewProject_Description); + addPage(dartProjectPage); + } + + private IProject createNewProject() { + if (newProject != null) { + return newProject; + } + + // get a project handle + final IProject newProjectHandle = dartProjectPage.getProjectHandle(); + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + final IProjectDescription description = workspace.newProjectDescription(newProjectHandle.getName()); + if (!dartProjectPage.useDefaults()) { + description.setLocationURI(dartProjectPage.getLocationURI()); + } + + // create the new project operation + IRunnableWithProgress operation = monitor -> { + CreateProjectOperation projectOperation = new CreateProjectOperation(description, + Messages.NewProject_WindowTitle); + try { + projectOperation.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell())); + ProjectUtil.addProjectNature(newProjectHandle, monitor); + } catch (ExecutionException e) { + throw new InvocationTargetException(e); + } + }; + + // run the new project creation operation + try { + getContainer().run(true, true, operation); + } catch (InterruptedException e) { + LOG.error(e.getMessage()); + return null; + } catch (InvocationTargetException e) { + Throwable t = e.getTargetException(); + if (t instanceof ExecutionException && t.getCause() instanceof CoreException) { + CoreException cause = (CoreException) t.getCause(); + StatusAdapter status; + if (cause.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) { + status = new StatusAdapter(StatusUtil.newStatus(IStatus.WARNING, + NLS.bind(Messages.NewProject_CaseVariantExistsError, newProjectHandle.getName()), cause)); + } else { + status = new StatusAdapter(StatusUtil.newStatus(cause.getStatus().getSeverity(), + Messages.NewProject_ErrorMessage, cause)); + } + status.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, Messages.NewProject_ErrorMessage); + StatusManager.getManager().handle(status, StatusManager.BLOCK); + } else { + StatusAdapter status = new StatusAdapter(new Status(IStatus.WARNING, Constants.PLUGIN_ID, 0, + NLS.bind(Messages.NewProject_InternalError, t.getMessage()), t)); + status.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, Messages.NewProject_ErrorMessage); + StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.BLOCK); + } + return null; + } + newProject = newProjectHandle; + return newProject; + } + + @Override + public boolean performFinish() { + createNewProject(); + if (newProject == null) { + return false; + } + BasicNewResourceWizard.selectAndReveal(newProject, getWorkbench().getActiveWorkbenchWindow()); + return true; + } + + public IWorkbench getWorkbench() { + return this.workbench; + } + + public IStructuredSelection getSelection() { + return selection; + } +} diff --git a/org.eclipse.dartboard/src/org/eclipse/dartboard/util/ProjectUtil.java b/org.eclipse.dartboard/src/org/eclipse/dartboard/util/ProjectUtil.java new file mode 100644 index 0000000..f3bf7d8 --- /dev/null +++ b/org.eclipse.dartboard/src/org/eclipse/dartboard/util/ProjectUtil.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * 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.util; + +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.core.runtime.Status; +import org.eclipse.dartboard.Constants; +import org.eclipse.dartboard.Messages; + +public class ProjectUtil { + public static IStatus addProjectNature(IProject project, IProgressMonitor monitor) throws ExecutionException { + try { + if (monitor != null && monitor.isCanceled()) { + throw new OperationCanceledException(); + } + if (project == null) + throw new ExecutionException(Messages.ProjectUtil_NullCheck); + if (!project.hasNature(Constants.NATURE_ID)) { + 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, monitor); + } + return status; + } else { + if (monitor != null) { + monitor.worked(1); + } + } + } catch (CoreException e) { + throw new ExecutionException(e.getMessage(), e); + } + return Status.OK_STATUS; + } +} diff --git a/org.eclipse.dartboard/src/org/eclipse/dartboard/util/StatusUtil.java b/org.eclipse.dartboard/src/org/eclipse/dartboard/util/StatusUtil.java new file mode 100644 index 0000000..1372d5b --- /dev/null +++ b/org.eclipse.dartboard/src/org/eclipse/dartboard/util/StatusUtil.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * 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.util; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.dartboard.Constants; +import org.eclipse.jface.dialogs.DialogPage; +import org.eclipse.jface.dialogs.IMessageProvider; + +public class StatusUtil { + + public static IStatus newStatus(int severity, String message, Throwable exception) { + String statusMessage = message; + if (message == null || message.trim().length() == 0) { + if (exception == null) { + throw new IllegalArgumentException(); + } else { + statusMessage = exception.getMessage() == null ? exception.toString() : exception.getMessage(); + } + } + return new Status(severity, Constants.PLUGIN_ID, severity, statusMessage, exception); + } + + public static IStatus createError(Throwable exception) { + return newStatus(IStatus.ERROR, "", exception); //$NON-NLS-1$ + } + + public static IStatus createWarning(Throwable exception) { + return newStatus(IStatus.WARNING, "", exception); //$NON-NLS-1$ + } + + public static IStatus createError(String message, Throwable exception) { + return newStatus(IStatus.ERROR, message, exception); + } + + public static IStatus createWarning(String message, Throwable exception) { + return newStatus(IStatus.WARNING, message, exception); + } + + public static IStatus newStatus(int severity, String message) { + return new Status(severity, Constants.PLUGIN_ID, message); + } + + public static IStatus createError(String message) { + return newStatus(IStatus.ERROR, message); + } + + public static IStatus createWarning(String message) { + return newStatus(IStatus.WARNING, message); + } + + public static void throwCoreException(String message) throws CoreException { + throw new CoreException(createError(message)); + } + + public static void applyToStatusLine(DialogPage page, IStatus status) { + String message = status.getMessage(); + switch (status.getSeverity()) { + case IStatus.OK: + page.setMessage(message, IMessageProvider.NONE); + page.setErrorMessage(null); + break; + case IStatus.WARNING: + page.setMessage(message, IMessageProvider.WARNING); + page.setErrorMessage(null); + break; + case IStatus.INFO: + page.setMessage(message, IMessageProvider.INFORMATION); + page.setErrorMessage(null); + break; + default: + if (message.length() == 0) { + message = null; + } + page.setMessage(null); + page.setErrorMessage(message); + break; + } + } + +}