]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/Application.js
Implement perspective auto-detection and switching
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / argeo-ria-src / class / org / argeo / ria / Application.js
index ce299c444ea7ef0404c887f17a99aface1e9eb8b..793bb90565078c88f9ffec1dcb554e179eb08c24 100644 (file)
 qx.Class.define("org.argeo.ria.Application",\r
 {\r
   extend : qx.application.Standalone,\r
-  settings : {\r
-       //"slc.Perspective" : "org.argeo.ria.custom.Perspective"\r
+  \r
+  statics : {\r
+       INSTANCE : null \r
   },\r
   \r
-  /*\r
-  *****************************************************************************\r
-     MEMBERS\r
-  *****************************************************************************\r
-  */\r
+  properties : {\r
+       perspectives : {\r
+               check : "Map",\r
+               init : {}\r
+       },\r
+       activePerspectiveName : {\r
+               check : "String",\r
+               init : ""\r
+       },\r
+       activePerspective : {\r
+               init : null\r
+       },\r
+       commandsDefinitions : {\r
+               init : {\r
+                       "stop" : {\r
+                               label           : "Stop", \r
+                               icon            : "resource/slc/process-stop.png",\r
+                               shortcut        : "Control+s",\r
+                               enabled         : false,\r
+                               menu            : null,\r
+                               toolbar         : "list",\r
+                               callback        : function(e){},\r
+                               command         : null\r
+                       },\r
+                       "switchperspective" : {\r
+                               label           : "Switch Perspective", \r
+                               icon            : "resource/slc/view-pane-tree.png",\r
+                               shortcut        : "",\r
+                               enabled         : true,\r
+                               menu            : "View",\r
+                               toolbar         : false,\r
+                               submenu         : [],\r
+                               submenuCallback : function(commandId){\r
+                                       // Defer execution to assure that the submenu is closed \r
+                                       // before it is rebuilt.\r
+                                       qx.event.Timer.once(function(){\r
+                                               org.argeo.ria.Application.INSTANCE.loadPerspective(commandId);\r
+                                       }, this, 10);                                   \r
+                               },\r
+                               callback        : function(e){},\r
+                               command         : null                                  \r
+                       },                      \r
+                       "log" : {\r
+                               label           : "Show Console", \r
+                               icon            : "resource/slc/help-contents.png",\r
+                               shortcut        : "",\r
+                               enabled         : true,\r
+                               menu            : "View",\r
+                               menuPosition: "last",\r
+                               toolbar         : false,\r
+                               callback        : function(e){                                          \r
+                                       org.argeo.ria.components.Logger.getInstance().toggle();\r
+                               }, \r
+                               command         : null\r
+                       },\r
+                       "help" : {\r
+                               label           : "About...", \r
+                               icon            : "resource/slc/help-about.png",\r
+                               shortcut        : "Control+h",\r
+                               enabled         : true,\r
+                               menu            : "View",\r
+                               toolbar         : false,\r
+                               callback        : function(e){\r
+                                       var win = new org.argeo.ria.components.Modal("About SLC", null, "SLC is a product from Argeo.");\r
+                                       win.attachAndShow();\r
+                               }, \r
+                               command         : null\r
+                       }\r
+               }\r
+       }\r
+  },\r
 \r
   members :\r
   {\r
@@ -40,6 +107,7 @@ qx.Class.define("org.argeo.ria.Application",
     {\r
       // Call super class\r
       this.base(arguments);\r
+      this.self(arguments).INSTANCE = this;\r
       this.views = {};\r
       \r
       var viewsManager = org.argeo.ria.components.ViewsManager.getInstance();\r
@@ -60,11 +128,43 @@ qx.Class.define("org.argeo.ria.Application",
       var container = new qx.ui.container.Composite(layout);\r
       viewsManager.setViewPanesContainer(container);\r
       // Document is the application root      \r
-      this.getRoot().add(container, {left:0,right:0,top:0,bottom:0});      \r
+      this.getRoot().add(container, {left:0,right:0,top:0,bottom:0});\r
+      \r
+      // Find available perspectives\r
+      var allPerspectives = {};\r
+      for(var key in qx.Bootstrap.$$registry){\r
+       if(qx.Class.hasInterface(qx.Bootstrap.$$registry[key], org.argeo.ria.components.IPerspective)){\r
+               allPerspectives[key] = qx.Bootstrap.$$registry[key];\r
+       }\r
+      }\r
+      var perspectiveNumber = qx.lang.Object.getLength(allPerspectives);\r
+      if(!perspectiveNumber){\r
+       this.error("Cannot find a perspective for startup!");\r
+       return;\r
+      }\r
+      this.setPerspectives(allPerspectives);\r
+      // Choose startup perspective, delete switch menu if only one perspective.\r
+      if(perspectiveNumber <= 1){\r
+                delete this.getCommandsDefinitions()["switchperspective"];\r
+                this.setActivePerspectiveName(qx.lang.Object.getKeys(allPerspectives)[0]);\r
+      }\r
+      else{\r
+       var startupSetting;\r
+       try{\r
+               startupSetting = qx.core.Setting.get("ria.StartupPerspective");\r
+       }catch(e){}\r
+       if(startupSetting && allPerspectives[startupSetting]){\r
+               this.setActivePerspectiveName(startupSetting);\r
+       }else{\r
+               this.setActivePerspectiveName(qx.lang.Object.getKeys(allPerspectives)[0]);\r
+       }\r
+       this.rebuildPerspectiveMenus();\r
+      }\r
          \r
       var menuBar = new qx.ui.menubar.MenuBar();\r
       var toolbar = new qx.ui.toolbar.ToolBar();\r
       var commandManager = org.argeo.ria.event.CommandsManager.getInstance();\r
+      commandManager.init(this.getCommandsDefinitions());\r
       commandManager.createCommands();\r
       commandManager.registerMenuBar(menuBar);\r
       commandManager.registerToolBar(toolbar);\r
@@ -78,22 +178,48 @@ qx.Class.define("org.argeo.ria.Application",
       container.add(menuBar);\r
       container.add(toolbar);      \r
 \r
-      /**\r
-       * Call GuiLoader to load default perspective and associated views.\r
-       */\r
-      var perspectiveClassName  = qx.core.Setting.get("ria.Perspective")\r
-      if(!perspectiveClassName){\r
-       this.error("Cannot find default perspective setting ! Please check your config.json file! (CUSTOM_PERSPECTIVE, line 23).");\r
-       return;\r
-      }\r
-      var perspectiveClass = qx.Class.getByName(perspectiveClassName);\r
-      if(!perspectiveClass){\r
-       this.error("Cannot find class for default perspective ("+perspectiveClassName+"). Please check your config.json file! (CUSTOM_PERSPECTIVE, line 23).");\r
-       return;\r
-      }\r
-      var perspective = new perspectiveClass;\r
-      perspective.initViewPanes(viewsManager);\r
-      perspective.initViews(viewsManager);      \r
+      this.loadPerspective();\r
+    },\r
+    \r
+    loadPerspective : function(perspectiveName){\r
+       if(perspectiveName){\r
+               this.setActivePerspectiveName(perspectiveName);\r
+               this.rebuildPerspectiveMenus();\r
+       }else{\r
+               perspectiveName = this.getActivePerspectiveName();\r
+       }\r
+               var viewsManager = org.argeo.ria.components.ViewsManager.getInstance();\r
+               if(this.getActivePerspective()){\r
+                       this.getActivePerspective().remove(viewsManager);\r
+               }\r
+       var allPerspectives = this.getPerspectives();           \r
+               var perspectiveClass = allPerspectives[perspectiveName];\r
+               if(!perspectiveClass){\r
+                       this.error("Cannot find class for startup perspective : "+perspectiveName);\r
+                       return;\r
+               }\r
+               var perspective = new perspectiveClass;\r
+               perspective.initViewPanes(viewsManager);\r
+               perspective.initViews(viewsManager);\r
+               this.setActivePerspective(perspective);\r
+    },\r
+    \r
+    rebuildPerspectiveMenus : function(){\r
+            var switchCommand = this.getCommandsDefinitions()["switchperspective"];\r
+            switchCommand.submenu = [];\r
+            var allPerspectives = this.getPerspectives();\r
+            for(var key in allPerspectives){\r
+               switchCommand.submenu.push({\r
+                       "label":(allPerspectives[key].LABEL || key)+(key==this.getActivePerspectiveName()?" (current)":""),\r
+                       "icon" :(allPerspectives[key].ICON || null),\r
+                       "commandId":key,\r
+                       "disabled" : (key==this.getActivePerspectiveName()?true:false)\r
+               });\r
+            }\r
+               if(switchCommand.command){ // Command already created : force reload\r
+                       switchCommand.command.clearMenus();\r
+                       switchCommand.command.setMenu(switchCommand.submenu);\r
+               }\r
     }\r
                \r
   }\r