use of com.sldeditor.common.undo.UndoEvent in project sldeditor by robward-scisys.
the class UndoManagerTest method testUndoMechanism.
/**
* Test method for adding undo events and calling undo and redo.
*/
@Test
public void testUndoMechanism() {
UndoManager.destroyInstance();
DummyUndo listener = new DummyUndo();
DummyUndoParent parentListener = new DummyUndoParent();
UndoManager instance = UndoManager.getInstance();
instance.addListener(listener);
// CHECKSTYLE:OFF
UndoEvent event1 = new UndoEvent(parentListener, FieldIdEnum.ANCHOR_POINT_H, Integer.valueOf(2), Integer.valueOf(3));
UndoEvent event2 = new UndoEvent(parentListener, FieldIdEnum.ANCHOR_POINT_H, Integer.valueOf(2), Integer.valueOf(3));
UndoEvent event3 = new UndoEvent(parentListener, FieldIdEnum.DISPLACEMENT_Y, Integer.valueOf(20), Integer.valueOf(900));
UndoEvent event4 = new UndoEvent(parentListener, FieldIdEnum.DEFAULT_STYLE, Boolean.TRUE, Boolean.FALSE);
UndoEvent event5 = new UndoEvent(parentListener, FieldIdEnum.HALO_RADIUS, Double.valueOf(44.3), Double.valueOf(67.5));
// CHECKSTYLE:ON
instance.addUndoEvent(event1);
assertTrue(listener.undoAllowed);
assertFalse(listener.redoAllowed);
instance.addUndoEvent(event2);
assertTrue(listener.undoAllowed);
assertFalse(listener.redoAllowed);
instance.addUndoEvent(event3);
assertTrue(listener.undoAllowed);
assertFalse(listener.redoAllowed);
instance.addUndoEvent(event4);
assertTrue(listener.undoAllowed);
assertFalse(listener.redoAllowed);
instance.addUndoEvent(event5);
assertTrue(listener.undoAllowed);
assertFalse(listener.redoAllowed);
// Test going beyond end of last item
assertNull(parentListener.redoAction);
assertNull(parentListener.undoAction);
instance.redo();
assertNull(parentListener.redoAction);
assertNull(parentListener.undoAction);
instance.undo();
assertNull(parentListener.redoAction);
assertEquals(event5, parentListener.undoAction);
assertTrue(listener.undoAllowed);
assertTrue(listener.redoAllowed);
instance.redo();
assertNull(parentListener.undoAction);
assertEquals(event5, parentListener.redoAction);
instance.undo();
instance.undo();
instance.undo();
instance.undo();
assertNull(parentListener.redoAction);
assertEquals(event2, parentListener.undoAction);
instance.redo();
assertNull(parentListener.undoAction);
assertEquals(event2, parentListener.redoAction);
// Test going beyond start of list
instance.undo();
assertEquals(true, listener.redoAllowed);
assertEquals(true, listener.undoAllowed);
instance.undo();
assertEquals(true, listener.redoAllowed);
assertEquals(false, listener.undoAllowed);
instance.undo();
assertEquals(true, listener.redoAllowed);
assertEquals(false, listener.undoAllowed);
assertEquals(event1, parentListener.undoAction);
assertNull(parentListener.redoAction);
instance.redo();
assertNull(parentListener.undoAction);
assertEquals(event1, parentListener.redoAction);
// Add new event when the undo pointer is not at the end
UndoEvent event10 = new UndoEvent(parentListener, FieldIdEnum.ALIGN, Integer.valueOf(19), Integer.valueOf(20));
// Expecting the undo events from the pointer onwards to be deleted and replaced with new
// event
parentListener.undoAction = null;
parentListener.redoAction = null;
instance.addUndoEvent(event10);
instance.redo();
// Should be at the end
assertNull(parentListener.undoAction);
assertNull(parentListener.redoAction);
instance.undo();
assertNull(parentListener.redoAction);
assertEquals(event10, parentListener.undoAction);
}
use of com.sldeditor.common.undo.UndoEvent in project sldeditor by robward-scisys.
the class UndoManagerTest method testFileLoaded.
@Test
public void testFileLoaded() {
UndoManager.destroyInstance();
DummyUndo listener = new DummyUndo();
DummyUndoParent parentListener = new DummyUndoParent();
UndoManager.getInstance().addListener(listener);
// CHECKSTYLE:OFF
UndoEvent event1 = new UndoEvent(parentListener, FieldIdEnum.ANCHOR_POINT_H, Integer.valueOf(2), Integer.valueOf(3));
UndoEvent event2 = new UndoEvent(parentListener, FieldIdEnum.ANCHOR_POINT_H, Integer.valueOf(2), Integer.valueOf(3));
UndoEvent event3 = new UndoEvent(parentListener, FieldIdEnum.DISPLACEMENT_Y, Integer.valueOf(20), Integer.valueOf(900));
UndoEvent event4 = new UndoEvent(parentListener, FieldIdEnum.DEFAULT_STYLE, Boolean.TRUE, Boolean.FALSE);
UndoEvent event5 = new UndoEvent(parentListener, FieldIdEnum.HALO_RADIUS, Double.valueOf(44.3), Double.valueOf(67.5));
// CHECKSTYLE:ON
UndoManager.getInstance().addUndoEvent(event1);
assertTrue(listener.undoAllowed);
assertFalse(listener.redoAllowed);
UndoManager.getInstance().addUndoEvent(event2);
UndoManager.getInstance().addUndoEvent(event3);
UndoManager.getInstance().addUndoEvent(event4);
UndoManager.getInstance().addUndoEvent(event5);
UndoManager.getInstance().undo();
UndoManager.getInstance().undo();
// This clears out all the undo events
UndoManager.getInstance().fileLoaded();
assertFalse(listener.undoAllowed);
assertFalse(listener.redoAllowed);
}
use of com.sldeditor.common.undo.UndoEvent in project sldeditor by robward-scisys.
the class SLDTreeTools method addNewImageOutlinePolygon.
/**
* Adds the new image outline polygon symbolizer.
*/
public void addNewImageOutlinePolygon() {
if (symbolTree == null) {
return;
}
// Store current state of the SLD before the add
Object oldValueObj = sldWriter.encodeSLD(null, SelectedSymbol.getInstance().getSld());
PolygonSymbolizer newPolygonSymbolizer = DefaultSymbols.createDefaultPolygonSymbolizer();
DefaultMutableTreeNode rasterNode = getRasterTreeNode();
SelectedSymbol.getInstance().addImageOutlineSymbolizerToRaster(newPolygonSymbolizer);
DefaultMutableTreeNode newNode = sldTree.addObject(rasterNode, newPolygonSymbolizer, true);
if (newNode != null) {
sldTree.addObject(newNode, SLDTreeLeafFactory.getInstance().getFill(newPolygonSymbolizer), true);
sldTree.addObject(newNode, SLDTreeLeafFactory.getInstance().getStroke(newPolygonSymbolizer), true);
SLDTreeManager.getInstance().rebuildTree((SLDTree) sldTree);
// Select the item just added
TreePath newPath = getPath(newNode);
symbolTree.setSelectionPath(newPath);
// Store current state of the SLD after the add
Object newValueObj = sldWriter.encodeSLD(null, SelectedSymbol.getInstance().getSld());
UndoManager.getInstance().addUndoEvent(new UndoEvent(sldTree.getUndoObject(), getClass().getName(), oldValueObj, newValueObj));
}
}
use of com.sldeditor.common.undo.UndoEvent in project sldeditor by robward-scisys.
the class SLDTreeTools method moveItem.
/**
* Move item within a list. The direction parameter determines which way the item is moved.
*
* @param moveUp the move up flags (true), or down (false)
*/
public void moveItem(boolean moveUp) {
if (symbolTree == null) {
return;
}
if (treeModel == null) {
return;
}
if (sldTree == null) {
return;
}
TreePath path = symbolTree.getSelectionPath();
if (path == null) {
return;
}
DefaultMutableTreeNode lastNode = (DefaultMutableTreeNode) path.getLastPathComponent();
if (lastNode == null) {
return;
}
Object obj = lastNode.getUserObject();
if (obj == null) {
return;
}
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) lastNode.getParent();
if (parentNode == null) {
return;
}
Object parentObj = parentNode.getUserObject();
if (parentObj == null) {
return;
}
// Calculate index offset value based on direction
int direction = moveUp ? -1 : 1;
// Store current state of the SLD before the move
// CHECKSTYLE:OFF
Object oldValueObj = sldWriter.encodeSLD(null, SelectedSymbol.getInstance().getSld());
if (obj instanceof StyledLayer) {
StyledLayerDescriptor sld = (StyledLayerDescriptor) parentObj;
// NamedLayerImpl.equals() doesn't work in the way I
// want it to, so indexOf() does not work
boolean found = false;
int index = 0;
for (StyledLayer styledLayer : sld.layers()) {
if (styledLayer == obj) {
found = true;
break;
} else {
index++;
}
}
if (found && ((index + direction) >= 0) && (index + direction) < sld.layers().size()) {
StyledLayer styledLayer = sld.layers().remove(index);
sld.layers().add(index + direction, styledLayer);
treeModel.removeNodeFromParent(lastNode);
treeModel.insertNodeInto(lastNode, parentNode, index + direction);
} else {
return;
}
} else if (obj instanceof Style) {
if (parentObj instanceof NamedLayerImpl) {
NamedLayerImpl namedLayer = (NamedLayerImpl) parentObj;
int index = namedLayer.styles().indexOf(obj);
if (((index + direction) >= 0) && (index + direction) < namedLayer.styles().size()) {
Style style = namedLayer.styles().remove(index);
namedLayer.styles().add(index + direction, style);
treeModel.removeNodeFromParent(lastNode);
treeModel.insertNodeInto(lastNode, parentNode, index + direction);
} else {
return;
}
}
} else if (obj instanceof FeatureTypeStyle) {
Style style = (Style) parentObj;
int index = style.featureTypeStyles().indexOf(obj);
if (((index + direction) >= 0) && (index + direction) < style.featureTypeStyles().size()) {
FeatureTypeStyle fts = style.featureTypeStyles().remove(index);
style.featureTypeStyles().add(index + direction, fts);
treeModel.removeNodeFromParent(lastNode);
treeModel.insertNodeInto(lastNode, parentNode, index + direction);
} else {
return;
}
} else if (obj instanceof Rule) {
FeatureTypeStyle fts = (FeatureTypeStyle) parentObj;
int index = fts.rules().indexOf(obj);
if (((index + direction) >= 0) && (index + direction) < fts.rules().size()) {
Rule rule = fts.rules().remove(index);
fts.rules().add(index + direction, rule);
treeModel.removeNodeFromParent(lastNode);
treeModel.insertNodeInto(lastNode, parentNode, index + direction);
} else {
return;
}
} else if (obj instanceof Symbolizer) {
Rule rule = (Rule) parentObj;
int index = rule.symbolizers().indexOf(obj);
if (((index + direction) >= 0) && (index + direction) < rule.symbolizers().size()) {
Symbolizer symbolizer = rule.symbolizers().remove(index);
rule.symbolizers().add(index + direction, symbolizer);
treeModel.removeNodeFromParent(lastNode);
treeModel.insertNodeInto(lastNode, parentNode, index + direction);
} else {
return;
}
}
// Refresh the tree structure. Not very efficient but gets result wanted.
// The node has been moved in the tree above. Now going to refresh model.
treeModel.nodeStructureChanged(lastNode);
// Get path for item moved
TreePath newNodePath = getPath(lastNode);
int[] selectedRows = new int[1];
selectedRows[0] = symbolTree.getRowForPath(newNodePath);
// Find the row of item moved
// Now clear tree structure and re-populate, inefficient but it means
// that all items are expanded as required.
SLDTreeManager.getInstance().rebuildTree((SLDTree) sldTree);
// Make item moved selected again
symbolTree.setSelectionRows(selectedRows);
// Re-render the symbol
if (renderList != null) {
for (RenderSymbolInterface render : renderList) {
render.renderSymbol();
}
}
// Store current state of the SLD after the move
Object newValueObj = sldWriter.encodeSLD(null, SelectedSymbol.getInstance().getSld());
UndoManager.getInstance().addUndoEvent(new UndoEvent(sldTree.getUndoObject(), getClass().getName(), oldValueObj, newValueObj));
}
use of com.sldeditor.common.undo.UndoEvent in project sldeditor by robward-scisys.
the class ColourRampPanel method createTopPanel.
/**
* Creates the top panel.
*/
private void createTopPanel() {
final UndoActionInterface undoObj = this;
JPanel topPanel = new JPanel();
topPanel.setPreferredSize(new Dimension(BasePanel.FIELD_PANEL_WIDTH, BasePanel.WIDGET_HEIGHT));
topPanel.setLayout(null);
panel.add(topPanel, BorderLayout.NORTH);
List<ValueComboBoxData> dataList = populateColourRamps(false);
rampComboBox = new ValueComboBox();
rampComboBox.initialiseSingle(dataList);
rampComboBox.setBounds(BasePanel.WIDGET_X_START, 0, BasePanel.WIDGET_EXTENDED_WIDTH, BasePanel.WIDGET_HEIGHT);
topPanel.add(rampComboBox);
rampComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!isPopulating()) {
Integer newValueObj = rampComboBox.getSelectedIndex();
UndoManager.getInstance().addUndoEvent(new UndoEvent(undoObj, FieldIdEnum.COLOUR_RAMP_COLOUR, oldColourRampIndex, newValueObj));
oldColourRampIndex = newValueObj;
}
}
});
reverseCheckbox = new JCheckBox(Localisation.getString(ColourRampConfigPanel.class, "ColourRampPanel.reverse"));
reverseCheckbox.setBounds(rampComboBox.getX() + rampComboBox.getWidth() + 20, 0, BasePanel.WIDGET_STANDARD_WIDTH, BasePanel.WIDGET_HEIGHT);
reverseCheckbox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isSelected = reverseCheckbox.isSelected();
Boolean oldValueObj = Boolean.valueOf(!isSelected);
Boolean newValueObj = Boolean.valueOf(isSelected);
UndoManager.getInstance().addUndoEvent(new UndoEvent(undoObj, FieldIdEnum.COLOUR_RAMP_REVERSE, oldValueObj, newValueObj));
reverseColourRamp(isSelected);
}
});
topPanel.add(reverseCheckbox);
}
Aggregations