001:package auction.controller;
002:
003:import auction.model.Bid;
004:import auction.model.User;
005:import java.util.ArrayList;
006:import java.util.List;
007:import net.xoetrope.optional.annotation.Find;
008:import net.xoetrope.optional.data.pojo.XPojoHelper;
009:import net.xoetrope.optional.data.pojo.XPersistenceController;
010:import net.xoetrope.optional.data.pojo.XPersistentPojoModel;
011:import net.xoetrope.optional.data.pojo.XPojoModel;
012:import net.xoetrope.swing.XButton;
013:import net.xoetrope.swing.XImage;
014:import net.xoetrope.swing.XLabel;
015:import net.xoetrope.swing.toolbar.XToolbar;
016:import net.xoetrope.xui.data.XModel;
017:
018:/**
019: * A toolbar of the application.
020: */
021:public class Toolbar extends XToolbar
022:{
023: private static final int MYBOOKS_PAGE = 0;
024: private static final int PROFILE_PAGE = 1;
025: private static final int SEARCH_PAGE = 2;
026: private static final int DETAILS_PAGE = 3;
027:
028: private int currentPage = -1;
029: private List<Integer> navigationHistory;
030: private int navigationIdx;
031:
032: // components
033: @Find private XLabel userLabel;
034: @Find private XLabel userNameLabel;
035: @Find private XButton logOffButton;
036: @Find private XButton goBackButton, goForwardButton;
037: @Find private XImage bigIcon;
038:
039: private XPersistenceController pojoContext;
040: private XModel pageNameModel;
041:
042: // transient properties
043: private XPersistentPojoModel selectedCategoryModel;
044: private XPersistentPojoModel selectedBookModel;
045: private XPersistentPojoModel resultListModel;
046:
047: /**
048: * Creates a new instance of Toolbar
049: */
050: public Toolbar()
051: {
052: super ();
053: navigationHistory = new ArrayList<Integer>();
054: navigationIdx = -1;
055:
056: XPojoHelper pojoHelper = new XPojoHelper( rootModel.get( "pojo" ));
057: pojoContext = (XPersistenceController)project.getObject( "PojoContext" );
058:
059: // get model references
060: pageNameModel = (XModel)rootModel.get( "pageName" );
061: pageNameModel.set( "Log On" );
062:
063: // get transient property references
064: selectedCategoryModel =
065: (XPersistentPojoModel)pojoHelper.get( "hibernateDAOFactory/categoryDAO/selectedCategory" );
066: selectedBookModel =
067: (XPersistentPojoModel)pojoHelper.get( "hibernateDAOFactory/bookDAO/selectedBook" );
068: resultListModel =
069: (XPersistentPojoModel)pojoHelper.get( "hibernateDAOFactory/bookDAO/resultListModel" );
070: }
071:
072: // enables/disables navigation buttons
073: private void setupNavigationButtons()
074: {
075: goBackButton.setEnabled( navigationIdx > 0 );
076: goForwardButton.setEnabled( navigationIdx < navigationHistory.size() - 1 );
077: }
078:
079: /**
080: * Displays the "MyBooks" page, saves the
081: * previous page in the navigation history,
082: * changes icons, etc
083: */
084: protected void displayMyBooksPage()
085: {
086: pageNameModel.set( "My Books" );
087: setBigIcon( "mybooks_big.png" );
088: updateBoundComponentValues();
089: pageMgr.showPage( "MyBooks", "content" );
090: setupNavigationButtons();
091: }
092:
093: protected void displayProfilePage()
094: {
095: pageNameModel.set( "Profile" );
096: setBigIcon( "profile_big.png" );
097: updateBoundComponentValues();
098: pageMgr.showPage( "Profile", "content" );
099: setupNavigationButtons();
100: }
101:
102: protected void displaySearchPage()
103: {
104: pageNameModel.set( "Search" );
105: setBigIcon( "search_big.png" );
106: updateBoundComponentValues();
107: pageMgr.showPage( "Search", "content" );
108: setupNavigationButtons();
109: }
110:
111: protected void displayDetailsPage()
112: {
113: pageNameModel.set( "Book Details" );
114: setBigIcon( "details_big.png" );
115: updateBoundComponentValues();
116: pageMgr.showPage( "BookDetails", "content" );
117: setupNavigationButtons();
118: }
119:
120: public void showMyBooksPage()
121: {
122: if ( currentPage != MYBOOKS_PAGE ) {
123: currentPage = MYBOOKS_PAGE;
124: saveInHistory( currentPage );
125: displayMyBooksPage();
126: }
127: }
128:
129: public void showSearchPage()
130: {
131: if ( currentPage != SEARCH_PAGE ) {
132: currentPage = SEARCH_PAGE;
133: saveInHistory( currentPage );
134: displaySearchPage();
135: }
136: }
137:
138: public void showProfilePage()
139: {
140: if ( currentPage != PROFILE_PAGE ) {
141: currentPage = PROFILE_PAGE;
142: saveInHistory( currentPage );
143: displayProfilePage();
144: }
145: }
146:
147: public void showDetailsPage()
148: {
149: if ( currentPage != DETAILS_PAGE ) {
150: currentPage = DETAILS_PAGE;
151: saveInHistory( currentPage );
152: displayDetailsPage();
153: }
154: }
155:
156: /**
157: * Saves the specified page in the navigation history
158: * @param pageType value indicating the page to be saved
159: */
160: private void saveInHistory( Integer pageType )
161: {
162: navigationHistory = navigationHistory.subList( 0, ++navigationIdx );
163: navigationHistory.add( pageType );
164: }
165:
166: /**
167: * Sets the specified icon on the right side of the toolbar
168: * @param iconName the name of the icon to be set
169: */
170: protected void setBigIcon( String iconName )
171: {
172: bigIcon.setImageName( iconName );
173: }
174:
175: /**
176: * Logs off the current user, hides the toolbar,
177: * clears the navigation histroy, displays the Welcome page.
178: */
179: public void logOff()
180: {
181: // clear transient property nodes
182: selectedCategoryModel.clear();
183: selectedBookModel.clear();
184: resultListModel.clear();
185:
186: userLabel.setVisible( false );
187: userNameLabel.setVisible( false );
188: logOffButton.setVisible( false );
189:
190: XModel currentUserModel =
191: (XModel)project.getModel().
192: get( "pojo/hibernateDAOFactory/userDAO/currentUser" );
193: currentUserModel.set( new User() );
194:
195: pageNameModel.set( "Log On" );
196: updateBoundComponentValues();
197: setVisible( false );
198: pageMgr.showPage( "Welcome", "content" );
199: navigationHistory.clear();
200: navigationIdx = -1;
201: setupNavigationButtons();
202: }
203:
204: /**
205: * Shows the specified page
206: * @param pageType the value indicating the page
207: * to be shown
208: */
209: private void showPage( int pageType )
210: {
211: switch( pageType ) {
212: case MYBOOKS_PAGE:
213: displayMyBooksPage();
214: break;
215:
216: case PROFILE_PAGE:
217: displayProfilePage();
218: break;
219:
220: case SEARCH_PAGE:
221: displaySearchPage();
222: break;
223:
224: case DETAILS_PAGE:
225: displayDetailsPage();
226: break;
227: }
228: }
229:
230: /**
231: * Displays the previous page.
232: */
233: public void goBack()
234: {
235: currentPage = (int)navigationHistory.get( --navigationIdx );
236: showPage( currentPage );
237: setupNavigationButtons();
238: }
239:
240: /**
241: * Displays the next page
242: */
243: public void goForward()
244: {
245: currentPage = (int)navigationHistory.get( ++navigationIdx );
246: showPage( currentPage );
247: setupNavigationButtons();
248: }
249:
250:}
|