KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > JonasProjectExtraResourcesPropertyPage


1 package com.bull.eclipse.jonas;
2
3 /*
4  * (c) Copyright Emmanuel Rias, Bull SA 2003.
5  * All Rights Reserved.
6  */

7  
8 import java.util.ArrayList JavaDoc;
9
10 import org.eclipse.core.resources.IResource;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IPath;
13 import org.eclipse.jdt.core.IClasspathEntry;
14 import org.eclipse.jdt.core.IJavaProject;
15 import org.eclipse.jdt.core.JavaModelException;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.List;
21
22 import com.bull.eclipse.jonas.editors.ClasspathFieldEditor;
23
24 /**
25  * This class adds a resources list to lib Web App directory.
26  *
27  * The selection is read and written to the file ".extraresources" in the
28  * root of the project.
29  *
30  * @version 1.0
31  * @author Emmanuel Rias (emmanuel.rias@bull.net)
32  */

33
34 public class JonasProjectExtraResourcesPropertyPage {
35     private static final String JavaDoc WEBAPP_EXTRA_RESOURCES_FILENAME = ".extraresources";
36     private static final String JavaDoc EXTRA_RESOURCES_CLASSPATH = "extraResources";
37     
38     private ClasspathFieldEditor erList;
39     private ExtraResourcesEntries entries;
40     private Composite group;
41     
42     private JonasProjectPropertyPage page;
43     
44     
45     public JonasProjectExtraResourcesPropertyPage(JonasProjectPropertyPage page) {
46         this.page = page;
47     }
48     
49     public IJavaProject getJavaProject() {
50         try {
51             return page.getJavaProject();
52         } catch (CoreException e) {
53             JonasLauncherPlugin.log(e);
54             return null;
55         }
56     }
57     
58     /** okay has been pressed */
59     public boolean performOk() {
60         List JavaDoc newSelection = erList.getListControl(group);
61         
62         try {
63             page.getJonasProject().setExtraResourcesEntries(new ExtraResourcesEntries(newSelection.getItems(),newSelection.getItemCount()));
64             page.getJonasProject().saveProperties();
65         } catch(Exception JavaDoc ex) {
66             JonasLauncherPlugin.log(ex);
67             return false;
68         }
69
70
71         return true;
72     }
73
74     public Control getControl(Composite ctrl) {
75         group = new Composite(ctrl, SWT.NONE);
76         GridLayout layout = new GridLayout();
77         layout.numColumns = 2;
78         group.setLayout(layout);
79         java.util.List JavaDoc listExRes = readSelectedEntries();
80         if (listExRes != null)
81             erList = new ClasspathFieldEditor(
82                         JonasLauncherPlugin.JONAS_EXTRA_RESOURCES_KEY,
83                         JonasPluginResources.JONAS_EXTRA_RESOURCES_LABEL,
84                         group,
85                         listExRes);
86         else
87             erList = new ClasspathFieldEditor(
88                         JonasLauncherPlugin.JONAS_EXTRA_RESOURCES_KEY,
89                         JonasPluginResources.JONAS_EXTRA_RESOURCES_LABEL,
90                         group);
91                                 
92         erList.load();
93         return group;
94
95     }
96     
97     public void getExtraResourcesEntries(IJavaProject prj, ArrayList JavaDoc data) {
98         
99         IClasspathEntry[] entries = null;
100         
101         try {
102             add(data, prj.getProject().getWorkspace().getRoot().findMember(prj.getOutputLocation()));
103             entries = prj.getResolvedClasspath(false);
104         } catch(JavaModelException e) {
105             JonasLauncherPlugin.log(e);
106         }
107         if (entries == null) return;
108         for (int i = 0; i < entries.length; i++) {
109             IClasspathEntry entry = entries[i];
110             if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
111                 String JavaDoc prjName = entry.getPath().lastSegment();
112                 getExtraResourcesEntries(getJavaProject().getJavaModel().getJavaProject(prjName), data);
113             } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
114                 IResource res = prj.getProject().getWorkspace().getRoot().findMember(entry.getPath());
115                 if (res != null)
116                     add(data, res);
117                 else
118                     add(data, entry.getPath());
119             } else if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
120                 add(data, entry.getPath());
121             }
122         }
123     }
124     
125     private void add(ArrayList JavaDoc data, IPath entry) {
126         if (entry.isAbsolute() == false) entry = entry.makeAbsolute();
127         if (!data.contains(entry.toFile().toString())) {
128             data.add(entry.toFile().toString());
129         }
130     }
131     private void add(ArrayList JavaDoc data, IResource con) {
132         if (con == null) return;
133         add(data, con.getLocation());
134     }
135     
136     /** reads the selected entries from persistent storage */
137     private java.util.List JavaDoc readSelectedEntries() {
138         
139         java.util.List JavaDoc listExtraRes = null;
140         try {
141             ExtraResourcesEntries exResEntr = page.getJonasProject().getExtraResourcesEntries();
142             if (exResEntr != null)
143                 listExtraRes = exResEntr.getList();
144         } catch(Exception JavaDoc ex) {
145             JonasLauncherPlugin.log(ex);
146         }
147         return listExtraRes;
148     }
149     
150 }
151
Popular Tags