Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adds a preference and an project preference page

The preference is initialized with framework defaults (play.api.templates._, play.api.templates.PlayMagic._).
It can be overriden at the project level, in the project properties.
Changes are not applied directly, as we are currently lacking a real builder.

Fix #35.
  • Loading branch information...
commit ccc7675d0c2a516137f4d990b8ad1d57066718f2 1 parent 5c7dd65
Luc Bourlier authored March 01, 2013
43  org.scala-ide.play2/plugin.xml
@@ -71,6 +71,9 @@
71 71
       <initializer
72 72
             class="org.scalaide.play2.templateeditor.properties.TemplateColourPreferenceInitializer">
73 73
       </initializer>
  74
+      <initializer
  75
+            class="org.scalaide.play2.properties.PreferenceInitializer">
  76
+      </initializer>
74 77
    </extension>
75 78
    <extension
76 79
          point="org.eclipse.core.contenttype.contentTypes">
@@ -138,6 +141,46 @@
138 141
             name="Format">
139 142
       </command>
140 143
    </extension>
  144
+   <extension
  145
+         point="org.eclipse.core.expressions.propertyTesters">
  146
+      <propertyTester
  147
+            class="org.scalaide.play2.util.Play2PropertyTester"
  148
+            id="org.scala-ide.play2.isJavaPlayProject"
  149
+            namespace="org.scala-ide.play2.properties"
  150
+            properties="isPlay2Project"
  151
+            type="org.eclipse.jdt.core.IJavaProject">
  152
+      </propertyTester>
  153
+      <propertyTester
  154
+            class="org.scalaide.play2.util.Play2PropertyTester"
  155
+            id="org.scala-ide.play2.isPlayProject"
  156
+            namespace="org.scala-ide.play2.properties"
  157
+            properties="isPlay2Project"
  158
+            type="org.eclipse.core.resources.IProject">
  159
+      </propertyTester>
  160
+   </extension>
  161
+   <extension
  162
+         point="org.eclipse.ui.propertyPages">
  163
+      <page
  164
+            class="org.scalaide.play2.properties.ProjectPropertyPage"
  165
+            id="org.scala-ide.play2.projectPropertyPage"
  166
+            name="Play2">
  167
+         <enabledWhen>
  168
+            <and>
  169
+               <or>
  170
+	               <instanceof
  171
+	                     value="org.eclipse.jdt.core.IJavaProject">
  172
+	               </instanceof>
  173
+	               <instanceof
  174
+	                     value="org.eclipse.core.resources.IProject">
  175
+	               </instanceof>
  176
+               </or>
  177
+               <test
  178
+                     property="org.scala-ide.play2.properties.isPlay2Project">
  179
+               </test>
  180
+            </and>
  181
+         </enabledWhen>
  182
+      </page>
  183
+   </extension>
141 184
    <!--
142 185
    <extension
143 186
          point="org.eclipse.ui.editors.markerAnnotationSpecification">
4  org.scala-ide.play2/src/org/scalaide/play2/PlayPlugin.scala
@@ -10,7 +10,7 @@ import org.eclipse.core.runtime.Status
10 10
 object PlayPlugin {
11 11
   @volatile var plugin: PlayPlugin = _
12 12
 
13  
-  private final val PluginId = "org.scala-ide.play2"
  13
+  final val PluginId = "org.scala-ide.play2"
14 14
   final val RouteFormatterMarginId = PluginId + ".routeeditor.margin"
15 15
   final val TemplateExtension = "scala.html"
16 16
 
@@ -50,4 +50,4 @@ class PlayPlugin extends AbstractUIPlugin {
50 50
       playProject <- asPlayProject(iProject)
51 51
     } playProject.initialize()
52 52
   }
53  
-}
  53
+}
8  org.scala-ide.play2/src/org/scalaide/play2/PlayProject.scala
@@ -8,10 +8,18 @@ import org.eclipse.core.resources.IFile
8 8
 import org.scalaide.play2.templateeditor.TemplateCompilationUnit
9 9
 import org.scalaide.play2.templateeditor.compiler.TemplatePresentationCompiler
10 10
 import org.scalaide.play2.util.AutoHashMap
  11
+import org.eclipse.ui.preferences.ScopedPreferenceStore
  12
+import org.eclipse.core.resources.ProjectScope
  13
+import org.scalaide.play2.util.SyncedScopedPreferenceStore
  14
+import org.eclipse.jface.preference.IPreferenceStore
11 15
 
12 16
 class PlayProject private (val scalaProject: ScalaProject) {
13 17
   private val presentationCompiler = new TemplatePresentationCompiler(this)
14 18
 
  19
+  val cachedPreferenceStore = new SyncedScopedPreferenceStore(scalaProject.underlying, PlayPlugin.PluginId)
  20
+
  21
+  def generateScopedPreferenceStore: IPreferenceStore = new ScopedPreferenceStore(new ProjectScope(scalaProject.underlying), PlayPlugin.PluginId)
  22
+
15 23
   def withPresentationCompiler[T](op: TemplatePresentationCompiler => T): T = {
16 24
     op(presentationCompiler)
17 25
   }
35  org.scala-ide.play2/src/org/scalaide/play2/properties/PlayPreferences.scala
@@ -16,6 +16,41 @@ import org.eclipse.ui.IWorkbenchPreferencePage
16 16
 import org.eclipse.ui.dialogs.PropertyPage
17 17
 import org.scalaide.play2.PlayPlugin
18 18
 
  19
+object PlayPreferences {
  20
+
  21
+  /** Preference containing the list of import to automatically add to the generate template code.
  22
+   *  The data is stored as the string which will be added to the generated source (to not recreate
  23
+   *  it everytime). The empty String represent an empty import list.
  24
+   *  [[org.scalaide.play2.properties.PlayPreferences.serializeImports]] and [[org.scalaide.play2.properties.PlayPreferences.deserializeImports]]
  25
+   *  need to be used when converting the preference value to/from Array[String].
  26
+   */
  27
+  final val TemplateImports = "templateImports"
  28
+
  29
+  // Regex used for the operations on the templateImports preference.
  30
+  private val importsRegex = "import ([^\n]+)\n".r
  31
+
  32
+  /** @see [[org.scalaide.play2.properties.PlayPreferences.TemplateImports]]
  33
+   */
  34
+  def serializeImports(entries: Array[String]): String = {
  35
+    if (entries.length == 0) {
  36
+      ""
  37
+    } else {
  38
+      entries.mkString("import ", "\nimport ", "\n")
  39
+    }
  40
+  }
  41
+
  42
+  /** @see [[org.scalaide.play2.properties.PlayPreferences.TemplateImports]]
  43
+   */
  44
+  def deserializeImports(s: String): Array[String] = {
  45
+    if (s.length == 0) {
  46
+      new Array(0)
  47
+    } else {
  48
+      importsRegex.findAllIn(s).matchData.map(m => m.group(1)).toArray
  49
+    }
  50
+  }
  51
+
  52
+}
  53
+
19 54
 class PlayPreferences extends PropertyPage with IWorkbenchPreferencePage with EclipseSettings
20 55
   with ScalaPluginPreferencePage with HasLogger {
21 56
 
12  org.scala-ide.play2/src/org/scalaide/play2/properties/PreferenceInitializer.scala
... ...
@@ -0,0 +1,12 @@
  1
+package org.scalaide.play2.properties
  2
+
  3
+import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer
  4
+import org.scalaide.play2.PlayPlugin
  5
+
  6
+class PreferenceInitializer extends AbstractPreferenceInitializer {
  7
+
  8
+  override def initializeDefaultPreferences() {
  9
+    PlayPlugin.prefStore.setDefault(PlayPreferences.TemplateImports, "import play.api.templates._\nimport play.api.templates.PlayMagic._\n")
  10
+  }
  11
+
  12
+}
83  org.scala-ide.play2/src/org/scalaide/play2/properties/ProjectPropertyPage.scala
... ...
@@ -0,0 +1,83 @@
  1
+package org.scalaide.play2.properties
  2
+
  3
+import org.eclipse.core.resources.IProject
  4
+import org.eclipse.core.runtime.IAdaptable
  5
+import org.eclipse.jdt.core.IJavaProject
  6
+import org.eclipse.jface.dialogs.IInputValidator
  7
+import org.eclipse.jface.dialogs.InputDialog
  8
+import org.eclipse.jface.preference.FieldEditorPreferencePage
  9
+import org.eclipse.jface.preference.IPreferenceStore
  10
+import org.eclipse.jface.preference.ListEditor
  11
+import org.eclipse.jface.window.Window
  12
+import org.eclipse.swt.widgets.Composite
  13
+import org.eclipse.swt.widgets.Display
  14
+import org.eclipse.ui.IWorkbenchPropertyPage
  15
+import org.scalaide.play2.PlayPlugin
  16
+
  17
+/** Preference page displayed in the property dialog of (play) projects.
  18
+ *  Used from the UI thread.
  19
+ */
  20
+class ProjectPropertyPage extends FieldEditorPreferencePage(FieldEditorPreferencePage.GRID) with IWorkbenchPropertyPage {
  21
+
  22
+  /** Preference field to display the list of extra imports.
  23
+   */
  24
+  private class ImportsFieldEditor(name: String, labelText: String, parent: Composite) extends ListEditor(name, labelText, parent) {
  25
+
  26
+    override protected def createList(entries: Array[String]): String =
  27
+      PlayPreferences.serializeImports(entries)
  28
+
  29
+    override protected def parseString(s: String): Array[String] =
  30
+      PlayPreferences.deserializeImports(s)
  31
+
  32
+    override protected def getNewInputObject(): String = {
  33
+
  34
+      val dlg = new InputDialog(
  35
+        Display.getCurrent().getActiveShell(),
  36
+        "Play template import",
  37
+        "Enter an import value:",
  38
+        "com.example._",
  39
+        new IInputValidator {
  40
+          def isValid(text: String) = null
  41
+        });
  42
+
  43
+      if (dlg.open() == Window.OK) {
  44
+        dlg.getValue()
  45
+      } else {
  46
+        null
  47
+      }
  48
+    }
  49
+
  50
+  }
  51
+
  52
+  // The preference store being edited.
  53
+  // The data require to get the store is provided by the workbench during the page lifecycle.
  54
+  var prefStore: IPreferenceStore = _
  55
+
  56
+  // Members declared in org.eclipse.jface.preference.FieldEditorPreferencePage
  57
+
  58
+  override def createFieldEditors() {
  59
+    addField(new ImportsFieldEditor(PlayPreferences.TemplateImports, "Template default imports", getFieldEditorParent()))
  60
+  }
  61
+
  62
+  // Members declared in org.eclipse.ui.IWorkbenchPropertyPage
  63
+
  64
+  // doesn't seem to be a real function for this method.
  65
+  // It looks like it leaked from the implementation of PropertyPage.
  66
+  override def getElement(): IAdaptable = null
  67
+
  68
+  override def setElement(element: IAdaptable) {
  69
+    prefStore = element match {
  70
+      case project: IProject =>
  71
+        PlayPlugin.plugin.asPlayProject(project).get.generateScopedPreferenceStore
  72
+      case project: IJavaProject =>
  73
+        PlayPlugin.plugin.asPlayProject(project.getProject()).get.generateScopedPreferenceStore
  74
+    }
  75
+  }
  76
+
  77
+  // ----
  78
+
  79
+  override def doGetPreferenceStore(): IPreferenceStore = {
  80
+    prefStore
  81
+  }
  82
+
  83
+}
16  org.scala-ide.play2/src/org/scalaide/play2/templateeditor/compiler/CompilerUsing.scala
@@ -9,25 +9,21 @@ import play.templates.ScalaTemplateCompiler
9 9
 import play.templates.ScalaTemplateCompiler._
10 10
 import play.templates.TemplateCompilationError
11 11
 import scalax.file.Path
  12
+import org.scalaide.play2.properties.PlayPreferences
12 13
 import scala.tools.eclipse.logging.HasLogger
13  
-
14 14
 /**
15 15
  * a helper for using template compiler
16 16
  */
17 17
 object CompilerUsing extends HasLogger {
18 18
   val templateCompiler = ScalaTemplateCompiler
19  
-  val additionalImports = """import play.templates._
20  
-import play.templates.TemplateMagic._
21  
-    
22  
-    
23  
-import play.api.templates._
24  
-import play.api.templates.PlayMagic._
  19
+  val additionalImports = """
25 20
 import models._
26 21
 import controllers._
27 22
 import play.api.i18n._
28 23
 import play.api.mvc._
29 24
 import play.api.data._
30  
-import views.html._"""
  25
+import views.html._
  26
+"""
31 27
 
32 28
   /**
33 29
    * invokes compile method of template compiler and returns generated source object or
@@ -39,7 +35,7 @@ import views.html._"""
39 35
       logger.debug(s"Template file '${source.getAbsolutePath}' must be located in '$sourcePath' or one of its subfolders!")
40 36
 
41 37
     Try {
42  
-      templateCompiler.compileVirtual(content, source, playProject.sourceDir, "play.api.templates.Html", "play.api.templates.HtmlFormat", additionalImports)
  38
+      templateCompiler.compileVirtual(content, source, playProject.sourceDir, "play.api.templates.Html", "play.api.templates.HtmlFormat", additionalImports + playProject.cachedPreferenceStore.getString(PlayPreferences.TemplateImports))
43 39
     } recoverWith {
44 40
       case TemplateCompilationError(source, message, line, column) =>
45 41
         val offset = PositionHelper.convertLineColumnToOffset(content, line, column)
@@ -100,4 +96,4 @@ object PositionHelper {
100 96
       }
101 97
     }
102 98
   }
103  
-}
  99
+}
36  org.scala-ide.play2/src/org/scalaide/play2/util/Play2PropertyTester.scala
... ...
@@ -0,0 +1,36 @@
  1
+package org.scalaide.play2.util
  2
+
  3
+import org.eclipse.core.expressions.PropertyTester
  4
+import org.eclipse.core.resources.IProject
  5
+import org.scalaide.play2.PlayPlugin
  6
+import org.eclipse.jdt.core.IJavaProject
  7
+
  8
+object Play2PropertyTester {
  9
+  final val IsPlayProject = "isPlay2Project"
  10
+}
  11
+
  12
+/** Eclipse property tester. Can check if a project is a play2 project.
  13
+ */
  14
+class Play2PropertyTester() extends PropertyTester {
  15
+
  16
+  // from IPropertyTester
  17
+
  18
+  override def test(receiver: Any, property: String, args: Array[Object], expectedValue: Any): Boolean = {
  19
+    import Play2PropertyTester._
  20
+
  21
+    property match {
  22
+      case IsPlayProject =>
  23
+        receiver match {
  24
+          case project: IProject =>
  25
+            PlayPlugin.plugin.asPlayProject(project).isDefined
  26
+          case project: IJavaProject =>
  27
+            PlayPlugin.plugin.asPlayProject(project.getProject()).isDefined
  28
+          case _ =>
  29
+            false
  30
+        }
  31
+      case _ =>
  32
+        false
  33
+    }
  34
+  }
  35
+
  36
+}
16  org.scala-ide.play2/src/org/scalaide/play2/util/SyncedScopedPreferenceStore.scala
... ...
@@ -0,0 +1,16 @@
  1
+package org.scalaide.play2.util
  2
+
  3
+import org.eclipse.core.resources.IProject
  4
+import org.eclipse.core.resources.ProjectScope
  5
+import org.eclipse.ui.preferences.ScopedPreferenceStore
  6
+
  7
+class SyncedScopedPreferenceStore(project: IProject, pluginId: String) {
  8
+
  9
+  private val preferenceStore = new ScopedPreferenceStore(new ProjectScope(project), pluginId)
  10
+
  11
+  def getString(name: String): String = {
  12
+    preferenceStore.synchronized {
  13
+      preferenceStore.getString(name)
  14
+    }
  15
+  }
  16
+}

0 notes on commit ccc7675

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