KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > browsing > LogicalPackagesProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.browsing;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.jface.util.IPropertyChangeListener;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 import org.eclipse.jface.viewers.StructuredViewer;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.jface.viewers.Viewer;
25
26 import org.eclipse.jdt.core.ElementChangedEvent;
27 import org.eclipse.jdt.core.IElementChangedListener;
28 import org.eclipse.jdt.core.IJavaElement;
29 import org.eclipse.jdt.core.IJavaElementDelta;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.core.IPackageFragment;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.core.JavaModelException;
34
35 import org.eclipse.jdt.internal.ui.JavaPlugin;
36
37 abstract class LogicalPackagesProvider implements IPropertyChangeListener, IElementChangedListener {
38
39     protected static final Object JavaDoc[] NO_CHILDREN= new Object JavaDoc[0];
40
41     protected Map JavaDoc fMapToLogicalPackage;
42     protected Map JavaDoc fMapToPackageFragments;
43     protected boolean fCompoundState;
44     protected StructuredViewer fViewer;
45     protected boolean fInputIsProject;
46
47     public LogicalPackagesProvider(StructuredViewer viewer){
48         fViewer= viewer;
49         fCompoundState= isInCompoundState();
50         fInputIsProject= true;
51         fMapToLogicalPackage= new HashMap JavaDoc();
52         fMapToPackageFragments= new HashMap JavaDoc();
53         JavaPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
54     }
55
56     /**
57      * Adds the given fragments to the internal map.
58      * Existing fragments will be replaced by the new ones.
59      *
60      * @param packageFragments the package fragments to add
61      */

62     protected void addFragmentsToMap(IPackageFragment[] packageFragments) {
63         for (int i= 0; i < packageFragments.length; i++) {
64             IPackageFragment fragment= packageFragments[i];
65             String JavaDoc key= getKey(fragment);
66             fMapToPackageFragments.put(key, fragment);
67         }
68     }
69
70     protected String JavaDoc getKey(IPackageFragment fragment) {
71         return fragment.getElementName() + fragment.getJavaProject().getElementName();
72     }
73
74     /**
75      * Returns the logical package for the given package fragment
76      * or <code>null</code> if it is not grouped by logical package.
77      *
78      * @param fragment the package fragment
79      * @return the logical package
80      */

81     public LogicalPackage findLogicalPackage(IPackageFragment fragment) {
82         Assert.isNotNull(fragment);
83         if (isInCompoundState())
84             return (LogicalPackage)fMapToLogicalPackage.get(getKey(fragment));
85         else
86             return null;
87     }
88
89     /**
90      * Combines packages with same names into a logical package which will
91      * be added to the resulting array. If a package is not yet in this content
92      * provider then the package fragment is added to the resulting array.
93      *
94      * @param packageFragments the package fragments to combine
95      * @return an array with combined (logical) packages and package fragments
96      */

97     protected Object JavaDoc[] combineSamePackagesIntoLogialPackages(IPackageFragment[] packageFragments) {
98
99         if (!fCompoundState)
100             return packageFragments;
101
102         List JavaDoc newChildren= new ArrayList JavaDoc();
103
104         for (int i= 0; i < packageFragments.length; i++) {
105             IPackageFragment fragment= packageFragments[i];
106
107             if (fragment == null)
108                 continue;
109
110             LogicalPackage lp= findLogicalPackage(fragment);
111
112             if (lp != null) {
113                 if (lp.belongs(fragment)) {
114                     lp.add(fragment);
115                 }
116                 if(!newChildren.contains(lp))
117                     newChildren.add(lp);
118
119             } else {
120                 String JavaDoc key= getKey(fragment);
121                 IPackageFragment frag= (IPackageFragment)fMapToPackageFragments.get(key);
122                 if (frag != null && !fragment.equals(frag)) {
123                     lp= new LogicalPackage(frag);
124                     lp.add(fragment);
125                     newChildren.remove(frag);
126                     newChildren.add(lp);
127                     fMapToLogicalPackage.put(key, lp);
128                     fMapToPackageFragments.remove(frag);
129                 } else {
130                     fMapToPackageFragments.put(key, fragment);
131                     newChildren.add(fragment);
132                 }
133             }
134         }
135         return newChildren.toArray();
136     }
137
138     /**
139      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
140      */

141     public void propertyChange(PropertyChangeEvent event) {
142         if (fCompoundState == isInCompoundState())
143             return;
144         else
145             fCompoundState= isInCompoundState();
146
147         if (!isInCompoundState()) {
148             fMapToLogicalPackage.clear();
149             fMapToPackageFragments.clear();
150         }
151
152         if(fViewer instanceof TreeViewer){
153             TreeViewer viewer= (TreeViewer) fViewer;
154             Object JavaDoc[] expandedObjects= viewer.getExpandedElements();
155             viewer.refresh();
156             viewer.setExpandedElements(expandedObjects);
157         } else
158             fViewer.refresh();
159     }
160
161     protected boolean isInCompoundState() {
162         // XXX: for now we don't offer a preference might become a view menu entry
163
// return AppearancePreferencePage.logicalPackagesInPackagesView();
164
return true;
165     }
166
167     public void dispose(){
168         JavaPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
169         fMapToLogicalPackage= null;
170         fMapToPackageFragments= null;
171     }
172
173     /*
174      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
175      */

176     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
177         if (newInput != null) {
178             JavaCore.addElementChangedListener(this);
179         } else {
180             JavaCore.removeElementChangedListener(this);
181         }
182         fInputIsProject= (newInput instanceof IJavaProject);
183
184         if(viewer instanceof StructuredViewer)
185             fViewer= (StructuredViewer)viewer;
186     }
187
188     abstract protected void processDelta(IJavaElementDelta delta) throws JavaModelException;
189
190     /*
191      * @since 3.0
192      */

193     protected boolean isClassPathChange(IJavaElementDelta delta) {
194
195         // need to test the flags only for package fragment roots
196
if (delta.getElement().getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT)
197             return false;
198
199         int flags= delta.getFlags();
200         return (delta.getKind() == IJavaElementDelta.CHANGED &&
201             ((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) != 0) ||
202              ((flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0) ||
203              ((flags & IJavaElementDelta.F_REORDER) != 0));
204     }
205
206     /*
207      * @see org.eclipse.jdt.core.IElementChangedListener#elementChanged(org.eclipse.jdt.core.ElementChangedEvent)
208      */

209     public void elementChanged(ElementChangedEvent event) {
210         try {
211             processDelta(event.getDelta());
212         } catch (JavaModelException e) {
213             JavaPlugin.log(e);
214         }
215     }
216
217 }
218
Popular Tags