001: package auction.controller;
002:
003: import auction.dao.BookDAO;
004: import auction.dao.CategoryDAO;
005: import auction.dao.DAOFactory;
006: import auction.dao.UserDAO;
007: import auction.model.Bid;
008: import auction.model.Category;
009: import auction.model.Language;
010: import auction.model.User;
011: import auction.model.Book;
012: import com.xoetrope.swing.XDateEdit;
013: import java.awt.Dimension;
014: import java.util.Collection;
015: import java.util.Date;
016: import javax.swing.ListSelectionModel;
017: import net.xoetrope.optional.annotation.Find;
018: import net.xoetrope.optional.data.pojo.XPersistenceController;
019: import net.xoetrope.optional.data.pojo.XPersistentListHelper;
020: import net.xoetrope.optional.data.pojo.XPojoHelper;
021: import net.xoetrope.optional.data.pojo.XPersistentPojoModel;
022: import net.xoetrope.xui.*;
023: import net.xoetrope.swing.*;
024: import net.xoetrope.xui.data.XDataBinding;
025: import net.xoetrope.xui.data.XListBinding;
026: import net.xoetrope.xui.data.XModel;
027:
028: /**
029: * A controller of the MyBooks page.
030: */
031: public class MyBooks extends CEPage {
032: // model nodes
033: private XPersistentPojoModel currentUserModel;
034: private XPersistentPojoModel newBookModel;
035:
036: // components
037: @Find
038: private XList bidList;
039: @Find
040: private XButton removeBidButton;
041: @Find
042: private XLabel bidAmountLabel;
043: @Find
044: private XLabel dateOfBidLabel;
045: @Find
046: private XLabel bidderLabel;
047: @Find
048: private XButton removeCategoryButton;
049: @Find
050: private XButton addCategoryButton;
051: @Find
052: private XCheckbox activeCheckbox;
053: @Find
054: private XButton updateBookButton;
055: @Find
056: private XButton removeBookButton;
057: @Find
058: private XButton setImageButton;
059: @Find
060: private XList bookList;
061: @Find
062: private XEdit authorEdit;
063: @Find
064: private XEdit titleEdit;
065: @Find
066: private XDateEdit startDateEdit;
067: @Find
068: private XDateEdit endDateEdit;
069: @Find
070: private XEdit initPriceEdit;
071: @Find
072: private XEdit reservePriceEdit;
073: @Find
074: private XEdit imageEdit;
075: @Find
076: private XTextArea descriptionArea;
077: @Find
078: private XComboBox languageComboBox;
079: @Find
080: private XImage bookCover;
081: @Find
082: private XList categoryList;
083: @Find
084: private XTree categoryTree;
085:
086: // helpers
087: private XPersistentListHelper bookListHelper;
088: private XPersistentListHelper bidListHelper;
089: private XPersistentListHelper categoryListHelper;
090: private XPersistentListHelper languageHelper;
091:
092: public MyBooks() {
093: super ();
094: }
095:
096: protected void init() {
097: super .init();
098: // get model nodes
099: currentUserModel = (XPersistentPojoModel) pojoHelper
100: .get("hibernateDAOFactory/userDAO/currentUser");
101: newBookModel = (XPersistentPojoModel) pojoHelper
102: .get("hibernateDAOFactory/bookDAO/newBook");
103: }
104:
105: public void pageCreated() {
106: // list helpers
107: bookListHelper = new XPersistentListHelper(bookList, this ,
108: "pojo/hibernateDAOFactory/bookDAO/selectedBook");
109: bidListHelper = new XPersistentListHelper(bidList, this ,
110: "pojo/hibernateDAOFactory/bookDAO/selectedBid");
111: categoryListHelper = new XPersistentListHelper(categoryList,
112: this );
113: languageHelper = new XPersistentListHelper(languageComboBox,
114: this );
115:
116: // set selection modes
117: bookList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
118: bidList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
119: categoryList
120: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
121: }
122:
123: public void pageActivated() {
124: updateCategoryButtons();
125: // update the book list
126: bookListHelper.updateListHolder();
127: // update the "remove bid" button
128: removeBidButton.setEnabled(!bidList.isSelectionEmpty());
129: }
130:
131: /**
132: * Updates the category list related buttons.
133: */
134: public void updateCategoryButtons() {
135: removeCategoryButton.setEnabled(!categoryList
136: .isSelectionEmpty());
137: addCategoryButton.setEnabled(!categoryTree.isSelectionEmpty());
138: }
139:
140: /**
141: * Updates the selected book with values from the forms
142: */
143: public void updateBook() {
144: if (bookListHelper.selectionEmpty())
145: return; // nothing's selected
146: try {
147: pojoContext.beginWorkUnit();
148:
149: saveBoundComponentValues();
150: Language language = (Language) languageHelper
151: .getSelectedPojo();
152: Book book = (Book) bookListHelper.getSelectedPojo();
153: book.setLanguage(language);
154: bookListHelper.mergeSelectedPojo();
155:
156: pojoContext.endWorkUnit();
157: } catch (Exception ex) {
158: handleException(ex);
159: }
160: }
161:
162: /**
163: * Shows the new book dialog and adds a new book.
164: */
165: public void addBook() {
166: // put a new book stub into the newBookModel node.
167: Language language = (Language) languageHelper.getSelectedPojo();
168: User user = (User) currentUserModel.get();
169: newBookModel.set(new Book("", "", "", language, user, 0, 0,
170: new Date(), new Date()));
171:
172: // show the "new book" dialog
173: if (showDialog("NewBook", "New book") == XDialog.OK_CLICKED) {
174: try {
175: pojoContext.beginWorkUnit();
176:
177: newBookModel.persist();
178: // update the book list.
179: bookListHelper.updateListHolder();
180: // select the new book on the book list.
181: bookList.setSelectedIndex(bookList.getItemCount() - 1);
182:
183: pojoContext.endWorkUnit();
184: } catch (Exception ex) {
185: handleException(ex);
186: }
187: }
188: }
189:
190: /**
191: * Removes the selected book
192: */
193: public void removeBook() {
194: if (bookListHelper.selectionEmpty())
195: return; // nothing's selected
196: try {
197: pojoContext.beginWorkUnit();
198:
199: // remove the book from the users's book list
200: Book book = (Book) bookListHelper.getSelectedPojo();
201: getCurrentUser().removeBookForSale(book);
202: // remove the book from the persistent store
203: bookListHelper.deleteSelectedPojo();
204: // clear the "selected bid" model node
205: bidListHelper.clearSelectionModel();
206:
207: // update the lists
208: bookListHelper.updateListHolder();
209: bidListHelper.updateListHolder();
210:
211: pojoContext.endWorkUnit();
212: } catch (Exception ex) {
213: handleException(ex);
214: }
215: }
216:
217: /**
218: * Removes the selected category from the book's list of categories
219: */
220: public void removeCategory() {
221: if (categoryListHelper.selectionEmpty())
222: return; // this shouldn't happened
223:
224: try {
225: pojoContext.beginWorkUnit();
226:
227: // persist the selected category
228: categoryListHelper.mergeSelectedPojo();
229: // persist the selected book
230: bookListHelper.mergeSelectedPojo();
231:
232: Book book = (Book) bookListHelper.getSelectedPojo();
233: Category category = (Category) categoryListHelper
234: .getSelectedPojo();
235: category.removeBook(book);
236:
237: // update the list of categories assigned to the selected book
238: categoryListHelper.updateListHolder();
239:
240: pojoContext.endWorkUnit();
241: } catch (Exception ex) {
242: handleException(ex);
243: }
244: }
245:
246: /**
247: * Adds the selected category to the book's list of categories
248: */
249: public void addCategory() {
250: // get the selected category model from the category tree
251: XPersistentPojoModel categoryModel = (XPersistentPojoModel) categoryTree
252: .getSelectedNode();
253:
254: try {
255: pojoContext.beginWorkUnit();
256:
257: categoryModel.merge();
258: bookListHelper.mergeSelectedPojo();
259:
260: Category category = (Category) categoryModel.get();
261: Book book = (Book) bookListHelper.getSelectedPojo();
262: category.addBook(book);
263:
264: // persist the category model, this operation will
265: // persist the book as well.
266: categoryModel.persist();
267: // update the list
268: categoryListHelper.updateListHolder();
269:
270: pojoContext.endWorkUnit();
271: } catch (Exception ex) {
272: handleException(ex);
273: }
274:
275: }
276:
277: /**
278: * Removes the selected bid
279: */
280: public void removeBid() {
281: if (bidListHelper.selectionEmpty())
282: return; // nothing's selected
283:
284: try {
285: pojoContext.beginWorkUnit();
286:
287: // persist the selected book
288: bookListHelper.mergeSelectedPojo();
289: // persist the selected bid
290: bidListHelper.mergeSelectedPojo();
291:
292: // clear the MaxBid node
293: XPersistentPojoModel maxBidModel = (XPersistentPojoModel) bookListHelper
294: .getSelectionModel().get("maxBid");
295: maxBidModel.clear();
296:
297: Bid bid = (Bid) bidListHelper.getSelectedPojo();
298: Book book = (Book) bookListHelper.getSelectedPojo();
299: // remove the selected bid from the book's list of bids.
300: book.removeBid(bid);
301: // remove the selected bid from the persistent store.
302: bidListHelper.deleteSelectedPojo();
303: // update the bid list
304: bidListHelper.updateListHolder();
305:
306: pojoContext.endWorkUnit();
307: } catch (Exception ex) {
308: handleException(ex);
309: }
310: }
311:
312: /**
313: * Invoked whenever the selection on the list of books
314: * is changed.
315: */
316: public void bookListSelectionChanged() {
317: if (!bookListHelper.selectionChanged())
318: return;
319:
320: // enable/disable book related buttons
321: boolean state = !bookList.isSelectionEmpty();
322: removeBookButton.setEnabled(state);
323: updateBookButton.setEnabled(state);
324: setImageButton.setEnabled(state);
325: languageComboBox.setEnabled(state);
326:
327: try {
328: pojoContext.beginWorkUnit();
329:
330: if (state) {
331: // update the language combo box
332: XModel languageModel = (XModel) bookListHelper
333: .getSelectionModel().get("language");
334: Object language = languageModel.get();
335: XModel languageNameModel = (XModel) languageModel
336: .get("name");
337: String languageName = (String) languageNameModel.get();
338: languageComboBox.setSelectedObject(languageName);
339: }
340:
341: // update the lists
342: bidListHelper.updateListHolder();
343: categoryListHelper.updateListHolder();
344: // update the book details panel
345: updateBinding(getBinding(authorEdit));
346: updateBinding(getBinding(startDateEdit));
347: updateBinding(getBinding(titleEdit));
348: updateBinding(getBinding(startDateEdit));
349: updateBinding(getBinding(endDateEdit));
350: updateBinding(getBinding(initPriceEdit));
351: updateBinding(getBinding(reservePriceEdit));
352: updateBinding(getBinding(imageEdit));
353: updateBinding(getBinding(descriptionArea));
354: updateBinding(getBinding(activeCheckbox));
355: // update the cover of the selected book
356: Book book = (Book) bookListHelper.getSelectedPojo();
357: setBookCover(book != null ? book.getImage() : null);
358:
359: pojoContext.endWorkUnit();
360: } catch (Exception ex) {
361: handleException(ex);
362: }
363:
364: }
365:
366: /**
367: * Retrieves the name of the file containing the book cover
368: * and the persists it.
369: */
370: public void setImage() {
371: if (bookListHelper.selectionEmpty())
372: return; // nothing's selected
373: try {
374: pojoContext.beginWorkUnit();
375:
376: saveBoundComponentValues();
377: Book book = (Book) bookListHelper.getSelectedPojo();
378: setBookCover(book.getImage());
379:
380: pojoContext.endWorkUnit();
381: } catch (RuntimeException ex) {
382: handleException(ex);
383: }
384: }
385:
386: /**
387: * Invoked whenever the bid list selection is changed
388: */
389: public void bidListSelectionChanged() {
390: if (bidListHelper.selectionChanged()) {
391: removeBidButton.setEnabled(!bidList.isSelectionEmpty());
392: // update the bid view
393: updateBinding(getBinding(bidAmountLabel));
394: updateBinding(getBinding(dateOfBidLabel));
395: updateBinding(getBinding(bidderLabel));
396: }
397: }
398:
399: /**
400: * Sets the cover of the book
401: * @param imageName the name of the file containing the book cover
402: */
403: private void setBookCover(String imageName) {
404: bookCover.setImageName(imageName);
405: bookCover.revalidate();
406: bookCover.repaint();
407:
408: if (imageName != null) {
409: Dimension prefSize = bookCover.getPreferredSize();
410: if ((prefSize.getWidth() > 0)
411: && (bookCover.getHeight() > 0))
412: bookCover.setSize(prefSize);
413: }
414: }
415: }
|