From bf11c220be14bab318134e9f61eb697a9a72005b Mon Sep 17 00:00:00 2001 From: tkellner Date: Wed, 10 Apr 2013 19:04:53 +0000 Subject: Allow dragging the signature git-svn-id: https://joinup.ec.europa.eu/svn/pdf-over/trunk@118 174cde9d-5d70-4d2a-aa98-46368bc2aaf7 --- .../gui/composites/PositioningComposite.java | 21 ++++++------ .../pdfover/gui/composites/SignaturePanel.java | 37 ++++++++++++++++++++-- 2 files changed, 46 insertions(+), 12 deletions(-) (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java index 716e1388..0470aeef 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java @@ -94,6 +94,17 @@ public class PositioningComposite extends StateComposite { showPage(this.numPages); } + /** + * Set the signature placeholder image + * Must be called _after_ displayDocument + * @param placeholder signature placeholder + */ + public void setPlaceholder(Image placeholder) { + if (this.viewer == null) + return; + this.viewer.setSignaturePlaceholder(placeholder); + } + /** * Create the composite. * @param parent @@ -216,14 +227,4 @@ public class PositioningComposite extends StateComposite { public SignaturePosition getPosition() { return this.position; } - - /** - * Set the signature placeholder image - * @param placeholder signature placeholder - */ - public void setPlaceholder(Image placeholder) { - if (this.viewer == null) - return; - this.viewer.setSignaturePlaceholder(placeholder); - } } diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java index 4c83e57f..6eb014d6 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java @@ -26,6 +26,8 @@ import java.awt.event.MouseEvent; import java.awt.geom.AffineTransform; import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.geom.Point2D.Double; import javax.swing.JPanel; @@ -258,6 +260,8 @@ public class SignaturePanel extends JPanel { private MouseAdapter mouseListener = new MouseAdapter() { private boolean doDrag = false; + private int dragXOffset = 0; + private int dragYOffset = 0; private void updateSigPos(int sigx, int sigy) { if (SignaturePanel.this.currentImage == null) @@ -276,8 +280,20 @@ public class SignaturePanel extends JPanel { public void mousePressed(MouseEvent evt) { if (evt.getButton() == MouseEvent.BUTTON1) { - updateSigPos(evt.getX(), evt.getY()); this.doDrag = true; + if (isOnSignature(evt.getX(), evt.getY())) { + this.dragXOffset = (int) + (SignaturePanel.this.sigScreenPos.getX() - + (evt.getX() - SignaturePanel.this.offx)); + this.dragYOffset = (int) + (SignaturePanel.this.sigScreenPos.getY() - + (evt.getY() - SignaturePanel.this.offy)); + } + else { + this.dragXOffset = 0; + this.dragYOffset = 0; + } + updateSigPos(evt.getX() + this.dragXOffset, evt.getY() + this.dragYOffset); } } @@ -293,10 +309,27 @@ public class SignaturePanel extends JPanel { @Override public void mouseDragged(MouseEvent evt) { if (this.doDrag) - updateSigPos(evt.getX(), evt.getY()); + updateSigPos(evt.getX() + this.dragXOffset, evt.getY() + this.dragYOffset); } }; + /** + * Check whether given point is on signature placeholder + * @param x x coordinate + * @param y y coordinate + * @return true if given point is on signature placeholder + */ + boolean isOnSignature(int x, int y) + { + Rectangle2D sig = new Rectangle2D.Double( + this.sigScreenPos.getX() + this.offx, + this.sigScreenPos.getY() + this.offy, + this.sigPlaceholder.getWidth(null), + this.sigPlaceholder.getHeight(null)); + Point2D pos = new Point2D.Double(x, y); + return (sig.contains(pos)); + } + /** * Clamp x to be within [min-max] * @param x int to clamp -- cgit v1.2.3