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;
22  
23  import java.awt.AWTEvent;
24  import java.awt.BorderLayout;
25  import java.awt.Color;
26  import java.awt.Dimension;
27  import java.awt.GridLayout;
28  import java.awt.event.ActionEvent;
29  import java.awt.event.FocusEvent;
30  import java.util.ArrayList;
31  import java.util.Arrays;
32  
33  import javax.swing.BorderFactory;
34  import javax.swing.ButtonGroup;
35  import javax.swing.JButton;
36  import javax.swing.JComboBox;
37  import javax.swing.JDialog;
38  import javax.swing.JLabel;
39  import javax.swing.JPanel;
40  import javax.swing.JRadioButton;
41  import javax.swing.JScrollPane;
42  import javax.swing.JTabbedPane;
43  import javax.swing.JTextArea;
44  import javax.swing.border.Border;
45  import javax.swing.border.TitledBorder;
46  
47  import unbbayes.prs.Node;
48  import unbbayes.prs.bn.ExplanationPhrase;
49  import unbbayes.prs.bn.ProbabilisticNode;
50  import unbbayes.prs.bn.SingleEntityNetwork;
51  
52  /**
53   *
54   * @author Mário Henrique Paes Vieira (mariohpv@bol.com.br)
55   * @version 1.0
56   */
57  public class ExplanationProperties extends JDialog
58  {
59    /** Serialization runtime version number */
60    private static final long serialVersionUID = 0;		
61  	
62    private JTabbedPane jTabbedPane = new JTabbedPane();
63    private JPanel southPanel = new JPanel();
64    private JPanel descriptionPanel = new JPanel();
65    private JPanel explanationPanel = new JPanel();
66    private JButton okButton = new JButton();
67    private Border border1;
68    private Border border2;
69    private JPanel evidencePhrasePanel = new JPanel();
70    private JPanel explanationTopPanel = new JPanel();
71    private JPanel evidenceTypePanel = new JPanel();
72    private JPanel evidenceNodePanel = new JPanel();
73    private JPanel evidencePhraseBottomPanel = new JPanel();
74    private JLabel evidenceNodeLabel = new JLabel();
75    private JComboBox evidenceNodeComboBox = new JComboBox();
76    private TitledBorder titledBorder1;
77    private JPanel notUsedPanel = new JPanel();
78    private JPanel exclusivePanel = new JPanel();
79    private JPanel necessaryPanel = new JPanel();
80    private JPanel naPanel = new JPanel();
81    private JPanel complementaryPanel = new JPanel();
82    private JPanel triggerPanel = new JPanel();
83    private JRadioButton triggerRadioButton = new JRadioButton();
84    private JRadioButton complementaryRadioButton = new JRadioButton();
85    private JRadioButton naRadioButton = new JRadioButton();
86    private JRadioButton necessaryRadioButton = new JRadioButton();
87    private JRadioButton exclusiveRadioButton = new JRadioButton();
88    private ButtonGroup buttonGroup1 = new ButtonGroup();
89    private Border evidenceTypeBorder;
90    private JPanel evidencePhraseTopPanel = new JPanel();
91    private JLabel evidencePhraseLabel = new JLabel();
92    private JScrollPane evidencePhraseScrollPane = new JScrollPane();
93    private JTextArea evidencePhraseTextArea = new JTextArea();
94    private NetworkWindow netWindow;
95    private ProbabilisticNode node;
96    private SingleEntityNetwork net;
97    private JButton cancelButton = new JButton();
98    private JLabel nodeNameLabel = new JLabel();
99    private JScrollPane explanationNodeScrollPane = new JScrollPane();
100   private JPanel descriptionTopPanel1 = new JPanel();
101   private JPanel descriptionTopPanel2 = new JPanel();
102   private JPanel descriptionTopPanel3 = new JPanel();
103   private JPanel descriptionTopPanel = new JPanel();
104   private JPanel descriptionBottomPanel = new JPanel();
105   private JLabel explanationVariableLabel = new JLabel();
106   private JLabel descriptionLabel = new JLabel();
107   private JTextArea explanationNodeTextArea = new JTextArea();
108 
109   public ExplanationProperties(NetworkWindow netWindow,SingleEntityNetwork net)
110   { this.netWindow = netWindow;
111     this.net = net;
112     try
113     {
114       jbInit();
115     }
116     catch(Exception e)
117     {
118       e.printStackTrace();
119     }
120   }
121   private void jbInit() throws Exception
122   {
123     this.setSize(550, 470);
124     this.setResizable(false);
125     this.setTitle("Propriedades da Variável de Explicação");
126 
127     // Cria bordas
128     border1 = BorderFactory.createEmptyBorder(0,10,0,10);
129     border2 = BorderFactory.createEmptyBorder(20,20,20,20);
130     titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(148, 145, 140)),"Tipo de Evidência:");
131     evidenceTypeBorder = BorderFactory.createCompoundBorder(new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(148, 145, 140)),"Tipo de Evidência:"),BorderFactory.createEmptyBorder(0,10,0,0));
132 
133     // Muda propriedades do botao ok
134     okButton.setMaximumSize(new Dimension(85, 27));
135     okButton.setMinimumSize(new Dimension(85, 27));
136     okButton.setPreferredSize(new Dimension(85, 27));
137     okButton.setText("OK");
138     okButton.addActionListener(new java.awt.event.ActionListener()
139     {
140       public void actionPerformed(ActionEvent e)
141       {
142         okButton_actionPerformed(e);
143       }
144     });
145 
146     descriptionPanel.setLayout(new BorderLayout(0,5));
147     explanationPanel.setLayout(new GridLayout(2,0,0,10));
148     explanationTopPanel.setLayout(new BorderLayout(0,10));
149     evidencePhrasePanel.setLayout(new BorderLayout(0,5));
150     evidenceNodeLabel.setText("Evidencia : ");
151     evidenceNodePanel.setLayout(new BorderLayout());
152     evidenceTypePanel.setBorder(evidenceTypeBorder);
153     evidenceTypePanel.setLayout(new GridLayout(2,3,10,0));
154     triggerRadioButton.setText("Trigger");
155     triggerRadioButton.addActionListener(new java.awt.event.ActionListener()
156     {
157       public void actionPerformed(ActionEvent e)
158       {
159         evidenceRadioButton_actionPerformed(e);
160       }
161     });
162     complementaryRadioButton.setText("Complementar");
163     complementaryRadioButton.addActionListener(new java.awt.event.ActionListener()
164     {
165       public void actionPerformed(ActionEvent e)
166       {
167         evidenceRadioButton_actionPerformed(e);
168       }
169     });
170     naRadioButton.setText("N/A");
171     naRadioButton.addActionListener(new java.awt.event.ActionListener()
172     {
173       public void actionPerformed(ActionEvent e)
174       {
175         evidenceRadioButton_actionPerformed(e);
176       }
177     });
178     necessaryRadioButton.setText("Essencial");
179     necessaryRadioButton.addActionListener(new java.awt.event.ActionListener()
180     {
181       public void actionPerformed(ActionEvent e)
182       {
183         evidenceRadioButton_actionPerformed(e);
184       }
185     });
186     exclusiveRadioButton.setText("Excludente");
187     exclusiveRadioButton.addActionListener(new java.awt.event.ActionListener()
188     {
189       public void actionPerformed(ActionEvent e)
190       {
191         evidenceRadioButton_actionPerformed(e);
192       }
193     });
194     triggerPanel.setLayout(new BorderLayout());
195     complementaryPanel.setLayout(new BorderLayout());
196     naPanel.setLayout(new BorderLayout());
197     necessaryPanel.setLayout(new BorderLayout());
198     exclusivePanel.setLayout(new BorderLayout());
199     notUsedPanel.setLayout(new BorderLayout());
200     evidencePhraseLabel.setText("Texto para Explanação :");
201     evidencePhraseTopPanel.setLayout(new BorderLayout());
202     evidencePhraseBottomPanel.setLayout(new BorderLayout());
203     cancelButton.setText("Cancelar");
204     cancelButton.addActionListener(new java.awt.event.ActionListener()
205     {
206       public void actionPerformed(ActionEvent e)
207       {
208         cancelButton_actionPerformed(e);
209       }
210     });
211     evidenceNodeComboBox.addActionListener(new java.awt.event.ActionListener()
212     {
213       public void actionPerformed(ActionEvent e)
214       {
215         evidenceNodeComboBox_actionPerformed(e);
216       }
217     });
218     evidencePhraseTextArea.addFocusListener(new java.awt.event.FocusAdapter()
219     {
220       public void focusLost(FocusEvent e)
221       {
222         evidencePhraseTextArea_focusLost(e);
223       }
224     });
225     descriptionTopPanel2.setLayout(new BorderLayout());
226     descriptionTopPanel3.setLayout(new BorderLayout());
227     descriptionTopPanel.setLayout(new GridLayout(3,0));
228     descriptionBottomPanel.setLayout(new BorderLayout());
229     explanationVariableLabel.setText("Variável de explicação : ");
230     descriptionPanel.setBorder(border2);
231     descriptionTopPanel1.setLayout(new BorderLayout());
232     descriptionLabel.setText("Descrição:");
233     explanationPanel.setBorder(border2);
234     this.getContentPane().add(southPanel,  BorderLayout.SOUTH);
235     descriptionTopPanel.add(descriptionTopPanel1, null);
236     descriptionTopPanel1.add(descriptionLabel,  BorderLayout.CENTER);
237     descriptionTopPanel.add(descriptionTopPanel2, null);
238     descriptionTopPanel.add(descriptionTopPanel3, null);
239     descriptionTopPanel3.add(explanationVariableLabel, BorderLayout.WEST);
240     descriptionTopPanel3.add(nodeNameLabel, BorderLayout.CENTER);
241     descriptionPanel.add(descriptionBottomPanel,  BorderLayout.CENTER);
242     descriptionBottomPanel.add(explanationNodeScrollPane, BorderLayout.CENTER);
243     explanationNodeScrollPane.getViewport().add(explanationNodeTextArea, null);
244     descriptionPanel.add(descriptionTopPanel, BorderLayout.NORTH);
245     southPanel.add(okButton, null);
246     southPanel.add(cancelButton, null);
247     explanationPanel.add(explanationTopPanel, null);
248     explanationPanel.add(evidencePhrasePanel, null);
249     explanationTopPanel.add(evidenceTypePanel, BorderLayout.CENTER);
250     evidenceTypePanel.add(triggerPanel, null);
251     triggerPanel.add(triggerRadioButton, BorderLayout.CENTER);
252     evidenceTypePanel.add(complementaryPanel, null);
253     complementaryPanel.add(complementaryRadioButton, BorderLayout.CENTER);
254     evidenceTypePanel.add(naPanel, null);
255     naPanel.add(naRadioButton, BorderLayout.CENTER);
256     evidenceTypePanel.add(necessaryPanel, null);
257     necessaryPanel.add(necessaryRadioButton, BorderLayout.CENTER);
258     evidenceTypePanel.add(exclusivePanel, null);
259     exclusivePanel.add(exclusiveRadioButton, BorderLayout.CENTER);
260     evidenceTypePanel.add(notUsedPanel, null);
261     explanationTopPanel.add(evidenceNodePanel, BorderLayout.NORTH);
262     evidenceNodePanel.add(evidenceNodeLabel, BorderLayout.WEST);
263     evidenceNodePanel.add(evidenceNodeComboBox, BorderLayout.CENTER);
264     evidencePhrasePanel.add(evidencePhraseBottomPanel, BorderLayout.CENTER);
265     evidencePhraseBottomPanel.add(evidencePhraseScrollPane,  BorderLayout.CENTER);
266     evidencePhraseScrollPane.getViewport().add(evidencePhraseTextArea, null);
267     evidencePhrasePanel.add(evidencePhraseTopPanel, BorderLayout.NORTH);
268     evidencePhraseTopPanel.add(evidencePhraseLabel, BorderLayout.CENTER);
269     buttonGroup1.add(triggerRadioButton);
270     buttonGroup1.add(complementaryRadioButton);
271     buttonGroup1.add(naRadioButton);
272     buttonGroup1.add(necessaryRadioButton);
273     buttonGroup1.add(exclusiveRadioButton);
274     jTabbedPane.setBorder(border1);
275     jTabbedPane.add(descriptionPanel, "Descrição");
276     jTabbedPane.add(explanationPanel, "Explanação");
277     this.getContentPane().add(jTabbedPane,  BorderLayout.CENTER);
278     ArrayList<Node> nodes = net.getNodes();
279     //ArrayList<Node> nodes = net.getDescriptionNodes();
280     int size = nodes.size();
281     int i;
282     String[] stringNodes = new String[size];
283     for (i=0; i<size; i++)
284     {   stringNodes[i] = nodes.get(i).getDescription();
285     }
286     Arrays.sort(stringNodes);
287     for (i=0; i<size; i++)
288     {   evidenceNodeComboBox.addItem(stringNodes[i]);
289     }
290   }
291 
292   public void setProbabilisticNode(ProbabilisticNode node)
293   {
294     this.node = node;
295     explanationNodeTextArea.setText(node.getExplanationDescription());
296     if (evidenceNodeComboBox.getItemCount() != 0)
297     {
298       updateExplanationInformation(evidenceNodeComboBox.getItemAt(0).toString());
299     }
300     nodeNameLabel.setText(node.getName());
301     descriptionLabel.setText("Descrição: "+node.getDescription());
302   }
303 
304   void okButton_actionPerformed(ActionEvent e)
305   {   node.setExplanationDescription(explanationNodeTextArea.getText());
306       dispose();
307   }
308 
309   void cancelButton_actionPerformed(ActionEvent e)
310   {   dispose();
311   }
312 
313   void evidenceNodeComboBox_actionPerformed(ActionEvent evt)
314   {   JComboBox source = (JComboBox)evt.getSource();
315       String item = (String)source.getSelectedItem();
316       updateExplanationInformation(item);
317   }
318 
319   private void updateExplanationInformation(String item)
320   {   try
321       {   ExplanationPhrase explanationPhrase = node.getExplanationPhrase(item);
322           int evidenceType = explanationPhrase.getEvidenceType();
323           switch (evidenceType)
324           {   case (ExplanationPhrase.TRIGGER_EVIDENCE_TYPE) :        triggerRadioButton.setSelected(true);
325                                                                       break;
326               case (ExplanationPhrase.NECESSARY_EVIDENCE_TYPE) :      necessaryRadioButton.setSelected(true);
327                                                                       break;
328               case (ExplanationPhrase.COMPLEMENTARY_EVIDENCE_TYPE) :  complementaryRadioButton.setSelected(true);
329                                                                       break;
330               case (ExplanationPhrase.EXCLUSIVE_EVIDENCE_TYPE) :      exclusiveRadioButton.setSelected(true);
331                                                                       break;
332               default : naRadioButton.setSelected(true);
333           }
334           evidencePhraseTextArea.setText(explanationPhrase.getPhrase());
335       }
336       catch (Exception e)
337       {   naRadioButton.setSelected(true);
338           evidencePhraseTextArea.setText("");
339       }
340   }
341 
342   void evidenceRadioButton_actionPerformed(ActionEvent e)
343   {   addEvidence(e);
344   }
345 
346   void evidencePhraseTextArea_focusLost(FocusEvent e)
347   {   addEvidence(e);
348   }
349 
350   private void addEvidence(AWTEvent e)
351   {   ExplanationPhrase explanationPhrase = new ExplanationPhrase();
352       explanationPhrase.setNode(evidenceNodeComboBox.getSelectedItem().toString());
353       if (triggerRadioButton.isSelected())
354       {   explanationPhrase.setEvidenceType(ExplanationPhrase.TRIGGER_EVIDENCE_TYPE);
355       }
356       else if (necessaryRadioButton.isSelected())
357       {   explanationPhrase.setEvidenceType(ExplanationPhrase.NECESSARY_EVIDENCE_TYPE);
358       }
359       else if (complementaryRadioButton.isSelected())
360       {   explanationPhrase.setEvidenceType(ExplanationPhrase.COMPLEMENTARY_EVIDENCE_TYPE);
361       }
362       else if (exclusiveRadioButton.isSelected())
363       {   explanationPhrase.setEvidenceType(ExplanationPhrase.EXCLUSIVE_EVIDENCE_TYPE);
364       }
365       else
366       {   explanationPhrase.setEvidenceType(ExplanationPhrase.NA_EVIDENCE_TYPE);
367       }
368       explanationPhrase.setPhrase(evidencePhraseTextArea.getText());
369       node.addExplanationPhrase(explanationPhrase);
370   }
371 }