/** * Update the appearance (icons and ports) of the entity with the change in a * GTIngredientAttribute, such as a criterion or an operation. * * @param entity The entity. * @param attribute The attribute whose recent change leads to an update of the entity. */ public static void updateAppearance(final GTEntity entity, GTIngredientsAttribute attribute) { NamedObj object = (NamedObj) entity; Workspace workspace = object.workspace(); try { workspace.getWriteAccess(); List<?> icons = object.attributeList(EditorIcon.class); boolean foundPersistentIcon = false; for (Object iconObject : icons) { EditorIcon icon = (EditorIcon) iconObject; if (icon.isPersistent()) { foundPersistentIcon = true; break; } } Set<String> preservedPortNames = new HashSet<String>(); boolean isIconSet = false; int i = 1; GTIngredientList list = attribute.getIngredientList(); for (GTIngredient ingredient : list) { if (ingredient instanceof PortCriterion) { PortCriterion criterion = (PortCriterion) ingredient; String portID = criterion.getPortID(list); preservedPortNames.add(portID); TypedIOPort port = (TypedIOPort) ((ComponentEntity) entity).getPort(portID); boolean isInput = criterion.isInput(); boolean isOutput = criterion.isOutput(); boolean isMultiport = !criterion.isMultiportEnabled() || criterion.isMultiport(); if (port != null) { if (port instanceof PortMatcher) { port.setInput(isInput); port.setOutput(isOutput); } else { MoMLChangeRequest request = new MoMLChangeRequest( entity, object, "<deletePort name=\"" + port.getName() + "\"/>"); request.setUndoable(true); request.setMergeWithPreviousUndo(true); request.execute(); port = new PortMatcher(criterion, (ComponentEntity) entity, portID, isInput, isOutput); port.setDerivedLevel(1); } } else { port = new PortMatcher(criterion, (ComponentEntity) entity, portID, isInput, isOutput); port.setDerivedLevel(1); } port.setMultiport(isMultiport); } else if (ingredient instanceof SubclassCriterion && !isIconSet && !foundPersistentIcon) { SubclassCriterion criterion = (SubclassCriterion) ingredient; final String superclass = criterion.getSuperclass(); object.requestChange( new ChangeRequest(entity, "Deferred load actor icon action.") { protected void _execute() throws Exception { _loadActorIcon(entity, superclass); } }); isIconSet = true; } i++; } if (!isIconSet && !foundPersistentIcon) { object.requestChange(new RestoreAppearanceChangeRequest(entity)); } ComponentEntity component = (ComponentEntity) entity; try { component.workspace().getReadAccess(); List<?> portList = new LinkedList<Object>(component.portList()); for (i = 0; i < portList.size(); i++) { Port port = (Port) portList.get(i); if (port instanceof PortMatcher && !preservedPortNames.contains(port.getName())) { ((PortMatcher) port)._setPortCriterion(null); port.setContainer(null); } } } finally { component.workspace().doneReading(); } } catch (KernelException e) { throw new KernelRuntimeException( e, "Cannot update appearance for " + "actor " + entity.getName() + "."); } finally { workspace.doneWriting(); } }
/** * React to a list of objects being dropped onto a target. * * @param target The target on which the objects are dropped. * @param dropObjects The list of objects dropped onto the target. * @param moml The moml string generated for the dropped objects. * @exception IllegalActionException If the handling is unsuccessful. */ public void dropObject(NamedObj target, List dropObjects, String moml) throws IllegalActionException { boolean merge = false; if (target instanceof State) { State state = (State) target; TreeMap<Class<? extends Entity>, String> map = _getRefinementClasses(); String refinementClass = null; boolean conflict = false; RefinementSuggestion suggestion = (RefinementSuggestion) target.getAttribute("refinementSuggestion"); for (Object dropObject : dropObjects) { if (suggestion != null) { String className = suggestion.getRefinementClass((NamedObj) dropObject); if (refinementClass == null) { refinementClass = className; } else if (!refinementClass.equals(className)) { conflict = true; } } else { for (Class<? extends Entity> keyClass : map.keySet()) { if (keyClass.isInstance(dropObject)) { String value = map.get(keyClass); if (refinementClass == null) { refinementClass = value; break; } else if (!refinementClass.equals(value)) { conflict = true; break; } } } } if (conflict) { break; } } if (conflict || refinementClass == null) { throw new IllegalActionException( this, "Unable to determine " + "the type of all the dropped objects."); } TypedActor[] refinements = state.getRefinement(); TypedActor refinement = null; if (refinements != null) { for (TypedActor actor : refinements) { if (((NamedObj) actor).getClassName().equals(refinementClass)) { refinement = actor; break; } } } if (refinement == null) { CompositeEntity containerContainer = (CompositeEntity) state.getContainer().getContainer(); String name = containerContainer.uniqueName(state.getName()); addRefinement(state, name, null, refinementClass, null); merge = true; refinement = (TypedActor) containerContainer.getEntity(name); } target = (NamedObj) refinement; } MoMLChangeRequest request = new MoMLChangeRequest(this, target, moml); request.setUndoable(true); if (merge) { request.setMergeWithPreviousUndo(true); } target.requestChange(request); }