Skip to content

Commit

Permalink
add Task Tag UI preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed May 5, 2023
1 parent 4aa4278 commit 33a4b1c
Show file tree
Hide file tree
Showing 14 changed files with 664 additions and 38 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-SymbolicName: org.eclipse.tm4e.ui;singleton:=true
Bundle-Version: 0.6.2.qualifier
Bundle-Version: 0.6.3.qualifier
Require-Bundle: org.eclipse.tm4e.core;bundle-version="0.5.2",
org.eclipse.jface.text,
org.eclipse.core.runtime,
Expand Down
1 change: 1 addition & 0 deletions org.eclipse.tm4e.ui/plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Theme.Dark.name=Dark
# Preferences
TextMatePreferencePage.name=TextMate
GrammarPreferencePage.name=Grammar
TaskTagsPreferencePage.name=Task Tags
ThemePreferencePage.name=Theme

# Wizards
Expand Down
4 changes: 4 additions & 0 deletions org.eclipse.tm4e.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
class="org.eclipse.tm4e.ui.internal.preferences.GrammarPreferencePage"
id="org.eclipse.tm4e.ui.preferences.GrammarPreferencePage"
category="org.eclipse.tm4e.ui.preferences.TextMatePreferencePage" />
<page name="%TaskTagsPreferencePage.name"
class="org.eclipse.tm4e.ui.internal.preferences.TaskTagsPreferencePage"
id="org.eclipse.tm4e.ui.preferences.TaskTagsPreferencePage"
category="org.eclipse.tm4e.ui.preferences.TextMatePreferencePage" />
<page name="%ThemePreferencePage.name"
class="org.eclipse.tm4e.ui.internal.preferences.ThemePreferencePage"
id="org.eclipse.tm4e.ui.preferences.ThemePreferencePage"
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

<artifactId>org.eclipse.tm4e.ui</artifactId>
<packaging>eclipse-plugin</packaging>
<version>0.6.2-SNAPSHOT</version>
<version>0.6.3-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public final class TMUIMessages extends NLS {
// TextMate preferences page
public static String TextMatePreferencePage_GrammarRelatedLink;
public static String TextMatePreferencePage_LanguageConfigurationRelatedLink;
public static String TextMatePreferencePage_TaskTagsRelatedLink;
public static String TextMatePreferencePage_ThemeRelatedLink;

// Grammar preferences page
Expand All @@ -49,6 +50,18 @@ public final class TMUIMessages extends NLS {
public static String GrammarPreferencePage_tab_injection_text;
public static String GrammarPreferencePage_preview;

// Task Tags preferences page
public static String TaskTagsPreferencePage_description;
public static String TaskTagsPreferencePage_column_tag;
public static String TaskTagsPreferencePage_column_type;
public static String TaskTagsPreferencePage_column_level;
public static String TaskTagsPreferencePage_addTagDialog_windowTitle;
public static String TaskTagsPreferencePage_addTagDialog_header;
public static String TaskTagsPreferencePage_addTagDialog_message;
public static String TaskTagsPreferencePage_editTagDialog_windowTitle;
public static String TaskTagsPreferencePage_editTagDialog_header;
public static String TaskTagsPreferencePage_editTagDialog_message;

// Theme preferences page
public static String ThemePreferencePage_title;
public static String ThemePreferencePage_description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Button_browse_Workspace=Browse Workspace...
# preference page
TextMatePreferencePage_GrammarRelatedLink=See <a>''{0}''</a> for associating editors with grammars.
TextMatePreferencePage_LanguageConfigurationRelatedLink=See <a>''{0}''</a> for associating editors with language configurations.
TextMatePreferencePage_TaskTagsRelatedLink=See <a>''{0}''</a> for task tags configuration.
TextMatePreferencePage_ThemeRelatedLink=See <a>''{0}''</a> for associating editors with themes.

GrammarPreferencePage_title=TextMate grammars
Expand All @@ -33,6 +34,17 @@ GrammarPreferencePage_tab_theme_text=Theme
GrammarPreferencePage_tab_injection_text=Injection
GrammarPreferencePage_preview=Previe&w:

TaskTagsPreferencePage_description=Manage comment task tags:
TaskTagsPreferencePage_column_tag=Tag
TaskTagsPreferencePage_column_type=Marker Type
TaskTagsPreferencePage_column_level=Level
TaskTagsPreferencePage_addTagDialog_windowTitle=Task Tag
TaskTagsPreferencePage_addTagDialog_header=New task tag configuration
TaskTagsPreferencePage_addTagDialog_message=Create a new task tag configuration
TaskTagsPreferencePage_editTagDialog_windowTitle=Task Tag
TaskTagsPreferencePage_editTagDialog_header=Edit task tag configuration
TaskTagsPreferencePage_editTagDialog_message=Modify an existing task tag configuration

ThemePreferencePage_title=TextMate themes
ThemePreferencePage_description=&Create, edit or remove TextMate themes:
ThemePreferencePage_column_name=Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
public final class PreferenceConstants {

public static final String TASK_TAGS = "org.eclipse.tm4e.ui.taskTags";

public static final String THEMES = "org.eclipse.tm4e.ui.themes";

public static final String THEME_ASSOCIATIONS = "org.eclipse.tm4e.ui.themeAssociations";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,72 @@
*/
package org.eclipse.tm4e.ui.internal.preferences;

import java.io.IOException;
import java.util.Collection;
import java.util.Set;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tm4e.ui.TMUIPlugin;
import org.eclipse.tm4e.ui.internal.utils.MarkerConfig;
import org.eclipse.tm4e.ui.themes.IThemeAssociation;
import org.eclipse.tm4e.ui.themes.ThemeAssociation;
import org.osgi.service.prefs.BackingStoreException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

/**
* Helper class load, save theme preferences with Json format.
*
* Helper class to load, save preferences in JSON format.
*/
public final class PreferenceHelper {

private static final Gson DEFAULT_GSON;

static {
DEFAULT_GSON = new GsonBuilder().registerTypeAdapter(IThemeAssociation.class,
(InstanceCreator<ThemeAssociation>) type -> new ThemeAssociation()).create();
DEFAULT_GSON = new GsonBuilder() //
.registerTypeAdapter(IThemeAssociation.class, (InstanceCreator<ThemeAssociation>) type -> new ThemeAssociation())
.registerTypeAdapterFactory(new TypeAdapterFactory() {
@SuppressWarnings("unchecked")
@Override
@NonNullByDefault({})
public @Nullable <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
if (!MarkerConfig.class.isAssignableFrom(type.getRawType()))
return null;

final var jsonElementAdapter = gson.getAdapter(JsonElement.class);
final var problemAdapter = gson.getDelegateAdapter(this, TypeToken.get(MarkerConfig.ProblemMarkerConfig.class));
final var taskAdapter = gson.getDelegateAdapter(this, TypeToken.get(MarkerConfig.TaskMarkerConfig.class));
return (TypeAdapter<T>) new TypeAdapter<MarkerConfig>() {
@Override
public void write(final JsonWriter out, final MarkerConfig value) throws IOException {
if (value.getClass().isAssignableFrom(MarkerConfig.ProblemMarkerConfig.class)) {
problemAdapter.write(out, (MarkerConfig.ProblemMarkerConfig) value);
} else if (value.getClass().isAssignableFrom(MarkerConfig.TaskMarkerConfig.class)) {
taskAdapter.write(out, (MarkerConfig.TaskMarkerConfig) value);
}
}

@Override
public MarkerConfig read(final JsonReader in) throws IOException {
final var objectJson = jsonElementAdapter.read(in).getAsJsonObject();
return switch (MarkerConfig.Type.valueOf(objectJson.get("type").getAsString())) {
case PROBLEM -> problemAdapter.fromJsonTree(objectJson);
case TASK -> taskAdapter.fromJsonTree(objectJson);
};
}
};
}
}).create();
}

public static IThemeAssociation[] loadThemeAssociations(final String json) {
Expand All @@ -41,6 +87,37 @@ public static String toJsonThemeAssociations(final Collection<IThemeAssociation>
return DEFAULT_GSON.toJson(themeAssociations);
}

public static Set<MarkerConfig> loadMarkerConfigs() {
final var prefs = InstanceScope.INSTANCE.getNode(TMUIPlugin.PLUGIN_ID);
final var json = prefs.get(PreferenceConstants.TASK_TAGS, null);
Set<MarkerConfig> result = null;
try {
result = loadMarkerConfigs(json);
} catch (JsonSyntaxException ex) {
TMUIPlugin.logError(ex);
}
return result == null ? MarkerConfig.getDefaults() : result;
}

public static Set<MarkerConfig> loadMarkerConfigs(final String json) {
return DEFAULT_GSON.fromJson(json, new TypeToken<Set<MarkerConfig>>() {
}.getType());
}

public static String toJsonMarkerConfigs(final Set<MarkerConfig> markerConfigs) {
return DEFAULT_GSON.toJson(markerConfigs);
}

public static void saveMarkerConfigs(final Set<MarkerConfig> markerConfigs) {
final var prefs = InstanceScope.INSTANCE.getNode(TMUIPlugin.PLUGIN_ID);
prefs.put(PreferenceConstants.TASK_TAGS, toJsonMarkerConfigs(markerConfigs));
try {
prefs.flush();
} catch (final BackingStoreException ex) {
TMUIPlugin.logError(ex);
}
}

private PreferenceHelper() {
}
}
Loading

0 comments on commit 33a4b1c

Please sign in to comment.