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.awt.event.ActionEvent;
19  import java.awt.event.ActionListener;
20  import java.io.File;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.swing.JButton;
25  import javax.swing.JFileChooser;
26  import javax.swing.JPanel;
27  import javax.swing.tree.TreePath;
28  
29  import p3j.database.DatabaseFactory;
30  import p3j.experiment.results.ResultExport;
31  import p3j.experiment.results.ResultsOfTrial;
32  import p3j.gui.P3J;
33  import p3j.gui.dialogs.ConfigureResultFilterDialog;
34  import p3j.gui.misc.SubNodeSummary;
35  import p3j.gui.panels.PropertiesShowPanelFactory;
36  import p3j.gui.panels.projections.IProjectionTree;
37  import p3j.gui.panels.projections.ProjectionTreeNode;
38  import p3j.misc.gui.GUI;
39  import p3j.pppm.ProjectionModel;
40  
41  /**
42   * Represents the root of the results tree.
43   * 
44   * @author Christina Bohk
45   * @author Roland Ewald
46   * 
47   */
48  public class ResultTreeRoot extends ProjectionTreeNode<ProjectionModel> {
49  
50  	/** The key width (left column) of the panel. */
51  	private static final int PANEL_KEY_WIDTH = 50;
52  
53  	/** Serialization ID. */
54  	private static final long serialVersionUID = -425870180408032392L;
55  
56  	/**
57  	 * Instantiates a new result tree root.
58  	 * 
59  	 * @param projectionModel
60  	 *          the projection model
61  	 */
62  	public ResultTreeRoot(ProjectionModel projectionModel) {
63  		super(projectionModel, "Overall Results");
64  	}
65  
66  	@Override
67  	public JPanel selected(TreePath selectionPath, final IProjectionTree projTree) {
68  
69  		JButton generateReport = new JButton("<html><b>Generate Report</b></html>");
70  		generateReport.addActionListener(new ActionListener() {
71  			@Override
72  			public void actionPerformed(ActionEvent e) {
73  				JFileChooser fileChooser = GUI
74  				    .getDirectoryChooser("Determine in which directory to store the report");
75  				if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
76  					ResultExport resultExport = configureResultExport(getEntity(),
77  					    fileChooser.getSelectedFile());
78  					if (resultExport == null) {
79  						return;
80  					}
81  					try {
82  						resultExport.createResultReport();
83  					} catch (Exception ex) {
84  						GUI.printErrorMessage("Report Generation Failed", ex);
85  					}
86  				}
87  			}
88  		});
89  
90  		JButton exportAggregatedData = new JButton("Aggregate");
91  		exportAggregatedData.addActionListener(new ActionListener() {
92  			@Override
93  			public void actionPerformed(ActionEvent e) {
94  				JFileChooser fileChooser = GUI
95  				    .getDirectoryChooser("Select directory for aggregated data");
96  				if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
97  					ResultExport resultExport = configureResultExport(getEntity(),
98  					    fileChooser.getSelectedFile());
99  					// TODO: Add dialog for aggregated data
100 					if (resultExport == null) {
101 						return;
102 					}
103 					try {
104 						resultExport.exportAggregatedResults();
105 					} catch (Exception ex) {
106 						GUI.printErrorMessage("Aggregated Data Export Failed", ex);
107 					}
108 				}
109 			}
110 		});
111 
112 		JButton exportData = new JButton("Export");
113 		exportData.addActionListener(new ActionListener() {
114 			@Override
115 			public void actionPerformed(ActionEvent e) {
116 				JFileChooser fileChooser = GUI
117 				    .getDirectoryChooser("Select directory for export");
118 				if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
119 					ResultExport resultExport = configureResultExport(getEntity(),
120 					    fileChooser.getSelectedFile());
121 					if (resultExport == null) {
122 						return;
123 					}
124 					try {
125 						resultExport.exportAllResults();
126 					} catch (Exception ex) {
127 						GUI.printErrorMessage("Data Export Failed", ex);
128 					}
129 				}
130 			}
131 		});
132 
133 		JButton clearResults = new JButton("Clear Results");
134 		clearResults.addActionListener(new ActionListener() {
135 			@Override
136 			public void actionPerformed(ActionEvent e) {
137 				if (GUI.printQuestion(P3J.getInstance(), "Really delete all results?",
138 				    "Approving this will delete ALL results associated with this projection.")) {
139 					try {
140 						DatabaseFactory.getDatabaseSingleton()
141 						    .deleteAllResults(getEntity());
142 					} catch (Exception ex) {
143 						GUI.printErrorMessage("Result Deletion Failed", ex);
144 					}
145 				}
146 				P3J.getInstance().refreshNavigationTree();
147 			}
148 		});
149 
150 		List<JButton> buttons = new ArrayList<JButton>();
151 		buttons.add(clearResults);
152 		buttons.add(exportData);
153 		buttons.add(exportAggregatedData);
154 		buttons.add(generateReport);
155 
156 		PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
157 		    PANEL_KEY_WIDTH, buttons, 0);
158 		pspf.sep("General Information");
159 		pspf.app("Projection:", getEntity().getName());
160 		pspf.app("#Trials:", getChildCount());
161 		pspf.appPreview(new SubNodeSummary<ResultsOfTrial>(this, projTree,
162 		    ResultsOfTrial.class));
163 		return pspf.constructPanel();
164 	}
165 
166 	/**
167 	 * Configures result export.
168 	 * 
169 	 * @param projection
170 	 *          the projection
171 	 * @param targetDir
172 	 *          the target directory
173 	 * 
174 	 * @return the result export
175 	 */
176 	protected ResultExport configureResultExport(ProjectionModel projection,
177 	    File targetDir) {
178 		ConfigureResultFilterDialog resultFilterDialog = new ConfigureResultFilterDialog(
179 		    null, getEntity());
180 		resultFilterDialog.setVisible(true);
181 		return resultFilterDialog.isCancelled() ? null : new ResultExport(
182 		    projection, targetDir, resultFilterDialog.getConfiguredResultFilter());
183 	}
184 
185 	@Override
186 	public void deselected() {
187 	}
188 
189 }