001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.mapgraphic.scalebar;
010:
011: import java.awt.Rectangle;
012:
013: import net.refractions.udig.mapgraphic.internal.Messages;
014: import net.refractions.udig.project.IBlackboard;
015: import net.refractions.udig.project.internal.Layer;
016: import net.refractions.udig.project.internal.StyleBlackboard;
017: import net.refractions.udig.project.render.displayAdapter.IMapDisplay;
018: import net.refractions.udig.style.IStyleConfigurator;
019:
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.events.SelectionEvent;
022: import org.eclipse.swt.events.SelectionListener;
023: import org.eclipse.swt.layout.GridLayout;
024: import org.eclipse.swt.widgets.Combo;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Label;
027:
028: /**
029: * Allows user to set the location of the scalebar?
030: *
031: * ScalebarStyleConfigurator x = new ScalebarStyleConfigurator( ... );
032: * TODO code example
033: *
034: * </code></pre>
035: *
036: * </p>
037: *
038: * @author jdeolive
039: * @since 0.6.0
040: */
041: public class ScalebarStyleConfigurator extends IStyleConfigurator
042: implements SelectionListener {
043:
044: /** vertical alignment constants * */
045: private static final String TOP = Messages.ScalebarStyleConfigurator_top;
046: private static final String MIDDLE = Messages.ScalebarStyleConfigurator_middle;
047: private static final String BOTTOM = Messages.ScalebarStyleConfigurator_bottom;
048:
049: /** horizontal alignment constants * */
050: private static final String LEFT = Messages.ScalebarStyleConfigurator_left;
051: private static final String CENTER = Messages.ScalebarStyleConfigurator_center;
052: private static final String RIGHT = Messages.ScalebarStyleConfigurator_right;
053:
054: /** ui widgets * */
055: private Combo xCombo;
056: private Combo yCombo;
057:
058: /* (non-Javadoc)
059: * @see net.refractions.udig.style.IStyleConfigurator#init()
060: */
061: public void init() {
062: // do nothing
063:
064: }
065:
066: /*
067: * @see net.refractions.udig.style.IStyleConfigurator#refresh()
068: */
069: public void refresh() {
070: IBlackboard blackboard = getStyleBlackboard();
071: Rectangle rect = (Rectangle) blackboard
072: .get(LocationStyleContent.ID);
073:
074: if (rect == null) {
075: rect = LocationStyleContent.createDefaultStyle();
076: setLeft(rect);
077: setTop(rect);
078:
079: blackboard.put(LocationStyleContent.ID, rect);
080: ((StyleBlackboard) blackboard)
081: .setSelected(new String[] { LocationStyleContent.ID });
082: }
083:
084: if (isLeft(rect))
085: xCombo.select(0);
086: else if (isCenter(rect))
087: xCombo.select(1);
088: else if (isRight(rect))
089: xCombo.select(2);
090:
091: if (isTop(rect))
092: yCombo.select(0);
093: else if (isMiddle(rect))
094: yCombo.select(1);
095: else if (isBottom(rect))
096: yCombo.select(2);
097:
098: }
099:
100: /*
101: * @see net.refractions.udig.style.IStyleConfigurator#apply()
102: */
103: public void apply() {
104: IBlackboard blackboard = getStyleBlackboard();
105: Rectangle rect = (Rectangle) blackboard
106: .get(LocationStyleContent.ID);
107:
108: if (rect == null) {
109: rect = LocationStyleContent.createDefaultStyle();
110: blackboard.put(LocationStyleContent.ID, rect);
111: ((StyleBlackboard) blackboard)
112: .setSelected(new String[] { LocationStyleContent.ID });
113:
114: }
115:
116: }
117:
118: /*
119: * @see net.refractions.udig.style.IStyleConfigurator#canStyle(net.refractions.udig.project.Layer)
120: */
121: public boolean canStyle(Layer layer) {
122: return layer.hasResource(ScalebarMapGraphic.class);
123: }
124:
125: protected boolean isLeft(Rectangle rect) {
126: return rect.x == LocationStyleContent.XPAD_LEFT;
127: }
128:
129: protected void setLeft(Rectangle rect) {
130: rect.x = LocationStyleContent.XPAD_LEFT;
131: }
132:
133: protected boolean isCenter(Rectangle rect) {
134: IMapDisplay display = getMapDisplay();
135:
136: int x = display.getWidth() / 2;
137: x -= rect.width / 2;
138:
139: return rect.x == x;
140: }
141:
142: protected void setCenter(Rectangle rect) {
143: IMapDisplay display = getMapDisplay();
144:
145: int x = display.getWidth() / 2;
146: x -= rect.width / 2;
147:
148: rect.x = x;
149: }
150:
151: protected boolean isRight(Rectangle rect) {
152: IMapDisplay display = getMapDisplay();
153:
154: int x = display.getWidth() - LocationStyleContent.XPAD_RIGHT
155: - rect.width;
156: return rect.x == x;
157: }
158:
159: protected void setRight(Rectangle rect) {
160: IMapDisplay display = getMapDisplay();
161:
162: int x = display.getWidth() - LocationStyleContent.XPAD_RIGHT
163: - rect.width;
164: rect.x = x;
165: }
166:
167: protected boolean isTop(Rectangle rect) {
168: return rect.y == LocationStyleContent.YPAD_TOP + rect.height;
169: }
170:
171: protected void setTop(Rectangle rect) {
172: rect.y = LocationStyleContent.YPAD_TOP + rect.height;
173: }
174:
175: protected boolean isMiddle(Rectangle rect) {
176: IMapDisplay display = getMapDisplay();
177:
178: int y = display.getHeight() / 2;
179: y -= rect.height / 2;
180:
181: return rect.y == y;
182: }
183:
184: protected void setMiddle(Rectangle rect) {
185: IMapDisplay display = getMapDisplay();
186:
187: int y = display.getHeight() / 2;
188: y -= rect.height / 2;
189:
190: rect.y = y;
191: }
192:
193: protected boolean isBottom(Rectangle rect) {
194: IMapDisplay display = getMapDisplay();
195:
196: int y = display.getHeight() - LocationStyleContent.YPAD_BOTTOM
197: - rect.height;
198:
199: return rect.y == y;
200: }
201:
202: protected void setBottom(Rectangle rect) {
203: IMapDisplay display = getMapDisplay();
204:
205: int y = display.getHeight() - LocationStyleContent.YPAD_BOTTOM
206: - rect.height;
207:
208: rect.y = y;
209: }
210:
211: protected IMapDisplay getMapDisplay() {
212: return getLayer().getMap().getRenderManager().getMapDisplay();
213: }
214:
215: /*
216: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
217: */
218: public void widgetSelected(SelectionEvent e) {
219: IBlackboard blackboard = getStyleBlackboard();
220: Rectangle rect = (Rectangle) blackboard
221: .get(LocationStyleContent.ID);
222:
223: if (rect == null) {
224: rect = LocationStyleContent.createDefaultStyle();
225: setLeft(rect);
226: setTop(rect);
227:
228: blackboard.put(LocationStyleContent.ID, rect);
229: ((StyleBlackboard) getStyleBlackboard())
230: .setSelected(new String[] { LocationStyleContent.ID });
231: }
232:
233: //read object state from ui widgets
234: switch (xCombo.getSelectionIndex()) {
235: case 0:
236: setLeft(rect);
237: break;
238: case 1:
239: setCenter(rect);
240: break;
241: case 2:
242: setRight(rect);
243:
244: }
245:
246: switch (yCombo.getSelectionIndex()) {
247: case 0:
248: setTop(rect);
249: break;
250: case 1:
251: setMiddle(rect);
252: break;
253: case 2:
254: setBottom(rect);
255: }
256: }
257:
258: /*
259: * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
260: */
261: public void widgetDefaultSelected(SelectionEvent e) {
262: //do nothing
263: }
264:
265: /* (non-Javadoc)
266: * @see net.refractions.udig.style.IStyleConfigurator#createControl(org.eclipse.swt.widgets.Composite)
267: */
268: public void createControl(Composite parent) {
269: parent.setLayout(new GridLayout(2, true));
270: Label xLabel = new Label(parent, SWT.RIGHT);
271: xLabel
272: .setText(Messages.ScalebarStyleConfigurator_horizontalAlignment);
273:
274: xCombo = new Combo(parent, SWT.DROP_DOWN);
275: xCombo.setItems(new String[] { LEFT, CENTER, RIGHT });
276: xCombo.select(0);
277: xCombo.addSelectionListener(this );
278:
279: Label yLabel = new Label(parent, SWT.RIGHT);
280: yLabel
281: .setText(Messages.ScalebarStyleConfigurator_verticalAlignment);
282:
283: yCombo = new Combo(parent, SWT.DROP_DOWN);
284: yCombo.setItems(new String[] { TOP, MIDDLE, BOTTOM });
285: yCombo.select(0);
286: yCombo.addSelectionListener(this);
287: }
288:
289: }
|