Skip to content

Commit

Permalink
[SITMAP-229] SetScaleCommand as NavCommand
Browse files Browse the repository at this point in the history
Change-Id: Ib3a2170d79322aef532753ec8e1bd50292f6c1f6
Signed-off-by: Frank Gasdorf <[email protected]>
  • Loading branch information
fgdrf committed Dec 8, 2020
1 parent 54f2970 commit df43668
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit df43668

Please sign in to comment.