Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed NPE occurring when creating a Scala Application using the wizard

This could occur if no Scala project was selected, e.g., by clicking on a
closed project (or outside of the Package Explorer) and File > New > Scala
Application, the reported NPE would have occurred.

Fix #1000797, #1001115
(cherry picked from commit b82bffe)
  • Loading branch information...
commit b317578200e91875dfb4c7f4e2e5ff1bd9035f11 1 parent 1638042
Mirco Dotta authored
105  org.scala-ide.sdt.core/src/scala/tools/eclipse/wizards/NewApplicationPage.scala
... ...
@@ -1,68 +1,95 @@
1 1
 package scala.tools.eclipse.wizards
2 2
 
3 3
 import org.eclipse.jdt.core.IPackageFragment
  4
+import org.eclipse.jface.wizard.IWizardPage
4 5
 import org.eclipse.jface.wizard.WizardPage
5  
-import org.eclipse.swt.layout._
6  
-import org.eclipse.swt.widgets.{ List => _, _}
7 6
 import org.eclipse.swt.SWT
  7
+import org.eclipse.swt.layout.GridData
  8
+import org.eclipse.swt.layout.GridLayout
  9
+import org.eclipse.swt.widgets.Combo
  10
+import org.eclipse.swt.widgets.Composite
  11
+import org.eclipse.swt.widgets.Group
  12
+import org.eclipse.swt.widgets.Text
8 13
 
9  
-class NewApplicationPage(packageFragments: List[IPackageFragment]) extends WizardPage("New Scala Application") {
10  
-
  14
+abstract class NewApplicationPage extends WizardPage("New Scala Application") {
11 15
   setTitle("New Scala Application")
12 16
   setDescription("Create a new top-level Scala application")
13 17
 
14  
-  private var nameField: Text = _
15  
-  private var packageComboBox: Combo = _
16  
-
17  
-  def createControl(parent: Composite) {
  18
+  final override def createControl(parent: Composite): Unit = {
18 19
     val outerComposite = new Composite(parent, SWT.NONE)
19 20
     outerComposite.setLayout(new GridLayout)
  21
+    populateWizard(outerComposite)
20 22
     setControl(outerComposite)
  23
+  }
  24
+
  25
+  protected def populateWizard(container: Composite): Unit
  26
+
  27
+  def getApplicationName: String
  28
+  def getSelectedPackage: Option[IPackageFragment]
  29
+}
  30
+
  31
+object NewApplicationPage {
  32
+
  33
+  def apply(packageFragments: List[IPackageFragment]): NewApplicationPage = {
  34
+    if (packageFragments.isEmpty) new NewApplicationPageError
  35
+    else new NewApplicationPageOk(packageFragments)
  36
+  }
  37
+
  38
+  private class NewApplicationPageOk(packageFragments: List[IPackageFragment]) extends NewApplicationPage {
  39
+    require(packageFragments.nonEmpty)
  40
+
  41
+    private var nameField: Text = _
  42
+    private var packageComboBox: Combo = _
21 43
 
22  
-    if (packageFragments.isEmpty)
23  
-      setErrorMessage("Cannot create a top-level Scala application unless a project or package is selected.")
24  
-    else {
25  
-      val packageGroup = makeGroup(outerComposite, "&Package:")
  44
+    override protected def populateWizard(container: Composite): Unit = {
  45
+      val packageGroup = makeGroup(container, "&Package:")
26 46
       packageGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL))
27 47
       packageComboBox = makePackageComboBox(packageGroup)
28 48
       packageComboBox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL))
29 49
 
30  
-      val nameGroup = makeGroup(outerComposite, "&Object name:")
  50
+      val nameGroup = makeGroup(container, "&Object name:")
31 51
       nameGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL))
32 52
       nameField = new Text(nameGroup, SWT.LEFT + SWT.BORDER)
33 53
       nameField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL))
34 54
     }
35  
-  }
36 55
 
37  
-  override def setVisible(visible: Boolean) {
38  
-    super.setVisible(visible)
39  
-    if (visible)
40  
-      nameField.setFocus()
41  
-  }
  56
+    override def setVisible(visible: Boolean) {
  57
+      super.setVisible(visible)
  58
+      if (visible)
  59
+        nameField.setFocus()
  60
+    }
42 61
 
43  
-  def getApplicationName: String = {
44  
-    val name = nameField.getText.trim
45  
-    if (name endsWith ".scala")
46  
-      name.substring(0, name.length - ".scala".length)
47  
-    else
48  
-      name
49  
-  }
  62
+    override def getApplicationName: String = {
  63
+      val name = nameField.getText.trim
  64
+      if (name endsWith ".scala")
  65
+        name.substring(0, name.length - ".scala".length)
  66
+      else
  67
+        name
  68
+    }
50 69
 
51  
-  def getSelectedPackage: IPackageFragment = packageFragments(packageComboBox.getSelectionIndex)
  70
+    override def getSelectedPackage: Option[IPackageFragment] = Some(packageFragments(packageComboBox.getSelectionIndex))
52 71
 
53  
-  private def makePackageComboBox(parent: Composite): Combo = {
54  
-    val packageComboBox = new Combo(parent, SWT.SINGLE | SWT.READ_ONLY)
55  
-    for (pkg <- packageFragments)
56  
-      packageComboBox.add(if (pkg.isDefaultPackage) "(default package)" else pkg.getElementName)
57  
-    packageComboBox.select(0)
58  
-    packageComboBox
59  
-  }
  72
+    private def makePackageComboBox(parent: Composite): Combo = {
  73
+      val packageComboBox = new Combo(parent, SWT.SINGLE | SWT.READ_ONLY)
  74
+      for (pkg <- packageFragments)
  75
+        packageComboBox.add(if (pkg.isDefaultPackage) "(default package)" else pkg.getElementName)
  76
+      packageComboBox.select(0)
  77
+      packageComboBox
  78
+    }
60 79
 
61  
-  private def makeGroup(parent: Composite, label: String): Group = {
62  
-    val group = new Group(parent, SWT.NONE)
63  
-    group.setText(label)
64  
-    group.setLayout(new GridLayout)
65  
-    group
  80
+    private def makeGroup(parent: Composite, label: String): Group = {
  81
+      val group = new Group(parent, SWT.NONE)
  82
+      group.setText(label)
  83
+      group.setLayout(new GridLayout)
  84
+      group
  85
+    }
66 86
   }
67 87
 
  88
+  private class NewApplicationPageError extends NewApplicationPage {
  89
+    override protected def populateWizard(container: Composite): Unit = {
  90
+      setErrorMessage("Cannot create Scala Application unless a project or a package is selected.")
  91
+    }
  92
+    override def getApplicationName: String = "Scala Application Wizard"
  93
+    override def getSelectedPackage: Option[IPackageFragment] = None
  94
+  }
68 95
 }
10  org.scala-ide.sdt.core/src/scala/tools/eclipse/wizards/NewApplicationWizard.scala
@@ -58,7 +58,7 @@ class NewApplicationWizard extends BasicNewResourceWizard with HasLogger {
58 58
       selection = getCurrentEditorAsSelection getOrElse selection
59 59
     val packageFragments = getPackageFragments(selection)
60 60
 
61  
-    page = new NewApplicationPage(packageFragments)
  61
+    page = NewApplicationPage(packageFragments)
62 62
     addPage(page)
63 63
   }
64 64
 
@@ -91,8 +91,11 @@ class NewApplicationWizard extends BasicNewResourceWizard with HasLogger {
91 91
     true
92 92
   }
93 93
 
94  
-  override def performFinish: Boolean =
95  
-    tryExecute(createApplication(page.getApplicationName, page.getSelectedPackage)).getOrElse(false)
  94
+  override def performFinish: Boolean = page.getSelectedPackage match {
  95
+    case None => true
  96
+    case Some(pkg) =>
  97
+      tryExecute(createApplication(page.getApplicationName, pkg)).getOrElse(false)
  98
+  }    
96 99
 
97 100
   private def openInEditor(file: IFile) = {
98 101
     selectAndReveal(file)
@@ -134,6 +137,7 @@ class NewApplicationWizard extends BasicNewResourceWizard with HasLogger {
134 137
       case _ =>
135 138
         (for {
136 139
           resource <- computeSelectedResources(selection)
  140
+          if resource.getProject.isOpen
137 141
           packageFragment <- getPackageFragments(resource.getProject)
138 142
         } yield packageFragment).distinct
139 143
     }

0 notes on commit b317578

Please sign in to comment.
Something went wrong with that request. Please try again.