aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/GlobalLintConfiguration.java
blob: 2f0261e7bf499b3e833e7575c742c75fa5917af7 (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
/*
 * 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.lint;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs;
import com.android.tools.lint.client.api.Configuration;
import com.android.tools.lint.client.api.IssueRegistry;
import com.android.tools.lint.detector.api.Context;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.Location;
import com.android.tools.lint.detector.api.Severity;

import org.eclipse.jface.preference.IPreferenceStore;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** Global (non-project-specific) configuration for Lint in Eclipse */
class GlobalLintConfiguration extends Configuration {
    private static final GlobalLintConfiguration sInstance = new GlobalLintConfiguration();

    private Map<Issue, Severity> mSeverities;
    private boolean mBulkEditing;

    private GlobalLintConfiguration() {
    }

    /**
     * Obtain a reference to the singleton
     *
     * @return the singleton configuration
     */
    @NonNull
    public static GlobalLintConfiguration get() {
        return sInstance;
    }

    @Override
    public Severity getSeverity(@NonNull Issue issue) {
        if (mSeverities == null) {
            IssueRegistry registry = EclipseLintClient.getRegistry();
            mSeverities = new HashMap<Issue, Severity>();
            IPreferenceStore store = getStore();
            String assignments = store.getString(AdtPrefs.PREFS_LINT_SEVERITIES);
            if (assignments != null && assignments.length() > 0) {
                for (String assignment : assignments.split(",")) { //$NON-NLS-1$
                    String[] s = assignment.split("="); //$NON-NLS-1$
                    if (s.length == 2) {
                        Issue d = registry.getIssue(s[0]);
                        if (d != null) {
                            Severity severity = Severity.valueOf(s[1]);
                            if (severity != null) {
                                mSeverities.put(d, severity);
                            }
                        }
                    }
                }
            }
        }

        Severity severity = mSeverities.get(issue);
        if (severity != null) {
            return severity;
        }

        if (!issue.isEnabledByDefault()) {
            return Severity.IGNORE;
        }

        return issue.getDefaultSeverity();
    }

    private IPreferenceStore getStore() {
        IPreferenceStore store = AdtPlugin.getDefault().getPreferenceStore();
        return store;
    }

    @Override
    public void ignore(@NonNull Context context, @NonNull Issue issue,
            @Nullable Location location, @NonNull String message) {
        throw new UnsupportedOperationException(
                "Can't ignore() in global configurations"); //$NON-NLS-1$
    }

    @Override
    public void setSeverity(@NonNull Issue issue, @Nullable Severity severity) {
        if (mSeverities == null) {
            // Force initialization
            getSeverity(issue);
        }

        if (severity == null) {
            mSeverities.remove(issue);
        } else {
            mSeverities.put(issue, severity);
        }

        if (!mBulkEditing) {
            setSeverities(mSeverities);
        }
    }

    /**
     * Sets the custom severities for the given issues, in bulk.
     *
     * @param severities a map from detector to severity to use from now on
     * @return true if something changed from the current settings
     */
    private boolean setSeverities(Map<Issue, Severity> severities) {
        mSeverities = severities;

        String value = "";
        if (severities.size() > 0) {
            List<Issue> sortedKeys = new ArrayList<Issue>(severities.keySet());
            Collections.sort(sortedKeys);

            StringBuilder sb = new StringBuilder(severities.size() * 20);
            for (Issue issue : sortedKeys) {
                Severity severity = severities.get(issue);
                if (severity != issue.getDefaultSeverity()) {
                    if (sb.length() > 0) {
                        sb.append(',');
                    }
                    sb.append(issue.getId());
                    sb.append('=');
                    sb.append(severity.name());
                }
            }

            value = sb.toString();
        }

        IPreferenceStore store = getStore();
        String previous = store.getString(AdtPrefs.PREFS_LINT_SEVERITIES);
        boolean changed = !value.equals(previous);
        if (changed) {
            if (value.length() == 0) {
                store.setToDefault(AdtPrefs.PREFS_LINT_SEVERITIES);
            } else {
                store.setValue(AdtPrefs.PREFS_LINT_SEVERITIES, value);
            }
        }

        return changed;
    }

    @Override
    public void startBulkEditing() {
        mBulkEditing = true;
    }

    @Override
    public void finishBulkEditing() {
        mBulkEditing = false;
        setSeverities(mSeverities);
    }
}