/*
* SelectPanel.java
*
* Created on 3 December 2004, 13:42
* @author k380h11
*/
package checkers;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import jess.*;
/**
* This dialog is a kind of jess (facts) commands ameliorated. In fact, when this dialogs is opens, two jess queries are sent to the database and the results are displayed in the two tables. The first table represents all the movement calculated by Jess. The second shows the actual and probable future positions of the pieces.
* Five inputs are present to enquire for specific movement in order to reduce the list of reply. Unfortunately, this option was foreseen, but has not been implemented. However, the widget are leaved for next amelioration of the program.
* They con be used by implementeing the falowing code for example:
* <pre>
* Checker.r.reset ();
* Checker.r.executeCommand("(assert (deepness "+ (String) levelInput.getText () + " " + (String) colorSelect.getSelectedItem () +"))");
* Checker.r.executeCommand("(focus MOVEMENT)");
* Checker.r.executeCommand("(run)");
* </pre>
* @author k31380h11
*/
public class SelectDialog
extends JDialog {
/**
* This method set the conditions for the game's graphical user interface.
*/
public SelectDialog () {
super ();
try {
System.
err.
println ("Warning: UnsupportedLookAndFeel: " + laf
);
System.
err.
println ("Error loading " + laf
+ ": " + exc
);
}
hide ();
}
});
setResizable (true);
setTitle (title);
setLocation (200, 200);
titleLabel.
setFont (new Font ("MS Sans Serif",
1,
18));
titleLabel.setText (title);
SelectPanel selectPanel = new SelectPanel ();
pack ();
}
/**
* main panel
*/
public class SelectPanel
extends JPanel {
/** Creates new form SelectPanel */
public SelectPanel () {
movementTable
= new JTable ();
add (pieceSelect);
add (actionSelect);
add(colorSelect);
add (pointInput);
pieceLabel.setText ("Piece");
add (pieceLabel);
colorLabel.setText("Color");
add(colorLabel);
actionLabel.setText ("Action");
add (actionLabel);
pointLabel.setText ("Point");
add (pointLabel);
levelLabel.setText ("Level");
add (levelLabel);
add (levelInput);
searchButton.setText ("Search");
searchButtonActionPerformed (evt);
}
});
add (searchButton);
movementTable.setRowSelectionAllowed (false);
jScrollPane.setViewportView (movementTable);
add (jScrollPane);
PieceTable.setRowSelectionAllowed (false);
jScrollPane2.setViewportView (PieceTable);
add (jScrollPane2);
init ();
}
/**
* This method is used to set up the window for the searchButton by collectin information from Jess.
* The information that it need arer the facts of the piece at all levels.
*@throws an Jessexception if the java program was unable to get the information from Jess.
*/
private void searchButtonActionPerformed
(ActionEvent evt
) {
try {
Checker.r.executeCommand ("(defquery search (MOVEMENT::movement (point ?point) (level ?level) (action ?action) (numberOfAction ?numberAction) (next ?next)))");
Checker.r.store ("RESULT", Checker.r.runQuery ("search", new ValueVector ()));
Checker.r.executeCommand ("(store RESULT (run-query search))");
Iterator it
= (Iterator) Checker.
r.
fetch ("RESULT").
externalAddressValue (null);
while (it.hasNext ()) {
Token token = (Token) it.next ();
resultMovement.add (token.fact (1));
}
Checker.r.executeCommand ("(defquery searchPiece (MAIN::Piece (PRS ?next) (OBJECT ?piece) (number ?number) (color ?color) (level ?level) (virtual ?virtual) (king ?king)))");
Checker.r.store ("RESULT2", Checker.r.runQuery ("searchPiece",new ValueVector ()));
Checker.r.executeCommand ("(store RESULT2 (run-query searchPiece))");
it
= (Iterator) Checker.
r.
fetch ("RESULT2").
externalAddressValue (null);
while (it.hasNext ()) {
Token token = (Token) it.next ();
resultPiece.add (token.fact (1));
}
} catch (JessException e
) {System.
err.
println (e
);}
if (resultMovement != null) {
int resultSize = resultMovement.size ();
String title
[] = {"Number",
"Color",
"Square",
"Point",
"NOA",
"Div",
"Action",
"Level",
"Next"};
for (int i = 0; i<resultSize ; i++) {
try {
Fact fact = (Fact) resultMovement.get (i);
Piece piece = (Piece) fact.getSlotValue ("piece").externalAddressValue (null);
Grid.Square curent = piece.getSquare ();
int number = piece.getNumber ();
boolean color = piece.getColor ();
Grid.Square next = (Grid.Square) fact.getSlotValue ("next").externalAddressValue (null);
double pts
= Double.
parseDouble(fact.
getSlotValue ("point").
toString ());
String level
= fact.
getSlotValue ("level").
toString ();
double numberOfAction
= Double.
parseDouble(fact.
getSlotValue ("numberOfAction").
toString ());
listData
[i
][0] = Integer.
toString (number
);
listData[i][1] = color ? "white" : "red";
listData[i][2] = curent.toString ();
listData
[i
][3] = Double.
toString(pts
);
listData
[i
][4] = Double.
toString(numberOfAction
);
listData
[i
][5] = Double.
toString(pts
/ numberOfAction
);
listData[i][7] = level;
listData[i][8] = next.toString ();
} catch (JessException e
) {System.
err.
println (e
);}
}
}
if (resultPiece != null) {
int resultSize = resultPiece.size ();
String title
[] = {"square",
"number",
"color",
"level",
"virtual",
"king"};
for (int i = 0; i<resultSize ; i++) {
try {
Fact fact = (Fact) resultPiece.get (i);
listData[i][0] = fact.getSlotValue ("square").externalAddressValue (null).toString ();
listData[i][1] = fact.getSlotValue ("number").toString ();
listData[i][2] = fact.getSlotValue ("color").toString ();
listData[i][3] = fact.getSlotValue ("level").toString ();
listData[i][4] = fact.getSlotValue ("virtual").toString ();
listData[i][5] = fact.getSlotValue ("king").toString ();
} catch (JessException e
) {System.
err.
println (e
);}
}
}
}
/**
* This method set bounds for the graphical user interface.
* This is done give parameter where to start and end i pixels
* and causes this container to lay out its components.
*/
public void doLayout () {
pieceSelect.setBounds (20, 40, 90, 20);
actionSelect.setBounds (210, 40, 80, 20);
colorSelect.setBounds(130, 40, 60, 20);
pointInput.setBounds (310, 40, 40, 20);
pieceLabel.setBounds (20, 10, 40, 20);
colorLabel.setBounds(130, 10, 40, 20);
actionLabel.setBounds (210, 10, 40, 20);
pointLabel.setBounds (310, 10, 40, 20);
levelLabel.setBounds (360, 10, 40, 20);
levelInput.setBounds (360, 40, 40, 20);
searchButton.setBounds (420, 40, 70, 20);
jScrollPane.setBounds (20, 90, 500, 120);
movementTable.setBounds (20, 90, 500, 120);
jScrollPane2.setBounds (20, 240, 500, 120);
PieceTable.setBounds (20, 240, 500, 120);
}
/**
* This method is used to set minimum size of the graphical user interface window .
* @return the dimension height, width.
*/
}
/**
* This method is used to set preferred size of the graphical user interface window .
* @return the dimension height, width.
*/
}
/**
* This method is used initilize the graphical user interface window .
* the method calls other methods to initilize board .
*/
public void init () {
actionSelect.addItem ("");
actionSelect.addItem ("eat");
actionSelect.addItem ("move");
colorSelect.
addItem (Boolean.
toString(Checker.
getInstance().
getTurn()).
toUpperCase ());
colorSelect.
addItem (Boolean.
toString(!Checker.
getInstance().
getTurn()).
toUpperCase ());
levelInput.setText("0");
pieceSelect.addItem (null);
searchButtonActionPerformed (null);
}
}
}