View Javadoc
1   /**
2    * 
3    * Copyright (c) 2014, Openflexo
4    * 
5    * This file is part of Gina-swing-editor, 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;
40  
41  import java.awt.Component;
42  import java.util.logging.Logger;
43  
44  import org.openflexo.gina.controller.FIBController;
45  import org.openflexo.gina.model.FIBComponent;
46  import org.openflexo.gina.model.FIBModelObject;
47  import org.openflexo.gina.model.FIBMouseEvent;
48  import org.openflexo.gina.model.FIBValidationReport;
49  import org.openflexo.gina.model.graph.FIBDiscreteFunction;
50  import org.openflexo.gina.model.graph.FIBGraph;
51  import org.openflexo.gina.model.graph.FIBGraphFunction;
52  import org.openflexo.gina.model.graph.FIBNumericFunction;
53  import org.openflexo.gina.model.widget.FIBBrowser;
54  import org.openflexo.gina.model.widget.FIBBrowserAction;
55  import org.openflexo.gina.model.widget.FIBBrowserDragOperation;
56  import org.openflexo.gina.model.widget.FIBBrowserElement;
57  import org.openflexo.gina.model.widget.FIBBrowserElementChildren;
58  import org.openflexo.gina.model.widget.FIBButtonColumn;
59  import org.openflexo.gina.model.widget.FIBCheckBoxColumn;
60  import org.openflexo.gina.model.widget.FIBCustomColumn;
61  import org.openflexo.gina.model.widget.FIBDropDownColumn;
62  import org.openflexo.gina.model.widget.FIBIconColumn;
63  import org.openflexo.gina.model.widget.FIBLabelColumn;
64  import org.openflexo.gina.model.widget.FIBNumberColumn;
65  import org.openflexo.gina.model.widget.FIBTable;
66  import org.openflexo.gina.model.widget.FIBTableAction;
67  import org.openflexo.gina.model.widget.FIBTableAction.FIBAddAction;
68  import org.openflexo.gina.model.widget.FIBTableAction.FIBCustomAction;
69  import org.openflexo.gina.model.widget.FIBTableAction.FIBRemoveAction;
70  import org.openflexo.gina.model.widget.FIBTableColumn;
71  import org.openflexo.gina.model.widget.FIBTextFieldColumn;
72  import org.openflexo.gina.swing.editor.controller.FIBEditorController;
73  import org.openflexo.gina.swing.view.SwingViewFactory;
74  import org.openflexo.gina.view.GinaViewFactory;
75  import org.openflexo.pamela.model.ModelEntity;
76  import org.openflexo.toolbox.StringUtils;
77  
78  /**
79   * A {@link FIBController} used in GINA SwingEditor, and addressing a single {@link FIBComponent}
80   * 
81   * @author sylvain
82   *
83   * @param <T>
84   *            type of data object beeing managed by this controller
85   */
86  public class ComponentSwingEditorFIBController extends SwingEditorFIBController<FIBComponent> {
87  
88  	private static final Logger logger = Logger.getLogger(ComponentSwingEditorFIBController.class.getPackage().getName());
89  
90  	private FIBEditorController editorController;
91  
92  	public ComponentSwingEditorFIBController(FIBComponent rootComponent) {
93  		super(rootComponent);
94  	}
95  
96  	public ComponentSwingEditorFIBController(FIBComponent rootComponent, GinaViewFactory<?> viewFactory) {
97  		super(rootComponent);
98  	}
99  
100 	public ComponentSwingEditorFIBController(FIBComponent rootComponent, FIBEditorController editorController) {
101 		this(rootComponent, SwingViewFactory.INSTANCE);
102 		setEditorController(editorController);
103 	}
104 
105 	public FIBEditorController getEditorController() {
106 		return editorController;
107 	}
108 
109 	public void setEditorController(FIBEditorController editorController) {
110 		this.editorController = editorController;
111 		getPropertyChangeSupport().firePropertyChange("editorController", null, editorController);
112 		getPropertyChangeSupport().firePropertyChange("selectedObject", null, getSelectedObject());
113 		getPropertyChangeSupport().firePropertyChange("selectedComponent", null, getSelectedComponent());
114 	}
115 
116 	@Override
117 	public FIBValidationReport getValidationReport(FIBModelObject object) {
118 		if (getEditorController() != null) {
119 			return getEditorController().getValidationReport();
120 		}
121 		return null;
122 	}
123 
124 	@Override
125 	public FIBModelObject getSelectedObject() {
126 		if (editorController != null) {
127 			return editorController.getSelectedObject();
128 		}
129 		return super.getSelectedObject();
130 	}
131 
132 	@Override
133 	public void setSelectedObject(FIBModelObject selectedObject) {
134 		if (editorController != null) {
135 			if (editorController.getSelectedObject() != selectedObject) {
136 				Object oldValue = editorController.getSelectedObject();
137 				editorController.setSelectedObject(selectedObject);
138 				getPropertyChangeSupport().firePropertyChange("selectedObject", oldValue, selectedObject);
139 			}
140 		}
141 		else {
142 			super.setSelectedObject(selectedObject);
143 		}
144 	}
145 
146 	public FIBComponent getSelectedComponent() {
147 		if (getSelectedObject() != null) {
148 			return getSelectedObject().getComponent();
149 		}
150 		return null;
151 	}
152 
153 	public void setSelectedComponent(FIBComponent selectedComponent) {
154 		setSelectedObject(selectedComponent);
155 	}
156 
157 	public String textFor(FIBComponent component) {
158 		if (component == null) {
159 			return null;
160 		}
161 
162 		ModelEntity<?> e = component.getModelFactory().getModelEntityForInstance(component);
163 
164 		if (StringUtils.isNotEmpty(component.getName())) {
165 			return component.getName();
166 		}
167 		else {
168 			return "<" + e.getImplementedInterface().getSimpleName() + ">";
169 		}
170 	}
171 
172 	public void rightClick(FIBModelObject component, FIBMouseEvent event) {
173 		System.out.println("rightClick with " + component + " event=" + event);
174 		editorController.getContextualMenu().displayPopupMenu(component,
175 				/*((JFIBView<?, ?>) getRootView()).getJComponent()*/(Component) event.getSource(), event.getPoint());
176 	}
177 
178 	public FIBBrowserElement createElement(FIBBrowser browser) {
179 		logger.info("Called createElement()");
180 		FIBBrowserElement newElement = editorController.getFactory().newInstance(FIBBrowserElement.class);
181 		newElement.setName("element" + (browser.getElements().size() > 0 ? browser.getElements().size() : ""));
182 		browser.addToElements(newElement);
183 		return newElement;
184 	}
185 
186 	public FIBBrowserElement deleteElement(FIBBrowserElement elementToDelete) {
187 		logger.info("Called elementToDelete() with " + elementToDelete);
188 		if (elementToDelete.getOwner() != null) {
189 			elementToDelete.getOwner().removeFromElements(elementToDelete);
190 			elementToDelete.delete();
191 		}
192 		return elementToDelete;
193 	}
194 
195 	public org.openflexo.gina.model.widget.FIBBrowserAction.FIBAddAction createBrowserAddAction(FIBBrowserElement element) {
196 		org.openflexo.gina.model.widget.FIBBrowserAction.FIBAddAction newAction = editorController.getFactory()
197 				.newInstance(org.openflexo.gina.model.widget.FIBBrowserAction.FIBAddAction.class);
198 		newAction.setName("add_action");
199 		element.addToActions(newAction);
200 		return newAction;
201 	}
202 
203 	public org.openflexo.gina.model.widget.FIBBrowserAction.FIBRemoveAction createBrowserRemoveAction(FIBBrowserElement element) {
204 		org.openflexo.gina.model.widget.FIBBrowserAction.FIBRemoveAction newAction = editorController.getFactory()
205 				.newInstance(org.openflexo.gina.model.widget.FIBBrowserAction.FIBRemoveAction.class);
206 		newAction.setName("delete_action");
207 		element.addToActions(newAction);
208 		return newAction;
209 	}
210 
211 	public org.openflexo.gina.model.widget.FIBBrowserAction.FIBCustomAction createBrowserCustomAction(FIBBrowserElement element) {
212 		org.openflexo.gina.model.widget.FIBBrowserAction.FIBCustomAction newAction = editorController.getFactory()
213 				.newInstance(org.openflexo.gina.model.widget.FIBBrowserAction.FIBCustomAction.class);
214 		newAction.setName("custom_action");
215 		element.addToActions(newAction);
216 		return newAction;
217 	}
218 
219 	public FIBBrowserAction deleteBrowserAction(FIBBrowserAction actionToDelete) {
220 		logger.info("Called deleteBrowserAction() with " + actionToDelete);
221 		if (actionToDelete.getOwner() != null) {
222 			actionToDelete.getOwner().removeFromActions(actionToDelete);
223 			actionToDelete.delete();
224 		}
225 		return actionToDelete;
226 	}
227 
228 	public FIBBrowserDragOperation createBrowserDragOperation(FIBBrowserElement element) {
229 		FIBBrowserDragOperation newOperation = editorController.getFactory().newInstance(FIBBrowserDragOperation.class);
230 		newOperation.setName("drag_operation");
231 		element.addToDragOperations(newOperation);
232 		return newOperation;
233 	}
234 
235 	public FIBBrowserDragOperation deleteBrowserDragOperation(FIBBrowserDragOperation operationToDelete) {
236 		logger.info("Called deleteBrowserDragOperation() with " + operationToDelete);
237 		if (operationToDelete.getOwner() != null) {
238 			operationToDelete.getOwner().removeFromDragOperations(operationToDelete);
239 			operationToDelete.delete();
240 		}
241 		return operationToDelete;
242 	}
243 
244 	public FIBBrowserElementChildren createChildren(FIBBrowserElement element) {
245 		logger.info("Called createChildren()");
246 		FIBBrowserElementChildren newChildren = editorController.getFactory().newInstance(FIBBrowserElementChildren.class);
247 		newChildren.setName("children" + (element.getChildren().size() > 0 ? element.getChildren().size() : ""));
248 		element.addToChildren(newChildren);
249 		return newChildren;
250 	}
251 
252 	public FIBBrowserElementChildren deleteChildren(FIBBrowserElementChildren childrenToDelete) {
253 		logger.info("Called elementToDelete() with " + childrenToDelete);
254 		if (childrenToDelete.getOwner() != null) {
255 			childrenToDelete.getOwner().removeFromChildren(childrenToDelete);
256 			childrenToDelete.delete();
257 		}
258 		return childrenToDelete;
259 	}
260 
261 	public FIBLabelColumn createLabelColumn(FIBTable table) {
262 		FIBLabelColumn newColumn = editorController.getFactory().newInstance(FIBLabelColumn.class);
263 		newColumn.setName("label");
264 		newColumn.setTitle("label");
265 		table.addToColumns(newColumn);
266 		return newColumn;
267 	}
268 
269 	public FIBTextFieldColumn createTextFieldColumn(FIBTable table) {
270 		FIBTextFieldColumn newColumn = editorController.getFactory().newInstance(FIBTextFieldColumn.class);
271 		newColumn.setName("textfield");
272 		newColumn.setTitle("textfield");
273 		table.addToColumns(newColumn);
274 		return newColumn;
275 	}
276 
277 	public FIBCheckBoxColumn createCheckBoxColumn(FIBTable table) {
278 		FIBCheckBoxColumn newColumn = editorController.getFactory().newInstance(FIBCheckBoxColumn.class);
279 		newColumn.setName("checkbox");
280 		newColumn.setTitle("checkbox");
281 		table.addToColumns(newColumn);
282 		return newColumn;
283 	}
284 
285 	public FIBDropDownColumn createDropDownColumn(FIBTable table) {
286 		FIBDropDownColumn newColumn = editorController.getFactory().newInstance(FIBDropDownColumn.class);
287 		newColumn.setName("dropdown");
288 		newColumn.setTitle("dropdown");
289 		table.addToColumns(newColumn);
290 		return newColumn;
291 	}
292 
293 	public FIBNumberColumn createNumberColumn(FIBTable table) {
294 		FIBNumberColumn newColumn = editorController.getFactory().newInstance(FIBNumberColumn.class);
295 		newColumn.setName("number");
296 		newColumn.setTitle("number");
297 		table.addToColumns(newColumn);
298 		return newColumn;
299 	}
300 
301 	public FIBIconColumn createIconColumn(FIBTable table) {
302 		FIBIconColumn newColumn = editorController.getFactory().newInstance(FIBIconColumn.class);
303 		newColumn.setName("icon");
304 		newColumn.setTitle("icon");
305 		table.addToColumns(newColumn);
306 		return newColumn;
307 	}
308 
309 	public FIBCustomColumn createCustomColumn(FIBTable table) {
310 		FIBCustomColumn newColumn = editorController.getFactory().newInstance(FIBCustomColumn.class);
311 		newColumn.setName("custom");
312 		newColumn.setTitle("custom");
313 		table.addToColumns(newColumn);
314 		return newColumn;
315 	}
316 
317 	public FIBButtonColumn createButtonColumn(FIBTable table) {
318 		FIBButtonColumn newColumn = editorController.getFactory().newInstance(FIBButtonColumn.class);
319 		newColumn.setName("button");
320 		newColumn.setTitle("button");
321 		table.addToColumns(newColumn);
322 		return newColumn;
323 	}
324 
325 	public FIBTableColumn deleteColumn(FIBTableColumn columnToDelete) {
326 		logger.info("Called deleteColumn() with " + columnToDelete);
327 		if (columnToDelete.getOwner() != null) {
328 			columnToDelete.getOwner().removeFromColumns(columnToDelete);
329 			columnToDelete.delete();
330 		}
331 		return columnToDelete;
332 	}
333 
334 	public FIBAddAction createAddAction(FIBTable table) {
335 		System.out.println("create add action");
336 		FIBAddAction newAction = editorController.getFactory().newInstance(FIBAddAction.class);
337 		newAction.setName("add_action");
338 		table.addToActions(newAction);
339 		return newAction;
340 	}
341 
342 	public FIBRemoveAction createRemoveAction(FIBTable table) {
343 		FIBRemoveAction newAction = editorController.getFactory().newInstance(FIBRemoveAction.class);
344 		newAction.setName("delete_action");
345 		table.addToActions(newAction);
346 		return newAction;
347 	}
348 
349 	public FIBCustomAction createCustomAction(FIBTable table) {
350 		FIBCustomAction newAction = editorController.getFactory().newInstance(FIBCustomAction.class);
351 		newAction.setName("custom_action");
352 		table.addToActions(newAction);
353 		return newAction;
354 	}
355 
356 	public FIBTableAction deleteAction(FIBTableAction actionToDelete) {
357 		logger.info("Called deleteAction() with " + actionToDelete);
358 		if (actionToDelete.getOwner() != null) {
359 			actionToDelete.getOwner().removeFromActions(actionToDelete);
360 			actionToDelete.delete();
361 		}
362 		return actionToDelete;
363 	}
364 
365 	public void moveToTop(FIBTableColumn c) {
366 		if (c == null || c.getOwner() == null) {
367 			return;
368 		}
369 		c.getOwner().getColumns().remove(c);
370 		c.getOwner().getColumns().add(0, c);
371 		c.getOwner().getPropertyChangeSupport().firePropertyChange(FIBTable.COLUMNS_KEY, null, c.getOwner().getColumns());
372 	}
373 
374 	public void moveUp(FIBTableColumn c) {
375 		if (c == null || c.getOwner() == null) {
376 			return;
377 		}
378 		int index = c.getOwner().getColumns().indexOf(c);
379 		c.getOwner().getColumns().remove(c);
380 		c.getOwner().getColumns().add(index - 1, c);
381 		c.getOwner().getPropertyChangeSupport().firePropertyChange(FIBTable.COLUMNS_KEY, null, c.getOwner().getColumns());
382 	}
383 
384 	public void moveDown(FIBTableColumn c) {
385 		if (c == null || c.getOwner() == null) {
386 			return;
387 		}
388 		int index = c.getOwner().getColumns().indexOf(c);
389 		c.getOwner().getColumns().remove(c);
390 		c.getOwner().getColumns().add(index + 1, c);
391 		c.getOwner().getPropertyChangeSupport().firePropertyChange(FIBTable.COLUMNS_KEY, null, c.getOwner().getColumns());
392 	}
393 
394 	public void moveToBottom(FIBTableColumn c) {
395 		if (c == null || c.getOwner() == null) {
396 			return;
397 		}
398 		c.getOwner().getColumns().remove(c);
399 		c.getOwner().getColumns().add(c);
400 		c.getOwner().getPropertyChangeSupport().firePropertyChange(FIBTable.COLUMNS_KEY, null, c.getOwner().getColumns());
401 	}
402 
403 	public FIBNumericFunction createNumericFunction(FIBGraph graph) {
404 		return editorController.getFactory().newFIBNumericFunction(graph);
405 	}
406 
407 	public FIBDiscreteFunction createDiscreteFunction(FIBGraph graph) {
408 		return editorController.getFactory().newFIBDiscreteFunction(graph);
409 	}
410 
411 	public FIBGraphFunction deleteFunction(FIBGraphFunction function) {
412 		if (function.getOwner() != null) {
413 			function.getOwner().removeFromFunctions(function);
414 			function.delete();
415 		}
416 		return function;
417 	}
418 }