While trying to find my first "dream" job after graduating from university I'm working on various "petty" projects.
Right now I'm trying to create swing application that imitate Acrobat Reader. Manipulating PDFs is no problem (already have hang of iText), I already put together GUI, but I'm not clear on how shall I share/handle some tasks.
My interface consist of 3 main component: JToolBar on top with buttons, JPanel on left to show thumbnails and JPanel on right to function as main display are for single page.
All these three are grouped under main panel.
Now imagine that you click "Open File" button, you retrieve the PDF document you wish to work with (this is no problem at all to this point)
- the application need to keep file path to the document stored somewhere
- need to call relevant methods and populate all 3 component (display page count in tool bar, display thumbnails on left and show first page in main display area)
Similar scenario will repeat when ever any other button is pressed, once document opened, and all 3 components need to be updated.
I know I will need to provide and abstract class whit all methods for document manipulation and the 3 components will be based upon this class. Also use of custom addListeners and fireEvents is expected. Just not sure how to kick of.
Can anyone help out?
Joined: May 26, 2009
Worked out my solution, and sweet-toned with help of chapter 2 from Head First Design Patterns.
Peter Miklosko wrote:Worked out my solution, and sweet-toned with help of chapter 2 from Head First Design Patterns.
Well done, but please tell us how ou sorted it, so everybody here can learn.
Joined: May 26, 2009
Campbell Ritchie wrote:Well done, but please tell us how ou sorted it, so everybody here can learn.
Nothing special really.
Having 3 classes that needs to be updated depending on event: ToolBar, Thumbnail, SingleView
Plus having class, MainPanel, that group above GUI components.
I known that I will need to create some "data" class to "house" some operations, store and share file path once selected, plus provide facilities for adding/removing/notifying listeners. On other hand there was need for some interfaces to dictate some classes.
At the start I created StatusListener interface with methods fileToOpen(), fileToSave(), fileToClose(). This interface is implemented in that 3 classes(ToolBar, Thumbnail, SingleView) so once event occur they will be notified and do they business depending on each class.
Then I created StatusEvent interface with add/remove/notify for each of StatusListener interface methods. The interface is implemented in "data" class called Manager. Manager class provide behind scene operations that are triggered by toolbar, but not strictly related only to toolbar (sort of general management for all 3 main components). On top of that it does work as safe-keeper of file path or current "working" file.
Each of the 3 classes has a need to register its listeners with Manager class(here is where I made mistake in first place, a dumb mistake of providing each class with own instance of Manager). MainPanel as direct grouping component is well suited for creating new instance of Manager class and passing references to ToolBar, Thumbnail, SingleView as it is adding them in layout.