Entity link
authorTomas Zeman <tzeman@volny.cz>
Fri, 13 Apr 2012 23:12:25 +0200
changeset 57 14557aef6287
parent 56 9409e7ab3f9d
child 58 4fa878322e47
Entity link
src/main/scala/fis/base/ui/EntityLink.scala
src/main/webapp/templates-hidden/_resources.html
src/main/webapp/templates-hidden/_resources_cs.html
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/base/ui/EntityLink.scala	Fri Apr 13 23:12:25 2012 +0200
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2011-2012 Tomas Zeman <tzeman@volny.cz>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package fis.base.ui
+
+import fis.base.model.Entity
+import java.util.concurrent.{ConcurrentHashMap => CHash}
+import net.liftweb.common._
+import net.liftweb.util._
+import net.tz.lift.model._
+import net.tz.lift.snippet._
+import scala.xml.Text
+
+abstract class EntityLink[T <: Entity[_]](val e: T, link: T => Box[String])
+  extends ReadableField {
+
+  type ValueType = String
+  def name = "linkName"
+  override def displayName = l10n(name)
+  override def asHtml = a(link(e))(Text(e.linkName))
+  def get = e.linkName
+  def is = e.linkName
+}
+
+object EntityLink {
+
+  type GenFunc[T <: Entity[_]] = T => EntityLink[T]
+
+  private val globalLinks: CHash[String, GenFunc[_]] = new CHash
+
+  def apply[T <: Entity[_]](v: T)(implicit man: Manifest[T]):
+    Box[EntityLink[T]] = {
+
+    val n = man.toString
+    val f = globalLinks.containsKey(n).box {
+      globalLinks.get(n).asInstanceOf[GenFunc[T]] }
+    f map { _(v) }
+  }
+
+  def register[T <: Entity[_]](func: GenFunc[T])(implicit man: Manifest[T]) = {
+    globalLinks.put(man.toString, func)
+    this
+  }
+}
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/webapp/templates-hidden/_resources.html	Thu Apr 12 17:07:35 2012 +0200
+++ b/src/main/webapp/templates-hidden/_resources.html	Fri Apr 13 23:12:25 2012 +0200
@@ -7,6 +7,7 @@
   <res name="Create" lang="en" default="true">Create</res>
   <res name="Edit" lang="en" default="true">Edit</res>
   <res name="Add" lang="en" default="true">Add</res>
+  <res name="linkName" lang="en" default="true">Name</res>
 
 
   <-- contact -->
--- a/src/main/webapp/templates-hidden/_resources_cs.html	Thu Apr 12 17:07:35 2012 +0200
+++ b/src/main/webapp/templates-hidden/_resources_cs.html	Fri Apr 13 23:12:25 2012 +0200
@@ -7,6 +7,7 @@
   <res name="Create" lang="cs">Vytvořit</res>
   <res name="Edit" lang="cs">Upravit</res>
   <res name="Add" lang="cs">Přidat</res>
+  <res name="linkName" lang="cs">Název</res>
 
 
   <-- contact -->