View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.panels.results;
17  
18  import java.util.List;
19  
20  import javax.swing.JPanel;
21  import javax.swing.JScrollPane;
22  import javax.swing.JTree;
23  import javax.swing.tree.DefaultTreeModel;
24  import javax.swing.tree.MutableTreeNode;
25  
26  import p3j.database.DatabaseFactory;
27  import p3j.experiment.results.ResultsOfTrial;
28  import p3j.gui.panels.projections.ProjectionTreeCellRenderer;
29  import p3j.gui.panels.projections.ProjectionTreePanel;
30  import p3j.gui.panels.projections.ProjectionTreeSelectionListener;
31  import p3j.pppm.ProjectionModel;
32  
33  /**
34   * Panel that displays and controls the result tree.
35   * 
36   * Created: August 23, 2008
37   * 
38   * @author Christina Bohk
39   * @author Roland Ewald
40   */
41  public class ResultTreePanel extends ProjectionTreePanel {
42  
43  	/** Serialization ID. */
44  	private static final long serialVersionUID = 3722491328298367310L;
45  
46  	/** The root of the results tree. */
47  	private ResultTreeRoot root;
48  
49  	/**
50  	 * Default constructor.
51  	 * 
52  	 * @param proj
53  	 *          the projection which has results to be displayed
54  	 * @param content
55  	 *          the content
56  	 */
57  	public ResultTreePanel(ProjectionModel proj, JPanel content) {
58  		super(proj, content);
59  	}
60  
61  	@Override
62  	protected void initTree() {
63  		root = new ResultTreeRoot(getProjectionModel());
64  		setTreeModel(new DefaultTreeModel(root));
65  		setTree(new JTree(getTreeModel()));
66  		getTree().setCellRenderer(new ProjectionTreeCellRenderer());
67  		setScrollPane(new JScrollPane(getTree()));
68  
69  		totalRefresh();
70  
71  		getTree().addTreeSelectionListener(
72  		    new ProjectionTreeSelectionListener(getContentPanel(), this));
73  	}
74  
75  	/**
76  	 * Refreshes results tree.
77  	 */
78  	@Override
79  	public void totalRefresh() {
80  
81  		// Remove old children from root
82  		for (int i = root.getChildCount(); i > 0; i--) {
83  			getTreeModel().removeNodeFromParent(
84  			    (MutableTreeNode) root.getChildAt(i - 1));
85  		}
86  
87  		// Add new children
88  		List<ResultsOfTrial> results = DatabaseFactory.getDatabaseSingleton()
89  		    .getAllResults(getProjectionModel());
90  		for (int i = 0; i < results.size(); i++) {
91  			root.add(new ResultTreeNode(results.get(i), i + 1));
92  		}
93  
94  		getTreeModel().nodeStructureChanged(root);
95  		getTree().repaint();
96  	}
97  
98  }