1010 */
1111package org .eclipse .che .ide .editor .synchronization ;
1212
13+ import static com .google .common .base .Strings .isNullOrEmpty ;
1314import static org .eclipse .che .ide .api .notification .StatusNotification .DisplayMode .EMERGE_MODE ;
1415import static org .eclipse .che .ide .api .notification .StatusNotification .DisplayMode .NOT_EMERGE_MODE ;
1516import static org .eclipse .che .ide .api .notification .StatusNotification .Status .FAIL ;
@@ -144,7 +145,7 @@ public void onDocumentChanged(DocumentChangedEvent event) {
144145 }
145146
146147 @ Override
147- public void onFileContentUpdate (final FileContentUpdateEvent event ) {
148+ public void onFileContentUpdate (FileContentUpdateEvent event ) {
148149 if (synchronizedEditors .keySet ().isEmpty ()) {
149150 return ;
150151 }
@@ -159,13 +160,54 @@ public void onFileContentUpdate(final FileContentUpdateEvent event) {
159160 return ;
160161 }
161162
163+ if (!(virtualFile instanceof File )) {
164+ updateContent (virtualFile , false );
165+ return ;
166+ }
167+
168+ File file = (File ) virtualFile ;
169+
170+ String eventModificationStamp = event .getModificationStamp ();
171+ String currentModificationStamp = file .getModificationStamp ();
172+ if (isNullOrEmpty (currentModificationStamp ) || isNullOrEmpty (eventModificationStamp )) {
173+ updateContent (virtualFile , false );
174+ return ;
175+ }
176+
177+ if (!Objects .equals (eventModificationStamp , currentModificationStamp )) {
178+ updateContent (virtualFile , true );
179+ }
180+ }
181+
182+ private void updateContent (VirtualFile virtualFile , boolean externalOperation ) {
183+ final DocumentHandle documentHandle = getDocumentHandleFor (groupLeaderEditor );
184+ if (documentHandle == null ) {
185+ return ;
186+ }
187+
162188 documentStorage .getDocument (
163189 virtualFile ,
164190 new DocumentStorage .DocumentCallback () {
165191
166192 @ Override
167- public void onDocumentReceived (final String content ) {
168- updateContent (content , event .getModificationStamp (), virtualFile );
193+ public void onDocumentReceived (String newContent ) {
194+ Document document = documentHandle .getDocument ();
195+
196+ String oldContent = document .getContents ();
197+ if (Objects .equals (newContent , oldContent )) {
198+ return ;
199+ }
200+
201+ TextPosition cursorPosition = document .getCursorPosition ();
202+ replaceContent (document , newContent , oldContent , cursorPosition );
203+
204+ if (externalOperation ) {
205+ notificationManager .notify (
206+ "External operation" ,
207+ "File '" + virtualFile .getName () + "' is updated" ,
208+ SUCCESS ,
209+ NOT_EMERGE_MODE );
210+ }
169211 }
170212
171213 @ Override
@@ -179,44 +221,6 @@ public void onDocumentLoadFailure(final Throwable caught) {
179221 });
180222 }
181223
182- private void updateContent (
183- String newContent , String eventModificationStamp , VirtualFile virtualFile ) {
184- final DocumentHandle documentHandle = getDocumentHandleFor (groupLeaderEditor );
185- if (documentHandle == null ) {
186- return ;
187- }
188-
189- final Document document = documentHandle .getDocument ();
190- final String oldContent = document .getContents ();
191- if (Objects .equals (newContent , oldContent )) {
192- return ;
193- }
194-
195- final TextPosition cursorPosition = document .getCursorPosition ();
196- if (!(virtualFile instanceof File )) {
197- replaceContent (document , newContent , oldContent , cursorPosition );
198- return ;
199- }
200-
201- final File file = (File ) virtualFile ;
202- final String currentStamp = file .getModificationStamp ();
203-
204- if (eventModificationStamp == null ) {
205- replaceContent (document , newContent , oldContent , cursorPosition );
206- return ;
207- }
208-
209- if (!Objects .equals (eventModificationStamp , currentStamp )) {
210- replaceContent (document , newContent , oldContent , cursorPosition );
211-
212- notificationManager .notify (
213- "External operation" ,
214- "File '" + file .getName () + "' is updated" ,
215- SUCCESS ,
216- NOT_EMERGE_MODE );
217- }
218- }
219-
220224 private void replaceContent (
221225 Document document , String newContent , String oldContent , TextPosition cursorPosition ) {
222226 document .replace (0 , oldContent .length (), newContent );
0 commit comments