View Javadoc

1   /*
2       Copyright 2007-2014 Fraunhofer IGD, http://www.igd.fraunhofer.de
3       Fraunhofer-Gesellschaft - Institute for Computer Graphics Research
4   
5       See the NOTICE file distributed with this work for additional
6       information regarding copyright ownership
7   
8       Licensed under the Apache License, Version 2.0 (the "License");
9       you may not use this file except in compliance with the License.
10      You may obtain a copy of the License at
11  
12        http://www.apache.org/licenses/LICENSE-2.0
13  
14      Unless required by applicable law or agreed to in writing, software
15      distributed under the License is distributed on an "AS IS" BASIS,
16      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17      See the License for the specific language governing permissions and
18      limitations under the License.
19   */
20  package org.universAAL.samples.context.reasoner.client.gui;
21  
22  import java.awt.Dimension;
23  import java.awt.GridBagConstraints;
24  import java.awt.GridBagLayout;
25  import java.awt.Insets;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  
32  import javax.swing.Box;
33  import javax.swing.JButton;
34  import javax.swing.JCheckBox;
35  import javax.swing.JComboBox;
36  import javax.swing.JFrame;
37  import javax.swing.JLabel;
38  import javax.swing.JOptionPane;
39  import javax.swing.JScrollPane;
40  import javax.swing.JTextArea;
41  import javax.swing.WindowConstants;
42  
43  import org.universAAL.ontology.reasoner.Query;
44  import org.universAAL.ontology.reasoner.Situation;
45  import org.universAAL.samples.context.reasoner.client.uaalinterface.ReasoningCaller;
46  
47  /**
48   * This frame is quite easy, because all have to be done to create a rule is to
49   * combine a Situation with a Query. Therefore two ComboBoxes are needed to
50   * display the available elements of both types and select the one you need. To
51   * make the identification of the elements more easy they are display below the
52   * boxes.
53   * 
54   * @author amarinc
55   * 
56   */
57  @SuppressWarnings("serial")
58  public class RuleCreatorFrame extends JFrame {
59  
60      private ReasoningCaller caller = null;
61      private ReasoningGUI parentGUI = null;
62  
63      private JLabel situationContentLabel = null;
64      private JTextArea searchStringArea = null;
65      private JComboBox situationBox = null;
66      private JComboBox queryBox = null;
67      private JCheckBox saveCheck = null;
68  
69      private RuleCreatorFrame self = this;
70      private HashMap<String, Situation> situations = new HashMap<String, Situation>();
71      private HashMap<String, Query> queries = new HashMap<String, Query>();
72  
73      public RuleCreatorFrame(ReasoningGUI parentGUI, ReasoningCaller caller) {
74  	this.caller = caller;
75  	this.parentGUI = parentGUI;
76  	initialize();
77      }
78  
79      private void initialize() {
80  	this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
81  	this.pack();
82  	this.setSize(600, 300);
83  	this.setVisible(true);
84  	this.getContentPane().setLayout(new GridBagLayout());
85  	this.setTitle("Rule Creator");
86  	this.setEnabled(true);
87  
88  	GridBagConstraints c = new GridBagConstraints();
89  	c.fill = GridBagConstraints.HORIZONTAL;
90  	c.insets = new Insets(5, 5, 5, 5);
91  	c.weighty = 1.0;
92  
93  	// first line
94  	c.gridx = 0;
95  	c.gridy = 0;
96  	c.weightx = 0.3;
97  	JLabel situationLabel = new JLabel("Situation:");
98  	this.getContentPane().add(situationLabel, c);
99  	c.gridx = 1;
100 	c.gridy = 0;
101 	c.weightx = 1.0;
102 	JLabel queryLabel = new JLabel("Query:");
103 	this.getContentPane().add(queryLabel, c);
104 
105 	// second line
106 	c.gridx = 0;
107 	c.gridy = 1;
108 	c.weightx = 0.3;
109 	situationBox = new JComboBox(refreshSituations().toArray(new String[0]));
110 	situationBox.addActionListener(new ActionListener() {
111 	    public void actionPerformed(ActionEvent e) {
112 		refreshSituationLabels();
113 	    }
114 	});
115 	this.getContentPane().add(situationBox, c);
116 	c.gridx = 1;
117 	c.gridy = 1;
118 	c.weightx = 1.0;
119 	queryBox = new JComboBox(refreshQueries().toArray(new String[0]));
120 	queryBox.addActionListener(new ActionListener() {
121 	    public void actionPerformed(ActionEvent e) {
122 		refreshQueryLabel();
123 	    }
124 	});
125 	this.getContentPane().add(queryBox, c);
126 
127 	// third line
128 	c.gridx = 0;
129 	c.gridy = 2;
130 	c.weightx = 0.3;
131 	situationContentLabel = new JLabel("");
132 	this.getContentPane().add(situationContentLabel, c);
133 	refreshSituationLabels();
134 	c.gridx = 1;
135 	c.gridy = 2;
136 	c.weightx = 1.0;
137 	c.weighty = 2.0;
138 	c.fill = GridBagConstraints.BOTH;
139 	searchStringArea = new JTextArea();
140 	searchStringArea.setEditable(false);
141 	JScrollPane scrollPane = new JScrollPane(searchStringArea);
142 	scrollPane.setPreferredSize(new Dimension(300, 200));
143 	this.getContentPane().add(scrollPane, c);
144 	refreshQueryLabel();
145 
146 	// footer
147 	c.gridx = 0;
148 	c.gridy = 3;
149 	c.gridwidth = 2;
150 	c.weighty = 1.0;
151 	c.fill = GridBagConstraints.HORIZONTAL;
152 	Box footerBox = Box.createHorizontalBox();
153 	this.getContentPane().add(footerBox, c);
154 	footerBox.add(Box.createHorizontalGlue());
155 	saveCheck = new JCheckBox("Save permanent: ");
156 	saveCheck.setSelected(true);
157 	footerBox.add(saveCheck);
158 	footerBox.add(Box.createHorizontalStrut(50));
159 	JButton addButton = new JButton("Add");
160 	addButton.setPreferredSize(new Dimension(80, 30));
161 	addButton.addActionListener(new ActionListener() {
162 	    public void actionPerformed(ActionEvent e) {
163 		String situationURI = situationBox.getSelectedItem().toString();
164 		Situation situation = situations.get(situationURI);
165 		String queryURI = queryBox.getSelectedItem().toString();
166 		Query query = queries.get(queryURI);
167 		if (situation == null || query == null)
168 		    JOptionPane
169 			    .showMessageDialog(self,
170 				    "Please select a query and a situation. Rule not added!");
171 		boolean persistent = saveCheck.isSelected();
172 		if (persistent
173 			&& (!situation.isPersistent() || !query.isPersistent())) {
174 		    JOptionPane
175 			    .showMessageDialog(
176 				    self,
177 				    "The rule can not be persistent since the given Situation and Query are not. A temporary rule will be created instead.");
178 		    persistent = false;
179 		}
180 		caller.addRule(situation, query, persistent);
181 		parentGUI.closeRuleFrame();
182 	    }
183 	});
184 	footerBox.add(addButton);
185 	footerBox.add(Box.createHorizontalStrut(5));
186 	JButton cancelButton = new JButton("Cancel");
187 	cancelButton.setPreferredSize(new Dimension(80, 30));
188 	cancelButton.addActionListener(new ActionListener() {
189 	    public void actionPerformed(ActionEvent e) {
190 		parentGUI.closeRuleFrame();
191 	    }
192 	});
193 	footerBox.add(cancelButton);
194 
195 	this.pack();
196     }
197 
198     /**
199      * Returns a list of URI's from the current available situations and saves
200      * the situation objects in a map to make them available by giving their
201      * URI's
202      * 
203      * @return List of URI's from currently available situations
204      */
205     private List<String> refreshSituations() {
206 	this.situations.clear();
207 	List<Situation> curSituations = caller.getSituations();
208 	List<String> result = new ArrayList<String>();
209 	for (Situation situation : curSituations) {
210 	    this.situations.put(situation.getURI(), situation);
211 	    result.add(situation.getURI());
212 	}
213 	return result;
214     }
215 
216     /**
217      * Returns a list of URI's from the current available queries and saves the
218      * query objects in a map to make them available by giving their URI's
219      * 
220      * @return List of URI's from currently available queries
221      */
222     private List<String> refreshQueries() {
223 	this.queries.clear();
224 	List<Query> curQueries = caller.getQueries();
225 	List<String> result = new ArrayList<String>();
226 	for (Query query : curQueries) {
227 	    this.queries.put(query.getURI(), query);
228 	    result.add(query.getURI());
229 	}
230 	return result;
231     }
232 
233     /**
234      * Used to refresh the content of a Situation after selecting a new one from
235      * the according ComboBox.
236      */
237     private void refreshSituationLabels() {
238 	String eventURI = situationBox.getSelectedItem().toString();
239 	Situation situation = situations.get(eventURI);
240 	if (situation != null)
241 	    situationContentLabel.setText(situation.toString());
242     }
243 
244     /**
245      * Used to refresh the content of a Query after selecting a new one from the
246      * according ComboBox.
247      */
248     private void refreshQueryLabel() {
249 	String eventURI = queryBox.getSelectedItem().toString();
250 	Query query = queries.get(eventURI);
251 	if (query != null) {
252 	    searchStringArea.setText(query.getResultingQuery());
253 	}
254     }
255 }