/*
 * 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.openapi.wm.impl.status;

import com.intellij.icons.AllIcons;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.StatusBarWidget;
import com.intellij.ui.UIBundle;
import com.intellij.util.Consumer;
import com.intellij.util.io.ReadOnlyAttributeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.io.IOException;

public class ToggleReadOnlyAttributePanel implements StatusBarWidget.Multiframe, StatusBarWidget.IconPresentation {

  private StatusBar myStatusBar;

  @NotNull
  public Icon getIcon() {
    final Editor editor = getEditor();
    return editor == null || editor.getDocument().isWritable() ? AllIcons.Ide.Readwrite : AllIcons.Ide.Readonly;
  }

  @NotNull
  public String ID() {
    return "ReadOnlyAttribute";
  }


  @Override
  public StatusBarWidget copy() {
    return new ToggleReadOnlyAttributePanel();
  }

  public WidgetPresentation getPresentation(@NotNull PlatformType type) {
    return this;
  }

  public void dispose() {
    myStatusBar = null;
  }

  public void install(@NotNull StatusBar statusBar) {
    myStatusBar = statusBar;
  }

  public String getTooltipText() {
    return isReadonlyApplicable() ? UIBundle.message("read.only.attr.panel.double.click.to.toggle.attr.tooltip.text") : null;
  }

  public Consumer<MouseEvent> getClickConsumer() {
    return new Consumer<MouseEvent>() {
      public void consume(MouseEvent mouseEvent) {
        final Project project = getProject();
        if (project == null) {
          return;
        }
        final FileEditorManager editorManager = FileEditorManager.getInstance(project);
        final VirtualFile[] files = editorManager.getSelectedFiles();
        if (!isReadOnlyApplicableForFiles(files)) {
          return;
        }
        FileDocumentManager.getInstance().saveAllDocuments();

        ApplicationManager.getApplication().runWriteAction(new Runnable() {
          public void run() {
            try {
              ReadOnlyAttributeUtil.setReadOnlyAttribute(files[0], files[0].isWritable());
              myStatusBar.updateWidget(ID());
            }
            catch (IOException e) {
              Messages.showMessageDialog(project, e.getMessage(), UIBundle.message("error.dialog.title"), Messages.getErrorIcon());
            }
          }
        });
      }
    };
  }

  private boolean isReadonlyApplicable() {
    final Project project = getProject();
    if (project == null) return false;
    final FileEditorManager editorManager = FileEditorManager.getInstance(project);
    if (editorManager == null) return false;
    VirtualFile[] selectedFiles = editorManager.getSelectedFiles();
    return isReadOnlyApplicableForFiles(selectedFiles);
  }

  private static boolean isReadOnlyApplicableForFiles(final VirtualFile[] files) {
    return files.length > 0 && !files[0].getFileSystem().isReadOnly();
  }

  @Nullable
  private Editor getEditor() {
    final Project project = getProject();
    if (project != null) {
      final FileEditorManager manager = FileEditorManager.getInstance(project);
      return manager.getSelectedTextEditor();
    }

    return null;
  }

  @Nullable
  private Project getProject() {
    return PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext((JComponent) myStatusBar));
  }
}

@@ -836,51 +836,8 @@
 er;%0A
-import com.intellij.openapi.editor.Editor;%0A
 impo
@@ -1657,92 +1657,376 @@
     
-final Editor editor = getEditor();%0A    return editor == null %7C%7C editor.getDocument()
+Project project = getProject();%0A    VirtualFile virtualFile = null;%0A    if (project != null) %7B%0A      FileEditorManager editorManager = FileEditorManager.getInstance(project);%0A      VirtualFile%5B%5D selectedFiles = editorManager.getSelectedFiles();%0A      virtualFile = selectedFiles.length %3E 0 ? selectedFiles%5B0%5D : null;%0A    %7D%0A    return virtualFile == null %7C%7C virtualFile
 .isW
@@ -4259,275 +4259,8 @@
  %7D%0A%0A
-  @Nullable%0A  private Editor getEditor() %7B%0A    final Project project = getProject();%0A    if (project != null) %7B%0A      final FileEditorManager manager = FileEditorManager.getInstance(project);%0A      return manager.getSelectedTextEditor();%0A    %7D%0A%0A    return null;%0A  %7D%0A%0A
   @N

fix icon for non-text files and/or editors