Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ebd1d9140bd87441b8ca7c5da2c01b60e2ac60ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/******************************************************************************
 * Copyright (c) 2010 Oracle
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Konstantin Komissarchik - initial implementation and ongoing maintenance
 ******************************************************************************/

package org.eclipse.jst.j2ee.internal.ui;

import static org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.checkForComponentDependencyAttribute;
import static org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.getDefaultRuntimePath;
import static org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.isClassFolderEntry;
import static org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.modifyDependencyPath;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.ui.ISharedImages;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jst.j2ee.classpathdep.IClasspathDependencyConstants.DependencyAttributeType;
import org.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyExtensions;
import org.eclipse.jst.j2ee.internal.plugin.J2EEUIPlugin;
import org.eclipse.jst.j2ee.internal.ui.J2EEModuleDependenciesPropertyPage.ClasspathEntryProxy;
import org.eclipse.jst.j2ee.project.JavaEEProjectUtilities;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.ui.internal.taskwizard.IWizardHandle;
import org.eclipse.wst.common.componentcore.ui.internal.taskwizard.WizardFragment;
import org.eclipse.wst.common.componentcore.ui.propertypage.IReferenceWizardConstants;

@SuppressWarnings( "restriction" )

public final class AddJavaBuildPathEntriesWizardFragment 

    extends WizardFragment 
    
{
    public static final String PROP_SELECTION = "selection"; //$NON-NLS-1$
    
    private IWizardHandle wizard;
    private TreeViewer viewer;
    private IJavaProject jproj;
    
    public AddJavaBuildPathEntriesWizardFragment() 
    {
        setComplete( false );
    }
    
    @Override
    public boolean hasComposite() 
    {
        return true;
    }

    @Override
    public Composite createComposite( final Composite parent, 
                                      final IWizardHandle handle ) 
    {
        this.wizard = handle;
        
        handle.setTitle( Resources.title );
        handle.setDescription( Resources.description );
        handle.setImageDescriptor( JavaPluginImages.DESC_WIZBAN_ADD_LIBRARY );
        
        final Composite composite = new Composite( parent, SWT.NONE );
        composite.setLayout( new GridLayout() );
        
        this.viewer = new TreeViewer( composite, SWT.MULTI | SWT.BORDER );

        final GridData gd = new GridData( GridData.FILL_BOTH );
        gd.widthHint = 390;
        gd.heightHint = 185;
        
        this.viewer.getTree().setLayoutData( gd );
        
        this.viewer.setContentProvider( new BuildPathEntriesContentProvider() );
        this.viewer.setLabelProvider( new BuildPathEntriesLabelProvider() );
        
        this.viewer.addSelectionChangedListener
        (
            new ISelectionChangedListener() 
            {
                public void selectionChanged( final SelectionChangedEvent event ) 
                {
                    handleSelectionChanged();
                }
            }
        );
        
        this.viewer.addDoubleClickListener
        (
            new IDoubleClickListener() 
            {
                public void doubleClick( final DoubleClickEvent event ) 
                {
                    handleDoubleClick();
                }
            }
        );
        
        final IProject proj = (IProject) getTaskModel().getObject( IReferenceWizardConstants.PROJECT );
        this.jproj = JavaCore.create( proj );
        
        this.viewer.setInput( new Object() );

        return composite;
    }
    
    private void handleSelectionChanged() 
    {
        final List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
        final IStructuredSelection selection = (IStructuredSelection) this.viewer.getSelection();
        final IVirtualComponent virtualComponent = ComponentCore.createComponent(this.jproj.getProject());
        final boolean isWebApp = JavaEEProjectUtilities.isDynamicWebProject( this.jproj.getProject() );
        
        for( Iterator<?> itr = selection.iterator(); itr.hasNext(); )
        {
            final Object obj = itr.next();
            
            if( obj instanceof IClasspathEntry )
            {
                final IClasspathEntry cpeOriginal = (IClasspathEntry) obj;
                
                IPath runtimePath = null;
                if(virtualComponent == null){
                	runtimePath = getDefaultRuntimePath( isWebApp, isClassFolderEntry( cpeOriginal ) );
                } else {
                	runtimePath = getDefaultRuntimePath(virtualComponent, cpeOriginal);
                }
                final IClasspathEntry cpeTagged = modifyDependencyPath( cpeOriginal, runtimePath );
                
                list.add( cpeTagged );
            }
            else
            {
                list.clear();
                break;
            }
        }
        
        getTaskModel().putObject( PROP_SELECTION, list );
        
        setComplete( ! list.isEmpty() );
        this.wizard.update();
    }
    
    private void handleDoubleClick()
    {
        advanceToNextPageOrFinish();
    }
    
    private IJavaProject getJavaProject()
    {
        return this.jproj;
    }

    public static String getClasspathEntryText( final IJavaProject jproj,
                                                final IClasspathEntry cpe )
    {
        final int type = cpe.getEntryKind();
        
        if( type == IClasspathEntry.CPE_CONTAINER )
        {
            try
            {
                final IClasspathContainer container = JavaCore.getClasspathContainer( cpe.getPath(), jproj );
                return container.getDescription();
            }
            catch( Exception e )
            {
            	J2EEUIPlugin.logError(e);
            }
        }
        else if( type == IClasspathEntry.CPE_LIBRARY )
        {
            final IPath path = cpe.getPath();
            
            final StringBuilder buf = new StringBuilder();
            buf.append( path.lastSegment() );
            buf.append( " - " ); //$NON-NLS-1$
            buf.append( path.removeLastSegments( 1 ).toOSString() );
            
            return buf.toString();
        }
        
        return cpe.getPath().toPortableString();
    }

    public static Image getClasspathEntryImage( final IJavaProject jproj,
                                                final IClasspathEntry cpe )
    {
        final int type = cpe.getEntryKind();
        final String imgId;
        
        if( type == IClasspathEntry.CPE_CONTAINER )
        {
            imgId = ISharedImages.IMG_OBJS_LIBRARY;
        }
        else if( type == IClasspathEntry.CPE_LIBRARY )
        {
            imgId = ISharedImages.IMG_OBJS_JAR;
        }
        else if( type == IClasspathEntry.CPE_VARIABLE )
        {
            imgId = ISharedImages.IMG_OBJS_CLASSPATH_VAR_ENTRY;
        }
        else
        {
            imgId = null;
        }
        
        return ( imgId == null ? null : JavaUI.getSharedImages().getImage( imgId ) );
    }

    private final class BuildPathEntriesContentProvider
    
        implements ITreeContentProvider
        
    {
        public Object[] getElements( final Object input ) 
        {
            final List<IClasspathEntry> elements = new ArrayList<IClasspathEntry>();
            final List<?> filteredContainerIds = ClasspathDependencyExtensions.get().getFilteredClasspathContainerIDs();
            
            try
            {
                for( IClasspathEntry cpe : getJavaProject().getRawClasspath() )
                {
                    final int type = cpe.getEntryKind();
                    final IPath path = cpe.getPath();
                    boolean relevant = false;
                    
                    if( type == IClasspathEntry.CPE_CONTAINER )
                    {
                        if( path.segmentCount() > 0 && ! filteredContainerIds.contains( path.segment( 0 ) ) )
                        {
                            relevant = true;
                        }
                    }
                    else if( type == IClasspathEntry.CPE_LIBRARY || type == IClasspathEntry.CPE_VARIABLE  )
                    {
                        relevant = true;
                    }
                    
                    if( relevant )
                    {
                        if( checkForComponentDependencyAttribute( cpe, DependencyAttributeType.CLASSPATH_COMPONENT_NONDEPENDENCY ) != null )
                        {
                            relevant = false;
                        }
                    }
                    
                    if( relevant )
                    {
                        boolean found = false;
                        
                        for( Object directive : (List<?>) getTaskModel().getObject( IReferenceWizardConstants.ALL_DIRECTIVES ) )
                        {
                            if( directive instanceof ClasspathEntryProxy )
                            {
                                final ClasspathEntryProxy proxy = (ClasspathEntryProxy) directive;
                                
                                if( type == proxy.entry.getEntryKind() && path.equals( proxy.entry.getPath() ) )
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                        
                        if( found )
                        {
                            relevant = false;
                        }
                    }
                    
                    if( relevant )
                    {
                        elements.add( cpe );
                    }
                }
            }
            catch( CoreException e )
            {
            	J2EEUIPlugin.logError(e);
            }
            
            return elements.toArray();
        }

        public boolean hasChildren( final Object element ) 
        {
            return ( element instanceof IClasspathEntry ) && 
                   ( ( (IClasspathEntry) element ).getEntryKind() == IClasspathEntry.CPE_CONTAINER );
        }
    
        public Object[] getChildren( final Object parent ) 
        {
            if( parent instanceof IClasspathEntry )
            {
                final IClasspathEntry cpe = (IClasspathEntry) parent;
                
                if( cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER )
                {
                    final List<ChildClasspathEntry> children = new ArrayList<ChildClasspathEntry>();
                    
                    try
                    {
                        final IClasspathContainer container = JavaCore.getClasspathContainer( cpe.getPath(), getJavaProject() );
                        
                        if( container != null )
                        {
                            for( IClasspathEntry child : container.getClasspathEntries() )
                            {
                                children.add( new ChildClasspathEntry( cpe, child ) );
                            }
                        }
                    }
                    catch( Exception e )
                    {
                    	J2EEUIPlugin.logError(e);
                    }
                    
                    return children.toArray();
                }
            }
            
            return new Object[ 0 ];
        }

        public Object getParent( final Object element ) 
        {
            if( element instanceof ChildClasspathEntry )
            {
                return ( (ChildClasspathEntry) element ).parent;
            }
            
            return null;
        }

        public void inputChanged( final Viewer viewer, 
                                  final Object oldInput, 
                                  final Object newInput ) 
        {
        }

        public void dispose() 
        {
        }
    }
    
    private final class BuildPathEntriesLabelProvider
    
        extends LabelProvider
        
    {
        @Override
        public String getText( final Object element ) 
        {
            final IClasspathEntry cpe;
            
            if( element instanceof IClasspathEntry )
            {
                cpe = (IClasspathEntry) element;
            }
            else
            {
                cpe = ( (ChildClasspathEntry) element ).entry;
            }
            
            return getClasspathEntryText( getJavaProject(), cpe );
        }

        @Override
        public Image getImage( final Object element ) 
        {
            final IClasspathEntry cpe;
            
            if( element instanceof IClasspathEntry )
            {
                cpe = (IClasspathEntry) element;
            }
            else
            {
                cpe = ( (ChildClasspathEntry) element ).entry;
            }
            
            return getClasspathEntryImage( getJavaProject(), cpe );
        }
    }
    
    private static final class ChildClasspathEntry
    {
        public final IClasspathEntry parent;
        public final IClasspathEntry entry;
        
        public ChildClasspathEntry( final IClasspathEntry parent,
                                    final IClasspathEntry entry )
        {
            this.parent = parent;
            this.entry = entry;
        }
    }
    
    public static final class Resources extends NLS
    {
        public static String title;
        public static String description;
        
        static
        {
            initializeMessages( AddJavaBuildPathEntriesWizardFragment.class.getName(), Resources.class );
        }
    }
    
}

Back to the top