/*
* 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.actioncontroller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.jar.Manifest;
import java.util.logging.Logger;
import org.geoforge.basprjdat.io.GfrIoBasNameDatPrjAbs;
import org.geoforge.basprjdatecl.io.GfrIoBasNameDatPrjEcl;
import org.geoforge.basprjdatogc.io.GfrIoBasNameDatPrjOgc;
import org.geoforge.basprjdspecl.io.GfrIoBasNameDspDftObjsCatPrjEcl;
import org.geoforge.basprjdspogc.io.GfrIoBasNameDspDftObjsCatPrjOgc;
import org.geoforge.basusrspcdsp.io.GfrPrpNamingBasDspUsr;
import org.geoforge.basusrspcdsp.table.GfrBasTblUsrSpcPrtHistoryAbs;
import org.geoforge.guillc.GfrAppSpcAbs;
import org.geoforge.guillc.action.*;
import org.geoforge.guillc.dialog.DlgAbs;
import org.geoforge.guillc.dialog.GfrDlgPgsSpaceOpen;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.guillc.dialog.GfrDlgOkOpenAbs;
import org.geoforge.guillc.dialog.GfrDlgOpenSpaceFromHistoryProject;
import org.geoforge.guillc.dialog.GfrDlgOpenSpaceFromListProject;
import org.geoforge.guillc.actionmanager.GfrAmrAppFrmSecManageChildrenSpaceWork;
import org.geoforge.guillc.actioncontroller.GfrAcrSpcAppAbs;
import org.geoforge.guillc.actionmanager.GfrAmrAbs;
import org.geoforge.guillc.dialog.*;
import org.geoforge.guillc.frame.GfrFrmMainAppAbs;
import org.geoforge.guillc.filechooser.GfrUtilFileChooserVol;
import org.geoforge.java.io.file.GfrUtilFile;
import org.geoforge.java.io.file.GfrJavIoFileExtension;
import org.geoforge.io.serial.GfrSerializeAbs;
import org.geoforge.io.serial.GfrSerializeStateWwdMan;
import org.geoforge.io.serial.GfrSerializeStateWwdSel;
import org.geoforge.io.util.jar.GfrManJarSpcPrj;
import org.geoforge.io.util.jar.JarHelper;
import org.geoforge.lang.text.GfrSimpleDateFormatAll;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.lang.util.threadpoolexecutor.GfrTpeSpcPrj;
import org.geoforge.lang.util.threadpoolexecutor.GfrWrkAbs;
import org.geoforge.mdldat.GfrMdlDatSpcPrtWork;
import org.geoforge.wrpbasprsdsp.viewer.GfrWrpDspSynViewer;
import org.geoforge.wrpbasusr.GfrValueDateHistory;
import org.geoforge.wrpbasusr.GfrWrpUsrSpcDspPrtAppRoot;
import org.geoforge.wrpbasusr.GfrWrpUsrSpcDspPrtAppWork;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
abstract public class GfrAcrSpcAppPrtWrkOgcEclAbs extends GfrAcrSpcAppPrtAbs implements
ActionListener
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrAcrSpcAppPrtWrkOgcEclAbs.class.getName());
static
{
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
protected GfrAcrSpcAppAbs _acrParent = null;
@Override
abstract public String actionControllerContainerSpaceNewChild() throws Exception;
abstract protected void _importProjectHandler(String strName) throws Exception;
abstract protected String _getNameDbDataProjectAppli();
abstract protected String _getNameDbDisplayDefaultProjectAppli();
abstract protected String _getIdProjectFromName(String strName) throws Exception;
protected GfrAcrSpcAppPrtWrkOgcEclAbs(
GfrAppSpcAbs app,
GfrAmrAbs amr,
GfrAcrSpcAppAbs acrParent)
throws Exception
{
super(app,
amr,
new GfrAmrAppFrmSecManageChildrenSpaceWork());
this._acrParent = acrParent;
}
@Override
public void actionPerformed(ActionEvent evt)
{
try
{
// void for now
}
catch (Exception exc)
{
exc.printStackTrace();
String strError = exc.getMessage();
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(strError);
GfrOptionPaneAbs.s_showDialogError(null, strError);
return;
}
String strError = "Uncaught source: " + evt.getSource().getClass().toString();
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(strError);
GfrOptionPaneAbs.s_showDialogError(null, strError);
}
// at launching time
@Override
public void displayChild(String strValue) throws Exception
{
if (! super._acrChild.init())
{
String str = "! super._acrChild.init()";
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(str);
throw new Exception(str);
}
((GfrAcrSpcAppClsPrjAbs) super._acrChild).actionControllerOpenSpace(strValue);
if (!super._app.getFrame().isVisible()) // always false!
{
super._app.getFrame().toFront();
super._app.getFrame().setVisible(true);
}
}
@Override
public void open() throws Exception
{
GfrWrpUsrSpcDspPrtAppWork.getInstance().setLast(GfrBasTblUsrSpcPrtHistoryAbs.STR_WHO_LAST_ME);
if (super._acrChild == null)
{
String str = "super._acrChild == null";
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(str);
throw new Exception(str);
}
super._acrChild.destroy();
super._acrChild = null;
String strPathAbsCurrentSpaceWork = GfrWrpUsrSpcDspPrtAppRoot.getInstance().getValueLastChildFromHistory();
this.actionControllerOpenSpace(strPathAbsCurrentSpaceWork);
}
@Override
public void actionControllerOpenSpace(String strPathAbs) throws Exception
{
super.open();
updateStateActions();
actionControllerContainerUpdateContentsDialogManageChildren();
((GfrFrmMainAppAbs) super._app.getFrame()).openSpaceWork(
strPathAbs, // to workspace
(ActionListener) this
);
ActionCloseChildSpaceWork.s_getInstance().setEnabled(true);
}
@Override
public void preferencesAppliHandler()
{
try
{
DlgAbs dlg = new GfrDlgTabsPrefsSpcWork();
if (!dlg.init())
{
String str = "! dlg.init()";
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(str);
throw new Exception(str);
}
dlg.setVisible(true);
}
catch (Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
return;
}
}
public void manageChildrenProjectsHandler() throws Exception
{
actionControllerContainerManageChildren();
}
public void newAndOpenChildProjectHandler() throws Exception
{
newAndOpenChild();
}
public String newChildProjectHandler() throws Exception
{
return actionControllerContainerSpaceNewChild();
}
@Override
abstract public void actionControllerContainerUpdateContentsDialogManageChildren() throws Exception;
/*
* the place to assign new feature!
* should disable or hide menuBar, toolBar, contents, ???statusBar???
*/
@Override
public void openChild(String strName) throws Exception
{
((GfrFrmMainAppAbs) super._app.getFrame()).closeSpaceWork();
GfrDlgPgsSpaceOpen.s_getInstance().setVisibleTrue(super._app.getFrame());
GfrTpeSpcPrj.s_getInstance().execute(new GfrWrkOpenDisplayMain(strName));
}
public void openChildFromHistProjectHandler() throws Exception
{
openChildFromHistory();
}
public void openChildLastProjectHandler() throws Exception
{
openChildLast();
}
public void openChildFromListProjectHandler() throws Exception
{
openChildFromList();
}
@Override
abstract public void actionControllerContainerSpaceDeleteChild() throws Exception;
public void deleteChildProjectHandler() throws Exception
{
actionControllerContainerSpaceDeleteChild();
}
public void closeChildWorkspaceHandler() throws Exception
{
ActionCloseChildSpaceWork.s_getInstance().setEnabled(false);
super.actionControllerClose();
((GfrFrmMainAppAbs) super._app.getFrame()).closeSpaceWork();
this._acrParent.open();
}
@Override
abstract public void actionControllerContainerManageChildren() throws Exception;
@Override
public void newAndOpenChild() throws Exception
{
String strValue = newChildProjectHandler();
if (strValue != null)
openChild(strValue);
}
@Override
public void closeChild() throws Exception
{
if (super._acrChild == null)
{
String str = "super._acrChild == null";
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(str);
throw new Exception(str);
}
((GfrAcrSpcAppClsPrjAbs) super._acrChild).actionControllerClose();
super._acrChild.destroy();
super._acrChild = null;
}
@Override
public void openChildLast() throws Exception
{
String strValue = GfrWrpUsrSpcDspPrtAppWork.getInstance().getValueLastChildFromHistory();
openChild(strValue);
}
@Override
public void openChildFromHistory() throws Exception
{
Object[] objsChildren = GfrWrpUsrSpcDspPrtAppWork.getInstance().getArrayValueDateChildrenFromHistory();
Object[][] objss = new Object[objsChildren.length][];
for (int i=objsChildren.length; i>0; i--)
{
objss[objsChildren.length - i] = new Object[2];
GfrValueDateHistory vdhCur = (GfrValueDateHistory) objsChildren[i-1];
String strDate = GfrSimpleDateFormatAll.s_format(vdhCur.getGregCal().getTime());
objss[objsChildren.length - i][0] = vdhCur.getValue();
objss[objsChildren.length - i][1] = strDate;
}
GfrDlgOpenSpaceFromHistoryProject dlg = new GfrDlgOpenSpaceFromHistoryProject(
super._app.getFrame(),
objss);
if (!dlg.init())
{
String str = "! dlg.init()";
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(str);
throw new Exception(str);
}
dlg.setVisible(true);
boolean blnCancelled = dlg.isCancelled();
String strValue = null;
if (!blnCancelled)
{
strValue = dlg.getValue();
}
dlg.destroy();
dlg = null;
if (blnCancelled)
return; // action cancelled by user
// ------
// do job
openChild(strValue);
}
@Override
public void openChildFromList() throws Exception
{
String[] strsChildrenExisting = GfrWrpUsrSpcDspPrtAppRoot.getInstance().getSortedNamesLastChildFromHistory();
GfrDlgOkOpenAbs dlg = new GfrDlgOpenSpaceFromListProject(
super._app.getFrame(), strsChildrenExisting);
if (!dlg.init())
{
String str = "! dlg.init()";
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(str);
throw new Exception(str);
}
dlg.setVisible(true);
final String strValue = dlg.getValue();
dlg.setVisible(false);
dlg.destroy();
if (strValue == null)
return; // action cancelled by user
// ------
// do job
openChild(strValue);
}
public void cloneChildProjectHandler() throws Exception
{
actionControllerContainerSpaceCloneChild();
}
public void moveChildProjectHandler() throws Exception
{
actionControllerContainerSpaceMoveChild();
}
@Override
abstract public void actionControllerContainerSpaceCloneChild() throws Exception;
@Override
abstract public void actionControllerContainerSpaceMoveChild() throws Exception;
public void deleteProjectsHandler() throws Exception
{
actionControllerContainerSpaceDeleteChildren();
}
@Override
abstract public void actionControllerContainerSpaceDeleteChildren() throws Exception;
@Override
public void updateStateActions() throws Exception
{
String[] strsChildrenExisting = GfrWrpUsrSpcDspPrtAppRoot.getInstance().getSortedNamesLastChildFromHistory();
boolean blnHasChild = false;
if (strsChildrenExisting != null && strsChildrenExisting.length > 0)
blnHasChild = true;
// ----
// main frame
ActionOpenChildFromListSpaceProject.s_getInstance().setEnabled(blnHasChild);
// ---
boolean blnIsHistory = false;
String strLastChild = GfrWrpUsrSpcDspPrtAppWork.getInstance().getValueLastChildFromHistory();
if (strLastChild!=null && strLastChild.length()>0)
blnIsHistory = true;
ActionOpenChildFromHistSpaceProject.s_getInstance().setEnabled(blnIsHistory);
ActionOpenChildLastSpaceProject.s_getInstance().setEnabled(blnIsHistory);
// ---
// ----
// dialogManagerChildren
ActionDeleteProjects.s_getInstance().setEnabled(blnHasChild);
}
@Override
public GfrAcrSpcAppAbs getChild()
{
return (GfrAcrSpcAppAbs) this._acrChild;
}
public void importProjectHandler() throws Exception
{
File fleJarFileSource = GfrUtilFileChooserVol.s_getOpenFile(
super._app.getFrame(),
GfrJavIoFileExtension.STRS_EXTENSION_PROJECT,
GfrJavIoFileExtension.STR_DESCRIPTION_PROJECT);
if (fleJarFileSource == null)
return; // cancelled by user
String strError = GfrManJarSpcPrj.s_check(fleJarFileSource);
if (strError != null)
{
String strMessage = "Failed to read project file:";
strMessage += "\n " + fleJarFileSource.getAbsolutePath();
strMessage += "\n\n\nDetails:";
strMessage += "\n " + strError;
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.warning(strMessage);
GfrOptionPaneAbs.s_showDialogWarning(super._app.getFrame(), strMessage);
return;
}
String[] strsChildrenExisting = GfrWrpUsrSpcDspPrtAppRoot.getInstance().getSortedNamesLastChildFromHistory();
// show dialog
GfrDlgNewSettingsNameSpcAppPrj dlg = new GfrDlgNewSettingsNameSpcAppPrj(
super._app.getFrame(), strsChildrenExisting);
if (!dlg.init())
{
String str = "! dlg.init()";
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(str);
throw new Exception(str);
}
dlg.setVisible(true);
boolean blnCancelled = dlg.isCancelled();
String strValue = null;
if (!blnCancelled)
{
strValue = dlg.getValue();
}
dlg.destroy();
dlg = null;
if (blnCancelled) // action cancelled by user
return;
// ---
_importProjectHandler(strValue);
String strIdTarget = _getIdProjectFromName(strValue);
File fleDirTarget = GfrMdlDatSpcPrtWork.getInstance().importChild(fleJarFileSource, strIdTarget);
// update actions
updateStateActions();
// ---
String strMessage = "Sucessfully imported project:";
strMessage += "\n " + fleJarFileSource.getAbsolutePath();
strMessage += "\n\nto file:";
strMessage += "\n " + fleDirTarget.getAbsolutePath();
GfrOptionPaneAbs.s_showDialogInfo(super._app.getFrame(), strMessage);
}
public void exportChildProjectHandler() throws Exception
{
File fleJarFileTarget = GfrUtilFileChooserVol.s_chooseFileSave(
super._app.getFrame(),
"Export project",
"project",
"." + GfrJavIoFileExtension.STRS_EXTENSION_PROJECT[0]
);
if (fleJarFileTarget == null)
return;
File fleFolderTemp = GfrUtilFile.s_createTempFile(
"_export_space_", // strPrefix
"_projects_" // strSuffix"
);
fleFolderTemp.delete();
fleFolderTemp.mkdir();
// ---
String strNameSource = ActionChildExportProject.s_getInstance().getChild();
// _getIdProjectFromName
String strIdSource = _getIdProjectFromName(strNameSource);
String strPathAbsSpaceWork = GfrWrpUsrSpcDspPrtAppRoot.getInstance().getValueLastChildFromHistory();
File fleProjectSourceCur = new File(strPathAbsSpaceWork, strIdSource);
File fleDataSqliteIn;
File fleDataSqliteOut;
// ---
// ogcDbSqlite
fleDataSqliteIn = new File(fleProjectSourceCur, GfrIoBasNameDatPrjOgc.STR_FILE);
fleDataSqliteOut = new File(fleFolderTemp, GfrIoBasNameDatPrjOgc.STR_FILE);
GfrUtilFile.s_copy(fleDataSqliteIn, fleDataSqliteOut);
// ---
// eclDbSqlite
fleDataSqliteIn = new File(fleProjectSourceCur, GfrIoBasNameDatPrjEcl.STR_FILE);
fleDataSqliteOut = new File(fleFolderTemp, GfrIoBasNameDatPrjEcl.STR_FILE);
GfrUtilFile.s_copy(fleDataSqliteIn, fleDataSqliteOut);
// ---
// [xxx]DbSqlite
fleDataSqliteIn = new File(fleProjectSourceCur, _getNameDbDataProjectAppli());
fleDataSqliteOut = new File(fleFolderTemp, _getNameDbDataProjectAppli());
GfrUtilFile.s_copy(fleDataSqliteIn, fleDataSqliteOut);
// ---
// ---
// displaySqlite
// ATTN: clean-up all related to serialized viewers
fleDataSqliteIn = new File(fleProjectSourceCur, GfrPrpNamingBasDspUsr.STR_NAME_FILE_DB_SPACEPROJECT);
fleDataSqliteOut = new File(fleFolderTemp, GfrPrpNamingBasDspUsr.STR_NAME_FILE_DB_SPACEPROJECT);
GfrUtilFile.s_copy(fleDataSqliteIn, fleDataSqliteOut);
// beg clean-up system-dependent
GfrWrpDspSynViewer.s_getInstance().deleteAll(fleDataSqliteOut);
// end clean-up system-dependent
// ---
// displaySqliteOgc
fleDataSqliteIn = new File(fleProjectSourceCur, GfrIoBasNameDspDftObjsCatPrjOgc.STR_FILE);
fleDataSqliteOut = new File(fleFolderTemp, GfrIoBasNameDspDftObjsCatPrjOgc.STR_FILE);
GfrUtilFile.s_copy(fleDataSqliteIn, fleDataSqliteOut);
// ---
// displaySqliteEcl
fleDataSqliteIn = new File(fleProjectSourceCur, GfrIoBasNameDspDftObjsCatPrjEcl.STR_FILE);
fleDataSqliteOut = new File(fleFolderTemp, GfrIoBasNameDspDftObjsCatPrjEcl.STR_FILE);
GfrUtilFile.s_copy(fleDataSqliteIn, fleDataSqliteOut);
// ---
// displaySqliteGtc
fleDataSqliteIn = new File(fleProjectSourceCur, _getNameDbDisplayDefaultProjectAppli());
fleDataSqliteOut = new File(fleFolderTemp, _getNameDbDisplayDefaultProjectAppli());
GfrUtilFile.s_copy(fleDataSqliteIn, fleDataSqliteOut);
// ---
// recursively store contents of folder named data
File fleDataFolderSourceCur = new File(fleProjectSourceCur, GfrIoBasNameDatPrjAbs.STR_NAME_FOLDER_DATA);
if (! fleDataFolderSourceCur.exists())
{
String strError = "! fleDataFolderSourceCur.exists(), fleDataFolderSourceCur.getAbsolutePath()=" + fleDataFolderSourceCur.getAbsolutePath();
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(strError);
throw new Exception(strError);
}
if (! fleDataFolderSourceCur.isDirectory())
{
String strError = "! fleDataFolderSourceCur.isDirectory(), fleDataFolderSourceCur.getAbsolutePath()=" + fleDataFolderSourceCur.getAbsolutePath();
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(strError);
throw new Exception(strError);
}
File fleDataFolderTargetCur = new File(fleFolderTemp, GfrIoBasNameDatPrjAbs.STR_NAME_FOLDER_DATA);
GfrUtilFile.s_copyDirectory(fleDataFolderSourceCur, fleDataFolderTargetCur);
// ---
// serial: only stateWwd[Manage|Select].xml
File fleSerialFolderSourceCur = new File(fleProjectSourceCur, GfrSerializeAbs.NAME_FOLDER);
if (fleSerialFolderSourceCur.exists())
{
// ---
if (! fleSerialFolderSourceCur.isDirectory())
{
String strError = "! fleSerialFolderSourceCur.isDirectory(), fleSerialFolderSourceCur.getAbsolutePath()=" + fleSerialFolderSourceCur.getAbsolutePath();
GfrAcrSpcAppPrtWrkOgcEclAbs._LOGGER_.severe(strError);
throw new Exception(strError);
}
File fleSerialFolderTargetCur = new File(fleFolderTemp, GfrSerializeAbs.NAME_FOLDER);
fleSerialFolderTargetCur.mkdir();
File fleXmlSourceCur = null;
File fleXmlTargetCur = null;
// --- manage
fleXmlSourceCur = new File(fleSerialFolderSourceCur, GfrSerializeStateWwdMan.s_getNameFile());
fleXmlTargetCur = new File(fleSerialFolderTargetCur, GfrSerializeStateWwdMan.s_getNameFile());
if (fleXmlSourceCur.exists())
GfrUtilFile.s_copy(fleXmlSourceCur, fleXmlTargetCur);
// --- select
fleXmlSourceCur = new File(fleSerialFolderSourceCur, GfrSerializeStateWwdSel.s_getNameFile());
fleXmlTargetCur = new File(fleSerialFolderTargetCur, GfrSerializeStateWwdSel.s_getNameFile());
if (fleXmlSourceCur.exists())
GfrUtilFile.s_copy(fleXmlSourceCur, fleXmlTargetCur);
}
// end serial
// jarring
Manifest man = GfrManJarSpcPrj.s_create();
JarHelper jarUtil = new JarHelper(man);
jarUtil.jarDir(fleFolderTemp, fleJarFileTarget);
String strMessage = "Sucessfully exported project name:";
strMessage += "\n " + strNameSource;
strMessage += "\n\nto file:";
strMessage += "\n " + fleJarFileTarget.getAbsolutePath();
GfrOptionPaneAbs.s_showDialogInfo(super._app.getFrame(), strMessage);
}
// beg inner-classes
// --- beg new stuff
private class GfrWrkOpenDisplayMain extends GfrWrkAbs
{
private String _strNameProject_ = null;
private GfrWrkOpenDisplayMain(String strNameProject)
{
super("openSpaceProject");
this._strNameProject_ = strNameProject;
}
@Override
public void run()
{
try
{
GfrAcrSpcAppPrtWrkOgcEclAbs.this.actionControllerClose(); // unload spacework's actions
displayChild(this._strNameProject_);
}
catch (Exception exc)
{
exc.printStackTrace();
System.out.println("exc caught");
}
}
}
// --- end new stuff
// end inner-classes
}