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.P3J;
31  import p3j.gui.misc.SubNodeSummary;
32  import p3j.gui.panels.PropertiesShowPanelFactory;
33  import p3j.misc.Misc;
34  import p3j.misc.gui.GUI;
35  import p3j.pppm.ProjectionModel;
36  import p3j.pppm.parameters.ParameterInstance;
37  import p3j.pppm.sets.Set;
38  import p3j.pppm.sets.SetType;
39  
40  /**
41   * A node in the projection tree that represents a {@link Set}.
42   * 
43   * Created: August 24, 2008
44   * 
45   * @author Christina Bohk
46   * @author Roland Ewald
47   * 
48   */
49  public class SetNode extends ProjectionTreeNode<Set> {
50  
51  	/**
52  	 * Serialization ID.
53  	 */
54  	private static final long serialVersionUID = -6441130209061752819L;
55  
56  	/**
57  	 * Default constructor.
58  	 * 
59  	 * @param set
60  	 *          the set to be represented
61  	 */
62  	public SetNode(Set set) {
63  		super(set, set.getName());
64  	}
65  
66  	@Override
67  	public JPanel selected(TreePath selectionPath, final IProjectionTree projTree) {
68  
69  		// Layout for info sheet on set.
70  
71  		final ProjectionTreeNode<?> node = this;
72  		final SetTypeNode stNode = (SetTypeNode) selectionPath.getParentPath()
73  		    .getLastPathComponent();
74  		final JTextField name = new JTextField(getEntity().getName());
75  		final JTextArea description = new JTextArea(getEntity().getDescription());
76  		final JTextField probability = new JTextField(getEntity().getProbability()
77  		    + "");
78  
79  		JButton removeButton = new JButton("Remove");
80  		removeButton.addActionListener(new ActionListener() {
81  			@Override
82  			public void actionPerformed(ActionEvent e) {
83  				if (!GUI.printQuestion(P3J.getInstance(), "Really remove set?",
84  				    "Do you really want to remove the set '" + getEntity().getName()
85  				        + "'? All associated data will be lost!")) {
86  					return;
87  				}
88  				SetType sType = stNode.getEntity();
89  				sType.removeSet(getEntity());
90  				DatabaseFactory.getDatabaseSingleton().saveSetType(sType);
91  				projTree.removeNode(node);
92  			}
93  		});
94  
95  		JButton applyButton = new JButton("Apply");
96  		applyButton.addActionListener(new ActionListener() {
97  			@Override
98  			public void actionPerformed(ActionEvent e) {
99  				Set set = getEntity();
100 				set.setName(name.getText());
101 				set.setDescription(description.getText());
102 				set.setProbability(Double.parseDouble(probability.getText()));
103 				setUserObject(set.getName());
104 				DatabaseFactory.getDatabaseSingleton().saveSet(set);
105 				projTree.refreshNode(node);
106 			}
107 		});
108 
109 		List<JButton> buttons = new ArrayList<JButton>();
110 		buttons.add(removeButton);
111 		buttons.add(applyButton);
112 
113 		PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(buttons, 1);
114 		pspf.sep("General Information");
115 		pspf.app(Misc.GUI_LABEL_NAME, name);
116 		pspf.app(Misc.GUI_LABEL_DESCRIPTION, description, 2);
117 		pspf.app(Misc.GUI_LABEL_PROBABILITY, probability);
118 
119 		// Set default Settype specifics - there will only be one set in the
120 		// default Settype, etc.
121 		SetType mySetType = getProjectionEntity(SetType.class);
122 		ProjectionModel projection = getProjectionEntity(ProjectionModel.class);
123 		boolean isDefault = (mySetType != null && projection != null && mySetType == projection
124 		    .getDefaultSetType());
125 		probability.setEditable(!isDefault);
126 		removeButton.setEnabled(!isDefault);
127 		pspf.app("Default set?", isDefault ? "Yes" : "No");
128 		pspf.appPreview(new SubNodeSummary<ParameterInstance>(this, projTree,
129 		    ParameterInstance.class));
130 
131 		return pspf.constructPanel();
132 	}
133 }