|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.netbeans.spi.editor.highlighting.support; |
| 20 | + |
| 21 | +import java.util.prefs.PreferenceChangeEvent; |
| 22 | +import java.util.prefs.PreferenceChangeListener; |
| 23 | +import java.util.prefs.Preferences; |
| 24 | +import javax.swing.text.Document; |
| 25 | +import org.netbeans.api.editor.mimelookup.MimeLookup; |
| 26 | +import org.netbeans.lib.editor.util.swing.DocumentUtilities; |
| 27 | +import org.netbeans.spi.editor.highlighting.HighlightsContainer; |
| 28 | +import org.netbeans.spi.editor.highlighting.HighlightsSequence; |
| 29 | +import org.openide.util.WeakListeners; |
| 30 | + |
| 31 | +public class HighlightsContainers { |
| 32 | + public static HighlightsContainer inlineHintsSettingAwareContainer(Document doc, HighlightsContainer delegate) { |
| 33 | + class InlineHintsSettingsAwareContainer extends AbstractHighlightsContainer implements PreferenceChangeListener { |
| 34 | + private static final String KEY_ENABLE_INLINE_HINTS = "enable.inline.hints"; |
| 35 | + |
| 36 | + private final Preferences prefs; |
| 37 | + private boolean enabled; |
| 38 | + |
| 39 | + public InlineHintsSettingsAwareContainer() { |
| 40 | + String mimeType = DocumentUtilities.getMimeType(doc); |
| 41 | + prefs = MimeLookup.getLookup(mimeType).lookup(Preferences.class); |
| 42 | + prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, this, prefs)); |
| 43 | + inlineSettingChanged(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public HighlightsSequence getHighlights(int startOffset, int endOffset) { |
| 48 | + synchronized (this) { |
| 49 | + if (!enabled) { |
| 50 | + return HighlightsSequence.EMPTY; |
| 51 | + } |
| 52 | + } |
| 53 | + return delegate.getHighlights(startOffset, endOffset); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void preferenceChange(PreferenceChangeEvent evt) { |
| 58 | + if (KEY_ENABLE_INLINE_HINTS.equals(evt.getKey())) { |
| 59 | + inlineSettingChanged(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void inlineSettingChanged() { |
| 64 | + synchronized (this) { |
| 65 | + boolean newValue = prefs.getBoolean(KEY_ENABLE_INLINE_HINTS, false); |
| 66 | + |
| 67 | + if (enabled == newValue) { |
| 68 | + return ; |
| 69 | + } |
| 70 | + |
| 71 | + enabled = newValue; |
| 72 | + } |
| 73 | + |
| 74 | + fireHighlightsChange(0, doc.getLength()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return new InlineHintsSettingsAwareContainer(); |
| 79 | + } |
| 80 | +} |
0 commit comments