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.
16 package com
.intellij
.ide
;
18 import com
.intellij
.openapi
.application
.ApplicationManager
;
19 import com
.intellij
.openapi
.application
.ex
.ApplicationManagerEx
;
20 import com
.intellij
.openapi
.application
.impl
.LaterInvocator
;
21 import com
.intellij
.openapi
.components
.ApplicationComponent
;
22 import com
.intellij
.openapi
.diagnostic
.Logger
;
23 import com
.intellij
.openapi
.fileEditor
.FileDocumentManager
;
24 import com
.intellij
.openapi
.fileEditor
.FileEditorManager
;
25 import com
.intellij
.openapi
.progress
.ProgressManager
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.project
.ex
.ProjectManagerEx
;
28 import com
.intellij
.openapi
.vfs
.VirtualFile
;
29 import com
.intellij
.openapi
.vfs
.VirtualFileManager
;
30 import com
.intellij
.openapi
.vfs
.newvfs
.NewVirtualFile
;
31 import com
.intellij
.openapi
.vfs
.newvfs
.RefreshQueue
;
32 import com
.intellij
.openapi
.vfs
.newvfs
.RefreshSession
;
33 import org
.jetbrains
.annotations
.NotNull
;
35 import java
.beans
.PropertyChangeEvent
;
36 import java
.beans
.PropertyChangeListener
;
39 * @author Anton Katilin
40 * @author Vladimir Kondratyev
42 public class SaveAndSyncHandler
implements ApplicationComponent
{
43 private static final Logger LOG
= Logger
.getInstance("#com.intellij.ide.SaveAndSyncHandler");
44 private final Runnable myIdleListener
;
45 private final PropertyChangeListener myGeneralSettingsListener
;
47 public SaveAndSyncHandler(final FrameStateManager frameStateManager
,
48 final FileDocumentManager fileDocumentManager
,
49 final GeneralSettings generalSettings
) {
51 myIdleListener
= new Runnable() {
53 if (generalSettings
.isAutoSaveIfInactive() && canSyncOrSave()) {
54 fileDocumentManager
.saveAllDocuments();
60 IdeEventQueue
.getInstance().addIdleListener(
62 generalSettings
.getInactiveTimeout() * 1000
65 myGeneralSettingsListener
= new PropertyChangeListener() {
66 public void propertyChange(PropertyChangeEvent e
) {
67 if (GeneralSettings
.PROP_INACTIVE_TIMEOUT
.equals(e
.getPropertyName())) {
68 IdeEventQueue eventQueue
= IdeEventQueue
.getInstance();
69 eventQueue
.removeIdleListener(myIdleListener
);
70 Integer timeout
= (Integer
)e
.getNewValue();
71 eventQueue
.addIdleListener(myIdleListener
, timeout
.intValue() * 1000);
75 generalSettings
.addPropertyChangeListener(myGeneralSettingsListener
);
77 frameStateManager
.addListener(new FrameStateListener() {
78 public void onFrameDeactivated() {
79 if (canSyncOrSave()) {
80 saveProjectsAndDocuments();
84 public void onFrameActivated() {
92 public String
getComponentName() {
93 return "SaveAndSyncHandler";
96 public void initComponent() {
99 public void disposeComponent() {
100 GeneralSettings
.getInstance().removePropertyChangeListener(myGeneralSettingsListener
);
101 IdeEventQueue
.getInstance().removeIdleListener(myIdleListener
);
104 private static boolean canSyncOrSave() {
105 return !LaterInvocator
.isInModalContext() && !ProgressManager
.getInstance().hasModalProgressIndicator();
109 private static void saveProjectsAndDocuments() {
110 if (LOG
.isDebugEnabled()) {
111 LOG
.debug("enter: save()");
113 if (ApplicationManager
.getApplication().isDisposed()) return;
115 if (GeneralSettings
.getInstance().isSaveOnFrameDeactivation()) {
116 FileDocumentManager
.getInstance().saveAllDocuments();
118 Project
[] openProjects
= ProjectManagerEx
.getInstanceEx().getOpenProjects();
119 for (Project project
: openProjects
) {
120 if (LOG
.isDebugEnabled()) {
121 LOG
.debug("save project: " + project
);
125 if (LOG
.isDebugEnabled()) {
126 LOG
.debug("save application settings");
128 ApplicationManagerEx
.getApplicationEx().saveSettings();
129 if (LOG
.isDebugEnabled()) {
130 LOG
.debug("exit: save()");
135 private static void refreshFiles() {
136 if (ApplicationManager
.getApplication().isDisposed()) return;
137 if (LOG
.isDebugEnabled()) {
138 LOG
.debug("enter: synchronize()");
141 if (canSyncOrSave()) {
145 if (GeneralSettings
.getInstance().isSyncOnFrameActivation()) {
146 if (LOG
.isDebugEnabled()) {
147 LOG
.debug("refresh VFS");
149 VirtualFileManager
.getInstance().refresh(true);
152 if (LOG
.isDebugEnabled()) {
153 LOG
.debug("exit: synchronize()");
157 public static void refreshOpenFiles() {
158 // Refresh open files synchronously so it doesn't wait for potentially longish refresh request in the queue to finish
159 final RefreshSession session
= RefreshQueue
.getInstance().createSession(false, false, null);
161 for (Project project
: ProjectManagerEx
.getInstanceEx().getOpenProjects()) {
162 VirtualFile
[] files
= FileEditorManager
.getInstance(project
).getSelectedFiles();
163 for (VirtualFile file
: files
) {
164 if (file
instanceof NewVirtualFile
) {
165 session
.addFile(file
);