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