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