Skip to content

Commit

Permalink
IEP-403 Fixing NPE in the new component wizard (#249)
Browse files Browse the repository at this point in the history
* Fixing NPE in the new component wizard

* update selectedProject value with value from combo button

* add newComponentWizard to the new shortcut

* Update NewComponentWizardPage.java

Co-authored-by: Kondal Kolipaka <[email protected]>
  • Loading branch information
sigmaaa and kolipakakondal authored Apr 16, 2021
1 parent 317c5c7 commit 5c4bcc4
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
14 changes: 14 additions & 0 deletions bundles/com.espressif.idf.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<newWizardShortcut
id="com.espressif.idf.ui.new.project.wizard">
</newWizardShortcut>
<newWizardShortcut
id="com.espressif.idf.ui.component.wizard">
</newWizardShortcut>
</perspectiveExtension>
</extension>
<extension
Expand Down Expand Up @@ -148,6 +151,11 @@
id="com.espressif.idf.ui.command.new"
name="%wizard.name">
</command>
<command
defaultHandler="com.espressif.idf.ui.handlers.NewComponentHandler"
id="com.espressif.idf.ui.command.component"
name="%wizard.name.1">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
Expand All @@ -168,6 +176,12 @@
</with>
</visibleWhen>
</command>
<command
commandId="com.espressif.idf.ui.command.component"
icon="icons/espressif_idf_project.png"
label="%wizard.name.1"
style="push">
</command>
</menuContribution>
</extension>
<extension
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.espressif.idf.ui.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

import com.espressif.idf.ui.wizard.NewComponentWizard;

public class NewComponentHandler extends AbstractHandler
{

@Override
public Object execute(ExecutionEvent event) throws ExecutionException
{
NewComponentWizard newComponentWizard = new NewComponentWizard();
newComponentWizard.init(PlatformUI.getWorkbench(), null);

WizardDialog dialog = new WizardDialog(Display.getDefault().getActiveShell(), newComponentWizard);
dialog.create();
dialog.open();
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class Messages extends NLS {
public static String NewIdfComponentWizard_NameCantIncludeSpaceErr;
public static String NewIdfComponentWizard_NameCantBeEmptyErr;
public static String NewIdfComponentWizard_NameAlreadyExistsErr;
public static String NewComponentWizardPage_CantCreateCompErr;
public static String NewComponentWizardPage_ProjectDoesntExistErr;
public static String NewComponentWizardPage_ProjectNameLbl;

static {
// initialize resource bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.IStructuredSelection;
Expand All @@ -17,6 +19,7 @@
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
Expand All @@ -26,13 +29,15 @@
import com.espressif.idf.core.ProcessBuilderFactory;
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.util.IDFUtil;
import com.espressif.idf.core.util.StringUtil;
import com.espressif.idf.ui.EclipseUtil;

public class NewComponentWizardPage extends WizardPage
{

private Text componentName;
private Composite container;
private Combo projectCombo;

protected NewComponentWizardPage(IStructuredSelection selection)
{
Expand Down Expand Up @@ -61,7 +66,7 @@ private String runCommand(List<String> arguments, Map<String, String> env)
ProcessBuilderFactory processRunner = new ProcessBuilderFactory();
try
{
IProject selectedProject = EclipseUtil.getSelectedProjectInExplorer();
IProject selectedProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectCombo.getText());
IPath newPath = selectedProject.getLocation();
IStatus status = processRunner.runInBackground(arguments, newPath, env);
if (status == null)
Expand Down Expand Up @@ -91,7 +96,7 @@ public void createControl(Composite parent)
Label label = new Label(container, SWT.NONE);
label.setText(Messages.NewIdfComponentWizard_Component_name);
componentName = new Text(container, SWT.BORDER);
componentName.setText(""); //
componentName.setText(""); //$NON-NLS-1$
componentName.addModifyListener(new ModifyListener()
{
@Override
Expand All @@ -102,13 +107,45 @@ public void modifyText(ModifyEvent e)
});
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
componentName.setLayoutData(gd);

Label projectNameLbl = new Label(container, SWT.NONE);
projectNameLbl.setText(Messages.NewComponentWizardPage_ProjectNameLbl);
projectCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
Optional<IProject> optProject = Optional.ofNullable(EclipseUtil.getSelectedProjectInExplorer());
optProject.ifPresent(project -> projectCombo.setText(project.getName()));
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (IProject project : projects)
{
projectCombo.add(project.getName());
}
projectCombo.addModifyListener(new ModifyListener()
{
@Override
public void modifyText(ModifyEvent e)
{
setPageComplete(validatePage());
}
});
projectCombo.setLayoutData(gd);
setControl(container);
setPageComplete(false);
}

protected boolean validatePage()
{

if (StringUtil.isEmpty(projectCombo.getText()))
{
setErrorMessage(Messages.NewComponentWizardPage_CantCreateCompErr);
return false;
}

if (!ResourcesPlugin.getWorkspace().getRoot().getProject(projectCombo.getText()).exists())
{
setErrorMessage(Messages.NewComponentWizardPage_ProjectDoesntExistErr);
return false;
}

if (componentName.getText().isEmpty()) // $NON-NLS-1$
{
setErrorMessage(Messages.NewIdfComponentWizard_NameCantBeEmptyErr);
Expand All @@ -131,7 +168,7 @@ protected boolean validatePage()

private boolean checkIfComponentExists()
{
IProject selectedProject = EclipseUtil.getSelectedProjectInExplorer();
IProject selectedProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectCombo.getText());
IPath newPath = selectedProject.getLocation().append("/components"); //$NON-NLS-1$
Path absPath = Paths.get(newPath.toString() + "\\" + componentName.getText()); //$NON-NLS-1$
if (Files.exists(absPath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ NewIdfComponentWizard_Component_name=Component Name:
NewIdfComponentWizard_Page = New IDF component page
NewIdfComponentWizard_NameCantIncludeSpaceErr=A component name can't include space
NewIdfComponentWizard_NameCantBeEmptyErr=A component name can't be empty
NewIdfComponentWizard_NameAlreadyExistsErr=A component with such a name is already exists
NewIdfComponentWizard_NameAlreadyExistsErr=A component with such a name is already exists
NewComponentWizardPage_CantCreateCompErr=Can not create a component without a project selection
NewComponentWizardPage_ProjectDoesntExistErr=Project doesn't exist
NewComponentWizardPage_ProjectNameLbl=Project name:

0 comments on commit 5c4bcc4

Please sign in to comment.