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