ParametersDialog.java 6.12 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
package jcircus.gui;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import jcircus.JCircusController;
import jcircus.JCircusControllerFrame;
import jcircus.exceptions.FailParsingException;
import jcircus.exceptions.FailTypeCheckingException;
import jcircus.util.ProcInfo;
import net.sourceforge.czt.z.ast.Expr;
import net.sourceforge.czt.z.ast.RefExpr;
import net.sourceforge.czt.z.ast.VarDecl;

/**
* ParametersDialog.java
*
* Dialog where the user enters the parameters for a parametrised process.
*
* @author Angela
*/
public class ParametersDialog extends JDialog implements ActionListener {

private JCircusController/*Frame*/ _controller;
private ProcInfo _procInfo;
// Interface components
private JTextField[] _inputFields;
private JButton _btnOk;
private JButton _btnCancel;

/**
* Constructor.
*/
public ParametersDialog(JCircusFrame jCircusFrame,
JCircusController/*Frame*/ controller, ProcInfo procInfo) {
super(jCircusFrame, "Parameters", true);

this._controller = controller;
this._procInfo = procInfo;

// User can not close by clicking on "X"
this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
this.getContentPane().setLayout(
new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));

String[] text = new String[] {
"Process " + procInfo.getProcessName() + " requires parameters.",
"Please enter their values."
};
for (int i = 0; i < text.length; i++) {
this.getContentPane().add(new JLabel(text[i]));
}
List<VarDecl> params = procInfo.getParameters();
JPanel pnlParams = new JPanel();
pnlParams.setLayout(new GridLayout((params.size() + 1), 3));
pnlParams.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

pnlParams.add(new JLabel("Parameter"));
pnlParams.add(new JLabel("Type"));
pnlParams.add(new JLabel("Value"));

_inputFields = new JTextField[params.size()];
for (int i = 0; i < params.size(); i++) {
// VarDecl declares at most one variable. Gets this variable.

//N�o tenho ctz se � assim
String param = params.get(i).getName()/*getDeclName()*/.get(0).toString();
// Gets the expression in the VarDecl.
Expr expr = params.get(i).getExpr();
// The type expression should be a name, that is, a reference to
// a Free Type or to \nat or \num or \arithmos. In the future,
// other types will be considered as well.
assert(expr instanceof RefExpr): "expr is " + expr.getClass();
//N�o tenho ctz se � assim
String type = (String) ((RefExpr) expr).getName()/*getRefName()*/.toString();

pnlParams.add(new JLabel(param));
pnlParams.add(new JLabel(type));
_inputFields[i] = new JTextField();
pnlParams.add(_inputFields[i]);
}
this.getContentPane().add(pnlParams);
JPanel pnlButtons = new JPanel();
_btnOk = new JButton("OK");
_btnOk.setSize(20, 10);
_btnOk.addActionListener(this);
pnlButtons.add(_btnOk);

_btnCancel = new JButton("Cancel");
_btnCancel.setSize(20, 10);
_btnCancel.addActionListener(this);
pnlButtons.add(_btnCancel);
this.getContentPane().add(pnlButtons);
}
/**
* This is invoked when a button is pressed.
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == _btnOk) {
btnOkPressed();
} else if (e.getSource() == _btnCancel) {
btnCancelPressed();
}
}
/**
* This is invoked when the button OK is pressed.
*
*/
private void btnOkPressed() {
// Tests if all the parameters have been entered by the user
for (int i = 0; i < _inputFields.length; i++) {
if (_inputFields[i].getText().equals("")) {
JOptionPane.showMessageDialog(this,
"You must enter all the parameters.");
return;
}
}
for (int i = 0; i < _inputFields.length; i++) {
// Parameter entered by the user
String paramEntered = _inputFields[i].getText().trim();
// Expression representing the type of the respective parameter
Expr type = _procInfo.getTypeOfParameter(i);

try {
// "Parse" and "typecheck" the value entered by the user
String codeForParam = _controller.codeForParameter(
paramEntered, type);
// Add the code to the ProcInfo
_procInfo.addCodeForActuals(i, codeForParam);

} catch(FailParsingException fpe) {
JOptionPane.showMessageDialog(this, fpe.getFirstError(),
"Parsing Error", JOptionPane.ERROR_MESSAGE);
_inputFields[i].selectAll();

} catch(FailTypeCheckingException ftce) {
JOptionPane.showMessageDialog(this, ftce.getFirstError(),
"Type Checking Error", JOptionPane.ERROR_MESSAGE);
_inputFields[i].selectAll();
}
}

// Finishes and closes the window
((JCircusFrame) this.getOwner()).setCanceled(false);
this.setVisible(false);
this.dispose();
}
/**
* This is invoked when the button Cancel is pressed.
*
*/
private void btnCancelPressed() {

// Finishes and closes the window
((JCircusFrame) this.getOwner()).setCanceled(true);
this.setVisible(false);
this.dispose();
}
}