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 java.awt.Frame;
19  import java.awt.event.ActionEvent;
20  import java.awt.event.ActionListener;
21  import java.util.ArrayList;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import javax.swing.ButtonGroup;
27  import javax.swing.JButton;
28  import javax.swing.JDialog;
29  import javax.swing.JPanel;
30  import javax.swing.JRadioButton;
31  import javax.swing.JTextField;
32  
33  import p3j.experiment.results.filters.ConfigurableResultFilter;
34  import p3j.experiment.results.filters.IResultFilter;
35  import p3j.experiment.results.filters.IncludeAllResultFilter;
36  import p3j.gui.panels.PropertiesShowPanelFactory;
37  import p3j.misc.gui.GUI;
38  import p3j.pppm.ProjectionModel;
39  import p3j.pppm.parameters.ParameterAssignment;
40  import p3j.pppm.parameters.ParameterInstance;
41  import p3j.pppm.sets.SetType;
42  
43  /**
44   * Dialog to instantiate a result filter interactively. Makes use of
45   * {@link ConfigurableResultFilter}.
46   * 
47   * @see ConfigurableResultFilter
48   * @see IResultFilter
49   * @see p3j.experiment.results.ResultExport
50   * @author Christina Bohk
51   * @author Roland Ewald
52   */
53  public class ConfigureResultFilterDialog extends JDialog {
54  
55  	/** The Constant serialVersionUID. */
56  	private static final long serialVersionUID = -2646722465211989759L;
57  
58  	/** Width of the dialog. */
59  	public static final int DIALOG_WIDTH = 640;
60  
61  	/** Height of the dialog. */
62  	public static final int DIALOG_HEIGHT = 200;
63  
64  	/** The width of the key column in the form. */
65  	private static final int KEY_COLUMN_WIDTH = 70;
66  
67  	/** The current projection. */
68  	private final ProjectionModel projection;
69  
70  	/** Flag to determine if the user cancelled the dialog. */
71  	private boolean cancelled;
72  
73  	/** The result filter to be returned. */
74  	private transient IResultFilter resultFilter = new IncludeAllResultFilter();
75  
76  	/** For editing the string to be matched. */
77  	private final JTextField matchString = new JTextField();
78  
79  	/** The 'no filter' option. */
80  	private final JRadioButton noFilter = new JRadioButton("No Filter");
81  	{
82  		noFilter.setSelected(true);
83  	}
84  
85  	/** The 'match set name' option. */
86  	private final JRadioButton matchSetName = new JRadioButton("Match Set Name");
87  
88  	/** The 'match assignment name' option. */
89  	private final JRadioButton matchAssignName = new JRadioButton(
90  	    "Match Assignment Name");
91  
92  	/** The button group for the filter options. */
93  	private final ButtonGroup buttonGroup = new ButtonGroup();
94  	{
95  		buttonGroup.add(noFilter);
96  		buttonGroup.add(matchSetName);
97  		buttonGroup.add(matchAssignName);
98  	}
99  
100 	/**
101 	 * Instantiates a new configure result filter dialog.
102 	 * 
103 	 * @param owner
104 	 *          the owner
105 	 * @param projectionModel
106 	 *          the projection model
107 	 */
108 	public ConfigureResultFilterDialog(Frame owner,
109 	    final ProjectionModel projectionModel) {
110 		super(owner, "Configure Result Filtering", true);
111 		setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
112 		GUI.centerOnScreen(this);
113 		projection = projectionModel;
114 
115 		// Create buttons
116 		JButton finish = new JButton("Finish");
117 		JButton cancel = new JButton("Cancel");
118 
119 		List<JButton> buttons = new ArrayList<JButton>();
120 		buttons.add(cancel);
121 		buttons.add(finish);
122 
123 		PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
124 		    KEY_COLUMN_WIDTH, buttons, 1);
125 
126 		pspf.sep("Filter Setup");
127 		pspf.app("Matching String:", matchString);
128 		final JPanel switchBox = new JPanel();
129 		switchBox.add(noFilter);
130 		switchBox.add(matchSetName);
131 		switchBox.add(matchAssignName);
132 		pspf.app("Filter Scope:", switchBox);
133 
134 		finish.addActionListener(new ActionListener() {
135 			@Override
136 			public void actionPerformed(ActionEvent e) {
137 				configureFilter();
138 				setVisible(false);
139 			}
140 		});
141 
142 		cancel.addActionListener(new ActionListener() {
143 			@Override
144 			public void actionPerformed(ActionEvent e) {
145 				cancelled = true;
146 				setVisible(false);
147 			}
148 		});
149 
150 		getContentPane().add(pspf.constructPanel());
151 	}
152 
153 	/**
154 	 * Configures filter if necessary.
155 	 */
156 	protected void configureFilter() {
157 		if (noFilter.isSelected()) {
158 			return;
159 		}
160 		if (this.matchSetName.isSelected()) {
161 			configureBySetName(matchString.getText());
162 		} else {
163 			configureByAssignmentName(matchString.getText());
164 		}
165 	}
166 
167 	/**
168 	 * Configure result filter by set name.
169 	 * 
170 	 * @param setName
171 	 *          the part of the set name that shall match
172 	 */
173 	private void configureBySetName(String setName) {
174 		int matchingSetCounter = 0;
175 		Set<Integer> permissibleIDs = new HashSet<Integer>();
176 		for (SetType setType : getProjection().getAllSetTypes()) {
177 			for (p3j.pppm.sets.Set set : setType.getSets()) {
178 				if (set.getName().contains(setName)) {
179 					matchingSetCounter++;
180 					Set<ParameterAssignment> assignmentSet = set.getParameterAssignments(
181 					    setType.getDefinedParameters().get(0)).getAssignments();
182 					for (ParameterAssignment assignment : assignmentSet) {
183 						permissibleIDs.add(assignment.getID());
184 					}
185 				}
186 			}
187 		}
188 		resultFilter = new ConfigurableResultFilter(permissibleIDs);
189 		GUI.printMessage(this, "Matching sets", matchingSetCounter
190 		    + " set(s) could be matched.");
191 	}
192 
193 	/**
194 	 * Configures filter by assignment name.
195 	 * 
196 	 * @param assignmentName
197 	 *          the assignment name
198 	 */
199 	private void configureByAssignmentName(String assignmentName) {
200 		Set<Integer> permissibleIDs = new HashSet<Integer>();
201 		for (SetType setType : getProjection().getAllSetTypes()) {
202 			for (ParameterInstance paramInstance : setType.getDefinedParameters()) {
203 				for (p3j.pppm.sets.Set set : setType.getSets()) {
204 					for (ParameterAssignment paramAssignment : set
205 					    .getParameterAssignments(paramInstance).getAssignments()) {
206 						if (paramAssignment.getName().contains(assignmentName)) {
207 							permissibleIDs.add(paramAssignment.getID());
208 						}
209 					}
210 				}
211 			}
212 		}
213 		resultFilter = new ConfigurableResultFilter(permissibleIDs);
214 		GUI.printMessage(this, "Matching assignments", permissibleIDs.size()
215 		    + " assignment(s) could be matched.");
216 	}
217 
218 	/**
219 	 * Checks if the dialog is cancelled.
220 	 * 
221 	 * @return true, if is cancelled
222 	 */
223 	public boolean isCancelled() {
224 		return cancelled;
225 	}
226 
227 	/**
228 	 * Create a result filter as defined by the user.
229 	 * 
230 	 * @return result filter
231 	 */
232 	public IResultFilter getConfiguredResultFilter() {
233 		return resultFilter;
234 	}
235 
236 	public ProjectionModel getProjection() {
237 		return projection;
238 	}
239 
240 }