2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Bruno JOFRET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.graphic_objects.graphicController;
15 import java.awt.GraphicsEnvironment;
16 import java.rmi.server.UID;
17 import java.util.Collections;
18 import java.util.ConcurrentModificationException;
19 import java.util.HashSet;
21 import java.util.Vector;
23 import org.scilab.modules.graphic_objects.graphicModel.GraphicModel;
24 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
25 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
26 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject.Type;
27 import org.scilab.modules.graphic_objects.graphicView.FlattenTreeView;
28 import org.scilab.modules.graphic_objects.graphicView.GedTreeView;
29 import org.scilab.modules.graphic_objects.graphicView.GraphicView;
30 import org.scilab.modules.graphic_objects.graphicView.GuiLogView;
31 import org.scilab.modules.graphic_objects.graphicView.LogView;
34 * GraphicController class
35 * @author Bruno JOFRET
37 public class GraphicController {
39 private static boolean MVCViewEnable = false;
40 private static boolean debugEnable = true;
41 private static boolean infoEnable = false;
43 private static void INFO(String message)
45 if (infoEnable == true)
47 System.err.println("[CONTROLLER - INFO] : "+message);
51 private static void DEBUG(String message)
53 if (debugEnable == true) {
54 System.err.println("[CONTROLLER - DEBUG] : "+message);
59 * Set of all views attached to this controller.
61 private volatile static Set<GraphicView> allViews = Collections.synchronizedSet(new HashSet<GraphicView>());
64 * Graphic controller singleton.
66 private static GraphicController me = null;
71 private GraphicController() {
72 if (!GraphicsEnvironment.isHeadless() && MVCViewEnable) {
73 register(GuiLogView.createGuiLogView());
74 register(GedTreeView.create());
75 register(FlattenTreeView.create());
78 register(LogView.createLogView());
83 * Returns the controller
84 * @return the controller
86 public static GraphicController getController() {
88 me = new GraphicController();
95 * Register a view that will receive notification
96 * of any model changes.
97 * @param view The view to register.
99 public void register(GraphicView view) {
100 INFO("Register view : " + view.toString());
106 * @param view The view to unregister.
108 public void unregister(GraphicView view) {
109 INFO("Unregister view : " + view.toString());
110 allViews.remove(view);
115 * @return the created UID
117 public UID createUID() {
122 * Returns the object associated to an id
123 * @param id the object id
126 public GraphicObject getObjectFromId(String id) {
127 return GraphicModel.getModel().getObjectFromId(id);
131 * Fast property set method
132 * @param id the object id
133 * @param prop the property name
134 * @param value the property value
135 * @return true if the property has been set, false otherwise
137 public boolean setProperty(String id, int prop, Object value) {
139 switch (GraphicModel.getModel().setProperty(id, prop, value)) {
140 case Success : // BroadCast Message + return true
141 objectUpdate(id, prop);
143 case NoChange : // Do not broadcast message
150 catch (Exception e) {
151 DEBUG("====== Exception caught ======");
152 DEBUG("setProperty : " + id + " " + prop);
154 DEBUG("====== Exception caught ======");
160 * Fast property get method
161 * @param id the object id
162 * @param prop the property name
163 * @return the property value
165 public Object getProperty(String id, int prop) {
167 return GraphicModel.getModel().getProperty(id, prop);
169 catch (Exception e) {
170 DEBUG("====== Exception caught ======");
171 DEBUG("getProperty : [" + id + "] " + prop);
173 DEBUG("====== Exception caught ======");
179 * Returns a null property
180 * @param id the object id
181 * @param prop the property name
182 * @return the null property
184 public Object getNullProperty(String id, String prop) {
185 return GraphicModel.getModel().getNullProperty(id, prop);
189 * Asks the model to create a new object
190 * @param type the object type
191 * @return the created object's id
193 public String askObject(Type type) {
196 UID id = createUID();
197 GraphicModel.getModel().createObject(id.toString(), type);
198 objectCreated(id.toString());
200 return id.toString();
202 catch (Exception e) {
203 DEBUG("====== Exception caught ======");
204 DEBUG(" askObject type = " + type.name());
206 DEBUG("====== Exception caught ======");
213 * Ask the model to clone an object
214 * @param id : the ID of the object to clone.
215 * @return the id of the clone.
217 public String cloneObject(String id) {
219 UID newId = createUID();
220 GraphicModel.getModel().cloneObject(id, newId.toString());
221 objectCreated(newId.toString());
223 return newId.toString();
225 catch (Exception e) {
226 DEBUG("====== Exception caught ======");
227 DEBUG("cloneObject id = " + id);
229 DEBUG("====== Exception caught ======");
236 * @param id the deleted object's id
238 public void deleteObject(String id) {
241 GraphicModel.getModel().deleteObject(id);
243 catch (Exception e) {
244 DEBUG("====== Exception caught ======");
245 DEBUG("deleteObject id = " + id);
247 DEBUG("====== Exception caught ======");
252 * Notifies the existing views that an object has been created
253 * @param id the created object's id
255 public void objectCreated(final String id) {
256 //INFO("### Create object : "+id);
257 //INFO("### type is : " + getProperty(id, GraphicObjectProperties.__GO_TYPE__));
258 Vector<Runnable> broadCastVector= new Vector<Runnable>();
261 for (final GraphicView view : allViews) {
262 broadCastVector.add(new Runnable() {
264 view.createObject(id);
268 for (final Runnable runMe : broadCastVector) {
271 } catch (ConcurrentModificationException e) {
277 * Notified the existing views that an object has been updated
278 * @param id the updated object's id
279 * @param prop the property that has been updated
281 public void objectUpdate(final String id, final int prop) {
282 //INFO("### Update object : "+id);
283 //INFO("### type is : " + getProperty(id, GraphicObjectProperties.__GO_TYPE__));
284 //INFO("### prop is : " + prop);
286 Vector<Runnable> broadCastVector= new Vector<Runnable>();
288 for (final GraphicView view : allViews) {
289 broadCastVector.add(new Runnable() {
291 view.updateObject(id, prop);
295 for (final Runnable runMe : broadCastVector) {
298 } catch (ConcurrentModificationException e) {
304 * Notified the existing views that an object has been deleted
305 * @param id the deleted object's id
307 public void objectDeleted(final String id) {
308 //INFO("### Delete object : "+id);
309 //INFO("### type is : " + getProperty(id, GraphicObjectProperties.__GO_TYPE__));
310 Vector<Runnable> broadCastVector= new Vector<Runnable>();
313 for (final GraphicView view : allViews) {
314 broadCastVector.add(new Runnable() {
316 view.deleteObject(id);
320 for (final Runnable runMe : broadCastVector) {
323 } catch (ConcurrentModificationException e) {
329 * Set relationship between two object and remove old relationship.
330 * @param parentId id of the parent object.
331 * @param childId id of the child object.
333 public void setGraphicObjectRelationship(String parentId, String childId) {
335 * All the parent and children get/set calls must be performed first,
336 * and only then the corresponding object updates.
338 Object oldParent = getProperty(childId, GraphicObjectProperties.__GO_PARENT__);
340 if (oldParent != null && oldParent instanceof String) {
341 String oldParentId = (String) oldParent;
343 if (oldParentId.equals(parentId)) {
347 if (!oldParentId.equals("")) {
348 getObjectFromId(oldParentId).removeChild(childId);
352 /* Insertion occurs at the head of the children list. */
353 if (parentId != null && !parentId.equals("")) {
354 getObjectFromId(parentId).addChild(childId);
357 setProperty(childId, GraphicObjectProperties.__GO_PARENT__, parentId);
359 /* Object updates can now be performed. */
360 if (oldParent != null && oldParent instanceof String && !((String)oldParent).equals("")) {
361 objectUpdate((String)oldParent, GraphicObjectProperties.__GO_CHILDREN__);
364 if (parentId != null && !parentId.equals("")) {
365 objectUpdate(parentId, GraphicObjectProperties.__GO_CHILDREN__);
368 // Useless (already done in setProperty(childId, GraphicObjectProperties.__GO_PARENT__, parentId);)
369 //objectUpdate(childId, GraphicObjectProperties.__GO_PARENT__);
373 * Remove relationship between given object and is parent.
375 * TODO : Manage children of deleted object.
376 * @param id deleted object identifier.
378 public void removeRelationShipAndDelete(String id) {
379 GraphicObject killMe = getObjectFromId(id);
380 String parentUID = killMe.getParent();
383 /* Remove object from Parent's Children list */
384 if (parentUID != null && !parentUID.equals("")) {
385 getObjectFromId(parentUID).removeChild(id);
386 //setProperty(id, GraphicObjectProperties.__GO_PARENT__, "");
388 objectUpdate(parentUID, GraphicObjectProperties.__GO_CHILDREN__);
389 //objectUpdate(id, GraphicObjectProperties.__GO_PARENT__);
392 killMe.setValid(false);
393 recursiveDeleteChildren(killMe);
398 private void recursiveDeleteChildren(GraphicObject killMe) {
399 String children[] = killMe.getChildren();
401 for (int i = 0 ; i < children.length ; ++i) {
402 GraphicObject killMeThisChild = getObjectFromId(children[i]);
403 killMeThisChild.setValid(false);
404 recursiveDeleteChildren(killMeThisChild);
405 deleteObject(children[i]);