Skip to content

Commit

Permalink
Final Clean-up of shapes example. Mostly externalize strings
Browse files Browse the repository at this point in the history
  • Loading branch information
azoitl committed Mar 29, 2024
1 parent 121f906 commit ea881ef
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.gef.examples.shapes;

import org.eclipse.osgi.util.NLS;

public class Messages extends NLS {
private static final String BUNDLE_NAME = Messages.class.getPackageName() + ".messages"; //$NON-NLS-1$
public static String ShapeSetConstraintCommand_MoveResize;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}

private Messages() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.text.MessageFormat;

import org.eclipse.swt.widgets.Composite;

Expand Down Expand Up @@ -80,7 +81,7 @@ public boolean performFinish() {
* This WizardPage can create an empty .shapes file for the ShapesEditor.
*/
private class CreationPage extends WizardNewFileCreationPage {
private static final String DEFAULT_EXTENSION = ".shapes";
private static final String DEFAULT_EXTENSION = ".shapes"; //$NON-NLS-1$
private final IWorkbench workbench;

/**
Expand All @@ -91,10 +92,12 @@ private class CreationPage extends WizardNewFileCreationPage {
* @see ShapesCreationWizard#init(IWorkbench, IStructuredSelection)
*/
CreationPage(IWorkbench workbench, IStructuredSelection selection) {
super("shapeCreationPage1", selection);
super("shapeCreationPage1", selection); //$NON-NLS-1$
this.workbench = workbench;
setTitle("Create a new " + DEFAULT_EXTENSION + " file");
setDescription("Create a new " + DEFAULT_EXTENSION + " file");
setTitle(
MessageFormat.format(ShapesExampleMessages.ShapesCreationWizard_CreateANewFile, DEFAULT_EXTENSION));
setDescription(
MessageFormat.format(ShapesExampleMessages.ShapesCreationWizard_CreateANewFile, DEFAULT_EXTENSION));
}

/*
Expand All @@ -106,7 +109,7 @@ private class CreationPage extends WizardNewFileCreationPage {
@Override
public void createControl(Composite parent) {
super.createControl(parent);
setFileName("shapesExample" + fileCount + DEFAULT_EXTENSION);
setFileName("shapesExample" + fileCount + DEFAULT_EXTENSION); //$NON-NLS-1$
setPageComplete(validatePage());
}

Expand Down Expand Up @@ -167,7 +170,8 @@ private boolean validateFilename() {
if (getFileName() != null && getFileName().endsWith(DEFAULT_EXTENSION)) {
return true;
}
setErrorMessage("The 'file' name must end with " + DEFAULT_EXTENSION);
setErrorMessage(MessageFormat.format(ShapesExampleMessages.ShapesCreationWizard_FileNameMustEndWith,
DEFAULT_EXTENSION));
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public void execute(final IProgressMonitor monitor) {
} catch (InterruptedException ie) {
// should not happen, since the monitor dialog is not cancelable
ie.printStackTrace();
Thread.currentThread().interrupt();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
}
Expand Down Expand Up @@ -279,7 +280,7 @@ protected PaletteRoot getPaletteRoot() {
}

private void handleLoadException(Exception e) {
System.err.println("** Load failed. Using default model. **");
System.err.println("** Load failed. Using default model. **"); //$NON-NLS-1$
e.printStackTrace();
diagram = new ShapesDiagram();
}
Expand Down Expand Up @@ -321,11 +322,7 @@ protected void setInput(IEditorInput input) {
try (ObjectInputStream in = new ObjectInputStream(file.getContents())) {
diagram = (ShapesDiagram) in.readObject();
setPartName(file.getName());
} catch (IOException e) {
handleLoadException(e);
} catch (CoreException e) {
handleLoadException(e);
} catch (ClassNotFoundException e) {
} catch (IOException | CoreException | ClassNotFoundException e) {
handleLoadException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ final class ShapesEditorPaletteFactory {

/** Create the "Shapes" drawer. */
private static PaletteContainer createShapesDrawer() {
PaletteDrawer componentsDrawer = new PaletteDrawer("Shapes");
PaletteDrawer componentsDrawer = new PaletteDrawer(ShapesExampleMessages.ShapesEditorPaletteFactory_Shapes);

CombinedTemplateCreationEntry component = new CombinedTemplateCreationEntry("Ellipse",
"Create an elliptical shape", EllipticalShape.class, new SimpleFactory<>(EllipticalShape.class),
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/ellipse16.gif"),
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/ellipse24.gif"));
CombinedTemplateCreationEntry component = new CombinedTemplateCreationEntry(ShapesExampleMessages.ShapesEditorPaletteFactory_Ellipse,
ShapesExampleMessages.ShapesEditorPaletteFactory_CreateEllipticalShape, EllipticalShape.class, new SimpleFactory<>(EllipticalShape.class),
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/ellipse16.gif"), //$NON-NLS-1$
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/ellipse24.gif")); //$NON-NLS-1$
componentsDrawer.add(component);

component = new CombinedTemplateCreationEntry("Rectangle", "Create a rectangular shape", RectangularShape.class,
component = new CombinedTemplateCreationEntry(ShapesExampleMessages.ShapesEditorPaletteFactory_Rectangle, ShapesExampleMessages.ShapesEditorPaletteFactory_CreateRectangularShape, RectangularShape.class,
new SimpleFactory<>(RectangularShape.class),
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/rectangle16.gif"),
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/rectangle24.gif"));
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/rectangle16.gif"), //$NON-NLS-1$
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/rectangle24.gif")); //$NON-NLS-1$
componentsDrawer.add(component);

return componentsDrawer;
Expand All @@ -72,7 +72,7 @@ static PaletteRoot createPalette() {

/** Create the "Tools" group. */
private static PaletteContainer createToolsGroup(PaletteRoot palette) {
PaletteToolbar toolbar = new PaletteToolbar("Tools");
PaletteToolbar toolbar = new PaletteToolbar(ShapesExampleMessages.ShapesEditorPaletteFactory_Tools);

// Add a selection tool to the group
ToolEntry tool = new PanningSelectionToolEntry();
Expand All @@ -83,7 +83,7 @@ private static PaletteContainer createToolsGroup(PaletteRoot palette) {
toolbar.add(new MarqueeToolEntry());

// Add (solid-line) connection tool
tool = new ConnectionCreationToolEntry("Solid connection", "Create a solid-line connection",
tool = new ConnectionCreationToolEntry(ShapesExampleMessages.ShapesEditorPaletteFactory_SolidConnection, ShapesExampleMessages.ShapesEditorPaletteFactory_CreateSolidLineConnection,
new CreationFactory() {
@Override
public Object getNewObject() {
Expand All @@ -96,12 +96,12 @@ public Object getNewObject() {
public Object getObjectType() {
return Connection.SOLID_CONNECTION;
}
}, ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_s16.gif"),
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_s24.gif"));
}, ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_s16.gif"), //$NON-NLS-1$
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_s24.gif")); //$NON-NLS-1$
toolbar.add(tool);

// Add (dashed-line) connection tool
tool = new ConnectionCreationToolEntry("Dashed connection", "Create a dashed-line connection",
tool = new ConnectionCreationToolEntry(ShapesExampleMessages.ShapesEditorPaletteFactory_DashedConnection, ShapesExampleMessages.ShapesEditorPaletteFactory_CreateDashedLineConnection,
new CreationFactory() {
@Override
public Object getNewObject() {
Expand All @@ -114,8 +114,8 @@ public Object getNewObject() {
public Object getObjectType() {
return Connection.DASHED_CONNECTION;
}
}, ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_d16.gif"),
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_d24.gif"));
}, ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_d16.gif"), //$NON-NLS-1$
ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_d24.gif")); //$NON-NLS-1$
toolbar.add(tool);

return toolbar;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2024 Johannes Kepler University Linz.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alois Zoitl - initial API and implementation
*******************************************************************************/

package org.eclipse.gef.examples.shapes;

import org.eclipse.osgi.util.NLS;

@SuppressWarnings("squid:S3008") // tell sonar the java naming convention does not make sense for this class
public class ShapesExampleMessages extends NLS {
private static final String BUNDLE_NAME = ShapesExampleMessages.class.getPackageName() + ".messages"; //$NON-NLS-1$
public static String ConnectionCreateCommand_ConnectionCreation;
public static String ConnectionDeleteCommand_ConnectionDeletion;
public static String ConnectionReconnectCommand_MoveConnectionEndPoint;
public static String ConnectionReconnectCommand_MoveConnectionStartPoint;
public static String EllipticalShape_Ellipse;
public static String RectangularShape_Rectangle;
public static String Shape_Height;
public static String Shape_NotANumber;
public static String Shape_ValueMustBeGreaterOrEqualZero;
public static String Shape_Width;
public static String Shape_X;
public static String Shape_Y;
public static String ShapeCreateCommand_ShapeCreation;
public static String ShapeDeleteCommand_ShapeDeletion;
public static String ShapesCreationWizard_CreateANewFile;
public static String ShapesCreationWizard_FileNameMustEndWith;
public static String ShapesEditorPaletteFactory_CreateDashedLineConnection;
public static String ShapesEditorPaletteFactory_CreateEllipticalShape;
public static String ShapesEditorPaletteFactory_CreateRectangularShape;
public static String ShapesEditorPaletteFactory_CreateSolidLineConnection;
public static String ShapesEditorPaletteFactory_DashedConnection;
public static String ShapesEditorPaletteFactory_Ellipse;
public static String ShapesEditorPaletteFactory_Rectangle;
public static String ShapesEditorPaletteFactory_Shapes;
public static String ShapesEditorPaletteFactory_SolidConnection;
public static String ShapesEditorPaletteFactory_Tools;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, ShapesExampleMessages.class);
}

private ShapesExampleMessages() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
ConnectionCreateCommand_ConnectionCreation=connection creation
ConnectionDeleteCommand_ConnectionDeletion=connection deletion
ConnectionReconnectCommand_MoveConnectionEndPoint=move connection endpoint
ConnectionReconnectCommand_MoveConnectionStartPoint=move connection startpoint
EllipticalShape_Ellipse=Ellipse {0}
RectangularShape_Rectangle=Rectangle {0}
Shape_Height=Height
Shape_NotANumber=Not a number
Shape_ValueMustBeGreaterOrEqualZero=Value must be >= 0
Shape_Width=Width
Shape_X=X
Shape_Y=Y
ShapeCreateCommand_ShapeCreation=shape creation
ShapeDeleteCommand_ShapeDeletion=shape deletion
ShapesCreationWizard_CreateANewFile=Create a new {0} file
ShapesCreationWizard_FileNameMustEndWith=The ''file'' name must end with {0}
ShapesEditorPaletteFactory_CreateDashedLineConnection=Create a dashed-line connection
ShapesEditorPaletteFactory_CreateEllipticalShape=Create an elliptical shape
ShapesEditorPaletteFactory_CreateRectangularShape=Create a rectangular shape
ShapesEditorPaletteFactory_CreateSolidLineConnection=Create a solid-line connection
ShapesEditorPaletteFactory_DashedConnection=Dashed connection
ShapesEditorPaletteFactory_Ellipse=Ellipse
ShapesEditorPaletteFactory_Rectangle=Rectangle
ShapesEditorPaletteFactory_Shapes=Shapes
ShapesEditorPaletteFactory_SolidConnection=Solid connection
ShapesEditorPaletteFactory_Tools=Tools
ShapeSetConstraintCommand_MoveResize=move / resize
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public class Connection extends ModelElement {
*/
public static final Integer DASHED_CONNECTION = Integer.valueOf(Graphics.LINE_DASH);
/** Property ID to use when the line style of this connection is modified. */
public static final String LINESTYLE_PROP = "LineStyle";
public static final String LINESTYLE_PROP = "LineStyle"; //$NON-NLS-1$
private static final IPropertyDescriptor[] descriptors = new IPropertyDescriptor[1];
private static final String SOLID_STR = "Solid";
private static final String DASHED_STR = "Dashed";
private static final String SOLID_STR = "Solid"; //$NON-NLS-1$
private static final String DASHED_STR = "Dashed"; //$NON-NLS-1$
private static final long serialVersionUID = 1;

/** True, if the connection is attached to its endpoints. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
*******************************************************************************/
package org.eclipse.gef.examples.shapes.model;

import java.text.MessageFormat;

import org.eclipse.swt.graphics.Image;

import org.eclipse.gef.examples.shapes.ShapesExampleMessages;

/**
* An elliptical shape.
*
Expand All @@ -22,7 +26,7 @@
public class EllipticalShape extends Shape {

/** A 16x16 pictogram of an elliptical shape. */
private static final Image ELLIPSE_ICON = createImage("icons/ellipse16.gif");
private static final Image ELLIPSE_ICON = createImage("icons/ellipse16.gif"); //$NON-NLS-1$

private static final long serialVersionUID = 1;

Expand All @@ -33,6 +37,6 @@ public Image getIcon() {

@Override
public String toString() {
return "Ellipse " + hashCode();
return MessageFormat.format(ShapesExampleMessages.EllipticalShape_Ellipse, hashCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
*******************************************************************************/
package org.eclipse.gef.examples.shapes.model;

import java.text.MessageFormat;

import org.eclipse.swt.graphics.Image;

import org.eclipse.gef.examples.shapes.ShapesExampleMessages;

/**
* A rectangular shape.
*
* @author Elias Volanakis
*/
public class RectangularShape extends Shape {
/** A 16x16 pictogram of a rectangular shape. */
private static final Image RECTANGLE_ICON = createImage("icons/rectangle16.gif");
private static final Image RECTANGLE_ICON = createImage("icons/rectangle16.gif"); //$NON-NLS-1$

private static final long serialVersionUID = 1;

Expand All @@ -32,6 +36,6 @@ public Image getIcon() {

@Override
public String toString() {
return "Rectangle " + hashCode();
return MessageFormat.format(ShapesExampleMessages.RectangularShape_Rectangle, hashCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;

import org.eclipse.gef.examples.shapes.ShapesExampleMessages;
import org.eclipse.gef.examples.shapes.ShapesPlugin;

/**
Expand Down Expand Up @@ -89,19 +90,19 @@ public abstract class Shape extends ModelElement {
* @see #setPropertyValue(Object, Object)
*/
static {
descriptors = new IPropertyDescriptor[] { new TextPropertyDescriptor(XPOS_PROP, "X"), // id and description pair
new TextPropertyDescriptor(YPOS_PROP, "Y"), new TextPropertyDescriptor(WIDTH_PROP, "Width"),
new TextPropertyDescriptor(HEIGHT_PROP, "Height"), };
descriptors = new IPropertyDescriptor[] { new TextPropertyDescriptor(XPOS_PROP, ShapesExampleMessages.Shape_X), // id and description pair
new TextPropertyDescriptor(YPOS_PROP, ShapesExampleMessages.Shape_Y), new TextPropertyDescriptor(WIDTH_PROP, ShapesExampleMessages.Shape_Width),
new TextPropertyDescriptor(HEIGHT_PROP, ShapesExampleMessages.Shape_Height), };
// use a custom cell editor validator for all four array entries
for (IPropertyDescriptor descriptor : descriptors) {
((PropertyDescriptor) descriptor).setValidator(value -> {
int intValue = -1;
try {
intValue = Integer.parseInt((String) value);
} catch (NumberFormatException exc) {
return "Not a number";
return ShapesExampleMessages.Shape_NotANumber;
}
return (intValue >= 0) ? null : "Value must be >= 0";
return (intValue >= 0) ? null : ShapesExampleMessages.Shape_ValueMustBeGreaterOrEqualZero;
});
}
} // static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
public class ShapesDiagram extends ModelElement {

/** Property ID to use when a child is added to this diagram. */
public static final String CHILD_ADDED_PROP = "ShapesDiagram.ChildAdded";
public static final String CHILD_ADDED_PROP = "ShapesDiagram.ChildAdded"; //$NON-NLS-1$
/** Property ID to use when a child is removed from this diagram. */
public static final String CHILD_REMOVED_PROP = "ShapesDiagram.ChildRemoved";
public static final String CHILD_REMOVED_PROP = "ShapesDiagram.ChildRemoved"; //$NON-NLS-1$
private static final long serialVersionUID = 1;
private final List<Shape> shapes = new ArrayList<>();

Expand Down
Loading

0 comments on commit ea881ef

Please sign in to comment.