001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.mapgraphic.grid;
016:
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.ModifyListener;
019: import org.eclipse.swt.widgets.Combo;
020: import org.eclipse.swt.widgets.Listener;
021: import org.eclipse.swt.widgets.Spinner;
022:
023: /**
024: * Controls the behaviour of the Spinner and Combo for setting the horizontal or vertical
025: * spacing of lines.
026: *
027: * @author Jesse
028: * @since 1.1.0
029: */
030: class SpacerController {
031:
032: private final Spinner spinner;
033: private final Combo unit;
034:
035: static final String PIXELS = "pixels";
036: static final String DEGREES = "degrees";
037: static final String MINUTES = "minutes";
038: static final String SECONDS = "seconds";
039:
040: static final String[] UNITS = new String[] { PIXELS, DEGREES,
041: MINUTES, SECONDS };
042:
043: public SpacerController(final Spinner spinner, final Combo unit) {
044: super ();
045: this .spinner = spinner;
046: this .unit = unit;
047: this .unit.setItems(UNITS);
048: this .unit.select(0);
049:
050: }
051:
052: public Spinner getSpinner() {
053: return spinner;
054: }
055:
056: public Combo getUnit() {
057: return unit;
058: }
059:
060: public static double spinnerUnit(int digitsInSpinner) {
061: return Math.pow(10, digitsInSpinner);
062: }
063:
064: private void setSpinner(int inc, int max, int digits, int pageinc) {
065: spinner.setIncrement(inc);
066: spinner.setMaximum(max);
067: spinner.setDigits(digits);
068: spinner.setPageIncrement(pageinc);
069: }
070:
071: /**
072: * Sets the spinner to the correct setting for the unit. Sets the incrememnt,
073: * maximum value, digits and pageincrement values on the spinner.
074: */
075: public void setSpinnerProperties() {
076: if (GridStyleConfigurator.selectedString(unit).equals(
077: SpacerController.PIXELS)) {
078: setSpinner(1, Integer.MAX_VALUE, 0, 10);
079: } else if (GridStyleConfigurator.selectedString(unit).equals(
080: SpacerController.MINUTES)) {
081: setSpinner((int) (spinnerUnit(4)),
082: (int) (360 * 60 * spinnerUnit(4)), 4, 60);
083: } else if (GridStyleConfigurator.selectedString(unit).equals(
084: SpacerController.SECONDS)) {
085: setSpinner((int) spinnerUnit(2),
086: (int) (360 * 3600 * spinnerUnit(2)), 2, 3600);
087: } else if (GridStyleConfigurator.selectedString(unit).equals(
088: SpacerController.DEGREES)) {
089: setSpinner((int) spinnerUnit(6),
090: (int) (360 * spinnerUnit(6)), 6, 10);
091: }
092: }
093:
094: /**
095: * Updates otherSpacer to be in the same unit as this if it is in pixel space.
096: * If not it leave it alone.
097: *
098: * @return true if it updated the other spacer.
099: */
100: public boolean validateWorld(SpacerController otherSpacer) {
101: if (otherSpacer.isPixelsSelected()) {
102: otherSpacer.getUnit().select(unit.getSelectionIndex());
103: return true;
104: } else {
105: return false;
106: }
107: }
108:
109: /**
110: * Updates otherSpacer to be in the same unit as this if it is in world space. If not it leave it alone.
111: *
112: * @param otherSpacer
113: * @return true if it updated the other spacer.
114: */
115: public boolean validatePixels(SpacerController otherSpacer) {
116: if (!otherSpacer.isPixelsSelected()) {
117: otherSpacer.getUnit().select(
118: otherSpacer.getUnit().indexOf(PIXELS));
119: return true;
120: }
121: return false;
122: }
123:
124: /**
125: * Returns true if this spacer is in Pixel Units.
126: *
127: * @return true if this spacer is in Pixel Units.
128: */
129: public boolean isPixelsSelected() {
130: return GridStyleConfigurator.selectedString(getUnit()).equals(
131: PIXELS);
132: }
133:
134: /**
135: * adds listener to unit and spinner as for Modify event listeners ;
136: * Adds modify listener to unit.
137: *
138: * @param modifyListener
139: * @param listener
140: */
141: public void addListeners(ModifyListener modifyListener,
142: Listener listener) {
143: unit.addModifyListener(modifyListener);
144: unit.addListener(SWT.Modify, listener);
145: spinner.addListener(SWT.Modify, listener);
146: spinner.addListener(SWT.KeyUp, listener);
147:
148: }
149:
150: /**
151: * Removes listeners from unit and spinner.
152: *
153: * @param modifyListener
154: * @param listener
155: */
156: public void removeListeners(ModifyListener modifyListener,
157: Listener listener) {
158: unit.removeModifyListener(modifyListener);
159: unit.removeListener(SWT.Modify, listener);
160: spinner.removeListener(SWT.Modify, listener);
161: spinner.removeListener(SWT.KeyUp, listener);
162: }
163:
164: /**
165: * Sets the unit and spinner in this spacer to the correct value. If the spacer is
166: * currently in pixel space then it will be changed to degrees.
167: *
168: * @param value value in DEGREES
169: */
170: public void setWorldSpacing(double value) {
171: double unitValue = spinnerUnit(spinner.getDigits());
172: if (GridStyleConfigurator.selectedString(unit).equals(
173: SpacerController.DEGREES)) {
174: spinner.setSelection((int) (value * unitValue));
175: } else if (GridStyleConfigurator.selectedString(unit).equals(
176: SpacerController.MINUTES)) {
177: spinner.setSelection((int) (value * 60 * unitValue));
178: } else if (GridStyleConfigurator.selectedString(unit).equals(
179: SpacerController.SECONDS)) {
180: spinner.setSelection((int) (value * 3600 * unitValue));
181: } else if (GridStyleConfigurator.selectedString(unit).equals(
182: SpacerController.PIXELS)) {
183: unit.select(unit.indexOf(SpacerController.DEGREES));
184: setSpinnerProperties();
185: spinner.setSelection((int) value);
186: }
187: }
188:
189: /**
190: * Sets this spacer to be in pixel space and set the value of the spinner.
191: * @param spacing the value to set on the spinner
192: */
193: public void setPixelSpacing(double spacing) {
194: unit.select(unit.indexOf(PIXELS));
195: setSpinnerProperties();
196: spinner.setSelection((int) spacing);
197: }
198:
199: public double getSpacing() {
200: double selection = (double) spinner.getSelection()
201: / spinnerUnit(spinner.getDigits());
202: if (GridStyleConfigurator.selectedString(unit).equals(
203: SpacerController.MINUTES)) {
204: return selection / 60;
205: }
206: if (GridStyleConfigurator.selectedString(unit).equals(
207: SpacerController.SECONDS)) {
208: return selection / 3600;
209: }
210: return selection;
211: }
212:
213: }
|