Have icon for "reset" entry in reflog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / revision / LocationEditableRevision.java
blob3e40dbcb028d1594f3c5435985dc56be94529f02
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;
33 /**
34 * Editable revision backed by a file outside of the workspace (just IPath).
35 * <p>
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;
44 /**
45 * @param fileRevision
46 * @param location
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;
57 @Override
58 public void setContent(final byte[] newContent) {
59 try {
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();
64 boolean fork = true;
65 if (rule instanceof IResource) {
66 IFile ourFile = ResourcesPlugin.getWorkspace().getRoot()
67 .getFile(location);
68 if (ourFile.exists()
69 && ((IResource) rule).isConflicting(ourFile))
70 fork = false;
72 runnableContext.run(fork, false, new IRunnableWithProgress() {
73 @Override
74 public void run(IProgressMonitor monitor)
75 throws InvocationTargetException, InterruptedException {
76 IFileStore store = EFS.getLocalFileSystem().getStore(
77 location);
78 BufferedOutputStream out = null;
79 try {
80 out = new BufferedOutputStream(store.openOutputStream(
81 0, monitor));
82 out.write(newContent);
83 } catch (CoreException e) {
84 throw new InvocationTargetException(e);
85 } catch (IOException e) {
86 throw new InvocationTargetException(e);
87 } finally {
88 if (out != null) {
89 try {
90 out.close();
91 } catch (IOException e) {
92 // Ignore this one
97 });
98 } catch (InvocationTargetException e) {
99 Activator.handleError(e.getTargetException().getMessage(),
100 e.getTargetException(), true);
101 } catch (InterruptedException e) {
102 // ignore here
106 @Override
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());
114 return result;
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj)
120 return true;
121 if (!super.equals(obj))
122 return false;
123 if (getClass() != obj.getClass())
124 return false;
125 LocationEditableRevision other = (LocationEditableRevision) obj;
126 if (location == null) {
127 if (other.location != null)
128 return false;
129 } else if (!location.equals(other.location))
130 return false;
131 if (runnableContext == null) {
132 if (other.runnableContext != null)
133 return false;
134 } else if (!runnableContext.equals(other.runnableContext))
135 return false;
136 return true;