001 /*--------------------------------------------------------------------------+
002 $Id: SimulinkElementBase.java 26285 2010-02-18 11:22:54Z juergens $
003 | |
004 | Copyright 2005-2010 Technische Universitaet Muenchen |
005 | |
006 | Licensed under the Apache License, Version 2.0 (the "License"); |
007 | you may not use this file except in compliance with the License. |
008 | You may obtain a copy of the License at |
009 | |
010 | http://www.apache.org/licenses/LICENSE-2.0 |
011 | |
012 | Unless required by applicable law or agreed to in writing, software |
013 | distributed under the License is distributed on an "AS IS" BASIS, |
014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015 | See the License for the specific language governing permissions and |
016 | limitations under the License. |
017 +--------------------------------------------------------------------------*/
018 package edu.tum.cs.simulink.model;
019
020 import edu.tum.cs.commons.assertion.CCSMPre;
021 import edu.tum.cs.commons.assertion.PreconditionException;
022 import edu.tum.cs.commons.clone.IDeepCloneable;
023 import edu.tum.cs.simulink.util.SimulinkUtils;
024
025 /**
026 * Base class for Simulink elements. This is either a {@link SimulinkAnnotation}
027 * or a {@link SimulinkBlock}. The common aspect is that they have a name and a
028 * parent.
029 *
030 * @author deissenb
031 * @author $Author: juergens $
032 * @version $Rev: 26285 $
033 * @levd.rating GREEN Hash: DF68AF5E130219CB5B908EAEB9AC1DAB
034 */
035 public abstract class SimulinkElementBase extends ParameterizedElement
036 implements IDeepCloneable {
037
038 /** The parent of this block. */
039 private SimulinkBlock parent;
040
041 /** Create element. */
042 protected SimulinkElementBase() {
043 super();
044 }
045
046 /**
047 * Create element from other element (for deep cloning).
048 */
049 protected SimulinkElementBase(SimulinkElementBase other) {
050 super(other);
051 }
052
053 /**
054 * Get id of this element.
055 */
056 public String getId() {
057 if (parent != null) {
058 return parent.getId() + "/" + SimulinkUtils.escape(getName());
059 }
060 return SimulinkUtils.escape(getName());
061 }
062
063 /** Get the model this element belongs to. */
064 public SimulinkModel getModel() {
065 if (parent == null) {
066 return null;
067 }
068 return parent.getModel();
069 }
070
071 /** Returns the name. */
072 public String getName() {
073 return getParameter(SimulinkConstants.PARAM_Name);
074 }
075
076 /** Returns the parent block (may be null). */
077 public SimulinkBlock getParent() {
078 return parent;
079 }
080
081 /** Remove this element from the model. */
082 public void remove() {
083 if (parent != null) {
084 parent.removeElement(this);
085 parent = null;
086 }
087 }
088
089 /** Get string representation of this block. */
090 @Override
091 public String toString() {
092 return getName();
093 }
094
095 /**
096 * Sets the parent for this block.
097 *
098 * @throws PreconditionException
099 * if element already has parent or the new parent is
100 * <code>null</code>.
101 */
102 protected void setParent(SimulinkBlock parent) {
103 CCSMPre.isTrue(this.parent == null, "Element already has a parent!");
104 CCSMPre.isFalse(parent == null, "Parent cannot be null!");
105 this.parent = parent;
106 }
107 }