001: /**
002: * <copyright>
003: * </copyright>
004: *
005: * $Id$
006: */package net.refractions.udig.printing.model.impl;
007:
008: import java.awt.Graphics2D;
009: import java.io.IOException;
010: import java.io.StringReader;
011: import java.io.StringWriter;
012:
013: import net.refractions.udig.printing.model.Box;
014: import net.refractions.udig.printing.model.BoxPrinter;
015: import net.refractions.udig.printing.model.Connection;
016: import net.refractions.udig.printing.model.DecoratorBox;
017: import net.refractions.udig.printing.model.Element;
018: import net.refractions.udig.printing.model.ModelFactory;
019: import net.refractions.udig.printing.model.ModelPackage;
020: import net.refractions.udig.printing.model.Page;
021: import net.refractions.udig.printing.model.PrintingModelPlugin;
022:
023: import org.eclipse.core.runtime.IConfigurationElement;
024: import org.eclipse.core.runtime.IExtension;
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.eclipse.core.runtime.Platform;
027: import org.eclipse.draw2d.geometry.Dimension;
028: import org.eclipse.draw2d.geometry.Point;
029: import org.eclipse.emf.ecore.EClass;
030: import org.eclipse.emf.ecore.EDataType;
031: import org.eclipse.emf.ecore.EObject;
032: import org.eclipse.emf.ecore.impl.EFactoryImpl;
033: import org.eclipse.ui.XMLMemento;
034:
035: /**
036: * <!-- begin-user-doc --> An implementation of the model <b>Factory</b>. <!-- end-user-doc -->
037: * @generated
038: */
039: public class ModelFactoryImpl extends EFactoryImpl implements
040: ModelFactory {
041: /**
042: * Creates an instance of the factory.
043: * <!-- begin-user-doc --> <!-- end-user-doc -->
044: * @generated
045: */
046: public ModelFactoryImpl() {
047: super ();
048: }
049:
050: /**
051: * <!-- begin-user-doc --> <!-- end-user-doc -->
052: * @generated
053: */
054: public EObject create(EClass eClass) {
055: switch (eClass.getClassifierID()) {
056: case ModelPackage.BOX:
057: return createBox();
058: case ModelPackage.CONNECTION:
059: return createConnection();
060: case ModelPackage.DECORATOR_BOX:
061: return createDecoratorBox();
062: case ModelPackage.ELEMENT:
063: return createElement();
064: case ModelPackage.PAGE:
065: return createPage();
066: default:
067: throw new IllegalArgumentException(
068: "The class '" + eClass.getName() + "' is not a valid classifier"); //$NON-NLS-1$ //$NON-NLS-2$
069: }
070: }
071:
072: /**
073: * <!-- begin-user-doc --> <!-- end-user-doc -->
074: * @generated
075: */
076: public Object createFromString(EDataType eDataType,
077: String initialValue) {
078: switch (eDataType.getClassifierID()) {
079: case ModelPackage.GRAPHICS2_D:
080: return createGraphics2DFromString(eDataType, initialValue);
081: case ModelPackage.DIMENSION:
082: return createDimensionFromString(eDataType, initialValue);
083: case ModelPackage.POINT:
084: return createPointFromString(eDataType, initialValue);
085: case ModelPackage.IPROGRESS_MONITOR:
086: return createIProgressMonitorFromString(eDataType,
087: initialValue);
088: case ModelPackage.BOX_PRINTER:
089: return createBoxPrinterFromString(eDataType, initialValue);
090: default:
091: throw new IllegalArgumentException(
092: "The datatype '" + eDataType.getName() + "' is not a valid classifier"); //$NON-NLS-1$ //$NON-NLS-2$
093: }
094: }
095:
096: /**
097: * <!-- begin-user-doc --> <!-- end-user-doc -->
098: * @generated
099: */
100: public String convertToString(EDataType eDataType,
101: Object instanceValue) {
102: switch (eDataType.getClassifierID()) {
103: case ModelPackage.GRAPHICS2_D:
104: return convertGraphics2DToString(eDataType, instanceValue);
105: case ModelPackage.DIMENSION:
106: return convertDimensionToString(eDataType, instanceValue);
107: case ModelPackage.POINT:
108: return convertPointToString(eDataType, instanceValue);
109: case ModelPackage.IPROGRESS_MONITOR:
110: return convertIProgressMonitorToString(eDataType,
111: instanceValue);
112: case ModelPackage.BOX_PRINTER:
113: return convertBoxPrinterToString(eDataType, instanceValue);
114: default:
115: throw new IllegalArgumentException(
116: "The datatype '" + eDataType.getName() + "' is not a valid classifier"); //$NON-NLS-1$ //$NON-NLS-2$
117: }
118: }
119:
120: /**
121: * <!-- begin-user-doc -->
122: * <!-- end-user-doc -->
123: * @generated
124: */
125: public Box createBox() {
126: BoxImpl box = new BoxImpl();
127: return box;
128: }
129:
130: /**
131: * <!-- begin-user-doc --> <!-- end-user-doc -->
132: * @generated
133: */
134: public Connection createConnection() {
135: ConnectionImpl connection = new ConnectionImpl();
136: return connection;
137: }
138:
139: /**
140: * <!-- begin-user-doc --> <!-- end-user-doc -->
141: * @generated
142: */
143: public DecoratorBox createDecoratorBox() {
144: DecoratorBoxImpl decoratorBox = new DecoratorBoxImpl();
145: return decoratorBox;
146: }
147:
148: /**
149: * <!-- begin-user-doc --> <!-- end-user-doc -->
150: * @generated
151: */
152: public Element createElement() {
153: ElementImpl element = new ElementImpl();
154: return element;
155: }
156:
157: /**
158: * <!-- begin-user-doc --> <!-- end-user-doc -->
159: * @generated
160: */
161: public Page createPage() {
162: PageImpl page = new PageImpl();
163: return page;
164: }
165:
166: /**
167: * <!-- begin-user-doc --> <!-- end-user-doc -->
168: * @generated
169: */
170: public Graphics2D createGraphics2DFromString(EDataType eDataType,
171: String initialValue) {
172: return (Graphics2D) super .createFromString(eDataType,
173: initialValue);
174: }
175:
176: /**
177: * <!-- begin-user-doc --> <!-- end-user-doc -->
178: * @generated
179: */
180: public String convertGraphics2DToString(EDataType eDataType,
181: Object instanceValue) {
182: return super .convertToString(eDataType, instanceValue);
183: }
184:
185: /**
186: * <!-- begin-user-doc --> <!-- end-user-doc -->
187: *
188: * @generated NOT
189: */
190: public Dimension createDimensionFromString(EDataType eDataType,
191: String initialValue) {
192: String[] parts = initialValue.split(","); //$NON-NLS-1$
193: Dimension dim;
194: try {
195: dim = new Dimension(Integer.parseInt(parts[0]), Integer
196: .parseInt(parts[1]));
197: } catch (Exception e) {
198: PrintingModelPlugin.log("", e); //$NON-NLS-1$
199: dim = new Dimension(0, 0);
200: }
201: return dim;
202: }
203:
204: /**
205: * <!-- begin-user-doc --> <!-- end-user-doc -->
206: *
207: * @generated NOT
208: */
209: public String convertDimensionToString(EDataType eDataType,
210: Object instanceValue) {
211: Dimension dim = (Dimension) instanceValue;
212: return dim.width + "," + dim.height; //$NON-NLS-1$
213: }
214:
215: /**
216: * <!-- begin-user-doc --> <!-- end-user-doc -->
217: *
218: * @generated NOT
219: */
220: public Point createPointFromString(EDataType eDataType,
221: String initialValue) {
222: String[] parts = initialValue.split(","); //$NON-NLS-1$
223: Point point;
224: try {
225: point = new Point(Integer.parseInt(parts[0].trim()),
226: Integer.parseInt(parts[1].trim()));
227: } catch (Exception e) {
228: PrintingModelPlugin.log("", e); //$NON-NLS-1$
229: point = new Point(0, 0);
230: }
231: return point;
232: }
233:
234: /**
235: * <!-- begin-user-doc --> <!-- end-user-doc -->
236: *
237: * @generated NOT
238: */
239: public String convertPointToString(EDataType eDataType,
240: Object instanceValue) {
241: Point point = (Point) instanceValue;
242: return point.x + "," + point.y; //$NON-NLS-1$
243: }
244:
245: /**
246: * <!-- begin-user-doc --> <!-- end-user-doc -->
247: * @generated
248: */
249: public IProgressMonitor createIProgressMonitorFromString(
250: EDataType eDataType, String initialValue) {
251: return (IProgressMonitor) super .createFromString(eDataType,
252: initialValue);
253: }
254:
255: /**
256: * <!-- begin-user-doc --> <!-- end-user-doc -->
257: * @generated
258: */
259: public String convertIProgressMonitorToString(EDataType eDataType,
260: Object instanceValue) {
261: return super .convertToString(eDataType, instanceValue);
262: }
263:
264: /**
265: * <!-- begin-user-doc -->
266: * <!-- end-user-doc -->
267: * @generated NOT
268: */
269: public BoxPrinter createBoxPrinterFromString(EDataType eDataType,
270: String initialValue) {
271: String[] split = initialValue.split("_\\|\\|\\|_"); //$NON-NLS-1$
272: String extensionID = split[0].trim();
273: String className = split[1].trim();
274: String mementoString = split[2].trim();
275:
276: if (mementoString.length() == 0) {
277: return null;
278: }
279:
280: try {
281: XMLMemento memento = XMLMemento
282: .createReadRoot(new StringReader(mementoString));
283: IExtension extension = Platform
284: .getExtensionRegistry()
285: .getExtension(
286: PrintingModelPlugin.BOX_PRINTER_EXTENSION_ID,
287: extensionID);
288: if (extension == null) {
289: PrintingModelPlugin
290: .log(
291: "The method " + className + "#getExtensionID() returned: " + extensionID + //$NON-NLS-1$ //$NON-NLS-2$
292: " this is not the correct extension id. Check the extension id. \nHint: Read the "
293: + //$NON-NLS-1$
294: "javadocs for the method", null); //$NON-NLS-1$
295: return null;
296: }
297: IConfigurationElement[] elements = extension
298: .getConfigurationElements();
299: IConfigurationElement current = null;
300: for (int i = 0; current == null && i < elements.length; i++) {
301: current = elements[i];
302: String attribute = current.getAttribute("class"); //$NON-NLS-1$
303: if (!attribute.equals(className))
304: current = null;
305: }
306: if (current == null)
307: return null;
308: BoxPrinter printer = (BoxPrinter) current
309: .createExecutableExtension("class"); //$NON-NLS-1$
310: printer.load(memento);
311: return printer;
312: } catch (Exception e) {
313: PrintingModelPlugin
314: .log(
315: "Error while creating a box printer from string.", e); //$NON-NLS-1$
316: return null;
317: }
318: }
319:
320: private final String SPLIT = "_|||_"; //$NON-NLS-1$
321:
322: /**
323: * <!-- begin-user-doc -->
324: * <!-- end-user-doc -->
325: * @generated NOT
326: */
327: public String convertBoxPrinterToString(EDataType eDataType,
328: Object instanceValue) {
329: BoxPrinter printer = (BoxPrinter) instanceValue;
330: XMLMemento memento = XMLMemento.createWriteRoot("boxPrinter"); //$NON-NLS-1$
331: printer.save(memento);
332:
333: StringWriter writer = new StringWriter();
334: writer.getBuffer().append(printer.getExtensionPointID());
335: writer.getBuffer().append(SPLIT);
336: writer.getBuffer().append(printer.getClass().getName());
337: writer.getBuffer().append(SPLIT);
338: try {
339: memento.save(writer);
340: } catch (IOException e) {
341: return "<boxPrinter></boxPrinter>"; //$NON-NLS-1$
342: }
343:
344: return writer.getBuffer().toString();
345: }
346:
347: /**
348: * <!-- begin-user-doc --> <!-- end-user-doc -->
349: * @generated
350: */
351: public ModelPackage getModelPackage() {
352: return (ModelPackage) getEPackage();
353: }
354:
355: /**
356: * <!-- begin-user-doc --> <!-- end-user-doc -->
357: * @deprecated
358: * @generated
359: */
360: public static ModelPackage getPackage() {
361: return ModelPackage.eINSTANCE;
362: }
363:
364: } // ModelFactoryImpl
|