/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.geoforge.appogcecl;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.geoforge.appogcecl.actioncontroller.GfrAcrSpcAppMainOgcAbs;
import org.geoforge.appogcecl.util.GfrResBundleAppOgcEcl;
import org.geoforge.guihlp.GfrCSH;
import org.geoforge.guihlp.GfrHelpBroker;
import org.geoforge.guihlp.GfrHelpSet;
import org.geoforge.guihlp.handler.IGfrHandlerSetEnabledTrueHelpOnThisSection;
import org.geoforge.guillc.GfrAppSpcAbs;
import org.geoforge.guillc.action.ActDlgLloExitApp;
import org.geoforge.guillc.actioncontroller.GfrAcrSpcAppMainAbs;
import org.geoforge.guillc.dialog.GfrDlgCmdCancelOkConfirmExitApp;
import org.geoforge.guillc.frame.GfrFrmAbs;
import org.geoforge.guillc.frame.GfrFrmMainAppAbs;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.guillc.panel.GfrPnlStatusBarMain;
import org.geoforge.io.util.property.PrpMgrPublicPropertiesSystemWriter;
import org.geoforge.java.enumeration.GfrEnuSystemPropertiesKeys;
import org.geoforge.lang.handler.IGfrHandlerLifeCycleObject;
import org.geoforge.lang.net.GfrProxySelector;
import org.geoforge.java.lang.system.GfrUtlSystem;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.lang.util.threadpoolexecutor.GfrTpeSpcPrj;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
abstract public class GfrAppSpcOgcAbs extends GfrAppSpcAbs implements
WindowListener,
IGfrHandlerLifeCycleObject
{
static
{
System.setProperty(GfrEnuSystemPropertiesKeys.NAME_3LETTERS_ORG_THIS.getLabel(), "gfr");
System.setProperty(GfrEnuSystemPropertiesKeys.NAME_FOLDER_FILES_TEMPORARY.getLabel(), "temp");
GfrAppSpcAbs._s_startSplash();
}
static protected boolean _BLN_FAILED_JAVAHELP = false;
static private boolean _BLN_DEBUG_ = false;
static
{
String str = System.getProperty(GfrEnuSystemPropertiesKeys.DEBUG_APPLI.getLabel());
if (str!=null && str.compareTo("true") == 0)
_BLN_DEBUG_ = true;
}
static
{
// beg tempo
System.setProperty("sun.io.serialization.extendedDebugInfo", "true");
// end tempo
}
static
{
System.setProperty("java.net.useSystemProxies", "true");
GfrProxySelector.doJob();
if (GfrUtlSystem.s_isMac())
{
// tempo
if (true)
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "WorldWind view");
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "World Wind Application");
System.setProperty("com.apple.mrj.application.growbox.intrudes", "false");
System.setProperty("apple.awt.brushMetalLook", "true");
}
else if (GfrUtlSystem.s_isWindows())
{
System.setProperty("sun.awt.noerasebackground", "true"); // prevents flashing during window resizing
}
}
/* NOT USING JRE 1.7 anymore !!!
static
{
// beg quick bug fix
// if no, appli freezing:
// Ubuntu 11.10, on laptops:
// . dell 32 bits, os.version: 3.0.0-12-generic, JRE 1.7.0-b147
// . asus 64 bits, os.version: 3.0.0-12-generic, JRE 1.7.0_01-b08
String strOs = System.getProperty("os.name").toLowerCase();
if (strOs.toLowerCase().compareTo("linux") == 0)
{
WorldWindowGLCanvas wwd = new WorldWindowGLCanvas();
wwd.setPreferredSize(new java.awt.Dimension(400, 300));
wwd.setModel(new BasicModel());
}
}
*/
// ----
// begin: instantiate logger for this app
final static private Logger _LOGGER_ = Logger.getLogger(GfrAppSpcOgcAbs.class.getName());
// end: instantiate logger for this class
// ----
static
{
// begin: load libraries (DLL or SO)
String strKey = GfrEnuSystemPropertiesKeys.LIBRARIES_NATIVE_CPP.getLabel();
String strValue = null;
try
{
strValue = System.getProperty(strKey);
// modif: nil value allowed
if (strValue != null)
System.loadLibrary(strValue);
}
catch (Exception exc)
{
exc.printStackTrace();
String str = "Exception caught"
+ "\n"
+ "failed to load library, strKey="
+ strKey
+ ", strValue="
+ strValue;
str += "\n exiting";
JOptionPane.showMessageDialog(GfrFrmAbs.s_getFrameOwner(GfrPnlStatusBarMain.s_getInstance()), str,
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
catch (UnsatisfiedLinkError err)
{
err.printStackTrace();
String str = "Exception caught"
+ "\n"
+ "failed to load library, strKey="
+ strKey
+ ", strValue="
+ strValue
+ ", err.getMessage()"
+ err.getMessage();
str += "\n exiting";
JOptionPane.showMessageDialog(GfrFrmAbs.s_getFrameOwner(GfrPnlStatusBarMain.s_getInstance()), str,
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
// end: load libraries (DLL or SO)
}
static protected synchronized void s_showDialogWarningJavaHelp()
{
if (GfrAppSpcOgcAbs._BLN_FAILED_JAVAHELP)
return;
GfrAppSpcOgcAbs._BLN_FAILED_JAVAHELP = true;
String str = "Failed to load integrated help";
str += "\n\n" + "Possible reasons: If launched by WebStart, you may have an obsolete WebStart version";
str += "\n" + "Else please contact GeoForge project";
str += "\n\n" + "Integrated help will be disabled in this session";
GfrOptionPaneAbs.s_showDialogWarning(GfrFrmAbs.s_getFrameOwner(GfrPnlStatusBarMain.s_getInstance()), str);
}
// ---
protected native void cppStartRoot(String strPathAbsLog);
protected native void cppEndRoot();
// ---
// beg protected
protected boolean _blnIsSerializedFrameMain = false;
protected javax.help.HelpBroker _hbr = null;
protected GfrAcrSpcAppMainAbs _acr = null;
private boolean _blnSetLAFSwing_ = false;
private boolean _blnInternAllowed_ = false;
private ArrayList<javax.help.HelpBroker> _altMyHelpBroker_ = null;
public javax.help.HelpBroker getHelpBroker() { return this._hbr; }
protected GfrAppSpcOgcAbs(
boolean blnSetLAFSwing, // used to fix up JH's toolbar buttons
boolean blnInternAllowed // custom help
)
{
super();
GfrAppSpcOgcAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance()); // ???
GfrAppSpcOgcAbs._LOGGER_.info(System.getProperty(GfrEnuSystemPropertiesKeys.NAME_LONG_APPLI.getLabel()) + " "
+ GfrResBundleAppOgcEcl.s_getInstance().getValue("sentence.nowStarting") +
"\n");
String strDebug = System.getProperty(GfrEnuSystemPropertiesKeys.DEBUG_APPLI.getLabel());
if (strDebug != null && strDebug.equalsIgnoreCase("true"))
{
GfrAppSpcOgcAbs._LOGGER_.info("user.home" + "=" + System.getProperty("user.home") + "\n");
//AppOgcAbs._LOGGER_.info("java.version" + "=" + System.getProperty("java.version") + "\n");
}
if (System.getProperty(GfrEnuSystemPropertiesKeys.LIBRARIES_NATIVE_CPP.getLabel()) != null)
{
String strPathAbsLog = FileHandlerLogger.s_getPathAbsFileLogCpp();
cppStartRoot(strPathAbsLog);
}
this._blnSetLAFSwing_ = blnSetLAFSwing;
this._blnInternAllowed_ = blnInternAllowed;
this._altMyHelpBroker_ = new ArrayList<javax.help.HelpBroker>();
//_attachShutDownHook_();
}
protected boolean _confirmDoExit()
{
GfrDlgCmdCancelOkConfirmExitApp dlg = new GfrDlgCmdCancelOkConfirmExitApp(super._frm);
dlg.init();
dlg.setVisible(true);
boolean blnCancelled = dlg.isCancelled();
dlg.destroy();
dlg = null;
if (blnCancelled)
return false;//Action cancelled by user
super._frm.setVisible(false);
GfrTpeSpcPrj.s_getInstance().shutDownAndTerminate();
GfrTpeSpcPrj.s_shutDownInstance();
if (_BLN_DEBUG_)
System.err.println(">> DONE: GfrTpeSpcPrj.s_shutDownInstance()");
return true;
}
// TODO: exit in a concurrent thread
@Override
public void doConfirmExit()
{
if(! _confirmDoExit())
return;
// TODO: wait for finished thread in TPE
// ----
destroy();
if (System.getProperty(GfrEnuSystemPropertiesKeys.LIBRARIES_NATIVE_CPP.getLabel()) != null)
cppEndRoot();
try
{
PrpMgrPublicPropertiesSystemWriter.s_doJob();
}
catch(Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
GfrAppSpcOgcAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
//System.exit(1);
}
String str = System.getProperty(GfrEnuSystemPropertiesKeys.NAME_LONG_APPLI.getLabel());
str += " ";
str += GfrResBundleAppOgcEcl.s_getInstance().getValue("sentence.isExitingNormally");
GfrAppSpcOgcAbs._LOGGER_.info(str);
System.exit(0);
}
@Override
public void windowClosing(WindowEvent e)
{
// rather wait in a thread!!!
// or get "Close" button disabled
if (! ActDlgLloExitApp.s_getInstance().isEnabled())
{
// ie. inside project's space, loading secondaryWindows
String str = "Actions in progress, please wait while closing";
str += "\n\n" + "Memo, \"Forcing an exit\" hotkey: Alt+F+E";
GfrOptionPaneAbs.s_showDialogInfo(null, str);
return;
}
doConfirmExit();
}
@Override
public void windowOpened(WindowEvent e)
{
}
@Override
public void windowClosed(WindowEvent e)
{
}
@Override
public void windowIconified(WindowEvent e)
{
}
@Override
public void windowDeiconified(WindowEvent e)
{
}
@Override
public void windowActivated(WindowEvent e)
{
}
@Override
public void windowDeactivated(WindowEvent e)
{
}
@Override
public boolean init()
{
if (!super._frm.init())
return false;
if (!this._acr.init())
{
String str = "this._acr.init()";
GfrAppSpcOgcAbs._LOGGER_.severe(str);
return false;
}
_delayDoHelpBroker();
// beg plugins
//GfrPluginManagerUtil.s_getInstance();
// end plugins
return true;
}
private void _packAndShowSerialized_() throws Exception
{
}
public void packAndShow() throws Exception
{
if (this._blnIsSerializedFrameMain)
_packAndShowSerialized_();
else
{
// below tempo code, pseudo serialize
// beg
_packAndShowUnserialized_();
return;
// end;
}
super._frm.toFront();
super._frm.setVisible(true);
}
protected void _packAndShowUnserialized_() throws Exception
{
super._frm.pack();
//super._frm.setVisible(true);
((GfrAcrSpcAppMainOgcAbs) this._acr).displaySpace();
}
@Override
public void destroy()
{
try
{
super._frm.setVisible(false); // !!!!
super._frm.removeWindowListener((WindowListener) this);
if (this._acr != null)
{
this._acr.destroy();
this._acr = null;
}
super._frm.releaseUnserializedObject();
}
catch(Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
GfrAppSpcOgcAbs._LOGGER_.severe(str);
}
if (super._frm != null)
{
super._frm.destroy();
super._frm = null;
}
if (this._altMyHelpBroker_ != null)
{
for (int i = 0; i < this._altMyHelpBroker_.size(); i++)
{
GfrHelpBroker mhbCur =
(GfrHelpBroker) this._altMyHelpBroker_.get(i);
if (mhbCur != null)
{
mhbCur.destroy();
mhbCur = null;
}
}
this._altMyHelpBroker_.clear();
this._altMyHelpBroker_ = null;
}
}
/**
a new document/project has been opened, updating JavaHelp's JHEditorPane's StyleSheet
arrayList should contain at least one element: helpStandard
**/
protected boolean _updateJavaHelpStyleSheets_()
{
if (this._altMyHelpBroker_ == null)
{
GfrAppSpcOgcAbs._LOGGER_.severe("this._altMyHelpBroker == null");
return false;
}
for (int i = 0; i < this._altMyHelpBroker_.size(); i++)
{
GfrHelpBroker mhbCur = null;
try
{
mhbCur = (GfrHelpBroker) this._altMyHelpBroker_.get(i);
}
catch (ClassCastException excClassCast)
{
excClassCast.printStackTrace();
GfrAppSpcOgcAbs._LOGGER_.log(Level.SEVERE, "Exception", excClassCast);
return false;
}
if (!mhbCur.assignStyleSheet())
{
GfrAppSpcOgcAbs._LOGGER_.severe("! mhbCur.assignStyleSheet()");
return false;
}
}
// ending
return true;
}
//@SuppressWarnings("unused")
protected javax.help.HelpBroker _doHelpBroker_(String strNameDirHS)
{
if (strNameDirHS == null)
{
GfrAppSpcOgcAbs._LOGGER_.severe("strNameDirHS == null");
return null;
}
// beg modif for internationalization
if (this._blnInternAllowed_)
{
Locale loc = Locale.getDefault();
String strLocLang = loc.getLanguage();
if (strLocLang == null)
{
GfrAppSpcOgcAbs._LOGGER_.severe("this._blnInternAllowed, strLocLang == null");
return null;
}
// for now, just handling FR
String[] strsSupported =
{
"fr"
};
for (int i = 0; i < strsSupported.length; i++)
{
if (strLocLang.toLowerCase().compareTo(strsSupported[i]) == 0)
//strPathRelativeDirHS += "_" + strsSupported[i];
break;
}
}
// end modif for internationalization
String strHelpsetPathRelativeShort = strNameDirHS; // tempo
// TEST NETBEANS MODULE
//if (AppMainAbs.s_isDeployedWithNbm())
// strHelpsetPathRelativeShort = "/" + strHelpsetPathRelativeShort;
// NOT WORKING!
// ---
GfrHelpSet hst = null;
try
{
ClassLoader clr = this.getClass().getClassLoader();
java.net.URL url = javax.help.HelpSet.findHelpSet(clr, strHelpsetPathRelativeShort);
if (url == null)
{
GfrAppSpcOgcAbs._LOGGER_.warning("url == null, strHelpsetPathRelativeShort=" + strHelpsetPathRelativeShort);
return null;
}
hst = new GfrHelpSet(clr, url);
if (hst == null)
{
GfrAppSpcOgcAbs._LOGGER_.severe("nil hst, strHelpsetPathRelativeShort=" + strHelpsetPathRelativeShort);
GfrAppSpcOgcAbs._LOGGER_.severe("... nil hst, url.toString()=" + url.toString());
return null;
}
}
catch (Exception exc)
{
exc.printStackTrace();
GfrAppSpcOgcAbs._LOGGER_.severe(exc.getMessage());
return null;
} catch (ExceptionInInitializerError errExceptionInInitializer)
{
errExceptionInInitializer.printStackTrace();
GfrAppSpcOgcAbs._LOGGER_.severe(errExceptionInInitializer.getMessage());
return null;
}
javax.help.HelpBroker hbrMine = new GfrHelpBroker(hst, this._blnSetLAFSwing_);
if (!((GfrHelpBroker) hbrMine).init())
{
GfrAppSpcOgcAbs._LOGGER_.severe("! ((GfrHelpBroker) hbrMine).init(), strHelpsetPathRelativeShort=" + strHelpsetPathRelativeShort);
return null;
}
this._altMyHelpBroker_.add(hbrMine);
return hbrMine;
}
@Override
protected void _delayDoHelpBroker()
{
if (GfrAppSpcOgcAbs._BLN_FAILED_JAVAHELP)
return;
// ???? invokeLater inside invokeLater !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
String strKey = GfrEnuSystemPropertiesKeys.PATH_RELATIVE_HELPSET_CONTENTS_APPLI.getLabel();
String strPathRelHelpset = System.getProperty(strKey);
if (strPathRelHelpset == null)
{
String str = "strPathRelHelpset == null, strKey=" + strKey;
GfrAppSpcOgcAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
System.exit(1);
}
_hbr = _doHelpBroker_(strPathRelHelpset);
if (_hbr == null)
{
String str = "_hbr_ == null, ignoring, strPathRelHelpset=" + strPathRelHelpset;
GfrAppSpcOgcAbs._LOGGER_.warning(str);
GfrAppSpcOgcAbs.s_showDialogWarningJavaHelp();
return;
}
((IGfrHandlerSetEnabledTrueHelpOnThisSection) _frm).setEnabledTrueHelpOnThisSection(_hbr); // menuBar's menuItem & toolbar's iconButtons
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
if (_frm!=null && _hbr!=null)
_frm.setEnabledHelpKey(_hbr); // F1 key
}
});
if (! GfrCSH.s_checkAndDumpCmp2ID(_hbr.getHelpSet()))
{
String str = "! GfrCSH.s_checkAndDumpCmp2ID(_hbr_.getHelpSet())";
GfrAppSpcOgcAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
System.exit(1);
}
}
});
//----
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
String strKey = GfrEnuSystemPropertiesKeys.PATH_RELATIVE_HELPSET_GETTINGSTARTED_APPLI.getLabel();
javax.help.HelpBroker hbrHelpGettingStarted =
_doHelpBroker_(System.getProperty(strKey));
if (hbrHelpGettingStarted == null)
{
GfrAppSpcOgcAbs._LOGGER_.warning("hbrHelpGettingStarted == null, strKey=" + strKey);
GfrAppSpcOgcAbs.s_showDialogWarningJavaHelp();
return;
}
((GfrFrmMainAppAbs) _frm).setEnabledHelpGettingStarted(hbrHelpGettingStarted); // menuBar's menuItem
}
});
}
}