package periman;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class ExpQstnrRowScale implements ExpQstnrRow , ActionListener {
private String leftLabel;
private String rightLabel;
private String header;
private int widthScale;
private boolean mandatory;
private final String NO_RADIO_BUTTON_SELECTED = "none";
String selectedResult = NO_RADIO_BUTTON_SELECTED;
DisableNextListener nextListener=null;
public ExpQstnrRowScale(Element rowElement, DisableNextListener listenerToInfluenceNext) {
nextListener = listenerToInfluenceNext;
nextListener.addInfluencer( this );
leftLabel = rowElement.getAttribute( "leftlabel" );
rightLabel = rowElement.getAttribute( "rightlabel" );
header = rowElement.getAttribute( "header" );
String manStr = rowElement.getAttribute( "mandatory" );
if(manStr.equals("true") == true )
mandatory = true;
else mandatory = false;
widthScale = Integer.parseInt( rowElement.getAttribute( "width" ) );
}
@Override
public Element resultToDom(Document dom) {
String hash = Integer.toString(header.hashCode(), 16);
try{
Element newRowElement = dom.createElement( "scale_"+hash );
newRowElement.setAttribute( "value", selectedResult );
return newRowElement;
}
catch(DOMException de)
{
JOptionPane.showMessageDialog(null, "Could not write scale row result.\n"+ de.getMessage());
}
return null;
}
@Override
public JPanel toPanel() {
GridBagLayout layout = new GridBagLayout();
JPanel panel = new JPanel();
panel.setLayout( layout );
// ***** scale header
GridBagConstraints headerConstraints = new GridBagConstraints();
headerConstraints.gridx = 0;
headerConstraints.gridy = 0;
headerConstraints.anchor = GridBagConstraints.CENTER;
headerConstraints.gridwidth = GridBagConstraints.REMAINDER;
headerConstraints.fill = GridBagConstraints.HORIZONTAL;
JLabel headerLbl = new JLabel( header );
panel.add( headerLbl, headerConstraints );
// ***** second line first element
GridBagConstraints leftConstraint = new GridBagConstraints();
leftConstraint.gridx = 0;
leftConstraint.gridy = 1;
JLabel leftJLbl = new JLabel( leftLabel );
panel.add(leftJLbl, leftConstraint);
// **** second line second element - the panel with all the radio buttons and the labels on the sides.
GridBagConstraints cbPanelConstraint = new GridBagConstraints();
cbPanelConstraint.gridx = 1;
cbPanelConstraint.gridy = 1;
JPanel cbPanel = new JPanel();
panel.add( cbPanel, cbPanelConstraint );
BoxLayout cbPanelLayout = new BoxLayout( cbPanel, BoxLayout.X_AXIS );
cbPanel.setLayout( cbPanelLayout );
ButtonGroup group = new ButtonGroup();
for(int i = 0 ; i<widthScale; i++)
{
JRadioButton radioBtn = new JRadioButton();
group.add( radioBtn );
radioBtn.setActionCommand( ""+(i+1) );
radioBtn.addActionListener( this );
cbPanel.add( radioBtn );
}
// ***** second line third element - the right label
GridBagConstraints rightConstraint = new GridBagConstraints();
rightConstraint.gridx = 2;
rightConstraint.gridy = 1;
JLabel rightJlbl = new JLabel( rightLabel );
panel.add( rightJlbl, rightConstraint );
return panel;
}
@Override
public boolean shouldNextBeEnabled() {
if(mandatory == false)
return true;
if(selectedResult.equals(NO_RADIO_BUTTON_SELECTED) == true )
return false;
return true;
}
@Override
public void actionPerformed(ActionEvent ae) {
selectedResult = ae.getActionCommand();
if(nextListener != null)
nextListener.changeDetected();
}
}