}
public NamedDataSet request(UIRequest... requests){
NamedDataSet reply = new NamedDataSet();
//if only one value is requested, sometimes a dialog box can be used
//(for boolean values, pure output and file paths)
/*++ TODO: problem: help texts ?! ++*/
//single boolean request
if( requests.length == 1 && requests[0].getType() == Request_Boolean ){
int response = JOptionPane.showConfirmDialog(mainFrame,requests[0].label,str("titleWindow"),JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if( response == JOptionPane.YES_OPTION ){
reply.add(requests[0].name,true);
}
else{
reply.add(requests[0].name,false);
}
}
//output-"request"
else if( requests.length == 1 && requests[0].getType() == Request_Output ){
output(requests[0].label);
}
//request for a file path
else if( requests.length == 1 && requests[0].getType() == UIRequest.Type.Request_String
&& ((UIRequest_String)requests[0]).layoutFlags != null
&& ( ((UIRequest_String)requests[0]).layoutFlags.contains(readPath)
|| ((UIRequest_String)requests[0]).layoutFlags.contains(writePath) ) ){
String response = null;
//create a file chooser dialog
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(requests[0].label);
//show the dialog
int returnVal;
if( ((UIRequest_String)requests[0]).layoutFlags.contains(readPath) ){
returnVal = chooser.showOpenDialog(mainFrame);
} else {
returnVal = chooser.showSaveDialog(mainFrame);
}
//get the result
if(returnVal == JFileChooser.APPROVE_OPTION) {
response = chooser.getSelectedFile().getPath();
}
else{ //cancelled
return null;
}
if( response != null ){
reply.add(requests[0].name,response);
}
}
//other requests: full window
else{
//prepare panel and layout-object
JPanel requestPanel = new JPanel();
GridBagLayout gbLayout = new GridBagLayout();
requestPanel.setLayout(gbLayout);
listener = new GUIListener();
//variable for finding out whether this is an output-only request
boolean outputOnly = true;
//create arrays where the GUI's components are stored so they can be accessed later
JLabel guiLabelArray[] = new JLabel[requests.length];
JComponent guiObjectArray[] = new JComponent[requests.length];
//arrange GUI
for( int requestIndex = 0; requestIndex < requests.length; ++ requestIndex ){
UIRequest request = requests[requestIndex];
//modify the outputOnly-variable
if( request.name != null ){ outputOnly = false; }
//place the label (unless type is UIRequest_Boolean or UIRequest_Output)
if( request.getType() != Request_Boolean && request.getType() != Request_Output ){
guiLabelArray[requestIndex] = new JLabel(request.label);
if(showToolTips && request.help != null && !request.help.equals("") ){
guiLabelArray[requestIndex].setToolTipText(request.help);
}
gbLayout.setConstraints(guiLabelArray[requestIndex],new GBC(0,(2*requestIndex),3,SOUTHWEST,NONE,new Insets(5,0,1,0)));
requestPanel.add(guiLabelArray[requestIndex]);
}
//place the other objects for the request
switch( requests[requestIndex].getType() ){
case Request_Output: {
UIRequest_Output outputRequest = (UIRequest_Output)request;
//create a not-editable text area (with line wrap at word boundaries)
//that has the same color as the panel (indicating read-only)
JTextArea outputTextArea = new JTextArea(outputRequest.label);
outputTextArea.setEditable(false);
outputTextArea.setLineWrap(true);
outputTextArea.setWrapStyleWord(true);
if(showToolTips && outputRequest.help != null && !outputRequest.help.equals("") ){
outputTextArea.setToolTipText(outputRequest.help);
}
outputTextArea.setBackground( requestPanel.getBackground() );
//create a scroll pane for the text area and put it on the panel
JScrollPane textScrollPane = new JScrollPane(outputTextArea);
gbLayout.setConstraints(textScrollPane,new GBC(0,(2*requestIndex)+1,3,NORTHWEST,BOTH,new Insets(10,0,3,0)));
requestPanel.add(textScrollPane);
} break;
case Request_Boolean:
UIRequest_Boolean booleanRequest = (UIRequest_Boolean)request;
//create the check box
JCheckBox checkBoxBooleanRequest = new JCheckBox(booleanRequest.label);
if(showToolTips && booleanRequest.help != null && !booleanRequest.help.equals("") ){
checkBoxBooleanRequest.setToolTipText(booleanRequest.help);
}
if( booleanRequest.startingValue != null ){
checkBoxBooleanRequest.setSelected(booleanRequest.startingValue);
}
if( booleanRequest.name == null ){
//setEditable impossible for checkboxes, setEnabled used instead
checkBoxBooleanRequest.setEnabled(false);
}
gbLayout.setConstraints(checkBoxBooleanRequest,new GBC(0,(2*requestIndex)+1,3,NORTHWEST,NONE,new Insets(5,0,0,0)));
requestPanel.add(checkBoxBooleanRequest);
guiObjectArray[requestIndex] = checkBoxBooleanRequest;
break;
case Request_String:
UIRequest_String stringRequest = (UIRequest_String)request;
//create the text field / password field / text area (depending on the flags)
JTextComponent textComponentStringRequest;
if( stringRequest.layoutFlags != null &&
stringRequest.layoutFlags.contains( multipleLines ) ){
textComponentStringRequest = new JTextArea(stringRequest.startingValue);
((JTextArea)textComponentStringRequest).setLineWrap(stringRequest.layoutFlags.contains(lineWrap));
((JTextArea)textComponentStringRequest).setWrapStyleWord(true);
JScrollPane textScrollPane = new JScrollPane(textComponentStringRequest);
gbLayout.setConstraints(textScrollPane,new GBC(0,(2*requestIndex)+1,3,NORTHWEST,BOTH));
requestPanel.add(textScrollPane);
} else if( stringRequest.layoutFlags != null &&
stringRequest.layoutFlags.contains( hideInput ) ){
textComponentStringRequest = new JPasswordField(stringRequest.startingValue);
gbLayout.setConstraints(textComponentStringRequest,new GBC(0,(2*requestIndex)+1,3,NORTHWEST,HORIZONTAL));
requestPanel.add(textComponentStringRequest);
} else{
textComponentStringRequest = new JTextField(stringRequest.startingValue);
gbLayout.setConstraints(textComponentStringRequest,new GBC(0,(2*requestIndex)+1,3,NORTHWEST,HORIZONTAL));
requestPanel.add(textComponentStringRequest);
}
if(showToolTips && stringRequest.help != null && !stringRequest.help.equals("") ){
textComponentStringRequest.setToolTipText(stringRequest.help);
}
if( stringRequest.name == null ){
textComponentStringRequest.setEditable(false);
}
guiObjectArray[requestIndex] = textComponentStringRequest;
break;
case Request_Integer:
UIRequest_Integer integerRequest = (UIRequest_Integer)request;
//create the text field
JTextField textFieldIntegerRequest = new JTextField();
if(showToolTips && integerRequest.help != null && !integerRequest.help.equals("") ){
textFieldIntegerRequest.setToolTipText(integerRequest.help);
}
if( integerRequest.startingValue != null ){
textFieldIntegerRequest.setText(integerRequest.startingValue.toString());
}
if( integerRequest.name == null ){
textFieldIntegerRequest.setEditable(false);
}
gbLayout.setConstraints(textFieldIntegerRequest,new GBC(0,(2*requestIndex)+1,3,NORTHWEST,HORIZONTAL));
requestPanel.add(textFieldIntegerRequest);
guiObjectArray[requestIndex] = textFieldIntegerRequest;
break;
case Request_Float:
UIRequest_Float floatRequest = (UIRequest_Float)request;
//create the text field
JTextField textFieldFloatRequest = new JTextField();
if( showToolTips && floatRequest.help != null && !floatRequest.help.equals("") ){
textFieldFloatRequest.setToolTipText(floatRequest.help);
}
if( floatRequest.startingValue != null ){
textFieldFloatRequest.setText(floatRequest.startingValue.toString());
}
if( floatRequest.name == null ){
textFieldFloatRequest.setEditable(false);
}
requestPanel.add(textFieldFloatRequest);
gbLayout.setConstraints(textFieldFloatRequest,new GBC(0,(2*requestIndex)+1,3,NORTHWEST,HORIZONTAL));
guiObjectArray[requestIndex] = textFieldFloatRequest;
break;
case Request_Selection:
UIRequest_Selection selectionRequest = (UIRequest_Selection)request;
//create the combo box
JComboBox comboBoxSelectionRequest = new JComboBox();
if( showToolTips && selectionRequest.help != null && !selectionRequest.help.equals("") ){
comboBoxSelectionRequest.setToolTipText(selectionRequest.help);
}
for( String selectionName : selectionRequest.selectionNames ){
comboBoxSelectionRequest.addItem(selectionName);
}
if( selectionRequest.startingValue != null
&& selectionRequest.startingValue >= 0 && selectionRequest.startingValue < selectionRequest.selectionNames.length ){
comboBoxSelectionRequest.setSelectedIndex(selectionRequest.startingValue);
} else {
comboBoxSelectionRequest.setSelectedIndex(-1);
}
if( selectionRequest.name == null ){
comboBoxSelectionRequest.setEditable(false);
}
gbLayout.setConstraints(comboBoxSelectionRequest,new GBC(0,(2*requestIndex)+1,3,NORTHWEST,HORIZONTAL));
requestPanel.add(comboBoxSelectionRequest);
guiObjectArray[requestIndex] = comboBoxSelectionRequest;
break;
}
}
//cancel-Button (only if at least one component requiring input exists)
//it will always be created because it's used for size calculation of the OK button.
JButton cancelButton = new JButton(str("buttonCancel"));
cancelButton.setActionCommand(ACTION_CANCEL);
if( !outputOnly ){
cancelButton.addActionListener(listener);
gbLayout.setConstraints(cancelButton,new GBC(1,(2*requests.length),1,SOUTHEAST,NONE,1,0.01,new Insets(3,0,0,5)));
requestPanel.add(cancelButton);
}
//ok-Button
JButton okButton = new JButton(str("buttonOK"));
okButton.setActionCommand(ACTION_OK);
okButton.addActionListener(listener);
okButton.setPreferredSize( cancelButton.getPreferredSize() ); //same size as Cancel-button
gbLayout.setConstraints(okButton,new GBC(2,(2*requests.length),1,SOUTHEAST,NONE,0,0.01,new Insets(3,0,0,0)));
requestPanel.add(okButton);
//put the request panel into the main frame
setMainFrameContent(requestPanel);
//wait for valid user replies
//if Cancel is pressed, the method is left immediately, returning null !!!
boolean validRepliesEntered = false;
while( !validRepliesEntered ){
//wait for OK or Cancel being pressed
boolean buttonOKPressed = false;
while( !buttonOKPressed ){
try{
Thread.sleep(100);
}
catch(InterruptedException ie){}
if( listener.hasNewActionCommand() ){
String actionCommand = listener.getNextActionCommand();
if( actionCommand.equals(ACTION_OK) ){
buttonOKPressed = true;
}
else if( actionCommand.equals(ACTION_CANCEL) ){
return null; // #### !! return-point !! ####
}
}
}
//get replies
validRepliesEntered = true;
for( int requestIndex = 0; requestIndex < requests.length && validRepliesEntered == true; ++ requestIndex ){
if( requests[requestIndex].name != null ){
switch( requests[requestIndex].getType() ){
case Request_Boolean:
reply.add(requests[requestIndex].name,((JCheckBox)guiObjectArray[requestIndex]).isSelected());
break;
case Request_String:
reply.add(requests[requestIndex].name,((JTextComponent)guiObjectArray[requestIndex]).getText());
break;
case Request_Integer:
String integerAsString = ((JTextField)guiObjectArray[requestIndex]).getText();
try{
int integerValue = Integer.parseInt(integerAsString);
if( integerValue < ((UIRequest_Integer)requests[requestIndex]).range_min
|| integerValue > ((UIRequest_Integer)requests[requestIndex]).range_max ){
output(str("errorRange", integerAsString, ((UIRequest_Integer)requests[requestIndex]).range_min, ((UIRequest_Integer)requests[requestIndex]).range_max));
validRepliesEntered = false;
}
reply.add(requests[requestIndex].name,integerValue);
}
catch( NumberFormatException nfe ){
output(str("errorNoValidInteger", integerAsString));
validRepliesEntered = false;
}
break;
case Request_Float:
String floatAsString = ((JTextField)guiObjectArray[requestIndex]).getText();
try{
float floatValue = Float.parseFloat(floatAsString);
if( floatValue < ((UIRequest_Float)requests[requestIndex]).range_min
|| floatValue > ((UIRequest_Float)requests[requestIndex]).range_max ){
output(str("errorRange", floatAsString, ((UIRequest_Float)requests[requestIndex]).range_min, ((UIRequest_Float)requests[requestIndex]).range_max));
validRepliesEntered = false;
}
reply.add(requests[requestIndex].name,floatValue);
}
catch( NumberFormatException nfe ){
output(str("errorNoValidFloat", floatAsString));
validRepliesEntered = false;
}
break;
case Request_Selection:
int selectedIndex = ((JComboBox)guiObjectArray[requestIndex]).getSelectedIndex();
if( selectedIndex < 0 || selectedIndex > ((UIRequest_Selection)requests[requestIndex]).selectionNames.length ){
output(str("errorNoValidOption"));
validRepliesEntered = false;
}
reply.add(requests[requestIndex].name,selectedIndex);
break;
}