2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Action delegate to cactify a project.
42   * 
43   * @version $Id: CactifyActionDelegate.java 238816 2004-02-29 16:36:46Z vmassol $
44   */
45  public class CactifyActionDelegate implements IObjectActionDelegate
46  {
47      /**
48       * The project selected by the user
49       */
50      private IJavaProject selectedProject;
51  
52      /**
53       * The active part
54       */
55      private IWorkbenchPart part;
56  
57      /**
58       * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
59       */
60      public void setActivePart(IAction theAction, IWorkbenchPart theTargetPart)
61      {
62          this.part = theTargetPart;
63      }
64  
65      /**
66       * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
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      * @see IActionDelegate#selectionChanged(IAction, ISelection)
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      * Returns a list of entries of jars files contained
145      * in the given directory. 
146      * @param theDirectory the directory to list jars from
147      * @return an array of jar entries
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      * @param theFirstArray an array of classpath entries
166      * @param theSecondArray an array of classpath entries
167      * @return the fusion of the two given arrays 
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 }