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.projections;
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.JTextArea;
26  import javax.swing.JTextField;
27  import javax.swing.tree.TreePath;
28  
29  import p3j.database.DatabaseFactory;
30  import p3j.gui.dialogs.AdjustMaxAgeDialog;
31  import p3j.gui.dialogs.DuplicateProjectionDialog;
32  import p3j.gui.dialogs.NewSetTypeDialog;
33  import p3j.gui.dialogs.ProjectionDialog;
34  import p3j.gui.dialogs.SubPopulationModelEditDialog;
35  import p3j.gui.misc.SubNodeSummary;
36  import p3j.gui.panels.PropertiesShowPanelFactory;
37  import p3j.misc.Misc;
38  import p3j.pppm.ProjectionModel;
39  import p3j.pppm.sets.SetType;
40  
41  /**
42   * Node that represents a projection.
43   * 
44   * Created: August 24, 2008
45   * 
46   * @author Christina Bohk
47   * @author Roland Ewald
48   * 
49   */
50  public class ProjectionNode extends ProjectionTreeNode<ProjectionModel> {
51  
52    /** Serialization ID. */
53    private static final long serialVersionUID = 8332182127722759791L;
54  
55    /**
56     * Default constructor.
57     * 
58     * @param projection
59     *          the projection to be represented
60     */
61    public ProjectionNode(ProjectionModel projection) {
62      super(projection, projection.getName());
63    }
64  
65    @Override
66    public void deselected() {
67  
68    }
69  
70    @Override
71    public JPanel selected(TreePath selectionPath, final IProjectionTree projTree) {
72  
73      // Layout for info sheet on projection.
74      final ProjectionNode node = this;
75      final JTextField name = new JTextField(getEntity().getName());
76      final JTextArea description = new JTextArea(getEntity().getDescription());
77      final JTextField jumpOffYear = new JTextField(Integer.toString(getEntity()
78          .getJumpOffYear()));
79  
80      JButton showSubPopsButton = new JButton("Show Sub-Population Structure");
81      showSubPopsButton.addActionListener(new ActionListener() {
82        @Override
83        public void actionPerformed(ActionEvent e) {
84          new SubPopulationModelEditDialog(getEntity().getSubPopulationModel(),
85              false).setVisible(true);
86        }
87      });
88  
89      JButton duplicateButton = new JButton("Duplicate");
90      duplicateButton.addActionListener(new ActionListener() {
91        @Override
92        public void actionPerformed(ActionEvent e) {
93          DuplicateProjectionDialog dp = new DuplicateProjectionDialog(
94              getEntity());
95          dp.setVisible(true);
96        }
97      });
98  
99      JButton createSetTypeButton = new JButton("New Settype");
100     createSetTypeButton.addActionListener(new ActionListener() {
101       @Override
102       public void actionPerformed(ActionEvent e) {
103         NewSetTypeDialog nstd = new NewSetTypeDialog(node, projTree);
104         nstd.setVisible(true);
105         SetType newSetType = nstd.getNewSetType();
106         DatabaseFactory.getDatabaseSingleton().saveSetType(newSetType);
107         projTree.totalRefresh();
108         projTree.selectNode(node.getChildWithEntity(newSetType));
109       }
110     });
111 
112     JButton applyButton = new JButton("Apply");
113     applyButton.addActionListener(new ActionListener() {
114       @Override
115       public void actionPerformed(ActionEvent e) {
116         ProjectionModel projection = getEntity();
117         projection.setName(name.getText());
118         projection.setDescription(description.getText());
119         projection.setJumpOffYear(Integer.parseInt(jumpOffYear.getText()));
120         setUserObject(projection.getName());
121         DatabaseFactory.getDatabaseSingleton().saveProjection(projection);
122         projTree.refreshNode(node);
123       }
124     });
125 
126     JButton adjustMaxAgeButton = new JButton("Adjust Age Classes");
127     adjustMaxAgeButton.addActionListener(new ActionListener() {
128       @Override
129       public void actionPerformed(ActionEvent e) {
130         adjustMaxAge(getEntity());
131       }
132     });
133 
134     List<JButton> buttons = new ArrayList<JButton>();
135     buttons.add(showSubPopsButton);
136     buttons.add(duplicateButton);
137     buttons.add(adjustMaxAgeButton);
138     buttons.add(createSetTypeButton);
139     buttons.add(applyButton);
140     PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(buttons, 1);
141     pspf.sep("General Information");
142     pspf.app(Misc.GUI_LABEL_NAME, name);
143     pspf.app(Misc.GUI_LABEL_DESCRIPTION, description, 2);
144     pspf.app(Misc.GUI_LABEL_JUMP_OFF_YEAR, jumpOffYear);
145     pspf.app("Settypes:", getEntity().getAllSetTypes().size());
146     pspf.app("Descendant Generations:", getEntity().getGenerations() - 1);
147     pspf.app("Years to be predicted:", getEntity().getYears());
148     pspf.app("Number of age classes:", getEntity().getNumberOfAgeClasses());
149     pspf.app("Number of sub-populations:", getEntity().getSubPopulationModel()
150         .getSubPopulations().size());
151     pspf.appPreview(new SubNodeSummary<SetType>(node, projTree, SetType.class));
152     return pspf.constructPanel();
153   }
154 
155   /**
156    * Adjusts maximum age.
157    * 
158    * @param projectionModel
159    *          the projection model for which the maximum age shall be adjusted
160    */
161   protected void adjustMaxAge(ProjectionModel projectionModel) {
162     ProjectionDialog maDialog = new AdjustMaxAgeDialog(projectionModel);
163     maDialog.setVisible(true);
164   }
165 }