001: package org.objectweb.salome_tmf.ihm.main;
002:
003: import java.awt.Container;
004: import java.awt.Font;
005: import java.awt.Frame;
006: import java.awt.event.WindowEvent;
007: import java.awt.event.WindowListener;
008: import java.net.URL;
009: import java.util.Locale;
010:
011: import javax.swing.JApplet;
012: import javax.swing.JOptionPane;
013: import javax.swing.LookAndFeel;
014: import javax.swing.UIManager;
015:
016: import org.objectweb.salome_tmf.api.Api;
017: import org.objectweb.salome_tmf.api.JavaScriptUtils;
018: import org.objectweb.salome_tmf.api.MD5paswd;
019: import org.objectweb.salome_tmf.api.Util;
020: import org.objectweb.salome_tmf.ihm.languages.Language;
021: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
022:
023: //import com.sun.java.browser.dom.DOMService;
024:
025: public class SalomeTMF_AppJWS extends JApplet implements BaseIHM,
026: WindowListener {
027: boolean problemURL = false;
028:
029: protected URL urlSalome;
030: protected Frame ptrFrame;
031:
032: boolean closeDB = true;
033:
034: protected String strProject = null;
035: protected String strLogin = null;
036: int idConn = -1;
037:
038: static boolean exit = false;
039:
040: /***************************************************************************************/
041: protected SalomeTMFContext pSalomeTMFContext;
042: protected SalomeTMFPanels pSalomeTMFPanels;
043: /****************************************************************************************/
044:
045: JApplet parentApplet = null;
046: JavaScriptUtils pJSUtils;
047:
048: public void setParentApplet(JApplet _parentApplet) {
049: parentApplet = _parentApplet;
050: System.out.println("SalomeTMF_AppJWS = "
051: + SalomeTMF_AppJWS.class.getClassLoader());
052: }
053:
054: public void init() {
055: if (parentApplet == null) {
056: parentApplet = this ;
057: }
058: Util.log("[SalomeTMF] - Applet init");
059: onInit();
060: exit = false;
061: }
062:
063: void initComponent() {
064: //Api.setUrlBase(getDocumentBase());
065: Api.setUrlBase(urlSalome);
066: try {
067: Class lnfClass = Class.forName(UIManager
068: .getSystemLookAndFeelClassName());
069: LookAndFeel newLAF = (LookAndFeel) (lnfClass.newInstance());
070: UIManager.setLookAndFeel(newLAF);
071: Util.adaptFont();
072: } catch (Exception exc) {
073: System.err.println("Error loading L&F: " + exc);
074: }
075: Util.log("[SalomeTMF->initComponent] Used Local is "
076: + Api.getUsedLocale());
077: Language.getInstance().setLocale(
078: new Locale(Api.getUsedLocale()));
079:
080: //ptrFrame = javax.swing.JOptionPane.getFrameForComponent(SalomeTMF_AppJWS.this); //FOR MINI BOOT
081: ptrFrame = javax.swing.JOptionPane
082: .getFrameForComponent(parentApplet);
083: if (parentApplet == this ) {
084: ptrFrame.addWindowListener(this );
085: }
086:
087: pSalomeTMFContext = new SalomeTMFContext(urlSalome, ptrFrame,
088: this );
089: pSalomeTMFPanels = new SalomeTMFPanels(pSalomeTMFContext, this );
090: pSalomeTMFPanels.initComponentPanel();
091:
092: //Container cp = this.getContentPane(); //FOR MINI BOOT
093: Container cp = parentApplet.getContentPane();
094: cp.add(pSalomeTMFPanels.tabs);
095:
096: //waitView.dispose();
097: /* try {
098: Cursor c = new Cursor(Cursor.DEFAULT_CURSOR);
099: this.setCursor(c);
100: } catch (Exception e) {
101: e.printStackTrace();
102: }*/
103: }
104:
105: void reInitComponent() {
106: try {
107: Class lnfClass = Class.forName(UIManager
108: .getSystemLookAndFeelClassName());
109: LookAndFeel newLAF = (LookAndFeel) (lnfClass.newInstance());
110: UIManager.setLookAndFeel(newLAF);
111: } catch (Exception exc) {
112: System.err.println("Error loading L&F: " + exc);
113: }
114:
115: ptrFrame = javax.swing.JOptionPane
116: .getFrameForComponent(parentApplet);
117: if (parentApplet == this ) {
118: ptrFrame.addWindowListener(this );
119: }
120:
121: //pSalomeTMFContext = new SalomeTMFContext(urlSalome,ptrFrame, this);
122: SalomeTMFContext.getInstance().ptrFrame = ptrFrame;
123: pSalomeTMFPanels = new SalomeTMFPanels(pSalomeTMFContext, this );
124: pSalomeTMFPanels.initComponentPanel();
125:
126: //Container cp = this.getContentPane(); //FOR MINI BOOT
127: Container cp = parentApplet.getContentPane();
128: cp.add(pSalomeTMFPanels.tabs);
129: }
130:
131: protected boolean connectToAPI() {
132: Util.log("[SalomeTMF] - Try connection to API (NbConnect = "
133: + Api.getNbConnect() + ")");
134: //if (Api.isConnected() && !reload) {
135: pJSUtils = new JavaScriptUtils(parentApplet);
136: if (Api.getNbConnect() > 0) {
137: //JOptionPane.showMessageDialog(SalomeTMF_AppJWS.this, //FOR MINI BOOT
138: JOptionPane
139: .showMessageDialog(
140: parentApplet,
141: Language
142: .getInstance()
143: .getText(
144: "Vous_avez_déjà_une_session_Salomé_ouverte_avec_ce_navigateur.\n")
145: + Language
146: .getInstance()
147: .getText(
148: "Une_seule_session_est_autorisée_par_navigateur_afin_d'éviter_les_conflits_!"),
149: Language.getInstance().getText("Erreur_!"),
150: JOptionPane.ERROR_MESSAGE);
151: quit(false, false);
152: closeDB = false;
153: return false;
154: } else {
155: //Api.openConnection(getDocumentBase()); //FOR MINI BOOT
156: Api.openConnection(parentApplet.getCodeBase());
157: if (!Api.isConnected()) {
158: //JOptionPane.showMessageDialog(SalomeTMF_AppJWS.this,////FOR MINI BOOT
159: JOptionPane.showMessageDialog(parentApplet,
160: "Can't connect to the database", Language
161: .getInstance().getText("Erreur_!"),
162: JOptionPane.ERROR_MESSAGE);
163: quit(true, false);
164: return false;
165: }
166: strLogin = pJSUtils.getLoginCookies();
167: strProject = pJSUtils.getProjectCookies();
168:
169: if (strProject == null && strLogin == null) {
170: ProjectLogin dialog = new ProjectLogin(pJSUtils);
171: dialog.setVisible(true);
172: if (dialog.getSelectedProject() != null
173: && dialog.getSelectedUser() != null) {
174: strProject = dialog.getSelectedProject();
175: strLogin = dialog.getSelectedUser();
176: // Récupération du login et du mot de passe pour l'authentification
177: try {
178: Api.setStrUsername(strLogin);
179: Api.setStrMD5Password(MD5paswd
180: .getEncodedPassword(dialog
181: .getSelectedPassword()));
182: } catch (Exception e) {
183: e.printStackTrace();
184: }
185: Api.initConnectionUser(strProject, strLogin);
186: } else {
187: quit(true, true);
188: return false;
189: }
190: } else {
191: Api.initConnectionUser(strProject, strLogin);
192: }
193:
194: }
195: //urlSalome = getDocumentBase();
196: urlSalome = Api.getCodeBase();
197: return true;
198: }
199:
200: void loadPlugin() {
201: pSalomeTMFContext.loadPlugin(pSalomeTMFPanels);
202: }
203:
204: void reloadPlugin() {
205: pSalomeTMFContext.reloadPlugin(pSalomeTMFPanels);
206: }
207:
208: void loadModel() {
209: if (strProject == null || strLogin == null) {
210: quit(true, true);
211: }
212: DataModel.loadFromBase(strProject, strLogin, this );
213: pSalomeTMFPanels.loadModel(strProject, strLogin);
214: }
215:
216: void reloadModel() {
217: if (strProject == null || strLogin == null) {
218: quit(true, true);
219: }
220: DataModel.loadFromBase(strProject, strLogin, this );
221: pSalomeTMFPanels.reloadModel();
222: }
223:
224: /**
225: * M?thode appel?e ? chaque chargement de la page
226: */
227: public void start() {
228: Util.log("[SalomeTMF] - Applet start");
229: onStart();
230: }
231:
232: /*********************************************/
233:
234: void onStart() {
235: pSalomeTMFContext.startPlugin();
236: }
237:
238: void onStop() {
239:
240: }
241:
242: public void reloadPanel(JApplet _parentApplet) {
243: parentApplet = _parentApplet;
244: reInitComponent();
245: String strP = strProject;
246: String strL = strLogin;
247: //loadingFromCookies();
248: if (strLogin == null) {
249: strLogin = strL;
250: strProject = strP;
251: }
252: loadModel();
253: reloadPlugin();
254: }
255:
256: void onInit() {
257: Util.log("[SalomeTMF] - Applet init");
258: System.runFinalization();
259: System.gc();
260: if (connectToAPI()) {
261: WaitView waitView = new WaitView();
262: long l1 = System.currentTimeMillis();
263: initComponent();
264: loadModel();
265: loadPlugin();
266: long l2 = System.currentTimeMillis();
267: System.out.println("Temps de chargement = " + (l2 - l1));
268: waitView.dispose();
269: }
270:
271: }
272:
273: /**
274: * M?thode appel? lorsque l'utilisateur quitte l'application.
275: */
276: public void quit(boolean do_recup, boolean doclose) {
277: if (parentApplet != this ) {
278: return;
279: }
280: if (pSalomeTMFContext != null && doclose == true) {
281: pSalomeTMFContext.suspendPlugin();
282: }
283: Util
284: .log("[SalomeTMF] - Applet quit (recup = " + do_recup
285: + ")");
286: if (Api.isConnected() && doclose) {
287: Api.closeConnection();
288: }
289: ptrFrame.dispose();
290: ptrFrame = null;
291: System.gc();
292: System.runFinalization();
293: if (pJSUtils != null) {
294: pJSUtils.closeWindow();
295: }
296: exit = true;
297:
298: } // Fin de la m?thode quit();
299:
300: public void stop() {
301: Util.log("[SalomeTMF] - Applet stop");
302: }
303:
304: public void destroy() {
305: Util.log("[SalomeTMF] Applet destroy (closeDB = " + closeDB
306: + ")");
307: if (closeDB == true)
308: Api.closeConnection();
309: }
310:
311: /* void closeWindow(){
312: try {
313: //Class classJSObject = Class.forName("sun.plugin.javascript.JSObject");
314: Class classJSObject = Class.forName("netscape.javascript.JSObject");
315: Method meth_getWindow = classJSObject.getMethod("getWindow", new Class[] {Applet.class});
316: Object pJSObject = meth_getWindow.invoke(null ,new Object[] {parentApplet});
317: System.out.println("pJSObject = " + pJSObject);
318: Method meth_call = classJSObject.getMethod("call", new Class[] {String.class, Object[].class});
319: meth_call.invoke(pJSObject, new Object[] {"close", null});
320:
321: } catch (Exception e) {
322: e.printStackTrace();
323: }
324: }*/
325:
326: /**********************************************************************************************************************/
327:
328: public SalomeTMFContext getSalomeTMFContext() {
329: return pSalomeTMFContext;
330: }
331:
332: public SalomeTMFPanels getSalomeTMFPanels() {
333: return pSalomeTMFPanels;
334: }
335:
336: public void showDocument(URL toShow, String where) {
337: //getAppletContext().showDocument(toShow, where); //FOR MINIBOOT
338: parentApplet.getAppletContext().showDocument(toShow, where);
339: }
340:
341: public boolean isGraphique() {
342: return true;
343: }
344:
345: /***************** Windows Listener ***************/
346: public void windowClosing(WindowEvent e) {
347: if (!exit)
348: quit(true, true);
349: }
350:
351: public void windowActivated(WindowEvent e) {
352: }
353:
354: public void windowClosed(WindowEvent e) {
355: }
356:
357: public void windowDeactivated(WindowEvent e) {
358: }
359:
360: public void windowDeiconified(WindowEvent e) {
361: }
362:
363: public void windowIconified(WindowEvent e) {
364: }
365:
366: public void windowOpened(WindowEvent e) {
367: }
368:
369: }
|