View Javadoc

1   package cz.cuni.amis.pogamut.sposh.elements;
2   
3   import java.awt.datatransfer.DataFlavor;
4   import java.util.List;
5   
6   import cz.cuni.amis.pogamut.sposh.exceptions.CycleException;
7   import cz.cuni.amis.pogamut.sposh.exceptions.DuplicateNameException;
8   import cz.cuni.amis.pogamut.sposh.exceptions.InvalidNameException;
9   
10  public class Adopt extends NamedLapElement<Adopt, PoshPlan> {
11  	
12  	/**
13       * Data flavor of adopt class, used for drag-and-drop
14       */
15      public static final DataFlavor dataFlavor = new DataFlavor(Adopt.class, "adopt-node");
16      
17      /**
18       * Property string of competence name
19       */
20      public static final String adName = "adName";
21  	
22  	private String name;
23  	
24  	private FormalParameters parameters;
25      
26      private Trigger<Adopt> exitCondition;
27      
28      private TriggeredAction adoptedElement;
29  
30      public Adopt(String name, FormalParameters parameters, List<Sense> exitCondition, PrimitiveCall adoptedElement) {
31      	this.name = name;
32      	this.parameters = parameters;
33      	
34      	this.exitCondition =  new Trigger<Adopt>(this, exitCondition);
35      	
36      	this.adoptedElement = LapElementsFactory.createAction(adoptedElement);
37          this.adoptedElement.setParent(this);
38      }
39      
40  	public FormalParameters getParameters() {
41  		return parameters;
42  	}
43  
44  	public void setParameters(FormalParameters parameters) {
45  		this.parameters = parameters;
46  	}
47  
48  	@Override
49  	public String getName() {
50  		return name;
51  	}
52  	
53  	public void setName(String name) throws InvalidNameException, DuplicateNameException, CycleException {
54  		PoshPlan plan = getRootNode();
55  
56          name = name.trim();
57  
58          if (!name.matches(IDENT_PATTERN)) {
59              throw new InvalidNameException("Name " + name + " is not valid.");
60          }
61  
62          // Check for duplicity
63          if (!this.name.equals(name)) {
64              if (plan != null && !plan.isUniqueAPorComp(name)) {
65                  throw new DuplicateNameException("New name for adopt '" + this.name + "'(" + name + ") is not unique for reaction plan.");
66              }
67          }
68  
69          String oldName = this.name;
70          this.name = name;
71  
72          if (plan != null && plan.isCycled()) {
73              this.name = oldName;
74              throw new CycleException("New name (" + name + ") for adopt '" + this.name + "' is causing cycle.");
75          }
76          firePropertyChange(adName, oldName, name);
77  	}
78  
79  	public void addCondition(Sense sense) {
80  		exitCondition.add(sense);
81  	}
82  	
83  	public Trigger<Adopt> getExitCondition() {
84  		return exitCondition;
85  	}
86  
87  	public TriggeredAction getAdoptedElement() {
88  		return adoptedElement;
89  	}
90  
91  	public void setAdoptedElement(TriggeredAction adoptedElement) {
92  		this.adoptedElement = adoptedElement;
93  	}
94  	
95  	@Override
96  	public DataFlavor getDataFlavor() {
97  		return dataFlavor;
98  	}
99      
100     @Override
101     public LapType getType() {
102         return LapType.ADOPT;
103     }
104 	
105 	@Override
106 	public List<? extends PoshElement> getChildDataNodes() {
107 		// TODO: how to implement?
108 		throw new UnsupportedOperationException();
109 	}
110 
111 	@Override
112 	public boolean moveChild(int newIndex, PoshElement child) {
113         throw new UnsupportedOperationException("Moving Adopt doesn't make sense.");
114 	}
115 
116 }