Skip to content

Commit

Permalink
Update CursorTool api to use idiomatic java8
Browse files Browse the repository at this point in the history
  • Loading branch information
mukoki committed Aug 22, 2021
1 parent 9289712 commit 45a764c
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 231 deletions.
6 changes: 3 additions & 3 deletions src/com/vividsolutions/jump/util/CollectionUtil.java
Expand Up @@ -290,9 +290,9 @@ public static <T> Collection<T> collect(Collection<T> collection, Block block) {
/**
* The Smalltalk #select method.
*/
public static Collection select(Collection collection, Block block) {
List<Object> result = new ArrayList<>();
for (Object item : collection) {
public static <T> Collection<T> select(Collection<T> collection, Block block) {
List<T> result = new ArrayList<>();
for (T item : collection) {
if (Boolean.TRUE.equals(block.yield(item))) {
result.add(item);
}
Expand Down
18 changes: 5 additions & 13 deletions src/com/vividsolutions/jump/workbench/ui/LayerViewPanel.java
Expand Up @@ -53,14 +53,7 @@
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;

import javax.swing.JPanel;
import javax.swing.JPopupMenu;
Expand Down Expand Up @@ -326,17 +319,16 @@ public Map visibleLayerToFeaturesInFenceMap() {
/**
* The Fence layer will be included.
*/
public Map visibleLayerToFeaturesInFenceMap(Geometry fence) {
Map map = new HashMap();
public Map<Layer, Set<Feature>> visibleLayerToFeaturesInFenceMap(Geometry fence) {
Map<Layer, Set<Feature>> map = new HashMap<>();

for (Iterator i = getLayerManager().iterator(Layer.class); i.hasNext();) {
Layer layer = (Layer) i.next();
for (Layer layer : getLayerManager().getLayerables(Layer.class)) {

if (!layer.isVisible()) {
continue;
}

HashSet features = new HashSet();
Set<Feature> features = new HashSet<>();

for (Iterator j = layer.getFeatureCollectionWrapper().query(
fence.getEnvelopeInternal()).iterator(); j.hasNext();) {
Expand Down
Expand Up @@ -4,8 +4,8 @@
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
Expand All @@ -15,15 +15,15 @@
import com.vividsolutions.jump.feature.Feature;
import com.vividsolutions.jump.util.Block;
import com.vividsolutions.jump.util.CollectionUtil;
import com.vividsolutions.jump.util.StringUtil;
import com.vividsolutions.jump.workbench.WorkbenchContext;
import com.vividsolutions.jump.workbench.model.Layer;
import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;

public abstract class AbstractClickSelectedLineStringsTool extends
SpecifyFeaturesTool {

public AbstractClickSelectedLineStringsTool(WorkbenchContext context) {
super(context);
super(context);
setViewClickBuffer(10);
}

Expand Down Expand Up @@ -59,7 +59,7 @@ protected void gestureFinished() throws Exception {
if (!check(checkFactory().createAtLeastNItemsMustBeSelectedCheck(1))) {
return;
}
Collection nearbyLineStringFeatures = CollectionUtil.select(
Collection<Feature> nearbyLineStringFeatures = CollectionUtil.select(
CollectionUtil.concatenate(layerToSpecifiedFeaturesMap()
.values()), new Block() {
public Object yield(Object feature) {
Expand All @@ -82,15 +82,12 @@ private EnableCheckFactory checkFactory() {
return EnableCheckFactory.getInstance(getWorkbench().getContext());
}

protected abstract void gestureFinished(Collection nearbyLineStringFeatures)
protected abstract void gestureFinished(Collection<Feature> nearbyLineStringFeatures)
throws NoninvertibleTransformException;

protected Layer layer(Feature feature, Map layerToSpecifiedFeaturesMap) {
for (Iterator i = layerToSpecifiedFeaturesMap.keySet().iterator(); i
.hasNext();) {
Layer layer = (Layer) i.next();
Collection features = (Collection) layerToSpecifiedFeaturesMap
.get(layer);
protected Layer layer(Feature feature, Map<Layer, Set<Feature>> layerToSpecifiedFeaturesMap) {
for (Layer layer : layerToSpecifiedFeaturesMap.keySet()) {
Set<Feature> features = layerToSpecifiedFeaturesMap.get(layer);
if (features.contains(feature)) {
return layer;
}
Expand Down
Expand Up @@ -43,15 +43,13 @@
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
Expand Down Expand Up @@ -98,7 +96,6 @@
public abstract class AbstractCursorTool implements CursorTool {

private boolean snappingInitialized = false;

private boolean snappingAllowed = false;
private boolean controlPressed = false;
private boolean shiftPressed = false;
Expand All @@ -117,12 +114,12 @@ public abstract class AbstractCursorTool implements CursorTool {
protected boolean isLinuxOS = System.getProperty("os.name").toLowerCase()
.startsWith("linux");

private LayerViewPanelListener layerViewPanelListener = new LayerViewPanelListener() {
private final LayerViewPanelListener layerViewPanelListener = new LayerViewPanelListener() {

public void cursorPositionChanged(String x, String y) {
// show scale view when cursos moves on view //
// show scale view when cursor moves on view //
// [Giuseppe Aruta 2012-feb-18] //
// [Micha�l Michaud 2013-03-13] move to workbenchFrame.changeZoom()
// [Michaël Michaud 2013-03-13] move to workbenchFrame.changeZoom()
// getWorkbench().getFrame().setScaleText("1:" + (int)
// Math.floor(ScreenScale.getHorizontalMapScale(panel.getViewport())));
}
Expand Down Expand Up @@ -158,11 +155,11 @@ public void painted(Graphics graphics) {

private boolean shapeOnScreen = false;

private SnapManager snapManager = new SnapManager();
private final SnapManager snapManager = new SnapManager();

private Stroke stroke = new BasicStroke(1);

private ArrayList listeners = new ArrayList();
private final List<Listener> listeners = new ArrayList<>();

private Cursor cursor;

Expand Down Expand Up @@ -202,17 +199,14 @@ protected void setShiftPressed(boolean onoff) {
}

protected boolean wasShiftPressed() {
// System.out.println("act shift pressed");
return shiftPressed;
}

protected void setControlPressed(boolean onoff) {
// System.out.println("set ctrl "+onoff+" -> "+this);
controlPressed = onoff;
}

protected boolean wasControlPressed() {
// System.out.println("get ctrl "+controlPressed+" -> "+this);
return controlPressed;
}

Expand All @@ -222,8 +216,7 @@ protected boolean wasControlPressed() {
* @return a Cursor
*/
public static Cursor createCursor(Image image) {
// <<TODO>> Compute image center rather than hardcoding 16, 16. [Jon
// Aquino]
// TODO Compute image center rather than hardcoding 16, 16. [Jon Aquino]
return createCursor(image, new Point(16, 16));
}

Expand Down Expand Up @@ -287,7 +280,6 @@ public void activate(LayerViewPanel new_panel) {
}

// following added to handle KEY shortcuts e.g. SPACEBAR snap switching
//WorkbenchFrame frame = this.panel.getWorkBenchFrame();
context.getWorkbench().getFrame().addEasyKeyListener(keyListener);
}

Expand All @@ -299,11 +291,12 @@ public void activate(LayerViewPanel new_panel) {
// return (window instanceof WorkbenchFrame) ? (WorkbenchFrame) window : null;
// }

protected List createStandardSnappingPolicies(Blackboard blackboard) {
return Arrays
.asList(new SnapPolicy[] { new SnapToVerticesPolicy(blackboard),
new SnapToFeaturesPolicy(blackboard),
new SnapToGridPolicy(blackboard) });
protected List<SnapPolicy> createStandardSnappingPolicies(Blackboard blackboard) {
return Arrays.asList(
new SnapToVerticesPolicy(blackboard),
new SnapToFeaturesPolicy(blackboard),
new SnapToGridPolicy(blackboard)
);
}

protected boolean isRollingBackInvalidEdits() {
Expand Down Expand Up @@ -352,12 +345,16 @@ protected void setFilling(boolean filling) {
this.filling = filling;
}

/**
* @deprecated Use #setStroke instead.
* @param strokeWidth stroke width of this cursor tool
*/
protected void setStrokeWidth(int strokeWidth) {
setStroke(new BasicStroke(strokeWidth));
///**
// * @deprecated Use #setStroke instead.
// * @param strokeWidth stroke width of this cursor tool
// */
//protected void setStrokeWidth(int strokeWidth) {
// setStroke(new BasicStroke(strokeWidth));
//}

protected Stroke getStroke() {
return stroke;
}

protected void setStroke(Stroke stroke) {
Expand Down Expand Up @@ -458,9 +455,8 @@ protected void drawShapeXOR(Graphics2D g) throws Exception {
* position and the image is remembered for a later clear.
*
* @param g the graphics context
* @throws Exception if an Exception occurs during drawing
*/
protected void drawImageXOR(Graphics2D g) throws Exception {
protected void drawImageXOR(Graphics2D g) {
Image newImage = getImage();
Point newPosition = getImagePosition();
drawImageXOR(newImage, newPosition, g);
Expand All @@ -474,8 +470,7 @@ protected void drawShapeXOR(Shape shape, Graphics2D graphics) {
try {
// Pan tool returns a null shape. [Jon Aquino]
if (shape != null) {
// Can't both draw and fill, because we're using XOR. [Jon
// Aquino]
// Can't both draw and fill, because we're using XOR. [Jon Aquino]
if (filling) {
graphics.fill(shape);
} else {
Expand Down Expand Up @@ -576,12 +571,9 @@ private void redrawShape(Graphics2D graphics) throws Exception {
/**
* Redraws the image on screen. This means the clearing the old image and draw
* the actual image.
*
* @param graphics
* the Graphics2D
* @throws Exception
* @param graphics the Graphics2D
*/
private void redrawImage(Graphics2D graphics) throws Exception {
private void redrawImage(Graphics2D graphics) {
clearImage(graphics);
drawImageXOR(graphics);

Expand Down Expand Up @@ -626,8 +618,7 @@ protected void fireGestureFinished() throws Exception {
getPanel().getLayerManager().getUndoableEditReceiver().stopReceiving();
}

for (Iterator i = listeners.iterator(); i.hasNext();) {
Listener listener = (Listener) i.next();
for (Listener listener : listeners) {
listener.gestureFinished();
}
}
Expand Down Expand Up @@ -679,7 +670,7 @@ protected void setPanel(LayerViewPanel panel) {
public static String name(CursorTool tool) {
try {
String key = tool.getClass().getName();
Class c;
Class<?> c;
// use superclass name if tool was modified as anonymous inner class in
// any way
while (key.contains("$") && (c = tool.getClass().getSuperclass()) != null) {
Expand Down Expand Up @@ -721,7 +712,7 @@ public interface Listener {

// memorize modifier key states (Shift/Ctrl etc.)
// snap on/off via key listener
private KeyListener keyListener = new KeyListener() {
private final KeyListener keyListener = new KeyListener() {
boolean off = false;

public void keyTyped(KeyEvent e) {
Expand Down
Expand Up @@ -43,6 +43,7 @@


public interface CursorTool extends MouseListener, MouseMotionListener {

Cursor getCursor();

/**
Expand Down
Expand Up @@ -50,6 +50,7 @@
* (even to draw nothing).
*/
public abstract class DragTool extends AbstractCursorTool {

public static final int DEFAULT_VIEW_CLICK_BUFFER = 2;
private int viewClickBuffer = DEFAULT_VIEW_CLICK_BUFFER;
/** Modify using #setSource */
Expand Down
Expand Up @@ -39,20 +39,17 @@
import java.awt.geom.Point2D;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import com.vividsolutions.jump.feature.Feature;
import org.openjump.core.CheckOS;
import org.openjump.core.rasterimage.RasterImageLayer;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import com.vividsolutions.jump.workbench.JUMPWorkbench;
import com.vividsolutions.jump.workbench.Logger;
import com.vividsolutions.jump.workbench.WorkbenchContext;
import com.vividsolutions.jump.workbench.model.FenceLayerFinder;
Expand Down Expand Up @@ -89,21 +86,20 @@ protected void gestureFinished() throws Exception {
if (!wasShiftPressed()) {
infoFrame.getModel().clear();
}
Map map = layerToSpecifiedFeaturesMap();
Iterator i = map.keySet().iterator();
while(i.hasNext()){
Layer layer = (Layer) i.next();
Map<Layer, Set<Feature>> map = layerToSpecifiedFeaturesMap();
for (Layer layer : map.keySet()){
if (layer.getName().equals(FenceLayerFinder.LAYER_NAME)) {
continue;
}
Collection features = (Collection) map.get(layer);
Collection<Feature> features = map.get(layer);
infoFrame.getModel().add(layer, features);
}

Coordinate coord = getPanel().getViewport().toModelCoordinate(getViewSource());

// WMS
List<WMSLayer> wmsLay_l = getWorkbench().getContext().getLayerManager().getLayerables(WMSLayer.class);
List<WMSLayer> wmsLay_l = getWorkbench().getContext().getLayerManager()
.getLayerables(WMSLayer.class);

String response = "";
String newLine = System.getProperty("line.separator");
Expand Down Expand Up @@ -131,7 +127,7 @@ protected void gestureFinished() throws Exception {
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
wmsResponse = sw.toString();
JUMPWorkbench.getInstance().getFrame().log(sw.toString());
Logger.warn(sw.toString());
wmsResponse = wmsResponse.concat(newLine);

Logger.debug(ex);
Expand All @@ -140,8 +136,7 @@ protected void gestureFinished() throws Exception {
response = response.concat(wmsResponse);
response = response.concat(newLine);
}



infoFrame.setWmsInfo(response);

// Raster data
Expand Down

0 comments on commit 45a764c

Please sign in to comment.