001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Fayçal SOUGRATI, Vincent Pautret, Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.ihm.main;
025:
026: import java.awt.Container;
027: import java.awt.Frame;
028: import java.awt.event.WindowEvent;
029: import java.awt.event.WindowListener;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.security.NoSuchAlgorithmException;
033: import java.util.Locale;
034:
035: import javax.swing.JApplet;
036: import javax.swing.JOptionPane;
037: import javax.swing.LookAndFeel;
038: import javax.swing.UIManager;
039:
040: import org.objectweb.salome_tmf.api.Api;
041: import org.objectweb.salome_tmf.api.JavaScriptUtils;
042: import org.objectweb.salome_tmf.api.MD5paswd;
043: import org.objectweb.salome_tmf.api.Util;
044: import org.objectweb.salome_tmf.ihm.languages.Language;
045: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
046: import org.objectweb.salome_tmf.ihm.tools.Tools;
047:
048: //import salome.plugins.gen_doc_xml.GenDocXMLPlugin;
049:
050: /**
051: * Applet repr?sentant l'outil de gestion de tests Voice Testing.
052: * @author teaml039
053: * @version 0.1
054: */
055: public class SalomeTMF extends JApplet implements BaseIHM,
056: WindowListener {
057:
058: boolean problemURL = false;
059:
060: URL urlSalome;
061: Frame ptrFrame;
062:
063: boolean closeDB = true;
064:
065: String strProject = null;
066: String strLogin = null;
067: int idConn = -1;
068:
069: /***************************************************************************************/
070: SalomeTMFContext pSalomeTMFContext;
071: SalomeTMFPanels pSalomeTMFPanels;
072: /****************************************************************************************/
073:
074: boolean standAlone = false;
075: static boolean exit = false;
076: JavaScriptUtils pJSUtils;
077:
078: public void init() {
079: standAlone = false;
080: Util.log("[SalomeTMF] - Applet init");
081: onInit();
082: exit = false;
083:
084: }
085:
086: void initComponent() {
087: //Api.setUrlBase(getDocumentBase());
088: Api.setUrlBase(urlSalome);
089: try {
090: Class lnfClass = Class.forName(UIManager
091: .getSystemLookAndFeelClassName());
092: LookAndFeel newLAF = (LookAndFeel) (lnfClass.newInstance());
093: UIManager.setLookAndFeel(newLAF);
094: Util.adaptFont();
095: } catch (Exception exc) {
096: System.err.println("Error loading L&F: " + exc);
097: }
098: Util.log("[SalomeTMF->initComponent] Used Local is "
099: + Api.getUsedLocale());
100: Language.getInstance().setLocale(
101: new Locale(Api.getUsedLocale()));
102:
103: ptrFrame = javax.swing.JOptionPane
104: .getFrameForComponent(SalomeTMF.this );
105: ptrFrame.addWindowListener(this );
106:
107: pSalomeTMFContext = new SalomeTMFContext(urlSalome, ptrFrame,
108: this );
109: pSalomeTMFPanels = new SalomeTMFPanels(pSalomeTMFContext, this );
110: pSalomeTMFPanels.initComponentPanel();
111:
112: Container cp = this .getContentPane();
113: cp.add(pSalomeTMFPanels.tabs);
114:
115: }
116:
117: boolean connectToAPI() {
118: Util.log("[SalomeTMF] - Try connection to API (NbConnect = "
119: + Api.getNbConnect() + ")");
120: //if (Api.isConnected() && !reload) {
121: pJSUtils = new JavaScriptUtils(this );
122: if (Api.getNbConnect() > 0) {
123: JOptionPane
124: .showMessageDialog(
125: SalomeTMF.this ,
126: Language
127: .getInstance()
128: .getText(
129: "Vous_avez_déjà_une_session_Salomé_ouverte_avec_ce_navigateur.\n")
130: + Language
131: .getInstance()
132: .getText(
133: "Une_seule_session_est_autorisée_par_navigateur_afin_d'éviter_les_conflits_!"),
134: Language.getInstance().getText("Erreur_!"),
135: JOptionPane.ERROR_MESSAGE);
136: quit(false, false);
137: closeDB = false;
138: return false;
139: // Api.setConnected(true);
140: } else {
141: Api.openConnection(getDocumentBase());
142:
143: //reload = true;
144: if (!Api.isConnected()) {
145: JOptionPane.showMessageDialog(SalomeTMF.this ,
146: "Can't connect to the database", Language
147: .getInstance().getText("Erreur_!"),
148: JOptionPane.ERROR_MESSAGE);
149: quit(true, false);
150: return false;
151: }
152: /*if (Api.isIDE_DEV()) {
153: tab[0]="projet_test";
154: tab[1] = "marchemi";
155: } else {
156: tab = Tools.chooseProjectAndUser(getDocumentBase());
157: }*/
158:
159: //Tools.chooseProjectAndUser(getDocumentBase());
160: strLogin = pJSUtils.getLoginCookies();
161: strProject = pJSUtils.getProjectCookies();
162:
163: Api.initConnectionUser(strProject, strLogin);
164: // Api.getConnectionProject(idConnection);
165: // Api.getConnectionUser(idConnection);
166:
167: if ((strProject == null) || (strProject.equals(""))) {
168: ProjectLogin dialog = new ProjectLogin(pJSUtils);
169: dialog.setVisible(true);
170: if (dialog.getSelectedProject() != null
171: && dialog.getSelectedUser() != null) {
172: strProject = dialog.getSelectedProject();
173: strLogin = dialog.getSelectedUser();
174: Util.log("project name: " + strProject);
175: Util.log("user name: " + strLogin);
176:
177: // Récupération du login et du mot de passe pour l'authentification
178: try {
179: Api.setStrUsername(strLogin);
180: Api.setStrMD5Password(MD5paswd
181: .getEncodedPassword(dialog
182: .getSelectedPassword()));
183: } catch (Exception e) {
184: e.printStackTrace();
185: }
186:
187: Api.initConnectionUser(strProject, strLogin);
188: standAlone = true;
189: } else {
190: quit(true, true);
191: return false;
192: }
193: } else {
194: Api.initConnectionUser(strProject, strLogin);
195: }
196:
197: /*if ((tab[0] == null)||(tab[0].equals(""))) {
198: JOptionPane.showMessageDialog(SalomeTMF.this,
199: Language.getInstance().getText("Probleme_dans_l'URL_saisie_!"),
200: Language.getInstance().getText("Erreur_!"),
201: JOptionPane.ERROR_MESSAGE);
202: problemURL = true;
203: //Api.closeConnection();
204: quit(true, true);
205: return false;
206: } else {
207: try{
208: String[] tab2 = getDocumentBase().toString().split("[?=]");
209: idConn = Integer.parseInt(tab2[2]);
210: } catch(Exception e1) {
211: Util.log("[SalomeTMF->connectToAPI] WARNING idConn can't be found");
212: }
213: strProject = tab[0];
214: strLogin = tab[1];
215:
216: }*/
217: }
218: //urlSalome = getDocumentBase();
219: urlSalome = Api.getCodeBase();
220: return true;
221: }
222:
223: void loadPlugin() {
224: pSalomeTMFContext.loadPlugin(pSalomeTMFPanels);
225: }
226:
227: void loadModel() {
228: if (strProject == null || strLogin == null) {
229: quit(true, true);
230: }
231: DataModel.loadFromBase(strProject, strLogin, this );
232: pSalomeTMFPanels.loadModel(strProject, strLogin);
233: }
234:
235: /**
236: * M?thode appel?e ? chaque chargement de la page
237: */
238: public void start() {
239: Util.log("[SalomeTMF] - Applet start");
240: onStart();
241: }
242:
243: /*********************************************/
244:
245: void onStart() {
246: pSalomeTMFContext.startPlugin();
247: }
248:
249: void onStop() {
250:
251: }
252:
253: void onInit() {
254: Util.log("[SalomeTMF] - Applet init");
255: System.runFinalization();
256: System.gc();
257: if (connectToAPI()) {
258: WaitView waitView = new WaitView();
259: initComponent();
260: loadModel();
261: loadPlugin();
262: waitView.dispose();
263: }
264:
265: }
266:
267: /**
268: * M?thode appel? lorsque l'utilisateur quitte l'application.
269: */
270: public void quit(boolean do_recup, boolean doclose) {
271: Util
272: .log("[SalomeTMF] - Applet quit (recup = " + do_recup
273: + ")");
274: if (pSalomeTMFContext != null && doclose == true) {
275: pSalomeTMFContext.suspendPlugin();
276: }
277: if (Api.isConnected() && doclose) {
278: if ((!problemURL) && (!Api.isIDE_DEV()) && idConn != -1) {
279:
280: Api.deleteConnection(idConn);
281: }
282: closeDB = false;
283: Api.closeConnection();
284: }
285: URL recup;
286:
287: ptrFrame.dispose();
288: ptrFrame = null;
289: System.gc();
290: System.runFinalization();
291:
292: if (!standAlone) {
293: try {
294: if (do_recup) {
295: /* r?cup?re l'URL ? partir du document courant et "page.html" */
296: recup = new URL(getDocumentBase(), "index.html");
297: /* Affiche le document apr?s avoir recup?rer le contexte courant */
298: getAppletContext().showDocument(recup);
299: repaint();
300: } else {
301: getAppletContext()
302: .showDocument(
303: new URL(
304: "http://wiki.objectweb.org/salome-tmf"));
305: }
306: } catch (MalformedURLException me) {
307: me.printStackTrace();
308: }
309: } else {
310: if (pJSUtils != null) {
311: pJSUtils.closeWindow();
312: }
313: }
314: exit = true;
315: } // Fin de la m?thode quit();
316:
317: public void stop() {
318: Util.log("[SalomeTMF] - Applet stop");
319: }
320:
321: public void destroy() {
322: Util.log("[SalomeTMF] Applet destroy (closeDB = " + closeDB
323: + ")");
324: if (closeDB == true)
325: Api.closeConnection();
326: }
327:
328: /* void closeWindow(){
329: try {
330: //Class classJSObject = Class.forName("sun.plugin.javascript.JSObject");
331: Class classJSObject = Class.forName("netscape.javascript.JSObject");
332: Method meth_getWindow = classJSObject.getMethod("getWindow", new Class[] {Applet.class});
333: Object pJSObject = meth_getWindow.invoke(null ,new Object[] {this});
334: System.out.println("pJSObject = " + pJSObject);
335: Method meth_call = classJSObject.getMethod("call", new Class[] {String.class, Object[].class});
336: meth_call.invoke(pJSObject, new Object[] {"close", null});
337:
338: } catch (Exception e) {
339: e.printStackTrace();
340: }
341: }*/
342:
343: /**********************************************************************************************************************/
344:
345: public SalomeTMFContext getSalomeTMFContext() {
346: return pSalomeTMFContext;
347: }
348:
349: public SalomeTMFPanels getSalomeTMFPanels() {
350: return pSalomeTMFPanels;
351: }
352:
353: public void showDocument(URL toShow, String where) {
354: getAppletContext().showDocument(toShow, where);
355: }
356:
357: public boolean isGraphique() {
358: return true;
359: }
360:
361: /***************** Windows Listener ***************/
362: public void windowClosing(WindowEvent e) {
363: if (!exit)
364: quit(true, true);
365: }
366:
367: public void windowActivated(WindowEvent e) {
368: }
369:
370: public void windowClosed(WindowEvent e) {
371: }
372:
373: public void windowDeactivated(WindowEvent e) {
374: }
375:
376: public void windowDeiconified(WindowEvent e) {
377: }
378:
379: public void windowIconified(WindowEvent e) {
380: }
381:
382: public void windowOpened(WindowEvent e) {
383: }
384:
385: } // Fin de la classe SalomeTMF
|