001: package org.objectweb.salome_tmf.test.common;
002:
003: import java.awt.Frame;
004: import java.awt.event.WindowEvent;
005: import java.awt.event.WindowListener;
006: import java.io.IOException;
007: import java.net.URL;
008: import java.util.Locale;
009:
010: import org.objectweb.salome_tmf.api.Api;
011: import org.objectweb.salome_tmf.ihm.languages.Language;
012: import org.objectweb.salome_tmf.ihm.main.BaseIHM;
013: import org.objectweb.salome_tmf.ihm.main.SalomeTMFContext;
014: import org.objectweb.salome_tmf.ihm.main.SalomeTMFPanels;
015: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
016:
017: public class SalomeProxy implements BaseIHM, WindowListener {
018:
019: private static String strProject = null;
020: private static String strLogin = null;
021: private static String urlSalome = null;
022:
023: Frame ptrFrame;
024:
025: /***************************************************************************************/
026: SalomeTMFContext pSalomeTMFContext;
027: SalomeTMFPanels pSalomeTMFPanels;
028:
029: /****************************************************************************************/
030:
031: public SalomeProxy() {
032: }
033:
034: /*public SalomeProxy(String url, String user, String project){
035: strLogin = user;
036: strProject = project;
037: urlSalome = url;
038: onInit();
039: quit(true, true);
040: }*/
041:
042: /******************** CLIF BEHAVIOR ***********************************/
043:
044: public void connect(String url, String user, String project) {
045: strLogin = user;
046: strProject = project;
047: urlSalome = url;
048: if (!onInit()) {
049: System.out.println("CONNECTION PROBLEM!");
050: }
051: }
052:
053: /*public void behaviorRunExec(String campName, String nameExec){
054:
055: try {
056: Campaign pCamp = DataModel.getCurrentProject().getCampaignFromModel(campName);
057: Execution pExec = pCamp.getExecutionFromModel("Exec0");
058: ArrayList arrayExec = new ArrayList();
059: arrayExec.add(pExec);
060: TestMethods.runExecution(arrayExec, ((Component) new Frame()), false, false);
061: } catch(Exception e){
062: e.printStackTrace();
063: }
064: }*/
065:
066: public void disconnect() {
067: quit(true, true);
068: }
069:
070: /************************************************************************************/
071:
072: public void quit(boolean do_recup, boolean doclose) {
073: if (Api.isConnected() && doclose) {
074: Api.closeConnection();
075: }
076: System.exit(0);
077: }
078:
079: /********************************************************/
080: boolean connectToAPI() {
081: boolean code = true;
082: try {
083: Api.openConnection(new URL(urlSalome));
084: if (!Api.isConnected()) {
085: quit(false, false);
086: }
087: Api.initConnectionUser(strProject, strLogin);
088: } catch (Exception e) {
089: e.printStackTrace();
090: code = false;
091: }
092: return code;
093: }
094:
095: boolean onInit() {
096: boolean code = true;
097: if (connectToAPI()) {
098: initComponent();
099: loadModel();
100: //loadPlugin();
101: } else {
102: code = false;
103: }
104: return code;
105: }
106:
107: void loadPlugin() {
108: pSalomeTMFContext.loadPlugin(pSalomeTMFPanels);
109: }
110:
111: void loadModel() {
112: DataModel.loadFromBase(strProject, strLogin, this );
113: }
114:
115: void initComponent() {
116: try {
117: URL pUrlSalome = new URL(urlSalome);
118: Api.setUrlBase(pUrlSalome);
119: Language.getInstance().setLocale(
120: new Locale(Api.getUsedLocale()));
121: //ptrFrame = new Frame();
122: ptrFrame = null;
123: //ptrFrame.addWindowListener(this);
124: //ptrFrame.setVisible(true);
125: pSalomeTMFContext = new SalomeTMFContext(pUrlSalome,
126: ptrFrame, this );
127: //pSalomeTMFPanels = new SalomeTMFPanels(pSalomeTMFContext, this);
128: //pSalomeTMFPanels.initComponentPanel();
129:
130: } catch (Exception e) {
131: e.printStackTrace();
132: }
133: }
134:
135: public SalomeTMFContext getSalomeTMFContext() {
136: return pSalomeTMFContext;
137: }
138:
139: public SalomeTMFPanels getSalomeTMFPanels() {
140: return pSalomeTMFPanels;
141: }
142:
143: public void showDocument(URL toShow, String where) {
144: switch (getPlatform()) {
145: case (WIN_ID): {
146: System.out.println("Try Windows Command Line");
147: runCmdLine(replaceToken(WIN_CMDLINE, URLTOKEN, toShow
148: .toString()));
149: }
150: case (MAC_ID): {
151: System.out.println("Try Mac Command Line");
152: runCmdLine(replaceToken(MAC_CMDLINE, URLTOKEN, toShow
153: .toString()));
154:
155: }
156: default:
157: System.out.println("Try Unix Command Line");
158: for (int i = 0; i < OTHER_CMDLINES.length; i++) {
159: if (runCmdLine(replaceToken(OTHER_CMDLINES[i],
160: URLTOKEN, toShow.toString()),
161: replaceToken(OTHER_FALLBACKS[i], URLTOKEN,
162: toShow.toString())))
163: return;
164: }
165: }
166: }
167:
168: public boolean isGraphique() {
169: return false;
170: }
171:
172: /******************/
173: /* This token is a placeholder for the actual URL */
174: private final String URLTOKEN = "%URLTOKEN%";
175:
176: // Used to identify the windows platform.
177: private final int WIN_ID = 1;
178:
179: // Used to discover the windows platform.
180: private final String WIN_PREFIX = "Windows";
181:
182: // The default system browser under windows.
183: // Once upon a time:
184: // for 'Windows 9' and 'Windows M': start
185: // for 'Windows': cmd /c start
186: private final String[] WIN_CMDLINE = { "rundll32",
187: "url.dll,FileProtocolHandler", URLTOKEN };
188:
189: // Used to identify the mac platform.
190: private final int MAC_ID = 2;
191:
192: // Used to discover the mac platform.
193: private final String MAC_PREFIX = "Mac";
194:
195: // The default system browser under mac.
196: private final String[] MAC_CMDLINE = { "open", URLTOKEN };
197:
198: // Used to identify the mac platform.
199: private final int OTHER_ID = -1;
200:
201: private final String[][] OTHER_CMDLINES = {
202:
203: // The first guess for a browser under other systems (and unix):
204: // Remote controlling firefox
205: // (http://www.mozilla.org/unix/remote.html)
206: { "firefox", "-remote",
207: "openURL(" + URLTOKEN + ",new-window)" },
208:
209: // Remote controlling mozilla
210: // (http://www.mozilla.org/unix/remote.html)
211: { "mozilla", "-remote",
212: "openURL(" + URLTOKEN + ",new-window)" },
213: // The second guess for a browser under other systems (and unix):
214: // The RedHat skript htmlview
215: { "htmlview", URLTOKEN },
216:
217: // The third guess for a browser under other systems (and unix):
218: // Remote controlling netscape
219: // (http://wp.netscape.com/newsref/std/x-remote.html)
220: { "netscape", "-remote", "openURL(" + URLTOKEN + ")" }
221:
222: };
223:
224: private final String[][] OTHER_FALLBACKS = {
225:
226: // Fallback for remote controlling mozilla:
227: //Starting up a new mozilla
228: { "firefox", URLTOKEN },
229:
230: // Starting up a new mozilla
231: { "mozilla", URLTOKEN },
232:
233: // No fallback for htmlview
234: null,
235:
236: // Fallback for remote controlling netscape:
237: // Starting up a new netscape
238: { "netscape", URLTOKEN }
239:
240: };
241:
242: private int getPlatform() {
243: String os = System.getProperty("os.name");
244: if (os != null && os.startsWith(WIN_PREFIX))
245: return WIN_ID;
246: if (os != null && os.startsWith(MAC_PREFIX))
247: return MAC_ID;
248: return OTHER_ID;
249: }
250:
251: private String[] replaceToken(String[] target, String token,
252: String replacement) {
253: if (null == target)
254: return null;
255: String[] result = new String[target.length];
256:
257: for (int i = 0; i < target.length; i++)
258: result[i] = target[i].replaceAll(token, replacement);
259:
260: return result;
261: }
262:
263: private boolean runCmdLine(String[] cmdLine) {
264: System.out.println("Try to execute commande line = "
265: + cmdLine);
266: return runCmdLine(cmdLine, null);
267: }
268:
269: private boolean runCmdLine(String[] cmdLine,
270: String[] fallBackCmdLine) {
271: try {
272: System.out.println("Try to execute commande line = "
273: + cmdLine + " with " + fallBackCmdLine);
274: /*
275: * System.out.println( "Trying to invoke browser, cmd='" +
276: * connectStringArray(cmdLine) + "' ... ");
277: */
278: Process p = Runtime.getRuntime().exec(cmdLine);
279:
280: if (null != fallBackCmdLine) {
281: // wait for exit code -- if it's 0, command worked,
282: // otherwise we need to start fallBackCmdLine.
283: int exitCode = p.waitFor();
284: if (exitCode != 0) {
285: /*
286: * System.out.println(exitCode); System.out.println();
287: */
288:
289: /*
290: * System.out.println( "Trying to invoke browser, cmd='" +
291: * connectStringArray(fallBackCmdLine) + "' ...");
292: */
293: Runtime.getRuntime().exec(fallBackCmdLine);
294:
295: }
296: }
297:
298: System.out.println();
299: return true;
300:
301: } catch (InterruptedException e) {
302: System.out.println("Caught: " + e);
303: } catch (IOException e) {
304: System.out.println("Caught: " + e);
305: }
306:
307: return false;
308: }
309:
310: /******************/
311:
312: /***************** Windows Listener ***************/
313: public void windowClosing(WindowEvent e) {
314: quit(true, true);
315: }
316:
317: public void windowActivated(WindowEvent e) {
318: }
319:
320: public void windowClosed(WindowEvent e) {
321: }
322:
323: public void windowDeactivated(WindowEvent e) {
324: }
325:
326: public void windowDeiconified(WindowEvent e) {
327: }
328:
329: public void windowIconified(WindowEvent e) {
330: }
331:
332: public void windowOpened(WindowEvent e) {
333: }
334:
335: public static void main(String[] args) {
336: SalomeProxy pSalomeTMF_Cmd = new SalomeProxy();
337: pSalomeTMF_Cmd
338: .connect(
339: "http://salome-tmf.rd.francetelecom.fr/www/salome-demo/salome_tmf/",
340: "marchemi", "salome-demo");
341: pSalomeTMF_Cmd.disconnect();
342: }
343:
344: }
|