package views;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import misc.CustomTreeNode;
import misc.ResultTreeNode;
import model.Model;
import application.Application;
import javax.swing.JScrollPane;
public class ResultPanel extends JPanel implements PropertyChangeListener {
// Reference to the Model
private Model model;
private JTree jResultTree = null;
private DefaultTreeCellRenderer renderer = null;
private JScrollPane jScrollPane = null;
public ResultPanel(Model model) {
super();
this.model = model;
model.addPropertyChangeListener(this);
initialize();
}
/**
* This method initializes this
*
*/
private void initialize() {
// Graphic settings
renderer = new DefaultTreeCellRenderer();
renderer.setLeafIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/match.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_RESULT")));
this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jScrollPane.setViewportView(getJResultTree());
}
return jScrollPane;
}
/**
* This method initializes jResultTree
*
* @return javax.swing.JTree
*/
private JTree getJResultTree() {
if (jResultTree == null) {
jResultTree = new JTree((ResultTreeNode)new ResultTreeNode("Results", "", 0, 0));
jResultTree.setCellRenderer(renderer);
}
return jResultTree;
}
/**
* This method initializes jResultTree with a ResultTreeNode
*
* @return javax.swing.JTree
*/
private JTree getJResultTree(ResultTreeNode resultTree) {
jResultTree = new JTree(resultTree){
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();
}
};
ToolTipManager.sharedInstance().registerComponent(jResultTree);
jResultTree.setCellRenderer(renderer);
jResultTree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int selRow = jResultTree.getRowForLocation(e.getX(), e.getY());
TreePath selPath = jResultTree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
try {
model.setSelectedMatch((ResultTreeNode)(selPath.getLastPathComponent()));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
return jResultTree;
}
public void propertyChange(PropertyChangeEvent arg0) {
if (arg0.getPropertyName() == "resultTree") {
this.jScrollPane.setViewportView(getJResultTree((ResultTreeNode)arg0.getNewValue()));
this.validate();
}
}
}