View Javadoc

1   package org.qdwizard;
2   
3   
4   import java.awt.Color;
5   import java.util.HashMap;
6   import java.util.Observer;
7   
8   import javax.swing.JPanel;
9   
10  /***
11   *  A wizard screen
12   *  <ul>
13   *  <li>For each wizard page, create a public Screen class. You have to implement initUI(), getDescription() and getName() abstract methods.</li>
14   *   <li>getName() method should return the step name and getDescription() the step description (return null if no description needed).</li>
15   *   <li>initUI() method contains graphical code for your screen. This method is automatically called from screen constructor so don't call it.</li>
16   *	</ul>
17   *
18   * @author     Bertrand Florat
19   * @created    1 may 2006
20   */
21  public abstract class Screen extends JPanel {
22      private ScreenState state;
23      public HashMap<String,Object> data;
24      private Color m_bgColor=null;
25      
26      private Wizard wizard;
27      /***
28       * Construct a screen
29       * @param sName Screen name
30       * @param sDesc description
31       */
32      public Screen() {
33          data = Wizard.data;
34          state = new ScreenState(true, true, true, false, null);
35          initUI();
36      }
37      
38      /***
39       * Give here the step name.
40       * @return screen name
41       */
42      abstract public String getName();
43      
44      /***
45       * Screen description (optional)
46       * @return screen description
47       */
48      abstract public String getDescription();
49      
50      boolean canFinish(){
51          //Can finish only if none problem
52          return state.getCanFinish() && (state.getProblem() == null);
53      }
54      
55      /***
56       * Set whether the finish button should be enabled
57       * @param b
58       */
59      public void setCanFinish(boolean b){
60          state.setCanFinish(b);
61      }
62      
63      boolean canGoNext(){
64          //if screen is last one, cannot go futher
65          return state.getCanGoNext() && !state.getCanFinish() && (state.getProblem() == null);
66      }
67      
68      public boolean canCancel(){
69          return state.getCanCancel();
70      }
71      
72      boolean canGoPrevious(){
73          return state.getCanGoPrevious();
74      }
75      
76      /***
77       * Set whether the next button should be enabled
78       * @param b
79       */
80      void setCanGoNext(boolean b){
81          state.setCanGoNext(b);
82      }
83      
84      /***
85       * Set whether the previous button should be enabled
86       * @param b
87       */
88      void setCanGoPrevious(boolean b){
89          state.setCanGoPrevious(b);
90      }
91      
92      /***
93       * Set whether the cancel (or System menu close) button should be enabled
94       * @param b
95       */
96      public void setCanCancel(boolean b){
97          state.setCanCancel(b);
98      }
99      
100     
101     /***
102      * Set a problem (set to null if problem is fixed)
103      * @param sProblem Problem string or null if no more problem
104      */
105     public void setProblem(String sProblem){
106         state.setProblem(sProblem);
107     }
108     
109     /***
110      * Get current problem
111      * @return the current problem
112      */
113     public String getProblem(){
114         return state.getProblem();
115     }
116     
117     /***UI creation*/
118     abstract public void initUI();
119     
120     /***
121      * Called by wizard before the screen is displayed.
122      * This happens only in forwardmode, which means onEnter won't be called when you return to
123      * a screen via the previous button.
124      *
125      */
126     public void onEnter() {
127         
128     }
129     
130     /***
131      * Called by wizard before the screen is left.
132      * This happens only in forwardmode, which means onLeave won't be called when you leave
133      * the screen via the previous button.
134      * <p>
135      * Deprecated: use onNext()
136      * */
137     @Deprecated
138     public void onLeave() {
139     }
140     
141     /***
142      * Called by wizard before the screen is left.
143      * This happens only in forwardmode, which means onLeave won't be called when you leave
144      * the screen via the previous button.
145      * <p>
146      * @return return true if the Wizard should display the next screen
147      * @return return false if the Wizard should stay on the current screen
148      * */
149     public boolean onNext() {
150         return true;
151     }
152     
153     /***
154      * Called by wizard when the wizard is being cancelled.
155      * Use this function to clean up (like stop any threads that this Screen might have created)
156      * */
157     public void onCancelled() {
158     }
159 
160     /***
161      * Called by wizard when the wizard is closing because the Finish button was pressed.
162      * Use this function to clean up (like stop any threads that this Screen might have created)
163      * */
164     public void onFinished() {
165     }
166 
167     /***
168      * access to wizard instance
169      * @return
170      */
171     public Wizard getWizard() {
172         return wizard;
173     }
174     
175     /***
176      * called in wizard after cosntructing a new Screen instance
177      * @param wizard
178      */
179     public void setWizard(Wizard wizard) {
180         this.wizard = wizard;
181     }
182     
183     public void setStateObserver(Observer ob) {
184         state.addObserver(ob);
185     }
186     
187     public void setBgColor(Color bgColor) {
188         m_bgColor = bgColor;
189     }
190     
191     public void paint(java.awt.Graphics g) {
192         if (m_bgColor != null) {
193             g.setColor(m_bgColor);
194             g.fillRect(0,0,getWidth(), getHeight()); 
195         }
196         super.paintChildren(g);
197     }
198     
199 }