Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1a9b6ce3feedb2c3bbf6556c8d75b4d6c7b7a0b4 (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
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
/*******************************************************************************
 * Copyright (c) 2007 Pascal Essiembre.
 * 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:
 *    Pascal Essiembre - initial API and implementation
 ******************************************************************************/
package org.eclipse.babel.editor.tree.internal;

import java.util.Collection;

import org.eclipse.babel.core.message.checks.IMessageCheck;
import org.eclipse.babel.core.message.internal.MessagesBundleGroup;
import org.eclipse.babel.core.message.tree.IKeyTreeNode;
import org.eclipse.babel.core.message.tree.internal.AbstractKeyTreeModel;
import org.eclipse.babel.core.message.tree.internal.KeyTreeNode;
import org.eclipse.babel.editor.internal.MessagesEditor;
import org.eclipse.babel.editor.internal.MessagesEditorMarkers;
import org.eclipse.babel.editor.util.OverlayImageIcon;
import org.eclipse.babel.editor.util.UIUtils;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.DecorationOverlayIcon;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;

/**
 * Label provider for key tree viewer.
 * 
 * @author Pascal Essiembre
 */
public class KeyTreeLabelProvider extends ColumnLabelProvider implements
        IFontProvider, IColorProvider {

    private static final int KEY_DEFAULT = 1 << 1;
    private static final int KEY_COMMENTED = 1 << 2;
    private static final int KEY_VIRTUAL = 1 << 3;
    private static final int BADGE_WARNING = 1 << 4;
    private static final int BADGE_WARNING_GREY = 1 << 5;

    /** Registry instead of UIUtils one for image not keyed by file name. */
    private static ImageRegistry imageRegistry = new ImageRegistry();

    private MessagesEditor editor;
    private MessagesBundleGroup messagesBundleGroup;

    /**
     * This label provider keeps a reference to the content provider. This is
     * only because the way the nodes are labeled depends on whether the node is
     * being displayed in a tree or a flat structure.
     * <P>
     * This label provider does not have to listen to changes in the tree/flat
     * selection because such a change would cause the content provider to do a
     * full refresh anyway.
     */
    private KeyTreeContentProvider contentProvider;

    /**
     * 
     */
    public KeyTreeLabelProvider(MessagesEditor editor,
            AbstractKeyTreeModel treeModel,
            KeyTreeContentProvider contentProvider) {
        super();
        this.editor = editor;
        this.messagesBundleGroup = editor.getBundleGroup();
        this.contentProvider = contentProvider;
    }

    /**
     * @see ILabelProvider#getImage(Object)
     */
    public Image getImage(Object element) {
        if (element instanceof KeyTreeNode) {
            KeyTreeNode node = (KeyTreeNode) element;
            Collection<IMessageCheck> c = editor.getMarkers().getFailedChecks(
                    node.getMessageKey());
            if (c == null || c.isEmpty()) {
                // Return the default key image as no issue exists
                return UIUtils.getKeyImage();
            }
            if (editor.getMarkers().isUnusedKey(node.getMessageKey(), false)) {
                if (editor.getMarkers().isMissingKey(node.getMessageKey())) {
                    return UIUtils.getMissingAndUnusedTranslationsImage();
                } else if (editor.getMarkers().isDuplicateValue(
                        node.getMessageKey())) {
                    return UIUtils
                            .getDuplicateEntryAndUnusedTranslationsImage();
                }
                return UIUtils.getUnusedTranslationsImage();
            } else if (editor.getMarkers().isMissingKey(node.getMessageKey())) {
                return UIUtils.getMissingTranslationImage();
            } else if (editor.getMarkers().isDuplicateValue(
                    node.getMessageKey())) {
                return UIUtils.getDuplicateEntryImage();
            }

            // This shouldnt happen, but just in case a default key with a
            // warning icon will be showed
            Image someWarning = UIUtils.getKeyImage();
            ImageDescriptor warning = ImageDescriptor.createFromImage(UIUtils
                    .getImage(UIUtils.IMAGE_WARNING));
            someWarning = new DecorationOverlayIcon(someWarning, warning,
                    IDecoration.BOTTOM_RIGHT).createImage();
            return someWarning;
            // return UIUtils.getImage(UIUtils.IMAGE_WARNED_TRANSLATION);
        } else {
            /*
             * // Figure out background icon if
             * (messagesBundleGroup.isMessageKey(key)) { //TODO create check (or
             * else) // if (!noInactiveKeyCheck.checkKey(messagesBundleGroup,
             * node.getPath())) { // iconFlags += KEY_COMMENTED; // } else {
             * iconFlags += KEY_DEFAULT;
             * 
             * // } } else { iconFlags += KEY_VIRTUAL; }
             */

            return UIUtils.getKeyImage();

        }
    }

    /**
     * @see ILabelProvider#getText(Object)
     */
    public String getText(Object element) {
        /*
         * We look to the content provider to see if the node is being displayed
         * in flat or tree mode.
         */
        KeyTreeNode node = (KeyTreeNode) element;
        switch (contentProvider.getTreeType()) {
        case Tree:
            return node.getName();
        case Flat:
            return node.getMessageKey();
        default:
            // Should not happen
            return "error";
        }
    }

    public String getToolTipText(Object element) {
        if (element instanceof KeyTreeNode) {
            KeyTreeNode node = (KeyTreeNode) element;
            Collection<IMessageCheck> c = editor.getMarkers().getFailedChecks(
                    node.getMessageKey());
            if (c == null || c.isEmpty()) {
                return null;
            }
            boolean isMissingOrUnused = editor.getMarkers()
                    .isMissingOrUnusedKey(node.getMessageKey());
            if (isMissingOrUnused) {
                if (editor.getMarkers().isUnusedKey(node.getMessageKey(),
                        isMissingOrUnused)) {
                    return "This Locale is unused";
                } else {
                    return "This Locale has missing translations";
                }
            }
            if (editor.getMarkers().isDuplicateValue(node.getMessageKey())) {
                return "This Locale has a duplicate value";
            }
        }
        return null;
    }

    /**
     * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
     */
    public void dispose() {
        // TODO imageRegistry.dispose(); could do if version 3.1
    }

    /**
     * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
     */
    public Font getFont(Object element) {
        return null;
    }

    /**
     * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
     */
    public Color getForeground(Object element) {
        return null;
    }

    /**
     * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
     */
    public Color getBackground(Object element) {
        return null;
    }

    /**
     * Generates an image based on icon flags.
     * 
     * @param iconFlags
     * @return generated image
     */
    private Image generateImage(int iconFlags) {
        Image image = imageRegistry.get("" + iconFlags); //$NON-NLS-1$
        if (image == null) {
            // Figure background image
            if ((iconFlags & KEY_COMMENTED) != 0) {
                image = getRegistryImage("keyCommented.png"); //$NON-NLS-1$
            } else if ((iconFlags & KEY_VIRTUAL) != 0) {
                image = getRegistryImage("keyVirtual.png"); //$NON-NLS-1$
            } else {
                image = getRegistryImage("keyDefault.png"); //$NON-NLS-1$
            }

            // Add warning icon
            if ((iconFlags & BADGE_WARNING) != 0) {
                image = overlayImage(image, "warning.gif", //$NON-NLS-1$
                        OverlayImageIcon.BOTTOM_RIGHT, iconFlags);
            } else if ((iconFlags & BADGE_WARNING_GREY) != 0) {
                image = overlayImage(image, "warningGrey.gif", //$NON-NLS-1$
                        OverlayImageIcon.BOTTOM_RIGHT, iconFlags);
            }
        }
        return image;
    }

    private Image overlayImage(Image baseImage, String imageName, int location,
            int iconFlags) {
        /*
         * To obtain a unique key, we assume here that the baseImage and
         * location are always the same for each imageName and keyFlags
         * combination.
         */
        String imageKey = imageName + iconFlags;
        Image image = imageRegistry.get(imageKey);
        if (image == null) {
            image = new OverlayImageIcon(baseImage,
                    getRegistryImage(imageName), location).createImage();
            imageRegistry.put(imageKey, image);
        }
        return image;
    }

    private Image getRegistryImage(String imageName) {
        Image image = imageRegistry.get(imageName);
        if (image == null) {
            image = UIUtils.getImageDescriptor(imageName).createImage();
            imageRegistry.put(imageName, image);
        }
        return image;
    }

    private boolean isOneChildrenMarked(IKeyTreeNode parentNode) {
        MessagesEditorMarkers markers = editor.getMarkers();
        IKeyTreeNode[] childNodes = editor.getKeyTreeModel().getChildren(
                parentNode);
        for (int i = 0; i < childNodes.length; i++) {
            IKeyTreeNode node = childNodes[i];
            if (markers.isMarked(node.getMessageKey())) {
                return true;
            }
            if (isOneChildrenMarked(node)) {
                return true;
            }
        }
        return false;
    }

}

Back to the top