/* * @(#)SequenceDialog.java * * This file is part of webCDwriter - Network CD/DVD Writing. * * Copyright (C) 2003-2006 Jörg P. M. Haeger * * webCDwriter is free software. See CDcreator.java for details. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A sequence of dialogs with previous and next buttons. * * @version 20060420 * @author Jörg P. M. Haeger */ abstract public class SequenceDialog extends JDialog { protected JPanel controlPanel; protected int state = 0; protected boolean helpState; private boolean canceled; protected JButton helpBackButton, helpButton, cancelButton, backButton, nextButton, OKButton; public SequenceDialog(String title) { super(CDcreator.frame, Version.product, true); setTitle(i18n(title)); initControlPanel(); getContentPane().add(controlPanel, BorderLayout.SOUTH); updateButtons(); addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent e) { if (OKButton != null) OKButton.requestFocus(); } public void windowClosing(WindowEvent e) { cancelPressed(); } }); } abstract protected boolean apply(); protected boolean backPressed() { if (state > 0) state--; updateButtons(); enterState(state); return false; } public void cancelPressed() { canceled = true; setVisible(false); } protected JComponent createHeader(String label) { label = i18n(label); MyBox hbox = new MyBox(BoxLayout.X_AXIS); hbox.setAlignmentX(LEFT_ALIGNMENT); JLabel l = new JLabel(label); l.setAlignmentY(CENTER_ALIGNMENT); hbox.add(l); hbox.add(Box.createHorizontalStrut(5)); JSeparator s = new JSeparator(); s.setAlignmentY(TOP_ALIGNMENT); hbox.add(s); return hbox; } protected void enterHelpState(int aState) { } protected void enterState(int aState) { } protected boolean hasHelp(int aState) { return false; } protected boolean hasMore() { return false; } String i18n(String str) { if (str.startsWith(".")) str = getClass().getName() + str; return CDcreator.i18n(str); } private void initControlPanel() { JPanel hp = new JPanel(); hp.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); hp.setLayout(new BoxLayout(hp, BoxLayout.X_AXIS)); helpBackButton = new JButton(i18n("MainWin.back")); helpBackButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { helpState = false; updateButtons(); enterState(state); } }); helpBackButton.setVisible(false); hp.add(helpBackButton); helpButton = new JButton(i18n("help")); helpButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { helpState = true; updateButtons(); enterHelpState(state); } }); hp.add(helpButton); cancelButton = new JButton(i18n("cancel")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cancelPressed(); } }); hp.add(cancelButton); hp.add(Box.createHorizontalGlue()); hp.add(Box.createHorizontalStrut(10)); backButton = new JButton(i18n("MainWin.back")); backButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { backPressed(); } }); backButton.setVisible(false); hp.add(backButton); nextButton = new JButton(i18n("MainWin.continue")); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { nextPressed(); } }); nextButton.setVisible(hasMore()); hp.add(nextButton); OKButton = new JButton(i18n("OK")) { public Dimension getPreferredSize() { Dimension a = nextButton.getPreferredSize(); Dimension b = super.getPreferredSize(); if (b.width > a.width) a.width = b.width; if (b.height > a.height) b.height = a.height; return a; } }; OKButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { OKPressed(); } }); OKButton.setVisible(!hasMore()); hp.add(OKButton); JPanel vp = new JPanel(); vp.setBorder(BorderFactory.createEmptyBorder(10, 5, 0, 5)); vp.setLayout(new BoxLayout(vp, BoxLayout.Y_AXIS)); vp.add(new JSeparator(JSeparator.HORIZONTAL)); vp.add(hp); controlPanel = vp; } protected void updateButtons() { getRootPane().setDefaultButton(nextButton); helpBackButton.setVisible(helpState); helpButton.setEnabled(hasHelp(state)); helpButton.setVisible(!helpState); cancelButton.setVisible(!helpState); backButton.setVisible(!helpState && state > 0); nextButton.setVisible(!helpState && hasMore()); OKButton.setVisible(!helpState && !hasMore()); JButton defaultButton = OKButton; if (!defaultButton.isVisible()) defaultButton = nextButton; if (!defaultButton.isVisible()) defaultButton = helpBackButton; getRootPane().setDefaultButton(defaultButton); } protected boolean nextPressed() { if (!hasMore()) return false; state++; updateButtons(); enterState(state); return false; } protected void OKPressed() { if (apply()) setVisible(false); } public void pack() { boolean v = backButton.isVisible(); backButton.setVisible(true); super.pack(); backButton.setVisible(v); } public boolean wasCanceled() { return canceled; } /** * Since Netscape 4 does not like a "new Box(BoxLayout.Y_AXIS)" */ protected static class MyBox extends JPanel { MyBox(int axis) { setLayout(new BoxLayout(this, axis)); } } }