KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > guiapp > xui > XuiContainer


1 /*
2  * $Id: XuiContainer.java 7215 2006-04-06 14:42:02Z les7arts $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.guiapp.xui;
26
27 import java.util.Locale JavaDoc;
28
29 import javax.swing.JFrame JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31
32 import net.xoetrope.swing.XApplet;
33
34 import org.ofbiz.base.container.Container;
35 import org.ofbiz.base.container.ContainerConfig;
36 import org.ofbiz.base.container.ContainerException;
37 import org.ofbiz.base.util.UtilValidate;
38 import org.ofbiz.entity.GenericDelegator;
39 import org.ofbiz.service.GenericDispatcher;
40 import org.ofbiz.service.GenericServiceException;
41 import org.ofbiz.service.LocalDispatcher;
42 import org.ofbiz.base.util.UtilProperties;
43
44 /**
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 7215 $
48  * @since 3.1
49  */

50 public abstract class XuiContainer implements Container {
51
52     public static final String JavaDoc module = XuiContainer.class.getName();
53     protected static XuiSession session = null;
54
55     protected XuiScreen initialScreen = null;
56
57     protected String JavaDoc startupFile = null;
58     protected String JavaDoc configFile = null;
59
60     public void init(String JavaDoc[] args, String JavaDoc configFile) throws ContainerException {
61         this.configFile = configFile;
62     }
63
64     public boolean start() throws ContainerException {
65         // make sure the subclass sets the config name
66
if (this.getContainerConfigName() == null) {
67             throw new ContainerException("Unknown container config name");
68         }
69         // get the container config
70
ContainerConfig.Container cc = ContainerConfig.getContainer(this.getContainerConfigName(), configFile);
71         if (cc == null) {
72             throw new ContainerException("No " + this.getContainerConfigName() + " configuration found in container config!");
73         }
74
75         // get the delegator
76
String JavaDoc delegatorName = ContainerConfig.getPropertyValue(cc, "delegator-name", "default");
77         GenericDelegator delegator = GenericDelegator.getGenericDelegator(delegatorName);
78
79         // get the dispatcher
80
String JavaDoc dispatcherName = ContainerConfig.getPropertyValue(cc, "dispatcher-name", "xui-dispatcher");
81         LocalDispatcher dispatcher = null;
82         try {
83             dispatcher = GenericDispatcher.getLocalDispatcher(dispatcherName, delegator);
84         } catch (GenericServiceException e) {
85             throw new ContainerException(e);
86         }
87
88         // get the pre-defined session ID
89
String JavaDoc xuiSessionId = ContainerConfig.getPropertyValue(cc, "xui-session-id", null);
90         if (UtilValidate.isEmpty(xuiSessionId)) {
91             throw new ContainerException("No xui-session-id value set in " + this.getContainerConfigName() + "!");
92         }
93
94         String JavaDoc laf = ContainerConfig.getPropertyValue(cc, "look-and-feel", null);
95         if (UtilValidate.isNotEmpty(laf)) {
96             try {
97                 UIManager.setLookAndFeel(laf);
98             } catch (Exception JavaDoc e) {
99                 throw new ContainerException(e);
100             }
101         }
102
103         // create and cache the session
104
session = new XuiSession(xuiSessionId, delegator, dispatcher, this);
105
106         // configure the rest of the container
107
this.configure(cc);
108
109         // load the XUI and render the initial screen
110
if (this.startupFile == null) {
111             this.startupFile = ContainerConfig.getPropertyValue(cc, "startup-file", "xui.properties");
112         }
113         this.initialScreen = new XuiScreen();
114         this.initialScreen.setup(this.startupFile);
115
116         return true;
117     }
118
119     public void stop() throws ContainerException {
120     }
121
122     public String JavaDoc getXuiPropertiesName() {
123         return this.startupFile;
124     }
125
126     /**
127      * @return String the name of the container name property
128      */

129     public abstract String JavaDoc getContainerConfigName();
130
131     /**
132      * Implementation specific configuration from the container config
133      * This method is called after the initial XUI configuration, after
134      * the session creation; before the initial screen is rendered.
135      *
136      * @param cc The container config object used to obtain the information
137      * @throws ContainerException
138      */

139     public abstract void configure(ContainerConfig.Container cc) throws ContainerException;
140
141     public static XuiSession getSession() {
142         return session;
143     }
144
145     class XuiScreen extends XApplet {
146
147         public void setup(String JavaDoc startupFile) {
148             String JavaDoc xuiProps = System.getProperty("ofbiz.home") + "/applications/pos/config/" + startupFile;
149             String JavaDoc suffix = Locale.getDefault().getLanguage();
150             if ("en" == suffix )
151                 suffix = "";
152             else
153                 suffix = "_" + suffix;
154             UtilProperties.setPropertyValue(xuiProps, "Language", "XuiLabels" + suffix);
155             JFrame JavaDoc frame = new JFrame JavaDoc();
156             frame.setUndecorated(true);
157             frame.setVisible(false);
158             frame.getContentPane().add(this);
159             super.setup(frame, new String JavaDoc[] { startupFile });
160         }
161     }
162 }
163
Popular Tags