/** * Copyright 2010 The University of North Carolina at Chapel Hill * * 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 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package unc.lib.cdr.workbench.commands; import java.util.Iterator; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.jobs.Job; 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; import unc.lib.cdr.workbench.project.MetsProjectNature; import unc.lib.cdr.workbench.stage.StagingJob; public class AutoStageAction implements IObjectActionDelegate { ISelection selection = null; public AutoStageAction() { } /* * (non-Javadoc) * * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */ @Override public void run(IAction action) { if (selection instanceof IStructuredSelection) { for (Iterator it = ((IStructuredSelection) selection).iterator(); it.hasNext();) { Object element = it.next(); IProject project = null; if (element instanceof IProject) { project = (IProject) element; } else if (element instanceof IAdaptable) { project = (IProject) ((IAdaptable) element).getAdapter(IProject.class); } if (project != null && project.isOpen()) { try { toggleStaging(project); } catch (CoreException e) { e.printStackTrace(); } } } } } /** * @param project */ private void toggleStaging(IProject project) throws CoreException { MetsProjectNature mpn = (MetsProjectNature)project.getNature(MetsProjectNature.NATURE_ID); boolean setting = mpn.getAutomaticStaging(); mpn.setAutomaticStaging(!setting); System.out.println("toggled auto staging, it now says "+mpn.getAutomaticStaging()); if(mpn.getAutomaticStaging()) { Job stagingJob = new StagingJob("Staging after autostage enabled", project); stagingJob.schedule(); } } /* * (non-Javadoc) * * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, * org.eclipse.jface.viewers.ISelection) */ @Override public void selectionChanged(IAction action, ISelection selection) { this.selection = selection; } /* * (non-Javadoc) * * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, * org.eclipse.ui.IWorkbenchPart) */ @Override public void setActivePart(IAction action, IWorkbenchPart targetPart) { selectionChanged(action, targetPart.getSite().getPage().getSelection()); if (selection instanceof IStructuredSelection) { for (Iterator it = ((IStructuredSelection) selection).iterator(); it.hasNext();) { Object element = it.next(); IProject project = null; if (element instanceof IProject) { project = (IProject) element; } else if (element instanceof IAdaptable) { project = (IProject) ((IAdaptable) element).getAdapter(IProject.class); } if (project != null && project.isOpen()) { try { MetsProjectNature mpn = (MetsProjectNature)project.getNature(MetsProjectNature.NATURE_ID); boolean status = mpn.getAutomaticStaging(); action.setChecked(new Boolean(status)); } catch (CoreException e) { e.printStackTrace(); } } } } } }