From df43668a23e0e80a1a36735e03de877a17936ac6 Mon Sep 17 00:00:00 2001 From: Frank Gasdorf Date: Tue, 8 Dec 2020 10:07:00 +0100 Subject: [PATCH] [SITMAP-229] SetScaleCommand as NavCommand Change-Id: Ib3a2170d79322aef532753ec8e1bd50292f6c1f6 Signed-off-by: Frank Gasdorf --- .../commands/SetScaleCommandTest.java | 61 +++++++++++++++++++ .../internal/commands/SetScaleCommand.java | 49 +++++++++++---- 2 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 plugins/org.locationtech.udig.project.tests/src/org/locationtech/udig/project/internal/commands/SetScaleCommandTest.java diff --git a/plugins/org.locationtech.udig.project.tests/src/org/locationtech/udig/project/internal/commands/SetScaleCommandTest.java b/plugins/org.locationtech.udig.project.tests/src/org/locationtech/udig/project/internal/commands/SetScaleCommandTest.java new file mode 100644 index 0000000000..4712254baf --- /dev/null +++ b/plugins/org.locationtech.udig.project.tests/src/org/locationtech/udig/project/internal/commands/SetScaleCommandTest.java @@ -0,0 +1,61 @@ +package org.locationtech.udig.project.internal.commands; + +import static org.junit.Assert.*; + +import org.easymock.EasyMock; +import org.eclipse.core.runtime.IProgressMonitor; +import org.junit.Test; +import org.locationtech.udig.project.internal.render.ViewportModel; + +public class SetScaleCommandTest { + + private static final double SCALE_TO_SET = 1 / 5000; + + private static final double PREVIOUS_SCALE = 1 / 500; + + @Test + public void testCopy() throws Exception { + SetScaleCommand setScaleCommand = new SetScaleCommand(SCALE_TO_SET); + assertNotSame(setScaleCommand, setScaleCommand.copy()); + } + + @Test + public void ifModelIsSetSetScaleIsCalled() throws Exception { + SetScaleCommand setScaleCommand = new SetScaleCommand(SCALE_TO_SET); + ViewportModel mockedViewportModel = EasyMock.createMock(ViewportModel.class); + IProgressMonitor monitor = EasyMock.createNiceMock(IProgressMonitor.class); + EasyMock.expect(mockedViewportModel.getScaleDenominator()).andReturn(PREVIOUS_SCALE).once(); + mockedViewportModel.setScale(SCALE_TO_SET); + EasyMock.expectLastCall().times(1); + + EasyMock.replay(mockedViewportModel); + setScaleCommand.setViewportModel(mockedViewportModel); + setScaleCommand.run(monitor); + EasyMock.verify(mockedViewportModel); + } + + @Test + public void rollbackSetsPreviousScaleFromModel() throws Exception { + SetScaleCommand setScaleCommand = new SetScaleCommand(SCALE_TO_SET); + ViewportModel mockedViewportModel = EasyMock.createMock(ViewportModel.class); + IProgressMonitor monitor = EasyMock.createNiceMock(IProgressMonitor.class); + // get scale from model on first execution + EasyMock.expect(mockedViewportModel.getScaleDenominator()).andReturn(PREVIOUS_SCALE).once(); + mockedViewportModel.setScale(SCALE_TO_SET); + EasyMock.expectLastCall().times(1); + // on roll-back, reset previous scale denominator + mockedViewportModel.setScale(PREVIOUS_SCALE); + EasyMock.expectLastCall().times(1); + + EasyMock.replay(mockedViewportModel); + setScaleCommand.setViewportModel(mockedViewportModel); + setScaleCommand.run(monitor); + setScaleCommand.rollback(monitor); + EasyMock.verify(mockedViewportModel); + } + + @Test + public void commandNameNotNull() throws Exception { + assertNotNull("Command name expected", new SetScaleCommand(SCALE_TO_SET).getName()); + } +} diff --git a/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/internal/commands/SetScaleCommand.java b/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/internal/commands/SetScaleCommand.java index 138760e739..b313a8b7ad 100644 --- a/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/internal/commands/SetScaleCommand.java +++ b/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/internal/commands/SetScaleCommand.java @@ -9,41 +9,66 @@ */ package org.locationtech.udig.project.internal.commands; -import org.locationtech.udig.project.command.AbstractCommand; +import org.eclipse.core.runtime.IProgressMonitor; +import org.locationtech.udig.project.command.Command; import org.locationtech.udig.project.command.UndoableCommand; import org.locationtech.udig.project.internal.Messages; - -import org.eclipse.core.runtime.IProgressMonitor; +import org.locationtech.udig.project.internal.command.navigation.AbstractNavCommand; +import org.locationtech.udig.project.internal.render.ViewportModel; /** * Sets the scale denominator of the map. - * + * * @author Jesse * @since 1.1.0 */ -public class SetScaleCommand extends AbstractCommand implements UndoableCommand { +public class SetScaleCommand extends AbstractNavCommand implements UndoableCommand { private double oldScale; + private double newScale; + /** - * + * * @param newScale Scale Denominator */ - public SetScaleCommand( double newScale ) { + public SetScaleCommand(double newScale) { this.newScale = newScale; } - public void rollback( IProgressMonitor monitor ) throws Exception { - getMap().getViewportModelInternal().setScale(oldScale); + @Override + public void rollback(IProgressMonitor monitor) throws Exception { + if (model != null) { + model.setScale(oldScale); + } else { + getMap().getViewportModelInternal().setScale(oldScale); + } } + @Override public String getName() { return Messages.SetScaleCommand_name; } - public void run( IProgressMonitor monitor ) throws Exception { - this.oldScale=getMap().getViewportModel().getScaleDenominator(); - getMap().getViewportModelInternal().setScale(newScale); + @Override + public void run(IProgressMonitor monitor) throws Exception { + if (model != null) { + oldScale = model.getScaleDenominator(); + model.setScale(newScale); + } else { + ViewportModel viewportModel = getMap().getViewportModelInternal(); + this.oldScale = viewportModel.getScaleDenominator(); + viewportModel.setScale(newScale); + } + } + + @Override + public Command copy() { + return new SetScaleCommand(newScale); } + @Override + protected void runImpl(IProgressMonitor monitor) throws Exception { + run(monitor); + } }