001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.editors;
020:
021: import java.net.URI;
022:
023: import org.openharmonise.him.*;
024: import org.openharmonise.vfs.context.*;
025: import org.openharmonise.vfs.servers.*;
026:
027: import com.sq.xmetal.api.*;
028: import com.sq.types.*;
029:
030: /**
031: * Controls integration with XMetaL. This is the bridge from Harmonise
032: * Information Manager to the XMetaL Java API.
033: *
034: * @author Matthew Large
035: * @version $Revision: 1.2 $
036: *
037: */
038: public class XMetaLController implements ContextListener {
039:
040: /**
041: * Instance, following singleton pattern.
042: */
043: private static XMetaLController m_instance = null;
044:
045: /**
046: * XMetaL application reference.
047: */
048: private Application m_xmApplication = null;
049:
050: /**
051: * true if XMetaL has been launched.
052: */
053: private boolean isXMetaLRunning = false;
054:
055: public XMetaLController() {
056: ContextHandler.getInstance().addListener(
057: ContextType.CONTEXT_SHUTDOWN, this );
058: }
059:
060: /**
061: * Returns the instance, follows singleton pattern.
062: *
063: * @return Instance
064: * @throws Exception
065: */
066: public static XMetaLController getInstance() throws Exception {
067: if (m_instance == null) {
068: m_instance = new XMetaLController();
069: }
070:
071: return m_instance;
072: }
073:
074: /**
075: * Opens a local working file in XMetaL.
076: *
077: * @param sPath Path to local working file
078: */
079: public synchronized void openFile(String sPath) {
080: try {
081: this .startXMetaL();
082: this .m_xmApplication.getDocuments().Open(sPath);
083: this .m_xmApplication.getActiveDocument()
084: .setStructureViewVisible(false);
085: } catch (Exception e) {
086: e.printStackTrace(System.out);
087: }
088: }
089:
090: /**
091: * Opens a string as a document in XMetaL.
092: *
093: * @param sContents Contents to open
094: */
095: public synchronized void openContents(String sContents) {
096: try {
097: this .startXMetaL();
098: this .m_xmApplication.getDocuments().OpenString(sContents);
099:
100: } catch (Exception e) {
101: e.printStackTrace(System.out);
102: }
103: }
104:
105: /**
106: * Launches and configures XMetaL.
107: *
108: */
109: private synchronized void startXMetaL() {
110: if (!isXMetaLRunning) {
111: try {
112: m_xmApplication = new Application();
113: m_xmApplication
114: .PreventExit("ExplorerApp",
115: "XMetaL will shutdown when you have closed Harmonise Information Manager.");
116:
117: SQDispatch dispatch = m_xmApplication
118: .getResourceManager().getAssets()
119: .getWebBrowser();
120:
121: m_xmApplication.getResourceManager().setVisible(true);
122: m_xmApplication.getResourceManager()
123: .SelectTab("Assets");
124:
125: SQWebBrowser browser = new SQWebBrowser(dispatch);
126: Server server = ServerList.getInstance()
127: .getHarmoniseServer();
128: URI uri = server.getURI();
129: String sURL = "http://"
130: + uri.getHost()
131: + ":"
132: + uri.getPort()
133: + "/webdav/servlet/HarmoniseServlet?Page/@id=3000";
134: System.out.println("Asset URL=[" + sURL + "]");
135: SQVariant url = new SQVariant(sURL);
136: browser.Navigate2(url);
137:
138: m_xmApplication.getResourceManager().setVisible(true);
139: m_xmApplication.getResourceManager()
140: .SelectTab("Assets");
141:
142: m_xmApplication.DisablePlainTextView();
143: } catch (Exception e2) {
144: e2.printStackTrace(System.err);
145: }
146:
147: isXMetaLRunning = true;
148: }
149: }
150:
151: /**
152: * Closes XMetaL.
153: *
154: */
155: public void shutdown() {
156: if (isXMetaLRunning) {
157: m_xmApplication.AllowExit("ExplorerApp");
158: m_xmApplication.getDocuments().Close();
159:
160: try {
161: m_xmApplication.Quit();
162: } catch (Exception e) {
163: e.printStackTrace(System.out);
164: } finally {
165: m_xmApplication = null;
166: }
167:
168: System.gc();
169: SQRuntime.releaseAll();
170:
171: isXMetaLRunning = false;
172: }
173: }
174:
175: /* (non-Javadoc)
176: * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
177: */
178: public void contextMessage(ContextEvent ce) {
179: this.shutdown();
180: }
181: }
|