Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 60beee2df926e317e04546aef8c03ecb52564baa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************************************
 * Copyright (c) 2010, 2016 THALES GLOBAL SERVICES.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.figure.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.SWTGraphics;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.draw2d.ui.internal.graphics.ScaledGraphics;
import org.eclipse.sirius.diagram.ui.tools.internal.render.SiriusGraphicsToGraphics2DAdaptor;

/**
 * .
 * 
 * @author mchauvin
 */
public final class GraphicsUtilities {

    private static Method method;

    private static Method zoomMethod;

    static {
        try {
            method = ScaledGraphics.class.getDeclaredMethod("getGraphics"); //$NON-NLS-1$
            method.setAccessible(true);
            zoomMethod = ScaledGraphics.class.getDeclaredMethod("zoomFillRect", int.class, int.class, int.class, int.class); //$NON-NLS-1$
            zoomMethod.setAccessible(true);
        } catch (final NoSuchMethodException e) {
            // Cannot happen here
        }
    }

    private GraphicsUtilities() {
    }

    /**
     * Get the SWT graphic instance.
     * 
     * @param graphics
     *            the wrapped SWT graphics instance
     * @return the wrapped graphic instance
     */
    public static SWTGraphics getSWTGraphics(final Graphics graphics) {

        SWTGraphics swtGrpahics = null;

        Graphics internalGraphics = null;
        if (graphics instanceof ScaledGraphics) {
            internalGraphics = GraphicsUtilities.getInternalGraphics((ScaledGraphics) graphics);
        }

        if (internalGraphics instanceof SWTGraphics) {
            swtGrpahics = (SWTGraphics) internalGraphics;
            /* we need to recheck in case of zoom see trac #1065 */
        } else if (internalGraphics instanceof ScaledGraphics) {
            swtGrpahics = GraphicsUtilities.getSWTGraphics(internalGraphics);
        } else if (internalGraphics instanceof org.eclipse.draw2d.ScaledGraphics) {
            swtGrpahics = GraphicsUtilities.getSWTGraphics(internalGraphics);
        }
        return swtGrpahics;
    }

    /**
     * Get the SiriusGraphicsToGraphics2DAdaptor graphic instance.
     * 
     * @param graphics
     *            the wrapped SiriusGraphicsToGraphics2DAdaptor graphics
     *            instance
     * @return the wrapped graphic instance
     */
    public static SiriusGraphicsToGraphics2DAdaptor getSiriusGraphicsToGraphics2DAdaptor(Graphics graphics) {
        Graphics internalGraphics = null;
        if (graphics instanceof ScaledGraphics) {
            internalGraphics = GraphicsUtilities.getInternalGraphics((ScaledGraphics) graphics);
        }
        if (internalGraphics instanceof SiriusGraphicsToGraphics2DAdaptor) {
            return (SiriusGraphicsToGraphics2DAdaptor) internalGraphics;
        }
        return null;
    }

    private static Graphics getInternalGraphics(final ScaledGraphics graphics) {
        try {
            method = ScaledGraphics.class.getDeclaredMethod("getGraphics"); //$NON-NLS-1$
            method.setAccessible(true);
            return (Graphics) method.invoke(graphics);
        } catch (final SecurityException e) {
            // Cannot happen here
        } catch (final IllegalArgumentException e) {
            // // Cannot happen here
        } catch (final IllegalAccessException e) {
            // // Cannot happen here
        } catch (final InvocationTargetException e) {
            // // Cannot happen here
        } catch (final NoSuchMethodException e) {
            // Cannot happen here
        }
        return null;
    }

    /**
     * Get zoom fill rectangle.
     * 
     * @param graphics
     *            the graphics
     * @param bounds
     *            the bounds
     * @return the rectangle
     */
    public static Rectangle zoomFillRectangle(final Graphics graphics, Rectangle bounds) {
        try {
            zoomMethod = ScaledGraphics.class.getDeclaredMethod("zoomFillRect", int.class, int.class, int.class, int.class); //$NON-NLS-1$
            zoomMethod.setAccessible(true);
            return (Rectangle) zoomMethod.invoke(graphics, bounds.x, bounds.y, bounds.width, bounds.height);
        } catch (final SecurityException e) {
            // Cannot happen here
        } catch (final IllegalArgumentException e) {
            // // Cannot happen here
        } catch (final IllegalAccessException e) {
            // // Cannot happen here
        } catch (final InvocationTargetException e) {
            // // Cannot happen here
        } catch (final NoSuchMethodException e) {
            // Cannot happen here
        }
        return null;
    }
}

Back to the top