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.dialogs;
17  
18  import javax.swing.JPanel;
19  import javax.swing.JTextArea;
20  import javax.swing.JTextField;
21  
22  import p3j.database.DatabaseFactory;
23  import p3j.gui.panels.projections.IProjectionTree;
24  import p3j.gui.panels.projections.SetNode;
25  import p3j.gui.panels.projections.SetTypeNode;
26  import p3j.misc.Misc;
27  import p3j.misc.gui.GUI;
28  import p3j.pppm.sets.Set;
29  import p3j.pppm.sets.SetType;
30  
31  import com.jgoodies.forms.layout.CellConstraints;
32  import com.jgoodies.forms.layout.FormLayout;
33  
34  /**
35   * Dialog to create a new {@link Set} for a given {@link SetType}.
36   * 
37   * Created: September 7, 2008
38   * 
39   * @author Christina Bohk
40   * @author Roland Ewald
41   */
42  public class NewSetDialog extends
43      AbstractProjectionTreeDialog<SetType, SetTypeNode> {
44  
45  	/** Serialization ID. */
46  	private static final long serialVersionUID = -6901469269123228049L;
47  
48  	/** Width of the dialog. */
49  	public static final int DIALOG_WIDTH = 480;
50  
51  	/** Height of the dialog. */
52  	public static final int DIALOG_HEIGHT = 320;
53  
54  	/** Text field for the name of the set. */
55  	private JTextField name = new JTextField("New Set");
56  
57  	/** Text field to enter the probability. */
58  	private JTextField probability = new JTextField("0.0");
59  
60  	/** Text area to enter a description of the set. */
61  	private JTextArea description = new JTextArea("");
62  
63  	/**
64  	 * Default constructor.
65  	 * 
66  	 * @param stNode
67  	 *          the node of the current Settype
68  	 * @param projTree
69  	 *          the current projection tree
70  	 */
71  	public NewSetDialog(SetTypeNode stNode, IProjectionTree projTree) {
72  		super(stNode, projTree, DIALOG_WIDTH, DIALOG_HEIGHT, "Add Set");
73  		initUI();
74  	}
75  
76  	/**
77  	 * Initializes user interface.
78  	 */
79  	private void initUI() {
80  		setTitle("Create New Set");
81  
82  		FormLayout layout = new FormLayout("10dlu,right:60dlu,10dlu,d:grow,10dlu",
83  		    "10dlu,d,10dlu,d,10dlu,fill:80dlu:grow,10dlu,d,10dlu");
84  		JPanel contentPanel = new JPanel(layout);
85  
86  		int currRow = GUI.ROW_SKIP_LAYOUT;
87  		currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_NAME, name,
88  		    currRow);
89  		currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_PROBABILITY,
90  		    probability, currRow);
91  		currRow = GUI.addRowToPanel(contentPanel, Misc.GUI_LABEL_DESCRIPTION,
92  		    description, currRow);
93  
94  		contentPanel.add(getButtonPanel(),
95  		    new CellConstraints().xy(GUI.INPUT_COLUMN_INDEX, currRow));
96  		setContentPane(contentPanel);
97  	}
98  
99  	@Override
100 	protected void cancel() {
101 		setVisible(false);
102 	}
103 
104 	@Override
105 	protected void ok() {
106 		Set set = getEntity().createSet(name.getText(), description.getText(),
107 		    Double.parseDouble(probability.getText()));
108 		DatabaseFactory.getDatabaseSingleton().saveSetType(getEntity());
109 
110 		SetNode newNode = getProjectionTree().createNewSetStructure(
111 		    getEntityNode(), set);
112 		getProjectionTree().refreshNodeSubStructure(getEntityNode());
113 		getProjectionTree().selectNode(newNode);
114 		setVisible(false);
115 	}
116 }