📄 wizard.java
字号:
/**
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jmt.gui.wizard;
import jmt.gui.common.Manager;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
/**
* @author alyf (Andrea Conti)
* Date: 11-set-2003
* Time: 17.38.00
*/
//Modified by Federico Dall'Orso
/**
* a generic Wizard framework
*/
public class Wizard extends JFrame {
private static final boolean DEBUG = false;
/* button Actions */
protected AbstractAction ACTION_NEXT = new AbstractAction("Next >") {
{
//NEW Dall'Orso
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK));
//END
putValue(SHORT_DESCRIPTION, "Go to next screen");
}
public void actionPerformed(ActionEvent e) {
showNext();
}
};
protected AbstractAction ACTION_PREV = new AbstractAction("< Back") {
{
//NEW Dall'Orso
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ActionEvent.CTRL_MASK));
//END
putValue(SHORT_DESCRIPTION, "Go to previous screen");
}
public void actionPerformed(ActionEvent e) {
showPrev();
}
};
protected AbstractAction ACTION_CANCEL = new AbstractAction("Cancel") {
{
putValue(SHORT_DESCRIPTION, "Exit discarding all changes");
}
public void actionPerformed(ActionEvent e) {
cancel();
}
};
protected AbstractAction ACTION_FINISH = new AbstractAction("Solve") {
{
putValue(SHORT_DESCRIPTION, "Solve this Model");
}
public void actionPerformed(ActionEvent e) {
if (checkFinish()) finish();
}
};
protected AbstractAction ACTION_HELP = new AbstractAction("Help") {
{
putValue(SHORT_DESCRIPTION, "Show quick help screen");
}
public void actionPerformed(ActionEvent e) {
help();
}
};
protected JTabbedPane tabbedPane;
protected JComponent buttons;
protected WizardPanel currentPanel = null;
protected int currentIndex;
protected int panelCount;
protected List panels;
public Wizard() {
Manager.addJMTWindow(this);
setDefaultCloseOperation(Wizard.DO_NOTHING_ON_CLOSE); //disable automatic event handling
// close=cancel
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
cancel();
}
});
panels = new ArrayList();
currentIndex = -1;
panelCount = 0;
tabbedPane = makeTabbedPane();
buttons = makeButtons();
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(tabbedPane, BorderLayout.CENTER);
cp.add(buttons, BorderLayout.SOUTH);
updateActions();
}
public Wizard(String title) {
this();
setTitle(title);
}
public Wizard(String title, Image icon) {
this(title);
this.setIconImage(icon);
}
/**
* @return the button panel
*/
protected JComponent makeButtons() {
JButton button_finish = new JButton(ACTION_FINISH);
JButton button_cancel = new JButton(ACTION_CANCEL);
JButton button_next = new JButton(ACTION_NEXT);
JButton button_previous = new JButton(ACTION_PREV);
JButton button_help = new JButton(ACTION_HELP);
JPanel res = new JPanel();
res.add(button_previous);
res.add(button_next);
res.add(button_finish);
res.add(button_cancel);
//res.add(Box.createHorizontalStrut(80));
res.add(button_help);
return res;
}
protected JTabbedPane makeTabbedPane() {
return new JTabbedPane() {
/* we hook into the tabbedpane's tab switching method and make the change
only if the current panel agrees */
public void setSelectedIndex(int index) {
int oldIndex = model.getSelectedIndex();
if (index != oldIndex) { //ignore useless requests
if (currentPanel != null) {
if (index > oldIndex) {
if (!currentPanel.canGoForward()) return;
} else {
if (!currentPanel.canGoBack()) return;
}
currentPanel.lostFocus();
}
super.setSelectedIndex(index);
currentIndex = model.getSelectedIndex();
currentPanel = (WizardPanel) getComponentAt(currentIndex);
if(oldIndex >= 0 && oldIndex<getTabCount()){
WizardPanel oldPanel = (WizardPanel) getComponentAt(oldIndex);
setTitleAt(oldIndex, oldPanel.getName());
}
setTitleAt(index, "<html><body align=\"center\"><font color=\"000000\"><b>"+
currentPanel.getName()+
"</b></font></body></html>");
currentPanel.gotFocus();
updateActions();
}
}
};
}
protected boolean checkFinish() {
if (currentPanel != null) currentPanel.lostFocus();
for (int i = 0; i < panelCount; i++) {
if (!((WizardPanel) panels.get(i)).canFinish()) return false;
}
return true;
}
/**
* Called when the "Finish" button is pressed. Must be overridden in subclasses!
*/
protected void finish() {
currentPanel.lostFocus();
dispose();
}
/**
* Called when the "Cancel" button is pressed. Should be overridden in subclasses
*/
protected void cancel() {
dispose();
Manager.exit(this);
}
private void help() {
currentPanel.help();
}
protected void showNext() {
if (currentIndex == -1) return;
if (currentIndex == panels.size() - 1) return; //should not happen anyway
tabbedPane.setSelectedIndex(currentIndex + 1);
}
protected void showPrev() {
if (currentIndex == -1) return;
if (currentIndex == 0) return; //should not happen anyway
tabbedPane.setSelectedIndex(currentIndex - 1);
}
/**
* enables/disables buttons according to current status
*/
protected void updateActions() {
if (DEBUG) System.out.println("Wizard.updateActions()");
ACTION_NEXT.setEnabled(currentIndex >= 0 && currentIndex < panelCount - 1);
ACTION_PREV.setEnabled(currentIndex > 0);
ACTION_CANCEL.setEnabled(true);
ACTION_FINISH.setEnabled(panelCount > 0);
ACTION_HELP.setEnabled(panelCount > 0);
}
/**
* Add a new WizardPanel to the Wizard
*/
public void addPanel(WizardPanel p) {
p.setParentWizard(this);
panels.add(p);
tabbedPane.add(p.getName(), p);
panelCount = tabbedPane.getTabCount();
if (panelCount == 1) {
tabbedPane.setSelectedIndex(0);
}
updateActions();
}
/**
* Add a new WizardPanel to the Wizard
*/
public void addPanel(WizardPanel p, int index) {
p.setParentWizard(this);
panels.add(p);
tabbedPane.insertTab(p.getName(), null, p, "",index);
panelCount = tabbedPane.getTabCount();
if (panelCount == 1) {
tabbedPane.setSelectedIndex(0);
}else{
tabbedPane.setSelectedIndex(index-1);
tabbedPane.setSelectedIndex(index);
}
updateActions();
}
/**
* Removes a WizardPanel to the Wizard
*/
public void removePanel(WizardPanel p) {
tabbedPane.remove(p);
panelCount = tabbedPane.getTabCount();
panels.remove(p);
updateActions();
}
/**
* Sets position of the frame at center of the screen.
*/
public static void centerOnScreen(Component c) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
c.setLocation((int) (screenSize.getWidth() - c.getWidth()) / 2,
(int) (screenSize.getHeight() - c.getHeight()) / 2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -