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.dboverview;
17  
18  import java.awt.event.ActionEvent;
19  import java.awt.event.ActionListener;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.swing.JButton;
24  import javax.swing.JPanel;
25  import javax.swing.SwingWorker;
26  import javax.swing.tree.TreePath;
27  
28  import p3j.database.DatabaseFactory;
29  import p3j.gui.P3J;
30  import p3j.gui.dialogs.execstatus.SimpleProgressDialog;
31  import p3j.gui.panels.PropertiesShowPanelFactory;
32  import p3j.gui.panels.projections.IProjectionTree;
33  import p3j.gui.panels.projections.ProjectionTreeNode;
34  import p3j.misc.IProgressObserver;
35  import p3j.misc.Misc;
36  import p3j.misc.gui.GUI;
37  import p3j.pppm.ProjectionModel;
38  
39  /**
40   * This node delivers an overview of a projections and options to remove or load
41   * it.
42   * 
43   * @author Christina Bohk
44   * @author Roland Ewald
45   * 
46   */
47  public class OverviewProjectionNode extends ProjectionTreeNode<ProjectionModel> {
48  
49    /** Serialization ID. */
50    private static final long serialVersionUID = -7668884101075657298L;
51  
52    /**
53     * Instantiates a new overview projection node.
54     * 
55     * @param projectionModel
56     *          the projection model
57     * @param name
58     *          the name of the node
59     */
60    public OverviewProjectionNode(ProjectionModel projectionModel, String name) {
61      super(projectionModel, name);
62    }
63  
64    @Override
65    public JPanel selected(TreePath selectionPath, final IProjectionTree projTree) {
66  
67      JButton loadProjection = new JButton("<html><b>Load</b></html>");
68      loadProjection.addActionListener(new ActionListener() {
69        @Override
70        public void actionPerformed(ActionEvent e) {
71          P3J.getInstance().setCurrentProjection(getEntity());
72        }
73      });
74  
75      JButton removeProjection = new JButton("Remove");
76      removeProjection.addActionListener(new ActionListener() {
77        @Override
78        public void actionPerformed(ActionEvent e) {
79          (new SwingWorker<Void, Void>() {
80            @Override
81            protected Void doInBackground() {
82              try {
83                if (GUI.printQuestion(P3J.getInstance(), "Are you sure?",
84                    "Do you really want to delete the projection'"
85                        + getEntity().getName() + "'? This cannot be undone!")) {
86                  IProgressObserver progress = SimpleProgressDialog.showDialog(
87                      P3J.getInstance(), "Deleting projection and its results",
88                      "", 2, true);
89                  DatabaseFactory.getDatabaseSingleton().deleteProjection(
90                      getEntity(), progress);
91                  progress.incrementProgress("Updating user interface...");
92                  P3J.getInstance().projectionDeleted(getEntity());
93                  progress.taskFinished();
94                }
95              } catch (Exception ex) {
96                GUI.printErrorMessage("Projection Deletion Failed", ex);
97              }
98              return null;
99            }
100         }).execute();
101       }
102     });
103 
104     List<JButton> buttons = new ArrayList<JButton>();
105     buttons.add(removeProjection);
106     buttons.add(loadProjection);
107 
108     PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(buttons, 1);
109     pspf.sep("General Information");
110     pspf.app(Misc.GUI_LABEL_NAME, getEntity().getName());
111     pspf.app(Misc.GUI_LABEL_DESCRIPTION, getEntity().getDescription());
112     pspf.app(Misc.GUI_LABEL_JUMP_OFF_YEAR,
113         Integer.toString(getEntity().getJumpOffYear()));
114     pspf.app("Settypes:", getEntity().getAllSetTypes().size());
115     pspf.app("Descendant Generations:", getEntity().getGenerations() - 1);
116     pspf.app("Years to be predicted:", getEntity().getYears());
117     pspf.app("Number of age classes:", getEntity().getNumberOfAgeClasses());
118     return pspf.constructPanel();
119   }
120 
121 }