Skip to content

Instantly share code, notes, and snippets.

@OlegYch
Created June 27, 2012 13:02
Show Gist options
  • Save OlegYch/3003942 to your computer and use it in GitHub Desktop.
Save OlegYch/3003942 to your computer and use it in GitHub Desktop.
import org.vaadin.navigator7.uri.ParamPageResource
import org.vaadin.navigator7.interceptor.PageInvocation
/**
*/
class BaseController[M <: BaseModel] {
def application = SpringWebApplication.current
private[this] var _model: M = _
def model = _model
def model_=(m: M) = updateModel(m)
def url[T <: BasePage[_] : Manifest](namedParams: Seq[(Symbol, Any)] = Nil,
positionalParams: Seq[Any] = Nil, modal: Boolean = false) = {
val resource = ModelParamPageResource(
manifest[T].erasure.asInstanceOf[Class[T]],
positionalParams.asInstanceOf[Seq[AnyRef]], None, modal)
namedParams.foreach {case (name, value) => resource.addParam(name.name, value)}
resource.getURL
}
def to[T <: BasePage[_] : Manifest](model: BaseModel = null, namedParams: Seq[(Symbol, Any)] = Nil,
positionalParams: Seq[Any] = Nil, modal: Boolean = false) {
val resource = ModelParamPageResource(
manifest[T].erasure.asInstanceOf[Class[T]],
positionalParams.asInstanceOf[Seq[AnyRef]], Option(model), modal)
namedParams.foreach {case (name, value) => resource.addParam(name.name, value)}
application.navigator.navigateTo(resource)
}
def to[T <: BasePage[_] : Manifest](menuItem: MenuItem[T]) {
to[T](modal = menuItem.modal)
}
def to(page: PageInvocation) {
application.navigator.navigateTo(page match {
case SpringPageInvocation(_, _, _, _, _, _, Some(r)) => r
case _ => new ModelParamPageResource(page.getPageClass, Nil, None) {
override def getParams = page.getParams
}
})
}
var modelChanged: (M, M) => Unit = (_, _) => ()
def onModelChange(block: => Unit) = modelChanged = (_, _) => block
def updateModel(newModel: M) {
val oldModel = model
_model = newModel
if (modelChanged != null) modelChanged(oldModel, newModel)
}
}
case class ModelParamPageResource(basePageClass: Class[_ <: com.vaadin.ui.Component],
positionalParams: Seq[AnyRef],
model: Option[BaseModel], modal: Boolean = false)
extends ParamPageResource(basePageClass, positionalParams: _*) {
}
abstract class BasePage[C <: BaseController[M] forSome {type M <: BaseModel} : Manifest]
extends CustomComponent with DelayedInit with UserAction {
setSizeUndefined()
def alwaysShowModal = false
lazy val parentPageClass: Class[_ <: BasePage[_]] = this.getClass
lazy val tabTitle: Option[Symbol] = None
override def setCompositionRoot(compositionRoot: Component) {
super.setCompositionRoot(compositionRoot)
}
private lazy val initCode = new ListBuffer[() => Unit]
/**The init hook. This saves all initialization code for execution within `main`.
* This method is normally never called directly from user code.
* Instead it is called as compiler-generated code for those classes and objects
* (but not traits) that inherit from the `DelayedInit` trait and that do not themselves define
* a `delayedInit` method.
* @param body the initialization code to be stored for later execution
*/
override def delayedInit(body: => Unit) {
initCode += (() => body)
}
def init = initCode.foreach(_())
def controller: C
def showModal {
new OptionDialog(controller.application.window).showCustomDialog("", this, OptionKind.OK) {
case OptionKind.OK => true
case _ => true
}
}
def validated(components: ValidatedComponent*)(block: => Unit): Boolean = {
for (c <- components) {
if (!c.validated({})) {
return false
}
}
block
true
}
implicit def application: SPPNavigableApplication = Option(
getApplication.asInstanceOf[SPPNavigableApplication]).getOrElse(SpringWebApplication.current.navigable)
application.setLocale(Locale.ENGLISH)
def window = controller.application.window
}
import application.SPPNavigableApplication
import org.springframework.web.context.support.WebApplicationContextUtils
import javax.servlet.ServletConfig
import javax.servlet.ServletContext
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.vaadin.navigator7.WebApplication
import org.springframework.web.context.WebApplicationContext
object SpringWebApplication {
val field = classOf[WebApplication].getDeclaredField("staticReference")
field.setAccessible(true)
def staticReference = field.get(null)
def staticReference_=(ref: WebApplication) = field.set(null, ref)
def current: SpringWebApplication = WebApplication.getCurrent.asInstanceOf[SpringWebApplication]
def init(servletConfig: ServletConfig, servletContext: ServletContext, classLoader: ClassLoader) {
synchronized {
val webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
val webApplication = if (staticReference == null) {
val applicationClassName = servletConfig.getInitParameter("webApplication")
val applicationClass: Class[_ <: SpringWebApplication] = if (applicationClassName == null) {
classOf[SpringWebApplication]
} else {
val clazz: Class[_] = classLoader.loadClass(applicationClassName)
if (!classOf[SpringWebApplication].isAssignableFrom(clazz)) {
sys.error(
"Class given as parameter is no subclass of SpringWebApplication (and it should): " +
clazz.getName +
". Check your web.xml configuration (init-param of the an ApplicationServlet configuration).")
}
clazz.asInstanceOf[Class[_ <: SpringWebApplication]]
}
webApplicationContext.getBean(applicationClass)
} else {
staticReference.asInstanceOf[SpringWebApplication]
}
webApplication.applicationContext = webApplicationContext
servletContext.setAttribute(WebApplication.WEBAPPLICATION_CONTEXT_ATTRIBUTE_NAME, webApplication)
staticReference = webApplication
}
}
def beforeService(request: HttpServletRequest, response: HttpServletResponse,
servletContext: ServletContext) {
WebApplication.beforeService(request, response, servletContext)
}
def afterService(request: HttpServletRequest, response: HttpServletResponse,
servletContext: ServletContext) {
WebApplication.afterService(request, response, servletContext)
}
}
class SpringWebApplication extends org.vaadin.navigator7.WebApplication {
protected var applicationContext: WebApplicationContext = _
def context = applicationContext
def webContext = navigable.getContext.asInstanceOf[com.vaadin.terminal.gwt.server.WebApplicationContext]
def servletContext = webContext.getHttpSession.getServletContext
def getBean[T](clazz: Class[T]): T = bean(Manifest.classType(clazz))
def bean[T: Manifest]: T = context.getBean(manifest[T].erasure.asInstanceOf[Class[T]])
implicit def navigable = org.vaadin.navigator7.NavigableApplication.getCurrent
.asInstanceOf[SPPNavigableApplication]
def window = org.vaadin.navigator7.NavigableApplication.getCurrentNavigableAppLevelWindow
def navigator = window.getNavigator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment