ContentManager: make mock & production behaviour a bit similar (add events)

This commit is contained in:
Gregory.Shrago
2015-01-27 20:51:41 +03:00
parent 2bc709c99f
commit cb64f535c4
2 changed files with 48 additions and 36 deletions
@@ -38,11 +38,9 @@ import com.intellij.openapi.wm.*;
import com.intellij.openapi.wm.ex.ToolWindowEx;
import com.intellij.openapi.wm.ex.ToolWindowManagerEx;
import com.intellij.openapi.wm.ex.ToolWindowManagerListener;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentManager;
import com.intellij.ui.content.ContentManagerListener;
import com.intellij.ui.content.*;
import com.intellij.util.ArrayUtil;
import com.intellij.util.EventDispatcher;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -480,6 +478,7 @@ public class ToolWindowHeadlessManagerImpl extends ToolWindowManagerEx {
}
private static class MockContentManager implements ContentManager {
private final EventDispatcher<ContentManagerListener> myDispatcher = EventDispatcher.create(ContentManagerListener.class);
private final List<Content> myContents = new ArrayList<Content>();
private Content mySelected;
@@ -492,13 +491,17 @@ public class ToolWindowHeadlessManagerImpl extends ToolWindowManagerEx {
@Override
public void addContent(@NotNull final Content content) {
myContents.add(content);
if (mySelected == null) mySelected = content;
ContentManagerEvent e = new ContentManagerEvent(this, content, myContents.indexOf(content), ContentManagerEvent.ContentOperation.add);
myDispatcher.getMulticaster().contentAdded(e);
if (mySelected == null) setSelectedContent(content);
}
@Override
public void addContent(@NotNull Content content, int order) {
myContents.add(order, content);
if (mySelected == null) mySelected = content;
ContentManagerEvent e = new ContentManagerEvent(this, content, myContents.indexOf(content), ContentManagerEvent.ContentOperation.add);
myDispatcher.getMulticaster().contentAdded(e);
if (mySelected == null) setSelectedContent(content);
}
@Override
@@ -514,6 +517,7 @@ public class ToolWindowHeadlessManagerImpl extends ToolWindowManagerEx {
@Override
public void addContentManagerListener(@NotNull final ContentManagerListener l) {
myDispatcher.getListeners().add(0, l);
}
@Override
@@ -625,20 +629,24 @@ public class ToolWindowHeadlessManagerImpl extends ToolWindowManagerEx {
@Override
public void removeAllContents(final boolean dispose) {
for (int i = myContents.size() - 1; i >= 0; i--) {
Content content = myContents.get(i);
for (Content content : getContents()) {
removeContent(content, dispose);
}
mySelected = null;
}
@Override
public boolean removeContent(@NotNull final Content content, final boolean dispose) {
if (dispose) Disposer.dispose(content);
boolean result = myContents.remove(content);
if (mySelected == content) {
mySelected = ContainerUtil.getFirstItem(myContents);
boolean wasSelected = mySelected == content;
int oldIndex = myContents.indexOf(content);
if (wasSelected) {
removeFromSelection(content);
}
boolean result = myContents.remove(content);
if (dispose) Disposer.dispose(content);
ContentManagerEvent e = new ContentManagerEvent(this, content, oldIndex, ContentManagerEvent.ContentOperation.remove);
myDispatcher.getMulticaster().contentRemoved(e);
Content item = ContainerUtil.getFirstItem(myContents);
if (item != null) setSelectedContent(item);
return result;
}
@@ -651,10 +659,13 @@ public class ToolWindowHeadlessManagerImpl extends ToolWindowManagerEx {
@Override
public void removeContentManagerListener(@NotNull final ContentManagerListener l) {
myDispatcher.removeListener(l);
}
@Override
public void removeFromSelection(@NotNull final Content content) {
ContentManagerEvent e = new ContentManagerEvent(this, content, myContents.indexOf(mySelected), ContentManagerEvent.ContentOperation.remove);
myDispatcher.getMulticaster().selectionChanged(e);
}
@Override
@@ -669,7 +680,12 @@ public class ToolWindowHeadlessManagerImpl extends ToolWindowManagerEx {
@Override
public void setSelectedContent(@NotNull final Content content) {
if (mySelected != null) {
removeFromSelection(mySelected);
}
mySelected = content;
ContentManagerEvent e = new ContentManagerEvent(this, content, myContents.indexOf(content), ContentManagerEvent.ContentOperation.add);
myDispatcher.getMulticaster().selectionChanged(e);
}
@NotNull
@@ -715,7 +731,9 @@ public class ToolWindowHeadlessManagerImpl extends ToolWindowManagerEx {
@Override
public void dispose() {
removeAllContents(true);
myContents.clear();
mySelected = null;
myDispatcher.getListeners().clear();
}
@Override
@@ -33,8 +33,8 @@ import com.intellij.ui.components.panels.Wrapper;
import com.intellij.ui.content.*;
import com.intellij.ui.switcher.SwitchProvider;
import com.intellij.ui.switcher.SwitchTarget;
import com.intellij.util.EventDispatcher;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NonNls;
@@ -59,7 +59,7 @@ public class ContentManagerImpl implements ContentManager, PropertyChangeListene
private ContentUI myUI;
private final List<Content> myContents = new ArrayList<Content>();
private final List<ContentManagerListener> myListeners = ContainerUtil.createLockFreeCopyOnWriteList();
private final EventDispatcher<ContentManagerListener> myDispatcher = EventDispatcher.create(ContentManagerListener.class);
private final List<Content> mySelection = new ArrayList<Content>();
private final boolean myCanCloseContents;
@@ -202,7 +202,7 @@ public class ContentManagerImpl implements ContentManager, PropertyChangeListene
final int insertIndex = index == -1 ? myContents.size() : index;
myContents.add(insertIndex, content);
content.addPropertyChangeListener(this);
fireContentAdded(content, insertIndex, ContentManagerEvent.ContentOperation.add);
fireContentAdded(content, insertIndex);
if (myUI.isToSelectAddedContent() || mySelection.isEmpty() && !myUI.canBeEmptySelection()) {
if (myUI.isSingleSelection()) {
setSelectedContent(content);
@@ -285,7 +285,7 @@ public class ContentManagerImpl implements ContentManager, PropertyChangeListene
myContents.remove(content);
content.removePropertyChangeListener(this);
fireContentRemoved(content, indexToBeRemoved, ContentManagerEvent.ContentOperation.remove);
fireContentRemoved(content, indexToBeRemoved);
((ContentImpl)content).setManager(null);
@@ -597,39 +597,33 @@ public class ContentManagerImpl implements ContentManager, PropertyChangeListene
@Override
public void addContentManagerListener(@NotNull ContentManagerListener l) {
myListeners.add(0,l);
myDispatcher.getListeners().add(0, l);
}
@Override
public void removeContentManagerListener(@NotNull ContentManagerListener l) {
myListeners.remove(l);
myDispatcher.removeListener(l);
}
private void fireContentAdded(Content content, int newIndex, ContentManagerEvent.ContentOperation operation) {
ContentManagerEvent event = new ContentManagerEvent(this, content, newIndex, operation);
for (ContentManagerListener listener : myListeners) {
listener.contentAdded(event);
}
private void fireContentAdded(Content content, int newIndex) {
ContentManagerEvent e = new ContentManagerEvent(this, content, newIndex, ContentManagerEvent.ContentOperation.add);
myDispatcher.getMulticaster().contentAdded(e);
}
private void fireContentRemoved(Content content, int oldIndex, ContentManagerEvent.ContentOperation operation) {
ContentManagerEvent event = new ContentManagerEvent(this, content, oldIndex, operation);
for (ContentManagerListener listener : myListeners) {
listener.contentRemoved(event);
}
private void fireContentRemoved(Content content, int oldIndex) {
ContentManagerEvent e = new ContentManagerEvent(this, content, oldIndex, ContentManagerEvent.ContentOperation.remove);
myDispatcher.getMulticaster().contentRemoved(e);
}
private void fireSelectionChanged(Content content, ContentManagerEvent.ContentOperation operation) {
ContentManagerEvent event = new ContentManagerEvent(this, content, myContents.indexOf(content), operation);
for (ContentManagerListener listener : myListeners) {
listener.selectionChanged(event);
}
ContentManagerEvent e = new ContentManagerEvent(this, content, getIndexOfContent(content), operation);
myDispatcher.getMulticaster().selectionChanged(e);
}
private boolean fireContentRemoveQuery(Content content, int oldIndex, ContentManagerEvent.ContentOperation operation) {
ContentManagerEvent event = new ContentManagerEvent(this, content, oldIndex, operation);
for (ContentManagerListener listener : myListeners) {
for (ContentManagerListener listener : myDispatcher.getListeners()) {
listener.contentRemoveQuery(event);
if (event.isConsumed()) {
return false;
@@ -710,7 +704,7 @@ public class ContentManagerImpl implements ContentManager, PropertyChangeListene
mySelection.clear();
myContentWithChangedComponent.clear();
myUI = null;
myListeners.clear();
myDispatcher.getListeners().clear();
dataProviders.clear();
}