diff --git a/editor.settings.storage/apichanges.xml b/editor.settings.storage/apichanges.xml --- a/editor.settings.storage/apichanges.xml +++ b/editor.settings.storage/apichanges.xml @@ -108,6 +108,26 @@ + Implement profiles for annotations coloring + + + + + +

+ EditorSettings is extended to handle saving and loading annotations colorings for any profile. +

+
+                    Class org.netbeans.modules.editor.settings.storage.api.EditorSettings
+  [sigtest]   "E5.2 - Adding abstract methods" : method public abstract void org.netbeans.modules.editor.settings.storage.api.EditorSettings.setAnnotations(java.lang.String,java.util.Map)
+  [sigtest]   "E5.2 - Adding abstract methods" : method public abstract java.util.Map org.netbeans.modules.editor.settings.storage.api.EditorSettings.getAnnotations(java.lang.String)
+  [sigtest]   "E5.2 - Adding abstract methods" : method public abstract java.util.Map org.netbeans.modules.editor.settings.storage.api.EditorSettings.getAnnotationDefaults(java.lang.String)
+  [sigtest] 
+                
+
+ +
+ Detect preferences override and support inheritance diff --git a/editor.settings.storage/manifest.mf b/editor.settings.storage/manifest.mf --- a/editor.settings.storage/manifest.mf +++ b/editor.settings.storage/manifest.mf @@ -2,6 +2,6 @@ OpenIDE-Module: org.netbeans.modules.editor.settings.storage/1 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/editor/settings/storage/Bundle.properties OpenIDE-Module-Provides: org.netbeans.api.editor.settings.implementation -OpenIDE-Module-Specification-Version: 1.42 +OpenIDE-Module-Specification-Version: 1.43 OpenIDE-Module-Layer: org/netbeans/modules/editor/settings/storage/layer.xml AutoUpdate-Show-In-Client: false diff --git a/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/EditorSettingsImpl.java b/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/EditorSettingsImpl.java --- a/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/EditorSettingsImpl.java +++ b/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/EditorSettingsImpl.java @@ -82,6 +82,9 @@ /** The name of the property change event for 'Highlighting' font and colors. */ public static final String PROP_HIGHLIGHT_COLORINGS = "editorFontColors"; //NOI18N + + /** The name of the property change event for 'Annotation' font and colors. */ + public static final String PROP_ANNOTATION_COLORINGS = "editorAnnotationFontColors"; //NOI18N /** The name of the property change event for 'Token' font and colors. */ public static final String PROP_TOKEN_COLORINGS = "fontColors"; //NOI18N @@ -281,7 +284,7 @@ } private final Map> highlightings = new HashMap>(); - private final StorageImpl highlightingsStorage = new StorageImpl(new ColoringStorage(false), null); + private final StorageImpl highlightingsStorage = new StorageImpl(new ColoringStorage(ColoringStorage.FAV_HIGHLIGHT), null); /** * Returns highlighting properties for given profile or null, if the @@ -395,6 +398,105 @@ pcs.firePropertyChange(PROP_HIGHLIGHT_COLORINGS, MimePath.EMPTY, profile); } + private final Map> annotations = new HashMap<>(); + private final StorageImpl annotationsStorage = new StorageImpl<>(new ColoringStorage(ColoringStorage.FAV_ANNOTATION), null); + + @Override + public Map getAnnotations(String profile) { + boolean specialProfile = profile.startsWith("test"); //NOI18N + profile = FontColorSettingsImpl.get(MimePath.EMPTY).getInternalFontColorProfile(profile); + + if (!annotations.containsKey(profile)) { + Map profileColorings = null; + + try { + profileColorings = annotationsStorage.load( + MimePath.EMPTY, + specialProfile ? DEFAULT_PROFILE : profile, + false + ); + } catch (IOException ioe) { + LOG.log(Level.WARNING, null, ioe); + } + + Map defaultProfileColorings = null; + if (!specialProfile && !DEFAULT_PROFILE.equals(profile)) { + try { + defaultProfileColorings = annotationsStorage.load( + MimePath.EMPTY, + DEFAULT_PROFILE, + false + ); + } catch (IOException ioe) { + LOG.log(Level.WARNING, null, ioe); + } + } + + // Add colorings from the default profile that do not exist in + // the profileColorings. They are normally the same, but when + // imported from previous version some colorings can be missing. + // See #119709 + Map m = new HashMap<>(); + if (defaultProfileColorings != null) { + m.putAll(defaultProfileColorings); + } + if (profileColorings != null) { + m.putAll(profileColorings); + } + + //todo prepare annotations. + profileColorings = Collections.unmodifiableMap(m); + + annotations.put(profile, profileColorings); + } + + Map h = annotations.get(profile); + return h == null ? Collections.emptyMap() : h; + } + + @Override + public Map getAnnotationDefaults(String profile) { + profile = FontColorSettingsImpl.get(MimePath.EMPTY).getInternalFontColorProfile(profile); + try { + return annotationsStorage.load(MimePath.EMPTY, profile, true); + } catch (IOException ioe) { + LOG.log(Level.WARNING, null, ioe); + return Collections.emptyMap(); + } + } + + @Override + public void setAnnotations( + String profile, + Map fontColors + ) { + boolean specialProfile = profile.startsWith("test"); //NOI18N + profile = FontColorSettingsImpl.get(MimePath.EMPTY).getInternalFontColorProfile(profile); + + if (fontColors == null) { + try { + annotationsStorage.delete(MimePath.EMPTY, profile, false); + } catch (IOException ioe) { + LOG.log(Level.WARNING, null, ioe); + } + annotations.remove(profile); + } else { + Map m = Utils.immutize(fontColors); + + // 3) save new values to disk + if (!specialProfile) { + try { + annotationsStorage.save(MimePath.EMPTY, profile, false, m); + } catch (IOException ioe) { + LOG.log(Level.WARNING, null, ioe); + } + } + + annotations.put(profile, m); + } + + pcs.firePropertyChange(PROP_ANNOTATION_COLORINGS, MimePath.EMPTY, profile); + } // ------------------------------------------------------ // Keybindings diff --git a/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/api/EditorSettings.java b/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/api/EditorSettings.java --- a/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/api/EditorSettings.java +++ b/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/api/EditorSettings.java @@ -227,6 +227,39 @@ ); /** + * Returns annotations properties for given profile or null, if the + * profile is not known. + * + * @param profile a profile name + * @return annotations properties for given profile or null + */ + public abstract Map getAnnotations ( + String profile + ); + + /** + * Returns defaults for annotation properties for given profile, + * or null if the profile is not known. + * + * @param profile a profile name + * @return annotation properties for given profile or null + */ + public abstract Map getAnnotationDefaults ( + String profile + ); + + /** + * Sets annotation properties for given profile. + * + * @param profile a profile name + * @param annotations a annotation properties to be used + */ + public abstract void setAnnotations ( + String profile, + Map annotations + ); + + /** * Returns FontColorSettings for given mimetypes. * * @param mimeTypes The mime path to get the settings for. diff --git a/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/fontscolors/ColoringStorage.java b/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/fontscolors/ColoringStorage.java --- a/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/fontscolors/ColoringStorage.java +++ b/editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/fontscolors/ColoringStorage.java @@ -87,8 +87,8 @@ public static final String ID = "FontsColors"; //NOI18N /* test */ static final String MIME_TYPE = "text/x-nbeditor-fontcolorsettings"; //NOI18N - public ColoringStorage(boolean tokenColoringStorage) { - this.tokenColoringStorage = tokenColoringStorage; + public ColoringStorage(String type) { + this.type = type; } // --------------------------------------------------------- @@ -96,7 +96,7 @@ // --------------------------------------------------------- public ColoringStorage() { - this(true); + this(FAV_TOKEN); } public String getId() { @@ -168,8 +168,7 @@ boolean legacyFile = ((Boolean) info[4]).booleanValue(); // Skip files with wrong type of colorings - boolean isTokenColoringFile = isTokenColoringFile(settingFile); - if (isTokenColoringFile != tokenColoringStorage) { + if (!type.equals(getColoringFileType(settingFile))) { continue; } @@ -180,11 +179,15 @@ // Process loaded colorings for(SimpleAttributeSet as : sets.values()) { + if (FAV_ANNOTATION.equals(type)) { + fontsColorsMap.put((String) as.getAttribute(StyleConstants.NameAttribute), as); + continue; + } String name = (String) as.getAttribute(StyleConstants.NameAttribute); String translatedName = null; SimpleAttributeSet previous = fontsColorsMap.get(name); - if (previous == null && !modulesFile && tokenColoringStorage) { + if (previous == null && !modulesFile && FAV_TOKEN.equals(type)) { // User files normally don't define extra colorings unless // for example loading a settings file from an older version // of Netbeans (or in a completely new profile!!). In this case @@ -269,14 +272,14 @@ final String settingFileName = SettingsType.getLocator(this).getWritableFileName( mimePath.getPath(), profile, - tokenColoringStorage ? "-tokenColorings" : "-highlights", //NOI18N + FAV_TOKEN.equals(type) ? "-tokenColorings" : FAV_HIGHLIGHT.equals(type) ? "-highlights" : "-annotations", //NOI18N defaults); FileUtil.runAtomicAction(new FileSystem.AtomicAction() { public void run() throws IOException { FileObject baseFolder = FileUtil.getConfigFile("Editors"); //NOI18N FileObject f = FileUtil.createData(baseFolder, settingFileName); - f.setAttribute(FA_TYPE, tokenColoringStorage ? FAV_TOKEN : FAV_HIGHLIGHT); + f.setAttribute(FA_TYPE, type); Map added = new HashMap(); Map removed = new HashMap(); @@ -311,8 +314,7 @@ FileObject settingFile = (FileObject) info[1]; // Skip files with wrong type of colorings - boolean isTokenColoringFile = isTokenColoringFile(settingFile); - if (isTokenColoringFile != tokenColoringStorage) { + if (!type.equals(getColoringFileType(settingFile))) { continue; } @@ -350,12 +352,13 @@ private static final String SYSTEM_ID = "http://www.netbeans.org/dtds/EditorFontsColors-1_1.dtd"; //NOI18N private static final String FA_TYPE = "nbeditor-settings-ColoringType"; //NOI18N - private static final String FAV_TOKEN = "token"; //NOI18N - private static final String FAV_HIGHLIGHT = "highlight"; //NOI18N + public static final String FAV_TOKEN = "token"; //NOI18N + public static final String FAV_HIGHLIGHT = "highlight"; //NOI18N + public static final String FAV_ANNOTATION = "annotation"; //NOI18N private static final Object ATTR_MODULE_SUPPLIED = new Object(); - private final boolean tokenColoringStorage; + private final String type; private static String findDisplayName(String name, FileObject settingFile, List filesForLocalization) { // Try the settingFile first @@ -584,13 +587,17 @@ return doc; } } // End of ColoringsWriter class - - private static boolean isTokenColoringFile(FileObject f) { + + private static String getColoringFileType(FileObject f) { Object typeValue = f.getAttribute(FA_TYPE); if (typeValue instanceof String) { - return typeValue.equals(FAV_TOKEN); + return (String)typeValue; } else { - return !f.getNameExt().equals(HIGHLIGHTING_FILE_NAME); + if (f.getNameExt().equals(HIGHLIGHTING_FILE_NAME)) { + return FAV_HIGHLIGHT; + } else { + return FAV_TOKEN; + } } } diff --git a/editor.settings.storage/test/unit/src/org/netbeans/modules/editor/settings/storage/fontscolors/ColoringStorageTest.java b/editor.settings.storage/test/unit/src/org/netbeans/modules/editor/settings/storage/fontscolors/ColoringStorageTest.java --- a/editor.settings.storage/test/unit/src/org/netbeans/modules/editor/settings/storage/fontscolors/ColoringStorageTest.java +++ b/editor.settings.storage/test/unit/src/org/netbeans/modules/editor/settings/storage/fontscolors/ColoringStorageTest.java @@ -118,7 +118,7 @@ } public void testAllLanguagesHighlights() { - ColoringStorage cs = new ColoringStorage(false); + ColoringStorage cs = new ColoringStorage(ColoringStorage.FAV_HIGHLIGHT); Map colorings = cs.load(MimePath.EMPTY, "MyProfileXyz", true); //NOI18N assertNotNull("Colorings map should not be null", colorings); assertEquals("Wrong number of colorings", 1, colorings.size()); @@ -129,7 +129,7 @@ } public void testAllLanguagesHighlights2() { - ColoringStorage cs = new ColoringStorage(false); + ColoringStorage cs = new ColoringStorage(ColoringStorage.FAV_HIGHLIGHT); Map colorings = cs.load(MimePath.EMPTY, "MyProfileXyz", false); //NOI18N assertNotNull("Colorings map should not be null", colorings); assertEquals("Wrong number of colorings", 1, colorings.size()); @@ -218,7 +218,7 @@ } public void testLegacyFilesWithNoDTD_Issue113137() { - ColoringStorage cs = new ColoringStorage(true); + ColoringStorage cs = new ColoringStorage(ColoringStorage.FAV_TOKEN); Map colorings = cs.load(MimePath.parse("text/x-legacy"), "NetBeans", false); //NOI18N assertNotNull("Colorings map should not be null", colorings); assertEquals("Wrong number of colorings", 2, colorings.size()); diff --git a/editor/nbproject/project.properties b/editor/nbproject/project.properties --- a/editor/nbproject/project.properties +++ b/editor/nbproject/project.properties @@ -42,7 +42,7 @@ javac.compilerargs=-Xlint:unchecked javac.source=1.7 -spec.version.base=1.76.0 +spec.version.base=1.77.0 is.autoload=true javadoc.arch=${basedir}/arch.xml diff --git a/editor/src/org/netbeans/modules/editor/resources/NetBeans-editor.xml b/editor/src/org/netbeans/modules/editor/resources/NetBeans-annotations.xml copy from editor/src/org/netbeans/modules/editor/resources/NetBeans-editor.xml copy to editor/src/org/netbeans/modules/editor/resources/NetBeans-annotations.xml --- a/editor/src/org/netbeans/modules/editor/resources/NetBeans-editor.xml +++ b/editor/src/org/netbeans/modules/editor/resources/NetBeans-annotations.xml @@ -46,25 +46,101 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/editor/src/org/netbeans/modules/editor/resources/layer.xml b/editor/src/org/netbeans/modules/editor/resources/layer.xml --- a/editor/src/org/netbeans/modules/editor/resources/layer.xml +++ b/editor/src/org/netbeans/modules/editor/resources/layer.xml @@ -303,6 +303,9 @@ + + + diff --git a/options.editor/manifest.mf b/options.editor/manifest.mf --- a/options.editor/manifest.mf +++ b/options.editor/manifest.mf @@ -2,6 +2,6 @@ OpenIDE-Module: org.netbeans.modules.options.editor/1 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/options/editor/Bundle.properties OpenIDE-Module-Layer: org/netbeans/modules/options/editor/mf-layer.xml -OpenIDE-Module-Specification-Version: 1.46 +OpenIDE-Module-Specification-Version: 1.47 AutoUpdate-Show-In-Client: false diff --git a/options.editor/nbproject/project.xml b/options.editor/nbproject/project.xml --- a/options.editor/nbproject/project.xml +++ b/options.editor/nbproject/project.xml @@ -109,7 +109,7 @@ 1 - 1.12 + 1.42 diff --git a/options.editor/src/org/netbeans/modules/options/colors/AnnotationsPanel.java b/options.editor/src/org/netbeans/modules/options/colors/AnnotationsPanel.java --- a/options.editor/src/org/netbeans/modules/options/colors/AnnotationsPanel.java +++ b/options.editor/src/org/netbeans/modules/options/colors/AnnotationsPanel.java @@ -89,7 +89,7 @@ private ColorModel colorModel; private boolean listen = false; private String currentScheme; - private Map> schemes = new HashMap>(); + private Map> schemes = new HashMap>(); private Set toBeSaved = new HashSet(); private boolean changed = false; @@ -263,7 +263,7 @@ this.colorModel = colorModel; listen = false; currentScheme = colorModel.getCurrentProfile (); - lCategories.setListData (getAnnotations (currentScheme)); + lCategories.setListData (getAnnotations (currentScheme).toArray(new AttributeSet[]{})); if (lCategories.getModel ().getSize () > 0) lCategories.setSelectedIndex (0); refreshUI (); @@ -273,7 +273,7 @@ public void cancel () { toBeSaved = new HashSet(); - schemes = new HashMap>(); + schemes = new HashMap>(); changed = false; } @@ -283,7 +283,7 @@ colorModel.setAnnotations(scheme, getAnnotations(scheme)); } toBeSaved = new HashSet(); - schemes = new HashMap>(); + schemes = new HashMap>(); } public boolean isChanged () { @@ -293,7 +293,7 @@ public void setCurrentProfile (String currentScheme) { String oldScheme = this.currentScheme; this.currentScheme = currentScheme; - Vector v = getAnnotations(currentScheme); + List v = getAnnotations(currentScheme); if (v == null) { // clone scheme v = getAnnotations (oldScheme); @@ -301,13 +301,23 @@ toBeSaved.add (currentScheme); v = getAnnotations (currentScheme); } - lCategories.setListData (v); + lCategories.setListData (v.toArray(new AttributeSet[]{})); if (lCategories.getModel ().getSize () > 0) lCategories.setSelectedIndex (0); refreshUI (); } public void deleteProfile (String scheme) { + if (colorModel.isCustomProfile (scheme)) + schemes.remove (scheme); + else { + schemes.put (scheme, getDefaults (scheme)); + lCategories.setListData (getAnnotations(scheme).toArray(new AttributeSet[]{})); + lCategories.repaint(); + lCategories.setSelectedIndex (0); + refreshUI (); + } + toBeSaved.add (scheme); } public JComponent getComponent() { @@ -334,8 +344,9 @@ } private void updateData () { - Vector annotations = getAnnotations(currentScheme); - SimpleAttributeSet c = (SimpleAttributeSet) annotations.get(lCategories.getSelectedIndex()); + List annotations = getAnnotations(currentScheme); + int index = lCategories.getSelectedIndex(); + SimpleAttributeSet c = new SimpleAttributeSet(annotations.get(index)); Color color = ColorComboBoxSupport.getSelectedColor( (ColorComboBox)cbBackground ); if (color != null) { @@ -360,6 +371,8 @@ c.removeAttribute(EditorStyleConstants.WaveUnderlineColor); } + annotations.set(index, c); + toBeSaved.add(currentScheme); changed = true; } @@ -396,7 +409,7 @@ } // set values - Vector annotations = getAnnotations (currentScheme); + List annotations = getAnnotations (currentScheme); AttributeSet c = annotations.get (index); ColorComboBoxSupport.setSelectedColor( (ColorComboBox)cbForeground, (Color) c.getAttribute (StyleConstants.Foreground)); ColorComboBoxSupport.setSelectedColor( (ColorComboBox)cbBackground, (Color) c.getAttribute (StyleConstants.Background)); @@ -426,14 +439,27 @@ return null; } - private Vector getAnnotations(String scheme) { + private List getAnnotations(String scheme) { if (!schemes.containsKey(scheme)) { Collection c = colorModel.getAnnotations(currentScheme); if (c == null) return null; List l = new ArrayList(c); Collections.sort(l, new CategoryComparator()); - schemes.put(scheme, new Vector(l)); + schemes.put(scheme, new ArrayList(l)); } return schemes.get(scheme); } + /** cache Map (String (profile name) > List (AttributeSet)). */ + private Map> profileToDefaults = new HashMap>(); + + private List getDefaults(String profile) { + if (!profileToDefaults.containsKey(profile)) { + Collection c = colorModel.getAnnotationsDefaults(profile); + List l = new ArrayList(c); + Collections.sort(l, new CategoryComparator()); + profileToDefaults.put(profile, l); + } + List defaultprofile = profileToDefaults.get(profile); + return new ArrayList(defaultprofile); + } } diff --git a/options.editor/src/org/netbeans/modules/options/colors/ColorModel.java b/options.editor/src/org/netbeans/modules/options/colors/ColorModel.java --- a/options.editor/src/org/netbeans/modules/options/colors/ColorModel.java +++ b/options.editor/src/org/netbeans/modules/options/colors/ColorModel.java @@ -128,6 +128,18 @@ // annotations ............................................................. public Collection getAnnotations(String profile) { + Map annos = EditorSettings.getDefault().getAnnotations(profile); + List annotations = processAnnotations(annos, false); + return annotations; + } + + public Collection getAnnotationsDefaults(String profile) { + Map annos = EditorSettings.getDefault().getAnnotationDefaults(profile); + List annotations = processAnnotations(annos, true); + return annotations; + } + + private List processAnnotations(Map annos, boolean isdefault) { List annotations = new ArrayList(); for(Iterator it = AnnotationTypes.getTypes().getAnnotationTypeNames(); it.hasNext(); ) { String name = (String) it.next (); @@ -141,10 +153,10 @@ if (description == null) { continue; } - + SimpleAttributeSet category = new SimpleAttributeSet(); category.addAttribute(EditorStyleConstants.DisplayName, description); - category.addAttribute(StyleConstants.NameAttribute, description); + category.addAttribute(StyleConstants.NameAttribute, annotationType.getName()); URL iconURL = annotationType.getGlyph (); Image image = null; @@ -173,26 +185,40 @@ } category.addAttribute("annotationType", annotationType); //NOI18N + if (annos.containsKey(name)) { + if (isdefault) { + category.removeAttribute(StyleConstants.Background); + category.removeAttribute(StyleConstants.Foreground); + category.removeAttribute(EditorStyleConstants.WaveUnderlineColor); + } + AttributeSet as = annos.get(name); + category.addAttributes(as); + + } + annotations.add(category); - } - - return annotations; + } + return annotations; } public void setAnnotations ( String profile, Collection annotations ) { - //S ystem.out.println("ColorModelImpl.setAnnotations "); + //S ystem.out.println("ColorModelImpl.setAnnotations "); + Collection annos = new ArrayList(); for(AttributeSet category : annotations) { AnnotationType annotationType = (AnnotationType) category.getAttribute ("annotationType"); + SimpleAttributeSet c = new SimpleAttributeSet(); + c.addAttribute(StyleConstants.NameAttribute, category.getAttribute(StyleConstants.NameAttribute)); if (category.isDefined (StyleConstants.Background)) { annotationType.setUseHighlightColor (true); annotationType.setHighlight ( (Color) category.getAttribute (StyleConstants.Background) ); + c.addAttribute(StyleConstants.Background, category.getAttribute(StyleConstants.Background)); } else annotationType.setUseHighlightColor (false); if (category.isDefined (StyleConstants.Foreground)) { @@ -200,6 +226,7 @@ annotationType.setForegroundColor ( (Color) category.getAttribute (StyleConstants.Foreground) ); + c.addAttribute(StyleConstants.Foreground, category.getAttribute(StyleConstants.Foreground)); } else annotationType.setInheritForegroundColor (true); if (category.isDefined (EditorStyleConstants.WaveUnderlineColor)) { @@ -207,13 +234,15 @@ annotationType.setWaveUnderlineColor ( (Color) category.getAttribute (EditorStyleConstants.WaveUnderlineColor) ); + c.addAttribute((EditorStyleConstants.WaveUnderlineColor), category.getAttribute (EditorStyleConstants.WaveUnderlineColor)); } else annotationType.setUseWaveUnderlineColor (false); //S ystem.out.println(" " + category.getDisplayName () + " : " + annotationType + " : " + annotationType.getHighlight() + " : " + annotationType.isUseHighlightColor()); + annos.add(c); } + + EditorSettings.getDefault().setAnnotations(profile, toMap(annos)); } - - // editor categories ....................................................... /**