001: /**
002: * <copyright></copyright> $Id: RenderFactoryImpl.java 21423 2006-09-14 19:17:05Z jeichar $
003: */package net.refractions.udig.project.internal.render.impl;
004:
005: import java.awt.Graphics2D;
006: import java.awt.Point;
007: import java.awt.Rectangle;
008: import java.awt.geom.AffineTransform;
009: import java.awt.image.BufferedImage;
010: import java.util.ArrayList;
011: import java.util.Collections;
012: import java.util.Comparator;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import net.refractions.udig.catalog.IGeoResource;
017: import net.refractions.udig.core.internal.ExtensionPointList;
018: import net.refractions.udig.project.internal.ProjectPlugin;
019: import net.refractions.udig.project.internal.render.MultiLayerRenderer;
020: import net.refractions.udig.project.internal.render.RenderExecutor;
021: import net.refractions.udig.project.internal.render.RenderFactory;
022: import net.refractions.udig.project.internal.render.RenderManager;
023: import net.refractions.udig.project.internal.render.RenderPackage;
024: import net.refractions.udig.project.internal.render.Renderer;
025: import net.refractions.udig.project.internal.render.RendererDecorator;
026: import net.refractions.udig.project.internal.render.ViewportModel;
027: import net.refractions.udig.project.render.displayAdapter.IMapDisplay;
028:
029: import org.eclipse.core.runtime.CoreException;
030: import org.eclipse.core.runtime.IConfigurationElement;
031: import org.eclipse.core.runtime.IProgressMonitor;
032: import org.eclipse.core.runtime.Platform;
033: import org.eclipse.emf.ecore.EClass;
034: import org.eclipse.emf.ecore.EDataType;
035: import org.eclipse.emf.ecore.EObject;
036: import org.eclipse.emf.ecore.impl.EFactoryImpl;
037: import org.geotools.data.Query;
038: import org.geotools.geometry.jts.ReferencedEnvelope;
039: import org.geotools.referencing.FactoryFinder;
040: import org.geotools.referencing.crs.DefaultGeographicCRS;
041: import org.opengis.referencing.crs.CoordinateReferenceSystem;
042: import org.osgi.framework.Bundle;
043:
044: import com.vividsolutions.jts.geom.Coordinate;
045: import com.vividsolutions.jts.geom.Envelope;
046:
047: /**
048: * <!-- begin-user-doc --> An implementation of the model <b>Factory </b>! <!-- end-user-doc -->
049: *
050: * @generated
051: */
052: public class RenderFactoryImpl extends EFactoryImpl implements
053: RenderFactory {
054: /**
055: * <!-- begin-user-doc --> <!-- end-user-doc -->
056: *
057: * @generated
058: */
059: public static final String copyright = "uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004, Refractions Research Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; version 2.1 of the License. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details."; //$NON-NLS-1$
060:
061: /**
062: * Creates an instance of the factory. <!-- begin-user-doc --> <!-- end-user-doc -->
063: *
064: * @generated
065: */
066: public RenderFactoryImpl() {
067: super ();
068: }
069:
070: /**
071: * <!-- begin-user-doc --> <!-- end-user-doc -->
072: *
073: * @generated
074: */
075: public EObject create(EClass eClass) {
076: switch (eClass.getClassifierID()) {
077: case RenderPackage.RENDER_EXECUTOR:
078: return createRenderExecutor();
079: case RenderPackage.RENDER_MANAGER:
080: return createRenderManager();
081: case RenderPackage.VIEWPORT_MODEL:
082: return createViewportModel();
083: default:
084: throw new IllegalArgumentException(
085: "The class '" + eClass.getName() + "' is not a valid classifier"); //$NON-NLS-1$ //$NON-NLS-2$
086: }
087: }
088:
089: /**
090: * @see net.refractions.udig.project.internal.render.RenderFactory#createRenderExecutor()
091: */
092: public RenderExecutor createRenderExecutor() {
093: return new RenderExecutorImpl();
094: }
095:
096: /**
097: * <!-- begin-user-doc --> <!-- end-user-doc -->
098: *
099: * @generated
100: */
101: public Object createFromString(EDataType eDataType,
102: String initialValue) {
103: switch (eDataType.getClassifierID()) {
104: case RenderPackage.COORDINATE_REFERENCE_SYSTEM:
105: return createCoordinateReferenceSystemFromString(eDataType,
106: initialValue);
107: case RenderPackage.ENVELOPE:
108: return createEnvelopeFromString(eDataType, initialValue);
109: case RenderPackage.IGEO_RESOURCE:
110: return createIGeoResourceFromString(eDataType, initialValue);
111: case RenderPackage.BUFFERED_IMAGE:
112: return createBufferedImageFromString(eDataType,
113: initialValue);
114: case RenderPackage.RECTANGLE:
115: return createRectangleFromString(eDataType, initialValue);
116: case RenderPackage.QUERY:
117: return createQueryFromString(eDataType, initialValue);
118: case RenderPackage.GRAPHICS2_D:
119: return createGraphics2DFromString(eDataType, initialValue);
120: case RenderPackage.LIST:
121: return createListFromString(eDataType, initialValue);
122: case RenderPackage.COORDINATE:
123: return createCoordinateFromString(eDataType, initialValue);
124: case RenderPackage.POINT:
125: return createPointFromString(eDataType, initialValue);
126: case RenderPackage.AFFINE_TRANSFORM:
127: return createAffineTransformFromString(eDataType,
128: initialValue);
129: case RenderPackage.MAP_DISPLAY:
130: return createMapDisplayFromString(eDataType, initialValue);
131: case RenderPackage.IPROGRESS_MONITOR:
132: return createIProgressMonitorFromString(eDataType,
133: initialValue);
134: default:
135: throw new IllegalArgumentException(
136: "The datatype '" + eDataType.getName() + "' is not a valid classifier"); //$NON-NLS-1$ //$NON-NLS-2$
137: }
138: }
139:
140: /**
141: * <!-- begin-user-doc --> <!-- end-user-doc -->
142: *
143: * @generated
144: */
145: public String convertToString(EDataType eDataType,
146: Object instanceValue) {
147: switch (eDataType.getClassifierID()) {
148: case RenderPackage.COORDINATE_REFERENCE_SYSTEM:
149: return convertCoordinateReferenceSystemToString(eDataType,
150: instanceValue);
151: case RenderPackage.ENVELOPE:
152: return convertEnvelopeToString(eDataType, instanceValue);
153: case RenderPackage.IGEO_RESOURCE:
154: return convertIGeoResourceToString(eDataType, instanceValue);
155: case RenderPackage.BUFFERED_IMAGE:
156: return convertBufferedImageToString(eDataType,
157: instanceValue);
158: case RenderPackage.RECTANGLE:
159: return convertRectangleToString(eDataType, instanceValue);
160: case RenderPackage.QUERY:
161: return convertQueryToString(eDataType, instanceValue);
162: case RenderPackage.GRAPHICS2_D:
163: return convertGraphics2DToString(eDataType, instanceValue);
164: case RenderPackage.LIST:
165: return convertListToString(eDataType, instanceValue);
166: case RenderPackage.COORDINATE:
167: return convertCoordinateToString(eDataType, instanceValue);
168: case RenderPackage.POINT:
169: return convertPointToString(eDataType, instanceValue);
170: case RenderPackage.AFFINE_TRANSFORM:
171: return convertAffineTransformToString(eDataType,
172: instanceValue);
173: case RenderPackage.MAP_DISPLAY:
174: return convertMapDisplayToString(eDataType, instanceValue);
175: case RenderPackage.IPROGRESS_MONITOR:
176: return convertIProgressMonitorToString(eDataType,
177: instanceValue);
178: default:
179: throw new IllegalArgumentException(
180: "The datatype '" + eDataType.getName() + "' is not a valid classifier"); //$NON-NLS-1$ //$NON-NLS-2$
181: }
182: }
183:
184: /**
185: * <!-- begin-user-doc --> <!-- end-user-doc -->
186: *
187: * @generated
188: */
189: public RenderManager createRenderManager() {
190: RenderManagerImpl renderManager = new RenderManagerImpl();
191: return renderManager;
192: }
193:
194: /**
195: * <!-- begin-user-doc --> <!-- end-user-doc -->
196: */
197: public RenderManager createRenderManagerViewer() {
198: RenderManagerImpl renderManager = new RenderManagerImpl();
199: renderManager.setViewer(true);
200: return renderManager;
201: }
202:
203: /**
204: * <!-- begin-user-doc --> <!-- end-user-doc -->
205: *
206: * @generated
207: */
208: public ViewportModel createViewportModel() {
209: ViewportModelImpl viewportModel = new ViewportModelImpl();
210: return viewportModel;
211: }
212:
213: /**
214: * <!-- begin-user-doc --> <!-- end-user-doc -->
215: */
216: public ViewportModel createViewportModelViewer() {
217: ViewportModelImpl viewportModel = new ViewportModelImpl();
218: viewportModel.setViewer(true);
219: return viewportModel;
220: }
221:
222: /**
223: * <!-- begin-user-doc --> Processes the RenderExecutor Extension Point and creates the
224: * appropriate Executor <!-- end-user-doc -->
225: *
226: * @generated NOT
227: */
228: public RenderExecutor createRenderExecutor(Renderer renderer) {
229:
230: RenderExecutor executor = locateMatch(renderer);
231: if (executor == null)
232: executor = locateClosestFit(renderer);
233:
234: executor.setRenderer(renderer);
235: return executor;
236: }
237:
238: /**
239: * searches through the RenderExecutor Extension point and locates the RenderExecutor whose
240: * associated renderer is closest in hierarchical terms to the renderer parameter.
241: *
242: * @return The closest renderexecutor for the renderer.
243: */
244: private RenderExecutor locateClosestFit(Renderer r) {
245: Renderer ofInterest = r;
246: if (r instanceof RendererDecorator) {
247: ofInterest = ((RendererDecorator) r).getRenderer();
248: }
249: final Renderer renderer = ofInterest;
250: List<IConfigurationElement> list = new ArrayList<IConfigurationElement>();
251: for (Iterator<IConfigurationElement> iter = ExtensionPointList
252: .getExtensionPointList(RenderExecutor.EXTENSION_ID)
253: .iterator(); iter.hasNext();) {
254: IConfigurationElement elem = iter.next();
255: try {
256: Bundle bundle = Platform.getBundle(elem
257: .getNamespaceIdentifier());
258: Class<?> rendererClass = bundle.loadClass(elem
259: .getAttribute(RenderExecutor.RENDERER_ATTR));
260: if (rendererClass.isAssignableFrom(renderer.getClass()))
261: list.add(elem);
262: } catch (Exception e) {
263: ProjectPlugin.log(null, e);
264: // do nothing
265: }
266: }
267: if (!list.isEmpty()) {
268: Collections.sort(list,
269: new Comparator<IConfigurationElement>() {
270:
271: public int compare(IConfigurationElement o1,
272: IConfigurationElement o2) {
273: try {
274: Bundle bundle = Platform.getBundle(o1
275: .getNamespaceIdentifier());
276: Class clazz1 = bundle
277: .loadClass(o1
278: .getAttribute(RenderExecutor.RENDERER_ATTR));
279:
280: bundle = Platform.getBundle(o2
281: .getNamespaceIdentifier());
282: Class clazz2 = bundle
283: .loadClass(o2
284: .getAttribute(RenderExecutor.RENDERER_ATTR));
285:
286: int dist1 = getDistance(renderer
287: .getClass(), clazz1);
288: int dist2 = getDistance(renderer
289: .getClass(), clazz2);
290: if (dist1 == dist2)
291: return 0;
292: return dist1 < dist2 ? -1 : 1;
293: } catch (Exception e) {
294: ProjectPlugin.log(null, e);
295: // do nothing
296: }
297: return 0;
298: }
299:
300: private int getDistance(Class<?> rendererClass,
301: Class<?> target) {
302: if (!target.isAssignableFrom(rendererClass))
303: return -1;
304: if (target == rendererClass)
305: return 0;
306:
307: for (Class iClass : rendererClass
308: .getInterfaces()) {
309: if (iClass == target)
310: return 1;
311: else {
312: int distance = getDistance(iClass,
313: target);
314: if (distance != -1)
315: return 1 + distance;
316: }
317: }
318: return 1 + getDistance(rendererClass
319: .getSuperclass(), target);
320: }
321: });
322:
323: try {
324: return (RenderExecutor) list.get(0)
325: .createExecutableExtension(
326: RenderExecutor.EXECUTOR_ATTR);
327: } catch (CoreException e) {
328: ProjectPlugin.log(null, e);
329: }
330: }
331: return createRenderExecutor();
332: }
333:
334: /**
335: * searches through the RenderExecutor Extension point and locates the RenderExecutor whose
336: * associated renderer and exact match to the renderer class passed in as a parameter
337: *
338: * @return a renderExecutor for the renderer
339: */
340: private RenderExecutor locateMatch(Renderer renderer) {
341: Renderer ofInterest = renderer;
342: if (renderer instanceof RendererDecorator) {
343: ofInterest = ((RendererDecorator) renderer).getRenderer();
344: }
345: List<IConfigurationElement> list = new ArrayList<IConfigurationElement>();
346: for (Iterator<IConfigurationElement> iter = ExtensionPointList
347: .getExtensionPointList(RenderExecutor.EXTENSION_ID)
348: .iterator(); iter.hasNext();) {
349: IConfigurationElement elem = iter.next();
350: if (elem.getAttribute(RenderExecutor.RENDERER_ATTR).equals(
351: ofInterest.getClass().getName()))
352: list.add(elem);
353: }
354:
355: if (!list.isEmpty()) {
356: // TODO if list has more than one element determine which is best.
357: try {
358: return (RenderExecutor) list.get(0)
359: .createExecutableExtension(
360: RenderExecutor.EXECUTOR_ATTR);
361: } catch (CoreException e) {
362: ProjectPlugin.log(null, e);
363: }
364: }
365: return null;
366:
367: }
368:
369: /**
370: * Creates a new RenderContext object for a renderer that renders selections.
371: *
372: * @param selection Indicates whether the getQuery method will return a query for selected
373: * features.
374: * @return a new RenderContext object for a renderer that renders selections.
375: */
376: public RenderContextImpl createRenderContext(boolean selection) {
377: RenderContextImpl renderContext = new RenderContextImpl(
378: selection);
379: return renderContext;
380: }
381:
382: /**
383: * <!-- begin-user-doc --> <!-- end-user-doc -->
384: *
385: * @generated
386: */
387: public Rectangle createRectangleFromString(EDataType eDataType,
388: String initialValue) {
389: return (Rectangle) super .createFromString(eDataType,
390: initialValue);
391: }
392:
393: /**
394: * <!-- begin-user-doc --> <!-- end-user-doc -->
395: *
396: * @generated
397: */
398: public String convertRectangleToString(EDataType eDataType,
399: Object instanceValue) {
400: return super .convertToString(eDataType, instanceValue);
401: }
402:
403: /**
404: * <!-- begin-user-doc --> <!-- end-user-doc -->
405: *
406: * @generated
407: */
408: public AffineTransform createAffineTransformFromString(
409: EDataType eDataType, String initialValue) {
410: return (AffineTransform) super .createFromString(eDataType,
411: initialValue);
412: }
413:
414: /**
415: * <!-- begin-user-doc --> <!-- end-user-doc -->
416: *
417: * @generated
418: */
419: public String convertAffineTransformToString(EDataType eDataType,
420: Object instanceValue) {
421: return super .convertToString(eDataType, instanceValue);
422: }
423:
424: /**
425: * <!-- begin-user-doc --> <!-- end-user-doc -->
426: *
427: * @generated
428: */
429: public Point createPointFromString(EDataType eDataType,
430: String initialValue) {
431: return (Point) super .createFromString(eDataType, initialValue);
432: }
433:
434: /**
435: * <!-- begin-user-doc --> <!-- end-user-doc -->
436: *
437: * @generated
438: */
439: public String convertPointToString(EDataType eDataType,
440: Object instanceValue) {
441: return super .convertToString(eDataType, instanceValue);
442: }
443:
444: /**
445: * <!-- begin-user-doc --> <!-- end-user-doc -->
446: *
447: * @generated
448: */
449: public IMapDisplay createMapDisplayFromString(EDataType eDataType,
450: String initialValue) {
451: return (IMapDisplay) super .createFromString(eDataType,
452: initialValue);
453: }
454:
455: /**
456: * <!-- begin-user-doc --> <!-- end-user-doc -->
457: *
458: * @generated
459: */
460: public String convertMapDisplayToString(EDataType eDataType,
461: Object instanceValue) {
462: return super .convertToString(eDataType, instanceValue);
463: }
464:
465: /**
466: * <!-- begin-user-doc --> <!-- end-user-doc -->
467: *
468: * @generated
469: */
470: public IProgressMonitor createIProgressMonitorFromString(
471: EDataType eDataType, String initialValue) {
472: return (IProgressMonitor) super .createFromString(eDataType,
473: initialValue);
474: }
475:
476: /**
477: * <!-- begin-user-doc --> <!-- end-user-doc -->
478: *
479: * @generated
480: */
481: public String convertIProgressMonitorToString(EDataType eDataType,
482: Object instanceValue) {
483: return super .convertToString(eDataType, instanceValue);
484: }
485:
486: /**
487: * <!-- begin-user-doc --> <!-- end-user-doc -->
488: *
489: * @generated public InfoList createInfoListFromString(EDataType eDataType, String initialValue) {
490: * return (InfoList)super.createFromString(eDataType, initialValue); }
491: */
492:
493: /**
494: * <!-- begin-user-doc --> <!-- end-user-doc -->
495: *
496: * @generated
497: */
498: public String convertInfoListToString(EDataType eDataType,
499: Object instanceValue) {
500: return super .convertToString(eDataType, instanceValue);
501: }
502:
503: /**
504: * <!-- begin-user-doc --> <!-- end-user-doc -->
505: *
506: * @generated
507: */
508: public Graphics2D createGraphics2DFromString(EDataType eDataType,
509: String initialValue) {
510: return (Graphics2D) super .createFromString(eDataType,
511: initialValue);
512: }
513:
514: /**
515: * <!-- begin-user-doc --> <!-- end-user-doc -->
516: *
517: * @generated
518: */
519: public String convertGraphics2DToString(EDataType eDataType,
520: Object instanceValue) {
521: return super .convertToString(eDataType, instanceValue);
522: }
523:
524: /**
525: * <!-- begin-user-doc --> <!-- end-user-doc -->
526: *
527: * @generated
528: */
529: public Coordinate createCoordinateFromString(EDataType eDataType,
530: String initialValue) {
531: return (Coordinate) super .createFromString(eDataType,
532: initialValue);
533: }
534:
535: /**
536: * <!-- begin-user-doc --> <!-- end-user-doc -->
537: *
538: * @generated
539: */
540: public String convertCoordinateToString(EDataType eDataType,
541: Object instanceValue) {
542: return super .convertToString(eDataType, instanceValue);
543: }
544:
545: /**
546: * <!-- begin-user-doc --> <!-- end-user-doc -->
547: *
548: * @generated NOT
549: */
550: public CoordinateReferenceSystem createCoordinateReferenceSystemFromString(
551: EDataType eDataType, String initialValue) {
552: try {
553: return FactoryFinder.getCRSFactory(null).createFromWKT(
554: initialValue);
555: } catch (Exception e) {
556: return ViewportModelImpl.getDefaultCRS();
557: }
558: }
559:
560: /**
561: * <!-- begin-user-doc --> <!-- end-user-doc -->
562: *
563: * @generated NOT
564: */
565: public String convertCoordinateReferenceSystemToString(
566: EDataType eDataType, Object instanceValue) {
567: try {
568: return ((CoordinateReferenceSystem) instanceValue).toWKT()
569: .replace("\n", " "); //$NON-NLS-1$ //$NON-NLS-2$
570: } catch (Exception e) {
571: ProjectPlugin.log("Couldn't write crs"); //$NON-NLS-1$
572: return DefaultGeographicCRS.WGS84.toWKT()
573: .replace("\n", " "); //$NON-NLS-1$//$NON-NLS-2$
574: }
575: }
576:
577: /**
578: * <!-- begin-user-doc --> <!-- end-user-doc -->
579: *
580: * @generated NOT
581: */
582: public Envelope createEnvelopeFromString(EDataType eDataType,
583: String initialValue) {
584: if (initialValue.equals("")) //$NON-NLS-1$
585: return new Envelope();
586: String[] coords = initialValue.split(","); //$NON-NLS-1$
587: return new ReferencedEnvelope(new Envelope(Double
588: .parseDouble(coords[0]), Double.parseDouble(coords[1]),
589: Double.parseDouble(coords[2]), Double
590: .parseDouble(coords[3])), null);
591: }
592:
593: /**
594: * <!-- begin-user-doc --> <!-- end-user-doc -->
595: *
596: * @generated NOT
597: */
598: public String convertEnvelopeToString(EDataType eDataType,
599: Object instanceValue) {
600: Envelope env = (Envelope) instanceValue;
601: if (env.isNull())
602: return ""; //$NON-NLS-1$
603:
604: return env.getMinX()
605: + "," + env.getMaxX() + "," + env.getMinY() + "," + env.getMaxY(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
606: }
607:
608: /**
609: * <!-- begin-user-doc --> <!-- end-user-doc -->
610: *
611: * @generated
612: */
613: public BufferedImage createBufferedImageFromString(
614: EDataType eDataType, String initialValue) {
615: return (BufferedImage) super .createFromString(eDataType,
616: initialValue);
617: }
618:
619: /**
620: * <!-- begin-user-doc --> <!-- end-user-doc -->
621: *
622: * @generated
623: */
624: public String convertBufferedImageToString(EDataType eDataType,
625: Object instanceValue) {
626: return super .convertToString(eDataType, instanceValue);
627: }
628:
629: /**
630: * <!-- begin-user-doc --> <!-- end-user-doc -->
631: *
632: * @generated
633: */
634: public Query createQueryFromString(EDataType eDataType,
635: String initialValue) {
636: return (Query) super .createFromString(eDataType, initialValue);
637: }
638:
639: /**
640: * <!-- begin-user-doc --> <!-- end-user-doc -->
641: *
642: * @generated
643: */
644: public String convertQueryToString(EDataType eDataType,
645: Object instanceValue) {
646: return super .convertToString(eDataType, instanceValue);
647: }
648:
649: /**
650: * <!-- begin-user-doc --> <!-- end-user-doc -->
651: *
652: * @generated
653: */
654: public List createListFromString(EDataType eDataType,
655: String initialValue) {
656: return (List) super .createFromString(eDataType, initialValue);
657: }
658:
659: /**
660: * <!-- begin-user-doc --> <!-- end-user-doc -->
661: *
662: * @generated
663: */
664: public String convertListToString(EDataType eDataType,
665: Object instanceValue) {
666: return super .convertToString(eDataType, instanceValue);
667: }
668:
669: /**
670: * <!-- begin-user-doc --> <!-- end-user-doc -->
671: *
672: * @generated
673: */
674: public IGeoResource createIGeoResourceFromString(
675: EDataType eDataType, String initialValue) {
676: return (IGeoResource) super .createFromString(eDataType,
677: initialValue);
678: }
679:
680: /**
681: * <!-- begin-user-doc --> <!-- end-user-doc -->
682: *
683: * @generated
684: */
685: public String convertIGeoResourceToString(EDataType eDataType,
686: Object instanceValue) {
687: return super .convertToString(eDataType, instanceValue);
688: }
689:
690: /**
691: * <!-- begin-user-doc --> <!-- end-user-doc -->
692: *
693: * @generated
694: */
695: public RenderPackage getRenderPackage() {
696: return (RenderPackage) getEPackage();
697: }
698:
699: /**
700: * <!-- begin-user-doc --> <!-- end-user-doc -->
701: *
702: * @deprecated
703: * @generated
704: */
705: public static RenderPackage getPackage() {
706: return RenderPackage.eINSTANCE;
707: }
708:
709: /**
710: * @see net.refractions.udig.project.internal.render.RenderFactory#createCompositeRenderer()
711: */
712: public MultiLayerRenderer createCompositeRenderer() {
713: return new CompositeRendererImpl();
714: }
715:
716: }
|