| /****************************************************************************** |
| * Copyright (c) 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 |
| ******************************************************************************/ |
| |
| package org.eclipse.jst.common.project.facet.ui.libprov; |
| |
| import static org.eclipse.jst.common.project.facet.ui.libprov.LibraryProviderFrameworkUi.createInstallLibraryPanel; |
| import static org.eclipse.wst.common.project.facet.ui.internal.util.GridLayoutUtil.gdhfill; |
| import static org.eclipse.wst.common.project.facet.ui.internal.util.GridLayoutUtil.gl; |
| |
| import java.lang.reflect.InvocationTargetException; |
| |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.IWorkspace; |
| import org.eclipse.core.resources.IWorkspaceRunnable; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.jface.dialogs.Dialog; |
| import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
| import org.eclipse.jface.operation.IRunnableWithProgress; |
| import org.eclipse.jst.common.project.facet.core.libprov.IPropertyChangeListener; |
| import org.eclipse.jst.common.project.facet.core.libprov.LibraryInstallDelegate; |
| import org.eclipse.osgi.util.NLS; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Text; |
| import org.eclipse.ui.dialogs.PropertyPage; |
| import org.eclipse.wst.common.project.facet.core.IFacetedProject; |
| import org.eclipse.wst.common.project.facet.core.IFacetedProjectWorkingCopy; |
| 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; |
| import org.eclipse.wst.common.project.facet.ui.internal.SharedWorkingCopyManager; |
| |
| /** |
| * Base implementation that can be used by those wishing to create a property page for |
| * a facet where the associated libraries can be changed after the facet has been installed. |
| * Other content can be added to the page by overriding the createOtherSettingsControl |
| * method. |
| * |
| * @author <a href="mailto:konstantin.komissarchik@oracle.com">Konstantin Komissarchik</a> |
| * @since WTP 3.1 |
| */ |
| |
| public abstract class FacetLibraryPropertyPage |
| |
| extends PropertyPage |
| |
| { |
| private IProject project; |
| private IFacetedProject facetedProject; |
| private IFacetedProjectWorkingCopy facetedProjectWorkingCopy; |
| private IFacetedProjectListener facetedProjectListener; |
| private Composite rootComposite; |
| private LibraryInstallDelegate libraryInstallDelegate; |
| |
| public abstract IProjectFacetVersion getProjectFacetVersion(); |
| |
| /** |
| * Returns the project that this property page belongs to. |
| * |
| * @return the project that this property page belongs to |
| */ |
| |
| protected final IProject getProject() |
| { |
| return this.project; |
| } |
| |
| /** |
| * Returns the faceted project that this property page belongs to. |
| * |
| * @return the faceted project that this property page belongs to |
| */ |
| |
| protected final IFacetedProject getFacetedProject() |
| { |
| return this.facetedProject; |
| } |
| |
| @Override |
| public void createControl( final Composite parent ) |
| { |
| super.createControl( parent ); |
| getDefaultsButton().setText( Resources.revertButton ); |
| updateButtons(); |
| getShell().layout( true, true ); |
| } |
| |
| protected final Control createContents( final Composite parent ) |
| { |
| final IAdaptable element = getElement(); |
| |
| if( element instanceof IProject ) |
| { |
| this.project = (IProject) element; |
| } |
| |
| if( this.project != null ) |
| { |
| try |
| { |
| this.facetedProject = ProjectFacetsManager.create( this.project ); |
| } |
| catch( CoreException e ) {} |
| } |
| |
| if( this.facetedProject != null ) |
| { |
| this.facetedProjectWorkingCopy = SharedWorkingCopyManager.getWorkingCopy( this.facetedProject ); |
| } |
| |
| Dialog.applyDialogFont( parent ); |
| |
| this.rootComposite = new Composite( parent, SWT.NONE ); |
| |
| this.facetedProjectListener = new IFacetedProjectListener() |
| { |
| public void handleEvent( final IFacetedProjectEvent event ) |
| { |
| updateContents(); |
| } |
| }; |
| |
| this.facetedProject.addListener( this.facetedProjectListener, IFacetedProjectEvent.Type.PROJECT_MODIFIED ); |
| this.facetedProjectWorkingCopy.addListener( this.facetedProjectListener, IFacetedProjectEvent.Type.PROJECT_MODIFIED ); |
| |
| updateContents(); |
| |
| return this.rootComposite; |
| } |
| |
| private void updateContents() |
| { |
| if( this.rootComposite.getDisplay().getThread() != Thread.currentThread() ) |
| { |
| this.rootComposite.getDisplay().asyncExec |
| ( |
| new Runnable() |
| { |
| public void run() |
| { |
| updateContents(); |
| } |
| } |
| ); |
| |
| return; |
| } |
| |
| final IProjectFacetVersion fv = getProjectFacetVersion(); |
| |
| String errorMessage = null; |
| |
| if( this.facetedProject == null ) |
| { |
| errorMessage = Resources.invalidContextMessage; |
| } |
| else if( fv == null || ! this.facetedProject.hasProjectFacet( fv ) ) |
| { |
| errorMessage = Resources.facetNotPresetInProjectMessage; |
| } |
| else if( this.facetedProjectWorkingCopy.isDirty() ) |
| { |
| errorMessage = Resources.dirtyWorkingCopyMessage; |
| } |
| else if( this.libraryInstallDelegate == null ) |
| { |
| for( Control control : this.rootComposite.getChildren() ) |
| { |
| control.dispose(); |
| } |
| |
| this.rootComposite.setLayout( gl( 1, 0, 0 ) ); |
| |
| final IPropertyChangeListener delegateListener = new IPropertyChangeListener() |
| { |
| public void propertyChanged( final String property, |
| final Object oldValue, |
| final Object newValue ) |
| { |
| updateValidation(); |
| } |
| }; |
| |
| this.libraryInstallDelegate = new LibraryInstallDelegate( this.facetedProject, fv ); |
| this.libraryInstallDelegate.addListener( delegateListener ); |
| |
| final Control installLibraryPanel |
| = createInstallLibraryPanel( this.rootComposite, this.libraryInstallDelegate ); |
| |
| installLibraryPanel.setLayoutData( gdhfill() ); |
| |
| final Control otherSettingsControl = createOtherSettingsControl( this.rootComposite ); |
| |
| if( otherSettingsControl != null ) |
| { |
| otherSettingsControl.setLayoutData( gdhfill() ); |
| } |
| } |
| |
| if( errorMessage != null ) |
| { |
| for( Control control : this.rootComposite.getChildren() ) |
| { |
| control.dispose(); |
| } |
| |
| this.libraryInstallDelegate = null; |
| |
| this.rootComposite.setLayout( gl( 1, 0, 0 ) ); |
| |
| final Text label = new Text( this.rootComposite, SWT.WRAP | SWT.READ_ONLY ); |
| label.setText( errorMessage ); |
| label.setLayoutData( gdhfill() ); |
| } |
| |
| updateValidation(); |
| updateButtons(); |
| |
| getShell().layout( true, true ); |
| } |
| |
| private void updateButtons() |
| { |
| boolean enableApply = isValid(); |
| boolean enableRevert = true; |
| |
| if( this.libraryInstallDelegate == null ) |
| { |
| enableApply = false; |
| enableRevert = false; |
| } |
| |
| final Button applyButton = getApplyButton(); |
| |
| if( applyButton != null ) |
| { |
| applyButton.setEnabled( enableApply ); |
| } |
| |
| final Button revertButton = getDefaultsButton(); |
| |
| if( revertButton != null ) |
| { |
| revertButton.setEnabled( enableRevert ); |
| } |
| } |
| |
| private void updateValidation() |
| { |
| if( this.rootComposite.getDisplay().getThread() != Thread.currentThread() ) |
| { |
| this.rootComposite.getDisplay().asyncExec |
| ( |
| new Runnable() |
| { |
| public void run() |
| { |
| updateValidation(); |
| } |
| } |
| ); |
| |
| return; |
| } |
| |
| String message = null; |
| |
| if( this.libraryInstallDelegate != null ) |
| { |
| IStatus status = this.libraryInstallDelegate.validate(); |
| |
| if( status.getSeverity() == IStatus.ERROR ) |
| { |
| message = status.getMessage(); |
| } |
| } |
| |
| if( message == null ) |
| { |
| setMessage( null ); |
| setValid( true ); |
| } |
| else |
| { |
| setMessage( message, ERROR ); |
| setValid( false ); |
| } |
| } |
| |
| protected Control createOtherSettingsControl( final Composite parent ) |
| { |
| return null; |
| } |
| |
| @Override |
| public boolean performOk() |
| { |
| final IWorkspaceRunnable wr = new IWorkspaceRunnable() |
| { |
| public void run( final IProgressMonitor monitor ) |
| |
| throws CoreException |
| |
| { |
| performOkInternal(); |
| } |
| }; |
| |
| final IRunnableWithProgress op = new IRunnableWithProgress() |
| { |
| public void run( final IProgressMonitor monitor ) |
| |
| throws InvocationTargetException, InterruptedException |
| |
| { |
| try |
| { |
| final IWorkspace ws = ResourcesPlugin.getWorkspace(); |
| ws.run( wr, ws.getRoot(), IWorkspace.AVOID_UPDATE, monitor ); |
| } |
| catch( CoreException e ) |
| { |
| throw new InvocationTargetException( e ); |
| } |
| } |
| }; |
| |
| try |
| { |
| new ProgressMonitorDialog( getShell() ).run( true, false, op ); |
| } |
| catch( InterruptedException e ) |
| { |
| return false; |
| } |
| catch( InvocationTargetException e ) |
| { |
| final Throwable te = e.getTargetException(); |
| throw new RuntimeException( te ); |
| } |
| |
| return true; |
| } |
| |
| private void performOkInternal() |
| |
| throws CoreException |
| |
| { |
| if( this.libraryInstallDelegate != null ) |
| { |
| this.libraryInstallDelegate.execute( null ); |
| this.libraryInstallDelegate.reset(); |
| } |
| } |
| |
| @Override |
| protected void performApply() |
| { |
| performOk(); |
| } |
| |
| @Override |
| protected void performDefaults() |
| { |
| if( this.libraryInstallDelegate != null ) |
| { |
| this.libraryInstallDelegate.reset(); |
| } |
| } |
| |
| @Override |
| public void dispose() |
| { |
| super.dispose(); |
| |
| if( this.facetedProject != null ) |
| { |
| this.facetedProject.removeListener( this.facetedProjectListener ); |
| SharedWorkingCopyManager.releaseWorkingCopy( this.facetedProject ); |
| } |
| } |
| |
| private static final class Resources |
| |
| extends NLS |
| |
| { |
| public static String revertButton; |
| public static String invalidContextMessage; |
| public static String facetNotPresetInProjectMessage; |
| public static String dirtyWorkingCopyMessage; |
| |
| static |
| { |
| initializeMessages( FacetLibraryPropertyPage.class.getName(), |
| Resources.class ); |
| } |
| } |
| } |