mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
vcs: Refactored "ValueConsumer", "GenericDetailsLoader"
This commit is contained in:
@@ -15,74 +15,46 @@
|
||||
*/
|
||||
package com.intellij.openapi.vcs;
|
||||
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.PairConsumer;
|
||||
import org.jetbrains.annotations.CalledInAny;
|
||||
import org.jetbrains.annotations.CalledInAwt;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author irengrig
|
||||
* Date: 6/29/11
|
||||
* Time: 11:38 PM
|
||||
*/
|
||||
public class GenericDetailsLoader<Id, Data> implements Details<Id,Data>, Disposable {
|
||||
public class GenericDetailsLoader<Id, Data> implements Details<Id, Data> {
|
||||
private final Consumer<Id> myLoader;
|
||||
private final ValueConsumer<Id, Data> myValueConsumer;
|
||||
private final AtomicReference<Id> myCurrentlySelected;
|
||||
private boolean myIsDisposed;
|
||||
private Id myCurrentlySelected;
|
||||
|
||||
/**
|
||||
* @param loader - is called in AWT. Should call {@link #take} with data when ready. Also in AWT
|
||||
* @param valueConsumer - is called in AWT. passive, just benefits from details loading
|
||||
*/
|
||||
public GenericDetailsLoader(final Consumer<Id> loader, final PairConsumer<Id, Data> valueConsumer) {
|
||||
public GenericDetailsLoader(Consumer<Id> loader, PairConsumer<Id, Data> valueConsumer) {
|
||||
myLoader = loader;
|
||||
myValueConsumer = new ValueConsumer<>(valueConsumer);
|
||||
myCurrentlySelected = new AtomicReference<>(null);
|
||||
}
|
||||
|
||||
@CalledInAwt
|
||||
public void updateSelection(@Nullable final Id id, boolean force) {
|
||||
ApplicationManager.getApplication().assertIsDispatchThread();
|
||||
if (myIsDisposed) return;
|
||||
public void updateSelection(@Nullable Id id, boolean force) {
|
||||
myValueConsumer.setId(id);
|
||||
|
||||
final Id wasId = myCurrentlySelected.getAndSet(id);
|
||||
if (force || ! Comparing.equal(id, wasId)) {
|
||||
Id previousId = myCurrentlySelected;
|
||||
myCurrentlySelected = id;
|
||||
if (force || !Comparing.equal(id, previousId)) {
|
||||
myLoader.consume(id);
|
||||
}
|
||||
}
|
||||
|
||||
public void setCacheConsumer(final PairConsumer<Id, Data> cacheConsumer) {
|
||||
myValueConsumer.setCacheConsumer(cacheConsumer);
|
||||
@CalledInAwt
|
||||
@Override
|
||||
public void take(Id id, Data data) {
|
||||
myValueConsumer.consume(id, data);
|
||||
}
|
||||
|
||||
@CalledInAwt
|
||||
@Override
|
||||
public void take(Id id, Data data) throws AlreadyDisposedException {
|
||||
ApplicationManager.getApplication().assertIsDispatchThread();
|
||||
if (myIsDisposed) throw new AlreadyDisposedException();
|
||||
myValueConsumer.consume(id, data);
|
||||
}
|
||||
|
||||
public void resetValueConsumer() {
|
||||
myValueConsumer.reset();
|
||||
}
|
||||
|
||||
@CalledInAny
|
||||
@Override
|
||||
public Id getCurrentlySelected() {
|
||||
return myCurrentlySelected.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
myIsDisposed = true;
|
||||
return myCurrentlySelected;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,42 +17,24 @@ package com.intellij.openapi.vcs;
|
||||
|
||||
import com.intellij.util.PairConsumer;
|
||||
|
||||
/**
|
||||
* @author irengrig
|
||||
* Date: 6/29/11
|
||||
* Time: 11:51 PM
|
||||
*/
|
||||
public class ValueConsumer<Id, Data> {
|
||||
private Id myId;
|
||||
private Id mySetId;
|
||||
private final PairConsumer<Id, Data> myConsumer;
|
||||
private PairConsumer<Id, Data> myCacheConsumer;
|
||||
|
||||
protected ValueConsumer(PairConsumer<Id, Data> consumer) {
|
||||
myConsumer = consumer;
|
||||
}
|
||||
|
||||
public void setCacheConsumer(PairConsumer<Id, Data> cacheConsumer) {
|
||||
myCacheConsumer = cacheConsumer;
|
||||
}
|
||||
|
||||
public void consume(final Id id, final Data data) {
|
||||
if (id.equals(mySetId) || ! id.equals(myId)) {
|
||||
if (myCacheConsumer != null) {
|
||||
myCacheConsumer.consume(id, data);
|
||||
}
|
||||
return;
|
||||
public void consume(Id id, Data data) {
|
||||
if (!id.equals(mySetId) && id.equals(myId)) {
|
||||
mySetId = id;
|
||||
myConsumer.consume(id, data);
|
||||
}
|
||||
mySetId = id;
|
||||
myConsumer.consume(id, data);
|
||||
}
|
||||
|
||||
public void setId(Id id) {
|
||||
myId = id;
|
||||
mySetId = null;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
mySetId = null;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-7
@@ -22,7 +22,6 @@ import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.MessageType;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.vcs.Details;
|
||||
import com.intellij.openapi.vcs.GenericDetailsLoader;
|
||||
import com.intellij.openapi.vcs.VcsException;
|
||||
import com.intellij.openapi.vcs.ui.VcsBalloonProblemNotifier;
|
||||
@@ -146,12 +145,7 @@ public abstract class AbstractRefreshablePanel<T> implements RefreshablePanel<Ch
|
||||
@Override
|
||||
protected void doInAwtIfSuccess() {
|
||||
if (myDisposed) return;
|
||||
try {
|
||||
myDetailsLoader.take(myTicketCopy, myT);
|
||||
}
|
||||
catch (Details.AlreadyDisposedException e) {
|
||||
// t is not disposable
|
||||
}
|
||||
myDetailsLoader.take(myTicketCopy, myT);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user