001: /**
002: *
003: */package org.openjump.core.ui.plugin.view;
004:
005: import java.awt.event.KeyEvent;
006: import java.awt.geom.NoninvertibleTransformException;
007:
008: import javax.swing.JInternalFrame;
009:
010: import com.vividsolutions.jts.geom.Envelope;
011: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
012: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
013: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
014: import com.vividsolutions.jump.workbench.ui.TaskFrame;
015: import com.vividsolutions.jump.workbench.ui.Viewport;
016:
017: /**
018: * Plug in for navigation with keyboards keys. <br/>
019: * Navigation is as follows:<br/>
020: * Arrows keys move the viewport.<br/>
021: * Page down and up are zoom in and zoom out, respectively<br/>
022: * Home key zooms in or out to full extent<br/>
023: * Pan and zoom percentage is, by default, 20%
024: *
025: * @author Ugo Taddei <taddei@lat-lon.de>
026: *
027: */
028: public class InstallKeyPanPlugIn extends AbstractPlugIn {
029:
030: private static final int NORTH = 0;
031: private static final int EAST = 1;
032: private static final int SOUTH = 2;
033: private static final int WEST = 3;
034:
035: private static final int ZOOM_IN = 4;
036: private static final int ZOOM_OUT = 5;
037:
038: /* matrix defining directions */
039: private static final int[][] DIRECTIONS = { { 0, -1, 0, -1 }, //NORTH
040: { -1, 0, -1, 0 }, //EAST
041: { 0, 1, 0, 1 }, { 1, 0, 1, 0 }, { 1, 1, -1, -1 }, //ZOOM_IN
042: { -1, -1, 1, 1 } };//ZOOM_OUT
043:
044: private static double panPercentage;
045:
046: /**
047: * Default constructor
048: */
049: public InstallKeyPanPlugIn() {
050: this (0.2);
051: }
052:
053: /**
054: *
055: * Creates a new plug-in with pan_percentage as pan percentage value
056: *
057: * pan_percentage The value in percent of screen size to pan/zoom. Accepted
058: * values are in the range 0 < percentage <= 1
059: */
060: public InstallKeyPanPlugIn(double panPercentag) {
061: super ();
062: setPanPercentage(panPercentag);
063: }
064:
065: public boolean execute(PlugInContext context) throws Exception {
066: context.getLayerViewPanel().getViewport().zoomToFullExtent();
067: return true;
068: }
069:
070: private static Envelope createEnvelopeFromDirection(
071: Envelope oldEnvelope, int direction) {
072:
073: double oldWidth = panPercentage * oldEnvelope.getWidth();
074: double oldHeight = panPercentage * oldEnvelope.getHeight();
075:
076: double dxPlus = DIRECTIONS[direction][0] * oldWidth;
077: double dyPlus = DIRECTIONS[direction][1] * oldHeight;
078: double dxMinus = DIRECTIONS[direction][2] * oldWidth;
079: double dyMinus = DIRECTIONS[direction][3] * oldHeight;
080:
081: return new Envelope(oldEnvelope.getMinX() - dxMinus,
082: oldEnvelope.getMaxX() - dxPlus, oldEnvelope.getMinY()
083: - dyMinus, oldEnvelope.getMaxY() - dyPlus);
084: }
085:
086: public boolean pan(JInternalFrame jif, int direction) {
087:
088: if (jif instanceof TaskFrame) {
089: TaskFrame taskFrame = (TaskFrame) jif;
090: LayerViewPanel lvp = taskFrame.getLayerViewPanel();
091: Viewport vp = lvp.getViewport();
092: Envelope oldEnvelope = vp.getEnvelopeInModelCoordinates();
093:
094: try {
095: vp.zoom(createEnvelopeFromDirection(oldEnvelope,
096: direction));
097: } catch (NoninvertibleTransformException e1) {
098: e1.printStackTrace();
099: return false;
100: }
101: }
102: return true;
103: }
104:
105: public void initialize(PlugInContext context) throws Exception {
106: super .initialize(context);
107:
108: AbstractPlugIn[] plugIns = { this , new PanHelper(NORTH),
109: new PanHelper(EAST), new PanHelper(SOUTH),
110: new PanHelper(WEST), new PanHelper(ZOOM_IN),
111: new PanHelper(ZOOM_OUT) };
112:
113: int[] keys = { KeyEvent.VK_HOME, KeyEvent.VK_UP,
114: KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT,
115: KeyEvent.VK_PAGE_DOWN, KeyEvent.VK_PAGE_UP };
116:
117: for (int i = 0; i < keys.length; i++) {
118: context.getWorkbenchContext().getWorkbench().getFrame()
119: .addKeyboardShortcut(keys[i], 0, plugIns[i], null);
120:
121: }
122:
123: }
124:
125: public String getName() {
126: return "";
127: }
128:
129: /**
130: * Get the pan/zoom percentage, a value between 0 and 1. Deafult is 0.25 (=25%)
131: * @return
132: */
133: public static double getPanPercentage() {
134: return 2 * panPercentage;
135: }
136:
137: /**
138: * Set the pan percentage. Legal values are between greater than 0 and less than
139: * or equal to 1.0
140: * @param panPercentage The value in percent of screen size to pan/zoom. Accepted
141: * values are in the range 0 < percentage <= 1
142: */
143: public static void setPanPercentage(double panPercent) {
144: if (panPercent <= 0 || panPercent > 1d) {
145: throw new IllegalArgumentException(
146: "Accepted values are in the "
147: + " range 0 < percentage <= 1");
148: }
149: //have percentage, otherwise it's percentage value in each direction
150: //making it twice as much!
151: panPercentage = panPercent / 2d;
152: }
153:
154: /**
155: * Helper class to pan in the direction given in constructor
156: */
157: private class PanHelper extends AbstractPlugIn {
158: private final int direction;
159:
160: public PanHelper(int direction) {
161: this .direction = direction;
162: }
163:
164: public boolean execute(PlugInContext context) throws Exception {
165: return pan(context.getWorkbenchFrame()
166: .getActiveInternalFrame(), direction);
167: }
168: }
169:
170: }
|