Skip to content

Commit

Permalink
Added type information to getEditPartRegisry #155
Browse files Browse the repository at this point in the history
As part of this clean-up a new helper method has been added for getting
an EditPart for a model instance and for getting the LayerManager from a
viewer.

#155
  • Loading branch information
azoitl authored and ptziegler committed Aug 18, 2024
1 parent aea5ba6 commit e5d6bac
Show file tree
Hide file tree
Showing 31 changed files with 149 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.eclipse.gef.AccessibleEditPart;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.NodeEditPart;
Expand Down Expand Up @@ -185,12 +184,12 @@ protected final String mapConnectionAnchorToTerminal(ConnectionAnchor c) {
public void propertyChange(PropertyChangeEvent evt) {
String prop = evt.getPropertyName();
if (LogicElement.CHILDREN.equals(prop)) {
if (evt.getOldValue() instanceof Integer) {
if (evt.getOldValue() instanceof Integer intVal) {
// new child
addChild(createChild(evt.getNewValue()), ((Integer) evt.getOldValue()).intValue());
addChild(createChild(evt.getNewValue()), intVal.intValue());
} else {
// remove child
removeChild((EditPart) getViewer().getEditPartRegistry().get(evt.getOldValue()));
removeChild(getViewer().getEditPartForModel(evt.getOldValue()));
}
} else if (LogicElement.INPUTS.equals(prop)) {
refreshTargetConnections();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;

import org.eclipse.gef.examples.logicdesigner.model.LED;
Expand Down Expand Up @@ -92,12 +91,12 @@ protected List<LogicElement> getModelChildren() {
@Override
public void propertyChange(PropertyChangeEvent change) {
if (change.getPropertyName().equals(LogicElement.CHILDREN)) {
if (change.getOldValue() instanceof Integer) {
if (change.getOldValue() instanceof Integer intVal) {
// new child
addChild(createChild(change.getNewValue()), ((Integer) change.getOldValue()).intValue());
addChild(createChild(change.getNewValue()), intVal.intValue());
} else {
// remove child
removeChild((EditPart) getViewer().getEditPartRegistry().get(change.getOldValue()));
removeChild(getViewer().getEditPartForModel(change.getOldValue()));
}
} else {
refreshVisuals();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public ShapesDiagram getModel() {
* @return the corresponding EditPart or null
*/
private EditPart getEditPartForChild(Object child) {
return (EditPart) getViewer().getEditPartRegistry().get(child);
return getViewer().getEditPartForModel(child);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void executePending() {
}

protected static TextEditPart lookupModel(GraphicalTextViewer viewer, ModelElement model) {
return (TextEditPart) viewer.getEditPartRegistry().get(model);
return (TextEditPart) viewer.getEditPartForModel(model);
}

}
8 changes: 8 additions & 0 deletions org.eclipse.gef/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/gef/EditPartViewer.java" type="org.eclipse.gef.EditPartViewer">
<filter id="403804204">
<message_arguments>
<message_argument value="org.eclipse.gef.EditPartViewer"/>
<message_argument value="getEditPartForModel(Object)"/>
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/gef/ui/actions/AlignmentRetargetAction.java" type="org.eclipse.gef.ui.actions.AlignmentRetargetAction">
<filter id="571473929">
<message_arguments>
Expand Down
15 changes: 14 additions & 1 deletion org.eclipse.gef/src/org/eclipse/gef/EditPartViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,20 @@ interface Conditional {
*
* @return the registry map
*/
Map getEditPartRegistry();
Map<Object, EditPart> getEditPartRegistry();

/**
* Convenience method to look up an edit part for a given model element in the
* EditPart registry.
*
* See also {@link #getEditPartRegistry()} for details on the EditPart registry.
*
* @param model the model object for which an EditPart is looked up
* @return the edit part or null if for the given model no EditPart is
* registered
* @since 3.19
*/
EditPart getEditPartForModel(Object model);

/**
* Returns the <i>focus</i> <code>EditPart</code>. Focus refers to keyboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ private void selectAddedObject() {
}
EditPartViewer viewer = getViewer();
viewer.getControl().forceFocus();
Object editpart = viewer.getEditPartRegistry().get(model);
if (editpart instanceof EditPart ep) {
if (viewer.getEditPartForModel(model) instanceof EditPart ep) {
// Force a layout first.
getViewer().flush();
viewer.select(ep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ protected final void unregisterAccessibility() {
* this method if they need to unregister this EditPart in additional ways.
*/
protected void unregisterModel() {
Map registry = getViewer().getEditPartRegistry();
Map<Object, EditPart> registry = getViewer().getEditPartRegistry();
if (registry.get(getModel()) == this) {
registry.remove(getModel());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,7 @@ protected ConnectionEditPart createConnection(Object model) {
* @return the ConnectionEditPart
*/
protected ConnectionEditPart createOrFindConnection(Object model) {
ConnectionEditPart conx = (ConnectionEditPart) getViewer().getEditPartRegistry().get(model);
if (conx != null) {
if (getViewer().getEditPartForModel(model) instanceof ConnectionEditPart conx) {
return conx;
}
return createConnection(model);
Expand Down Expand Up @@ -484,8 +483,7 @@ public IFigure getFigure() {
* @return The requested layer or <code>null</code> if it doesn't exist
*/
protected IFigure getLayer(Object layer) {
LayerManager manager = (LayerManager) getViewer().getEditPartRegistry().get(LayerManager.ID);
return manager.getLayer(layer);
return LayerManager.Helper.find(this).getLayer(layer);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion org.eclipse.gef/src/org/eclipse/gef/editparts/LayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.draw2d.IFigure;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;

/**
* Responsible for locating <i>layers</i> in a <code>GraphicalViewer</code>.
Expand Down Expand Up @@ -57,7 +58,18 @@ class Helper {
* @return the <code>LayerManager</code>
*/
public static LayerManager find(EditPart part) {
return (LayerManager) part.getViewer().getEditPartRegistry().get(ID);
return find(part.getViewer());
}

/**
* Finds the LayerManager given a Viewer.
*
* @param viewer the viewer for which to search in
* @return the <code>LayerManager</code>
* @since 3.19
*/
public static LayerManager find(EditPartViewer viewer) {
return (LayerManager) viewer.getEditPartForModel(ID);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public <T> T getAdapter(final Class<T> key) {
}

private PaletteAnimator getPaletteAnimator() {
return (PaletteAnimator) getViewer().getEditPartRegistry().get(PaletteAnimator.class);
return getViewer().getPaletteAnimator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public PaletteStackEditPart(PaletteStack model) {
public void activate() {
// in case the model is out of sync
checkActiveEntrySync();
getPaletteViewer().addPaletteListener(paletteListener);
getViewer().addPaletteListener(paletteListener);
super.activate();
}

Expand All @@ -130,7 +130,7 @@ private void activeEntryChanged(Object oldValue, Object newValue) {
Clickable clickable = null;

if (newValue != null) {
part = (GraphicalEditPart) getViewer().getEditPartRegistry().get(newValue);
part = (GraphicalEditPart) getViewer().getEditPartForModel(newValue);
clickable = (Clickable) part.getFigure();
clickable.setVisible(true);
clickable.addChangeListener(clickableListener);
Expand All @@ -140,7 +140,7 @@ private void activeEntryChanged(Object oldValue, Object newValue) {
}

if (oldValue != null) {
part = (GraphicalEditPart) getViewer().getEditPartRegistry().get(oldValue);
part = (GraphicalEditPart) getViewer().getEditPartForModel(oldValue);
// if part is null, its no longer a child.
if (part != null) {
clickable = (Clickable) part.getFigure();
Expand Down Expand Up @@ -198,7 +198,7 @@ public void deactivate() {
}
arrowFigure.removeActionListener(actionListener);
arrowFigure.removeChangeListener(clickableArrowListener);
getPaletteViewer().removePaletteListener(paletteListener);
getViewer().removePaletteListener(paletteListener);
super.deactivate();
}

Expand Down Expand Up @@ -232,17 +232,17 @@ public void openMenu() {

getChildren().forEach(part -> {
PaletteEntry entry = part.getModel();
menuManager.add(new SetActivePaletteToolAction(getPaletteViewer(), entry.getLabel(), entry.getSmallIcon(),
menuManager.add(new SetActivePaletteToolAction(getViewer(), entry.getLabel(), entry.getSmallIcon(),
getStack().getActiveEntry().equals(entry), (ToolEntry) entry));
});

menu = menuManager.createContextMenu(getPaletteViewer().getControl());
menu = menuManager.createContextMenu(getViewer().getControl());

// make the menu open below the figure
Rectangle figureBounds = getFigure().getBounds().getCopy();
getFigure().translateToAbsolute(figureBounds);

Point menuLocation = getPaletteViewer().getControl().toDisplay(figureBounds.getBottomLeft().x,
Point menuLocation = getViewer().getControl().toDisplay(figureBounds.getBottomLeft().x,
figureBounds.getBottomLeft().y);

// remove feedback from the arrow Figure and children figures
Expand Down Expand Up @@ -293,7 +293,7 @@ public void showTargetFeedback(Request request) {

@Override
public PaletteEditPart getActiveEntry() {
return (PaletteEditPart) getViewer().getEditPartRegistry().get(getStack().getActiveEntry());
return (PaletteEditPart) getViewer().getEditPartForModel(getStack().getActiveEntry());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public PinnablePaletteStackEditPart(PaletteStack model) {
public void activate() {
// in case the model is out of sync
checkActiveEntrySync();
getPaletteViewer().addPaletteListener(paletteListener);
getViewer().addPaletteListener(paletteListener);
super.activate();
}

Expand All @@ -79,7 +79,7 @@ private void activeEntryChanged(Object oldValue, Object newValue) {
int index = -1;

if (oldValue != null) {
part = (GraphicalEditPart) getViewer().getEditPartRegistry().get(oldValue);
part = (GraphicalEditPart) getViewer().getEditPartForModel(oldValue);
// if part is null, its no longer a child.
if (part != null) {
oldFigure = part.getFigure();
Expand All @@ -90,7 +90,7 @@ private void activeEntryChanged(Object oldValue, Object newValue) {
}

if (newValue != null) {
part = (GraphicalEditPart) getViewer().getEditPartRegistry().get(newValue);
part = (GraphicalEditPart) getViewer().getEditPartForModel(newValue);
newFigure = part.getFigure();
}

Expand All @@ -115,7 +115,7 @@ public PinnablePaletteStackFigure getFigure() {

@Override
public void deactivate() {
getPaletteViewer().removePaletteListener(paletteListener);
getViewer().removePaletteListener(paletteListener);
super.deactivate();
}

Expand Down Expand Up @@ -229,7 +229,7 @@ public void setPinnedOpen(boolean pinned) {

@Override
public PaletteEditPart getActiveEntry() {
return (PaletteEditPart) getViewer().getEditPartRegistry().get(getStack().getActiveEntry());
return (PaletteEditPart) getViewer().getEditPartForModel(getStack().getActiveEntry());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
import org.eclipse.draw2d.ToolbarLayout;

import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.palette.PaletteViewer;
import org.eclipse.gef.ui.palette.editparts.PaletteAnimator;
import org.eclipse.gef.ui.palette.editparts.PaletteEditPart;
import org.eclipse.gef.ui.palette.editparts.PaletteToolbarLayout;

public class SliderPaletteEditPart extends PaletteEditPart {

private PaletteAnimator controller;

public SliderPaletteEditPart(PaletteRoot paletteRoot) {
super(paletteRoot);
}
Expand Down Expand Up @@ -55,8 +52,8 @@ protected void refreshVisuals() {
@Override
protected void registerVisuals() {
super.registerVisuals();
controller = new PaletteAnimator(((PaletteViewer) getViewer()).getPaletteViewerPreferences());
getViewer().getEditPartRegistry().put(PaletteAnimator.class, controller);
PaletteAnimator controller = new PaletteAnimator(getViewer().getPaletteViewerPreferences());
getViewer().setPaletteAnimator(controller);
ToolbarLayout layout = new PaletteToolbarLayout();
getFigure().setLayoutManager(layout);
getFigure().addLayoutListener(controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected boolean handleDrag() {

@Override
protected boolean handleNativeDragFinished(DragSourceEvent event) {
getPaletteViewer().setActiveTool(null);
getViewer().setActiveTool(null);
return true;
}
}
Expand Down Expand Up @@ -195,7 +195,7 @@ protected boolean handleNativeDragStarted(DragSourceEvent event) {

// win hack because button down is delayed
if (getParent() instanceof IPaletteStackEditPart psEP && SWT.getPlatform().equals("win32")) { //$NON-NLS-1$
Point nds = getPaletteViewer().getControl().toControl(event.display.getCursorLocation());
Point nds = getViewer().getControl().toControl(event.display.getCursorLocation());
if (mouseDownLoc != null
&& (Math.abs(nds.x - mouseDownLoc.x) + Math.abs(nds.y - mouseDownLoc.y)) < WIN_THRESHOLD) {
getButtonModel().setArmed(false);
Expand Down Expand Up @@ -393,7 +393,7 @@ public void getState(AccessibleControlEvent e) {
public IFigure createFigure() {
customLabel = new DetailedLabelFigure();
Clickable button = new ToolEntryToggle(customLabel);
button.addActionListener(event -> getPaletteViewer().setActiveTool(getModel()));
button.addActionListener(event -> getViewer().setActiveTool(getModel()));
return button;
}

Expand Down Expand Up @@ -498,7 +498,7 @@ protected void refreshVisuals() {
@Override
public void removeNotify() {
if (getButtonModel().isSelected()) {
getPaletteViewer().setActiveTool(null);
getViewer().setActiveTool(null);
}
super.removeNotify();
}
Expand All @@ -510,14 +510,14 @@ public void setToolSelected(boolean value) {
@Override
public void restoreState(IMemento memento) {
if (Boolean.parseBoolean(memento.getString(ACTIVE_STATE))) {
getPaletteViewer().setActiveTool(getModel());
getViewer().setActiveTool(getModel());
}
super.restoreState(memento);
}

@Override
public void saveState(IMemento memento) {
memento.putString(ACTIVE_STATE, Boolean.toString(getPaletteViewer().getActiveTool() == getModel()));
memento.putString(ACTIVE_STATE, Boolean.toString(getViewer().getActiveTool() == getModel()));
super.saveState(memento);
}

Expand All @@ -536,8 +536,8 @@ protected void setImageInFigure(Image image) {
@Override
public void setSelected(int value) {
super.setSelected(value);
if (value == SELECTED_PRIMARY && getPaletteViewer().getControl() != null
&& !getPaletteViewer().getControl().isDisposed() && getPaletteViewer().getControl().isFocusControl()) {
if (value == SELECTED_PRIMARY && getViewer().getControl() != null && !getViewer().getControl().isDisposed()
&& getViewer().getControl().isFocusControl()) {
getFigure().requestFocus();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public DragTracker getDragTracker(Request request) {
}

public IFigure getGuideLayer() {
LayerManager lm = (LayerManager) diagramViewer.getEditPartRegistry().get(LayerManager.ID);
LayerManager lm = LayerManager.Helper.find(diagramViewer);
if (lm != null) {
return lm.getLayer(LayerConstants.GUIDE_LAYER);
}
Expand Down Expand Up @@ -208,7 +208,7 @@ public ZoomManager getZoomManager() {

public void handleGuideReparented(Object guide) {
refreshChildren();
EditPart guidePart = (EditPart) getViewer().getEditPartRegistry().get(guide);
EditPart guidePart = getViewer().getEditPartForModel(guide);
if (guidePart != null) {
getViewer().select(guidePart);
}
Expand Down
Loading

0 comments on commit e5d6bac

Please sign in to comment.