2011/08/05 - Jakarta Cactus has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.cactus.eclipse.runner.ui;
22
23 import java.io.File;
24 import java.util.Vector;
25
26 import org.apache.cactus.eclipse.runner.common.JarFilenameFilter;
27 import org.eclipse.core.internal.resources.Project;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.Platform;
30 import org.eclipse.jdt.core.IClasspathEntry;
31 import org.eclipse.jdt.core.IJavaProject;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jface.action.IAction;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.ui.IActionDelegate;
37 import org.eclipse.ui.IObjectActionDelegate;
38 import org.eclipse.ui.IWorkbenchPart;
39
40
41
42
43
44
45 public class CactifyActionDelegate implements IObjectActionDelegate
46 {
47
48
49
50 private IJavaProject selectedProject;
51
52
53
54
55 private IWorkbenchPart part;
56
57
58
59
60 public void setActivePart(IAction theAction, IWorkbenchPart theTargetPart)
61 {
62 this.part = theTargetPart;
63 }
64
65
66
67
68 public void run(IAction theAction)
69 {
70 if (selectedProject != null)
71 {
72 CactusPlugin thePlugin = CactusPlugin.getDefault();
73 File commonLibDir = null;
74 File clientLibDir = null;
75
76 try {
77 commonLibDir = new File(Platform.asLocalURL(thePlugin.getBundle().getEntry("/" +CactusPlugin.CACTUS_LIBRARY_PATH +"/" +CactusPlugin.CACTUS_COMMON_LIBRARY_PATH)).getPath());
78 clientLibDir = new File(Platform.asLocalURL(thePlugin.getBundle().getEntry("/" +CactusPlugin.CACTUS_LIBRARY_PATH +"/" +CactusPlugin.CACTUS_CLIENT_LIBRARY_PATH)).getPath());
79 } catch (Exception ex) {
80 CactusPlugin.log(ex);
81 }
82
83 IClasspathEntry[] commonEntries =
84 getLibClassPathEntries(commonLibDir);
85 IClasspathEntry[] clientEntries =
86 getLibClassPathEntries(clientLibDir);
87 IClasspathEntry[] allNewEntries =
88 new IClasspathEntry[commonEntries.length
89 + clientEntries.length];
90 System.arraycopy(
91 commonEntries,
92 0,
93 allNewEntries,
94 0,
95 commonEntries.length);
96 System.arraycopy(
97 clientEntries,
98 0,
99 allNewEntries,
100 commonEntries.length,
101 clientEntries.length);
102 try
103 {
104
105 IClasspathEntry[] existingEntries =
106 selectedProject.getRawClasspath();
107 selectedProject.setRawClasspath(
108 merge(existingEntries, allNewEntries),
109 null);
110 }
111 catch (Exception e)
112 {
113 CactusPlugin.displayErrorMessage(
114 CactusMessages.getString("Cactify.message.error"),
115 e.getMessage(),
116 null);
117 }
118 }
119 }
120
121
122
123
124 public void selectionChanged(IAction theAction, ISelection theSelection)
125 {
126 selectedProject = null;
127 if (theSelection instanceof IStructuredSelection)
128 {
129 IStructuredSelection structuredSelection =
130 (IStructuredSelection) theSelection;
131 if (structuredSelection.size() == 1)
132 {
133 Object selectedResource = structuredSelection.getFirstElement();
134 if (selectedResource instanceof org.eclipse.core.internal.resources.Project)
135 {
136 Project project = (Project) selectedResource;
137 selectedProject = JavaCore.create(project);
138 }
139 }
140 }
141 }
142
143
144
145
146
147
148
149 private IClasspathEntry[] getLibClassPathEntries(File theDirectory)
150 {
151 File[] jarFiles = theDirectory.listFiles(new JarFilenameFilter());
152 IClasspathEntry[] result = new IClasspathEntry[jarFiles.length];
153 for (int i = 0; i < jarFiles.length; i++)
154 {
155 File currentJarFile = jarFiles[i];
156 result[i] =
157 JavaCore.newLibraryEntry(
158 new Path(currentJarFile.getAbsolutePath()),
159 null,
160 null);
161 }
162 return result;
163 }
164
165
166
167
168
169 private static IClasspathEntry[] merge(
170 IClasspathEntry[] theFirstArray,
171 IClasspathEntry[] theSecondArray)
172 {
173
174 Vector result = new Vector();
175 for (int i = 0; i < theFirstArray.length; i++)
176 {
177 result.add(theFirstArray[i]);
178 }
179 for (int i = 0; i < theSecondArray.length; i++)
180 {
181 IClasspathEntry currentEntry = theSecondArray[i];
182 String currentEntryFileName = currentEntry.getPath().toFile().getName();
183 boolean entryAlreadyExists = false;
184 boolean isFile = false;
185 for (int j = 0; j < theFirstArray.length; j++)
186 {
187 IClasspathEntry comparedEntry = theFirstArray[j];
188 isFile = comparedEntry.getPath().toFile().getAbsolutePath().endsWith(".jar");
189 String comparedFileName = comparedEntry.getPath().toFile().getName();
190 if (comparedEntry.getPath().equals(currentEntry.getPath()) || (comparedFileName.equals(currentEntryFileName) && isFile))
191 {
192 entryAlreadyExists = true;
193 break;
194 }
195 }
196 if (!entryAlreadyExists)
197 {
198 result.add(currentEntry);
199 }
200 }
201 return (IClasspathEntry[]) result.toArray(
202 new IClasspathEntry[result.size()]);
203 }
204
205 public void setSelectedProject(IJavaProject selectedProject) {
206 this.selectedProject = selectedProject;
207 }
208 }