/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HIDB2 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 Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2. If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui;
import hidb2.Activator;
import hidb2.gui.preferences.PrefConst;
import hidb2.gui.util.IReStartableView;
import hidb2.kern.DataStore;
import hidb2.kern.H2DataStore;
import hidb2.kern.HIDBConst;
import hidb2.kern.ObjetBdd;
import hidb2.kern.ValuedInstance;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
/**
* This class controls all aspects of the application's execution.
*
* Perspectives :
* - Management Perspective that provides the means to edit the folder descriptions :
* TreeView (for the current DATASTORE) with
* DataPath ==> open the Table with DataPath
* FolderDescriptions ==> open the FolderDescription Editor
* ListDescriptions ==> open the ListDescription Editor
* LogView
*
* - User Perspective that provides the means to populate and work with data contained by the various descriptions :
* TreeView (for the current DATASTORE) with
* FolderDescriptions ==> new : create a new folder and open the FolderEditor
* open : open the FolderTableView
*
* Folders ==> new = create a new Folder and open the FolderListEditor (Folders in a Table)
* open the FolderEditor
* delete : delete the current folder
* TODO: Masse Import : Open the Mass Import Wizard
*
* TODO: Cards ==> open : open the FolderEditor
* delete : delete the current card
*
* LogView
*
* FolderEditor = Folder Attributs + Table for Card Attributs
* ListEditor = Table for List Attributs
*
*/
public class Application implements IApplication
{
final static Logger log = Logger.getLogger("hidb2.gui");
/** Information Database */
private static DataStore _das;
/** Open description are kept in this set */
private static Set<ObjetBdd> _openTab = new HashSet<ObjetBdd>();
/** Open instance are kept in the set */
private static Set<ValuedInstance> _openInst = new HashSet<ValuedInstance>();
/* (non-Javadoc)
* @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
*/
public Object start(IApplicationContext context)
{
Display display = PlatformUI.createDisplay();
try
{
// Open the default database
String dbDir = Activator.getDefault().getPreferenceStore().getString(PrefConst.P_DB_DIR);
_das = new H2DataStore(dbDir);
if (_das.getOpenStatus() == HIDBConst.C_OK)
{
int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
if (returnCode == PlatformUI.RETURN_RESTART)
{
return IApplication.EXIT_RESTART;
}
}
else
{
Shell sh = new Shell();
boolean b = MessageDialog.openQuestion(sh, "Migration Alert", "Current DB version is : " + _das.getDBVersion()
+ "\nRequired Version is : " + _das.getExecVersion()
+ "\nDo you confirm the migration (if no the application shall stop)?");
if (b)
{
int res = _das.migrate();
switch (res)
{
case HIDBConst.C_OK:
MessageDialog
.openInformation(sh, "Migration Result", "Migration was successfull\nHIDB2 will now restart");
return IApplication.EXIT_RESTART;
case HIDBConst.C_ERROR:
MessageDialog.openWarning(sh, "Migration Result",
"Migration has failed\nMigration script not found\nHIDB2 shall stop");
break;
case HIDBConst.C_FAIL:
MessageDialog.openWarning(sh, "Migration Result",
"Migration has failed\nThe Migration script returns an error\nHIDB2 could restart");
break;
}
return IApplication.EXIT_RESTART;
}
}
return IApplication.EXIT_OK;
}
finally
{
// Close DB if needed
if (_das != null)
{
_das.close();
}
display.dispose();
}
}
/* (non-Javadoc)
* @see org.eclipse.equinox.app.IApplication#stop()
*/
public void stop()
{
final IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench == null)
return;
final Display display = workbench.getDisplay();
display.syncExec(new Runnable()
{
public void run()
{
if (!display.isDisposed())
workbench.close();
}
});
}
/**
* The current datastore (i.e. database)
* @return
*/
public static DataStore getDataStore()
{
return _das;
}
/**
* Called when a tree or sub tree of a view has to be updated.
*/
public static void signalRefresh(int subject, Object clientData)
{
IWorkbenchWindow wbwin = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage iwp = wbwin.getActivePage();
IViewReference[] ivrt = iwp.getViewReferences();
for (IViewReference ivrf : ivrt)
{
IViewPart ivpart = ivrf.getView(false);
if (ivpart instanceof DataUpdatable)
{
((DataUpdatable) ivpart).refresh(subject, clientData);
}
}
}
/**
* Called when an editor wants to open an object for write.
* Return true if lock acquired and record the open object
*/
public static boolean getLock(ObjetBdd o)
{
boolean added = _openTab.add(o);
if (!added)
{
log.info("Description [" + o.getName() + "] already open");
}
return added;
}
/**
* Called when an editor releases an object.
*/
public static void release(ObjetBdd o)
{
log.info("Description [" + o.getName() + "] released");
_openTab.remove(o);
}
/**
* Called when an editor wants to open an object for write.
* Return true if lock acquired and record the open object
*/
public static boolean getLock(ValuedInstance o)
{
boolean added = _openInst.add(o);
if (!added)
{
log.info("Description [" + o + "] already open");
}
return added;
}
/**
* Called when an editor releases an object.
*/
public static void release(ValuedInstance o)
{
log.info("Description [" + o + "] released");
_openInst.remove(o);
}
/**
* Change Database directory
*/
public static void restart()
{
PlatformUI.getWorkbench().restart();
}
/**
* Change Database directory
*/
public static void restartViews()
{
IWorkbenchWindow wbwin = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IViewReference[] vr = wbwin.getActivePage().getViewReferences();
for (int i = 0; i < vr.length; i++)
{
IViewPart vp = vr[i].getView(true);
if (vp instanceof IReStartableView)
{
((IReStartableView) vp).restart();
}
}
}
}