Skip to content

Commit

Permalink
[Modernize Code] Utilize new generic SimpleFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
azoitl committed Nov 5, 2023
1 parent f5ae4fa commit 14d17aa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -53,19 +55,19 @@ private static PaletteContainer createComponentsDrawer() {
List<PaletteEntry> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,41 @@ public class LogicPlugin extends org.eclipse.ui.plugin.AbstractUIPlugin {

private static LogicPlugin singleton;

public static Dimension getMaximumSizeFor(Class modelClass) {
public static <T> Dimension getMaximumSizeFor(Class<T> 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;
}
return IFigure.MAX_DIMENSION;
}

public static Dimension getMinimumSizeFor(Class modelClass) {
public static <T> Dimension getMinimumSizeFor(Class<T> 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;
Expand Down Expand Up @@ -145,15 +153,15 @@ 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$
);
combined.setToolClass(LogicCreationTool.class);
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$
);
Expand All @@ -163,39 +171,39 @@ 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$
);
combined.setToolClass(LogicCreationTool.class);
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$
);
combined.setToolClass(LogicCreationTool.class);
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$
);
combined.setToolClass(LogicCreationTool.class);
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$
);
combined.setToolClass(LogicCreationTool.class);
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$
);
Expand All @@ -207,15 +215,16 @@ 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$
);
combined.setToolClass(LogicCreationTool.class);
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$
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}
Expand Down Expand Up @@ -254,8 +254,9 @@ public void execute(final IProgressMonitor monitor) {

@Override
public <T> T getAdapter(final Class<T> type) {
if (type == IContentOutlinePage.class)
if (type == IContentOutlinePage.class) {
return type.cast(new ShapesOutlinePage(new TreeViewer()));
}
return super.getAdapter(type);
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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.
*
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 14d17aa

Please sign in to comment.