/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * 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 com.intellij.ide.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.newvfs.persistent.FSRecords;

public class InvalidateCachesAction extends AnAction implements DumbAware {
  public void actionPerformed(AnActionEvent e) {
    final Application app = ApplicationManager.getApplication();
    final boolean mac = Messages.canShowMacSheetPanel();
    String[] options = new String[3];
    options[0] = app.isRestartCapable() ? "Invalidate and Restart" : "Invalidate and Exit";
    options[1] = mac ? "Cancel" : "Invalidate";
    options[2] = mac ? "Invalidate" : "Cancel";

    int result = Messages.showYesNoCancelDialog(e.getData(CommonDataKeys.PROJECT),
                                     "The caches will be invalidated and rebuilt on the next startup.\n" +
                                     "WARNING: Local History will be also cleared.\n\n" +
                                     "Would you like to continue?\n\n",
                                     "Invalidate Caches",
                                     options[0], options[1], options[2],
                                     Messages.getWarningIcon());

    if (result == -1 || result == (mac ? 1 : 2)) {
      return;
    }
    FSRecords.invalidateCaches();
    if (result == 0) app.restart();
  }
}
