Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HK] Use MapPart in 'ActiveMapTracker' #478

Merged
merged 8 commits into from
Sep 17, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
/**
* Tracks which map is the active map. Essentially just listens to the workbench for part changes
* and when a part is an MapEditorPart it marks that as the active map.
*
*
* @author jesse
* @since 1.1.0
*/
public class ActiveMapTracker implements IStartup, IPartListener2, IWindowListener, IPageListener {

/**
* All the parts that are have been active in the order of activation.
*
*
*/
private List<MapPart> activeParts = new CopyOnWriteArrayList<>();

Expand All @@ -57,7 +57,7 @@ public class ActiveMapTracker implements IStartup, IPartListener2, IWindowListen
/**
* All the maps that are currently open
*/
private Set<MapPart> openMaps = new CopyOnWriteArraySet<>();
private List<MapPart> openMaps = new CopyOnWriteArrayList<>();
fgdrf marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the set of the visible maps.
Expand All @@ -79,15 +79,26 @@ public Collection<? extends IMap> getVisibleMaps() {
* @return
*/
public Map getActiveMap() {
MapPart activePart = getActiveMapPartInternal();
if (activePart != null) {
return activePart.getMap();
}

if (Display.getCurrent() != null) {
MapPart part = getActivePart();
if (part != null && !activeParts.isEmpty() && part != activeParts.get(0)) {
addActivePart(part);
return part.getMap();
}
MapPart visibleMapPart = getFirstVisibleMapPart();
return (visibleMapPart == null ? ApplicationGIS.NO_MAP : visibleMapPart.getMap());
}

/**
* @return most recent opened MapPart or null.
*/
public MapPart getMostRecentOpenedPart() {
if (!openMaps.isEmpty()) {
return openMaps.get(0);
}
return null;
fgdrf marked this conversation as resolved.
Show resolved Hide resolved
}

private MapPart getFirstVisibleMapPart() {
// lets first look at activeParts
MapPart mapPart = null;
if (!activeParts.isEmpty()) {
Expand All @@ -103,15 +114,28 @@ public Map getActiveMap() {
}
}
}
return mapPart;
}

public MapPart getActiveMapPart() {
return getActiveMapPartInternal();
}

if (mapPart == null || mapPart.getMap() == null) {
// nothing found
return ApplicationGIS.NO_MAP;
private MapPart getActiveMapPartInternal() {
if (Display.getCurrent() != null) {
MapPart part = getActiveMapPartFromWorkbench();
if (part == null) {
part = getMostRecentOpenedPart();
}
if (!activeParts.isEmpty() && part != activeParts.get(0)) {
addActivePart(part);
}
return part;
}
return mapPart.getMap();
return null;
}

private MapPart getActivePart() {
private MapPart getActiveMapPartFromWorkbench() {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null) {
return null;
Expand All @@ -124,7 +148,6 @@ private MapPart getActivePart() {
return (MapPart) part;
}
}

return null;
}

Expand All @@ -142,22 +165,28 @@ public Collection<? extends IMap> getOpenMaps() {
}

private void addActivePart(MapPart part) {
while (activeParts.remove(part))
while (activeParts.remove(part)) {
;
}
activeParts.add(0, part);
}

private void removePart(MapPart part) {
while (activeParts.remove(part))
while (activeParts.remove(part)) {
;
}
visibleMaps.remove(part);
openMaps.remove(part);
}

private boolean addOpenMap(IWorkbenchPart part) {
return openMaps.add((MapPart) part);
private void addOpenMap(IWorkbenchPart part) {
while (openMaps.remove((MapPart)part)) {
;
}
openMaps.add(0, (MapPart) part);
fgdrf marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void windowClosed(IWorkbenchWindow window) {
// stop listening to pages and parts
IWorkbenchPage[] pages = window.getPages();
Expand All @@ -167,6 +196,7 @@ public void windowClosed(IWorkbenchWindow window) {
window.removePageListener(this);
}

@Override
public void windowOpened(IWorkbenchWindow window) {
// start listening to pages and parts
// stop listening to pages and parts
Expand All @@ -177,38 +207,39 @@ public void windowOpened(IWorkbenchWindow window) {
window.addPageListener(this);
}

@Override
public void pageClosed(IWorkbenchPage page) {
page.removePartListener(this);
}

@Override
public void pageOpened(IWorkbenchPage page) {
page.addPartListener(this);
IEditorReference[] editors = page.getEditorReferences();
for (IEditorReference reference : editors) {
IWorkbenchPart workpart = reference.getPart(false);
if (reference.getPart(false) instanceof MapPart) {
MapPart part = (MapPart) reference.getPart(false);
openMaps.add(part);
IWorkbenchPart workbenchPart = reference.getPart(false);
if (workbenchPart instanceof MapPart) {
addOpenMap(workbenchPart);

if (page.isPartVisible(workpart)) {
visibleMaps.add(part);
if (page.isPartVisible(workbenchPart)) {
visibleMaps.add((MapPart) workbenchPart);
}
}
}
IViewReference[] views = page.getViewReferences();
for (IViewReference reference : views) {
IWorkbenchPart workpart = reference.getPart(false);
if (workpart instanceof MapPart) {
MapPart part = (MapPart) reference.getPart(false);
openMaps.add(part);
IWorkbenchPart workbenchPart = reference.getPart(false);
if (workbenchPart instanceof MapPart) {
addOpenMap(workbenchPart);

if (page.isPartVisible(workpart)) {
visibleMaps.add(part);
if (page.isPartVisible(workbenchPart)) {
visibleMaps.add((MapPart) workbenchPart);
}
}
}
}

@Override
public void partActivated(IWorkbenchPartReference partRef) {
// make this the active map(if it is a MapPart)
IWorkbenchPart part = partRef.getPart(false);
Expand All @@ -217,6 +248,7 @@ public void partActivated(IWorkbenchPartReference partRef) {
}
}

@Override
public void partClosed(IWorkbenchPartReference partRef) {
// if active map then make previous map be the active map
IWorkbenchPart part = partRef.getPart(false);
Expand All @@ -225,6 +257,7 @@ public void partClosed(IWorkbenchPartReference partRef) {
}
}

@Override
public void partVisible(IWorkbenchPartReference partRef) {
// if no active map then make this the active map (if it is a MapPart)
IWorkbenchPart part = partRef.getPart(false);
Expand All @@ -236,40 +269,48 @@ public void partVisible(IWorkbenchPartReference partRef) {
}
}

@Override
public void partHidden(IWorkbenchPartReference partRef) {
IWorkbenchPart part = partRef.getPart(false);
if (part instanceof MapPart) {
visibleMaps.remove((MapPart)part);
}
}

@Override
public void partOpened(IWorkbenchPartReference partRef) {
IWorkbenchPart part = partRef.getPart(false);
if (part instanceof MapPart) {
addOpenMap(part);
}
}

@Override
public void partBroughtToTop(IWorkbenchPartReference partRef) {
// do nothing
}

@Override
public void partDeactivated(IWorkbenchPartReference partRef) {
// do nothing
}

@Override
public void partInputChanged(IWorkbenchPartReference partRef) {
// do nothing
}

@Override
public void windowActivated(IWorkbenchWindow window) {
// do nothing
}

@Override
public void windowDeactivated(IWorkbenchWindow window) {
// do nothing
}

@Override
public void pageActivated(IWorkbenchPage page) {
// do nothing
}
Expand All @@ -288,4 +329,11 @@ public void earlyStartup() {
}
}

/**
* @return Collection of open MapParts.
*/
public Collection<MapPart> getOpenMapParts() {
sschulz92 marked this conversation as resolved.
Show resolved Hide resolved
return Collections.unmodifiableCollection(openMaps);
}

}