1 | package de.uka.ipd.sdq.cip.runtime.wizards; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | import java.util.List; |
6 | |
7 | import org.eclipse.core.resources.IContainer; |
8 | import org.eclipse.core.resources.IFile; |
9 | import org.eclipse.core.resources.IProject; |
10 | import org.eclipse.core.resources.IResource; |
11 | import org.eclipse.core.resources.IWorkspace; |
12 | import org.eclipse.core.resources.IWorkspaceRoot; |
13 | import org.eclipse.core.resources.ResourcesPlugin; |
14 | import org.eclipse.core.runtime.CoreException; |
15 | import org.eclipse.emf.common.util.URI; |
16 | |
17 | import de.uka.ipd.sdq.cip.completions.RegisteredCompletion; |
18 | import de.uka.ipd.sdq.cip.completions.RegisteredCompletions; |
19 | |
20 | public class CompletionConfigNode { |
21 | |
22 | static public class FeatureConfig { |
23 | |
24 | private IFile file; |
25 | |
26 | public FeatureConfig(IFile file) { |
27 | this.file = file; |
28 | } |
29 | |
30 | public String getName() { |
31 | return file.getName() + " - " + file.getProject().getName(); |
32 | } |
33 | |
34 | public String getFileURI() { |
35 | return URI.createPlatformResourceURI(file.getLocationURI().toString(),true).toString(); |
36 | } |
37 | |
38 | } |
39 | |
40 | static public class UserDefined { |
41 | |
42 | private String caption; |
43 | private UserDefinedID id; |
44 | |
45 | public UserDefined(String caption, UserDefinedID id) { |
46 | this.caption = caption; |
47 | this.id = id; |
48 | } |
49 | |
50 | public String getCaption() { |
51 | return caption; |
52 | } |
53 | public UserDefinedID getID() { |
54 | return id; |
55 | } |
56 | |
57 | |
58 | |
59 | } |
60 | |
61 | public enum CompletionConfigNodeID{ |
62 | WORKSPACE, |
63 | REGISTRED, |
64 | USERDEFINED |
65 | |
66 | }; |
67 | |
68 | public enum UserDefinedID{ |
69 | FEATURECONFIG, |
70 | ANNOTATED, |
71 | PLAIN, |
72 | PLAINQVTO |
73 | |
74 | }; |
75 | |
76 | private CompletionConfigNodeID id; |
77 | private Collection<?> configElements; |
78 | private String caption; |
79 | |
80 | private CompletionConfigNode(CompletionConfigNodeID id, String caption,Collection<?> configElements) { |
81 | this.id = id; |
82 | this.caption = caption; |
83 | this.configElements = configElements; |
84 | } |
85 | |
86 | public static CompletionConfigNode createWorkspaceNode() throws CoreException { |
87 | |
88 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
89 | IWorkspaceRoot workspaceRoot = workspace.getRoot(); |
90 | List<?> featureConfigs = (List<?>) scanFolder(workspaceRoot); |
91 | CompletionConfigNode node = new CompletionConfigNode(CompletionConfigNodeID.WORKSPACE,"Workspace FeatureConfigs",featureConfigs); |
92 | return node; |
93 | } |
94 | |
95 | private static List< FeatureConfig> scanFolder(IContainer container) throws CoreException { |
96 | IResource[] resources = container.members(); |
97 | ArrayList<FeatureConfig> featureConfigs = new ArrayList<FeatureConfig>(); |
98 | for(int i = 0; i < resources.length; i++) { |
99 | if(resources[i].getType() == IResource.PROJECT){ |
100 | IProject project = (IProject) resources[i]; |
101 | if(project.isOpen()) { |
102 | featureConfigs.addAll(scanFolder((IContainer)resources[i])); |
103 | } |
104 | } |
105 | if(resources[i].getType() == IResource.FOLDER) |
106 | featureConfigs.addAll(scanFolder((IContainer)resources[i])); |
107 | else if (resources[i].getType() == IResource.FILE){ |
108 | IFile file = (IFile) resources[i]; |
109 | if(file.getFileExtension() != null && file.getFileExtension().contains("featureconfig")) |
110 | { |
111 | FeatureConfig featureConfig = new FeatureConfig(file); |
112 | featureConfigs.add(featureConfig); |
113 | } |
114 | } |
115 | } |
116 | |
117 | return featureConfigs; |
118 | } |
119 | |
120 | public static CompletionConfigNode createRegisteredNode() { |
121 | Collection<RegisteredCompletion> completions = RegisteredCompletions.getCompletions(); |
122 | for(RegisteredCompletion completion : completions) { |
123 | if(completion.getFeaturemodel() != null) |
124 | completions.remove(completion); |
125 | } |
126 | CompletionConfigNode node = new CompletionConfigNode(CompletionConfigNodeID.REGISTRED,"Registered Completions", completions); |
127 | return node; |
128 | } |
129 | public static CompletionConfigNode createUserDefinedNode() { |
130 | ArrayList<UserDefined> userDefineds = new ArrayList<UserDefined>(); |
131 | userDefineds.add(new UserDefined("FeatureConfig based Completion",UserDefinedID.FEATURECONFIG)); |
132 | userDefineds.add(new UserDefined("Annotated Completion",UserDefinedID.ANNOTATED)); |
133 | userDefineds.add(new UserDefined("Plain Completion",UserDefinedID.PLAIN)); |
134 | userDefineds.add(new UserDefined("Plain QVTO Completion",UserDefinedID.PLAINQVTO)); |
135 | CompletionConfigNode node = new CompletionConfigNode(CompletionConfigNodeID.USERDEFINED,"User-defined", userDefineds); |
136 | return node; |
137 | } |
138 | |
139 | public Object[] getChildren() { |
140 | return configElements.toArray(); |
141 | } |
142 | |
143 | public String getCaption() { |
144 | return caption; |
145 | } |
146 | |
147 | public CompletionConfigNodeID getID() { |
148 | return id; |
149 | } |
150 | } |