diff --git a/.github/m2_settings/settings.xml b/.github/m2_settings/settings.xml new file mode 100644 index 000000000..954403d32 --- /dev/null +++ b/.github/m2_settings/settings.xml @@ -0,0 +1,34 @@ + + + + github + + + + + github + + + GitHub External Packages + github + https://maven.pkg.github.com/sweat-tek/MavenRepository + + + central + https://repo1.maven.org/maven2 + + + + + + + + github + ${env.GITHUB_USERNAME} + ${env.GITHUB_TOKEN} + + + + diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 000000000..1448fbf9f --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,37 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Java CI with Maven + +on: + push: + branches: [ "Master" ] + pull_request: + branches: + - Master + - develop + +jobs: + Build-Test: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn --batch-mode --update-snapshots -Dmaven.test.skip=true -s .github/m2_settings/settings.xml verify + + env: + GITHUB_USERNAME: ${{ secrets.USER_NAME }} + GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }} diff --git a/jhotdraw-actions/jhotdraw-actions.iml b/jhotdraw-actions/jhotdraw-actions.iml new file mode 100644 index 000000000..41fcb840f --- /dev/null +++ b/jhotdraw-actions/jhotdraw-actions.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-actions/pom.xml b/jhotdraw-actions/pom.xml index 5cc33e1c6..0eca25ca3 100644 --- a/jhotdraw-actions/pom.xml +++ b/jhotdraw-actions/pom.xml @@ -18,6 +18,11 @@ ${project.groupId} jhotdraw-utils ${project.version} + + + dk.sdu.cbse + featuretracerlite + 1.1-SNAPSHOT ${project.groupId} diff --git a/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/ActionIDs.java b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/ActionIDs.java new file mode 100644 index 000000000..ec4dd7ea0 --- /dev/null +++ b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/ActionIDs.java @@ -0,0 +1,6 @@ +package org.jhotdraw.action.edit; + +enum ActionIDs {; + static final String UNDOID = "edit.undo"; + static final String REDOID = "edit.redo"; +} diff --git a/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/DeleteAction.java b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/DeleteAction.java index 05ae13f58..932b667a4 100644 --- a/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/DeleteAction.java +++ b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/DeleteAction.java @@ -13,6 +13,7 @@ import java.beans.PropertyChangeListener; import javax.swing.JComponent; import javax.swing.text.*; + import org.jhotdraw.api.gui.EditableComponent; import org.jhotdraw.beans.WeakPropertyChangeListener; import org.jhotdraw.util.*; diff --git a/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/RedoAction.java b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/RedoAction.java index 794ab08c8..a9b7c719b 100644 --- a/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/RedoAction.java +++ b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/RedoAction.java @@ -6,11 +6,6 @@ * accompanying license terms. */ package org.jhotdraw.action.edit; - -import java.awt.event.*; -import java.beans.*; -import javax.swing.*; -import org.jhotdraw.action.AbstractViewAction; import org.jhotdraw.api.app.Application; import org.jhotdraw.api.app.View; import org.jhotdraw.util.*; @@ -28,89 +23,18 @@ * with this ID and put it in your {@code ApplicationModel} in method * {@link org.jhotdraw.app.ApplicationModel#initApplication}. * - * * @author Werner Randelshofer * @version $Id$ */ -public class RedoAction extends AbstractViewAction { - - private static final long serialVersionUID = 1L; - public static final String ID = "edit.redo"; - private ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.action.Labels"); - private PropertyChangeListener redoActionPropertyListener = new PropertyChangeListener() { - @Override - public void propertyChange(PropertyChangeEvent evt) { - String name = evt.getPropertyName(); - if ((name == null && AbstractAction.NAME == null) || (name != null && name.equals(AbstractAction.NAME))) { - putValue(AbstractAction.NAME, evt.getNewValue()); - } else if ("enabled".equals(name)) { - updateEnabledState(); - } - } - }; +public class RedoAction extends UndoRedoAction { + public static final String ID = getID(ActionIDs.REDOID); /** * Creates a new instance. */ public RedoAction(Application app, View view) { - super(app, view); + super(app, view, ID); + ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.action.Labels"); labels.configureAction(this, ID); } - - protected void updateEnabledState() { - boolean isEnabled = false; - Action realRedoAction = getRealRedoAction(); - if (realRedoAction != null && realRedoAction != this) { - isEnabled = realRedoAction.isEnabled(); - } - setEnabled(isEnabled); - } - - @Override - protected void updateView(View oldValue, View newValue) { - super.updateView(oldValue, newValue); - if (newValue != null - && newValue.getActionMap().get(ID) != null - && newValue.getActionMap().get(ID) != this) { - putValue(AbstractAction.NAME, newValue.getActionMap().get(ID). - getValue(AbstractAction.NAME)); - updateEnabledState(); - } - } - - /** - * Installs listeners on the view object. - */ - @Override - protected void installViewListeners(View p) { - super.installViewListeners(p); - Action redoActionInView = p.getActionMap().get(ID); - if (redoActionInView != null && redoActionInView != this) { - redoActionInView.addPropertyChangeListener(redoActionPropertyListener); - } - } - - /** - * Installs listeners on the view object. - */ - @Override - protected void uninstallViewListeners(View p) { - super.uninstallViewListeners(p); - Action redoActionInView = p.getActionMap().get(ID); - if (redoActionInView != null && redoActionInView != this) { - redoActionInView.removePropertyChangeListener(redoActionPropertyListener); - } - } - - @Override - public void actionPerformed(ActionEvent e) { - Action realAction = getRealRedoAction(); - if (realAction != null && realAction != this) { - realAction.actionPerformed(e); - } - } - - private Action getRealRedoAction() { - return (getActiveView() == null) ? null : getActiveView().getActionMap().get(ID); - } } diff --git a/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/UndoAction.java b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/UndoAction.java index 74c4f2cea..3e8f09573 100644 --- a/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/UndoAction.java +++ b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/UndoAction.java @@ -6,11 +6,6 @@ * accompanying license terms. */ package org.jhotdraw.action.edit; - -import java.awt.event.*; -import java.beans.*; -import javax.swing.*; -import org.jhotdraw.action.AbstractViewAction; import org.jhotdraw.api.app.Application; import org.jhotdraw.api.app.View; import org.jhotdraw.util.*; @@ -31,85 +26,15 @@ * @author Werner Randelshofer * @version $Id$ */ -public class UndoAction extends AbstractViewAction { - - private static final long serialVersionUID = 1L; - public static final String ID = "edit.undo"; - private ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.action.Labels"); - private PropertyChangeListener redoActionPropertyListener = new PropertyChangeListener() { - @Override - public void propertyChange(PropertyChangeEvent evt) { - String name = evt.getPropertyName(); - if ((name == null && AbstractAction.NAME == null) || (name != null && name.equals(AbstractAction.NAME))) { - putValue(AbstractAction.NAME, evt.getNewValue()); - } else if ("enabled".equals(name)) { - updateEnabledState(); - } - } - }; +public class UndoAction extends UndoRedoAction { + public static final String ID = getID(ActionIDs.UNDOID); /** * Creates a new instance. */ public UndoAction(Application app, View view) { - super(app, view); + super(app, view, ID); + ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.action.Labels"); labels.configureAction(this, ID); } - - protected void updateEnabledState() { - boolean isEnabled = false; - Action realAction = getRealUndoAction(); - if (realAction != null && realAction != this) { - isEnabled = realAction.isEnabled(); - } - setEnabled(isEnabled); - } - - @Override - protected void updateView(View oldValue, View newValue) { - super.updateView(oldValue, newValue); - if (newValue != null - && newValue.getActionMap().get(ID) != null - && newValue.getActionMap().get(ID) != this) { - putValue(AbstractAction.NAME, newValue.getActionMap().get(ID). - getValue(AbstractAction.NAME)); - updateEnabledState(); - } - } - - /** - * Installs listeners on the view object. - */ - @Override - protected void installViewListeners(View p) { - super.installViewListeners(p); - Action undoActionInView = p.getActionMap().get(ID); - if (undoActionInView != null && undoActionInView != this) { - undoActionInView.addPropertyChangeListener(redoActionPropertyListener); - } - } - - /** - * Installs listeners on the view object. - */ - @Override - protected void uninstallViewListeners(View p) { - super.uninstallViewListeners(p); - Action undoActionInView = p.getActionMap().get(ID); - if (undoActionInView != null && undoActionInView != this) { - undoActionInView.removePropertyChangeListener(redoActionPropertyListener); - } - } - - @Override - public void actionPerformed(ActionEvent e) { - Action realUndoAction = getRealUndoAction(); - if (realUndoAction != null && realUndoAction != this) { - realUndoAction.actionPerformed(e); - } - } - - private Action getRealUndoAction() { - return (getActiveView() == null) ? null : getActiveView().getActionMap().get(ID); - } } diff --git a/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/UndoRedoAction.java b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/UndoRedoAction.java new file mode 100644 index 000000000..24f0414d0 --- /dev/null +++ b/jhotdraw-actions/src/main/java/org/jhotdraw/action/edit/UndoRedoAction.java @@ -0,0 +1,95 @@ +package org.jhotdraw.action.edit; + +import org.jhotdraw.action.AbstractViewAction; +import org.jhotdraw.api.app.Application; +import org.jhotdraw.api.app.View; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.beans.PropertyChangeListener; + +abstract class UndoRedoAction extends AbstractViewAction { + private static final long serialVersionUID = 1L; + private final String id; + + private final transient PropertyChangeListener redoActionPropertyListener = evt -> { + String name = evt.getPropertyName(); + if (name != null && name.equals(Action.NAME)) { + putValue(Action.NAME, evt.getNewValue()); + } else if ("enabled".equals(name)) { + updateEnabledState(); + } + }; + + /** + * Creates a new instance which acts on the specified view of the application. + * + * @param app application + * @param view view + */ + protected UndoRedoAction(Application app, View view, String id) { + super(app, view); + this.id = id; + } + + protected void updateEnabledState() { + boolean isEnabled = false; + Action realAction = getRealAction(); + if (realAction != null && realAction != this) { + isEnabled = realAction.isEnabled(); + } + setEnabled(isEnabled); + } + + @Override + protected void updateView(View oldValue, View newValue) { + super.updateView(oldValue, newValue); + if (newValue != null + && newValue.getActionMap().get(getID(id)) != null + && newValue.getActionMap().get(getID(id)) != this) { + putValue(Action.NAME, newValue.getActionMap().get(getID(id)). + getValue(Action.NAME)); + updateEnabledState(); + } + } + + /** + * Installs listeners on the view object. + */ + @Override + protected void installViewListeners(View p) { + super.installViewListeners(p); + Action actionInView = p.getActionMap().get(getID(id)); + if (actionInView != null && actionInView != this) { + actionInView.addPropertyChangeListener(redoActionPropertyListener); + } + } + + /** + * Installs listeners on the view object. + */ + @Override + protected void uninstallViewListeners(View p) { + super.uninstallViewListeners(p); + Action actionInView = p.getActionMap().get(getID(id)); + if (actionInView != null && actionInView != this) { + actionInView.removePropertyChangeListener(redoActionPropertyListener); + } + } + + @Override + public void actionPerformed(ActionEvent e) { + Action realAction = getRealAction(); + if (realAction != null && realAction != this) { + realAction.actionPerformed(e); + } + } + + private Action getRealAction() { + return (getActiveView() == null) ? null : getActiveView().getActionMap().get(id); + } + + protected static String getID(String id) { + return id; + } +} diff --git a/jhotdraw-api/jhotdraw-api.iml b/jhotdraw-api/jhotdraw-api.iml new file mode 100644 index 000000000..7a76742c6 --- /dev/null +++ b/jhotdraw-api/jhotdraw-api.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-app/jhotdraw-app.iml b/jhotdraw-app/jhotdraw-app.iml new file mode 100644 index 000000000..c439400b8 --- /dev/null +++ b/jhotdraw-app/jhotdraw-app.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-core/jhotdraw-core.iml b/jhotdraw-core/jhotdraw-core.iml new file mode 100644 index 000000000..ce8352880 --- /dev/null +++ b/jhotdraw-core/jhotdraw-core.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-core/pom.xml b/jhotdraw-core/pom.xml index 7c276da85..259be131b 100644 --- a/jhotdraw-core/pom.xml +++ b/jhotdraw-core/pom.xml @@ -40,5 +40,37 @@ jhotdraw-actions ${project.version} + + junit + junit + 4.13.1 + test + + + org.mockito + mockito-core + 5.8.0 + test + + + org.junit.jupiter + junit-jupiter-api + 5.10.1 + test + + + + + org.mockito + mockito-junit-jupiter + 4.5.1 + test + + + org.assertj + assertj-swing-junit + 3.9.2 + test + \ No newline at end of file diff --git a/jhotdraw-core/src/main/java/org/jhotdraw/draw/action/AlignAction.java b/jhotdraw-core/src/main/java/org/jhotdraw/draw/action/AlignAction.java index 9b682f099..c8c2907b6 100644 --- a/jhotdraw-core/src/main/java/org/jhotdraw/draw/action/AlignAction.java +++ b/jhotdraw-core/src/main/java/org/jhotdraw/draw/action/AlignAction.java @@ -1,14 +1,9 @@ -/* - * @(#)AlignAction.java - * - * Copyright (c) 1996-2010 The authors and contributors of JHotDraw. - * You may not use, copy or modify this file, except in compliance with the - * accompanying license terms. - */ package org.jhotdraw.draw.action; import java.awt.geom.*; import java.util.*; + +import dk.sdu.mmmi.featuretracer.lib.FeatureEntryPoint; import org.jhotdraw.draw.DrawingEditor; import org.jhotdraw.draw.figure.Figure; import org.jhotdraw.draw.event.TransformEdit; @@ -16,245 +11,107 @@ import org.jhotdraw.util.ResourceBundleUtil; /** - * Aligns the selected figures. - * - * XXX - Fire edit events - * - * @author Werner Randelshofer - * @version $Id$ + * Aligns the selected figures based on the specified alignment type. */ -public abstract class AlignAction extends AbstractSelectedAction { - +public class AlignAction extends AbstractSelectedAction { private static final long serialVersionUID = 1L; - protected ResourceBundleUtil labels - = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels"); + protected ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels"); + private final AlignmentType alignmentType; + + public enum AlignmentType { + NORTH, EAST, WEST, SOUTH, VERTICAL, HORIZONTAL + } - /** - * Creates a new instance. - */ - public AlignAction(DrawingEditor editor) { + public AlignAction(DrawingEditor editor, AlignmentType alignmentType) { super(editor); + this.alignmentType = alignmentType; updateEnabledState(); + labels.configureAction(this, "edit.align" + formatAlignmentName(alignmentType)); + } + + public AlignAction(DrawingEditor editor, AlignmentType alignmentType, ResourceBundleUtil labels) { + super(editor); + this.labels = labels; + this.alignmentType = alignmentType; + labels.configureAction(this, "edit.align" + formatAlignmentName(alignmentType)); + } + + String formatAlignmentName(AlignmentType alignmentType) { + String name = alignmentType.name().toLowerCase(); + return Character.toUpperCase(name.charAt(0)) + name.substring(1); } @Override public void updateEnabledState() { - if (getView() != null) { - setEnabled(getView().isEnabled() - && getView().getSelectionCount() > 1); - } else { - setEnabled(false); - } + setEnabled(getView() != null && getView().isEnabled() && getView().getSelectionCount() > 1); } + @FeatureEntryPoint(value = "align") @Override public void actionPerformed(java.awt.event.ActionEvent e) { - CompositeEdit edit = new CompositeEdit(labels.getString("edit.align.text")); - fireUndoableEditHappened(edit); + System.out.println("Something Happened"); + CompositeEdit compositeEdit = new CompositeEdit(labels.getString("edit.align.text")); + fireUndoableEditHappened(compositeEdit); alignFigures(getView().getSelectedFigures(), getSelectionBounds()); - fireUndoableEditHappened(edit); + fireUndoableEditHappened(compositeEdit); } - protected abstract void alignFigures(Collection
selectedFigures, Rectangle2D.Double selectionBounds); - - /** - * Returns the bounds of the selected figures. - */ - protected Rectangle2D.Double getSelectionBounds() { - Rectangle2D.Double bounds = null; - for (Figure f : getView().getSelectedFigures()) { - if (bounds == null) { - bounds = f.getBounds(); - } else { - bounds.add(f.getBounds()); - } + private void alignFigures(Collection
selectedFigures, Rectangle2D.Double selectionBounds) { + for (Figure figure : selectedFigures) { + Point2D.Double delta = calculateAlignmentDelta(figure.getBounds(), selectionBounds); + AffineTransform translateTransform = new AffineTransform(1, 0, 0, 1, delta.x, delta.y); + transformFigure(figure, translateTransform); } - return bounds; } - public static class North extends AlignAction { - - private static final long serialVersionUID = 1L; - - public North(DrawingEditor editor) { - super(editor); - labels.configureAction(this, "edit.alignNorth"); - } - - public North(DrawingEditor editor, ResourceBundleUtil labels) { - super(editor); - labels.configureAction(this, "edit.alignNorth"); - } - - @Override - protected void alignFigures(Collection
selectedFigures, Rectangle2D.Double selectionBounds) { - double y = selectionBounds.y; - for (Figure f : getView().getSelectedFigures()) { - if (f.isTransformable()) { - f.willChange(); - Rectangle2D.Double b = f.getBounds(); - AffineTransform tx = new AffineTransform(); - tx.translate(0, y - b.y); - f.transform(tx); - f.changed(); - fireUndoableEditHappened(new TransformEdit(f, tx)); - } - } - } + Point2D.Double calculateAlignmentDelta(Rectangle2D.Double figureBounds, Rectangle2D.Double selectionBounds) { + double deltaX = 0; + double deltaY = 0; + + switch (alignmentType) { + case NORTH: + deltaY = selectionBounds.y - figureBounds.y; + break; + case EAST: + deltaX = selectionBounds.getMaxX() - figureBounds.getMaxX(); + break; + case WEST: + deltaX = selectionBounds.getMinX() - figureBounds.x; + break; + case SOUTH: + deltaY = selectionBounds.getMaxY() - figureBounds.getMaxY(); + break; + case VERTICAL: + deltaY = selectionBounds.getCenterY() - figureBounds.getCenterY(); + break; + case HORIZONTAL: + deltaX = selectionBounds.getCenterX() - figureBounds.getCenterX(); + break; + } + return new Point2D.Double(deltaX, deltaY); } - public static class East extends AlignAction { - - private static final long serialVersionUID = 1L; - - public East(DrawingEditor editor) { - super(editor); - labels.configureAction(this, "edit.alignEast"); - } - - public East(DrawingEditor editor, ResourceBundleUtil labels) { - super(editor); - labels.configureAction(this, "edit.alignEast"); - } - - @Override - protected void alignFigures(Collection
selectedFigures, Rectangle2D.Double selectionBounds) { - double x = selectionBounds.x + selectionBounds.width; - for (Figure f : getView().getSelectedFigures()) { - if (f.isTransformable()) { - f.willChange(); - Rectangle2D.Double b = f.getBounds(); - AffineTransform tx = new AffineTransform(); - tx.translate(x - b.x - b.width, 0); - f.transform(tx); - f.changed(); - fireUndoableEditHappened(new TransformEdit(f, tx)); - } - } - } - } - - public static class West extends AlignAction { - - private static final long serialVersionUID = 1L; - - public West(DrawingEditor editor) { - super(editor); - labels.configureAction(this, "edit.alignWest"); - } - - public West(DrawingEditor editor, ResourceBundleUtil labels) { - super(editor); - labels.configureAction(this, "edit.alignWest"); - } - - @Override - protected void alignFigures(Collection
selectedFigures, Rectangle2D.Double selectionBounds) { - double x = selectionBounds.x; - for (Figure f : getView().getSelectedFigures()) { - if (f.isTransformable()) { - f.willChange(); - Rectangle2D.Double b = f.getBounds(); - AffineTransform tx = new AffineTransform(); - tx.translate(x - b.x, 0); - f.transform(tx); - f.changed(); - fireUndoableEditHappened(new TransformEdit(f, tx)); - } - } - } - } - - public static class South extends AlignAction { - - private static final long serialVersionUID = 1L; - - public South(DrawingEditor editor) { - super(editor); - labels.configureAction(this, "edit.alignSouth"); - } - - public South(DrawingEditor editor, ResourceBundleUtil labels) { - super(editor); - labels.configureAction(this, "edit.alignSouth"); - } - - @Override - protected void alignFigures(Collection
selectedFigures, Rectangle2D.Double selectionBounds) { - double y = selectionBounds.y + selectionBounds.height; - for (Figure f : getView().getSelectedFigures()) { - if (f.isTransformable()) { - f.willChange(); - Rectangle2D.Double b = f.getBounds(); - AffineTransform tx = new AffineTransform(); - tx.translate(0, y - b.y - b.height); - f.transform(tx); - f.changed(); - fireUndoableEditHappened(new TransformEdit(f, tx)); - } - } - } - } - - public static class Vertical extends AlignAction { - - private static final long serialVersionUID = 1L; - - public Vertical(DrawingEditor editor) { - super(editor); - labels.configureAction(this, "edit.alignVertical"); - } - - public Vertical(DrawingEditor editor, ResourceBundleUtil labels) { - super(editor); - labels.configureAction(this, "edit.alignVertical"); - } + protected Rectangle2D.Double getSelectionBounds() { + Rectangle2D.Double bounds = new Rectangle2D.Double(); + boolean isFirst = true; - @Override - protected void alignFigures(Collection
selectedFigures, Rectangle2D.Double selectionBounds) { - double y = selectionBounds.y + selectionBounds.height / 2; - for (Figure f : getView().getSelectedFigures()) { - if (f.isTransformable()) { - f.willChange(); - Rectangle2D.Double b = f.getBounds(); - AffineTransform tx = new AffineTransform(); - tx.translate(0, y - b.y - b.height / 2); - f.transform(tx); - f.changed(); - fireUndoableEditHappened(new TransformEdit(f, tx)); - } + for (Figure figure : getView().getSelectedFigures()) { + if (isFirst) { + bounds.setRect(figure.getBounds()); + isFirst = false; + } else { + bounds.add(figure.getBounds()); } } + return bounds; } - public static class Horizontal extends AlignAction { - - private static final long serialVersionUID = 1L; - - public Horizontal(DrawingEditor editor) { - super(editor); - labels.configureAction(this, "edit.alignHorizontal"); - } - - public Horizontal(DrawingEditor editor, ResourceBundleUtil labels) { - super(editor); - labels.configureAction(this, "edit.alignHorizontal"); - } - - @Override - protected void alignFigures(Collection
selectedFigures, Rectangle2D.Double selectionBounds) { - double x = selectionBounds.x + selectionBounds.width / 2; - for (Figure f : getView().getSelectedFigures()) { - if (f.isTransformable()) { - f.willChange(); - Rectangle2D.Double b = f.getBounds(); - AffineTransform tx = new AffineTransform(); - tx.translate(x - b.x - b.width / 2, 0); - f.transform(tx); - f.changed(); - fireUndoableEditHappened(new TransformEdit(f, tx)); - } - } + protected void transformFigure(Figure figure, AffineTransform transform) { + if (figure.isTransformable()) { + figure.willChange(); + figure.transform(transform); + figure.changed(); + fireUndoableEditHappened(new TransformEdit(figure, transform)); } } -} +} \ No newline at end of file diff --git a/jhotdraw-core/src/main/java/org/jhotdraw/draw/figure/ImageHolderFigure.java b/jhotdraw-core/src/main/java/org/jhotdraw/draw/figure/ImageHolderFigure.java index 60b27fe9b..faa8bc1fa 100644 --- a/jhotdraw-core/src/main/java/org/jhotdraw/draw/figure/ImageHolderFigure.java +++ b/jhotdraw-core/src/main/java/org/jhotdraw/draw/figure/ImageHolderFigure.java @@ -13,7 +13,7 @@ /** * The interface of a {@link Figure} which has some editable image contents. *

- * The {@link org.jhotdraw.draw.tool.ImageTool} can be used to create figures which implement this + * The {@link org.jhotdraw.samples.svg.SVGCreateFromFileTool} can be used to create figures which implement this * interface. * *


@@ -25,7 +25,7 @@ * That's the reason why {@code Figure} extends the {@code Cloneable} interface. *
* Prototype: {@link ImageHolderFigure}; - * Client: {@link org.jhotdraw.draw.tool.ImageTool}. + * Client: {@link org.jhotdraw.samples.svg.SVGCreateFromFileTool}. * * *

diff --git a/jhotdraw-core/src/main/java/org/jhotdraw/draw/tool/ImageTool.java b/jhotdraw-core/src/main/java/org/jhotdraw/draw/tool/ImageTool.java deleted file mode 100644 index 6671e2f9f..000000000 --- a/jhotdraw-core/src/main/java/org/jhotdraw/draw/tool/ImageTool.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * @(#)ImageTool.java - * - * Copyright (c) 1996-2010 The authors and contributors of JHotDraw. - * You may not use, copy or modify this file, except in compliance with the - * accompanying license terms. - */ -package org.jhotdraw.draw.tool; - -import java.awt.*; -import java.io.*; -import java.util.*; -import java.util.concurrent.ExecutionException; -import javax.swing.*; -import org.jhotdraw.draw.AttributeKey; -import org.jhotdraw.draw.DrawingEditor; -import org.jhotdraw.draw.DrawingView; -import org.jhotdraw.draw.figure.ImageHolderFigure; - -/** - * A tool to create new figures that implement the ImageHolderFigure - * interface, such as ImageFigure. The figure to be created is specified by a - * prototype. - *

- * Immediately, after the ImageTool has been activated, it opens a JFileChooser, - * letting the user specify an image file. The the user then performs - * the following mouse gesture: - *

    - *
  1. Press the mouse button and drag the mouse over the DrawingView. - * This defines the bounds of the created figure.
  2. - *
- * - *
- * Design Patterns - * - *

- * Prototype
- * The {@code ImageTool} creates new figures by cloning a prototype - * {@code ImageHolderFigure} object.
- * Prototype: {@link ImageHolderFigure}; Client: {@link ImageTool}. - *


- * - * @author Werner Randelshofer - * @version $Id$ - */ -public class ImageTool extends CreationTool { - - private static final long serialVersionUID = 1L; - protected FileDialog fileDialog; - protected JFileChooser fileChooser; - protected boolean useFileDialog; - - /** - * Creates a new instance. - */ - public ImageTool(ImageHolderFigure prototype) { - super(prototype); - } - - /** - * Creates a new instance. - */ - public ImageTool(ImageHolderFigure prototype, Map, Object> attributes) { - super(prototype, attributes); - } - - public void setUseFileDialog(boolean newValue) { - useFileDialog = newValue; - if (useFileDialog) { - fileChooser = null; - } else { - fileDialog = null; - } - } - - public boolean isUseFileDialog() { - return useFileDialog; - } - - @Override - public void activate(DrawingEditor editor) { - super.activate(editor); - final DrawingView v = getView(); - if (v == null) { - return; - } - final File file; - if (useFileDialog) { - getFileDialog().setVisible(true); - if (getFileDialog().getFile() != null) { - file = new File(getFileDialog().getDirectory(), getFileDialog().getFile()); - } else { - file = null; - } - } else { - if (getFileChooser().showOpenDialog(v.getComponent()) == JFileChooser.APPROVE_OPTION) { - file = getFileChooser().getSelectedFile(); - } else { - file = null; - } - } - if (file != null) { - final ImageHolderFigure loaderFigure = ((ImageHolderFigure) prototype.clone()); - new SwingWorker() { - @Override - protected Object doInBackground() throws Exception { - loaderFigure.loadImage(file); - return null; - } - - @Override - protected void done() { - try { - get(); //will throw an ExecutionException if in doInBackground something went wrong. - if (createdFigure == null) { - ((ImageHolderFigure) prototype).setImage(loaderFigure.getImageData(), loaderFigure.getBufferedImage()); - } else { - ((ImageHolderFigure) createdFigure).setImage(loaderFigure.getImageData(), loaderFigure.getBufferedImage()); - } - } catch (IOException ex) { - JOptionPane.showMessageDialog(v.getComponent(), - ex.getMessage(), - null, - JOptionPane.ERROR_MESSAGE); - } catch (InterruptedException | ExecutionException ex) { - JOptionPane.showMessageDialog(v.getComponent(), - ex.getMessage(), - null, - JOptionPane.ERROR_MESSAGE); - getDrawing().remove(createdFigure); - fireToolDone(); - } - } - }.execute(); - } else { - //getDrawing().remove(createdFigure); - if (isToolDoneAfterCreation()) { - fireToolDone(); - } - } - } - - private JFileChooser getFileChooser() { - if (fileChooser == null) { - fileChooser = new JFileChooser(); - } - return fileChooser; - } - - private FileDialog getFileDialog() { - if (fileDialog == null) { - fileDialog = new FileDialog(new Frame()); - } - return fileDialog; - } -} diff --git a/jhotdraw-core/src/test/java/org/jhotdraw/draw/action/AlignActionTest.java b/jhotdraw-core/src/test/java/org/jhotdraw/draw/action/AlignActionTest.java new file mode 100644 index 000000000..0f30d9979 --- /dev/null +++ b/jhotdraw-core/src/test/java/org/jhotdraw/draw/action/AlignActionTest.java @@ -0,0 +1,150 @@ +package org.jhotdraw.draw.action; + +import org.jhotdraw.draw.Drawing; +import org.jhotdraw.draw.DrawingEditor; +import org.jhotdraw.draw.DrawingView; +import org.jhotdraw.draw.figure.Figure; +import org.jhotdraw.draw.action.AlignAction.AlignmentType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +class AlignActionTest { + + @Mock + private DrawingEditor editor; + + @Mock + private DrawingView view; + + @Mock + private Figure figure; + + @Mock + private Drawing drawing; + + private AlignAction alignAction; + + private Set
figuresSet; + + @BeforeEach + public void setUp() { + figuresSet = new HashSet<>(Arrays.asList(figure, figure)); + alignAction = new AlignAction(editor, AlignmentType.NORTH); // or any other alignment type + lenient().when(editor.getActiveView()).thenReturn(view); + lenient().when(view.getDrawing()).thenReturn(drawing); + lenient().when(view.getSelectedFigures()).thenReturn(figuresSet); // For multiple figures + lenient().when(figure.getBounds()).thenReturn(new Rectangle2D.Double(0, 0, 100, 100)); + lenient().when(view.getDrawing()).thenReturn(drawing); + lenient().when(figure.isTransformable()).thenReturn(true); + } + + @Test + public void testConstructor() { + AlignAction action = new AlignAction(editor, AlignmentType.NORTH); + assertNotNull(action); + // Additional assertions for constructor behavior + } + + @Test + public void testActionPerformed() { + AlignAction action = new AlignAction(editor, AlignmentType.NORTH); + action.actionPerformed(null); + // Additional assertions to verify behavior + } + + @Test + public void testCalculateAlignmentDelta() { + Rectangle2D.Double figureBounds = new Rectangle2D.Double(10, 10, 100, 100); + Rectangle2D.Double selectionBounds = new Rectangle2D.Double(0, 0, 200, 200); + + // Define alignment types and expected deltas + Object[][] testCases = { + { AlignmentType.NORTH, new Point2D.Double(0, -10) }, + { AlignmentType.EAST, new Point2D.Double(90, 0) }, + { AlignmentType.SOUTH, new Point2D.Double(0, 90) }, + { AlignmentType.WEST, new Point2D.Double(-10, 0) }, + { AlignmentType.HORIZONTAL, new Point2D.Double(40, 0) }, + { AlignmentType.VERTICAL, new Point2D.Double(0, 40) } + }; + + for (Object[] testCase : testCases) { + AlignmentType alignmentType = (AlignmentType) testCase[0]; + Point2D.Double expectedDelta = (Point2D.Double) testCase[1]; + + AlignAction action = new AlignAction(editor, alignmentType); + Point2D.Double actualDelta = action.calculateAlignmentDelta(figureBounds, selectionBounds); + + assertEquals(expectedDelta.x, actualDelta.x, "Mismatch in X delta for " + alignmentType); + assertEquals(expectedDelta.y, actualDelta.y, "Mismatch in Y delta for " + alignmentType); + } + } + + + @Test + public void testTransformFigure() { + AffineTransform transform = new AffineTransform(); + alignAction.transformFigure(figure, transform); + verify(figure, times(1)).willChange(); + verify(figure, times(1)).transform(transform); + verify(figure, times(1)).changed(); + // Additional assertions to verify correct transformation + } + + @Test + public void testGetSelectionBounds() { + when(view.getSelectedFigures()).thenReturn(figuresSet); // For a single figure + Rectangle2D.Double bounds = alignAction.getSelectionBounds(); + assertNotNull(bounds); + // Assertions to verify correct bounds + } + + @Test + public void testUpdateEnabledState() { + when(view.isEnabled()).thenReturn(true); // Ensure view is enabled + when(view.getSelectionCount()).thenReturn(2); // Ensure selection count is more than 1 + + alignAction.updateEnabledState(); + + verify(view, times(1)).isEnabled(); + verify(view, times(1)).getSelectionCount(); + // Additional assertions to verify state update + } + @Test + public void testFormatAlignmentName() { + AlignAction action = new AlignAction(editor, AlignmentType.NORTH); + String formattedName = action.formatAlignmentName(AlignmentType.NORTH); + assertEquals("North", formattedName); + // Additional assertions for other alignment types + } + + @Test + public void testAlignNonTransformableFigures() { + when(figure.isTransformable()).thenReturn(false); + alignAction.actionPerformed(null); + verify(figure, never()).willChange(); + // Assert no transformations are applied + } + + @Test + public void testUpdateEnabledStateWithNoSelection() { + lenient().when(view.getSelectionCount()).thenReturn(0); + alignAction.updateEnabledState(); + assertFalse(alignAction.isEnabled()); + } +} + + diff --git a/jhotdraw-datatransfer/jhotdraw-datatransfer.iml b/jhotdraw-datatransfer/jhotdraw-datatransfer.iml new file mode 100644 index 000000000..5cc1bbfd2 --- /dev/null +++ b/jhotdraw-datatransfer/jhotdraw-datatransfer.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-gui/jhotdraw-gui.iml b/jhotdraw-gui/jhotdraw-gui.iml new file mode 100644 index 000000000..6fe88eafe --- /dev/null +++ b/jhotdraw-gui/jhotdraw-gui.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-gui/src/main/java/org/jhotdraw/gui/action/ButtonFactory.java b/jhotdraw-gui/src/main/java/org/jhotdraw/gui/action/ButtonFactory.java index 1470a011b..9da4d5385 100644 --- a/jhotdraw-gui/src/main/java/org/jhotdraw/gui/action/ButtonFactory.java +++ b/jhotdraw-gui/src/main/java/org/jhotdraw/gui/action/ButtonFactory.java @@ -64,6 +64,7 @@ import static org.jhotdraw.draw.AttributeKeys.STROKE_TYPE; import static org.jhotdraw.draw.AttributeKeys.STROKE_WIDTH; import static org.jhotdraw.draw.AttributeKeys.TEXT_COLOR; + import org.jhotdraw.draw.DrawingEditor; import org.jhotdraw.draw.DrawingView; import org.jhotdraw.draw.action.AbstractSelectedAction; @@ -367,6 +368,7 @@ public static JToggleButton addSelectionToolTo(JToolBar tb, final DrawingEditor return addSelectionToolTo(tb, editor, selectionTool); } + public static JToggleButton addSelectionToolTo(JToolBar tb, final DrawingEditor editor, Tool selectionTool) { ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels"); JToggleButton t; @@ -1655,17 +1657,42 @@ public static void addAlignmentButtonsTo(JToolBar bar, final DrawingEditor edito */ public static void addAlignmentButtonsTo(JToolBar bar, final DrawingEditor editor, java.util.List dsp) { AbstractSelectedAction d; - bar.add(d = new AlignAction.West(editor)).setFocusable(false); + JButton button; + + // West Alignment + button = bar.add(d = new AlignAction(editor, AlignAction.AlignmentType.WEST)); + button.setName("alignWestButton"); // Setting name + button.setFocusable(false); dsp.add(d); - bar.add(d = new AlignAction.East(editor)).setFocusable(false); + + // East Alignment + button = bar.add(d = new AlignAction(editor, AlignAction.AlignmentType.EAST)); + button.setName("alignEastButton"); + button.setFocusable(false); dsp.add(d); - bar.add(d = new AlignAction.Horizontal(editor)).setFocusable(false); + + // Horizontal Alignment + button = bar.add(d = new AlignAction(editor, AlignAction.AlignmentType.HORIZONTAL)); + button.setName("alignHorizontalButton"); + button.setFocusable(false); dsp.add(d); - bar.add(d = new AlignAction.North(editor)).setFocusable(false); + + // North Alignment + button = bar.add(d = new AlignAction(editor, AlignAction.AlignmentType.NORTH)); + button.setName("alignNorthButton"); + button.setFocusable(false); dsp.add(d); - bar.add(d = new AlignAction.South(editor)).setFocusable(false); + + // South Alignment + button = bar.add(d = new AlignAction(editor, AlignAction.AlignmentType.SOUTH)); + button.setName("alignSouthButton"); + button.setFocusable(false); dsp.add(d); - bar.add(d = new AlignAction.Vertical(editor)).setFocusable(false); + + // Vertical Alignment + button = bar.add(d = new AlignAction(editor, AlignAction.AlignmentType.VERTICAL)); + button.setName("alignVerticalButton"); + button.setFocusable(false); dsp.add(d); bar.addSeparator(); bar.add(d = new MoveAction.West(editor)).setFocusable(false); diff --git a/jhotdraw-samples/jhotdraw-samples (2).iml b/jhotdraw-samples/jhotdraw-samples (2).iml new file mode 100644 index 000000000..5c7108fc1 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples (2).iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-mini/jhotdraw-samples-mini.iml b/jhotdraw-samples/jhotdraw-samples-mini/jhotdraw-samples-mini.iml new file mode 100644 index 000000000..ba08956d2 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-mini/jhotdraw-samples-mini.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/jhotdraw-samples-misc.iml b/jhotdraw-samples/jhotdraw-samples-misc/jhotdraw-samples-misc.iml new file mode 100644 index 000000000..a1b871bff --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/jhotdraw-samples-misc.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/jhotdraw-samples.iml b/jhotdraw-samples/jhotdraw-samples-misc/jhotdraw-samples.iml new file mode 100644 index 000000000..b30d8cebc --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/jhotdraw-samples.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/pom.xml b/jhotdraw-samples/jhotdraw-samples-misc/pom.xml index 1d63de64d..7c0f0d7e2 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/pom.xml +++ b/jhotdraw-samples/jhotdraw-samples-misc/pom.xml @@ -1,6 +1,13 @@ 4.0.0 + + + github + GitHub external Packages + https://maven.pkg.github.com/sweat-tek/MavenRepository + + org.jhotdraw jhotdraw-samples @@ -24,11 +31,28 @@ htmlunit 2.37.0 + + dk.sdu.cbse + featuretracerlite + 1.1-SNAPSHOT + org.aspectj aspectjweaver 1.9.9 + + org.assertj + assertj-swing-junit + 3.9.2 + test + + + com.tngtech.jgiven + jgiven-junit + 1.3.0 + test + ${project.groupId} jhotdraw-app @@ -40,26 +64,65 @@ 4.13.2 test + + org.mockito + mockito-core + 5.3.1 + test + + + org.assertj + assertj-core + 3.24.2 + test + + + net.bytebuddy + byte-buddy + 1.14.9 + test + + + ch.qos.logback + logback-classic + 1.4.12 + + - - org.codehaus.mojo - exec-maven-plugin - 3.1.0 - - - run-java - install - - java - - - - - org.jhotdraw.samples.svg.Main - - + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + run-java + install + + java + + + + + org.jhotdraw.samples.svg.Main + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 + + + -javaagent:"/Users/jespe/.m2/repository/org/aspectj/aspectjweaver/1.9.9/aspectjweaver-1.9.9.jar" + --add-opens java.base/java.lang=ALL-UNNAMED + + true + always + + + + \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/draw/DrawApplicationModel.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/draw/DrawApplicationModel.java index ae9f128a2..c74ff29c2 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/draw/DrawApplicationModel.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/draw/DrawApplicationModel.java @@ -15,6 +15,8 @@ import org.jhotdraw.api.app.View; import org.jhotdraw.api.gui.URIChooser; import org.jhotdraw.app.DefaultApplicationModel; +import org.jhotdraw.draw.AttributeKey; +import org.jhotdraw.draw.AttributeKeys; import org.jhotdraw.draw.figure.AbstractAttributedFigure; import static org.jhotdraw.draw.AttributeKeys.*; import org.jhotdraw.draw.figure.BezierFigure; @@ -23,7 +25,6 @@ import org.jhotdraw.draw.figure.DiamondFigure; import org.jhotdraw.draw.DrawingEditor; import org.jhotdraw.draw.figure.EllipseFigure; -import org.jhotdraw.draw.figure.ImageFigure; import org.jhotdraw.draw.figure.LineConnectionFigure; import org.jhotdraw.draw.figure.LineFigure; import org.jhotdraw.draw.figure.RectangleFigure; @@ -37,11 +38,13 @@ import org.jhotdraw.draw.tool.BezierTool; import org.jhotdraw.draw.tool.ConnectionTool; import org.jhotdraw.draw.tool.CreationTool; -import org.jhotdraw.draw.tool.ImageTool; import org.jhotdraw.draw.tool.TextAreaCreationTool; import org.jhotdraw.draw.tool.TextCreationTool; import org.jhotdraw.gui.JFileURIChooser; import org.jhotdraw.gui.action.ButtonFactory; +import org.jhotdraw.samples.svg.SVGCreateFromFileTool; +import org.jhotdraw.samples.svg.figures.SVGGroupFigure; +import org.jhotdraw.samples.svg.figures.SVGImageFigure; import org.jhotdraw.util.*; /** @@ -147,7 +150,11 @@ public void addDefaultCreationButtonsTo(JToolBar tb, final DrawingEditor editor, ButtonFactory.addToolTo(tb, editor, new BezierTool(new BezierFigure(true)), "edit.createPolygon", labels); ButtonFactory.addToolTo(tb, editor, new TextCreationTool(new TextFigure()), "edit.createText", labels); ButtonFactory.addToolTo(tb, editor, new TextAreaCreationTool(new TextAreaFigure()), "edit.createTextArea", labels); - ButtonFactory.addToolTo(tb, editor, new ImageTool(new ImageFigure()), "edit.createImage", labels); + HashMap, Object> attributes; + attributes = new HashMap, Object>(); + attributes.put(AttributeKeys.FILL_COLOR, null); + attributes.put(AttributeKeys.STROKE_COLOR, null); + ButtonFactory.addToolTo(tb, editor, new SVGCreateFromFileTool(new SVGImageFigure(), new SVGGroupFigure(), attributes), "edit.createImage", labels); } @Override diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/draw/Main.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/draw/Main.java index b1a7bb876..208c1ab81 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/draw/Main.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/draw/Main.java @@ -7,6 +7,7 @@ */ package org.jhotdraw.samples.draw; +import dk.sdu.mmmi.featuretracer.lib.FeatureEntryPoint; import org.jhotdraw.api.app.Application; import org.jhotdraw.app.OSXApplication; import org.jhotdraw.app.SDIApplication; @@ -25,7 +26,9 @@ public class Main { /** * Creates a new instance. + * @param args */ + @FeatureEntryPoint("Main") public static void main(String[] args) { ResourceBundleUtil.setVerbose(true); Application app; diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/odg/ODGDrawingPanel.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/odg/ODGDrawingPanel.java index 184a5a226..0fc95906b 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/odg/ODGDrawingPanel.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/odg/ODGDrawingPanel.java @@ -10,6 +10,7 @@ import java.awt.*; import java.util.*; import javax.swing.*; + import org.jhotdraw.action.edit.CopyAction; import org.jhotdraw.action.edit.CutAction; import org.jhotdraw.action.edit.DuplicateAction; diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/Main.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/Main.java index 11e156729..ca929d3ab 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/Main.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/Main.java @@ -26,6 +26,8 @@ public class Main { /** * Creates a new instance. */ + + public static void main(String[] args) { // Debug resource bundle ResourceBundleUtil.setVerbose(true); diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGCreateFromFileTool.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGCreateFromFileTool.java index a222f482c..5eef1d00e 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGCreateFromFileTool.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGCreateFromFileTool.java @@ -14,6 +14,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; + +import dk.sdu.mmmi.featuretracer.lib.FeatureEntryPoint; import org.jhotdraw.draw.AttributeKey; import org.jhotdraw.draw.figure.CompositeFigure; import org.jhotdraw.draw.DefaultDrawing; @@ -53,6 +55,7 @@ public class SVGCreateFromFileTool extends CreationTool { /** * Creates a new instance. */ + @FeatureEntryPoint("SVGTool1") public SVGCreateFromFileTool(ImageHolderFigure imagePrototype, CompositeFigure groupPrototype) { super(imagePrototype); this.groupPrototype = groupPrototype; @@ -62,12 +65,14 @@ public SVGCreateFromFileTool(ImageHolderFigure imagePrototype, CompositeFigure g /** * Creates a new instance. */ + @FeatureEntryPoint("SVGTool2") public SVGCreateFromFileTool(ImageHolderFigure imagePrototype, CompositeFigure groupPrototype, Map, Object> attributes) { super(imagePrototype, attributes); this.groupPrototype = groupPrototype; this.imagePrototype = imagePrototype; } + @FeatureEntryPoint("SVGTool3") public void setUseFileDialog(boolean newValue) { useFileDialog = newValue; if (useFileDialog) { @@ -77,15 +82,21 @@ public void setUseFileDialog(boolean newValue) { } } + @FeatureEntryPoint("SVGTool4") public boolean isUseFileDialog() { return useFileDialog; } + @FeatureEntryPoint("SVGTool5") @Override public void activate(DrawingEditor editor) { super.activate(editor); - final DrawingView v = getView(); - if (v == null) { + final DrawingView v; + try { + v = getView(); + } + catch(NullPointerException e) + { return; } final File file; @@ -97,109 +108,126 @@ public void activate(DrawingEditor editor) { file = null; } } else { - if (getFileChooser().showOpenDialog(v.getComponent()) == JFileChooser.APPROVE_OPTION) { - file = getFileChooser().getSelectedFile(); - } else { - file = null; + try { + if (getFileChooser().showOpenDialog(v.getComponent()) == JFileChooser.APPROVE_OPTION) { + file = getFileChooser().getSelectedFile(); + } else { + file = null; + } + } + catch(NullPointerException e) + { + return; } } if (file != null) { if (file.getName().toLowerCase().endsWith(".svg") || file.getName().toLowerCase().endsWith(".svgz")) { prototype = groupPrototype.clone(); - new SwingWorker() { - @Override - protected Drawing doInBackground() throws Exception { - Drawing drawing = new DefaultDrawing(); - InputFormat in = (file.getName().toLowerCase().endsWith(".svg")) ? new SVGInputFormat() : new SVGZInputFormat(); - in.read(file.toURI(), drawing); - return drawing; - } - - @Override - protected void done() { - try { - Drawing drawing = get(); - CompositeFigure parent; - if (createdFigure == null) { - parent = (CompositeFigure) prototype; - for (Figure f : drawing.getChildren()) { - parent.basicAdd(f); - } - } else { - parent = (CompositeFigure) createdFigure; - parent.willChange(); - for (Figure f : drawing.getChildren()) { - parent.add(f); - } - parent.changed(); - } - } catch (InterruptedException | ExecutionException ex) { - Logger.getLogger(SVGCreateFromFileTool.class.getName()).log(Level.SEVERE, null, ex); - failed(ex); - } - } - - protected void failed(Throwable t) { - JOptionPane.showMessageDialog(v.getComponent(), - t.getMessage(), - null, - JOptionPane.ERROR_MESSAGE); - getDrawing().remove(createdFigure); - fireToolDone(); - } - }.execute(); + SVGSwingWorker(file, v); } else { prototype = imagePrototype; - final ImageHolderFigure loaderFigure = ((ImageHolderFigure) prototype.clone()); - new SwingWorker() { - @Override - protected Object doInBackground() throws Exception { - loaderFigure.loadImage(file); - return null; - } + imageSwingWorker(file, v); + } + } else { + if (isToolDoneAfterCreation()) { + fireToolDone(); + } + } + } + + protected void SVGSwingWorker(File file, DrawingView v) { + new SwingWorker() { - @Override - protected void done() { - try { - get(); - try { - if (createdFigure == null) { - ((ImageHolderFigure) prototype).setImage(loaderFigure.getImageData(), loaderFigure.getBufferedImage()); - } else { - ((ImageHolderFigure) createdFigure).setImage(loaderFigure.getImageData(), loaderFigure.getBufferedImage()); - } - } catch (IOException ex) { - JOptionPane.showMessageDialog(v.getComponent(), - ex.getMessage(), - null, - JOptionPane.ERROR_MESSAGE); - } - } catch (InterruptedException | ExecutionException ex) { - Logger.getLogger(SVGCreateFromFileTool.class.getName()).log( Level.SEVERE, null, ex); - failed(ex); + @Override + protected Drawing doInBackground() throws Exception { + Drawing drawing = new DefaultDrawing(); + InputFormat in = (file.getName().toLowerCase().endsWith(".svg")) ? new SVGInputFormat() : new SVGZInputFormat(); + in.read(file.toURI(), drawing); + return drawing; + } + + @Override + protected void done() { + try { + Drawing drawing = get(); + CompositeFigure parent; + if (createdFigure == null) { + parent = (CompositeFigure) prototype; + for (Figure f : drawing.getChildren()) { + parent.basicAdd(f); + } + } else { + parent = (CompositeFigure) createdFigure; + parent.willChange(); + for (Figure f : drawing.getChildren()) { + parent.add(f); } - + parent.changed(); } + } catch (InterruptedException | ExecutionException ex) { + Logger.getLogger(SVGCreateFromFileTool.class.getName()).log(Level.SEVERE, null, ex); + failed(ex); + } + } + + protected void failed(Throwable t) { + JOptionPane.showMessageDialog(v.getComponent(), + t.getMessage(), + null, + JOptionPane.ERROR_MESSAGE); + getDrawing().remove(createdFigure); + fireToolDone(); + } + + }.execute(); + } + + protected void imageSwingWorker(File file, DrawingView v) { + final ImageHolderFigure loaderFigure = ((ImageHolderFigure) prototype.clone()); + new SwingWorker() { - protected void failed(Throwable t) { + @Override + protected Object doInBackground() throws Exception { + loaderFigure.loadImage(file); + return null; + } + + @Override + protected void done() { + try { + get(); + try { + if (createdFigure == null) { + ((ImageHolderFigure) prototype).setImage(loaderFigure.getImageData(), loaderFigure.getBufferedImage()); + } else { + ((ImageHolderFigure) createdFigure).setImage(loaderFigure.getImageData(), loaderFigure.getBufferedImage()); + } + } catch (IOException ex) { JOptionPane.showMessageDialog(v.getComponent(), - t.getMessage(), + ex.getMessage(), null, JOptionPane.ERROR_MESSAGE); - getDrawing().remove(createdFigure); - fireToolDone(); } - }.execute(); + } catch (InterruptedException | ExecutionException ex) { + Logger.getLogger(SVGCreateFromFileTool.class.getName()).log(Level.SEVERE, null, ex); + failed(ex); + } + } - } else { - //getDrawing().remove(createdFigure); - if (isToolDoneAfterCreation()) { + protected void failed(Throwable t) { + JOptionPane.showMessageDialog(v.getComponent(), + t.getMessage(), + null, + JOptionPane.ERROR_MESSAGE); + getDrawing().remove(createdFigure); fireToolDone(); } - } + + }.execute(); } + @FeatureEntryPoint("SVGTool6") @Override protected Figure createFigure() { if (prototype instanceof CompositeFigure) { @@ -212,6 +240,7 @@ protected Figure createFigure() { } } + @FeatureEntryPoint("SVGTool7") private JFileChooser getFileChooser() { if (fileChooser == null) { fileChooser = new JFileChooser(); @@ -219,6 +248,7 @@ private JFileChooser getFileChooser() { return fileChooser; } + @FeatureEntryPoint("SVGTool8") private FileDialog getFileDialog() { if (fileDialog == null) { fileDialog = new FileDialog(new Frame()); diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/HandleCreator.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/HandleCreator.java new file mode 100644 index 000000000..e0b05a2fc --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/HandleCreator.java @@ -0,0 +1,9 @@ +package org.jhotdraw.samples.svg.figures; + +import org.jhotdraw.draw.handle.Handle; + +import java.util.Collection; + +public abstract class HandleCreator { + public abstract Collection createHandles(SVGFigure figure); +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/MouseHoverHandleCreator.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/MouseHoverHandleCreator.java new file mode 100644 index 000000000..fc3980e36 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/MouseHoverHandleCreator.java @@ -0,0 +1,17 @@ +package org.jhotdraw.samples.svg.figures; + +import org.jhotdraw.draw.handle.BoundsOutlineHandle; +import org.jhotdraw.draw.handle.Handle; + +import java.util.Collection; +import java.util.LinkedList; + +public class MouseHoverHandleCreator extends HandleCreator { + + @Override + public Collection createHandles(SVGFigure figure) { + LinkedList handles = new LinkedList<>(); + handles.add(new BoundsOutlineHandle(figure, false, true)); + return handles; + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/OneDetailLevelHandleCreator.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/OneDetailLevelHandleCreator.java new file mode 100644 index 000000000..587c70239 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/OneDetailLevelHandleCreator.java @@ -0,0 +1,16 @@ +package org.jhotdraw.samples.svg.figures; + +import org.jhotdraw.draw.handle.Handle; +import org.jhotdraw.draw.handle.TransformHandleKit; + +import java.util.Collection; +import java.util.LinkedList; + +public class OneDetailLevelHandleCreator extends HandleCreator{ + @Override + public Collection createHandles(SVGFigure figure) { + LinkedList handles = new LinkedList<>(); + TransformHandleKit.addTransformHandles(figure, handles); + return handles; + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/PROPERTY.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/PROPERTY.java new file mode 100644 index 000000000..9ab099d5a --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/PROPERTY.java @@ -0,0 +1,22 @@ +package org.jhotdraw.samples.svg.figures; + +public enum PROPERTY { + /** + * Identifies the {@code arcWidth} JavaBeans property. + */ + ARC_WIDTH("arcWidth"), + + /** + * Identifies the {@code arcHeight} JavaBeans property. + */ + ARC_HEIGHT("arcHeight"); + + private final String name; + PROPERTY(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/SVGRectFigure.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/SVGRectFigure.java index d1ba6eae5..35b6aca5f 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/SVGRectFigure.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/SVGRectFigure.java @@ -10,16 +10,9 @@ import java.awt.*; import java.awt.geom.*; import java.util.*; +import dk.sdu.mmmi.featuretracer.lib.FeatureEntryPoint; import org.jhotdraw.draw.*; -import static org.jhotdraw.draw.AttributeKeys.FILL_COLOR; -import static org.jhotdraw.draw.AttributeKeys.STROKE_CAP; -import static org.jhotdraw.draw.AttributeKeys.STROKE_JOIN; -import static org.jhotdraw.draw.AttributeKeys.STROKE_MITER_LIMIT; -import static org.jhotdraw.draw.AttributeKeys.TRANSFORM; -import org.jhotdraw.draw.handle.BoundsOutlineHandle; import org.jhotdraw.draw.handle.Handle; -import org.jhotdraw.draw.handle.ResizeHandleKit; -import org.jhotdraw.draw.handle.TransformHandleKit; import org.jhotdraw.geom.Geom; import org.jhotdraw.geom.GrowStroke; import org.jhotdraw.samples.svg.Gradient; @@ -35,14 +28,6 @@ public class SVGRectFigure extends SVGAttributedFigure implements SVGFigure { private static final long serialVersionUID = 1L; - /** - * Identifies the {@code arcWidth} JavaBeans property. - */ - public static final String ARC_WIDTH_PROPERTY = "arcWidth"; - /** - * Identifies the {@code arcHeight} JavaBeans property. - */ - public static final String ARC_HEIGHT_PROPERTY = "arcHeight"; /** * The variable acv is used for generating the locations of the control * points for the rounded rectangle using path.curveTo. @@ -59,7 +44,7 @@ public class SVGRectFigure extends SVGAttributedFigure implements SVGFigure { } /** */ - private RoundRectangle2D.Double roundrect; + private RoundRectangle2D.Double roundRect; /** * This is used to perform faster drawing. */ @@ -79,9 +64,8 @@ public SVGRectFigure() { public SVGRectFigure(double x, double y, double width, double height) { this(x, y, width, height, 0, 0); } - public SVGRectFigure(double x, double y, double width, double height, double rx, double ry) { - roundrect = new RoundRectangle2D.Double(x, y, width, height, rx, ry); + roundRect = new RoundRectangle2D.Double(x, y, width, height, rx, ry); SVGAttributeKeys.setDefaults(this); setConnectable(false); } @@ -90,93 +74,94 @@ public SVGRectFigure(double x, double y, double width, double height, double rx, @Override protected void drawFill(Graphics2D g) { if (getArcHeight() == 0d && getArcWidth() == 0d) { - g.fill(roundrect.getBounds2D()); + g.fill(roundRect.getBounds2D()); } else { - g.fill(roundrect); + g.fill(roundRect); } } @Override protected void drawStroke(Graphics2D g) { - if (roundrect.archeight == 0 && roundrect.arcwidth == 0) { - g.draw(roundrect.getBounds2D()); + if (roundRect.archeight == 0 && roundRect.arcwidth == 0) { + g.draw(roundRect.getBounds2D()); } else { // We have to generate the path for the round rectangle manually, - // because the path of a Java RoundRectangle is drawn counter clockwise + // because the path of a Java RoundRectangle is drawn counterclockwise // whereas an SVG rect needs to be drawn clockwise. Path2D.Double p = new Path2D.Double(); - double aw = roundrect.arcwidth / 2d; - double ah = roundrect.archeight / 2d; - p.moveTo((roundrect.x + aw), (float) roundrect.y); - p.lineTo((roundrect.x + roundrect.width - aw), (float) roundrect.y); - p.curveTo((roundrect.x + roundrect.width - aw * ACV), (float) roundrect.y, - (roundrect.x + roundrect.width), (float) (roundrect.y + ah * ACV), - (roundrect.x + roundrect.width), (roundrect.y + ah)); - p.lineTo((roundrect.x + roundrect.width), (roundrect.y + roundrect.height - ah)); + double aw = roundRect.arcwidth / 2d; + double ah = roundRect.archeight / 2d; + p.moveTo((roundRect.x + aw), (float) roundRect.y); + p.lineTo((roundRect.x + roundRect.width - aw), (float) roundRect.y); + p.curveTo((roundRect.x + roundRect.width - aw * ACV), (float) roundRect.y, + (roundRect.x + roundRect.width), (float) (roundRect.y + ah * ACV), + (roundRect.x + roundRect.width), (roundRect.y + ah)); + p.lineTo((roundRect.x + roundRect.width), (roundRect.y + roundRect.height - ah)); p.curveTo( - (roundrect.x + roundrect.width), (roundrect.y + roundrect.height - ah * ACV), - (roundrect.x + roundrect.width - aw * ACV), (roundrect.y + roundrect.height), - (roundrect.x + roundrect.width - aw), (roundrect.y + roundrect.height)); - p.lineTo((roundrect.x + aw), (roundrect.y + roundrect.height)); - p.curveTo((roundrect.x + aw * ACV), (roundrect.y + roundrect.height), - (roundrect.x), (roundrect.y + roundrect.height - ah * ACV), - (float) roundrect.x, (roundrect.y + roundrect.height - ah)); - p.lineTo((float) roundrect.x, (roundrect.y + ah)); - p.curveTo((roundrect.x), (roundrect.y + ah * ACV), - (roundrect.x + aw * ACV), (float) (roundrect.y), - (float) (roundrect.x + aw), (float) (roundrect.y)); + (roundRect.x + roundRect.width), (roundRect.y + roundRect.height - ah * ACV), + (roundRect.x + roundRect.width - aw * ACV), (roundRect.y + roundRect.height), + (roundRect.x + roundRect.width - aw), (roundRect.y + roundRect.height)); + p.lineTo((roundRect.x + aw), (roundRect.y + roundRect.height)); + p.curveTo((roundRect.x + aw * ACV), (roundRect.y + roundRect.height), + (roundRect.x), (roundRect.y + roundRect.height - ah * ACV), + (float) roundRect.x, (roundRect.y + roundRect.height - ah)); + p.lineTo((float) roundRect.x, (roundRect.y + ah)); + p.curveTo((roundRect.x), (roundRect.y + ah * ACV), + (roundRect.x + aw * ACV), (float) (roundRect.y), + (float) (roundRect.x + aw), (float) (roundRect.y)); p.closePath(); g.draw(p); } } + // SHAPE AND BOUNDS public double getX() { - return roundrect.x; + return roundRect.x; } public double getY() { - return roundrect.y; + return roundRect.y; } public double getWidth() { - return roundrect.width; + return roundRect.width; } public double getHeight() { - return roundrect.height; + return roundRect.height; } /** * Gets the arc width. */ public double getArcWidth() { - return roundrect.arcwidth; + return roundRect.arcwidth; } /** * Gets the arc height. */ public double getArcHeight() { - return roundrect.archeight; + return roundRect.archeight; } /** * Sets the arc width. */ public void setArcWidth(double newValue) { - double oldValue = roundrect.arcwidth; - roundrect.arcwidth = newValue; - firePropertyChange(ARC_WIDTH_PROPERTY, oldValue, newValue); + double oldValue = roundRect.arcwidth; + roundRect.arcwidth = newValue; + firePropertyChange(PROPERTY.ARC_WIDTH.getName(), oldValue, newValue); } /** * Sets the arc height. */ public void setArcHeight(double newValue) { - double oldValue = roundrect.archeight; - roundrect.archeight = newValue; - firePropertyChange(ARC_HEIGHT_PROPERTY, oldValue, newValue); + double oldValue = roundRect.archeight; + roundRect.archeight = newValue; + firePropertyChange(PROPERTY.ARC_HEIGHT.getName(), oldValue, newValue); } /** @@ -189,7 +174,7 @@ public void setArc(double width, double height) { @Override public Rectangle2D.Double getBounds() { - return (Rectangle2D.Double) roundrect.getBounds2D(); + return (Rectangle2D.Double) roundRect.getBounds2D(); } @Override @@ -217,6 +202,7 @@ public Rectangle2D.Double getDrawingArea() { /** * Checks if a Point2D.Double is inside the figure. */ + @Override public boolean contains(Point2D.Double p) { return getHitShape().contains(p); @@ -225,10 +211,10 @@ public boolean contains(Point2D.Double p) { @Override public void setBounds(Point2D.Double anchor, Point2D.Double lead) { invalidateTransformedShape(); - roundrect.x = Math.min(anchor.x, lead.x); - roundrect.y = Math.min(anchor.y, lead.y); - roundrect.width = Math.max(0.1, Math.abs(lead.x - anchor.x)); - roundrect.height = Math.max(0.1, Math.abs(lead.y - anchor.y)); + roundRect.x = Math.min(anchor.x, lead.x); + roundRect.y = Math.min(anchor.y, lead.y); + roundRect.width = Math.max(0.1, Math.abs(lead.x - anchor.x)); + roundRect.height = Math.max(0.1, Math.abs(lead.y - anchor.y)); invalidate(); } @@ -240,9 +226,9 @@ private void invalidateTransformedShape() { private Shape getTransformedShape() { if (cachedTransformedShape == null) { if (getArcHeight() == 0 || getArcWidth() == 0) { - cachedTransformedShape = roundrect.getBounds2D(); + cachedTransformedShape = roundRect.getBounds2D(); } else { - cachedTransformedShape = (Shape) roundrect.clone(); + cachedTransformedShape = (Shape) roundRect.clone(); } if (get(TRANSFORM) != null) { cachedTransformedShape = get(TRANSFORM).createTransformedShape(cachedTransformedShape); @@ -255,10 +241,10 @@ private Shape getHitShape() { if (cachedHitShape == null) { if (get(FILL_COLOR) != null || get(FILL_GRADIENT) != null) { cachedHitShape = new GrowStroke( - (float) SVGAttributeKeys.getStrokeTotalWidth(this, 1.0) / 2f, - (float) SVGAttributeKeys.getStrokeTotalMiterLimit(this, 1.0)).createStrokedShape(getTransformedShape()); + (float) AttributeKeys.getStrokeTotalWidth(this, 1.0) / 2f, + (float) AttributeKeys.getStrokeTotalMiterLimit(this, 1.0)).createStrokedShape(getTransformedShape()); } else { - cachedHitShape = SVGAttributeKeys.getHitStroke(this, 1.0).createStrokedShape(getTransformedShape()); + cachedHitShape = AttributeKeys.getHitStroke(this, 1.0).createStrokedShape(getTransformedShape()); } } return cachedHitShape; @@ -273,7 +259,7 @@ private Shape getHitShape() { public void transform(AffineTransform tx) { invalidateTransformedShape(); if (get(TRANSFORM) != null - || // (tx.getType() & (AffineTransform.TYPE_TRANSLATION | AffineTransform.TYPE_MASK_SCALE)) != tx.getType()) { + || (tx.getType() & (AffineTransform.TYPE_TRANSLATION)) != tx.getType()) { if (get(TRANSFORM) == null) { set(TRANSFORM, (AffineTransform) tx.clone()); @@ -307,7 +293,7 @@ public void transform(AffineTransform tx) { public void restoreTransformTo(Object geometry) { invalidateTransformedShape(); Object[] restoreData = (Object[]) geometry; - roundrect = (RoundRectangle2D.Double) ((RoundRectangle2D.Double) restoreData[0]).clone(); + roundRect = (RoundRectangle2D.Double) ((RoundRectangle2D.Double) restoreData[0]).clone(); TRANSFORM.setClone(this, (AffineTransform) restoreData[1]); FILL_GRADIENT.setClone(this, (Gradient) restoreData[2]); STROKE_GRADIENT.setClone(this, (Gradient) restoreData[3]); @@ -316,7 +302,7 @@ public void restoreTransformTo(Object geometry) { @Override public Object getTransformRestoreData() { return new Object[]{ - roundrect.clone(), + roundRect.clone(), TRANSFORM.getClone(this), FILL_GRADIENT.getClone(this), STROKE_GRADIENT.getClone(this)}; @@ -324,31 +310,31 @@ public Object getTransformRestoreData() { // EDITING @Override + @FeatureEntryPoint(value = "Edit a rectangle") public Collection createHandles(int detailLevel) { - LinkedList handles = new LinkedList(); + HandleCreator handleCreator; switch (detailLevel % 2) { case -1: // Mouse hover handles - handles.add(new BoundsOutlineHandle(this, false, true)); + handleCreator = new MouseHoverHandleCreator(); break; case 0: - ResizeHandleKit.addResizeHandles(this, handles); - handles.add(new SVGRectRadiusHandle(this)); - handles.add(new LinkHandle(this)); + handleCreator = new ZeroDetailLevelHandleCreator(); break; case 1: - TransformHandleKit.addTransformHandles(this, handles); + handleCreator = new OneDetailLevelHandleCreator(); break; default: - break; + throw new UnsupportedOperationException("Unsupported detail level"); } - return handles; + + return handleCreator.createHandles(this); } // CLONING @Override public SVGRectFigure clone() { SVGRectFigure that = (SVGRectFigure) super.clone(); - that.roundrect = (RoundRectangle2D.Double) this.roundrect.clone(); + that.roundRect = (RoundRectangle2D.Double) this.roundRect.clone(); that.cachedTransformedShape = null; that.cachedHitShape = null; return that; diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/SVGRectRadiusHandle.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/SVGRectRadiusHandle.java index 5655cf070..e9514d93a 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/SVGRectRadiusHandle.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/SVGRectRadiusHandle.java @@ -11,7 +11,6 @@ import java.awt.*; import java.awt.event.KeyEvent; import java.awt.geom.*; -import org.jhotdraw.draw.*; import static org.jhotdraw.draw.AttributeKeys.TRANSFORM; import org.jhotdraw.draw.event.CompositeFigureEdit; import org.jhotdraw.draw.handle.AbstractHandle; @@ -111,8 +110,8 @@ public void trackEnd(Point anchor, Point lead, int modifiersEx) { CompositeFigureEdit edit = new CompositeFigureEdit(svgRect, labels.getString("attribute.roundRectRadius")); edit.setVerbose(true); fireUndoableEditHappened(edit); - fireUndoableEditHappened(new PropertyChangeEdit(svgRect, SVGRectFigure.ARC_WIDTH_PROPERTY, oldValue.width, newValue.width)); - fireUndoableEditHappened(new PropertyChangeEdit(svgRect, SVGRectFigure.ARC_HEIGHT_PROPERTY, oldValue.height, newValue.height)); + fireUndoableEditHappened(new PropertyChangeEdit(svgRect, PROPERTY.ARC_WIDTH.name(), oldValue.width, newValue.width)); + fireUndoableEditHappened(new PropertyChangeEdit(svgRect, PROPERTY.ARC_HEIGHT.getName(), oldValue.height, newValue.height)); fireUndoableEditHappened(edit); } @@ -151,8 +150,8 @@ public void keyPressed(KeyEvent evt) { = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels"); CompositeFigureEdit edit = new CompositeFigureEdit(owner, labels.getString("attribute.roundRectRadius")); fireUndoableEditHappened(edit); - fireUndoableEditHappened(new PropertyChangeEdit(owner, SVGRectFigure.ARC_WIDTH_PROPERTY, oldArc.width, newArc.width)); - fireUndoableEditHappened(new PropertyChangeEdit(owner, SVGRectFigure.ARC_HEIGHT_PROPERTY, oldArc.height, newArc.height)); + fireUndoableEditHappened(new PropertyChangeEdit(owner, PROPERTY.ARC_WIDTH.getName(), oldArc.width, newArc.width)); + fireUndoableEditHappened(new PropertyChangeEdit(owner, PROPERTY.ARC_HEIGHT.getName(), oldArc.height, newArc.height)); fireUndoableEditHappened(edit); } } diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/ZeroDetailLevelHandleCreator.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/ZeroDetailLevelHandleCreator.java new file mode 100644 index 000000000..aa68450b1 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/figures/ZeroDetailLevelHandleCreator.java @@ -0,0 +1,18 @@ +package org.jhotdraw.samples.svg.figures; + +import org.jhotdraw.draw.handle.Handle; +import org.jhotdraw.draw.handle.ResizeHandleKit; + +import java.util.Collection; +import java.util.LinkedList; + +public class ZeroDetailLevelHandleCreator extends HandleCreator{ + @Override + public Collection createHandles(SVGFigure figure) { + LinkedList handles = new LinkedList<>(); + ResizeHandleKit.addResizeHandles(figure, handles); + handles.add(new SVGRectRadiusHandle(figure)); + handles.add(new LinkHandle(figure)); + return handles; + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/gui/AlignToolBar.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/gui/AlignToolBar.java index 7d5671d65..d862a46aa 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/gui/AlignToolBar.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/gui/AlignToolBar.java @@ -69,13 +69,13 @@ protected JComponent createDisclosedComponent(int state) { AbstractSelectedAction d; gbc = new GridBagConstraints(); gbc.gridy = 0; - btn = new JButton(d = new AlignAction.West(editor, labels)); + btn = new JButton(d = new AlignAction(editor, AlignAction.AlignmentType.WEST, labels)); disposables.add(d); btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); btn.setText(null); p.add(btn, gbc); gbc.insets = new Insets(0, 3, 0, 0); - btn = new JButton(d = new AlignAction.East(editor, labels)); + btn = new JButton(d = new AlignAction(editor, AlignAction.AlignmentType.EAST, labels)); disposables.add(d); btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); btn.putClientProperty("hideActionText", Boolean.TRUE); @@ -83,14 +83,14 @@ protected JComponent createDisclosedComponent(int state) { p.add(btn, gbc); gbc.gridy = 1; gbc.insets = new Insets(3, 0, 0, 0); - btn = new JButton(d = new AlignAction.North(editor, labels)); + btn = new JButton(d = new AlignAction(editor, AlignAction.AlignmentType.NORTH, labels)); disposables.add(d); btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); btn.putClientProperty("hideActionText", Boolean.TRUE); btn.setText(null); p.add(btn, gbc); gbc.insets = new Insets(3, 3, 0, 0); - btn = new JButton(d = new AlignAction.South(editor, labels)); + btn = new JButton(d = new AlignAction(editor, AlignAction.AlignmentType.SOUTH, labels)); disposables.add(d); btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); btn.putClientProperty("hideActionText", Boolean.TRUE); @@ -99,7 +99,7 @@ protected JComponent createDisclosedComponent(int state) { gbc.gridx = 0; gbc.gridy = 2; gbc.insets = new Insets(3, 0, 0, 0); - btn = new JButton(d = new AlignAction.Horizontal(editor, labels)); + btn = new JButton(d = new AlignAction(editor, AlignAction.AlignmentType.HORIZONTAL, labels)); disposables.add(d); btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); btn.putClientProperty("hideActionText", Boolean.TRUE); @@ -107,7 +107,7 @@ protected JComponent createDisclosedComponent(int state) { p.add(btn, gbc); gbc.gridx = 1; gbc.insets = new Insets(3, 3, 0, 0); - btn = new JButton(d = new AlignAction.Vertical(editor, labels)); + btn = new JButton(d = new AlignAction(editor, AlignAction.AlignmentType.VERTICAL, labels)); disposables.add(d); btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); btn.putClientProperty("hideActionText", Boolean.TRUE); diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/AlignFigureToNorthTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/AlignFigureToNorthTest.java new file mode 100644 index 000000000..f3cae44e0 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/AlignFigureToNorthTest.java @@ -0,0 +1,15 @@ +package org.jhotdraw.samples.svg.BDDAlign; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.junit.Test; + +public class AlignFigureToNorthTest extends ScenarioTest { + + @Test + public void figures_are_aligned_to_north_when_align_north_is_clicked() { + given().a_drawing_with_two_figures(); + when().align_north_button_is_clicked(); + then().line_has_moved_north(); + } +} + diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/GivenFigure.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/GivenFigure.java new file mode 100644 index 000000000..a53acf1ac --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/GivenFigure.java @@ -0,0 +1,52 @@ +package org.jhotdraw.samples.svg.BDDAlign; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ProvidedScenarioState; +import org.assertj.swing.edt.FailOnThreadViolationRepaintManager; +import org.assertj.swing.edt.GuiActionRunner; +import org.assertj.swing.fixture.FrameFixture; +import org.jhotdraw.draw.figure.Figure; +import org.jhotdraw.draw.figure.LineFigure; +import org.junit.BeforeClass; + +import java.awt.geom.Point2D; +import java.util.Arrays; +import java.util.List; + +public class GivenFigure extends Stage { + public static Point2D.Double initialStart; + public static Point2D.Double initialEnd; + + @BeforeClass + public static void setUpOnce() { + FailOnThreadViolationRepaintManager.install(); + } + + @ProvidedScenarioState + private FrameFixture window; + + + public GivenFigure a_drawing_with_two_figures() { + TestApplication frame = GuiActionRunner.execute(TestApplication::new); + window = new FrameFixture(frame); + window.show(); // shows the frame to test + // Create figures here + List
figures = createFigures(); + LineFigure lf = new LineFigure(); + initialStart = new Point2D.Double(30, 20); + initialEnd = new Point2D.Double(80, 30); + lf.setBounds(initialStart, initialEnd); + + frame.setFiguresInView(figures); + + return self(); + } + + private List
createFigures() { + LineFigure lf = new LineFigure(); + lf.setBounds(new Point2D.Double(30, 20), new Point2D.Double(80, 30)); + LineFigure lf2 = new LineFigure(); + lf2.setBounds(new Point2D.Double(20, 40), new Point2D.Double(110, 10)); + return Arrays.asList(lf, lf2); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/TestApplication.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/TestApplication.java new file mode 100644 index 000000000..0bfe93be9 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/TestApplication.java @@ -0,0 +1,106 @@ +package org.jhotdraw.samples.svg.BDDAlign; + +import org.jhotdraw.draw.*; +import org.jhotdraw.draw.figure.Figure; +import org.jhotdraw.draw.figure.LineFigure; +import org.jhotdraw.draw.io.ImageOutputFormat; +import org.jhotdraw.draw.io.SerializationInputOutputFormat; +import org.jhotdraw.draw.tool.CreationTool; +import org.jhotdraw.draw.tool.DnDTracker; +import org.jhotdraw.draw.tool.SelectionTool; +import org.jhotdraw.gui.action.ButtonFactory; +import org.jhotdraw.util.ResourceBundleUtil; +import javax.swing.*; +import java.awt.*; +import java.util.List; + +public class TestApplication extends JFrame { + private static final long serialVersionUID = 1L; + private JToolBar toolBar; + private JPanel innerPane; + private DrawingView drawingView; + + public TestApplication() { + initializeComponents(); + configureFrame(); + } + + private void initializeComponents() { + drawingView = createDrawingView(); + DrawingEditor editor = createDrawingEditor(drawingView); + toolBar = createToolBar(editor); + innerPane = createInnerPane(drawingView); + } + + private DrawingView createDrawingView() { + DrawingView view = new DefaultDrawingView(); + view.setDrawing(createDrawing()); + return view; + } + + private DrawingEditor createDrawingEditor(DrawingView view) { + DrawingEditor editor = new DefaultDrawingEditor(); + editor.add(view); + return editor; + } + + private JToolBar createToolBar(DrawingEditor editor) { + ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels"); + JToolBar tb = new JToolBar(); + addToolsToToolBar(tb, editor, labels); + tb.setOrientation(JToolBar.VERTICAL); + return tb; + } + + private void addToolsToToolBar(JToolBar tb, DrawingEditor editor, ResourceBundleUtil labels) { + SelectionTool selectionTool = new SelectionTool(); + selectionTool.setDragTracker(new DnDTracker()); + ButtonFactory.addSelectionToolTo(tb, editor, selectionTool); + ButtonFactory.addToolTo(tb, editor, new CreationTool(new LineFigure()), "edit.createLine", labels); + ButtonFactory.addAlignmentButtonsTo(tb, editor); + } + + private JPanel createInnerPane(DrawingView view) { + JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 20)); + JScrollPane scrollPane = new JScrollPane(view.getComponent()); + scrollPane.setPreferredSize(new Dimension(200, 200)); + panel.add(scrollPane); + return panel; + } + + private void configureFrame() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + getContentPane().add(new JScrollPane(innerPane), BorderLayout.CENTER); + getContentPane().add(toolBar, BorderLayout.WEST); + setSize(600, 400); + } + + private Drawing createDrawing() { + DefaultDrawing drawing = new DefaultDrawing(); + drawing.addInputFormat(new SerializationInputOutputFormat()); + drawing.addOutputFormat(new SerializationInputOutputFormat()); + drawing.addOutputFormat(new ImageOutputFormat()); + return drawing; + } + + public void setFiguresInView(List
figures) { + Drawing drawing = getDrawing(); + if (drawing != null) { + for (Figure fig : figures) { + drawing.add(fig); + drawingView.addToSelection(fig); + } + } + } + + public Drawing getDrawing() { + return drawingView != null ? drawingView.getDrawing() : null; + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + TestApplication app = new TestApplication(); + app.setVisible(true); + }); + } +} \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/ThenAligned.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/ThenAligned.java new file mode 100644 index 000000000..16c224941 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/ThenAligned.java @@ -0,0 +1,26 @@ +package org.jhotdraw.samples.svg.BDDAlign; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.assertj.swing.assertions.Assertions; +import org.assertj.swing.fixture.FrameFixture; +import org.jhotdraw.draw.figure.LineFigure; + +import java.awt.geom.Point2D; + +public class ThenAligned extends Stage { + @ExpectedScenarioState + private FrameFixture window; + + public ThenAligned line_has_moved_north() { + TestApplication frame = (TestApplication) window.target(); + LineFigure lineFigure = (LineFigure) frame.getDrawing().getChildren().get(0); + + Point2D.Double newStart = lineFigure.getStartPoint(); + Point2D.Double newEnd = lineFigure.getEndPoint(); + + Assertions.assertThat(newStart).isNotEqualTo(GivenFigure.initialStart); + Assertions.assertThat(newEnd).isNotEqualTo(GivenFigure.initialEnd); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/WhenAligned.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/WhenAligned.java new file mode 100644 index 000000000..8dbe95ca4 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/BDDAlign/WhenAligned.java @@ -0,0 +1,16 @@ +package org.jhotdraw.samples.svg.BDDAlign; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.assertj.swing.fixture.FrameFixture; + +public class WhenAligned extends Stage { + + @ExpectedScenarioState + private FrameFixture window; + + public WhenAligned align_north_button_is_clicked() { + window.button("alignWestButton").click(); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/SVGCreateFromFileToolTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/SVGCreateFromFileToolTest.java new file mode 100644 index 000000000..d3235b713 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/SVGCreateFromFileToolTest.java @@ -0,0 +1,81 @@ +package org.jhotdraw.samples.svg; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.io.File; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +import org.jhotdraw.draw.AttributeKey; + +import org.jhotdraw.draw.DefaultDrawing; +import org.jhotdraw.draw.DefaultDrawingEditor; +import org.jhotdraw.draw.DefaultDrawingView; +import org.jhotdraw.draw.DrawingEditor; +import org.jhotdraw.draw.DrawingView; +import org.jhotdraw.draw.event.CompositeFigureEvent; +import org.jhotdraw.draw.event.CompositeFigureListener; +import org.jhotdraw.draw.figure.CompositeFigure; +import org.jhotdraw.draw.figure.Figure; +import org.jhotdraw.draw.figure.ImageFigure; +import org.jhotdraw.draw.figure.ImageHolderFigure; +import org.junit.Ignore; +import org.junit.Test; + +public class SVGCreateFromFileToolTest { + + + /** + * Method under test: {@link SVGCreateFromFileTool#SVGCreateFromFileTool(ImageHolderFigure, CompositeFigure)} + */ + @Test + public void testConstructor() { + ImageFigure imagePrototype = new ImageFigure(); + SVGCreateFromFileTool actualSvgCreateFromFileTool = new SVGCreateFromFileTool(imagePrototype, new DefaultDrawing()); + + ImageHolderFigure imageHolderFigure = actualSvgCreateFromFileTool.imagePrototype; + assertTrue(imageHolderFigure instanceof ImageFigure); + assertTrue(actualSvgCreateFromFileTool.groupPrototype instanceof DefaultDrawing); + assertSame(imageHolderFigure, actualSvgCreateFromFileTool.getPrototype()); + } + + /** + * Method under test: {@link SVGCreateFromFileTool#SVGCreateFromFileTool(ImageHolderFigure, CompositeFigure, Map)} + */ + @Test + public void testConstructor2() { + ImageFigure imagePrototype = new ImageFigure(); + DefaultDrawing groupPrototype = new DefaultDrawing(); + SVGCreateFromFileTool actualSvgCreateFromFileTool = new SVGCreateFromFileTool(imagePrototype, groupPrototype, + new HashMap<>()); + + ImageHolderFigure imageHolderFigure = actualSvgCreateFromFileTool.imagePrototype; + assertTrue(imageHolderFigure instanceof ImageFigure); + assertTrue(actualSvgCreateFromFileTool.groupPrototype instanceof DefaultDrawing); + assertSame(imageHolderFigure, actualSvgCreateFromFileTool.getPrototype()); + assertTrue(actualSvgCreateFromFileTool.isToolDoneAfterCreation()); + } + + /** + * Method under test: {@link SVGCreateFromFileTool#activate(DrawingEditor)} + */ + @Test + public void testActivate() { + ImageFigure imagePrototype = new ImageFigure(); + SVGCreateFromFileTool svgCreateFromFileTool = new SVGCreateFromFileTool(imagePrototype, new DefaultDrawing()); + svgCreateFromFileTool.activate(new DefaultDrawingEditor()); + assertTrue(svgCreateFromFileTool.isActive()); + } +} + diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/EditRectangle.feature b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/EditRectangle.feature new file mode 100644 index 000000000..78d452225 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/EditRectangle.feature @@ -0,0 +1,8 @@ +Feature: Edit A Rectangle + Scenario: User resize the rectangle + Given The user has an existing rectangle + When When the user selects one of the rectangle's corners + When The mouse is moved on x or y positions + Then The rectangle should be resized + + diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/GivenAnExistingRectangle.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/GivenAnExistingRectangle.java new file mode 100644 index 000000000..68edbfd3b --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/GivenAnExistingRectangle.java @@ -0,0 +1,15 @@ +package org.jhotdraw.samples.svg.figures.BDD; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.samples.svg.figures.SVGRectFigure; + +public class GivenAnExistingRectangle extends Stage { + @ExpectedScenarioState + private SVGRectFigure svgRectFigure; + + public GivenAnExistingRectangle givenAnExistingRectangle(){ + svgRectFigure = new SVGRectFigure(0, 0, 50, 25); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/ThenResize.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/ThenResize.java new file mode 100644 index 000000000..0a2783e1a --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/ThenResize.java @@ -0,0 +1,27 @@ +package org.jhotdraw.samples.svg.figures.BDD; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.draw.handle.Handle; +import org.jhotdraw.samples.svg.figures.SVGRectFigure; + +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; + +public class ThenResize extends Stage { + @ExpectedScenarioState + private SVGRectFigure svgRectFigure; + private Collection handles; + public ThenResize thenResize(){ + Rectangle2D.Double initialBounds = svgRectFigure.getBounds(); + svgRectFigure.setBounds(svgRectFigure.getStartPoint(), new Point2D.Double(initialBounds.width + 10, initialBounds.height + 10)); + Rectangle2D.Double actualBounds = svgRectFigure.getBounds(); + + assertEquals("Rectangle is resized", initialBounds.width + 10, actualBounds.width, 0.01); + assertEquals("Rectangle is resized", initialBounds.height + 10, actualBounds.height, 0.01); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/WhenUserSelectsRectangleCorner.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/WhenUserSelectsRectangleCorner.java new file mode 100644 index 000000000..272eab8c6 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/BDD/WhenUserSelectsRectangleCorner.java @@ -0,0 +1,25 @@ +package org.jhotdraw.samples.svg.figures.BDD; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.samples.svg.figures.SVGRectFigure; + +import java.awt.geom.Point2D; + +public class WhenUserSelectsRectangleCorner extends Stage { + @ExpectedScenarioState + private SVGRectFigure svgRectFigure; + + public WhenUserSelectsRectangleCorner whenUserSelectsRectangleCorner() { + svgRectFigure.setBounds(svgRectFigure.getStartPoint(), svgRectFigure.getEndPoint()); + return self(); + } + + public WhenUserSelectsRectangleCorner whenMouseIsMoved() { + Point2D.Double newPoint = new Point2D.Double(svgRectFigure.getX() + 10, svgRectFigure.getY() + 10); + svgRectFigure.setBounds(svgRectFigure.getStartPoint(), newPoint); + return self(); + } + + +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/GivenAnExistingRectangleTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/GivenAnExistingRectangleTest.java new file mode 100644 index 000000000..5ffd92223 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/GivenAnExistingRectangleTest.java @@ -0,0 +1,19 @@ +package org.jhotdraw.samples.svg.figures; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.jhotdraw.samples.svg.figures.BDD.GivenAnExistingRectangle; +import org.jhotdraw.samples.svg.figures.BDD.ThenResize; +import org.jhotdraw.samples.svg.figures.BDD.WhenUserSelectsRectangleCorner; +import org.junit.Test; + + +public class GivenAnExistingRectangleTest extends ScenarioTest { + @Test + public void testScenario(){ + given().givenAnExistingRectangle(); + when().whenUserSelectsRectangleCorner(); + when().whenMouseIsMoved(); + then().thenResize(); + } + +} \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/SVGRectFigureTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/SVGRectFigureTest.java new file mode 100644 index 000000000..5bf73f56b --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/figures/SVGRectFigureTest.java @@ -0,0 +1,53 @@ +package org.jhotdraw.samples.svg.figures; + + +import org.jhotdraw.draw.handle.Handle; +import org.junit.Before; +import org.junit.Test; +import java.util.Collection; +import java.util.Collections; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + + + +public class SVGRectFigureTest { + private SVGRectFigure svgRectFigure; + + @Before + public void setup(){ + svgRectFigure = new SVGRectFigure(0, 0, 50, 25); + } + @Test + public void testCreateHandlesZeroDetailLevel() { + Collection handles = svgRectFigure.createHandles(0); + assertNotNull("not null", handles); + } + @Test + public void testCreateHandlesOneDetailLevel() { + Collection handles = svgRectFigure.createHandles(1); + assertNotNull("not null", handles); + } + @Test + public void testMocking() { + SVGRectFigure rectFigure = mock(SVGRectFigure.class); + HandleCreator mockMouseHoverCreator = mock(MouseHoverHandleCreator.class); + when(mockMouseHoverCreator.createHandles(rectFigure)).thenReturn(Collections.singletonList(mock(Handle.class))); + + HandleCreator mockOneDetailLevelCreator = mock(OneDetailLevelHandleCreator.class); + when(mockOneDetailLevelCreator.createHandles(rectFigure)).thenReturn(Collections.singletonList(mock(Handle.class))); + + HandleCreator mockZeroDetailLevelCreator = mock(ZeroDetailLevelHandleCreator.class); + when(mockZeroDetailLevelCreator.createHandles(rectFigure)).thenReturn(Collections.singletonList(mock(Handle.class))); + + Collection handlesForMouseHoverDetailLevel = mockMouseHoverCreator.createHandles(rectFigure); + Collection handlesForZeroDetailLevel = mockZeroDetailLevelCreator.createHandles(rectFigure); + Collection handlesForOneDetailLevel = mockOneDetailLevelCreator.createHandles(rectFigure); + + assertEquals(1, handlesForMouseHoverDetailLevel.size()); + assertEquals(1, handlesForZeroDetailLevel.size()); + assertEquals(1, handlesForOneDetailLevel.size()); + } + +} \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/UndoAndRedoTestSuite.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/UndoAndRedoTestSuite.java new file mode 100644 index 000000000..b80d60517 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/UndoAndRedoTestSuite.java @@ -0,0 +1,26 @@ +package org.jhotdraw.samples.svg.undoredo.BDD; + +import org.jhotdraw.samples.svg.undoredo.BDD.constructor.redo.GivenAnRedoActionTest; +import org.jhotdraw.samples.svg.undoredo.BDD.constructor.undo.GivenAnUndoActionTest; +import org.jhotdraw.samples.svg.undoredo.BDD.getid.redo.GetIDRedoTest; +import org.jhotdraw.samples.svg.undoredo.BDD.getid.undo.GetIDUndoTest; +import org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.redo.RedoStateTest; +import org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.undo.UndoStateTest; +import org.jhotdraw.samples.svg.undoredo.BDD.updateview.redo.UpdateRedoUpdateViewTest; +import org.jhotdraw.samples.svg.undoredo.BDD.updateview.undo.UpdateUndoUpdateViewTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + GivenAnRedoActionTest.class, + GivenAnUndoActionTest.class, + RedoStateTest.class, + UndoStateTest.class, + UpdateRedoUpdateViewTest.class, + UpdateUndoUpdateViewTest.class, + GetIDRedoTest.class, + GetIDUndoTest.class +}) +public class UndoAndRedoTestSuite { +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/GivenAnRedoAction.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/GivenAnRedoAction.java new file mode 100644 index 000000000..28b720830 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/GivenAnRedoAction.java @@ -0,0 +1,15 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.constructor.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; + +public class GivenAnRedoAction extends Stage { + + @ExpectedScenarioState + private RedoAction redoAction; + + public GivenAnRedoAction givenAnAction() { + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/GivenAnRedoActionTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/GivenAnRedoActionTest.java new file mode 100644 index 000000000..7082f9813 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/GivenAnRedoActionTest.java @@ -0,0 +1,13 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.constructor.redo; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.junit.Test; + +public class GivenAnRedoActionTest extends ScenarioTest { + @Test + public void testScenario(){ + given().givenAnAction(); + when().whenInitialized(); + then().thenExists(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/ThenExists.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/ThenExists.java new file mode 100644 index 000000000..b64c745bb --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/ThenExists.java @@ -0,0 +1,22 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.constructor.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; + +import static org.junit.Assert.*; + +public class ThenExists extends Stage { + @ExpectedScenarioState + private RedoAction redoAction; + + public ThenExists thenExists() { + // Not null: + assertNotNull("The redoAction is null.", redoAction); + + // Check ID: + assertEquals("The id is '" + redoAction.ID + "', not 'edit.redo'.", "edit.redo", "" + redoAction.ID); + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/WhenInitialized.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/WhenInitialized.java new file mode 100644 index 000000000..3c94687e6 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/redo/WhenInitialized.java @@ -0,0 +1,23 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.constructor.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; +import org.jhotdraw.api.app.Application; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +public class WhenInitialized extends Stage { + @ExpectedScenarioState + private RedoAction redoAction; + private Application app; + private View view; + + public WhenInitialized whenInitialized() { + // Initialize the undo action + app = Mockito.mock(Application.class); + view = Mockito.mock(View.class); + redoAction = new RedoAction(app, view); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/GivenAnUndoAction.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/GivenAnUndoAction.java new file mode 100644 index 000000000..976599258 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/GivenAnUndoAction.java @@ -0,0 +1,15 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.constructor.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; + +public class GivenAnUndoAction extends Stage { + + @ExpectedScenarioState + private UndoAction undoAction; + + public GivenAnUndoAction givenAnAction() { + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/GivenAnUndoActionTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/GivenAnUndoActionTest.java new file mode 100644 index 000000000..6f0bca14b --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/GivenAnUndoActionTest.java @@ -0,0 +1,15 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.constructor.undo; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.jhotdraw.samples.svg.undoredo.BDD.constructor.redo.ThenExists; +import org.jhotdraw.samples.svg.undoredo.BDD.constructor.redo.WhenInitialized; +import org.junit.Test; + +public class GivenAnUndoActionTest extends ScenarioTest { + @Test + public void testScenario(){ + given().givenAnAction(); + when().whenInitialized(); + then().thenExists(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/ThenExists.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/ThenExists.java new file mode 100644 index 000000000..185e6c540 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/ThenExists.java @@ -0,0 +1,22 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.constructor.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; + +import static org.junit.Assert.*; + +public class ThenExists extends Stage { + @ExpectedScenarioState + private UndoAction undoAction; + + public ThenExists thenExists() { + // Not null: + assertNotNull("The undoAction is null.", undoAction); + + // Check ID: + assertEquals("The id is '" + undoAction.ID + "', not 'edit.undo'.", "edit.undo", "" + undoAction.ID); + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/WhenInitialized.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/WhenInitialized.java new file mode 100644 index 000000000..2aba09a34 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/constructor/undo/WhenInitialized.java @@ -0,0 +1,23 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.constructor.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; +import org.jhotdraw.api.app.Application; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +public class WhenInitialized extends Stage { + @ExpectedScenarioState + private UndoAction undoAction; + private Application app; + private View view; + + public WhenInitialized whenInitialized() { + // Initialize the undo action + app = Mockito.mock(Application.class); + view = Mockito.mock(View.class); + undoAction = new UndoAction(app, view); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/GetIDRedoTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/GetIDRedoTest.java new file mode 100644 index 000000000..8a4357d24 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/GetIDRedoTest.java @@ -0,0 +1,13 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.getid.redo; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.junit.Test; + +public class GetIDRedoTest extends ScenarioTest { + @Test + public void testScenario1() { + given().aRedoAction(); + when().whenGetIDIsCalled(); + then().thenTheInsertedIDIsReturned(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/GivenARedoAction.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/GivenARedoAction.java new file mode 100644 index 000000000..49d9d87fa --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/GivenARedoAction.java @@ -0,0 +1,26 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.getid.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; +import org.jhotdraw.api.app.Application; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +public class GivenARedoAction extends Stage { + + @ExpectedScenarioState + private RedoAction redoAction; + private Application app; + private View view; + + + public GivenARedoAction aRedoAction() { + app = Mockito.mock(Application.class); + view = Mockito.mock(View.class); + + redoAction = new RedoAction(app, view); + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/ThenTheInsertedIDIsReturned.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/ThenTheInsertedIDIsReturned.java new file mode 100644 index 000000000..5d8f21c4c --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/ThenTheInsertedIDIsReturned.java @@ -0,0 +1,35 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.getid.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class ThenTheInsertedIDIsReturned extends Stage { + @ExpectedScenarioState + private RedoAction redoAction; + + public ThenTheInsertedIDIsReturned thenTheInsertedIDIsReturned() { + try { + Method method = RedoAction.class.getDeclaredMethod("getID"); + method.setAccessible(true); + method.invoke(redoAction, "edit.undo"); + + assertEquals("Edit.redo", method.invoke(redoAction, "edit.redo") + ""); + assertNotEquals("Edit.redo", method.invoke(redoAction, "edit.undo") + ""); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/WhenGetIDIsCalled.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/WhenGetIDIsCalled.java new file mode 100644 index 000000000..0c4baf441 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/redo/WhenGetIDIsCalled.java @@ -0,0 +1,28 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.getid.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class WhenGetIDIsCalled extends Stage { + @ExpectedScenarioState + private RedoAction redoAction; + public WhenGetIDIsCalled whenGetIDIsCalled() { + try { + Method method = RedoAction.class.getDeclaredMethod("getID"); + method.setAccessible(true); + method.invoke(redoAction, "edit.redo"); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/GetIDUndoTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/GetIDUndoTest.java new file mode 100644 index 000000000..490789b56 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/GetIDUndoTest.java @@ -0,0 +1,13 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.getid.undo; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.junit.Test; + +public class GetIDUndoTest extends ScenarioTest { + @Test + public void testScenario1() { + given().aUndoAction(); + when().whenGetIDIsCalled(); + then().thenTheInsertedIDIsReturned(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/GivenARedoAction.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/GivenARedoAction.java new file mode 100644 index 000000000..089a1f3f9 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/GivenARedoAction.java @@ -0,0 +1,26 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.getid.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; +import org.jhotdraw.api.app.Application; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +public class GivenARedoAction extends Stage { + + @ExpectedScenarioState + private UndoAction undoAction; + private Application app; + private View view; + + + public GivenARedoAction aUndoAction() { + app = Mockito.mock(Application.class); + view = Mockito.mock(View.class); + + undoAction = new UndoAction(app, view); + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/ThenTheInsertedIDIsReturned.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/ThenTheInsertedIDIsReturned.java new file mode 100644 index 000000000..a5654cf17 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/ThenTheInsertedIDIsReturned.java @@ -0,0 +1,36 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.getid.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; +import org.jhotdraw.action.edit.UndoAction; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class ThenTheInsertedIDIsReturned extends Stage { + @ExpectedScenarioState + private UndoAction undoAction; + + public ThenTheInsertedIDIsReturned thenTheInsertedIDIsReturned() { + try { + Method method = RedoAction.class.getDeclaredMethod("getID"); + method.setAccessible(true); + method.invoke(undoAction, "edit.undo"); + + assertEquals("Edit.undo", method.invoke(undoAction, "edit.undo") + ""); + assertNotEquals("Edit.undo", method.invoke(undoAction, "edit.redo") + ""); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/WhenGetIDIsCalled.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/WhenGetIDIsCalled.java new file mode 100644 index 000000000..3818a3625 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/getid/undo/WhenGetIDIsCalled.java @@ -0,0 +1,28 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.getid.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class WhenGetIDIsCalled extends Stage { + @ExpectedScenarioState + private UndoAction undoAction; + public WhenGetIDIsCalled whenGetIDIsCalled() { + try { + Method method = UndoAction.class.getDeclaredMethod("getID"); + method.setAccessible(true); + method.invoke(undoAction, "edit.undo"); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/GivenRedoState.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/GivenRedoState.java new file mode 100644 index 000000000..31b898476 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/GivenRedoState.java @@ -0,0 +1,29 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; +import org.jhotdraw.action.edit.UndoAction; +import org.jhotdraw.api.app.Application; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +public class GivenRedoState extends Stage { + + @ExpectedScenarioState + private RedoAction redoAction; + + private Application app; + private View view; + + public GivenRedoState anRedoAction() { + app = Mockito.mock(Application.class); + view = Mockito.mock(View.class); + + redoAction = new RedoAction(app, view); + redoAction.getActiveView().getActionMap().put(redoAction.ID, redoAction); + redoAction.getActiveView().getActionMap().put("edit.undo", new UndoAction(app, view)); + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/RedoStateTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/RedoStateTest.java new file mode 100644 index 000000000..a77684cf5 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/RedoStateTest.java @@ -0,0 +1,27 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.redo; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.junit.Test; + +public class RedoStateTest extends ScenarioTest { + @Test + public void testScenario1() { + given().anRedoAction(); + when().actionIsNull(); + then().isFalse(); + } + + @Test + public void testScenario2() { + given().anRedoAction(); + when().actionIsThis(); + then().isFalse(); + } + + @Test + public void testScenario3() { + given().anRedoAction(); + when().actionIsOther(); + then().isTrue(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/ThenEnabledState.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/ThenEnabledState.java new file mode 100644 index 000000000..d10ec95ee --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/ThenEnabledState.java @@ -0,0 +1,22 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; + +import static org.junit.Assert.*; + +public class ThenEnabledState extends Stage { + @ExpectedScenarioState + private RedoAction redoAction; + + public ThenEnabledState isTrue() { + assertTrue("Enabled state is '" + redoAction.isEnabled() + "'." , redoAction.isEnabled()); + return self(); + } + + public ThenEnabledState isFalse() { + assertFalse("Enabled state is '" + redoAction.isEnabled() + "'.", redoAction.isEnabled()); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/WhenStateUpdated.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/WhenStateUpdated.java new file mode 100644 index 000000000..b72eb3d54 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/redo/WhenStateUpdated.java @@ -0,0 +1,77 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; + +import javax.swing.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class WhenStateUpdated extends Stage { + @ExpectedScenarioState + private RedoAction redoAction; + @ExpectedScenarioState + private Action realAction; + + public WhenStateUpdated actionIsNull() { + try { + + Method method2 = RedoAction.class.getDeclaredMethod("updateView"); + method2.setAccessible(true); + method2.invoke(redoAction, redoAction.getActiveView(), null); + + Method method1 = RedoAction.class.getDeclaredMethod("updateEnabledState"); + method1.setAccessible(true); + method1.invoke(redoAction); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + + return self(); + } + + public WhenStateUpdated actionIsThis() { + try { + Method method2 = RedoAction.class.getDeclaredMethod("updateView"); + method2.setAccessible(true); + method2.invoke(redoAction, redoAction.getActiveView(), redoAction.getActiveView()); + + Method method = RedoAction.class.getDeclaredMethod("updateEnabledState"); + method.setAccessible(true); + method.invoke(redoAction); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + + return self(); + } + + public WhenStateUpdated actionIsOther() { + try { + Method method2 = RedoAction.class.getDeclaredMethod("updateView"); + method2.setAccessible(true); + method2.invoke(redoAction, redoAction.getActiveView(), redoAction.getActiveView().getActionMap().get("edit.undo")); + + Method method = RedoAction.class.getDeclaredMethod("updateEnabledState"); + method.setAccessible(true); + method.invoke(redoAction); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/GivenUndoState.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/GivenUndoState.java new file mode 100644 index 000000000..ceea5f909 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/GivenUndoState.java @@ -0,0 +1,28 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; +import org.jhotdraw.api.app.Application; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +public class GivenUndoState extends Stage { + + @ExpectedScenarioState + private UndoAction undoAction; + + private Application app; + private View view; + + public GivenUndoState anUndoAction() { + app = Mockito.mock(Application.class); + view = Mockito.mock(View.class); + + undoAction = new UndoAction(app, view); + undoAction.getActiveView().getActionMap().put(undoAction.ID, undoAction); + undoAction.getActiveView().getActionMap().put("edit.undo", new UndoAction(app, view)); + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/ThenEnabledState.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/ThenEnabledState.java new file mode 100644 index 000000000..03fd6cfa3 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/ThenEnabledState.java @@ -0,0 +1,23 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ThenEnabledState extends Stage { + @ExpectedScenarioState + private UndoAction undoAction; + + public ThenEnabledState isTrue() { + assertTrue("Enabled state is '" + undoAction.isEnabled() + "'." , undoAction.isEnabled()); + return self(); + } + + public ThenEnabledState isFalse() { + assertFalse("Enabled state is '" + undoAction.isEnabled() + "'.", undoAction.isEnabled()); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/UndoStateTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/UndoStateTest.java new file mode 100644 index 000000000..117ff6722 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/UndoStateTest.java @@ -0,0 +1,27 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.undo; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.junit.Test; + +public class UndoStateTest extends ScenarioTest { + @Test + public void testScenario1() { + given().anUndoAction(); + when().actionIsNull(); + then().isFalse(); + } + + @Test + public void testScenario2() { + given().anUndoAction(); + when().actionIsThis(); + then().isFalse(); + } + + @Test + public void testScenario3() { + given().anUndoAction(); + when().actionIsOther(); + then().isTrue(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/WhenStateUpdated.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/WhenStateUpdated.java new file mode 100644 index 000000000..25eadeee5 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateenabledstate/undo/WhenStateUpdated.java @@ -0,0 +1,77 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateenabledstate.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; + +import javax.swing.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class WhenStateUpdated extends Stage { + @ExpectedScenarioState + private UndoAction undoAction; + @ExpectedScenarioState + private Action realAction; + + public WhenStateUpdated actionIsNull() { + try { + + Method method2 = UndoAction.class.getDeclaredMethod("updateView"); + method2.setAccessible(true); + method2.invoke(undoAction, undoAction.getActiveView(), null); + + Method method1 = UndoAction.class.getDeclaredMethod("updateEnabledState"); + method1.setAccessible(true); + method1.invoke(undoAction); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + + return self(); + } + + public WhenStateUpdated actionIsThis() { + try { + Method method2 = UndoAction.class.getDeclaredMethod("updateView"); + method2.setAccessible(true); + method2.invoke(undoAction, undoAction.getActiveView(), undoAction.getActiveView()); + + Method method = UndoAction.class.getDeclaredMethod("updateEnabledState"); + method.setAccessible(true); + method.invoke(undoAction); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + + return self(); + } + + public WhenStateUpdated actionIsOther() { + try { + Method method2 = UndoAction.class.getDeclaredMethod("updateView"); + method2.setAccessible(true); + method2.invoke(undoAction, undoAction.getActiveView(), undoAction.getActiveView().getActionMap().get("edit.undo")); + + Method method = UndoAction.class.getDeclaredMethod("updateEnabledState"); + method.setAccessible(true); + method.invoke(undoAction); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/GivenAView.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/GivenAView.java new file mode 100644 index 000000000..5de88bd4f --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/GivenAView.java @@ -0,0 +1,29 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateview.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; +import org.jhotdraw.api.app.Application; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +public class GivenAView extends Stage { + + @ExpectedScenarioState + private RedoAction redoAction; + @ExpectedScenarioState + private View viewOld; + @ExpectedScenarioState + private View viewNew; + private Application app; + + + public GivenAView aView() { + app = Mockito.mock(Application.class); + viewOld = Mockito.mock(View.class); + + redoAction = new RedoAction(app, viewOld); + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/ThenViewIsUpdated.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/ThenViewIsUpdated.java new file mode 100644 index 000000000..4a6bb95a5 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/ThenViewIsUpdated.java @@ -0,0 +1,29 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateview.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; +import org.jhotdraw.api.app.View; + +import static org.junit.Assert.*; + +public class ThenViewIsUpdated extends Stage { + @ExpectedScenarioState + private RedoAction redoAction; + @ExpectedScenarioState + private View viewOld; + @ExpectedScenarioState + private View viewNew; + + public ThenViewIsUpdated isNew() { + assertEquals(redoAction.getActiveView(), viewNew); + assertNotEquals(redoAction.getActiveView(), viewOld); + return self(); + } + + public ThenViewIsUpdated isOld() { + assertEquals(redoAction.getActiveView(), viewOld); + assertNotEquals(redoAction.getActiveView(), viewNew); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/UpdateRedoUpdateViewTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/UpdateRedoUpdateViewTest.java new file mode 100644 index 000000000..4a2998ae4 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/UpdateRedoUpdateViewTest.java @@ -0,0 +1,20 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateview.redo; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.junit.Test; + +public class UpdateRedoUpdateViewTest extends ScenarioTest { + @Test + public void testScenario1() { + given().aView(); + when().whenViewIsUpdatedNew(); + then().isNew(); + } + + @Test + public void testScenario2() { + given().aView(); + when().whenViewIsUpdatedOld(); + then().isOld(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/WhenViewIsUpdated.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/WhenViewIsUpdated.java new file mode 100644 index 000000000..ff886362d --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/redo/WhenViewIsUpdated.java @@ -0,0 +1,55 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateview.redo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class WhenViewIsUpdated extends Stage { + @ExpectedScenarioState + private RedoAction redoAction; + @ExpectedScenarioState + private View viewOld; + @ExpectedScenarioState + private View viewNew; + + public WhenViewIsUpdated whenViewIsUpdatedNew() { + try { + Method method = RedoAction.class.getDeclaredMethod("updateView"); + method.setAccessible(true); + + viewNew = Mockito.mock(View.class); + method.invoke(redoAction, viewOld, viewNew); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + return self(); + } + + public WhenViewIsUpdated whenViewIsUpdatedOld() { + try { + Method method = RedoAction.class.getDeclaredMethod("updateView"); + method.setAccessible(true); + + viewNew = Mockito.mock(View.class); + method.invoke(redoAction, viewOld, viewOld); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/GivenAView.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/GivenAView.java new file mode 100644 index 000000000..eb1ceb576 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/GivenAView.java @@ -0,0 +1,29 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateview.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; +import org.jhotdraw.api.app.Application; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +public class GivenAView extends Stage { + + @ExpectedScenarioState + private UndoAction undoAction; + @ExpectedScenarioState + private View viewOld; + @ExpectedScenarioState + private View viewNew; + private Application app; + + + public GivenAView aView() { + app = Mockito.mock(Application.class); + viewOld = Mockito.mock(View.class); + + undoAction = new UndoAction(app, viewOld); + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/ThenViewIsUpdated.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/ThenViewIsUpdated.java new file mode 100644 index 000000000..efb47bc9c --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/ThenViewIsUpdated.java @@ -0,0 +1,30 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateview.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.UndoAction; +import org.jhotdraw.api.app.View; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class ThenViewIsUpdated extends Stage { + @ExpectedScenarioState + private UndoAction undoAction; + @ExpectedScenarioState + private View viewOld; + @ExpectedScenarioState + private View viewNew; + + public ThenViewIsUpdated isNew() { + assertEquals(undoAction.getActiveView(), viewNew); + assertNotEquals(undoAction.getActiveView(), viewOld); + return self(); + } + + public ThenViewIsUpdated isOld() { + assertEquals(undoAction.getActiveView(), viewOld); + assertNotEquals(undoAction.getActiveView(), viewNew); + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/UpdateUndoUpdateViewTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/UpdateUndoUpdateViewTest.java new file mode 100644 index 000000000..b3f06618b --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/UpdateUndoUpdateViewTest.java @@ -0,0 +1,20 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateview.undo; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.junit.Test; + +public class UpdateUndoUpdateViewTest extends ScenarioTest { + @Test + public void testScenario1() { + given().aView(); + when().whenViewIsUpdatedNew(); + then().isNew(); + } + + @Test + public void testScenario2() { + given().aView(); + when().whenViewIsUpdatedOld(); + then().isOld(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/WhenViewIsUpdated.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/WhenViewIsUpdated.java new file mode 100644 index 000000000..e2a2714a7 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/undoredo/BDD/updateview/undo/WhenViewIsUpdated.java @@ -0,0 +1,56 @@ +package org.jhotdraw.samples.svg.undoredo.BDD.updateview.undo; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.action.edit.RedoAction; +import org.jhotdraw.action.edit.UndoAction; +import org.jhotdraw.api.app.View; +import org.mockito.Mockito; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class WhenViewIsUpdated extends Stage { + @ExpectedScenarioState + private UndoAction undoAction; + @ExpectedScenarioState + private View viewOld; + @ExpectedScenarioState + private View viewNew; + + public WhenViewIsUpdated whenViewIsUpdatedNew() { + try { + Method method = RedoAction.class.getDeclaredMethod("updateView"); + method.setAccessible(true); + + viewNew = Mockito.mock(View.class); + method.invoke(undoAction, viewOld, viewNew); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + return self(); + } + + public WhenViewIsUpdated whenViewIsUpdatedOld() { + try { + Method method = RedoAction.class.getDeclaredMethod("updateView"); + method.setAccessible(true); + + viewNew = Mockito.mock(View.class); + method.invoke(undoAction, viewOld, viewOld); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + return self(); + } +} diff --git a/jhotdraw-samples/jhotdraw-samples.iml b/jhotdraw-samples/jhotdraw-samples.iml new file mode 100644 index 000000000..38e271629 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-utils/jhotdraw-utils.iml b/jhotdraw-utils/jhotdraw-utils.iml new file mode 100644 index 000000000..a9fd139a6 --- /dev/null +++ b/jhotdraw-utils/jhotdraw-utils.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw-utils/pom.xml b/jhotdraw-utils/pom.xml index b1b2faf01..37d5545cf 100644 --- a/jhotdraw-utils/pom.xml +++ b/jhotdraw-utils/pom.xml @@ -1,6 +1,7 @@ 4.0.0 + org.jhotdraw jhotdraw @@ -15,5 +16,10 @@ 6.8.21 test + + dk.sdu.cbse + featuretracerlite + 1.1-SNAPSHOT + \ No newline at end of file diff --git a/jhotdraw-utils/src/main/java/org/jhotdraw/undo/UndoRedoManager.java b/jhotdraw-utils/src/main/java/org/jhotdraw/undo/UndoRedoManager.java index 40ac0b4ce..f7b997d6e 100644 --- a/jhotdraw-utils/src/main/java/org/jhotdraw/undo/UndoRedoManager.java +++ b/jhotdraw-utils/src/main/java/org/jhotdraw/undo/UndoRedoManager.java @@ -69,7 +69,6 @@ private class UndoAction extends AbstractAction { private static final long serialVersionUID = 1L; - public UndoAction() { labels.configureAction(this, "edit.undo"); setEnabled(false); @@ -96,7 +95,6 @@ private class RedoAction extends AbstractAction { private static final long serialVersionUID = 1L; - public RedoAction() { labels.configureAction(this, "edit.redo"); setEnabled(false); diff --git a/jhotdraw-utils/src/main/java/org/jhotdraw/util/Images.java b/jhotdraw-utils/src/main/java/org/jhotdraw/util/Images.java index 08b66a084..b99accda0 100644 --- a/jhotdraw-utils/src/main/java/org/jhotdraw/util/Images.java +++ b/jhotdraw-utils/src/main/java/org/jhotdraw/util/Images.java @@ -11,6 +11,7 @@ import java.awt.image.*; import java.net.*; import javax.swing.*; +import dk.sdu.mmmi.featuretracer.lib.FeatureEntryPoint; /** * Image processing methods. diff --git a/jhotdraw-xml/jhotdraw-xml.iml b/jhotdraw-xml/jhotdraw-xml.iml new file mode 100644 index 000000000..fcef7aa89 --- /dev/null +++ b/jhotdraw-xml/jhotdraw-xml.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/jhotdraw.iml b/jhotdraw.iml new file mode 100644 index 000000000..c730eb3de --- /dev/null +++ b/jhotdraw.iml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index bf1c45f13..b8c8f48ae 100644 --- a/pom.xml +++ b/pom.xml @@ -1,135 +1,135 @@ - 4.0.0 - org.jhotdraw - jhotdraw - 9.1-SNAPSHOT - pom - - JHotDraw - - - - github - GitHub external Packages - https://maven.pkg.github.com/sweat-tek/MavenRepository - - - - - GNU Library or Lesser General Public License (LGPL) V2.1 - http://www.gnu.org/licenses/lgpl-2.1.html - - - Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/ - - - - - Tobias Warneke - t.warneke@gmx.net - - - - UTF-8 - 1.8 - 1.8 - - This is a fork of jhotdraw from http://sourceforge.net/projects/jhotdraw - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.1.1 - - false - false - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 - - ${maven.compiler.source} - ${maven.compiler.target} - ${project.build.sourceEncoding} - - - - - org.apache.maven.plugins - maven-checkstyle-plugin - 3.1.0 - - - verify-style - process-classes - - check - - - - - true - true - ${project.build.sourceDirectory} - - - - - - - - - - - - - - - - + 4.0.0 + org.jhotdraw + jhotdraw + 9.1-SNAPSHOT + pom + + JHotDraw + + + + github + GitHub external Packages + https://maven.pkg.github.com/sweat-tek/MavenRepository + + + + + GNU Library or Lesser General Public License (LGPL) V2.1 + http://www.gnu.org/licenses/lgpl-2.1.html + + + Creative Commons Attribution 2.5 License + http://creativecommons.org/licenses/by/2.5/ + + + + + Tobias Warneke + t.warneke@gmx.net + + + + UTF-8 + 1.8 + 1.8 + + This is a fork of jhotdraw from http://sourceforge.net/projects/jhotdraw + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.1.1 + + false + false + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${maven.compiler.source} + ${maven.compiler.target} + ${project.build.sourceEncoding} + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.0 + + + verify-style + process-classes + + check + + + + + true + true + ${project.build.sourceDirectory} + + + + + + + + + + + + + + + + - + - - - - + + + + - - - - - - - - com.puppycrawl.tools - checkstyle - 8.29 - - - - - - - jhotdraw-core - jhotdraw-samples - jhotdraw-xml - jhotdraw-api - jhotdraw-utils - jhotdraw-gui - jhotdraw-app - jhotdraw-datatransfer - jhotdraw-actions - + + + + + + + + com.puppycrawl.tools + checkstyle + 8.29 + + + + + + + jhotdraw-core + jhotdraw-samples + jhotdraw-xml + jhotdraw-api + jhotdraw-utils + jhotdraw-gui + jhotdraw-app + jhotdraw-datatransfer + jhotdraw-actions + \ No newline at end of file