Skip to content

Commit c22f57c

Browse files
committed
PMD Preferences are overridden and lost
Fixes #46
1 parent adb3cb2 commit c22f57c

File tree

7 files changed

+220
-102
lines changed

7 files changed

+220
-102
lines changed

ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Eclipse Update Site:
2828
* [#32](https://github.com/pmd/pmd-eclipse-plugin/issues/32): Upgrade PMD to 6.2.0
2929
* [#42](https://github.com/pmd/pmd-eclipse-plugin/issues/42): Marker colors not updated in all views after change
3030
* [#43](https://github.com/pmd/pmd-eclipse-plugin/issues/43): Update unit tests to use new ruleset categories
31+
* [#46](https://github.com/pmd/pmd-eclipse-plugin/issues/46): PMD Preferences are overridden and lost
3132

3233
### External Contributions
3334

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/preferences/IPreferences.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public interface IPreferences {
7474

7575
boolean GLOBAL_RULE_MANAGEMENT_DEFAULT = false;
7676

77+
int TABLE_FRACTION_DEFAULT = 55;
78+
boolean DEFAULT_SORT_UP = false;
79+
String DEFAULT_GROUPING_COLUMN = "";
80+
7781
/**
7882
* Get a comma-separated list of rules that are active by default.
7983
*/
@@ -278,4 +282,28 @@ public interface IPreferences {
278282
*/
279283
void sync();
280284

285+
int tableFraction();
286+
287+
void tableFraction(int aFraction);
288+
289+
Set<String> getHiddenColumnIds();
290+
291+
void setHiddenColumnIds(Set<String> names);
292+
293+
boolean isSortDirectionUp();
294+
295+
void setSortDirectionUp(boolean isUp);
296+
297+
String getGroupingColumn();
298+
299+
void setGroupingColumn(String columnName);
300+
301+
Set<String> getSelectedRuleNames();
302+
303+
void setSelectedRuleNames(Set<String> ruleNames);
304+
305+
int getSelectedPropertyTab();
306+
307+
void setSelectedPropertyTab(int anIndex);
308+
281309
}
Lines changed: 23 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,88 @@
11

22
package net.sourceforge.pmd.eclipse.runtime.preferences.impl;
33

4-
import java.io.IOException;
5-
import java.util.Collection;
6-
import java.util.HashSet;
74
import java.util.Set;
85

9-
import org.eclipse.core.resources.IWorkspaceRoot;
10-
import org.eclipse.core.resources.ResourcesPlugin;
11-
import org.eclipse.core.runtime.IPath;
12-
import org.eclipse.jface.preference.PreferenceStore;
13-
146
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
15-
import net.sourceforge.pmd.eclipse.ui.preferences.br.RuleColumnDescriptor;
16-
import net.sourceforge.pmd.eclipse.ui.preferences.br.RuleTableColumns;
17-
import net.sourceforge.pmd.eclipse.ui.preferences.editors.SWTUtil;
7+
import net.sourceforge.pmd.eclipse.runtime.preferences.IPreferences;
188

199
/**
20-
*
21-
*
10+
* Stores preferences to restore UI state, such as selected row in the rule table.
11+
* TODO - replace this with the existing ViewMemento
12+
*
2213
* @author Brian Remedios
2314
*/
2415
public class PreferenceUIStore {
2516

26-
private PreferenceStore preferenceStore;
27-
28-
private static final String TABLE_FRACTION = PMDPlugin.PLUGIN_ID + ".ruletable.fraction";
29-
private static final String TABLE_HIDDEN_COLS = PMDPlugin.PLUGIN_ID + ".ruletable.hiddenColumns";
30-
private static final String TABLE_COLUMN_SORT_UP = PMDPlugin.PLUGIN_ID + ".ruletable.sortUp";
31-
private static final String GROUPING_COLUMN = PMDPlugin.PLUGIN_ID + ".ruletable.groupingColumn";
32-
private static final String SELECTED_RULE_NAMES = PMDPlugin.PLUGIN_ID + ".ruletable.selectedRules";
33-
private static final String SELECTED_PROPERTY_TAB = PMDPlugin.PLUGIN_ID + ".ruletable.selectedPropertyTab";
34-
private static final String GLOBAL_RULE_MANAGEMENT = PMDPlugin.PLUGIN_ID + ".globalRuleManagement";
35-
36-
private static final int TABLE_FRACTION_DEFAULT = 55;
37-
private static final char STRING_SEPARATOR = ',';
38-
39-
private static final RuleColumnDescriptor[] DEFAULT_HIDDEN_COLUMNS = new RuleColumnDescriptor[] {
40-
RuleTableColumns.EXTERNAL_URL, RuleTableColumns.MIN_LANGUAGE_VERSION, RuleTableColumns.FIX_COUNT,
41-
RuleTableColumns.EXAMPLE_COUNT, RuleTableColumns.MAX_LANGUAGE_VERSION, RuleTableColumns.SINCE,
42-
RuleTableColumns.MOD_COUNT };
43-
44-
private static final boolean DEFAULT_SORT_UP = false;
45-
4617
public static final PreferenceUIStore INSTANCE = new PreferenceUIStore();
4718

19+
private IPreferences preferences;
20+
4821
private PreferenceUIStore() {
4922
initialize();
5023
}
5124

52-
private static String defaultHiddenColumnIds() {
53-
Set<String> colNames = new HashSet<String>(DEFAULT_HIDDEN_COLUMNS.length);
54-
for (RuleColumnDescriptor rcDesc : DEFAULT_HIDDEN_COLUMNS) {
55-
colNames.add(rcDesc.id());
56-
}
57-
return SWTUtil.asString(colNames, STRING_SEPARATOR);
58-
}
59-
6025
private void initialize() {
61-
62-
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
63-
IPath path = root.getLocation();
64-
String fileName = path.append(PreferencesManagerImpl.NEW_PREFERENCE_LOCATION).toString();
65-
66-
// TODO - replace this with the existing ViewMemento
67-
preferenceStore = new PreferenceStore(fileName);
68-
preferenceStore.setDefault(GLOBAL_RULE_MANAGEMENT, false);
69-
70-
try {
71-
preferenceStore.load();
72-
} catch (IOException e) {
73-
createNewStore();
74-
}
75-
}
76-
77-
private void createNewStore() {
78-
79-
preferenceStore.setValue(TABLE_FRACTION, TABLE_FRACTION_DEFAULT);
80-
preferenceStore.setValue(TABLE_HIDDEN_COLS, defaultHiddenColumnIds());
81-
preferenceStore.setValue(TABLE_COLUMN_SORT_UP, DEFAULT_SORT_UP);
82-
preferenceStore.setValue(GROUPING_COLUMN, "");
83-
preferenceStore.setValue(SELECTED_RULE_NAMES, "");
84-
preferenceStore.setValue(SELECTED_PROPERTY_TAB, 0);
85-
preferenceStore.setValue(GLOBAL_RULE_MANAGEMENT, false);
86-
87-
save();
26+
preferences = PMDPlugin.getDefault().loadPreferences();
8827
}
8928

9029
public void save() {
91-
92-
try {
93-
preferenceStore.save();
94-
} catch (IOException e) {
95-
e.printStackTrace();
96-
}
30+
preferences.sync();
9731
}
9832

9933
public int tableFraction() {
100-
return preferenceStore.getInt(TABLE_FRACTION);
34+
return preferences.tableFraction();
10135
}
10236

10337
public void tableFraction(int aFraction) {
104-
preferenceStore.setValue(TABLE_FRACTION, aFraction);
38+
preferences.tableFraction(aFraction);
10539
}
10640

10741
public Set<String> hiddenColumnIds() {
108-
String names = preferenceStore.getString(TABLE_HIDDEN_COLS);
109-
return SWTUtil.asStringSet(names, STRING_SEPARATOR);
42+
return preferences.getHiddenColumnIds();
11043
}
11144

11245
public void hiddenColumnIds(Set<String> names) {
113-
String nameStr = SWTUtil.asString(names, STRING_SEPARATOR);
114-
preferenceStore.setValue(TABLE_HIDDEN_COLS, nameStr);
46+
preferences.setHiddenColumnIds(names);
11547
}
11648

11749
public int selectedPropertyTab() {
118-
return preferenceStore.getInt(SELECTED_PROPERTY_TAB);
50+
return preferences.getSelectedPropertyTab();
11951
}
12052

12153
public void selectedPropertyTab(int anIndex) {
122-
preferenceStore.setValue(SELECTED_PROPERTY_TAB, anIndex);
54+
preferences.setSelectedPropertyTab(anIndex);
12355
}
12456

12557
public boolean globalRuleManagement() {
126-
return preferenceStore.getBoolean(GLOBAL_RULE_MANAGEMENT);
58+
return preferences.getGlobalRuleManagement();
12759
}
12860

12961
public void globalRuleManagement(boolean b) {
130-
preferenceStore.setValue(GLOBAL_RULE_MANAGEMENT, b);
62+
preferences.setGlobalRuleManagement(b);
13163
}
13264

13365
public Set<String> selectedRuleNames() {
134-
String names = preferenceStore.getString(SELECTED_RULE_NAMES);
135-
return SWTUtil.asStringSet(names, STRING_SEPARATOR);
66+
return preferences.getSelectedRuleNames();
13667
}
13768

138-
public void selectedRuleNames(Collection<String> ruleNames) {
139-
String nameStr = SWTUtil.asString(ruleNames, STRING_SEPARATOR);
140-
preferenceStore.setValue(SELECTED_RULE_NAMES, nameStr);
69+
public void selectedRuleNames(Set<String> ruleNames) {
70+
preferences.setSelectedRuleNames(ruleNames);
14171
}
14272

14373
public boolean sortDirectionUp() {
144-
return preferenceStore.getBoolean(TABLE_COLUMN_SORT_UP);
74+
return preferences.isSortDirectionUp();
14575
}
14676

14777
public void sortDirectionUp(boolean isUp) {
148-
preferenceStore.setValue(TABLE_COLUMN_SORT_UP, isUp);
78+
preferences.setSortDirectionUp(isUp);
14979
}
15080

15181
public String groupingColumnName() {
152-
return preferenceStore.getString(GROUPING_COLUMN);
82+
return preferences.getGroupingColumn();
15383
}
15484

15585
public void groupingColumnName(String columnName) {
156-
preferenceStore.setValue(GROUPING_COLUMN, columnName);
86+
preferences.setGroupingColumn(columnName);
15787
}
15888
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/preferences/impl/PreferencesImpl.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ class PreferencesImpl implements IPreferences {
8585
private Map<RulePriority, PriorityDescriptor> uiDescriptorsByPriority = new HashMap<RulePriority, PriorityDescriptor>(
8686
5);
8787

88+
private int tableFraction;
89+
private Set<String> hiddenColumnIds;
90+
private boolean sortDirectionUp;
91+
private String groupingColumn;
92+
private Set<String> selectedRuleNames;
93+
private int selectedPropertyTab;
94+
8895
/**
8996
* Is constructed from a preferences manager
9097
*
@@ -354,4 +361,62 @@ public void setPmdViolationsOutlineEnabled(boolean pmdViolationsOutlineEnabled)
354361
this.pmdViolationsOutlineEnabled = pmdViolationsOutlineEnabled;
355362
}
356363

364+
@Override
365+
public int tableFraction() {
366+
return tableFraction;
367+
}
368+
@Override
369+
public void tableFraction(int aFraction) {
370+
this.tableFraction = aFraction;
371+
}
372+
373+
@Override
374+
public Set<String> getHiddenColumnIds() {
375+
return hiddenColumnIds;
376+
}
377+
378+
@Override
379+
public void setHiddenColumnIds(Set<String> names) {
380+
hiddenColumnIds = names;
381+
}
382+
383+
@Override
384+
public boolean isSortDirectionUp() {
385+
return sortDirectionUp;
386+
}
387+
388+
@Override
389+
public void setSortDirectionUp(boolean isUp) {
390+
sortDirectionUp = isUp;
391+
}
392+
393+
@Override
394+
public String getGroupingColumn() {
395+
return groupingColumn;
396+
}
397+
398+
@Override
399+
public void setGroupingColumn(String columnName) {
400+
groupingColumn = columnName;
401+
}
402+
403+
@Override
404+
public Set<String> getSelectedRuleNames() {
405+
return selectedRuleNames;
406+
}
407+
408+
@Override
409+
public void setSelectedRuleNames(Set<String> ruleNames) {
410+
selectedRuleNames = ruleNames;
411+
}
412+
413+
@Override
414+
public int getSelectedPropertyTab() {
415+
return selectedPropertyTab;
416+
}
417+
418+
@Override
419+
public void setSelectedPropertyTab(int anIndex) {
420+
selectedPropertyTab = anIndex;
421+
}
357422
}

0 commit comments

Comments
 (0)