aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/PreviewIconFactory.java
blob: 5661b2919ee4fcb3debc9938bd17c0c3a0e9847e (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors.layout.gle2;

import static com.android.SdkConstants.DOT_PNG;
import static com.android.SdkConstants.FQCN_DATE_PICKER;
import static com.android.SdkConstants.FQCN_EXPANDABLE_LIST_VIEW;
import static com.android.SdkConstants.FQCN_LIST_VIEW;
import static com.android.SdkConstants.FQCN_TIME_PICKER;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.common.rendering.LayoutLibrary;
import com.android.ide.common.rendering.api.Capability;
import com.android.ide.common.rendering.api.RenderSession;
import com.android.ide.common.rendering.api.ResourceValue;
import com.android.ide.common.rendering.api.SessionParams.RenderingMode;
import com.android.ide.common.rendering.api.StyleResourceValue;
import com.android.ide.common.rendering.api.ViewInfo;
import com.android.ide.common.resources.ResourceResolver;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.descriptors.DocumentDescriptor;
import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor;
import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate;
import com.android.ide.eclipse.adt.internal.editors.layout.gre.PaletteMetadataDescriptor;
import com.android.ide.eclipse.adt.internal.editors.layout.gre.ViewMetadataRepository;
import com.android.ide.eclipse.adt.internal.editors.layout.gre.ViewMetadataRepository.RenderMode;
import com.android.ide.eclipse.adt.internal.editors.uimodel.UiDocumentNode;
import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
import com.android.ide.eclipse.adt.internal.resources.ResourceHelper;
import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
import com.android.sdklib.IAndroidTarget;
import com.android.utils.Pair;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.RGB;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import javax.imageio.ImageIO;

/**
 * Factory which can provide preview icons for android views of a particular SDK and
 * editor's configuration chooser
 */
public class PreviewIconFactory {
    private PaletteControl mPalette;
    private RGB mBackground;
    private RGB mForeground;
    private File mImageDir;

    private static final String PREVIEW_INFO_FILE = "preview.properties"; //$NON-NLS-1$

    public PreviewIconFactory(PaletteControl palette) {
        mPalette = palette;
    }

    /**
     * Resets the state in the preview icon factory such that it will re-fetch information
     * like the theme and SDK (the icons themselves are cached in a directory across IDE
     * session though)
     */
    public void reset() {
        mImageDir = null;
        mBackground = null;
        mForeground = null;
    }

    /**
     * Deletes all the persistent state for the current settings such that it will be regenerated
     */
    public void refresh() {
        File imageDir = getImageDir(false);
        if (imageDir != null && imageDir.exists()) {
            File[] files = imageDir.listFiles();
            for (File file : files) {
                file.delete();
            }
            imageDir.delete();
            reset();
        }
    }

    /**
     * Returns an image descriptor for the given element descriptor, or null if no image
     * could be computed. The rendering parameters (SDK, theme etc) correspond to those
     * stored in the associated palette.
     *
     * @param desc the element descriptor to get an image for
     * @return an image descriptor, or null if no image could be rendered
     */
    public ImageDescriptor getImageDescriptor(ElementDescriptor desc) {
        File imageDir = getImageDir(false);
        if (!imageDir.exists()) {
            render();
        }
        File file = new File(imageDir, getFileName(desc));
        if (file.exists()) {
            try {
                return ImageDescriptor.createFromURL(file.toURI().toURL());
            } catch (MalformedURLException e) {
                AdtPlugin.log(e, "Could not create image descriptor for %s", file);
            }
        }

        return null;
    }

    /**
     * Partition the elements in the document according to their rendering preferences;
     * elements that should be skipped are removed, elements that should be rendered alone
     * are placed in their own list, etc
     *
     * @param document the document containing render fragments for the various elements
     * @return
     */
    private List<List<Element>> partitionRenderElements(Document document) {
        List<List<Element>> elements = new ArrayList<List<Element>>();

        List<Element> shared = new ArrayList<Element>();
        Element root = document.getDocumentElement();
        elements.add(shared);

        ViewMetadataRepository repository = ViewMetadataRepository.get();

        NodeList children = root.getChildNodes();
        for (int i = 0, n = children.getLength(); i < n; i++) {
            Node node = children.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                String fqn = repository.getFullClassName(element);
                assert fqn.length() > 0 : element.getNodeName();
                RenderMode renderMode = repository.getRenderMode(fqn);

                // Temporary special cases
                if (fqn.equals(FQCN_LIST_VIEW) || fqn.equals(FQCN_EXPANDABLE_LIST_VIEW)) {
                    if (!mPalette.getEditor().renderingSupports(Capability.ADAPTER_BINDING)) {
                        renderMode = RenderMode.SKIP;
                    }
                } else if (fqn.equals(FQCN_DATE_PICKER) || fqn.equals(FQCN_TIME_PICKER)) {
                    IAndroidTarget renderingTarget = mPalette.getEditor().getRenderingTarget();
                    // In Honeycomb, these widgets only render properly in the Holo themes.
                    int apiLevel = renderingTarget.getVersion().getApiLevel();
                    if (apiLevel == 11) {
                        String themeName = mPalette.getCurrentTheme();
                        if (themeName == null || !themeName.startsWith("Theme.Holo")) { //$NON-NLS-1$
                            // Note - it's possible that the the theme is some other theme
                            // such as a user theme which inherits from Theme.Holo and that
                            // the render -would- have worked, but it's harder to detect that
                            // scenario, so we err on the side of caution and just show an
                            // icon + name for the time widgets.
                            renderMode = RenderMode.SKIP;
                        }
                    } else if (apiLevel >= 12) {
                        // Currently broken, even for Holo.
                        renderMode = RenderMode.SKIP;
                    } // apiLevel <= 10 is fine
                }

                if (renderMode == RenderMode.ALONE) {
                    elements.add(Collections.singletonList(element));
                } else if (renderMode == RenderMode.NORMAL) {
                    shared.add(element);
                } else {
                    assert renderMode == RenderMode.SKIP;
                }
            }
        }

        return elements;
    }

    /**
     * Renders ALL the widgets and then extracts image data for each view and saves it on
     * disk
     */
    private boolean render() {
        File imageDir = getImageDir(true);

        GraphicalEditorPart editor = mPalette.getEditor();
        LayoutEditorDelegate layoutEditorDelegate = editor.getEditorDelegate();
        LayoutLibrary layoutLibrary = editor.getLayoutLibrary();
        Integer overrideBgColor = null;
        if (layoutLibrary != null) {
            if (layoutLibrary.supports(Capability.CUSTOM_BACKGROUND_COLOR)) {
                Pair<RGB, RGB> themeColors = getColorsFromTheme();
                RGB bg = themeColors.getFirst();
                RGB fg = themeColors.getSecond();
                if (bg != null) {
                    storeBackground(imageDir, bg, fg);
                    overrideBgColor = Integer.valueOf(ImageUtils.rgbToInt(bg, 0xFF));
                }
            }
        }

        ViewMetadataRepository repository = ViewMetadataRepository.get();
        Document document = repository.getRenderingConfigDoc();

        if (document == null) {
            return false;
        }

        // Construct UI model from XML
        AndroidTargetData data = layoutEditorDelegate.getEditor().getTargetData();
        DocumentDescriptor documentDescriptor;
        if (data == null) {
            documentDescriptor = new DocumentDescriptor("temp", null/*children*/);//$NON-NLS-1$
        } else {
            documentDescriptor = data.getLayoutDescriptors().getDescriptor();
        }
        UiDocumentNode model = (UiDocumentNode) documentDescriptor.createUiNode();
        model.setEditor(layoutEditorDelegate.getEditor());
        model.setUnknownDescriptorProvider(editor.getModel().getUnknownDescriptorProvider());

        Element documentElement = document.getDocumentElement();
        List<List<Element>> elements = partitionRenderElements(document);
        for (List<Element> elementGroup : elements) {
            // Replace the document elements with the current element group
            while (documentElement.getFirstChild() != null) {
                documentElement.removeChild(documentElement.getFirstChild());
            }
            for (Element element : elementGroup) {
                documentElement.appendChild(element);
            }

            model.loadFromXmlNode(document);

            RenderSession session = null;
            NodeList childNodes = documentElement.getChildNodes();
            try {
                // Important to get these sizes large enough for clients that don't support
                // RenderMode.FULL_EXPAND such as 1.6
                int width = 200;
                int height = childNodes.getLength() == 1 ? 400 : 1600;

                session = RenderService.create(editor)
                    .setModel(model)
                    .setOverrideRenderSize(width, height)
                    .setRenderingMode(RenderingMode.FULL_EXPAND)
                    .setLog(editor.createRenderLogger("palette"))
                    .setOverrideBgColor(overrideBgColor)
                    .setDecorations(false)
                    .createRenderSession();
            } catch (Throwable t) {
                // If there are internal errors previewing the components just revert to plain
                // icons and labels
                continue;
            }

            if (session != null) {
                if (session.getResult().isSuccess()) {
                    BufferedImage image = session.getImage();
                    if (image != null && image.getWidth() > 0 && image.getHeight() > 0) {

                        // Fallback for older platforms where we couldn't do background rendering
                        // at the beginning of this method
                        if (mBackground == null) {
                            Pair<RGB, RGB> themeColors = getColorsFromTheme();
                            RGB bg = themeColors.getFirst();
                            RGB fg = themeColors.getSecond();

                            if (bg == null) {
                                // Just use a pixel from the rendering instead.
                                int p = image.getRGB(image.getWidth() - 1, image.getHeight() - 1);
                                // However, in this case we don't trust the foreground color
                                // even if one was found in the themes; pick one that is guaranteed
                                // to contrast with the background
                                bg = ImageUtils.intToRgb(p);
                                if (ImageUtils.getBrightness(ImageUtils.rgbToInt(bg, 255)) < 128) {
                                    fg = new RGB(255, 255, 255);
                                } else {
                                    fg = new RGB(0, 0, 0);
                                }
                            }
                            storeBackground(imageDir, bg, fg);
                            assert mBackground != null;
                        }

                        List<ViewInfo> viewInfoList = session.getRootViews();
                        if (viewInfoList != null && viewInfoList.size() > 0) {
                            // We don't render previews under a <merge> so there should
                            // only be one root.
                            ViewInfo firstRoot = viewInfoList.get(0);
                            int parentX = firstRoot.getLeft();
                            int parentY = firstRoot.getTop();
                            List<ViewInfo> infos = firstRoot.getChildren();
                            for (ViewInfo info : infos) {
                                Object cookie = info.getCookie();
                                if (!(cookie instanceof UiElementNode)) {
                                    continue;
                                }
                                UiElementNode node = (UiElementNode) cookie;
                                String fileName = getFileName(node);
                                File file = new File(imageDir, fileName);
                                if (file.exists()) {
                                    // On Windows, perhaps we need to rename instead?
                                    file.delete();
                                }
                                int x1 = parentX + info.getLeft();
                                int y1 = parentY + info.getTop();
                                int x2 = parentX + info.getRight();
                                int y2 = parentY + info.getBottom();
                                if (x1 != x2 && y1 != y2) {
                                    savePreview(file, image, x1, y1, x2, y2);
                                }
                            }
                        }
                    }
                } else {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0, n = childNodes.getLength(); i < n; i++) {
                        Node node = childNodes.item(i);
                        if (node instanceof Element) {
                            Element e = (Element) node;
                            String fqn = repository.getFullClassName(e);
                            fqn = fqn.substring(fqn.lastIndexOf('.') + 1);
                            if (sb.length() > 0) {
                                sb.append(", "); //$NON-NLS-1$
                            }
                            sb.append(fqn);
                        }
                    }
                    AdtPlugin.log(IStatus.WARNING, "Failed to render set of icons for %1$s",
                            sb.toString());

                    if (session.getResult().getException() != null) {
                        AdtPlugin.log(session.getResult().getException(),
                                session.getResult().getErrorMessage());
                    } else if (session.getResult().getErrorMessage() != null) {
                        AdtPlugin.log(IStatus.WARNING, session.getResult().getErrorMessage());
                    }
                }

                session.dispose();
            }
        }

        mPalette.getEditor().recomputeLayout();

        return true;
    }

    /**
     * Look up the background and foreground colors from the theme. May not find either
     * the background or foreground or both, but will always return a pair of possibly
     * null colors.
     *
     * @return a pair of possibly null color descriptions
     */
    @NonNull
    private Pair<RGB, RGB> getColorsFromTheme() {
        RGB background = null;
        RGB foreground = null;

        ResourceResolver resources = mPalette.getEditor().getResourceResolver();
        if (resources == null) {
            return Pair.of(background, foreground);
        }
        StyleResourceValue theme = resources.getCurrentTheme();
        if (theme != null) {
            background = resolveThemeColor(resources, "windowBackground"); //$NON-NLS-1$
            if (background == null) {
                background = renderDrawableResource("windowBackground"); //$NON-NLS-1$
                // This causes some harm with some themes: We'll find a color, say black,
                // that isn't actually rendered in the theme. Better to use null here,
                // which will cause the caller to pick a pixel from the observed background
                // instead.
                //if (background == null) {
                //    background = resolveThemeColor(resources, "colorBackground"); //$NON-NLS-1$
                //}
            }
            foreground = resolveThemeColor(resources, "textColorPrimary"); //$NON-NLS-1$
        }

        // Ensure that the foreground color is suitably distinct from the background color
        if (background != null) {
            int bgRgb = ImageUtils.rgbToInt(background, 0xFF);
            int backgroundBrightness = ImageUtils.getBrightness(bgRgb);
            if (foreground == null) {
                if (backgroundBrightness < 128) {
                    foreground = new RGB(255, 255, 255);
                } else {
                    foreground = new RGB(0, 0, 0);
                }
            } else {
                int fgRgb = ImageUtils.rgbToInt(foreground, 0xFF);
                int foregroundBrightness = ImageUtils.getBrightness(fgRgb);
                if (Math.abs(backgroundBrightness - foregroundBrightness) < 64) {
                    if (backgroundBrightness < 128) {
                        foreground = new RGB(255, 255, 255);
                    } else {
                        foreground = new RGB(0, 0, 0);
                    }
                }
            }
        }

        return Pair.of(background, foreground);
    }

    /**
     * Renders the given resource which should refer to a drawable and returns a
     * representative color value for the drawable (such as the color in the center)
     *
     * @param themeItemName the item in the theme to be looked up and rendered
     * @return a color representing a typical color in the drawable
     */
    private RGB renderDrawableResource(String themeItemName) {
        GraphicalEditorPart editor = mPalette.getEditor();
        ResourceResolver resources = editor.getResourceResolver();
        ResourceValue resourceValue = resources.findItemInTheme(themeItemName);
        BufferedImage image = RenderService.create(editor)
            .setOverrideRenderSize(100, 100)
            .renderDrawable(resourceValue);
        if (image != null) {
            // Use the middle pixel as the color since that works better for gradients;
            // solid colors work too.
            int rgb = image.getRGB(image.getWidth() / 2, image.getHeight() / 2);
            return ImageUtils.intToRgb(rgb);
        }

        return null;
    }

    private static RGB resolveThemeColor(ResourceResolver resources, String resourceName) {
        ResourceValue textColor = resources.findItemInTheme(resourceName);
        return ResourceHelper.resolveColor(resources, textColor);
    }

    private String getFileName(ElementDescriptor descriptor) {
        if (descriptor instanceof PaletteMetadataDescriptor) {
            PaletteMetadataDescriptor pmd = (PaletteMetadataDescriptor) descriptor;
            StringBuilder sb = new StringBuilder();
            String name = pmd.getUiName();
            // Strip out whitespace, parentheses, etc.
            for (int i = 0, n = name.length(); i < n; i++) {
                char c = name.charAt(i);
                if (Character.isLetter(c)) {
                    sb.append(c);
                }
            }
            return sb.toString() + DOT_PNG;
        }
        return descriptor.getUiName() + DOT_PNG;
    }

    private String getFileName(UiElementNode node) {
        ViewMetadataRepository repository = ViewMetadataRepository.get();
        String fqn = repository.getFullClassName((Element) node.getXmlNode());
        return fqn.substring(fqn.lastIndexOf('.') + 1) + DOT_PNG;
    }

    /**
     * Cleans up a name by removing punctuation and whitespace etc to make
     * it a better filename
     * @param name the name to clean
     * @return a cleaned up name
     */
    @NonNull
    private static String cleanup(@Nullable String name) {
        if (name == null) {
            return "";
        }

        // Extract just the characters (no whitespace, parentheses, punctuation etc)
        // to ensure that the filename is pretty portable
        StringBuilder sb = new StringBuilder(name.length());
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if (Character.isJavaIdentifierPart(c)) {
                sb.append(Character.toLowerCase(c));
            }
        }

        return sb.toString();
    }

    /** Returns the location of a directory containing image previews (which may not exist) */
    private File getImageDir(boolean create) {
        if (mImageDir == null) {
            // Location for plugin-related state data
            IPath pluginState = AdtPlugin.getDefault().getStateLocation();

            // We have multiple directories - one for each combination of SDK, theme and device
            // (and later, possibly other qualifiers).
            // These are created -lazily-.
            String targetName = mPalette.getCurrentTarget().hashString();
            String androidTargetNamePrefix = "android-";
            String themeNamePrefix = "Theme.";
            if (targetName.startsWith(androidTargetNamePrefix)) {
                targetName = targetName.substring(androidTargetNamePrefix.length());
            }
            String themeName = mPalette.getCurrentTheme();
            if (themeName == null) {
                themeName = "Theme"; //$NON-NLS-1$
            }
            if (themeName.startsWith(themeNamePrefix)) {
                themeName = themeName.substring(themeNamePrefix.length());
            }
            targetName = cleanup(targetName);
            themeName = cleanup(themeName);
            String deviceName = cleanup(mPalette.getCurrentDevice());
            String dirName = String.format("palette-preview-r16b-%s-%s-%s", targetName,
                    themeName, deviceName);
            IPath dirPath = pluginState.append(dirName);

            mImageDir = new File(dirPath.toOSString());
        }

        if (create && !mImageDir.exists()) {
            mImageDir.mkdirs();
        }

        return mImageDir;
    }

    private void savePreview(File output, BufferedImage image,
            int left, int top, int right, int bottom) {
        try {
            BufferedImage im = ImageUtils.subImage(image, left, top, right, bottom);
            ImageIO.write(im, "PNG", output); //$NON-NLS-1$
        } catch (IOException e) {
            AdtPlugin.log(e, "Failed writing palette file");
        }
    }

    private void storeBackground(File imageDir, RGB bg, RGB fg) {
        mBackground = bg;
        mForeground = fg;
        File file = new File(imageDir, PREVIEW_INFO_FILE);
        String colors = String.format(
                "background=#%02x%02x%02x\nforeground=#%02x%02x%02x\n", //$NON-NLS-1$
                bg.red, bg.green, bg.blue,
                fg.red, fg.green, fg.blue);
        AdtPlugin.writeFile(file, colors);
    }

    public RGB getBackgroundColor() {
        if (mBackground == null) {
            initColors();
        }

        return mBackground;
    }

    public RGB getForegroundColor() {
        if (mForeground == null) {
            initColors();
        }

        return mForeground;
    }

    public void initColors() {
        try {
            // Already initialized? Foreground can be null which would call
            // initColors again and again, but background is never null after
            // initialization so we use it as the have-initialized flag.
            if (mBackground != null) {
                return;
            }

            File imageDir = getImageDir(false);
            if (!imageDir.exists()) {
                render();

                // Initialized as part of the render
                if (mBackground != null) {
                    return;
                }
            }

            File file = new File(imageDir, PREVIEW_INFO_FILE);
            if (file.exists()) {
                Properties properties = new Properties();
                InputStream is = null;
                try {
                    is = new BufferedInputStream(new FileInputStream(file));
                    properties.load(is);
                } catch (IOException e) {
                    AdtPlugin.log(e, "Can't read preview properties");
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            // Nothing useful can be done.
                        }
                    }
                }

                String colorString = (String) properties.get("background"); //$NON-NLS-1$
                if (colorString != null) {
                    int rgb = ImageUtils.getColor(colorString.trim());
                    mBackground = ImageUtils.intToRgb(rgb);
                }
                colorString = (String) properties.get("foreground"); //$NON-NLS-1$
                if (colorString != null) {
                    int rgb = ImageUtils.getColor(colorString.trim());
                    mForeground = ImageUtils.intToRgb(rgb);
                }
            }

            if (mBackground == null) {
                mBackground = new RGB(0, 0, 0);
            }
            // mForeground is allowed to be null.
        } catch (Throwable t) {
            AdtPlugin.log(t, "Cannot initialize preview color settings");
        }
    }
}