From 14d17aad263d33c8f5f82f205134bf5821d3fbfa Mon Sep 17 00:00:00 2001 From: Alois Zoitl Date: Sat, 4 Nov 2023 14:48:15 +0100 Subject: [PATCH] [Modernize Code] Utilize new generic SimpleFactory --- .../flow/FlowEditorPaletteFactory.java | 16 ++++--- .../examples/logicdesigner/LogicPlugin.java | 47 +++++++++++-------- .../gef/examples/shapes/ShapesEditor.java | 13 +++-- .../shapes/ShapesEditorPaletteFactory.java | 4 +- .../TemplateTransferDropTargetListener.java | 21 +++++---- 5 files changed, 59 insertions(+), 42 deletions(-) diff --git a/org.eclipse.gef.examples.flow/src/org/eclipse/gef/examples/flow/FlowEditorPaletteFactory.java b/org.eclipse.gef.examples.flow/src/org/eclipse/gef/examples/flow/FlowEditorPaletteFactory.java index b1d4422da..03759abfc 100644 --- a/org.eclipse.gef.examples.flow/src/org/eclipse/gef/examples/flow/FlowEditorPaletteFactory.java +++ b/org.eclipse.gef.examples.flow/src/org/eclipse/gef/examples/flow/FlowEditorPaletteFactory.java @@ -15,9 +15,8 @@ import java.util.ArrayList; import java.util.List; -import org.eclipse.gef.examples.flow.model.Activity; -import org.eclipse.gef.examples.flow.model.ParallelActivity; -import org.eclipse.gef.examples.flow.model.SequentialActivity; +import org.eclipse.jface.resource.ImageDescriptor; + import org.eclipse.gef.palette.CombinedTemplateCreationEntry; import org.eclipse.gef.palette.ConnectionCreationToolEntry; import org.eclipse.gef.palette.MarqueeToolEntry; @@ -30,7 +29,10 @@ import org.eclipse.gef.palette.SelectionToolEntry; import org.eclipse.gef.palette.ToolEntry; import org.eclipse.gef.requests.SimpleFactory; -import org.eclipse.jface.resource.ImageDescriptor; + +import org.eclipse.gef.examples.flow.model.Activity; +import org.eclipse.gef.examples.flow.model.ParallelActivity; +import org.eclipse.gef.examples.flow.model.SequentialActivity; /** * Handles the creation of the palette for the Flow Editor. @@ -53,19 +55,19 @@ private static PaletteContainer createComponentsDrawer() { List entries = new ArrayList<>(); CombinedTemplateCreationEntry combined = new CombinedTemplateCreationEntry("Activity", //$NON-NLS-1$ - "Create a new Activity Node", Activity.class, new SimpleFactory(Activity.class), //$NON-NLS-1$ + "Create a new Activity Node", Activity.class, new SimpleFactory<>(Activity.class), //$NON-NLS-1$ ImageDescriptor.createFromFile(FlowPlugin.class, "images/gear16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Activity.class, "images/gear16.gif")); //$NON-NLS-1$ entries.add(combined); combined = new CombinedTemplateCreationEntry("Sequential Activity", "Create a Sequential Activity", //$NON-NLS-1$ //$NON-NLS-2$ - SequentialActivity.class, new SimpleFactory(SequentialActivity.class), + SequentialActivity.class, new SimpleFactory<>(SequentialActivity.class), ImageDescriptor.createFromFile(FlowPlugin.class, "images/sequence16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(FlowPlugin.class, "images/sequence16.gif")); //$NON-NLS-1$ entries.add(combined); combined = new CombinedTemplateCreationEntry("Parallel Activity", "Create a Parallel Activity", //$NON-NLS-1$ //$NON-NLS-2$ - ParallelActivity.class, new SimpleFactory(ParallelActivity.class), + ParallelActivity.class, new SimpleFactory<>(ParallelActivity.class), ImageDescriptor.createFromFile(FlowPlugin.class, "images/parallel16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(FlowPlugin.class, "images/parallel16.gif")); //$NON-NLS-1$ entries.add(combined); diff --git a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicPlugin.java b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicPlugin.java index dfe77dd42..ba3ab5b16 100644 --- a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicPlugin.java +++ b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/LogicPlugin.java @@ -58,16 +58,20 @@ public class LogicPlugin extends org.eclipse.ui.plugin.AbstractUIPlugin { private static LogicPlugin singleton; - public static Dimension getMaximumSizeFor(Class modelClass) { + public static Dimension getMaximumSizeFor(Class modelClass) { if (LED.class.equals(modelClass)) { return LEDFigure.SIZE; - } else if (AndGate.class.equals(modelClass)) { + } + if (AndGate.class.equals(modelClass)) { return AndGateFigure.SIZE; - } else if (OrGate.class.equals(modelClass)) { + } + if (OrGate.class.equals(modelClass)) { return OrGateFigure.SIZE; - } else if (XORGate.class.equals(modelClass)) { + } + if (XORGate.class.equals(modelClass)) { return XOrGateFigure.SIZE; - } else if (GroundOutput.class.isAssignableFrom(modelClass)) { + } + if (GroundOutput.class.isAssignableFrom(modelClass)) { return GroundFigure.SIZE; } else if (LiveOutput.class.equals(modelClass)) { return LiveOutputFigure.SIZE; @@ -75,16 +79,20 @@ public static Dimension getMaximumSizeFor(Class modelClass) { return IFigure.MAX_DIMENSION; } - public static Dimension getMinimumSizeFor(Class modelClass) { + public static Dimension getMinimumSizeFor(Class modelClass) { if (LogicLabel.class.equals(modelClass)) { return new Dimension(IFigure.MIN_DIMENSION.width, 30); - } else if (Circuit.class.equals(modelClass)) { + } + if (Circuit.class.equals(modelClass)) { return new Dimension(25, 20); - } else if (LED.class.equals(modelClass)) { + } + if (LED.class.equals(modelClass)) { return LEDFigure.SIZE; - } else if (AndGate.class.equals(modelClass)) { + } + if (AndGate.class.equals(modelClass)) { return AndGateFigure.SIZE; - } else if (OrGate.class.equals(modelClass)) { + } + if (OrGate.class.equals(modelClass)) { return OrGateFigure.SIZE; } else if (XORGate.class.equals(modelClass)) { return XOrGateFigure.SIZE; @@ -145,7 +153,7 @@ private static PaletteContainer createComponentsDrawer() { CombinedTemplateCreationEntry combined = new CombinedTemplateCreationEntry( LogicMessages.LogicPlugin_Tool_CreationTool_FlowContainer_Label, LogicMessages.LogicPlugin_Tool_CreationTool_FlowContainer_Description, - new SimpleFactory(LogicFlowContainer.class), + new SimpleFactory<>(LogicFlowContainer.class), ImageDescriptor.createFromFile(Circuit.class, "icons/logicflow16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Circuit.class, "icons/logicflow24.gif")//$NON-NLS-1$ ); @@ -153,7 +161,7 @@ private static PaletteContainer createComponentsDrawer() { entries.add(combined); combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_Circuit_Label, - LogicMessages.LogicPlugin_Tool_CreationTool_Circuit_Description, new SimpleFactory(Circuit.class), + LogicMessages.LogicPlugin_Tool_CreationTool_Circuit_Description, new SimpleFactory<>(Circuit.class), ImageDescriptor.createFromFile(Circuit.class, "icons/circuit16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Circuit.class, "icons/circuit24.gif")//$NON-NLS-1$ ); @@ -163,7 +171,7 @@ LogicMessages.LogicPlugin_Tool_CreationTool_Circuit_Description, new SimpleFacto entries.add(new PaletteSeparator()); combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_Label_Label, - LogicMessages.LogicPlugin_Tool_CreationTool_Label_Description, new SimpleFactory(LogicLabel.class), + LogicMessages.LogicPlugin_Tool_CreationTool_Label_Description, new SimpleFactory<>(LogicLabel.class), ImageDescriptor.createFromFile(Circuit.class, "icons/label16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Circuit.class, "icons/label24.gif")//$NON-NLS-1$ ); @@ -171,7 +179,7 @@ LogicMessages.LogicPlugin_Tool_CreationTool_Label_Description, new SimpleFactory entries.add(combined); combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_LED_Label, - LogicMessages.LogicPlugin_Tool_CreationTool_LED_Description, new SimpleFactory(LED.class), + LogicMessages.LogicPlugin_Tool_CreationTool_LED_Description, new SimpleFactory<>(LED.class), ImageDescriptor.createFromFile(Circuit.class, "icons/ledicon16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Circuit.class, "icons/ledicon24.gif")//$NON-NLS-1$ ); @@ -179,7 +187,7 @@ LogicMessages.LogicPlugin_Tool_CreationTool_LED_Description, new SimpleFactory(L entries.add(combined); combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_ORGate_Label, - LogicMessages.LogicPlugin_Tool_CreationTool_ORGate_Description, new SimpleFactory(OrGate.class), + LogicMessages.LogicPlugin_Tool_CreationTool_ORGate_Description, new SimpleFactory<>(OrGate.class), ImageDescriptor.createFromFile(Circuit.class, "icons/or16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Circuit.class, "icons/or24.gif")//$NON-NLS-1$ ); @@ -187,7 +195,7 @@ LogicMessages.LogicPlugin_Tool_CreationTool_ORGate_Description, new SimpleFactor entries.add(combined); combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_XORGate_Label, - LogicMessages.LogicPlugin_Tool_CreationTool_XORGate_Description, new SimpleFactory(XORGate.class), + LogicMessages.LogicPlugin_Tool_CreationTool_XORGate_Description, new SimpleFactory<>(XORGate.class), ImageDescriptor.createFromFile(Circuit.class, "icons/xor16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Circuit.class, "icons/xor24.gif")//$NON-NLS-1$ ); @@ -195,7 +203,7 @@ LogicMessages.LogicPlugin_Tool_CreationTool_XORGate_Description, new SimpleFacto entries.add(combined); combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_ANDGate_Label, - LogicMessages.LogicPlugin_Tool_CreationTool_ANDGate_Description, new SimpleFactory(AndGate.class), + LogicMessages.LogicPlugin_Tool_CreationTool_ANDGate_Description, new SimpleFactory<>(AndGate.class), ImageDescriptor.createFromFile(Circuit.class, "icons/and16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Circuit.class, "icons/and24.gif")//$NON-NLS-1$ ); @@ -207,7 +215,8 @@ LogicMessages.LogicPlugin_Tool_CreationTool_ANDGate_Description, new SimpleFacto LogicMessages.LogicPlugin_Tool_CreationTool_LiveGroundStack_Description, null); combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_LiveOutput_Label, - LogicMessages.LogicPlugin_Tool_CreationTool_LiveOutput_Description, new SimpleFactory(LiveOutput.class), + LogicMessages.LogicPlugin_Tool_CreationTool_LiveOutput_Description, + new SimpleFactory<>(LiveOutput.class), ImageDescriptor.createFromFile(Circuit.class, "icons/live16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Circuit.class, "icons/live24.gif")//$NON-NLS-1$ ); @@ -215,7 +224,7 @@ LogicMessages.LogicPlugin_Tool_CreationTool_LiveOutput_Description, new SimpleFa liveGroundStack.add(combined); combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_Ground_Label, - LogicMessages.LogicPlugin_Tool_CreationTool_Ground_Description, new SimpleFactory(GroundOutput.class), + LogicMessages.LogicPlugin_Tool_CreationTool_Ground_Description, new SimpleFactory<>(GroundOutput.class), ImageDescriptor.createFromFile(Circuit.class, "icons/ground16.gif"), //$NON-NLS-1$ ImageDescriptor.createFromFile(Circuit.class, "icons/ground24.gif")//$NON-NLS-1$ ); diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java index 87999d882..e30c0f1dc 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java @@ -163,7 +163,7 @@ private TransferDropTargetListener createTransferDropTargetListener() { return new TemplateTransferDropTargetListener(getGraphicalViewer()) { @Override protected CreationFactory getFactory(Object template) { - return new SimpleFactory((Class) template); + return new SimpleFactory<>((Class) template); } }; } @@ -254,8 +254,9 @@ public void execute(final IProgressMonitor monitor) { @Override public T getAdapter(final Class type) { - if (type == IContentOutlinePage.class) + if (type == IContentOutlinePage.class) { return type.cast(new ShapesOutlinePage(new TreeViewer())); + } return super.getAdapter(type); } @@ -271,8 +272,9 @@ ShapesDiagram getModel() { */ @Override protected PaletteRoot getPaletteRoot() { - if (PALETTE_MODEL == null) + if (PALETTE_MODEL == null) { PALETTE_MODEL = ShapesEditorPaletteFactory.createPalette(); + } return PALETTE_MODEL; } @@ -332,6 +334,8 @@ protected void setInput(IEditorInput input) { * Creates an outline pagebook for this editor. */ public class ShapesOutlinePage extends ContentOutlinePage { + private static final String SHAPES_OUTLINE_CONTEXTMENU = "org.eclipse.gef.examples.shapes.outline.contextmenu"; //$NON-NLS-1$ + /** * Create a new outline page for the shapes editor. * @@ -358,8 +362,7 @@ public void createControl(Composite parent) { // configure & add context menu to viewer ContextMenuProvider cmProvider = new ShapesEditorContextMenuProvider(getViewer(), getActionRegistry()); getViewer().setContextMenu(cmProvider); - getSite().registerContextMenu("org.eclipse.gef.examples.shapes.outline.contextmenu", cmProvider, - getSite().getSelectionProvider()); + getSite().registerContextMenu(SHAPES_OUTLINE_CONTEXTMENU, cmProvider, getSite().getSelectionProvider()); // hook outline viewer getSelectionSynchronizer().addViewer(getViewer()); // initialize outline viewer with model diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditorPaletteFactory.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditorPaletteFactory.java index f7f97fdb7..bd2db4095 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditorPaletteFactory.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditorPaletteFactory.java @@ -43,13 +43,13 @@ private static PaletteContainer createShapesDrawer() { PaletteDrawer componentsDrawer = new PaletteDrawer("Shapes"); CombinedTemplateCreationEntry component = new CombinedTemplateCreationEntry("Ellipse", - "Create an elliptical shape", EllipticalShape.class, new SimpleFactory(EllipticalShape.class), + "Create an elliptical shape", EllipticalShape.class, new SimpleFactory<>(EllipticalShape.class), ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/ellipse16.gif"), ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/ellipse24.gif")); componentsDrawer.add(component); component = new CombinedTemplateCreationEntry("Rectangle", "Create a rectangular shape", RectangularShape.class, - new SimpleFactory(RectangularShape.class), + new SimpleFactory<>(RectangularShape.class), ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/rectangle16.gif"), ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/rectangle24.gif")); componentsDrawer.add(component); 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 a65af8d76..a778a0ea5 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/dnd/TemplateTransferDropTargetListener.java +++ b/org.eclipse.gef/src/org/eclipse/gef/dnd/TemplateTransferDropTargetListener.java @@ -73,13 +73,15 @@ protected final CreateRequest getCreateRequest() { * @param template the template Object * @return a Factory */ + @SuppressWarnings("static-method") // allow children to override this method protected CreationFactory getFactory(Object template) { - if (template instanceof CreationFactory) { - return ((CreationFactory) template); - } else if (template instanceof Class) { - return new SimpleFactory((Class) template); - } else - return null; + if (template instanceof CreationFactory creationFactory) { + return creationFactory; + } + if (template instanceof Class clazz) { + return new SimpleFactory<>(clazz); + } + return null; } /** @@ -120,15 +122,16 @@ protected void handleDrop() { private void selectAddedObject() { Object model = getCreateRequest().getNewObject(); - if (model == null) + if (model == null) { return; + } EditPartViewer viewer = getViewer(); viewer.getControl().forceFocus(); Object editpart = viewer.getEditPartRegistry().get(model); - if (editpart instanceof EditPart) { + if (editpart instanceof EditPart ep) { // Force a layout first. getViewer().flush(); - viewer.select((EditPart) editpart); + viewer.select(ep); } }