/*******************************************************************************
* Copyright (c) 2006-2010 eBay Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*******************************************************************************/
package org.ebayopensource.turmeric.eclipse.services.ui.actions;
import java.util.ArrayList;
import java.util.List;
import org.ebayopensource.turmeric.eclipse.buildsystem.utils.ActionUtil;
import org.ebayopensource.turmeric.eclipse.core.logging.SOALogger;
import org.ebayopensource.turmeric.eclipse.core.resources.constants.SOAProjectConstants;
import org.ebayopensource.turmeric.eclipse.core.resources.constants.SOAProjectConstants.SupportedProjectType;
import org.ebayopensource.turmeric.eclipse.repositorysystem.core.GlobalRepositorySystem;
import org.ebayopensource.turmeric.eclipse.repositorysystem.core.TrackingEvent;
import org.ebayopensource.turmeric.eclipse.repositorysystem.utils.GlobalProjectHealthChecker;
import org.ebayopensource.turmeric.eclipse.utils.ui.UIUtil;
import org.ebayopensource.turmeric.eclipse.validator.utils.ValidateUtil;
import org.ebayopensource.turmeric.eclipse.validator.utils.common.AbstractBaseAccessValidator;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
/**
* The Class ConfigureService.
*
* @author smathew
*/
public class ConfigureService implements IObjectActionDelegate {
private IStructuredSelection selection;
private static final SOALogger logger = SOALogger.getLogger();
/**
* Gets the property page id.
*
* @param projectType the project type
* @return the property page id
*/
protected String getPropertyPageID(SupportedProjectType projectType){
return SOAProjectConstants.PROP_PAGE_ID_SOA_PROJ;
}
/**
* {@inheritDoc}
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
*/
@Override
public void setActivePart(final IAction action,
final IWorkbenchPart targetPart) {
}
/**
* {@inheritDoc}
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
*/
@Override
public void run(final IAction action) {
if (SOALogger.DEBUG)
logger.entering(action, selection);
try {
if (selection == null)
return;
final IProject project =
ActionUtil.preValidateAction(selection.getFirstElement(), logger);
if (project == null)
return;
final IStatus status = new AbstractBaseAccessValidator() {
@Override
public List<IResource> getReadableFiles() {
//should check the following files
try {
return GlobalProjectHealthChecker.getSOAProjectReadableResources(project);
} catch (Exception e) {
logger.warning(e);
}
return new ArrayList<IResource>(1);
}
@Override
public List<IResource> getWritableFiles() {
//we should ensure that the project is writable
final List<IResource> result = new ArrayList<IResource>();
result.add(GlobalRepositorySystem.instanceOf().getActiveRepositorySystem()
.getAssetRegistry().getProjectConfigurationFile(project));
return result;
}
}.validate(project.getName());
final String messages = ValidateUtil.getFormattedStatusMessagesForAction(status);
if (messages != null) {
UIUtil.showErrorDialog(UIUtil.getActiveShell(), "Error",
messages, (Throwable)null);
return;
}
GlobalRepositorySystem.instanceOf().getActiveRepositorySystem()
.trackingUsage(new TrackingEvent(
getClass().getName(),
TrackingEvent.TRACKING_ACTION));
UIUtil.openPropertyPage(project, getPropertyPageID(
GlobalRepositorySystem.instanceOf().getActiveRepositorySystem().getProjectType(project)));
} catch (Exception e) {
logger.error(e);
UIUtil.showErrorDialog(e);
} finally {
if (SOALogger.DEBUG)
logger.exiting();
}
}
/**
* {@inheritDoc}
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
*/
@Override
public void selectionChanged(final IAction action,
final ISelection selection) {
this.selection = (IStructuredSelection) selection;
}
}