View Javadoc
1   /**
2    * 
3    * Copyright (c) 2014, Openflexo
4    * 
5    * This file is part of Gina-swing, a component of the software infrastructure 
6    * developed at Openflexo.
7    * 
8    * 
9    * Openflexo is dual-licensed under the European Union Public License (EUPL, either 
10   * version 1.1 of the License, or any later version ), which is available at 
11   * https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
12   * and the GNU General Public License (GPL, either version 3 of the License, or any 
13   * later version), which is available at http://www.gnu.org/licenses/gpl.html .
14   * 
15   * You can redistribute it and/or modify under the terms of either of these licenses
16   * 
17   * If you choose to redistribute it and/or modify under the terms of the GNU GPL, you
18   * must include the following additional permission.
19   *
20   *          Additional permission under GNU GPL version 3 section 7
21   *
22   *          If you modify this Program, or any covered work, by linking or 
23   *          combining it with software containing parts covered by the terms 
24   *          of EPL 1.0, the licensors of this Program grant you additional permission
25   *          to convey the resulting work. * 
26   * 
27   * This software is distributed in the hope that it will be useful, but WITHOUT ANY 
28   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
29   * PARTICULAR PURPOSE. 
30   *
31   * See http://www.openflexo.org/license.html for details.
32   * 
33   * 
34   * Please contact Openflexo (openflexo-contacts@openflexo.org)
35   * or visit www.openflexo.org if you need additional information.
36   * 
37   */
38  
39  package org.openflexo.gina.swing.editor.validation;
40  
41  import java.awt.Color;
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.List;
45  import java.util.logging.Logger;
46  
47  import org.openflexo.gina.model.FIBComponent;
48  import org.openflexo.gina.model.FIBModelObject;
49  import org.openflexo.gina.model.FIBValidationReport;
50  import org.openflexo.gina.swing.editor.ComponentSwingEditorFIBController;
51  import org.openflexo.gina.swing.editor.controller.FIBEditorController;
52  import org.openflexo.gina.view.GinaViewFactory;
53  import org.openflexo.pamela.validation.FixProposal;
54  import org.openflexo.pamela.validation.ProblemIssue;
55  import org.openflexo.pamela.validation.Validable;
56  import org.openflexo.pamela.validation.ValidationIssue;
57  import org.openflexo.pamela.validation.ValidationModel;
58  import org.openflexo.pamela.validation.ValidationRule;
59  
60  public class ValidationFIBController extends ComponentSwingEditorFIBController {
61  
62  	static final Logger LOGGER = Logger.getLogger(ValidationFIBController.class.getPackage().getName());
63  
64  	private ValidationIssue<?, ?> selectedValidationIssue;
65  
66  	public ValidationFIBController(FIBComponent rootComponent, GinaViewFactory<?> viewFactory) {
67  		super(rootComponent, viewFactory);
68  	}
69  
70  	public ValidationFIBController(FIBComponent rootComponent) {
71  		super(rootComponent);
72  	}
73  
74  	public ValidationFIBController(FIBComponent rootComponent, FIBEditorController editorController) {
75  		super(rootComponent, editorController);
76  	}
77  
78  	public ValidationIssue<?, ?> getSelectedValidationIssue() {
79  		return selectedValidationIssue;
80  	}
81  
82  	public void setSelectedValidationIssue(ValidationIssue<?, ?> validationIssue) {
83  		if (selectedValidationIssue != validationIssue) {
84  			selectedValidationIssue = validationIssue;
85  			performSelect(validationIssue);
86  		}
87  	}
88  
89  	@Override
90  	public FIBModelObject getSelectedObject() {
91  		FIBModelObject returned = super.getSelectedObject();
92  		if (returned == null) {
93  			return getDataObject();
94  		}
95  		return returned;
96  	}
97  
98  	// To be overriden
99  	protected void performSelect(ValidationIssue<?, ?> validationIssue) {
100 
101 	}
102 
103 	public void checkAgain() throws InterruptedException {
104 		if (getValidationModel() != null && getValidatedObject() != null) {
105 			setDataObject(getValidationModel().validate(getValidatedObject()));
106 		}
107 	}
108 
109 	public void fixIssue(FixProposal<?, ?> fixProposal) {
110 		if (fixProposal != null) {
111 			fixProposal.apply(true);
112 			setDataObject(getDataObject(), true);
113 		}
114 	}
115 
116 	public void fixAllIssues(Collection<ValidationIssue<?, ?>> issues) {
117 		fixIssues(issues, null);
118 	}
119 
120 	public void fixIssues(Collection<ValidationIssue<?, ?>> issues, FixProposal<?, ?> fixProposal) {
121 
122 		if (issues.size() == 1 && fixProposal != null && fixProposal.getProblemIssue() == issues.iterator().next()) {
123 			fixIssue(fixProposal);
124 		}
125 		else {
126 			for (ValidationIssue<?, ?> issue : new ArrayList<>(issues)) {
127 				if (issue instanceof ProblemIssue) {
128 					FixProposal<?, ?> proposal = null;
129 					if (fixProposal != null && fixProposal.getProblemIssue() == issue) {
130 						proposal = fixProposal;
131 					}
132 					else {
133 						List<FixProposal<?, ?>> proposals = ((ProblemIssue) issue).getFixProposals();
134 						if (proposals.size() > 0) {
135 							proposal = proposals.get(0);
136 							if (proposals.size() > 1) {
137 								List<FixProposal<?, ?>> availableProposals = ((ProblemIssue) issue).getFixProposals(fixProposal.getClass());
138 								if (availableProposals.size() > 0) {
139 									proposal = availableProposals.get(0);
140 								}
141 							}
142 						}
143 					}
144 
145 					if (proposal != null) {
146 						fixIssue(proposal);
147 					}
148 				}
149 			}
150 		}
151 	}
152 
153 	public void enableRule(ValidationRule<?, ?> rule) {
154 
155 		System.out.println("enableRule " + rule);
156 
157 		rule.setIsEnabled(true);
158 	}
159 
160 	public void disableRule(ValidationRule<?, ?> rule) {
161 
162 		System.out.println("disableRule " + rule);
163 
164 		if (getDataObject() != null) {
165 			rule.setIsEnabled(false);
166 			FIBValidationReport report = getValidationReport(getDataObject());
167 			List<ValidationIssue<?, ?>> issuesToRemove = report.issuesRegarding(rule);
168 			for (ValidationIssue<?, ?> issue : issuesToRemove) {
169 				issue.delete();
170 			}
171 			report.getPropertyChangeSupport().firePropertyChange("filteredIssues", null, report.getFilteredIssues());
172 		}
173 
174 	}
175 
176 	public Validable getValidatedObject() {
177 		return getDataObject();
178 	}
179 
180 	public ValidationModel getValidationModel() {
181 		return getValidationModel(getDataObject());
182 	}
183 
184 	public Color getValidationRuleColor(ValidationRule<?, ?> rule) {
185 		return (rule.getIsEnabled() ? Color.BLACK : Color.LIGHT_GRAY);
186 	}
187 
188 	@Override
189 	public void setDataObject(Object anObject) {
190 		super.setDataObject(anObject);
191 		getPropertyChangeSupport().firePropertyChange("showErrorsWarnings", !showErrorsWarnings(), showErrorsWarnings());
192 	}
193 
194 	private boolean showErrorsWarnings = true;
195 
196 	public boolean showErrorsWarnings() {
197 		return showErrorsWarnings && getDataObject() != null;
198 	}
199 
200 	public void setShowErrorsWarnings(boolean showErrorsWarnings) {
201 		System.out.println("setShowErrorsWarnings with " + showErrorsWarnings);
202 		if (this.showErrorsWarnings != showErrorsWarnings) {
203 			this.showErrorsWarnings = showErrorsWarnings;
204 			getPropertyChangeSupport().firePropertyChange("showErrorsWarnings", !showErrorsWarnings, showErrorsWarnings);
205 		}
206 	}
207 
208 	public void showIssue(ValidationIssue<?, ?> issue) {
209 		if (issue != null) {
210 			// System.out.println("select issue " + issue);
211 			if (issue.getValidable() instanceof FIBModelObject) {
212 				getEditorController().setSelectedObject((FIBModelObject) issue.getValidable());
213 			}
214 		}
215 	}
216 
217 	public void fixIssue(ValidationIssue<?, ?> issue) {
218 		if (issue instanceof ProblemIssue) {
219 
220 			FIBComponent componentToRevalidate = null;
221 			if (issue.getValidationReport().getRootObject() instanceof FIBComponent) {
222 				componentToRevalidate = (FIBComponent) issue.getValidationReport().getRootObject();
223 			}
224 			IssueFixing<?, ?> fixing = new IssueFixing<>((ProblemIssue<?, ?>) issue, getEditorController());
225 			FixIssueDialog dialog = new FixIssueDialog(fixing, getEditorController());
226 			dialog.showDialog();
227 			if (dialog.getStatus() == Status.VALIDATED) {
228 				fixing.fix();
229 				if (componentToRevalidate != null) {
230 					revalidate(componentToRevalidate);
231 				}
232 			}
233 			else if (dialog.getStatus() == Status.NO) {
234 				fixing.ignore();
235 			}
236 		}
237 	}
238 
239 	public void revalidate(FIBComponent component) {
240 		System.out.println("Tiens, faudrait revalider " + component);
241 		try {
242 			getValidationReport(getDataObject()).revalidate();
243 		} catch (InterruptedException e) {
244 			e.printStackTrace();
245 		}
246 	}
247 
248 }