package miw2;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JTextArea;
/**
* Class for controling programs in prolog. Constructor arguments are buttons used for controling the aplication,
* to close aplication press X on ControlForm.
* @author Filip
*/
public class ControlForm extends javax.swing.JFrame {
/**
* Command wchich was choosed.
*/
private static int command = -1;
/**
* State if any button was pressed.
*/
private static boolean enable = false;
/**
* Singleton variable to avoid duplicated Control Forms.
*/
private static ControlForm form;
/**
* List of commands' buttons.
*/
private static List<JButton> buttonList;
/**
* Instruction how to use ControlForm.
*/
private static JTextArea textA;
/**
* Box for commands' buttons.
*/
public static Box bx;
/**
* Create ControlForm with given instruction and command buttons. Exit button is not used, close ConrtolForm
* by pressing the x.
* @param text instruction.
* @param buttons command buttons.
*/
public ControlForm(String text, String[] buttons) {
if(form == null){
form = new ControlForm();
form.setDefaultCloseOperation(ControlForm.EXIT_ON_CLOSE);
form.setLayout(new BorderLayout());
textA = new JTextArea(text);
form.add(textA,BorderLayout.NORTH);
form.setTitle("CONTROL PANEL");
buttonList = new ArrayList<JButton>();
bx = Box.createHorizontalBox();
for (String str : buttons) {
buttonList.add(new JButton(str));
bx.add(buttonList.get(buttonList.size()-1),BorderLayout.SOUTH);
buttonList.get(buttonList.size()-1).addMouseListener(new MyMouseListener());
}
form.add(bx);
form.pack();
form.setVisible(true);
}
}
/**
* Default ControForm constructor.
*/
public ControlForm() {
}
/**
* Do loop until any command button is pressed.
* @return command.
*/
public static int getValue() {
try {
while (!enable) {
Thread.sleep(100);
}
} catch (Exception ex) {
ex.printStackTrace();
}
enable = false;
return command;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 244, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 204, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ControlForm("Text", new String[]{"one", "two"});
}
});
}
private class MyMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
int i=0;
for(JButton but: buttonList){
if(e.getComponent().equals(but)){
command = i;
enable = true;
break;
}
i++;
}
}
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}
// Variables declaration - do not modify
// End of variables declaration
}