// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.debug.ui.actions;

import java.util.List;

import org.chromium.debug.core.ChromiumDebugPlugin;
import org.chromium.debug.core.util.ChromiumDebugPluginUtil;
import org.chromium.debug.core.util.ScriptTargetMapping;
import org.eclipse.core.resources.IFile;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPart;

/**
 * A base class for all LiveEdit actions that are scoped to a working file from user workspace.
 * It makes all necessary checks and prepares data in form of {@link ScriptTargetMapping} class.
 * The concrete actions implement the {@link #execute(List, Shell)} method.
 */
public abstract class V8ScriptAction extends FileBasedAction.Single<IFile> {

  public V8ScriptAction() {
    super(false, JS_FILE_NAME_FILTER);
  }

  @Override
  protected ActionRunnable createRunnable(final IFile file) {
    return new ActionRunnable() {
      public void adjustAction() {
      }
      public void run(Shell shell, IWorkbenchPart workbenchPart) {
        List<? extends ScriptTargetMapping> filePairList =
            ChromiumDebugPlugin.getScriptTargetMapping(file);
        execute(filePairList, shell, workbenchPart);
      }
    };
  }

  protected abstract void execute(List<? extends ScriptTargetMapping> filePairList, Shell shell,
      IWorkbenchPart workbenchPart);

  static final FileBasedAction.FileFilter<IFile> JS_FILE_NAME_FILTER =
      new FileBasedAction.FileFilter<IFile>() {
    @Override
    IFile accept(IFile file) {
      if (filterFileName(file.getName())) {
        return file;
      } else {
        return null;
      }
    }

    /**
     * @return true if action should be enabled for this file name
     */
    private boolean filterFileName(String name) {
      for (String suffix : ChromiumDebugPluginUtil.SUPPORTED_EXTENSIONS_SUFFIX_LIST) {
        if (name.endsWith(suffix)) {
          return true;
        }
      }
      return false;
    }
  };
}


@@ -438,16 +438,61 @@
 .IFile;%0A
+import org.eclipse.core.resources.IResource;%0A
 import o
@@ -558,16 +558,47 @@
 nchPart;
+%0Aimport org.eclipse.ui.ide.IDE;
 %0A%0A/**%0A *
@@ -1232,24 +1232,150 @@
 enchPart) %7B%0A
+        boolean saved = IDE.saveAllEditors(new IResource%5B%5D %7B file %7D, true);%0A        if (!saved) %7B%0A          return;%0A        %7D%0A
         List

Save files before live edit