import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.DefaultCaret;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
/**
* CMSC 335 Project #3
*/
/**
* @author Roger Joudrey
*/
public final class GUI extends JFrame {
private static final long serialVersionUID = 1L;
public static JPanel getJpJobs() {
return jpJobs;
}
public static void setJpJobs(JPanel aJpJobs) {
jpJobs = aJpJobs;
}
private JTextField jtfmsg = new JTextField(10);
private JRadioButton jrbChar, jrbParty, jrbLoot, jrbArt;
private JButton jbtFile, jbtFear, jbtEmp, jbtCarrCap, jbtTrWeight, jbtTrVal;
private JTextArea jpTxtArea;
private JScrollPane jpScroll;
private DefaultCaret caret;
private static JPanel jpJobs = new JPanel();
private JTree jTree;
public void updateTextArea(final String text) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
getJpTxtArea().append(text);
getJpTxtArea().setTabSize(7);
getJpScroll().setPreferredSize(new Dimension(200,600));
}
});
}
public void redir() {
OutputStream out;
out = new OutputStream() {
@Override
public void write(int b) throws IOException {
updateTextArea(String.valueOf((char) b));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
updateTextArea(new String(b, off, len));
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
public GUI() throws FileNotFoundException {
this.jpScroll = new JScrollPane(getJpTxtArea());
this.jpTxtArea = new JTextArea();
final Project3 cave = new Project3();
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Sorcerer's Cave");
jTree = new JTree(top);
createNodes(top, cave);
jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
for (int i = 0; i < jTree.getRowCount(); i++) {
jTree.expandRow(i);
}
JPanel jpFile = new JPanel();
jpFile.setLayout(new BorderLayout(5, 0));
jpFile.add(new JLabel("Choose a source file: "), BorderLayout.WEST);
jpFile.add(jbtFile = new JButton("Open"), BorderLayout.EAST);
JPanel jpSort1 = new JPanel();
jpSort1.setLayout(new FlowLayout());
jpSort1.add(new JLabel("Sort Creatures: "));
jpSort1.add(jbtFear = new JButton("Fear"));
jpSort1.add(jbtEmp = new JButton("Empathy"));
jpSort1.add(jbtCarrCap = new JButton("Carrying Capacity"));
JPanel jpSort2 = new JPanel();
jpSort2.setLayout(new FlowLayout());
jpSort2.add(new JLabel("Sort Treasure: "));
jpSort2.add(jbtTrWeight = new JButton("Weight"));
jpSort2.add(jbtTrVal = new JButton("Value"));
JPanel jpText = new JPanel();
jpText.setLayout(new BorderLayout());
jpText.add(new JLabel("Enter name: "), BorderLayout.WEST);
jpText.add(jtfmsg, BorderLayout.CENTER);
jtfmsg.setHorizontalAlignment(JTextField.RIGHT);
JPanel jpRad = new JPanel();
jpRad.setLayout(new FlowLayout(5));
jpRad.add(new JLabel("Search by: "));
jpRad.add(jrbChar = new JRadioButton("Character"));
jpRad.add(jrbParty = new JRadioButton("Party"));
jpRad.add(jrbLoot = new JRadioButton("Treasure"));
jpRad.add(jrbArt = new JRadioButton("Artifact"));
jrbChar.setSelected(true);
ButtonGroup group = new ButtonGroup();
group.add(jrbChar);
group.add(jrbParty);
group.add(jrbLoot);
group.add(jrbArt);
JButton jbtCave;
JPanel jpBtns = new JPanel();
jpBtns.setLayout(new GridLayout(1, 50));
jpBtns.add(new JLabel("Return the elements in the cave:"));
jpBtns.add(jbtCave = new JButton("Enter... "), BorderLayout.EAST);
JPanel jpSearch = new JPanel();
jpSearch.setLayout(new GridLayout(1, 2));
jpSearch.add(jpRad);
jpSearch.add(jpText);
JPanel jpSort = new JPanel();
jpSort.setLayout(new GridLayout(1, 2));
jpSort.add(jpSort1);
jpSort.add(jpSort2);
JPanel jpTop = new JPanel();
Border searchBorder = BorderFactory.createTitledBorder("Search and Sort");
jpTop.setLayout(new GridLayout(2, 1));
jpTop.setBorder(searchBorder);
jpTop.add(jpSearch);
jpTop.add(jpSort);
jpJobs.setLayout(new GridLayout(9,1));
JPanel jpMaster = new JPanel();
Border mainBorder = BorderFactory.createCompoundBorder();
jpMaster.setBorder(mainBorder);
jpMaster.setLayout(new BorderLayout(5,25));
jpMaster.add(jpTop, BorderLayout.NORTH);
jpMaster.add(jpScroll, BorderLayout.CENTER);
jpMaster.add(jpBtns, BorderLayout.SOUTH);
jpMaster.add(jpJobs, BorderLayout.EAST);
jpMaster.add(new JScrollPane(jTree), BorderLayout.WEST);
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);//Update scrolling text
setLayout(new FlowLayout());
add(jpMaster);
jbtFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Project3.openFile();
} catch (FileNotFoundException ex) {
Logger.getLogger(TextField.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
jrbChar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
jrbParty.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
jrbLoot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
jrbArt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
jtfmsg.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jrbChar.isSelected() == true){
Iterator<Party> partyIterator = cave.getPartyList().iterator();
while (partyIterator.hasNext()){
Party p = partyIterator.next();
Iterator<Creature> creatureIterator = p.getCreatureList().iterator();
while (creatureIterator.hasNext()){
Creature c = creatureIterator.next();
if (c.getName().equals(jtfmsg.getText())){
System.out.println("Found: \n" + c.toString() + "\n\t member of " +
p.toString() + "\n carrying: \n" + c.getTreasureList().toString() + c.getArtifactList());
break;
}
}
}
}
else if (jrbParty.isSelected() == true){
Iterator<Party> partyIterator = cave.getPartyList().iterator();
while (partyIterator.hasNext()){
Party p = partyIterator.next();
if (p.getName().equals(jtfmsg.getText())){
System.out.println("Found: \n" + p.toString() +
", with members: \n" + p.getCreatureList().toString());
break;
}
}
}
else if (jrbLoot.isSelected() == true){
Iterator<Party> partyIterator = cave.getPartyList().iterator();
while (partyIterator.hasNext()){
Party p = partyIterator.next();
Iterator<Creature> creatureIterator = p.getCreatureList().iterator();
while (creatureIterator.hasNext()){
Creature c = creatureIterator.next();
Iterator<Treasure> treasureIterator = c.getTreasureList().iterator();
while (treasureIterator.hasNext()){
Treasure t = treasureIterator.next();
if (t.getType().equals(jtfmsg.getText())){
System.out.println("Found: \n" + t.toString() + ", carried by " +
c.toString() + ", member of \n" + p.toString());
break;
}
}
}
}
}
Iterator<Party> partyIterator = cave.getPartyList().iterator();
while (partyIterator.hasNext()){
Party p = partyIterator.next();
Iterator<Creature> creatureIterator = p.getCreatureList().iterator();
while (creatureIterator.hasNext()){
Creature c = creatureIterator.next();
Iterator<Treasure> treasureIterator = c.getTreasureList().iterator();
while (treasureIterator.hasNext()){
Treasure t = treasureIterator.next();
}
Iterator<Artifact> artifactIterator = c.getArtifactList().iterator();
while (artifactIterator.hasNext()){
Artifact a = artifactIterator.next();
if (a.getType().equals(jtfmsg.getText())){
System.out.println("Found: \n" + a.toString() + ", carried by " +
c.toString() + ", member of " + p.toString());
break;
}
}
}
}
}
});
jbtCave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Iterator<Party> partyIterator = cave.getPartyList().iterator();
System.out.println("----------");
while (partyIterator.hasNext()){
Party p = partyIterator.next();
System.out.println("Party " + p.toString() + " has members: ");
Iterator<Creature> creatureIterator = p.getCreatureList().iterator();
while (creatureIterator.hasNext()){
Creature c = creatureIterator.next();
System.out.println(" " + c.toString());
Iterator<Treasure> treasureIterator = c.getTreasureList().iterator();
while (treasureIterator.hasNext()){
Treasure t = treasureIterator.next();
System.out.println(" " +
" carrying: " + t.toString());
}
Iterator<Artifact> artifactIterator = c.getArtifactList().iterator();
while (artifactIterator.hasNext()){
Artifact a = artifactIterator.next();
System.out.println(" "
+ " carrying: " + a.toString());
}
}
System.out.println("----------");
}
for (Treasure t: cave.getUnfoundTreasure()) {
System.out.println(t + " awaits to be found");
}
}
});
jbtFear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collections.sort(cave.getAllCreatures(), new SortFear());
System.out.println("----------");
System.out.println("Sorted by fear: ");
for (Creature c: cave.getAllCreatures()) {
System.out.println(c);
}
}
});
jbtEmp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collections.sort(cave.getAllCreatures(), new SortEmpathy());
System.out.println("-----------");
System.out.println("Sorted by empathy: ");
for (Creature c: cave.getAllCreatures()) {
System.out.println(c);
}
}
});
jbtCarrCap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collections.sort(cave.getAllCreatures(), new SortCarrCap());
System.out.println("-----------");
System.out.println("Sorted by carrying capacity: ");
for (Creature c: cave.getAllCreatures()) {
System.out.println(c);
}
}
});
jbtTrWeight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collections.sort(cave.getAllTreasures(), new SortWeight());
System.out.println("----------");
System.out.println("Sorted by weight: ");
for (Treasure t: cave.getAllTreasures()) {
System.out.println(t);
}
}
});
jbtTrVal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collections.sort(cave.getAllTreasures(), new SortValue());
System.out.println("----------");
System.out.println("Sorted by value: ");
for (Treasure t: cave.getAllTreasures()) {
System.out.println(t);
}
}
});
jtfmsg.requestFocusInWindow();
}
public static JPanel getPanel(){
return getJpJobs();
}
private void createNodes(DefaultMutableTreeNode top,
Project3 cave) {
DefaultMutableTreeNode party = null;
DefaultMutableTreeNode creature = null;
DefaultMutableTreeNode treasure = null;
DefaultMutableTreeNode artifact = null;
for (Party p: cave.getPartyList()){
top.add(party = new DefaultMutableTreeNode("Party -" + p.toString()));
for (Creature c: p.getCreatureList()){
party.add(creature = new DefaultMutableTreeNode(c.getName() + " - " + c.getType()));
for (Treasure t: c.getTreasureList()){
creature.add(treasure = new DefaultMutableTreeNode(t.getType() + " - " + t.getValue()));
}
for (Artifact a: c.getArtifactList()){
creature.add(artifact= new DefaultMutableTreeNode(a.getName() + " - " + a.getType()));
}
}
}
DefaultMutableTreeNode unfound = new DefaultMutableTreeNode("Unfound");
top.add(unfound);
for (Treasure t: cave.getUnfoundTreasure()){
unfound.add(treasure = new DefaultMutableTreeNode(t.getType() + " - " + t.getValue()));
}
}
public JTextArea getJpTxtArea() {
return jpTxtArea;
}
public void setJpTxtArea(JTextArea jpTxtArea) {
this.jpTxtArea = jpTxtArea;
}
public JScrollPane getJpScroll() {
return jpScroll;
}
public void setJpScroll(JScrollPane jpScroll) {
this.jpScroll = jpScroll;
}
public DefaultCaret getCaret() {
return caret;
}
public void setCaret(DefaultCaret caret) {
this.caret = caret;
}
class Jobs extends JPanel implements Runnable, ActionListener{
private static final long serialVersionUID = 1L;
private boolean goFlag;
private JProgressBar progressBar;
private String jobName;
private long jobTime;
private HashMap <Integer, SorcerersCave> caveMap;
private long delay;
private Creature creature;
public Jobs(Creature c){
this.creature = null;
this.delay = 200L;
this.progressBar = new JProgressBar ();
this.goFlag = false;
this.creature = c;
run();
}
@Override
public void actionPerformed (ActionEvent e) {
if (isGoFlag()) {
setGoFlag(false);
}
else {
setGoFlag(true);
synchronized (this) {
notify ();}
}
}
@Override
public final void run() {
getProgressBar().setString(getCreature().getJobName());
getProgressBar().setStringPainted(true);
JLabel crName = new JLabel(getCreature().getName());
JButton start = new JButton("Start/Stop");
JPanel crJobs = new JPanel();
crJobs.setLayout(new FlowLayout());
crJobs.add(crName);
crJobs.add(getProgressBar());
crJobs.add(start);
start.addActionListener(this);
JPanel panel = GUI.getPanel();
panel.add(crJobs);
int progress = 0;
while (progress != 100) {
try {
progress++;
getProgressBar().setValue(progress);
Thread.sleep(getDelay());
synchronized(this){
while (!isGoFlag()){
wait();
}
}
} catch (InterruptedException e) {}
}
}
public boolean isGoFlag() {
return goFlag;
}
public void setGoFlag(boolean goFlag) {
this.goFlag = goFlag;
}
public JProgressBar getProgressBar() {
return progressBar;
}
public void setProgressBar(JProgressBar progressBar) {
this.progressBar = progressBar;
}
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public long getJobTime() {
return jobTime;
}
public void setJobTime(long jobTime) {
this.jobTime = jobTime;
}
public HashMap <Integer, SorcerersCave> getCaveMap() {
return caveMap;
}
public void setCaveMap(HashMap <Integer, SorcerersCave> caveMap) {
this.caveMap = caveMap;
}
public long getDelay() {
return delay;
}
public void setDelay(long delay) {
this.delay = delay;
}
public Creature getCreature() {
return creature;
}
public void setCreature(Creature creature) {
this.creature = creature;
}
}
public static void main(String[] args) throws FileNotFoundException {
GUI frame = new GUI();
frame.setTitle("Sorcerer's Cave");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}