Reworked some parts of the MachineContext to make its status clear.
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / nature / ProjectContextEvent.java
1 package net.mograsim.plugin.nature;
2
3 import java.util.Optional;
4
5 import org.eclipse.core.resources.IProject;
6 import org.eclipse.core.resources.IResourceChangeEvent;
7
8 public class ProjectContextEvent
9 {
10         private final MachineContext machineContext;
11         private final ProjectContextEventType eventType;
12
13         public ProjectContextEvent(MachineContext machineContext, ProjectContextEventType eventType)
14         {
15                 this.machineContext = machineContext;
16                 this.eventType = eventType;
17         }
18
19         public final MachineContext getMachineContext()
20         {
21                 return machineContext;
22         }
23
24         public final ProjectContextEventType getEventType()
25         {
26                 return eventType;
27         }
28
29         public final IProject getProject()
30         {
31                 return machineContext.getProject();
32         }
33
34         public enum ProjectContextEventType
35         {
36                 NEW, MACHINE_DEFINITION_CHANGE, OTHER_CHANGE, REFRESH, CLOSE, DELETE;
37
38                 static ProjectContextEventType ofResourceChangeEvent(int id)
39                 {
40                         switch (id)
41                         {
42                         case IResourceChangeEvent.POST_CHANGE:
43                                 return OTHER_CHANGE;
44                         case IResourceChangeEvent.PRE_CLOSE:
45                                 return CLOSE;
46                         case IResourceChangeEvent.PRE_DELETE:
47                                 return DELETE;
48                         case IResourceChangeEvent.PRE_REFRESH:
49                                 return REFRESH;
50                         default:
51                                 return null;
52                         }
53                 }
54
55                 Optional<MachineContextStatus> getForcedStatus()
56                 {
57                         switch (this)
58                         {
59                         case CLOSE:
60                                 return Optional.of(MachineContextStatus.CLOSED);
61                         case DELETE:
62                                 return Optional.of(MachineContextStatus.DEAD);
63                         default:
64                                 return Optional.empty();
65                         }
66                 }
67         }
68 }