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