001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.guiapp.xui;
019:
020: import java.util.Locale;
021:
022: import javax.swing.JFrame;
023: import javax.swing.UIManager;
024:
025: import net.xoetrope.swing.XApplet;
026:
027: import org.ofbiz.base.container.Container;
028: import org.ofbiz.base.container.ContainerConfig;
029: import org.ofbiz.base.container.ContainerException;
030: import org.ofbiz.base.util.UtilValidate;
031: import org.ofbiz.entity.GenericDelegator;
032: import org.ofbiz.service.GenericDispatcher;
033: import org.ofbiz.service.GenericServiceException;
034: import org.ofbiz.service.LocalDispatcher;
035: import org.ofbiz.base.util.UtilProperties;
036:
037: public abstract class XuiContainer implements Container {
038:
039: public static final String module = XuiContainer.class.getName();
040: protected static XuiSession session = null;
041:
042: protected XuiScreen initialScreen = null;
043:
044: protected String startupDir = null;
045: protected String startupFile = null;
046: protected String configFile = null;
047:
048: public void init(String[] args, String configFile)
049: throws ContainerException {
050: this .configFile = configFile;
051: }
052:
053: public boolean start() throws ContainerException {
054: // make sure the subclass sets the config name
055: if (this .getContainerConfigName() == null) {
056: throw new ContainerException(
057: "Unknown container config name");
058: }
059: // get the container config
060: ContainerConfig.Container cc = ContainerConfig.getContainer(
061: this .getContainerConfigName(), configFile);
062: if (cc == null) {
063: throw new ContainerException("No "
064: + this .getContainerConfigName()
065: + " configuration found in container config!");
066: }
067:
068: // get the delegator
069: String delegatorName = ContainerConfig.getPropertyValue(cc,
070: "delegator-name", "default");
071: GenericDelegator delegator = GenericDelegator
072: .getGenericDelegator(delegatorName);
073:
074: // get the dispatcher
075: String dispatcherName = ContainerConfig.getPropertyValue(cc,
076: "dispatcher-name", "xui-dispatcher");
077: LocalDispatcher dispatcher = GenericDispatcher
078: .getLocalDispatcher(dispatcherName, delegator);
079:
080: // get the pre-defined session ID
081: String xuiSessionId = ContainerConfig.getPropertyValue(cc,
082: "xui-session-id", null);
083: if (UtilValidate.isEmpty(xuiSessionId)) {
084: throw new ContainerException(
085: "No xui-session-id value set in "
086: + this .getContainerConfigName() + "!");
087: }
088:
089: String laf = ContainerConfig.getPropertyValue(cc,
090: "look-and-feel", null);
091: if (UtilValidate.isNotEmpty(laf)) {
092: try {
093: UIManager.setLookAndFeel(laf);
094: } catch (Exception e) {
095: throw new ContainerException(e);
096: }
097: }
098:
099: // create and cache the session
100: session = new XuiSession(xuiSessionId, delegator, dispatcher,
101: this );
102:
103: // configure the rest of the container
104: this .configure(cc);
105:
106: // load the XUI and render the initial screen
107: if (this .startupFile == null) {
108: this .startupDir = ContainerConfig.getPropertyValue(cc,
109: "startup-directory", "/specialpurpose/pos/config/");
110: this .startupFile = ContainerConfig.getPropertyValue(cc,
111: "startup-file", "xpos.properties");
112: }
113: this .initialScreen = new XuiScreen();
114: this .initialScreen.setup(this .startupDir, this .startupFile);
115:
116: return true;
117: }
118:
119: public void stop() throws ContainerException {
120: }
121:
122: public String getXuiPropertiesName() {
123: return this .startupFile;
124: }
125:
126: /**
127: * @return String the name of the container name property
128: */
129: public abstract String 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)
140: throws ContainerException;
141:
142: public static XuiSession getSession() {
143: return session;
144: }
145:
146: class XuiScreen extends XApplet {
147:
148: public void setup(String startupDir, String startupFile) {
149: String xuiProps = System.getProperty("ofbiz.home")
150: + startupDir + startupFile;
151: String suffix = Locale.getDefault().getLanguage();
152: if ("en".equals(suffix)) {
153: suffix = "";
154: } else {
155: suffix = "_" + suffix;
156: }
157: UtilProperties.setPropertyValue(xuiProps, "Language",
158: "XuiLabels" + suffix);
159: JFrame frame = new JFrame();
160: frame.setUndecorated(true);
161: frame.setVisible(false);
162: frame.getContentPane().add(this );
163: super .setup(frame, new String[] { startupFile });
164: }
165: }
166: }
|