KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > XMetaLController


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.editors;
20
21 import java.net.URI JavaDoc;
22
23 import org.openharmonise.him.*;
24 import org.openharmonise.vfs.context.*;
25 import org.openharmonise.vfs.servers.*;
26
27 import com.sq.xmetal.api.*;
28 import com.sq.types.*;
29
30 /**
31  * Controls integration with XMetaL. This is the bridge from Harmonise
32  * Information Manager to the XMetaL Java API.
33  *
34  * @author Matthew Large
35  * @version $Revision: 1.2 $
36  *
37  */

38 public class XMetaLController implements ContextListener {
39     
40     /**
41      * Instance, following singleton pattern.
42      */

43     private static XMetaLController m_instance = null;
44     
45     /**
46      * XMetaL application reference.
47      */

48     private Application m_xmApplication = null;
49
50     /**
51      * true if XMetaL has been launched.
52      */

53     private boolean isXMetaLRunning = false;
54
55     public XMetaLController() {
56         ContextHandler.getInstance().addListener(ContextType.CONTEXT_SHUTDOWN, this);
57     }
58
59     /**
60      * Returns the instance, follows singleton pattern.
61      *
62      * @return Instance
63      * @throws Exception
64      */

65     public static XMetaLController getInstance() throws Exception JavaDoc {
66         if (m_instance == null) {
67             m_instance = new XMetaLController();
68         }
69
70         return m_instance;
71     }
72
73     /**
74      * Opens a local working file in XMetaL.
75      *
76      * @param sPath Path to local working file
77      */

78     public synchronized void openFile( String JavaDoc sPath ) {
79       try {
80         this.startXMetaL();
81         this.m_xmApplication.getDocuments().Open(sPath);
82         this.m_xmApplication.getActiveDocument().setStructureViewVisible(false);
83       } catch (Exception JavaDoc e) {
84         e.printStackTrace( System.out );
85       }
86     }
87
88     /**
89      * Opens a string as a document in XMetaL.
90      *
91      * @param sContents Contents to open
92      */

93     public synchronized void openContents( String JavaDoc sContents ) {
94       try {
95         this.startXMetaL();
96         this.m_xmApplication.getDocuments().OpenString(sContents);
97         
98       } catch (Exception JavaDoc e) {
99         e.printStackTrace( System.out );
100       }
101     }
102
103     /**
104      * Launches and configures XMetaL.
105      *
106      */

107     private synchronized void startXMetaL() {
108       if( !isXMetaLRunning ) {
109           try {
110               m_xmApplication = new Application();
111               m_xmApplication.PreventExit("ExplorerApp",
112                                           "XMetaL will shutdown when you have closed Harmonise Information Manager.");
113     
114               
115               SQDispatch dispatch = m_xmApplication.getResourceManager().getAssets().getWebBrowser();
116     
117               m_xmApplication.getResourceManager().setVisible(true);
118               m_xmApplication.getResourceManager().SelectTab("Assets");
119               
120               SQWebBrowser browser = new SQWebBrowser(dispatch);
121               Server server = ServerList.getInstance().getHarmoniseServer();
122               URI JavaDoc uri = server.getURI();
123               String JavaDoc sURL = "http://" + uri.getHost() + ":" + uri.getPort() + "/webdav/servlet/HarmoniseServlet?Page/@id=3000";
124               System.out.println("Asset URL=[" + sURL + "]");
125               SQVariant url = new SQVariant(sURL);
126               browser.Navigate2(url);
127     
128               m_xmApplication.getResourceManager().setVisible(true);
129               m_xmApplication.getResourceManager().SelectTab("Assets");
130               
131               m_xmApplication.DisablePlainTextView();
132           } catch (Exception JavaDoc e2) {
133             e2.printStackTrace(System.err);
134           }
135
136         isXMetaLRunning=true;
137       }
138     }
139
140     /**
141      * Closes XMetaL.
142      *
143      */

144     public void shutdown() {
145         if (isXMetaLRunning) {
146             m_xmApplication.AllowExit("ExplorerApp");
147             m_xmApplication.getDocuments().Close();
148
149             try {
150               m_xmApplication.Quit();
151             } catch (Exception JavaDoc e) {
152               e.printStackTrace(System.out);
153             } finally {
154               m_xmApplication=null;
155             }
156
157             System.gc();
158             SQRuntime.releaseAll();
159
160             isXMetaLRunning = false;
161         }
162     }
163
164     /* (non-Javadoc)
165      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
166      */

167     public void contextMessage(ContextEvent ce) {
168         this.shutdown();
169     }
170 }
171
Popular Tags