package views;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import misc.CreationTreeNode;
import misc.CreationTreeXML;
import misc.CustomTreeNode;
import model.Model;
import application.Application;
public class CreationPanel extends JPanel implements ICreationPanel {
private JScrollPane jScrollPane = null;
private JTree jCreationTree = null;
private DefaultTreeCellRenderer renderer = null;
private JPanel jOptionPanel = null;
private Model model = null;
public CreationPanel(Model model) {
super();
this.model = model;
initialize();
}
/**
* This method initializes this
*
*/
private void initialize() {
// Graphic settings
renderer = new DefaultTreeCellRenderer();
renderer.setLeafIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/creation.png")));
renderer.setClosedIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/folder_blue.png")));
renderer.setOpenIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/folder_blue_open.png")));
this.setLayout(new BorderLayout());
this.setSize(new java.awt.Dimension(300,550));
this.setBorder(BorderFactory.createTitledBorder(Application.messages.getString("TITLE_CREATION")));
this.add(getJScrollPane(), java.awt.BorderLayout.NORTH);
this.add(getJPanelOption(), java.awt.BorderLayout.CENTER);
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJCreationTree());
jScrollPane.setMaximumSize(new Dimension(500,500));
}
return jScrollPane;
}
/**
* This method initializes jCreationTree
*
* @return javax.swing.JTree
*/
private JTree getJCreationTree() {
if (jCreationTree == null) {
CreationTreeXML facility = new CreationTreeXML();
jCreationTree = new JTree(facility.getTreeFromXml()){
public String getToolTipText(MouseEvent evt) {
if (getRowForLocation(evt.getX(), evt.getY()) == -1)
return null;
TreePath curPath = getPathForLocation(evt.getX(), evt.getY());
return ((CustomTreeNode) curPath.getLastPathComponent()).getToolTipText();
}
};
jCreationTree.setCellRenderer(renderer);
jCreationTree.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
if(e.getClickCount() == 2) {
jtreeDoubleClicked(jCreationTree,e);
}
else
{
jtreeClicked(jCreationTree,e);
}
//System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
}
});
ToolTipManager.sharedInstance().registerComponent(jCreationTree);
}
return jCreationTree;
}
private void jtreeDoubleClicked(JTree tree,java.awt.event.MouseEvent evt)
{
CreationTreeNode node;
node = jtreeClicked(tree,evt);
if(node==null)
return;
node.getPanel().launch();
}
//Occurs when the user click in the tree
private CreationTreeNode jtreeClicked(JTree tree,java.awt.event.MouseEvent evt)
{
TreePath curPath = tree.getPathForLocation(evt.getX(), evt.getY());
if(curPath == null)
return null; //the user didn't click a node, so we just return
Object objectClicked = curPath.getLastPathComponent();
if(objectClicked.getClass() != CreationTreeNode.class)
return null; //the user didn't click a CreationTreeNode (an item, the leaf)
//now we are sure the user click an item, we may draw the panel and register for validation
CreationTreeNode node = (CreationTreeNode)curPath.getLastPathComponent();
node.getPanel().register(this);
this.getJPanelOption().removeAll();
this.getJPanelOption().add(node.getPanel(),true);
this.validate();
this.repaint();
node.getPanel().repaint();
return node;
}
private JPanel getJPanelOption() {
if (jOptionPanel == null) {
jOptionPanel = new JPanel();
jOptionPanel.setLayout(new FlowLayout());
jOptionPanel.setBorder(BorderFactory.createTitledBorder(Application.messages.getString("TITLE_PARAMETERS")));
}
return jOptionPanel;
}
//Occurs when the user validate the panel of an item
public void onValidatePanel(String regexItem) {
this.model.addRegExItem(regexItem);
}
}