2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Created by IntelliJ IDEA.
23 package com
.intellij
.openapi
.vcs
.changes
;
25 import com
.intellij
.AppTopics
;
26 import com
.intellij
.openapi
.application
.ApplicationManager
;
27 import com
.intellij
.openapi
.components
.ApplicationComponent
;
28 import com
.intellij
.openapi
.editor
.Document
;
29 import com
.intellij
.openapi
.fileEditor
.*;
30 import com
.intellij
.openapi
.project
.Project
;
31 import com
.intellij
.openapi
.ui
.Messages
;
32 import com
.intellij
.openapi
.util
.io
.FileUtil
;
33 import com
.intellij
.openapi
.vfs
.VirtualFile
;
34 import org
.jetbrains
.annotations
.NonNls
;
35 import org
.jetbrains
.annotations
.NotNull
;
37 import java
.util
.ArrayList
;
38 import java
.util
.Collections
;
39 import java
.util
.List
;
41 public class VetoSavingCommittingDocumentsAdapter
implements ApplicationComponent
, FileDocumentSynchronizationVetoListener
{
42 private static final Object SAVE_DENIED
= new Object();
44 private final FileDocumentManager myFileDocumentManager
;
46 public VetoSavingCommittingDocumentsAdapter(final FileDocumentManager fileDocumentManager
) {
47 myFileDocumentManager
= fileDocumentManager
;
50 public void beforeDocumentSaving(Document document
) throws VetoDocumentSavingException
{
51 final Object beingCommitted
= document
.getUserData(ChangeListManagerImpl
.DOCUMENT_BEING_COMMITTED_KEY
);
52 if (beingCommitted
== SAVE_DENIED
) {
53 throw new VetoDocumentSavingException();
55 if (beingCommitted
instanceof Project
) {
56 boolean allowSave
= showAllowSaveDialog((Project
) beingCommitted
, Collections
.singletonList(document
));
58 throw new VetoDocumentSavingException();
64 public void beforeFileContentReload(VirtualFile file
, Document document
) throws VetoDocumentReloadException
{
68 public String
getComponentName() {
69 return "VetoSavingComittingDocumentsAdapter";
72 public void initComponent() {
73 myFileDocumentManager
.addFileDocumentSynchronizationVetoer(this);
74 ApplicationManager
.getApplication().getMessageBus().connect().subscribe(AppTopics
.FILE_DOCUMENT_SYNC
, new FileDocumentManagerAdapter() {
76 public void beforeAllDocumentsSaving() {
77 List
<Document
> documentsToWarn
= new ArrayList
<Document
>();
78 final Document
[] unsavedDocuments
= myFileDocumentManager
.getUnsavedDocuments();
79 Project commitOwnerProject
= null;
80 for (Document unsavedDocument
: unsavedDocuments
) {
81 final Object data
= unsavedDocument
.getUserData(ChangeListManagerImpl
.DOCUMENT_BEING_COMMITTED_KEY
);
82 if (data
instanceof Project
) {
83 commitOwnerProject
= (Project
) data
;
84 documentsToWarn
.add(unsavedDocument
);
87 if (!documentsToWarn
.isEmpty()) {
88 boolean allowSave
= showAllowSaveDialog(commitOwnerProject
, documentsToWarn
);
89 for (Document document
: documentsToWarn
) {
90 document
.putUserData(ChangeListManagerImpl
.DOCUMENT_BEING_COMMITTED_KEY
, allowSave ?
null : SAVE_DENIED
);
97 public void disposeComponent() {
98 myFileDocumentManager
.removeFileDocumentSynchronizationVetoer(this);
101 private boolean showAllowSaveDialog(Project project
, List
<Document
> documentsToWarn
) {
102 StringBuilder messageBuilder
= new StringBuilder("The following " + (documentsToWarn
.size() == 1 ?
"file is" : "files are") +
103 " currently being committed to the VCS. " +
104 "Saving now could cause inconsistent data to be committed.\n");
105 for (Document document
: documentsToWarn
) {
106 final VirtualFile file
= myFileDocumentManager
.getFile(document
);
107 messageBuilder
.append(FileUtil
.toSystemDependentName(file
.getPath())).append("\n");
109 messageBuilder
.append("Save the ").append(documentsToWarn
.size() == 1 ?
"file" : "files").append(" now?");
111 int rc
= Messages
.showDialog(project
, messageBuilder
.toString(), "Save Files During Commit", new String
[] { "Save Now", "Postpone Save" },
112 0, Messages
.getQuestionIcon());