001: package auction.controller;
002:
003: import auction.dao.DAOFactory;
004: import auction.dao.UserDAO;
005: import auction.exceptions.BusinessException;
006: import net.xoetrope.optional.data.pojo.XPersistentDataBindingContext;
007: import net.xoetrope.optional.data.pojo.XPojoHelper;
008: import net.xoetrope.optional.data.pojo.XPersistenceController;
009: import net.xoetrope.optional.data.pojo.XPersistentPojoModel;
010: import net.xoetrope.swing.XDialog;
011: import net.xoetrope.xui.XDataBindingContext;
012: import net.xoetrope.xui.XPage;
013: import auction.model.*;
014: import net.xoetrope.xui.data.XModel;
015:
016: /**
017: * Superclass for all pages in the application,
018: * handles exceptions, etc.
019: */
020: public class CEPage extends XPage {
021: protected XPersistenceController pojoContext;
022: protected XPojoHelper pojoHelper;
023: protected UserDAO userDAO;
024:
025: protected XPersistentPojoModel currentUserModel;
026: protected DAOFactory factory;
027:
028: /**
029: * Creates a new instance of CEPage
030: */
031: public CEPage() {
032: super ();
033: init();
034: setCurrentUser();
035: }
036:
037: protected void init() {
038: pojoContext = (XPersistenceController) project
039: .getObject("PojoContext");
040: pojoHelper = new XPojoHelper(rootModel.get("pojo"));
041:
042: // mark that the bindings should be updated withing the boundaries
043: // of an open session.
044: XDataBindingContext bindingContext = getDataBindingContext();
045:
046: if (bindingContext instanceof XPersistentDataBindingContext)
047: ((XPersistentDataBindingContext) bindingContext)
048: .startWorkUnit(true);
049:
050: // createDAOs
051: factory = DAOFactory.instance(DAOFactory.HIBERNATE);
052: userDAO = factory.getUserDAO();
053:
054: // get model nodes
055: currentUserModel = (XPersistentPojoModel) pojoHelper
056: .get("hibernateDAOFactory/userDAO/currentUser");
057: }
058:
059: /**
060: * Creates the "currentUser" model node
061: */
062: protected void setCurrentUser() {
063: if (currentUserModel.get() == null) {
064: User user = new User();
065: user.setUsername("johndoe");
066: user.setPassword("passwd");
067: currentUserModel.set(user);
068: }
069: }
070:
071: /**
072: * Gets the currently logged-on user.
073: */
074: protected User getCurrentUser() {
075: return (currentUserModel != null ? (User) currentUserModel
076: .get() : null);
077: }
078:
079: /**
080: * Displays the exception message in an error dialog.
081: * @param ex the exception whose message is to be shown
082: */
083: protected void handleException(Exception ex) {
084: Throwable t = ex.getCause();
085: String msg = (t != null ? t.getMessage() : ex.getMessage());
086: showErrorDialog(msg);
087: }
088:
089: /**
090: * Shows the message dialog
091: * @param msg the message to be shown
092: */
093: protected void showMessageDialog(String msg) {
094: ((XModel) rootModel.get("message")).set(msg);
095: XDialog messageDialog = (XDialog) pageMgr.loadPage("Message",
096: false);
097: messageDialog.pack();
098: messageDialog.showDialog(this );
099: }
100:
101: /**
102: * Shows the error dialog
103: * @param msg the message to be shown
104: */
105: protected void showErrorDialog(String msg) {
106: ((XModel) rootModel.get("errorMessage")).set(msg);
107: XDialog errorDialog = (XDialog) pageMgr
108: .loadPage("Error", false);
109: errorDialog.pack();
110: errorDialog.showDialog(this );
111: }
112:
113: /**
114: * Shows the specified dialog
115: * @param dialogName the name of the dialog
116: * @param caption the caption to be set on the dialog
117: * @return a value indicating the button that was used to dismiss the dialog
118: */
119: protected int showDialog(String dialogName, String caption) {
120: XDialog dialog = (XDialog) pageMgr.loadPage(dialogName, false);
121: dialog.setCaption(caption);
122: dialog.pack();
123: dialog.showDialog(this);
124: return dialog.getLastReturnValue();
125: }
126:
127: }
|