Home | History | Annotate | Download | only in lint
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.ide.eclipse.adt.internal.lint;
     17 
     18 import com.android.annotations.NonNull;
     19 import com.android.annotations.Nullable;
     20 import com.android.ide.eclipse.adt.AdtPlugin;
     21 import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs;
     22 import com.android.tools.lint.client.api.Configuration;
     23 import com.android.tools.lint.client.api.IssueRegistry;
     24 import com.android.tools.lint.detector.api.Context;
     25 import com.android.tools.lint.detector.api.Issue;
     26 import com.android.tools.lint.detector.api.Location;
     27 import com.android.tools.lint.detector.api.Severity;
     28 
     29 import org.eclipse.jface.preference.IPreferenceStore;
     30 
     31 import java.util.ArrayList;
     32 import java.util.Collections;
     33 import java.util.HashMap;
     34 import java.util.List;
     35 import java.util.Map;
     36 
     37 /** Global (non-project-specific) configuration for Lint in Eclipse */
     38 class GlobalLintConfiguration extends Configuration {
     39     private static final GlobalLintConfiguration sInstance = new GlobalLintConfiguration();
     40 
     41     private Map<Issue, Severity> mSeverities;
     42     private boolean mBulkEditing;
     43 
     44     private GlobalLintConfiguration() {
     45     }
     46 
     47     /**
     48      * Obtain a reference to the singleton
     49      *
     50      * @return the singleton configuration
     51      */
     52     @NonNull
     53     public static GlobalLintConfiguration get() {
     54         return sInstance;
     55     }
     56 
     57     @Override
     58     public Severity getSeverity(@NonNull Issue issue) {
     59         if (mSeverities == null) {
     60             IssueRegistry registry = EclipseLintClient.getRegistry();
     61             mSeverities = new HashMap<Issue, Severity>();
     62             IPreferenceStore store = getStore();
     63             String assignments = store.getString(AdtPrefs.PREFS_LINT_SEVERITIES);
     64             if (assignments != null && assignments.length() > 0) {
     65                 for (String assignment : assignments.split(",")) { //$NON-NLS-1$
     66                     String[] s = assignment.split("="); //$NON-NLS-1$
     67                     if (s.length == 2) {
     68                         Issue d = registry.getIssue(s[0]);
     69                         if (d != null) {
     70                             Severity severity = Severity.valueOf(s[1]);
     71                             if (severity != null) {
     72                                 mSeverities.put(d, severity);
     73                             }
     74                         }
     75                     }
     76                 }
     77             }
     78         }
     79 
     80         Severity severity = mSeverities.get(issue);
     81         if (severity != null) {
     82             return severity;
     83         }
     84 
     85         if (!issue.isEnabledByDefault()) {
     86             return Severity.IGNORE;
     87         }
     88 
     89         return issue.getDefaultSeverity();
     90     }
     91 
     92     private IPreferenceStore getStore() {
     93         IPreferenceStore store = AdtPlugin.getDefault().getPreferenceStore();
     94         return store;
     95     }
     96 
     97     @Override
     98     public void ignore(@NonNull Context context, @NonNull Issue issue,
     99             @Nullable Location location, @NonNull String message,
    100             @Nullable Object data) {
    101         throw new UnsupportedOperationException(
    102                 "Can't ignore() in global configurations"); //$NON-NLS-1$
    103     }
    104 
    105     @Override
    106     public void setSeverity(@NonNull Issue issue, @Nullable Severity severity) {
    107         if (mSeverities == null) {
    108             // Force initialization
    109             getSeverity(issue);
    110         }
    111 
    112         if (severity == null) {
    113             mSeverities.remove(issue);
    114         } else {
    115             mSeverities.put(issue, severity);
    116         }
    117 
    118         if (!mBulkEditing) {
    119             setSeverities(mSeverities);
    120         }
    121     }
    122 
    123     /**
    124      * Sets the custom severities for the given issues, in bulk.
    125      *
    126      * @param severities a map from detector to severity to use from now on
    127      * @return true if something changed from the current settings
    128      */
    129     private boolean setSeverities(Map<Issue, Severity> severities) {
    130         mSeverities = severities;
    131 
    132         String value = "";
    133         if (severities.size() > 0) {
    134             List<Issue> sortedKeys = new ArrayList<Issue>(severities.keySet());
    135             Collections.sort(sortedKeys);
    136 
    137             StringBuilder sb = new StringBuilder(severities.size() * 20);
    138             for (Issue issue : sortedKeys) {
    139                 Severity severity = severities.get(issue);
    140                 if (severity != issue.getDefaultSeverity()) {
    141                     if (sb.length() > 0) {
    142                         sb.append(',');
    143                     }
    144                     sb.append(issue.getId());
    145                     sb.append('=');
    146                     sb.append(severity.name());
    147                 }
    148             }
    149 
    150             value = sb.toString();
    151         }
    152 
    153         IPreferenceStore store = getStore();
    154         String previous = store.getString(AdtPrefs.PREFS_LINT_SEVERITIES);
    155         boolean changed = !value.equals(previous);
    156         if (changed) {
    157             if (value.length() == 0) {
    158                 store.setToDefault(AdtPrefs.PREFS_LINT_SEVERITIES);
    159             } else {
    160                 store.setValue(AdtPrefs.PREFS_LINT_SEVERITIES, value);
    161             }
    162         }
    163 
    164         return changed;
    165     }
    166 
    167     @Override
    168     public void startBulkEditing() {
    169         mBulkEditing = true;
    170     }
    171 
    172     @Override
    173     public void finishBulkEditing() {
    174         mBulkEditing = false;
    175         setSeverities(mSeverities);
    176     }
    177 }