Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fee9a39879fe90aa6035610a24719118a64e48f9 (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
/******************************************************************************
 * Copyright (c) 2010, 2008 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
 *    Paul Fullbright - [324111] Need better enablement behavior for WTP library providers
 ******************************************************************************/

package org.eclipse.jst.j2ee.internal.common.classpath;

import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jst.common.project.facet.core.libprov.ILibraryProvider;
import org.eclipse.jst.common.project.facet.core.libprov.LibraryProviderFramework;
import org.eclipse.jst.common.project.facet.core.libprov.user.UserLibraryProviderInstallOperationConfig;
import org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil;
import org.eclipse.jst.j2ee.classpathdep.IClasspathDependencyConstants;
import org.eclipse.jst.j2ee.project.facet.IJ2EEFacetConstants;
import org.eclipse.wst.common.project.facet.core.IFacetedProjectBase;
import org.eclipse.wst.common.project.facet.core.IProjectFacet;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.eclipse.wst.common.project.facet.core.events.IFacetedProjectEvent;
import org.eclipse.wst.common.project.facet.core.events.IFacetedProjectListener;

/**
 * @author <a href="mailto:konstantin.komissarchik@oracle.com">Konstantin Komissarchik</a>
 */

@SuppressWarnings( "boxing" )

public class WtpUserLibraryProviderInstallOperationConfig

    extends UserLibraryProviderInstallOperationConfig
    
{
    private static final IProjectFacet WEB_FACET 
        = ProjectFacetsManager.getProjectFacet( IJ2EEFacetConstants.DYNAMIC_WEB );

    private static final String CLASS_NAME 
        = WtpUserLibraryProviderInstallOperationConfig.class.getName();
    
    public static final String PROP_INCLUDE_WITH_APPLICATION_ENABLED 
        = CLASS_NAME + ".INCLUDE_WITH_APPLICATION_ENABLED"; //$NON-NLS-1$

    public static final String PROP_INCLUDE_WITH_APPLICATION_SETTING_ENABLED 
        = CLASS_NAME + ".INCLUDE_WITH_APPLICATION_SETTING_ENABLED"; //$NON-NLS-1$

    private boolean includeWithApplicationEnabled;
    private boolean includeWithApplicationSettingEnabled;
    private IFacetedProjectListener facetedProjectListener;
    
    @Override
    public synchronized void init( final IFacetedProjectBase fproj,
                                   final IProjectFacetVersion fv,
                                   final ILibraryProvider provider )
    {
        super.init( fproj, fv, provider );
        
        this.includeWithApplicationEnabled = hasModuleFacet( fproj );
        
        final IProject project = fproj.getProject();
        
        if( project != null )
        {
            final IProjectFacet f = fv.getProjectFacet();
            
            final ILibraryProvider currentProvider 
                = LibraryProviderFramework.getCurrentProvider( project, f );
            
            if( currentProvider == provider )
            {
                this.includeWithApplicationEnabled = false;
                
                final IJavaProject jproj = JavaCore.create( project );
                
                try
                {
                    for( IClasspathEntry cpe : jproj.getRawClasspath() )
                    {
                        if( cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER )
                        {
                            final IPath path = cpe.getPath();
                            
                            if( path.segmentCount() >= 2 && path.segment( 0 ).equals( JavaCore.USER_LIBRARY_CONTAINER_ID ) )
                            {
                                for( IClasspathAttribute attr : cpe.getExtraAttributes() )
                                {
                                    if( attr.getName().equals( IClasspathDependencyConstants.CLASSPATH_COMPONENT_DEPENDENCY ) )
                                    {
                                        this.includeWithApplicationEnabled = true;
                                        break;
                                    }
                                }
                            }
                        }
                        
                        if( this.includeWithApplicationEnabled )
                        {
                            break;
                        }
                    }
                }
                catch( CoreException e )
                {
                    throw new RuntimeException( e );
                }
            }
        }
        
        this.includeWithApplicationSettingEnabled 
            = ( this.includeWithApplicationEnabled ) ? true : hasModuleFacet( fproj );
        
        this.facetedProjectListener = new IFacetedProjectListener() 
        {
            public void handleEvent( final IFacetedProjectEvent event ) 
            {
                final boolean moduleFaceted = hasModuleFacet( event.getWorkingCopy() );
                setIncludeWithApplicationEnabled( moduleFaceted );
                setIncludeWithApplicationSettingEnabled( moduleFaceted );
            }
        };
        
        fproj.addListener( this.facetedProjectListener, IFacetedProjectEvent.Type.PROJECT_FACETS_CHANGED );
    }

    public boolean isIncludeWithApplicationEnabled()
    {
        return this.includeWithApplicationEnabled;
    }
    
    public void setIncludeWithApplicationEnabled( final boolean value )
    {
        final boolean oldValue = this.includeWithApplicationEnabled;
        this.includeWithApplicationEnabled = value;
        notifyListeners( PROP_INCLUDE_WITH_APPLICATION_ENABLED, oldValue, this.includeWithApplicationEnabled );
    }

    public boolean isIncludeWithApplicationSettingEnabled()
    {
        return this.includeWithApplicationSettingEnabled;
    }
    
    public void setIncludeWithApplicationSettingEnabled( final boolean value )
    {
        final boolean oldValue = this.includeWithApplicationSettingEnabled;
        this.includeWithApplicationSettingEnabled = value;
        notifyListeners( PROP_INCLUDE_WITH_APPLICATION_SETTING_ENABLED, oldValue, this.includeWithApplicationSettingEnabled );
    }

    public IClasspathAttribute[] getClasspathAttributes()
    {
        final IFacetedProjectBase fproj = getFacetedProject();
        final boolean isWebProject = fproj.hasProjectFacet( WEB_FACET );
        
        IClasspathAttribute attr = null;
        
        if ( isIncludeWithApplicationSettingEnabled() )
        {
            if( isIncludeWithApplicationEnabled() )
            {
                attr = JavaCore.newClasspathAttribute( IClasspathDependencyConstants.CLASSPATH_COMPONENT_DEPENDENCY,
                                                       ClasspathDependencyUtil.getDefaultRuntimePath( isWebProject ).toString() );
            }
            else
            {
                attr = JavaCore.newClasspathAttribute( IClasspathDependencyConstants.CLASSPATH_COMPONENT_NON_DEPENDENCY, "" ); //$NON-NLS-1$
            }
        }
        
        return ( attr == null ? null : new IClasspathAttribute[] { attr } );
    }
    
    private static boolean hasModuleFacet( final IFacetedProjectBase fproj ) 
    {
        final Set<IProjectFacetVersion> moduleFacets = ProjectFacetsManager.getGroup( "modules" ).getMembers(); //$NON-NLS-1$
        
        for( IProjectFacetVersion facetVersion : fproj.getProjectFacets() ) 
        {
            if( moduleFacets.contains( facetVersion ) )
            {
                return true;
            }
        }
        
        return false;
    }
    
    @Override
    public void dispose()
    {
        super.dispose();
        getFacetedProject().removeListener( this.facetedProjectListener );
    }
    
}

Back to the top