1 43 44 package org.jfree.ui; 45 46 import java.awt.BorderLayout ; 47 import java.awt.Container ; 48 import java.awt.event.ActionEvent ; 49 import java.awt.event.ActionListener ; 50 import java.util.ArrayList ; 51 52 import javax.swing.BorderFactory ; 53 import javax.swing.JButton ; 54 import javax.swing.JDialog ; 55 import javax.swing.JFrame ; 56 import javax.swing.JPanel ; 57 58 74 public class WizardDialog extends JDialog implements ActionListener { 75 76 77 private Object result; 78 79 80 private int step; 81 82 83 private WizardPanel currentPanel; 84 85 87 private java.util.List panels; 88 89 90 private JButton previousButton; 91 92 93 private JButton nextButton; 94 95 96 private JButton finishButton; 97 98 99 private JButton helpButton; 100 101 109 public WizardDialog(final JDialog owner, final boolean modal, 110 final String title, final WizardPanel firstPanel) { 111 112 super(owner, title + " : step 1", modal); 113 this.result = null; 114 this.currentPanel = firstPanel; 115 this.step = 0; 116 this.panels = new ArrayList (); 117 this.panels.add(firstPanel); 118 setContentPane(createContent()); 119 120 } 121 122 130 public WizardDialog(final JFrame owner, final boolean modal, 131 final String title, final WizardPanel firstPanel) { 132 133 super(owner, title + " : step 1", modal); 134 this.result = null; 135 this.currentPanel = firstPanel; 136 this.step = 0; 137 this.panels = new ArrayList (); 138 this.panels.add(firstPanel); 139 setContentPane(createContent()); 140 } 141 142 147 public Object getResult() { 148 return this.result; 149 } 150 151 158 public int getStepCount() { 159 return 0; 160 } 161 162 167 public boolean canDoPreviousPanel() { 168 return (this.step > 0); 169 } 170 171 176 public boolean canDoNextPanel() { 177 return this.currentPanel.hasNextPanel(); 178 } 179 180 186 public boolean canFinish() { 187 return this.currentPanel.canFinish(); 188 } 189 190 197 public WizardPanel getWizardPanel(final int step) { 198 if (step < this.panels.size()) { 199 return (WizardPanel) this.panels.get(step); 200 } 201 else { 202 return null; 203 } 204 } 205 206 211 public void actionPerformed(final ActionEvent event) { 212 final String command = event.getActionCommand(); 213 if (command.equals("nextButton")) { 214 next(); 215 } 216 else if (command.equals("previousButton")) { 217 previous(); 218 } 219 else if (command.equals("finishButton")) { 220 finish(); 221 } 222 } 223 224 227 public void previous() { 228 if (this.step > 0) { 229 final WizardPanel previousPanel = getWizardPanel(this.step - 1); 230 previousPanel.returnFromLaterStep(); 232 final Container content = getContentPane(); 233 content.remove(this.currentPanel); 234 content.add(previousPanel); 235 this.step = this.step - 1; 236 this.currentPanel = previousPanel; 237 setTitle("Step " + (this.step + 1)); 238 enableButtons(); 239 pack(); 240 } 241 } 242 243 246 public void next() { 247 248 WizardPanel nextPanel = getWizardPanel(this.step + 1); 249 if (nextPanel != null) { 250 if (!this.currentPanel.canRedisplayNextPanel()) { 251 nextPanel = this.currentPanel.getNextPanel(); 252 } 253 } 254 else { 255 nextPanel = this.currentPanel.getNextPanel(); 256 } 257 258 this.step = this.step + 1; 259 if (this.step < this.panels.size()) { 260 this.panels.set(this.step, nextPanel); 261 } 262 else { 263 this.panels.add(nextPanel); 264 } 265 266 final Container content = getContentPane(); 267 content.remove(this.currentPanel); 268 content.add(nextPanel); 269 270 this.currentPanel = nextPanel; 271 setTitle("Step " + (this.step + 1)); 272 enableButtons(); 273 pack(); 274 275 } 276 277 280 public void finish() { 281 this.result = this.currentPanel.getResult(); 282 setVisible(false); 283 } 284 285 289 private void enableButtons() { 290 this.previousButton.setEnabled(this.step > 0); 291 this.nextButton.setEnabled(canDoNextPanel()); 292 this.finishButton.setEnabled(canFinish()); 293 this.helpButton.setEnabled(false); 294 } 295 296 301 public boolean isCancelled() { 302 return false; 303 } 304 305 310 public JPanel createContent() { 311 312 final JPanel content = new JPanel (new BorderLayout ()); 313 content.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 314 content.add((JPanel ) this.panels.get(0)); 315 final L1R3ButtonPanel buttons = new L1R3ButtonPanel("Help", "Previous", "Next", "Finish"); 316 317 this.helpButton = buttons.getLeftButton(); 318 this.helpButton.setEnabled(false); 319 320 this.previousButton = buttons.getRightButton1(); 321 this.previousButton.setActionCommand("previousButton"); 322 this.previousButton.addActionListener(this); 323 this.previousButton.setEnabled(false); 324 325 this.nextButton = buttons.getRightButton2(); 326 this.nextButton.setActionCommand("nextButton"); 327 this.nextButton.addActionListener(this); 328 this.nextButton.setEnabled(true); 329 330 this.finishButton = buttons.getRightButton3(); 331 this.finishButton.setActionCommand("finishButton"); 332 this.finishButton.addActionListener(this); 333 this.finishButton.setEnabled(false); 334 335 buttons.setBorder(BorderFactory.createEmptyBorder(4, 0, 0, 0)); 336 content.add(buttons, BorderLayout.SOUTH); 337 338 return content; 339 } 340 341 } 342 | Popular Tags |