Java tutorial
/* * Copyright (c) 2003- Shinji Kashihara. All rights reserved. * This program are made available under the terms of the Common Public License * v1.0 which accompanies this distribution, and is available at cpl-v10.html. */ package mergedoc.ui; import java.awt.Dimension; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * ??? * @author Shinji Kashihara */ public class ButtonBar extends JPanel { /** */ private static final Log log = LogFactory.getLog(ButtonBar.class); /** ? */ private JButton runButton; /** ?? */ private ActionListener runListener; /** ?? */ private ActionListener backListener; /** ? */ private JButton endButton; /** ?? */ private ActionListener cancelListener; /** ?? */ private ActionListener endListener; /** */ private static interface ButtonState { void apply(); } /** ? */ public final ButtonState INIT_STATE = new ButtonState() { @Override public void apply() { runButton.setText("(R)"); runButton.setMnemonic(KeyEvent.VK_R); runButton.setEnabled(true); runButton.addActionListener(runListener); endButton.setText("(E)"); endButton.setMnemonic(KeyEvent.VK_E); endButton.setEnabled(true); endButton.addActionListener(endListener); } }; /** ? */ public final ButtonState WORKING_STATE = new ButtonState() { @Override public void apply() { runButton.setText("(R)"); runButton.setEnabled(false); endButton.setText("(C)"); endButton.setMnemonic(KeyEvent.VK_C); endButton.setEnabled(true); endButton.addActionListener(cancelListener); } }; /** */ public final ButtonState FINISH_STATE = new ButtonState() { @Override public void apply() { runButton.setText("(B)"); runButton.setMnemonic(KeyEvent.VK_B); runButton.setEnabled(true); runButton.addActionListener(backListener); endButton.setText("(E)"); endButton.setMnemonic(KeyEvent.VK_E); endButton.setEnabled(true); endButton.addActionListener(endListener); } }; /** */ public final ButtonState CANCEL_STATE = new ButtonState() { @Override public void apply() { runButton.setText("(B)"); runButton.setMnemonic(KeyEvent.VK_B); runButton.setEnabled(true); runButton.addActionListener(backListener); endButton.setText("(C)"); endButton.setEnabled(false); } }; /** * ?? */ public ButtonBar() { setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); } /** * ??? * ?????????????????<br> * ???????? * @param state */ public void setState(final ButtonState state) { if (runListener == null || backListener == null || cancelListener == null || endListener == null) { String m = "?????????????"; RuntimeException e = new IllegalStateException(m); log.fatal(m, e); throw e; } // ? final JLabel filler = new JLabel(); int maxWidth = (int) ComponentFactory.createMaxDimension().getWidth(); filler.setMaximumSize(new Dimension(maxWidth, 0)); runButton = createButton(); endButton = createButton(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { // ?????? removeAll(); add(filler); add(runButton); add(ComponentFactory.createSpacer(10, 0)); add(endButton); // ? state.apply(); } }); } /** * ??????? * @return */ private JButton createButton() { JButton button = new JButton(); ComponentFactory.ensureSize(button, 100, 21); return button; } /** * ????? * @param runListener ?? */ public void setRunListener(ActionListener runListener) { this.runListener = runListener; } /** * ????? * @param runListener ?? */ public void setBackListener(ActionListener backListener) { this.backListener = backListener; } /** * ????? * @param cancelListener ?? */ public void setCancelListener(ActionListener cancelListener) { this.cancelListener = cancelListener; } /** * ????? * @param endListener ?? */ public void setEndListener(ActionListener endListener) { this.endListener = endListener; } /** * ???????<br> * ???????? * @param enabled ???? true */ @Override public void setEnabled(final boolean enabled) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { runButton.setEnabled(enabled); endButton.setEnabled(enabled); } }); } }