summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/DesignerPlugin.java
blob: 564a068dd26526950c386c9560ef208ba53a6167 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * 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:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.wb.internal.core;

import com.google.common.collect.Maps;
import com.google.common.io.CharStreams;
import com.google.common.io.Closeables;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import java.io.Closeable;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * The DesignerPlugin class is the "nexus" of the propertysheet. In WindowBuilder,
 * it's the plugin activator, and contains a number of important utility methods, such
 * as resource loading, logging, obtaining a display and shell, etc.
 * <p>
 * In the AOSP fork, most of the functionality has been ripped out, except for the
 * above mentioned pieces, and this class is no longer a plugin. Instead, it *delegates*
 * to the plugin which initializes it via the {@link #initialize} method for things
 * like logging. For things like image loading, it has its own local code such that
 * it can find its image resources locally instead of requiring the embedding plugin
 * to copy the images into its own jar.
 * <p>
 * "DesignerPlugin" is not a very good name for this class since it is not a plugin,
 * but it was left that way to avoid modifying all the various propertysheet classes;
 * we'd like to keep those as unmodified as possible to make absorbing future
 * WindowBuilder improvements as easy as possible.
 */
public class DesignerPlugin {
    private static AbstractUIPlugin sPlugin;
    private static String sPluginId;

    /**
     * Initialize the property sheet for use in the ADT plugin
     *
     * @param hostPlugin the plugin to embed the property sheet
     * @param pluginId the id of the plugin to use in status messages etc
     * @param isWindows whether we're running on Windows
     * @param isMac whether we're running on Mac
     * @param isLinux whether we're running on Linux
     */
    public static void initialize(AbstractUIPlugin hostPlugin, String pluginId,
            boolean isWindows, boolean isMac, boolean isLinux) {
        assert sPlugin == null; // Can only be used by one client in the same classloader
        sPlugin = hostPlugin;
        sPluginId = pluginId;
        EnvironmentUtils.IS_WINDOWS = isWindows;
        EnvironmentUtils.IS_MAC = isMac;
        EnvironmentUtils.IS_LINUX = isLinux;
    }

    /**
     * Dispose the propertysheet library: free up images from the cache, unregister the
     * plugin reference etc.
     */
    public static void dispose() {
        sPlugin = null;
        for (Image image : sImageCache.values()) {
            image.dispose();
        }
        sImageCache.clear();
        sDescriptorCache.clear();
    }

    /**
     * Reads the contents of an {@link InputStreamReader} using the default
     * platform encoding and return it as a String. This method will close the
     * input stream.
     *
     * @param inputStream the input stream to be read from
     * @param charset the charset to use
     * @return the String read from the stream, or null if there was an error
     */
    public static String readFile(InputStream inputStream, Charset charset) {
        if (inputStream == null) {
            return null;
        }
        Closeable closeMe = inputStream;
        try {
            final InputStreamReader isr = new InputStreamReader(inputStream, charset);
            closeMe = isr;
            try {
                return CharStreams.toString(isr);
            } catch (Exception ioe) {
                // pass -- ignore files we can't read
                return null;
            }
        } finally {
            Closeables.closeQuietly(closeMe);
        }
    }

    /**
     * @return the instance of {@link DesignerPlugin}
     */
    public static AbstractUIPlugin getDefault() {
        assert sPlugin != null;
        return sPlugin;
    }

    // //////////////////////////////////////////////////////////////////////////
    //
    // Display/Shell
    //
    // //////////////////////////////////////////////////////////////////////////
    /**
     * @return the {@link Display} instance, current (if in GUI thread) or
     *         default.
     */
    public static Display getStandardDisplay() {
        Display display = Display.getCurrent();
        if (display == null) {
            display = Display.getDefault();
        }
        return display;
    }

    /**
     * @return the active {@link IWorkbenchWindow}.
     */
    public static IWorkbenchWindow getActiveWorkbenchWindow() {
        return getDefault().getWorkbench().getActiveWorkbenchWindow();
    }

    /**
     * @return the {@link Shell} of active {@link IWorkbenchWindow}.
     */
    public static Shell getShell() {
        if (getActiveWorkbenchWindow() != null) {
            return getActiveWorkbenchWindow().getShell();
        }
        return null;
    }

    /**
     * Logs given {@link IStatus} into Eclipse .log.
     */
    public static void log(IStatus status) {
        getDefault().getLog().log(status);
    }

    /**
     * Logs {@link IStatus} with given message into Eclipse .log.
     */
    public static void log(String message) {
        log(new Status(IStatus.INFO, sPluginId, IStatus.INFO, message, null));
    }

    /**
     * Logs {@link IStatus} with given exception into Eclipse .log.
     */
    public static void log(Throwable e) {
        Status status = new Status(IStatus.ERROR, sPluginId, "", e);
        getDefault().getLog().log(status);
    }

    /**
     * Logs {@link IStatus} with given message and exception into Eclipse .log.
     */
    public static void log(String message, Throwable e) {
        log(createStatus(message, e));
    }

    /**
     * Creates {@link IStatus} for given message and exception.
     */
    public static Status createStatus(String message, Throwable e) {
        return new Status(IStatus.ERROR, "wb", IStatus.ERROR, message, e) {
            @Override
            public boolean isMultiStatus() {
                return true;
            }
        };
    }

    // //////////////////////////////////////////////////////////////////////////
    //
    // Resources
    //
    // //////////////////////////////////////////////////////////////////////////
    private static Map<String, ImageDescriptor> sDescriptorCache = Maps.newHashMap();
    private static Map<String, Image> sImageCache = Maps.newHashMap();

    public static Image getImage(String path) {
        Image image = sImageCache.get(path);
        if (image == null) {
            ImageDescriptor descriptor = getImageDescriptor(path);
            if (descriptor != null) {
                return descriptor.createImage();
            }
            sImageCache.put(path, image);
        }
        return image;
    }

    public static ImageDescriptor getImageDescriptor(String path) {
        ImageDescriptor descriptor = sDescriptorCache.get(path);
        if (descriptor == null) {
            URL url = DesignerPlugin.class.getResource("icons/" + path); //$NON-NLS-1$
            if (url != null) {
                descriptor = ImageDescriptor.createFromURL(url);
                sDescriptorCache.put(path, descriptor);
            }
        }
        return descriptor;
    }
}