View Javadoc

1   package com.explosion.expfmodules.wizard.standard.view;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.BorderLayout;
24  import java.awt.Dimension;
25  import java.awt.GridBagConstraints;
26  import java.awt.GridBagLayout;
27  import java.awt.Image;
28  import java.awt.Insets;
29  import java.awt.Window;
30  import java.awt.event.ActionEvent;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import javax.swing.ImageIcon;
35  import javax.swing.JButton;
36  import javax.swing.JDialog;
37  import javax.swing.JEditorPane;
38  import javax.swing.JFrame;
39  import javax.swing.JLabel;
40  import javax.swing.JPanel;
41  import javax.swing.text.html.HTMLEditorKit;
42  
43  import com.explosion.expf.ExpComponent;
44  import com.explosion.expfmodules.wizard.Wizard;
45  import com.explosion.expfmodules.wizard.standard.Constants;
46  import com.explosion.expfmodules.wizard.standard.PreferenceDataItem;
47  import com.explosion.expfmodules.wizard.standard.StandardStepDefinition;
48  import com.explosion.utilities.FileSystemUtils;
49  import com.explosion.utilities.GeneralConstants;
50  import com.explosion.utilities.GeneralUtils;
51  import com.explosion.utilities.exception.ExceptionManagerFactory;
52  import com.explosion.utilities.preferences.Preference;
53  import com.explosion.utilities.preferences.PreferenceChangeEvent;
54  import com.explosion.utilities.preferences.PreferenceChangeListener;
55  
56  /***
57   * @author Stephen Cowx
58   * @version
59   */
60  
61  public class WizardBasePanel extends JPanel implements ExpComponent, PreferenceChangeListener
62  {
63  
64      private static org.apache.log4j.Logger log = org.apache.log4j.LogManager.getLogger(WizardBasePanel.class);
65      private static int stateofit = 0;
66      private JPanel controlPanel = new JPanel();
67      private JPanel buttonPanel = new JPanel();
68      private JPanel fillerPanel = new JPanel();
69  
70      private JButton nextButton = new JButton("Next");
71      private JButton backButton = new JButton("Back");
72      private JButton cancelButton = new JButton("Cancel");
73      private JButton helpButton = new JButton("Help");
74      
75      public static final String FONT = "<font face=\"arial\" size=\"small\">";
76  
77      private Wizard wizard;
78      private Window parent;
79  
80      public WizardBasePanel(Wizard wizard, Window parent)
81      {
82          try
83          {
84              this.wizard = wizard;
85              this.parent = parent;
86              init();
87          }
88          catch (Exception e)
89          {
90              e.printStackTrace();
91          }
92      }
93  
94      private void init() throws Exception
95      {
96          backButton.addActionListener(new java.awt.event.ActionListener()
97          {
98  
99              public void actionPerformed(ActionEvent e)
100             {
101                 backButton_actionPerformed(e);
102             }
103         });
104         nextButton.addActionListener(new java.awt.event.ActionListener()
105         {
106 
107             public void actionPerformed(ActionEvent e)
108             {
109                 nextButton_actionPerformed(e);
110             }
111         });
112         cancelButton.addActionListener(new java.awt.event.ActionListener()
113         {
114 
115             public void actionPerformed(ActionEvent e)
116             {
117                 cancelButton_actionPerformed(e);
118             }
119         });
120         
121         helpButton.addActionListener(new java.awt.event.ActionListener()
122         {
123             
124             public void actionPerformed(ActionEvent e)
125             {
126                 helpButton_actionPerformed(e);
127             }
128         });
129 
130         buttonPanel.add(backButton);
131         buttonPanel.add(nextButton);
132         buttonPanel.add(cancelButton);
133         buttonPanel.add(helpButton);
134 
135         show();
136     }
137 
138     /***
139      * @param buttonPanel
140      */
141     public void show()
142     {
143         JPanel panel = new JPanel();
144         panel.setLayout(new GridBagLayout());
145         
146         /* Add all of the components */
147         StandardStepDefinition step = (StandardStepDefinition) wizard.getCurrentStep().getStepDefinition();
148 
149         /* Check the Back button text */
150         if (wizard.getCurrentStep().getOutboundSteps() == null || wizard.getCurrentStep().getOutboundSteps().size() < 1)
151         {
152             this.nextButton.setText("Finish");
153         }
154         else
155         {
156             this.nextButton.setText("Next");
157         }
158         
159         /* Check the Next button text */
160         if (wizard.getCurrentStep() == wizard.getFirstStep())
161         {
162             this.backButton.setVisible(false);
163         }
164         else
165         {
166             this.backButton.setVisible(true);
167         }
168         
169         /* Check the Help button */
170         if (wizard.getCurrentStep().getStepDefinition().getHelp() != null)
171         {
172             this.helpButton.setVisible(true);
173         }
174         else
175         {
176             this.helpButton.setVisible(false);
177         }
178         
179         /* Get the Icon for this step */
180         JPanel iconPanel = getIconPanel(step);
181         
182         /* Get the input panel for this step */
183         StandardStepView inputPanel = (StandardStepView) step.getView();
184         
185         try
186         {
187             /* Initialise the step view */
188             step.getView().init(step);
189         }
190         catch (Exception e)
191         {
192             ExceptionManagerFactory.getExceptionManager().manageException(e,"Exception caught while initialising stepView");
193         }
194        
195         /* Add them all to the container */
196         panel.add(iconPanel, 	new GridBagConstraints(0, 0, 1, 2, 0.0, 0.0, GridBagConstraints.SOUTH, 		GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
197         panel.add(inputPanel, 	new GridBagConstraints(1, 0, 1, 1, 0.0, 1.0, GridBagConstraints.NORTHWEST, 	GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
198         panel.add(fillerPanel, 	new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, 	GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
199         panel.add(buttonPanel, 	new GridBagConstraints(0, 2, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, 	GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
200         
201         this.removeAll();
202         this.setLayout(new BorderLayout());
203         this.add(panel, BorderLayout.CENTER);
204         
205         /* Here is the code to handle disabling the Next button if mandatory values have not been filled in */
206         registerAsListener();
207         checkEnablements();
208         
209         this.revalidate();       
210     }
211     
212     /***
213      * 
214      */
215     private void registerAsListener()
216     {
217         log.debug("Called registerAsListener");
218         StandardStepDefinition step = (StandardStepDefinition) wizard.getCurrentStep().getStepDefinition();
219         List list = step.getOrderedDataItems();
220 		if (list != null)
221 		{
222 		  for (Iterator it = list.iterator(); it.hasNext();)
223 		  {
224 			 PreferenceDataItem item = (PreferenceDataItem) it.next();
225 			 log.debug(item.getName() + item.getPreference() != null ? item.getPreference().getUniqueIdentifier() : "null" );
226 			 item.getPreference().addPreferenceChangeListener(this);
227 		  }
228 		}
229     }
230 
231     /***
232      * Gets the icon and inserts it onto a panel.  Returns the Panel
233      * @param step
234      * @return
235      */
236     private JPanel getIconPanel(StandardStepDefinition step)
237     {
238         /* First the Icon */
239         Image wizardIcon = null;
240         try
241         {
242             if (step.getIcon() == null)
243             {
244                 wizardIcon = FileSystemUtils.loadImage(Constants.STANDARD_ICON, this);
245             }
246             else
247             {
248                 wizardIcon = FileSystemUtils.loadImage(step.getIcon(), this);
249             }
250         }
251         catch (Exception e)
252         {
253             ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while ");
254         }
255         JPanel iconPanel = new JPanel();
256         if (wizardIcon != null)
257         {
258             JLabel iconLabel = new JLabel();
259 
260             iconLabel.setIcon(new ImageIcon(wizardIcon));
261             iconPanel.add(iconLabel);
262         }
263         return iconPanel;
264     }
265 
266     void backButton_actionPerformed(ActionEvent e)
267     {
268         wizard.back();
269         show();
270     }
271 
272     void nextButton_actionPerformed(ActionEvent e)
273     {
274         /* Check the Back button text */
275         if (wizard.getCurrentStep().getOutboundSteps() == null || wizard.getCurrentStep().getOutboundSteps().size() < 1)
276         {
277             wizard.finish();
278             if (wizard.isFinished())
279               this.close();
280             else
281               this.show();
282         }
283         else
284         {
285             wizard.next();
286             this.show();
287         }
288         
289     }
290 
291     void cancelButton_actionPerformed(ActionEvent e)
292     {
293         wizard.cancel();
294         close();
295     }
296     
297     void helpButton_actionPerformed(ActionEvent e)
298     {
299         String help = wizard.getCurrentStep().getStepDefinition().getHelp();
300         showHelp(parent, help);
301     }
302     
303     /***
304      * This method shows a help dialog with the associated text.  The text can be HTML text
305      * @param owner
306      * @param helpText
307      */
308     private static void showHelp(Window owner, String helpText)
309     {
310         JEditorPane help = new JEditorPane();
311 		help.setEditorKit(new HTMLEditorKit());
312 		String heading = "<H1>Help</H1>";
313 		help.setText(FONT + heading+ helpText);		
314 		help.setFont(GeneralConstants.DEFAULT_WINDOW_FONT);
315 		help.setEditable(false);
316 		
317         Window helpWindow = null;
318         if (owner instanceof JDialog)
319         {
320             helpWindow = new JDialog((JDialog)owner, "Help", true);
321             ((JDialog)helpWindow).getContentPane().setLayout(new BorderLayout());
322             ((JDialog)helpWindow).getContentPane().add(help,BorderLayout.CENTER);
323         }
324         else if (owner instanceof JFrame)
325         {
326             helpWindow = new JFrame("Help");
327             ((JFrame)helpWindow).getContentPane().setLayout(new BorderLayout());
328             ((JFrame)helpWindow).getContentPane().add(help,BorderLayout.CENTER);
329         }
330         
331         helpWindow.setSize(new Dimension(400,400));
332         GeneralUtils.centreWindowInParent(helpWindow);
333         helpWindow.setVisible(true);
334 		
335     }
336 
337     public void close()
338     {
339         setVisible(false);
340         parent.dispose();
341     }
342 
343     public void applyPreferences()
344     {}
345 
346     /***
347      * @see com.explosion.utilities.preferences.PreferenceChangeListener#preferenceChanged(com.explosion.utilities.preferences.PreferenceChangeEvent)
348      * @param preferenceChangeEvent
349      */
350     public void preferenceChanged(PreferenceChangeEvent preferenceChangeEvent)
351     {
352         log.debug("Called preferenceChanged");
353         checkEnablements();
354     }
355 
356     /***
357      * 
358      */
359     private void checkEnablements()
360     {
361         log.debug("Called checkEnablements");
362         StandardStepDefinition step = (StandardStepDefinition) wizard.getCurrentStep().getStepDefinition();
363         boolean enabled = true;
364         List list = step.getOrderedDataItems();
365 		if (list != null)
366 		{
367 		  for (Iterator it = list.iterator(); it.hasNext();)
368 		  {
369 			 PreferenceDataItem item = (PreferenceDataItem) it.next();
370 			 Preference preference = item.getPreference();
371 			 if (item.isMandatory() )
372 			 {
373 			    if (item.getPreference().getType() != Preference.PROPERTY_TYPE_COLLECTION 
374 			            && item.getPreference().getValue().toString().length() < 1)
375 			    {
376 			     enabled = false;
377 			    }
378 			    else if (item.getPreference().getType() == Preference.PROPERTY_TYPE_COLLECTION 
379 			            && item.getPreference().getValues().size() < 1)
380 			    {
381 			        enabled = false;
382 			    }
383 			    
384 			 }			 
385 		  }
386 		}
387 		this.nextButton.setEnabled(enabled); 
388     }
389 
390 }
391