package controls;
import java.awt.FlowLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import application.Application;
import misc.PatternItem;
import views.ICreationPanel;
//used for dynamic creation of the creationPanel view from the library.xml file
public class ParametersPanel extends JPanel implements PropertyChangeListener{
private JButton jButton = null;
private ICreationPanel creationPanel = null;
private JLabel jLabel = null;
private JPanel jPanel = null;
private Hashtable<String,IParametersComponent> componentsList = null;
private Vector<PatternItem> pattern;
//key is a parameter name, value is its description
private Hashtable<String,String> parametersDescription;
/**
* This is the default constructor
*/
public ParametersPanel(String description) {
super();
initialize(description);
componentsList = new Hashtable<String,IParametersComponent>();
pattern = new Vector<PatternItem>();
parametersDescription =new Hashtable<String,String>();
}
public void register(ICreationPanel creationPanel)
{
this.creationPanel = creationPanel;
}
public void unRegister()
{
this.creationPanel = null;
}
/**
* This method initializes this
*
* @return void
*/
private void initialize(String description) {
jLabel = new JLabel();
jLabel.setText("<html>" + description +"<br><hr></html>" );
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setSize(300, 200);
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new FlowLayout());
labelPanel.add(jLabel);
this.add(labelPanel);
jPanel = new JPanel();
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
this.add(jPanel,null);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(getJButton());
this.add(buttonPanel,null);
this.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent e) {
System.out.println("KEy typed");
if(e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER)
{
System.out.println("KEy typed ENTER");
onValidate();
}
}
});
}
public void launch()
{
// if(componentsList.size() == 0)
onValidate();
// else
// {
// JOptionPane.showMessageDialog(null,
// Application.messages.getString("CREATION_PANEL_CHOOSE_PARAMETER_FIRST_TEXT"),
// Application.messages.getString("CREATION_PANEL_CHOOSE_PARAMETER_FIRST_TITLE"),
// JOptionPane.INFORMATION_MESSAGE);
// }
}
public void addParameter(String type, String name,String description)
{
javax.swing.JComponent newComp = null;
//first create the type spcific component
if(type.compareToIgnoreCase("int") ==0)
newComp = new ParametersComponentInt();
if(type.compareToIgnoreCase("string") ==0)
newComp = new ParametersComponentString();
if (newComp == null)
return;
JLabel newLabel = new JLabel();
newLabel.setText(description);
//this.add(newLabel, null);
JPanel flowPanel = new JPanel();
flowPanel.setLayout(new FlowLayout());
flowPanel.add(newLabel, null);
flowPanel.add(newComp,null);
jPanel.add(flowPanel,null);
//this.add(newComp, null);
((IParametersComponent)newComp).addPropertyChangeListener(this);
componentsList.put(name,(IParametersComponent)newComp);
parametersDescription.put(name,description);
}
public void addPatternItem(PatternItem.Type type,String value)
{
PatternItem itemToAdd = new PatternItem(type,value);
pattern.add(itemToAdd);
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText(Application.messages.getString("CREATION_PANEL_VALIDATE"));
jButton.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
onValidate();
}
});
}
return jButton;
}
public void onValidate()
{
if(this.creationPanel != null)
{
//First create the regex item
String regExItem ="";
for(int It=0;It<pattern.size();It++)
{
PatternItem item = pattern.get(It);
if(item.getType() == PatternItem.Type.LITTERAL)
regExItem += item.getValue();
if(item.getType() == PatternItem.Type.REFERENCE)
{
IParametersComponent component = componentsList.get(item.getValue());
if(component.isValid() == false)
{
JOptionPane.showMessageDialog(null,
Application.messages.getString("CREATION_PANEL_CHOOSE_PARAMETER_FIRST_TEXT") + "\n" + parametersDescription.get(item.getValue()),
Application.messages.getString("CREATION_PANEL_CHOOSE_PARAMETER_FIRST_TITLE"),
JOptionPane.INFORMATION_MESSAGE);
return;
}
regExItem += component.getValue();
}
}
this.creationPanel.onValidatePanel(regExItem);
}
}
public void propertyChange(PropertyChangeEvent arg0) {
if(arg0.getPropertyName() == "validate")
onValidate();
}
}