From e5d6bac76ff38625b7c04877c0582452f04055ff Mon Sep 17 00:00:00 2001 From: Alois Zoitl Date: Sat, 10 Aug 2024 16:55:05 +0200 Subject: [PATCH] Added type information to getEditPartRegisry #155 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. https://github.com/eclipse/gef-classic/issues/155 --- .../logicdesigner/edit/LogicEditPart.java | 7 ++-- .../logicdesigner/edit/LogicTreeEditPart.java | 7 ++-- .../shapes/parts/DiagramTreeEditPart.java | 2 +- .../model/commands/ExampleTextCommand.java | 2 +- org.eclipse.gef/.settings/.api_filters | 8 +++++ .../src/org/eclipse/gef/EditPartViewer.java | 15 ++++++++- .../TemplateTransferDropTargetListener.java | 3 +- .../gef/editparts/AbstractEditPart.java | 2 +- .../editparts/AbstractGraphicalEditPart.java | 6 ++-- .../eclipse/gef/editparts/LayerManager.java | 14 +++++++- .../ui/palette/editparts/DrawerEditPart.java | 2 +- .../editparts/PaletteStackEditPart.java | 16 ++++----- .../PinnablePaletteStackEditPart.java | 10 +++--- .../editparts/SliderPaletteEditPart.java | 7 ++-- .../palette/editparts/ToolEntryEditPart.java | 16 ++++----- .../gef/internal/ui/rulers/RulerEditPart.java | 4 +-- .../print/PrintGraphicalViewerOperation.java | 2 +- .../org/eclipse/gef/rulers/RulerProvider.java | 2 +- .../org/eclipse/gef/tools/AbstractTool.java | 4 +-- .../org/eclipse/gef/tools/CreationTool.java | 6 ++-- .../gef/tools/DragEditPartsTracker.java | 2 +- .../gef/ui/actions/CreateGuideAction.java | 5 ++- .../actions/SetActivePaletteToolAction.java | 6 ++-- .../eclipse/gef/ui/palette/PaletteViewer.java | 33 ++++++++++++++++--- .../ui/palette/editparts/PaletteEditPart.java | 19 ++++++++--- .../gef/ui/parts/AbstractEditPartViewer.java | 10 +++++- .../gef/ui/parts/GraphicalViewerImpl.java | 4 +-- .../gef/ui/parts/PaletteViewerKeyHandler.java | 2 +- .../gef/ui/parts/SelectionSynchronizer.java | 8 ++--- .../parts/TreeViewerTransferDragListener.java | 3 +- .../ui/views/palette/PaletteViewerPage.java | 8 ++--- 31 files changed, 149 insertions(+), 86 deletions(-) diff --git a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/edit/LogicEditPart.java b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/edit/LogicEditPart.java index fff6e5ac5..9405d12a3 100644 --- a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/edit/LogicEditPart.java +++ b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/edit/LogicEditPart.java @@ -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; @@ -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(); diff --git a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/edit/LogicTreeEditPart.java b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/edit/LogicTreeEditPart.java index 82eeddfc4..aac9df05e 100644 --- a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/edit/LogicTreeEditPart.java +++ b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/edit/LogicTreeEditPart.java @@ -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; @@ -92,12 +91,12 @@ protected List 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(); diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/parts/DiagramTreeEditPart.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/parts/DiagramTreeEditPart.java index 9976f1465..a1202fde4 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/parts/DiagramTreeEditPart.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/parts/DiagramTreeEditPart.java @@ -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); } /* diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ExampleTextCommand.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ExampleTextCommand.java index b9dcdd1c2..bdb3c9e5d 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ExampleTextCommand.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ExampleTextCommand.java @@ -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); } } diff --git a/org.eclipse.gef/.settings/.api_filters b/org.eclipse.gef/.settings/.api_filters index 84a9edc8b..1dc315cd6 100644 --- a/org.eclipse.gef/.settings/.api_filters +++ b/org.eclipse.gef/.settings/.api_filters @@ -64,6 +64,14 @@ + + + + + + + + diff --git a/org.eclipse.gef/src/org/eclipse/gef/EditPartViewer.java b/org.eclipse.gef/src/org/eclipse/gef/EditPartViewer.java index 1495fd5db..d70966d32 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/EditPartViewer.java +++ b/org.eclipse.gef/src/org/eclipse/gef/EditPartViewer.java @@ -281,7 +281,20 @@ interface Conditional { * * @return the registry map */ - Map getEditPartRegistry(); + Map 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 focus EditPart. Focus refers to keyboard diff --git a/org.eclipse.gef/src/org/eclipse/gef/dnd/TemplateTransferDropTargetListener.java b/org.eclipse.gef/src/org/eclipse/gef/dnd/TemplateTransferDropTargetListener.java index a778a0ea5..b6c2fa78e 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/dnd/TemplateTransferDropTargetListener.java +++ b/org.eclipse.gef/src/org/eclipse/gef/dnd/TemplateTransferDropTargetListener.java @@ -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); diff --git a/org.eclipse.gef/src/org/eclipse/gef/editparts/AbstractEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/editparts/AbstractEditPart.java index 3c58bc94b..2da7711b9 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/editparts/AbstractEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/editparts/AbstractEditPart.java @@ -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 registry = getViewer().getEditPartRegistry(); if (registry.get(getModel()) == this) { registry.remove(getModel()); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/editparts/AbstractGraphicalEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/editparts/AbstractGraphicalEditPart.java index bb31c9c2f..7fd9b233a 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/editparts/AbstractGraphicalEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/editparts/AbstractGraphicalEditPart.java @@ -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); @@ -484,8 +483,7 @@ public IFigure getFigure() { * @return The requested layer or null 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); } /** diff --git a/org.eclipse.gef/src/org/eclipse/gef/editparts/LayerManager.java b/org.eclipse.gef/src/org/eclipse/gef/editparts/LayerManager.java index 90d102a46..c30601d1a 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/editparts/LayerManager.java +++ b/org.eclipse.gef/src/org/eclipse/gef/editparts/LayerManager.java @@ -15,6 +15,7 @@ import org.eclipse.draw2d.IFigure; import org.eclipse.gef.EditPart; +import org.eclipse.gef.EditPartViewer; /** * Responsible for locating layers in a GraphicalViewer. @@ -57,7 +58,18 @@ class Helper { * @return the LayerManager */ 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 LayerManager + * @since 3.19 + */ + public static LayerManager find(EditPartViewer viewer) { + return (LayerManager) viewer.getEditPartForModel(ID); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerEditPart.java index d88cafc57..cf546c36b 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerEditPart.java @@ -104,7 +104,7 @@ public T getAdapter(final Class key) { } private PaletteAnimator getPaletteAnimator() { - return (PaletteAnimator) getViewer().getEditPartRegistry().get(PaletteAnimator.class); + return getViewer().getPaletteAnimator(); } @Override diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PaletteStackEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PaletteStackEditPart.java index 7843b72db..a7a069531 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PaletteStackEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PaletteStackEditPart.java @@ -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(); } @@ -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); @@ -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(); @@ -198,7 +198,7 @@ public void deactivate() { } arrowFigure.removeActionListener(actionListener); arrowFigure.removeChangeListener(clickableArrowListener); - getPaletteViewer().removePaletteListener(paletteListener); + getViewer().removePaletteListener(paletteListener); super.deactivate(); } @@ -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 @@ -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()); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PinnablePaletteStackEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PinnablePaletteStackEditPart.java index 20cb4089c..420d31d04 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PinnablePaletteStackEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PinnablePaletteStackEditPart.java @@ -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(); } @@ -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(); @@ -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(); } @@ -115,7 +115,7 @@ public PinnablePaletteStackFigure getFigure() { @Override public void deactivate() { - getPaletteViewer().removePaletteListener(paletteListener); + getViewer().removePaletteListener(paletteListener); super.deactivate(); } @@ -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()); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/SliderPaletteEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/SliderPaletteEditPart.java index 27963a45e..ccbfaf835 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/SliderPaletteEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/SliderPaletteEditPart.java @@ -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); } @@ -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); diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/ToolEntryEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/ToolEntryEditPart.java index 1ef1cd761..5d36a607c 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/ToolEntryEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/ToolEntryEditPart.java @@ -118,7 +118,7 @@ protected boolean handleDrag() { @Override protected boolean handleNativeDragFinished(DragSourceEvent event) { - getPaletteViewer().setActiveTool(null); + getViewer().setActiveTool(null); return true; } } @@ -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); @@ -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; } @@ -498,7 +498,7 @@ protected void refreshVisuals() { @Override public void removeNotify() { if (getButtonModel().isSelected()) { - getPaletteViewer().setActiveTool(null); + getViewer().setActiveTool(null); } super.removeNotify(); } @@ -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); } @@ -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(); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/rulers/RulerEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/rulers/RulerEditPart.java index 5129998b1..726e0a904 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/rulers/RulerEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/rulers/RulerEditPart.java @@ -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); } @@ -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); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/print/PrintGraphicalViewerOperation.java b/org.eclipse.gef/src/org/eclipse/gef/print/PrintGraphicalViewerOperation.java index 4723a95de..99f20ea79 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/print/PrintGraphicalViewerOperation.java +++ b/org.eclipse.gef/src/org/eclipse/gef/print/PrintGraphicalViewerOperation.java @@ -47,7 +47,7 @@ public class PrintGraphicalViewerOperation extends PrintFigureOperation { public PrintGraphicalViewerOperation(Printer p, GraphicalViewer g) { super(p); viewer = g; - LayerManager lm = (LayerManager) viewer.getEditPartRegistry().get(LayerManager.ID); + LayerManager lm = LayerManager.Helper.find(viewer); IFigure f = lm.getLayer(LayerConstants.PRINTABLE_LAYERS); setPrintSource(f); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/rulers/RulerProvider.java b/org.eclipse.gef/src/org/eclipse/gef/rulers/RulerProvider.java index 5cbb63e6f..c69925bf2 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/rulers/RulerProvider.java +++ b/org.eclipse.gef/src/org/eclipse/gef/rulers/RulerProvider.java @@ -161,7 +161,7 @@ public List getAttachedEditParts(Object guide, GraphicalView List attachedEditParts = new ArrayList<>(attachedModelObjects.size()); for (Object attachedModelObject : attachedModelObjects) { - EditPart editPart = (EditPart) viewer.getEditPartRegistry().get(attachedModelObject); + EditPart editPart = viewer.getEditPartForModel(attachedModelObject); if (editPart != null) { attachedEditParts.add(editPart); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/tools/AbstractTool.java b/org.eclipse.gef/src/org/eclipse/gef/tools/AbstractTool.java index 54b9afa44..b952cdcb8 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/tools/AbstractTool.java +++ b/org.eclipse.gef/src/org/eclipse/gef/tools/AbstractTool.java @@ -296,7 +296,7 @@ public void activate() { * @param figure the feedback being added */ protected void addFeedback(IFigure figure) { - LayerManager lm = (LayerManager) getCurrentViewer().getEditPartRegistry().get(LayerManager.ID); + LayerManager lm = LayerManager.Helper.find(getCurrentViewer()); if (lm == null) { return; } @@ -1376,7 +1376,7 @@ protected void releaseToolCapture() { * @param figure the figure being removed */ protected void removeFeedback(IFigure figure) { - LayerManager lm = (LayerManager) getCurrentViewer().getEditPartRegistry().get(LayerManager.ID); + LayerManager lm = LayerManager.Helper.find(getCurrentViewer()); if (lm == null) { return; } diff --git a/org.eclipse.gef/src/org/eclipse/gef/tools/CreationTool.java b/org.eclipse.gef/src/org/eclipse/gef/tools/CreationTool.java index 3d5911d5c..707abe546 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/tools/CreationTool.java +++ b/org.eclipse.gef/src/org/eclipse/gef/tools/CreationTool.java @@ -292,11 +292,11 @@ private void selectAddedObject(EditPartViewer viewer) { if (model == null || viewer == null) { return; } - Object editpart = viewer.getEditPartRegistry().get(model); + EditPart editpart = viewer.getEditPartForModel(model); viewer.flush(); - if (editpart instanceof EditPart ep && ep.isSelectable()) { + if (editpart != null && editpart.isSelectable()) { // Force the new object to get positioned in the viewer. - viewer.select((EditPart) editpart); + viewer.select(editpart); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/tools/DragEditPartsTracker.java b/org.eclipse.gef/src/org/eclipse/gef/tools/DragEditPartsTracker.java index 851f763f0..08beb8d9d 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/tools/DragEditPartsTracker.java +++ b/org.eclipse.gef/src/org/eclipse/gef/tools/DragEditPartsTracker.java @@ -363,7 +363,7 @@ protected Collection getExclusionSet() { GraphicalEditPart editpart = (GraphicalEditPart) element; exclusionSet.add(editpart.getFigure()); } - LayerManager layerManager = (LayerManager) getCurrentViewer().getEditPartRegistry().get(LayerManager.ID); + LayerManager layerManager = LayerManager.Helper.find(getCurrentViewer()); if (layerManager != null) { exclusionSet.add(layerManager.getLayer(LayerConstants.CONNECTION_LAYER)); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/CreateGuideAction.java b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/CreateGuideAction.java index e6165ba87..74303129b 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/CreateGuideAction.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/CreateGuideAction.java @@ -16,7 +16,6 @@ import org.eclipse.jface.action.Action; -import org.eclipse.gef.EditPart; import org.eclipse.gef.EditPartViewer; import org.eclipse.gef.internal.GEFMessages; import org.eclipse.gef.internal.ui.rulers.GuideEditPart; @@ -31,7 +30,7 @@ */ public class CreateGuideAction extends Action { - private EditPartViewer viewer; + private final EditPartViewer viewer; /** * Constructor @@ -71,7 +70,7 @@ public void run() { // Create the guide and reveal it viewer.getEditDomain().getCommandStack().execute(provider.getCreateGuideCommand(newPosition)); - viewer.reveal((EditPart) viewer.getEditPartRegistry().get(provider.getGuideAt(newPosition))); + viewer.reveal(viewer.getEditPartForModel(provider.getGuideAt(newPosition))); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/SetActivePaletteToolAction.java b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/SetActivePaletteToolAction.java index 646ed9b3d..815fbf63c 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/actions/SetActivePaletteToolAction.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/actions/SetActivePaletteToolAction.java @@ -24,8 +24,8 @@ */ public class SetActivePaletteToolAction extends Action { - private PaletteViewer viewer; - private ToolEntry entry; + private final PaletteViewer viewer; + private final ToolEntry entry; /** * Creates a new SetActivePaletteToolAction with the given entry to set, as well @@ -57,7 +57,7 @@ public void run() { // context menu is open for palette stacks so we need to indicate // that the focus needs to change to the tool entry that the user // has just selected. See GraphicalViewerKeyHandler.procesSelect(). - EditPart part = (EditPart) viewer.getEditPartRegistry().get(entry); + EditPart part = viewer.getEditPartForModel(entry); viewer.appendSelection(part); viewer.setFocus(part); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteViewer.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteViewer.java index fe65c1019..a6b985ecc 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteViewer.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteViewer.java @@ -42,6 +42,7 @@ import org.eclipse.gef.palette.PaletteStack; import org.eclipse.gef.palette.ToolEntry; import org.eclipse.gef.ui.palette.customize.PaletteCustomizerDialog; +import org.eclipse.gef.ui.palette.editparts.PaletteAnimator; import org.eclipse.gef.ui.palette.editparts.PaletteEditPart; import org.eclipse.gef.ui.parts.PaletteViewerKeyHandler; import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer; @@ -89,6 +90,7 @@ private void refreshAllEditParts(EditPart part) { private final PreferenceListener prefListener = new PreferenceListener(); private PaletteViewerPreferences prefs = PREFERENCE_STORE; private Font font = null; + private PaletteAnimator paletteAnimator; /** * Constructor @@ -195,6 +197,16 @@ public ToolEntry getActiveTool() { return activeEntry; } + /** + * Gets the PaletteAnimator for this palette + * + * @return the palletteAnimator to be used for palette animations + * @since 3.19 + */ + public PaletteAnimator getPaletteAnimator() { + return paletteAnimator; + } + /** * Returns the palette's root model. * @@ -214,7 +226,7 @@ public PaletteViewerPreferences getPaletteViewerPreferences() { } private ToolEntryEditPart getToolEntryEditPart(ToolEntry entry) { - return (ToolEntryEditPart) getEditPartRegistry().get(entry); + return (ToolEntryEditPart) getEditPartForModel(entry); } /** @@ -271,7 +283,7 @@ protected void hookControl() { * @return true if expanded */ public boolean isExpanded(PaletteDrawer drawer) { - EditPart ep = (EditPart) getEditPartRegistry().get(drawer); + EditPart ep = getEditPartForModel(drawer); if (ep instanceof DrawerEditPart dep) { return dep.isExpanded(); } @@ -285,7 +297,7 @@ public boolean isExpanded(PaletteDrawer drawer) { * @return true if pinned */ public boolean isPinned(PaletteDrawer drawer) { - EditPart ep = (EditPart) getEditPartRegistry().get(drawer); + EditPart ep = getEditPartForModel(drawer); if (ep instanceof DrawerEditPart dep) { return dep.isPinnedOpen(); } @@ -316,7 +328,7 @@ public void removePaletteListener(PaletteListener paletteListener) { */ public boolean restoreState(IMemento memento) { try { - PaletteEditPart part = (PaletteEditPart) getEditPartRegistry().get(getPaletteRoot()); + PaletteEditPart part = (PaletteEditPart) getEditPartForModel(getPaletteRoot()); if (part != null) { part.restoreState(memento); } @@ -357,7 +369,7 @@ public void reveal(EditPart part) { */ public void saveState(IMemento memento) { // Bug# 69026 - The PaletteRoot can be null initially for VEP - PaletteEditPart base = (PaletteEditPart) getEditPartRegistry().get(getPaletteRoot()); + PaletteEditPart base = (PaletteEditPart) getEditPartForModel(getPaletteRoot()); if (base != null) { base.saveState(memento); } @@ -396,6 +408,17 @@ public void setActiveTool(ToolEntry newMode) { fireModeChanged(); } + /** + * Sets the PaletteAnimator for this palette + * + * @param paletteAnimator the PaletteAnimator to be used for this + * palletteViewer's children + * @since 3.19 + */ + public void setPaletteAnimator(PaletteAnimator paletteAnimator) { + this.paletteAnimator = paletteAnimator; + } + /** * Sets the root for this palette. * diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/editparts/PaletteEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/editparts/PaletteEditPart.java index 3725f5e3d..af0de1a38 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/editparts/PaletteEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/editparts/PaletteEditPart.java @@ -274,9 +274,9 @@ public PaletteEntry getModel() { * Get the model element for this palette edit part. * * @return the model element. - * @deprecated use getModel() instead + * @deprecated use {@link #getModel()} instead */ - @Deprecated + @Deprecated(since = "3.19", forRemoval = true) protected PaletteEntry getPaletteEntry() { return getModel(); } @@ -285,9 +285,11 @@ protected PaletteEntry getPaletteEntry() { * Get the palette viewer for this palette edit part. * * @return the palette viewer. + * @deprecated use {@link #getViewer()} instead */ + @Deprecated(since = "3.19", forRemoval = true) protected PaletteViewer getPaletteViewer() { - return (PaletteViewer) getViewer(); + return getViewer(); } /** @@ -296,7 +298,7 @@ protected PaletteViewer getPaletteViewer() { * @return the palette viewer preferences. */ protected PaletteViewerPreferences getPreferenceSource() { - return ((PaletteViewer) getViewer()).getPaletteViewerPreferences(); + return getViewer().getPaletteViewerPreferences(); } /** @@ -336,6 +338,15 @@ protected String getToolTipText() { return text; } + /** + * @see org.eclipse.gef.EditPart#getViewer() + * @since 3.19 + */ + @Override + public PaletteViewer getViewer() { + return (PaletteViewer) super.getViewer(); + } + /** * Determine if the name is needed in the tool tip. * diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/AbstractEditPartViewer.java b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/AbstractEditPartViewer.java index 989d02608..e056be416 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/AbstractEditPartViewer.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/AbstractEditPartViewer.java @@ -354,10 +354,18 @@ public EditPartFactory getEditPartFactory() { * @see EditPartViewer#getEditPartRegistry() */ @Override - public Map getEditPartRegistry() { + public Map getEditPartRegistry() { return mapIDToEditPart; } + /** + * @see EditPartViewer#getEditPartForModel(Object) + */ + @Override + public final EditPart getEditPartForModel(Object model) { + return getEditPartRegistry().get(model); + } + /** * @see EditPartViewer#getFocusEditPart() */ diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/GraphicalViewerImpl.java b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/GraphicalViewerImpl.java index 09349eb3e..cdc7416e8 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/GraphicalViewerImpl.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/GraphicalViewerImpl.java @@ -140,7 +140,7 @@ protected void handleFocusLost(FocusEvent fe) { */ @Override public Handle findHandleAt(Point p) { - LayerManager layermanager = (LayerManager) getEditPartRegistry().get(LayerManager.ID); + LayerManager layermanager = getLayerManager(); if (layermanager == null) { return null; } @@ -216,7 +216,7 @@ protected DomainEventDispatcher getEventDispatcher() { * @return the LayerManager */ protected LayerManager getLayerManager() { - return (LayerManager) getEditPartRegistry().get(LayerManager.ID); + return LayerManager.Helper.find(this); } /** diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/PaletteViewerKeyHandler.java b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/PaletteViewerKeyHandler.java index 53d1618f5..d0d80265c 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/PaletteViewerKeyHandler.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/PaletteViewerKeyHandler.java @@ -266,7 +266,7 @@ protected void navigateTo(EditPart part, KeyEvent event) { } if (part instanceof IPaletteStackEditPart) { PaletteEntry activeEntry = ((PaletteStack) part.getModel()).getActiveEntry(); - part = (EditPart) getViewer().getEditPartRegistry().get(activeEntry); + part = getViewer().getEditPartForModel(activeEntry); } getViewer().select(part); getViewer().reveal(part); diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/SelectionSynchronizer.java b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/SelectionSynchronizer.java index 76e508f43..13a671b14 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/SelectionSynchronizer.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/SelectionSynchronizer.java @@ -61,13 +61,9 @@ public void addViewer(EditPartViewer viewer) { * @param part a part from another viewer * @return null or a corresponding editpart */ + @SuppressWarnings("static-method") protected EditPart convert(EditPartViewer viewer, EditPart part) { - Object temp = viewer.getEditPartRegistry().get(part.getModel()); - EditPart newPart = null; - if (temp != null) { - newPart = (EditPart) temp; - } - return newPart; + return viewer.getEditPartForModel(part.getModel()); } /** diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/TreeViewerTransferDragListener.java b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/TreeViewerTransferDragListener.java index 035d7247a..62916a9b0 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/parts/TreeViewerTransferDragListener.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/parts/TreeViewerTransferDragListener.java @@ -35,6 +35,7 @@ public TreeViewerTransferDragListener(EditPartViewer viewer) { /** * @deprecated */ + @Deprecated public TreeViewerTransferDragListener(EditPartViewer viewer, Transfer xfer) { super(viewer, xfer); } @@ -67,7 +68,7 @@ protected void revertModelSelection() { List list = new ArrayList(); Object editpart; for (int i = 0; i < modelSelection.size(); i++) { - editpart = getViewer().getEditPartRegistry().get(modelSelection.get(i)); + editpart = getViewer().getEditPartForModel(modelSelection.get(i)); if (editpart != null) { list.add(editpart); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/views/palette/PaletteViewerPage.java b/org.eclipse.gef/src/org/eclipse/gef/ui/views/palette/PaletteViewerPage.java index 05977a5b3..970eaac5e 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/views/palette/PaletteViewerPage.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/views/palette/PaletteViewerPage.java @@ -84,12 +84,12 @@ public void dispose() { @Override public T getAdapter(final Class adapter) { if (adapter == EditPart.class && viewer != null) { - return adapter.cast(viewer.getEditPartRegistry().get(viewer.getPaletteRoot())); + return adapter.cast(viewer.getEditPartForModel(viewer.getPaletteRoot())); } if (adapter == IFigure.class && viewer != null) { - Object obj = viewer.getEditPartRegistry().get(viewer.getPaletteRoot()); - if (obj instanceof GraphicalEditPart) { - return adapter.cast(((GraphicalEditPart) obj).getFigure()); + EditPart ep = viewer.getEditPartForModel(viewer.getPaletteRoot()); + if (ep instanceof GraphicalEditPart gep) { + return adapter.cast(gep.getFigure()); } } return null;