1 /*******************************************************************************
2 * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org
.eclipse
.egit
.ui
.internal
.revision
;
13 import java
.io
.BufferedOutputStream
;
14 import java
.io
.IOException
;
15 import java
.lang
.reflect
.InvocationTargetException
;
17 import org
.eclipse
.core
.filesystem
.EFS
;
18 import org
.eclipse
.core
.filesystem
.IFileStore
;
19 import org
.eclipse
.core
.resources
.IFile
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
22 import org
.eclipse
.core
.runtime
.Assert
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.core
.runtime
.IPath
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
27 import org
.eclipse
.core
.runtime
.jobs
.Job
;
28 import org
.eclipse
.egit
.ui
.Activator
;
29 import org
.eclipse
.jface
.operation
.IRunnableContext
;
30 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
31 import org
.eclipse
.team
.core
.history
.IFileRevision
;
34 * Editable revision backed by a file outside of the workspace (just IPath).
36 * Use {@link ResourceEditableRevision} if you have a resource.
38 public class LocationEditableRevision
extends EditableRevision
{
40 private final IPath location
;
42 private final IRunnableContext runnableContext
;
47 * @param runnableContext
49 public LocationEditableRevision(IFileRevision fileRevision
, IPath location
,
50 IRunnableContext runnableContext
) {
51 super(fileRevision
, null);
52 this.location
= location
;
53 Assert
.isNotNull(runnableContext
);
54 this.runnableContext
= runnableContext
;
58 public void setContent(final byte[] newContent
) {
60 // Don't fork: if we are called from a thread which locked
61 // workspace our *forked* operation will never complete because it
62 // requires file lock which cannot be acquired from another thread
63 ISchedulingRule rule
= Job
.getJobManager().currentRule();
65 if (rule
instanceof IResource
) {
66 IFile ourFile
= ResourcesPlugin
.getWorkspace().getRoot()
69 && ((IResource
) rule
).isConflicting(ourFile
))
72 runnableContext
.run(fork
, false, new IRunnableWithProgress() {
74 public void run(IProgressMonitor monitor
)
75 throws InvocationTargetException
, InterruptedException
{
76 IFileStore store
= EFS
.getLocalFileSystem().getStore(
78 BufferedOutputStream out
= null;
80 out
= new BufferedOutputStream(store
.openOutputStream(
82 out
.write(newContent
);
83 } catch (CoreException e
) {
84 throw new InvocationTargetException(e
);
85 } catch (IOException e
) {
86 throw new InvocationTargetException(e
);
91 } catch (IOException e
) {
98 } catch (InvocationTargetException e
) {
99 Activator
.handleError(e
.getTargetException().getMessage(),
100 e
.getTargetException(), true);
101 } catch (InterruptedException e
) {
107 public int hashCode() {
108 final int prime
= 31;
109 int result
= super.hashCode();
110 result
= prime
* result
111 + ((location
== null) ?
0 : location
.hashCode());
112 result
= prime
* result
113 + ((runnableContext
== null) ?
0 : runnableContext
.hashCode());
118 public boolean equals(Object obj
) {
121 if (!super.equals(obj
))
123 if (getClass() != obj
.getClass())
125 LocationEditableRevision other
= (LocationEditableRevision
) obj
;
126 if (location
== null) {
127 if (other
.location
!= null)
129 } else if (!location
.equals(other
.location
))
131 if (runnableContext
== null) {
132 if (other
.runnableContext
!= null)
134 } else if (!runnableContext
.equals(other
.runnableContext
))