aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LintTooltip.java
blob: cedd436598a58fe5a4a03b7de25ff46f6dc6ee24 (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
/*
 * Copyright (C) 2012 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.ATTR_ID;

import com.android.ide.common.layout.BaseLayoutRule;
import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate;
import com.android.ide.eclipse.adt.internal.editors.layout.uimodel.UiViewElementNode;

import org.eclipse.core.resources.IMarker;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import java.util.List;

/** Actual tooltip showing multiple lines for various widgets that have lint errors */
class LintTooltip extends Shell {
    private final LayoutCanvas mCanvas;
    private final List<UiViewElementNode> mNodes;

    LintTooltip(LayoutCanvas canvas, List<UiViewElementNode> nodes) {
        super(canvas.getDisplay(), SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
        mCanvas = canvas;
        mNodes = nodes;

        createContents();
    }

    protected void createContents() {
        Display display = getDisplay();
        Color fg = display.getSystemColor(SWT.COLOR_INFO_FOREGROUND);
        Color bg = display.getSystemColor(SWT.COLOR_INFO_BACKGROUND);
        setBackground(bg);
        GridLayout gridLayout = new GridLayout(2, false);
        setLayout(gridLayout);

        LayoutEditorDelegate delegate = mCanvas.getEditorDelegate();

        boolean first = true;
        for (UiViewElementNode node : mNodes) {
            IMarker marker = delegate.getIssueForNode(node);
            if (marker != null) {
                String message = marker.getAttribute(IMarker.MESSAGE, null);
                if (message != null) {
                    Label icon = new Label(this, SWT.NONE);
                    icon.setForeground(fg);
                    icon.setBackground(bg);
                    icon.setImage(node.getIcon());

                    Label label = new Label(this, SWT.WRAP);
                    if (first) {
                        label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
                        first = false;
                    }

                    String id = BaseLayoutRule.stripIdPrefix(node.getAttributeValue(ATTR_ID));
                    if (id.isEmpty()) {
                        if (node.getXmlNode() != null) {
                            id = node.getXmlNode().getNodeName();
                        } else {
                            id = node.getDescriptor().getUiName();
                        }
                    }

                    label.setText(String.format("%1$s: %2$s", id, message));
                }
            }
        }
    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }
}