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.printing.ui;
010:
011: import java.awt.Rectangle;
012:
013: import net.refractions.udig.printing.ui.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().getContextModel().getMap().getRenderManager()
213: .getMapDisplay();
214: }
215:
216: /*
217: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
218: */
219: public void widgetSelected(SelectionEvent e) {
220: IBlackboard blackboard = getStyleBlackboard();
221: Rectangle rect = (Rectangle) blackboard
222: .get(LocationStyleContent.ID);
223:
224: if (rect == null) {
225: rect = LocationStyleContent.createDefaultStyle();
226: setLeft(rect);
227: setTop(rect);
228:
229: blackboard.put(LocationStyleContent.ID, rect);
230: ((StyleBlackboard) getStyleBlackboard())
231: .setSelected(new String[] { LocationStyleContent.ID });
232: }
233:
234: //read object state from ui widgets
235: switch (xCombo.getSelectionIndex()) {
236: case 0:
237: setLeft(rect);
238: break;
239: case 1:
240: setCenter(rect);
241: break;
242: case 2:
243: setRight(rect);
244:
245: }
246:
247: switch (yCombo.getSelectionIndex()) {
248: case 0:
249: setTop(rect);
250: break;
251: case 1:
252: setMiddle(rect);
253: break;
254: case 2:
255: setBottom(rect);
256: }
257: }
258:
259: /*
260: * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
261: */
262: public void widgetDefaultSelected(SelectionEvent e) {
263: //do nothing
264: }
265:
266: /* (non-Javadoc)
267: * @see net.refractions.udig.style.IStyleConfigurator#createControl(org.eclipse.swt.widgets.Composite)
268: */
269: public void createControl(Composite parent) {
270: parent.setLayout(new GridLayout(2, true));
271: Label xLabel = new Label(parent, SWT.RIGHT);
272: xLabel
273: .setText(Messages.ScalebarStyleConfigurator_horizontalAlignment);
274:
275: xCombo = new Combo(parent, SWT.DROP_DOWN);
276: xCombo.setItems(new String[] { LEFT, CENTER, RIGHT });
277: xCombo.select(0);
278: xCombo.addSelectionListener(this );
279:
280: Label yLabel = new Label(parent, SWT.RIGHT);
281: yLabel
282: .setText(Messages.ScalebarStyleConfigurator_verticalAlignment);
283:
284: yCombo = new Combo(parent, SWT.DROP_DOWN);
285: yCombo.setItems(new String[] { TOP, MIDDLE, BOTTOM });
286: yCombo.select(0);
287: yCombo.addSelectionListener(this);
288: }
289:
290: }
|