-
Notifications
You must be signed in to change notification settings - Fork 3
/
AutoSaveManager.java
143 lines (111 loc) · 4.71 KB
/
AutoSaveManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.core.engine;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;
import java.io.ByteArrayInputStream;
import java.util.concurrent.ExecutorService;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.IFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension4;
import org.eclipse.jface.text.IDocumentListener;
import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.utils.CoreExecutors;
import melnorme.lang.ide.core.utils.DefaultBufferListener;
import melnorme.utilbox.misc.StringUtil;
import melnorme.utilbox.ownership.LifecycleObject;
public class AutoSaveManager extends LifecycleObject {
protected final ExecutorService executor = CoreExecutors.newExecutorTaskAgent(getClass());
public AutoSaveManager() {
this(true);
}
public AutoSaveManager(boolean initialize) {
if(initialize) {
initialize();
}
}
public void initialize() {
ITextFileBufferManager fbm = FileBuffers.getTextFileBufferManager();
fbm.addFileBufferListener(fbmListener);
// setup the FBM dispose code:
owned.add(() -> fbm.removeFileBufferListener(fbmListener));
}
protected final DefaultBufferListener fbmListener = new DefaultBufferListener() {
@Override
public void bufferCreated(IFileBuffer buffer) {
if(buffer instanceof ITextFileBuffer) {
ITextFileBuffer textFileBuffer = (ITextFileBuffer) buffer;
IDocument doc = textFileBuffer.getDocument();
IDocumentListener docListener = new DocumentAutoSaveListener(textFileBuffer);
doc.addDocumentListener(docListener);
}
}
};
protected class DocumentAutoSaveListener implements IDocumentListener {
protected final ITextFileBuffer textFileBuffer;
protected volatile UpdaterTask currentUpdateTask;
public DocumentAutoSaveListener(ITextFileBuffer textFileBuffer) {
this.textFileBuffer = assertNotNull(textFileBuffer);
}
@Override
public void documentAboutToBeChanged(DocumentEvent event) {
}
@Override
public void documentChanged(DocumentEvent event) {
IDocument document = event.fDocument;
long modificationStamp = IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP;
if (document instanceof IDocumentExtension4) {
modificationStamp = ((IDocumentExtension4)document).getModificationStamp();
}
String contents = document.get();
// Usually synchronized block should not be necessary, given #documentChanged is triggered
// under document lock. But just in case the document has no lock.
synchronized (this) {
if(currentUpdateTask != null) {
currentUpdateTask.cancel();
}
UpdaterTask newUpdaterTask = new UpdaterTask(textFileBuffer, contents, modificationStamp);
executor.submit(newUpdaterTask);
}
}
}
protected class UpdaterTask implements Runnable {
protected final ITextFileBuffer textFileBuffer;
protected final ByteArrayInputStream contents;
protected final long modificationStamp;
protected final IProgressMonitor pm = new NullProgressMonitor();
public UpdaterTask(ITextFileBuffer textFileBuffer, String contents, long modificationStamp) {
this.textFileBuffer = textFileBuffer;
/* FIXME: todo hasBOM and UTF BOM in stream */
this.contents = new ByteArrayInputStream(contents.getBytes(StringUtil.UTF8));
this.modificationStamp = modificationStamp;
}
public void cancel() {
pm.setCanceled(true);
}
@Override
public void run() {
try {
//System.out.println(" commitTextFileBuffer: " + contents.length());
textFileBuffer.internalCommitFileContents(pm, false, contents, modificationStamp, false);
//System.out.println("finished update");
} catch(CoreException e) {
LangCore.logStatus(e);
}
}
}
}