📄 jshop2gui.java
字号:
package JSHOP2;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
/**
* This is the graphical interface for JSHOP2
*
* @author John Shin
*
*/
public class JSHOP2GUI extends JFrame {
private static final long serialVersionUID = 112832006;
// The list of plan steps dynamically set by JSHOP2
protected static ArrayList<PlanStepInfo> planStepList;
// The number of plans found in the current problem. This is
// dynamically set by JSHOP2
protected static int numPlans;
//-----------------------------------------------------------------------------
// Used to iterate through 'planStepList'
protected int iterator;
// Used in various places when a global counter is convenient
protected int count;
// Tracks which plan is currently being worked on
protected int planNumber;
// Holds the data that make up a command from the input
protected PlanStepInfo newCommand;
// A string that temporarily holds the method info that will be
// transferred to the "reduced" command that will follow
protected String newMethod;
// A vector to store the leaf nodes that make up the plan. The elements are
// DefaultMutableTreeNode's, and their indices represent the order in which they were
// visited.
protected ArrayList<DefaultMutableTreeNode> leafNodes;
// The tree object that is displayed on-screen
protected JTree tree;
// The tree model that contains all the data within the tree structure
protected DefaultTreeModel treeModel;
// The invisible root node that is the parent for the first node displayed in the tree
protected DefaultMutableTreeNode rootNode;
// The label that displays the action taken at every step of the plan
protected Label messageLabel;
protected Label stateLabel;
// The text area where information about the current state and preconditions is displayed.
protected TextArea stateTextArea;
protected TextArea stepInfoTextArea;
// The text input box used to specifiy the step interval length
protected JTextField multiStepField;
// A hashtable of DefaultMutableTreeNode references used to keep track of the nodes in the tree.
// Currently, nodes that are deleted in the tree when backtracking aren't deleted in this
// hashtable. This doesn't seem to cause a problem except that it may become inefficient for
// large domains that backtrack a lot. In those cases, a search for a particular node within
// treeNodeReferences may waste a lot of time looking over nodes that have been deleted.
protected Hashtable<Integer, DefaultMutableTreeNode> treeNodeReferences;
// The progress bar object
protected JProgressBar progressBar;
// The label used to show the number of steps in the plan
protected Label progressLabel;
protected LeafTrackerDialog leafTracker;
// The name of the currently selected node
protected String selectedNodeName;
// Holds the dimensions of the screen. Used to center all frames and dialog boxes.
protected Dimension screenSize;
//-----------------------------------------------------------------------------
/**
* The default constructor. Call this constructor only after the plan step
* list has been initialized by <code>setPlanStepList</code> and the the
* number of plans has been set by <code>setNumPlans</code>. When called
* successfully, this constructor will launch the GUI.
*
*/
public JSHOP2GUI() {
// Calculate values to center all frames and dialog boxes
Toolkit toolkit = Toolkit.getDefaultToolkit();
screenSize = toolkit.getScreenSize();
initFieldsAndCreateInterface();
int x = (screenSize.width - getWidth()) / 2;
int y = (screenSize.height - getHeight()) / 2;
// Center this frame's location
setLocation(x, y);
progressBar.setMaximum( planStepList.size() );
progressBar.setValue( 0 );
String msg = "Progress: ";
msg += String.valueOf( iterator );
msg += " / ";
msg += String.valueOf( planStepList.size() );
progressLabel.setText( msg );
setVisible( true );
}
//-----------------------------------------------------------------------------
/**
* This function is used to pass in the list of plan steps that represent the
* actions taken by JSHOP2 to find plans for the current problem.
*
* @param inputList - an ArrayList of PlanStepInfo objects that are used to
* reconstruct the plan finding process
*/
public static void setPlanStepList(ArrayList<PlanStepInfo> inputList) {
planStepList = inputList;
}
/**
* This function is used to set the total number of plans found for the current
* problem by JSHOP2
*
* @param numPlansIn - an integer representing the total number of plans found
*/
public static void setNumPlans(int numPlansIn) {
numPlans = numPlansIn;
}
//-----------------------------------------------------------------------------
/**
* Initializes all fields and constructs the graphical interface
*/
private void initFieldsAndCreateInterface() {
iterator = 0;
count = 0;
planNumber = 0;
treeNodeReferences = new Hashtable<Integer, DefaultMutableTreeNode>();
getContentPane().setLayout( new BorderLayout() );
newMethod = "";
selectedNodeName = "";
// createPlanList();
leafNodes = new ArrayList<DefaultMutableTreeNode>();
// Creating the leafTracker object
leafTracker = new LeafTrackerDialog( this );
// Creating the menu bar and menus
MenuBar mbar = new MenuBar();
setMenuBar( mbar );
SHOP2GUIMenuHandler menuHandler = new SHOP2GUIMenuHandler();
MenuItem item1, item2;
Menu file = new Menu( "File" );
file.add( item1 = new MenuItem("Exit") );
mbar.add( file );
// Registering listeners for File menu items
item1.addActionListener( menuHandler );
Menu view = new Menu( "View" );
view.add( item1 = new MenuItem("Leaf Node Tracker...") );
view.add( item2 = new MenuItem("Show State..."));
mbar.add( view );
// Registering listeners for View menu items
item1.addActionListener( menuHandler );
item2.addActionListener( menuHandler );
// ********************************************************* //
// ------------------ Creating the Center ------------------ //
// ********************************************************* //
// Creating tree and its scroll pane
rootNode = new DefaultMutableTreeNode( "ROOTNODE" );
treeModel = new DefaultTreeModel(rootNode);
tree = new JTree( treeModel );
tree.setRootVisible(false);
tree.putClientProperty("JTree.lineStyle", "Angled");
tree.setCellRenderer( new NodeRenderer() ); // setting cell rendered to paint nodes
ToolTipManager.sharedInstance().registerComponent(tree); //enable tool tips
tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
// Adding a treeSelectionListner to the tree
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode treeNode =
(DefaultMutableTreeNode)e.getPath().getLastPathComponent();
Node node = (Node)treeNode.getUserObject();
// setting the name for the currently selected node
selectedNodeName = node.name;
// setting the text for stateTextArea
String msg = node.getState();
stateTextArea.replaceRange( msg, 0, stateTextArea.getText().length() );
stateTextArea.setCaretPosition(0);
// setting the text for Plan Step Info
msg = "";
if (!node.method.equals("")) {
msg += "METHOD USED:\n";
msg += node.method;
} else if (node.delAdd != null) {
if (node.delAdd[0] != null) {
Vector v = node.delAdd[0];
if (v.size() > 0) {
msg += "DELETED ATOMS:\n";
for (int i = 0; i < v.size(); i++) {
msg += v.get(i).toString();
msg += "\n";
} msg += "\n";
}
} if (node.delAdd[1] != null) {
Vector v = node.delAdd[1];
if (v.size() > 0) {
msg += "ADDED ATOMS:\n";
for (int i = 0; i < v.size(); i++) {
msg += v.get(i).toString();
msg += "\n";
} msg += "\n";
}
} if (node.delAdd[2] != null) {
Vector v = node.delAdd[2];
if (v.size() > 0) {
msg += "DELETED PROTECTIONS:\n";
for (int i = 0; i < v.size(); i++) {
msg += v.get(i).toString();
msg += "\n";
} msg += "\n";
}
} if (node.delAdd[3] != null) {
Vector v = node.delAdd[3];
if (v.size() > 0) {
msg += "ADDED PROTECTIONS:\n";
for (int i = 0; i < v.size(); i++) {
msg += v.get(i).toString();
msg += "\n";
} msg += "\n";
}
}
}
stepInfoTextArea.replaceRange( msg, 0, stepInfoTextArea.getText().length() );
stepInfoTextArea.setCaretPosition(0);
msg = "Current State ( Total: ";
msg += node.getStateSize();
msg += " )";
stateLabel.setText( msg );
}
});
JScrollPane jpane = new JScrollPane(tree);
getContentPane().add( jpane, BorderLayout.CENTER );
// ****************************************************** //
// --------------- Creating the East Side --------------- //
// ****************************************************** //
// Creating buttons
JButton singleStepButton = new JButton( "Single Step" );
singleStepButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
runOneStep();
}
});
JButton multiStepButton = new JButton( "Multi-Step" );
multiStepButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -