View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2008 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.gui.continuous;
22  
23  import java.awt.BorderLayout;
24  import java.awt.Color;
25  import java.awt.Font;
26  import java.awt.GridLayout;
27  import java.awt.event.ActionListener;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.ResourceBundle;
31  
32  import javax.swing.JButton;
33  import javax.swing.JComboBox;
34  import javax.swing.JFrame;
35  import javax.swing.JLabel;
36  import javax.swing.JPanel;
37  import javax.swing.JTextField;
38  
39  import unbbayes.util.ResourceController;
40  import unbbayes.util.SortUtil;
41  
42  public class ContinuousNormalDistributionPane extends JPanel {
43  
44  	private static final long serialVersionUID = 1L;
45  	
46  	private JPanel followsPane;
47  	private JLabel followsLabel;
48  	
49  	private List<String> discreteParentNodeNameList;
50  	private List<String> continuousParentNodeNameList;
51  	
52  	private JPanel parentStateListPane;
53  	private List<JComboBox> discreteParentNodeStateSelectionList;
54  	
55  	private JPanel inputPane;
56  	private List<JTextField> constantTextFieldList;
57  	private JTextField meanTextField;
58  	private JTextField varianceTextField;
59  	
60  	private JPanel buttonPane;
61  	private JButton confirmButton;
62  	private JButton cancelButton;
63  	
64  	private ResourceBundle resource = ResourceController.RS_GUI;
65  	
66  	public ContinuousNormalDistributionPane(List<String> discreteParentNodeNameList, List<String> continuousParentNodeNameList) {
67  		if (discreteParentNodeNameList != null) {
68  			this.discreteParentNodeNameList = discreteParentNodeNameList;
69  		} else {
70  			this.discreteParentNodeNameList = new ArrayList<String>(0);
71  		}
72  		if (continuousParentNodeNameList != null) {
73  			this.continuousParentNodeNameList = continuousParentNodeNameList;
74  		} else {
75  			this.continuousParentNodeNameList = new ArrayList<String>(0);
76  		}
77  		SortUtil.sort(this.discreteParentNodeNameList);
78  		SortUtil.sort(this.continuousParentNodeNameList);
79  		
80  		createFollowsPane();
81  		createParentStateListPane();
82  		createInputPane();
83  		createButtonPane();
84  		createMainPane();
85  	}
86  	
87  	private void createMainPane() {
88  		this.setLayout(new BorderLayout());
89  		
90  		// Set north panel
91  		JPanel northPanel = new JPanel(new GridLayout(2, 1));
92  		northPanel.add(followsPane);
93  		northPanel.add(parentStateListPane);
94  		this.add(northPanel, BorderLayout.NORTH);
95  		
96  		// Set center panel
97  		this.add(inputPane, BorderLayout.CENTER);
98  		
99  		// Set south panel
100 		this.add(buttonPane, BorderLayout.SOUTH);
101 		
102 	}
103 
104 	public void addConfirmButtonActionListener(ActionListener al) {
105 		this.confirmButton.addActionListener(al);
106 	}
107 	
108 	public void addCancelButtonActionListener(ActionListener al) {
109 		this.cancelButton.addActionListener(al);
110 	}
111 	
112 	public void addParentStateChangeActionListener(ActionListener al) {
113 		for (JComboBox comboBox : discreteParentNodeStateSelectionList) {
114 			comboBox.addActionListener(al);
115 		}
116 	}
117 	
118 	private void createButtonPane() {
119 		this.buttonPane = new JPanel();
120 		this.confirmButton = new JButton(resource.getString("confirmLabel"));
121 		this.cancelButton = new JButton(resource.getString("cancelLabel"));
122 		buttonPane.add(confirmButton);
123 		buttonPane.add(cancelButton);
124 	}
125 
126 	private void createInputPane() {
127 		this.inputPane = new JPanel(new GridLayout((int)((continuousParentNodeNameList.size() + 2) / 2), 4));
128 		
129 		meanTextField = new JTextField(10);
130 		inputPane.add(new JLabel(resource.getString("meanLabel")));
131 		inputPane.add(meanTextField);
132 		varianceTextField = new JTextField(10);
133 		inputPane.add(new JLabel(resource.getString("varianceLabel")));
134 		inputPane.add(varianceTextField);
135 		
136 		this.constantTextFieldList = new ArrayList<JTextField>(continuousParentNodeNameList.size());
137 		for (int i = 0; i < continuousParentNodeNameList.size(); i++) {
138 			constantTextFieldList.add(i, new JTextField(10));
139 			inputPane.add(new JLabel(resource.getString("constantLabel") + i));
140 			inputPane.add(constantTextFieldList.get(i));
141 		}
142 		
143 	}
144 
145 	public void createFollowsPane() {
146 		followsPane = new JPanel();
147 
148 		StringBuffer followsTitle = new StringBuffer();
149 		followsTitle.append(resource.getString("followsLabel") + " ");
150 		for (int i = 0; i < continuousParentNodeNameList.size(); i++) {
151 			if (i != 0) {
152 				followsTitle.append(" + ");
153 			}
154 			followsTitle.append(resource.getString("constantLabel") + i + " * " + continuousParentNodeNameList.get(i));
155 		}
156 		if (continuousParentNodeNameList.size() > 0) {
157 			followsTitle.append(" + ");
158 		}
159 		followsTitle.append(resource.getString("normalFunctionLabel"));
160 		 
161 		followsLabel = new JLabel(followsTitle.toString());
162 		followsLabel.setFont(new Font("Arial", Font.BOLD, 12));
163 		followsLabel.setForeground(Color.BLUE);
164 		followsPane.add(followsLabel);
165 	}
166 	
167 	private void createParentStateListPane() {
168 		parentStateListPane = new JPanel();
169 		discreteParentNodeStateSelectionList = new ArrayList<JComboBox>(discreteParentNodeNameList.size());
170 		JLabel label;
171 		JComboBox comboBox;
172 		for (String name : discreteParentNodeNameList) {
173 			label = new JLabel(name + " = ");
174 			comboBox = new JComboBox();
175 			discreteParentNodeStateSelectionList.add(comboBox);
176 			parentStateListPane.add(label);
177 			parentStateListPane.add(comboBox);
178 			parentStateListPane.add(new JLabel("\t"));
179 		}
180 		parentStateListPane.add(new JLabel("-\t"));
181 		for (String name : continuousParentNodeNameList) {
182 			parentStateListPane.add(new JLabel(name + "\t"));
183 		}
184 	}
185 	
186 	public void fillDiscreteParentStateSelection(String parentName, List<String> stateList) {
187 		JComboBox comboBox = null;
188 		for (int i = 0; i < discreteParentNodeNameList.size(); i++) {
189 			if (discreteParentNodeNameList.get(i).equals(parentName)) {
190 				comboBox = discreteParentNodeStateSelectionList.get(i);
191 				break;
192 			}
193 		}
194 		if (comboBox != null) {
195 			comboBox.removeAllItems();
196 			for (String state : stateList) {
197 				comboBox.addItem(state);
198 			}
199 		}
200 	}
201 
202 	public void setDiscreteAndContinuousParentNodeNameList(
203 			List<String> discreteParentNodeNameList, List<String> continuousParentNodeNameList) {
204 		this.discreteParentNodeNameList = discreteParentNodeNameList;
205 		this.continuousParentNodeNameList = continuousParentNodeNameList;
206 		SortUtil.sort(this.discreteParentNodeNameList);
207 		SortUtil.sort(this.continuousParentNodeNameList);
208 		createParentStateListPane();
209 	}
210 	
211 	public void setDiscreteParentNodeNameList(
212 			List<String> discreteParentNodeNameList) {
213 		this.discreteParentNodeNameList = discreteParentNodeNameList;
214 		SortUtil.sort(this.discreteParentNodeNameList);
215 		createParentStateListPane();
216 	}
217 
218 	public void setContinuousParentNodeNameList(
219 			List<String> continuousParentNodeNameList) {
220 		this.continuousParentNodeNameList = continuousParentNodeNameList;
221 		SortUtil.sort(this.continuousParentNodeNameList);
222 		createParentStateListPane();
223 	}
224 	
225 	public static void main(String[] args) {
226 		JFrame f = new JFrame();
227 		
228 		List<String> dis = new ArrayList<String>();
229 		dis.add("D2");
230 		dis.add("D1");
231 		dis.add("D4");
232 		dis.add("D3");
233 		
234 		List<String> con = new ArrayList<String>();
235 		con.add("C2");
236 		con.add("C1");
237 		con.add("C3");
238 		
239 		ContinuousNormalDistributionPane p = new ContinuousNormalDistributionPane(dis, con);
240 		
241 		for (String string : dis) {
242 			p.fillDiscreteParentStateSelection(string, con);
243 		}
244 		
245 		
246 		f.add(p);
247 		f.pack();
248 		f.setVisible(true);
249 		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
250 	}
251 	
252 	public String getMeanText() {
253 		return meanTextField.getText();
254 	}
255 	
256 	public void setMeanText(String mean) {
257 		meanTextField.setText(mean);
258 	}
259 	
260 	public String getVarianceText() {
261 		return varianceTextField.getText();
262 	}
263 	
264 	public void setVarianceText(String variance) {
265 		varianceTextField.setText(variance);
266 	}
267 	
268 	public List<String> getConstantTextList() {
269 		List<String> constantTextList = new ArrayList<String>(constantTextFieldList.size());
270 		for (JTextField textField : constantTextFieldList) {
271 			constantTextList.add(textField.getText());
272 		}
273 		return constantTextList;
274 	}
275 	
276 	public void setConstantTextList(List<String> constantList) {
277 		if (constantList.size() == constantTextFieldList.size()) {
278 			for (int i = 0; i < constantList.size(); i++) {
279 				constantTextFieldList.get(i).setText(constantList.get(i));
280 			}
281 		}
282 	}
283 	
284 	public int[] getDiscreteParentNodeStateSelectedList() {
285 		int[] parentStateList  = new int[discreteParentNodeStateSelectionList.size()];
286 		for (int i = 0; i < discreteParentNodeStateSelectionList.size(); i++) {
287 			parentStateList[i] = discreteParentNodeStateSelectionList.get(i).getSelectedIndex();
288 		}
289 		return parentStateList;
290 	}
291 
292 }