package org.eparapher.rcp.actions; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import org.apache.log4j.Logger; import org.eclipse.jface.action.Action; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; import org.eparapher.core.tools.DefaulteParapherParameters; import org.eparapher.core.tools.FileUtil; import org.eparapher.rcp.tools.RCPGUI; import org.eparapher.rcp.views.documents.SecuredDocumentsView.TreeObject; import org.eparapher.rcp.views.documents.SecuredDocumentsView.TreeParent; /** * Generic action * @author Arnault MICHEL * */ public abstract class GenericAction extends Action implements IWorkbenchAction { private static Logger log = Logger.getLogger(GenericAction.class); protected String actionName; protected String[] filesForAction = null; protected TreeViewer viewer; protected boolean usewizard; protected IWorkbenchWindow window; public GenericAction(String msignerName) { this(null,true,msignerName); } public GenericAction(TreeViewer mviewer, boolean museWizard, String msignerName ) { viewer = mviewer; usewizard = museWizard; window = null; actionName=msignerName; buildAction(); } public GenericAction(IWorkbenchWindow mwindow, String msignerName ) { this(msignerName); window=mwindow; } public void dispose() { } protected File[] getFileSelectionFromParameters() { ArrayList fileList = new ArrayList(); for (String filePath : filesForAction) { if ( filePath!=null && !filePath.equals("") && !filePath.startsWith("-")) { File f = new File(filePath); if(f.isDirectory()) { ArrayList list = FileUtil.scanDirectory(filePath); for (File file : list) { if (isFileWithCorrectFormat(file)) fileList.add(file); else { log.debug("[" + actionName + "] Cannot process "+file.getAbsolutePath()+ " (bad format/extention)"); RCPGUI.warnMessage(actionName, actionName + " impossible on " + file.getAbsolutePath()+ " (bad format/extention)"); } } } else { if (!f.exists()) { log.error("file " + filePath + " doesn't exists or has been deleted"); } else { if (isFileWithCorrectFormat(f)) fileList.add(f); else { log.debug("[" + actionName + "] Cannot process "+f.getAbsolutePath()+ " (bad format/extention)"); RCPGUI.warnMessage(actionName, actionName + " impossible on " + f.getAbsolutePath()+ " (bad format/extention)"); } } } } } return fileList.toArray(new File[] {}); } protected File[] getFileSelectionFromView() { ArrayList fileList = new ArrayList(); if (viewer.getSelection()!=null && !viewer.getSelection().isEmpty()) { // Iterate over selected files in view ISelection selection = viewer.getSelection(); IStructuredSelection structselection = (IStructuredSelection) selection; //Build File Array from Table Selection for ( Iterator iterator = structselection.iterator(); iterator.hasNext();) { Object obj = iterator.next(); if (obj instanceof TreeParent) { TreeObject to = (TreeObject) obj; ArrayList list = FileUtil.scanDirectory(to.getFilePath()); for (File file : list) { if (isFileWithCorrectFormat(file)) fileList.add(file); else { log.debug("[" + actionName + "] Cannot process "+file.getAbsolutePath()+ " (bad format/extention)"); RCPGUI.warnMessage(actionName, actionName + " impossible on " + file.getAbsolutePath()+ " (bad format/extention)"); } } } else { TreeObject to = (TreeObject) obj; File f = new File(to.getFilePath()); if (!f.exists()) { log.error("file " + to.getFilePath() + " has been deleted"); } else { if (isFileWithCorrectFormat(f)) fileList.add(f); else { log.debug("[" + actionName + "] Cannot process "+f.getAbsolutePath()+ " (bad format/extention)"); RCPGUI.warnMessage(actionName, actionName + " impossible on " + f.getAbsolutePath()+ " (bad format/extention)"); } } } } } return fileList.toArray(new File[] {}); } abstract void buildAction(); abstract boolean isFileWithCorrectFormat(File file2verify); abstract DefaulteParapherParameters getParameters(); }