|
1 | 1 |
|
2 | 2 | package net.sourceforge.pmd.eclipse.runtime.preferences.impl; |
3 | 3 |
|
4 | | -import java.io.IOException; |
5 | | -import java.util.Collection; |
6 | | -import java.util.HashSet; |
7 | 4 | import java.util.Set; |
8 | 5 |
|
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 | | - |
14 | 6 | 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; |
18 | 8 |
|
19 | 9 | /** |
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 | + * |
22 | 13 | * @author Brian Remedios |
23 | 14 | */ |
24 | 15 | public class PreferenceUIStore { |
25 | 16 |
|
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 | | - |
46 | 17 | public static final PreferenceUIStore INSTANCE = new PreferenceUIStore(); |
47 | 18 |
|
| 19 | + private IPreferences preferences; |
| 20 | + |
48 | 21 | private PreferenceUIStore() { |
49 | 22 | initialize(); |
50 | 23 | } |
51 | 24 |
|
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 | | - |
60 | 25 | 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(); |
88 | 27 | } |
89 | 28 |
|
90 | 29 | public void save() { |
91 | | - |
92 | | - try { |
93 | | - preferenceStore.save(); |
94 | | - } catch (IOException e) { |
95 | | - e.printStackTrace(); |
96 | | - } |
| 30 | + preferences.sync(); |
97 | 31 | } |
98 | 32 |
|
99 | 33 | public int tableFraction() { |
100 | | - return preferenceStore.getInt(TABLE_FRACTION); |
| 34 | + return preferences.tableFraction(); |
101 | 35 | } |
102 | 36 |
|
103 | 37 | public void tableFraction(int aFraction) { |
104 | | - preferenceStore.setValue(TABLE_FRACTION, aFraction); |
| 38 | + preferences.tableFraction(aFraction); |
105 | 39 | } |
106 | 40 |
|
107 | 41 | public Set<String> hiddenColumnIds() { |
108 | | - String names = preferenceStore.getString(TABLE_HIDDEN_COLS); |
109 | | - return SWTUtil.asStringSet(names, STRING_SEPARATOR); |
| 42 | + return preferences.getHiddenColumnIds(); |
110 | 43 | } |
111 | 44 |
|
112 | 45 | 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); |
115 | 47 | } |
116 | 48 |
|
117 | 49 | public int selectedPropertyTab() { |
118 | | - return preferenceStore.getInt(SELECTED_PROPERTY_TAB); |
| 50 | + return preferences.getSelectedPropertyTab(); |
119 | 51 | } |
120 | 52 |
|
121 | 53 | public void selectedPropertyTab(int anIndex) { |
122 | | - preferenceStore.setValue(SELECTED_PROPERTY_TAB, anIndex); |
| 54 | + preferences.setSelectedPropertyTab(anIndex); |
123 | 55 | } |
124 | 56 |
|
125 | 57 | public boolean globalRuleManagement() { |
126 | | - return preferenceStore.getBoolean(GLOBAL_RULE_MANAGEMENT); |
| 58 | + return preferences.getGlobalRuleManagement(); |
127 | 59 | } |
128 | 60 |
|
129 | 61 | public void globalRuleManagement(boolean b) { |
130 | | - preferenceStore.setValue(GLOBAL_RULE_MANAGEMENT, b); |
| 62 | + preferences.setGlobalRuleManagement(b); |
131 | 63 | } |
132 | 64 |
|
133 | 65 | public Set<String> selectedRuleNames() { |
134 | | - String names = preferenceStore.getString(SELECTED_RULE_NAMES); |
135 | | - return SWTUtil.asStringSet(names, STRING_SEPARATOR); |
| 66 | + return preferences.getSelectedRuleNames(); |
136 | 67 | } |
137 | 68 |
|
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); |
141 | 71 | } |
142 | 72 |
|
143 | 73 | public boolean sortDirectionUp() { |
144 | | - return preferenceStore.getBoolean(TABLE_COLUMN_SORT_UP); |
| 74 | + return preferences.isSortDirectionUp(); |
145 | 75 | } |
146 | 76 |
|
147 | 77 | public void sortDirectionUp(boolean isUp) { |
148 | | - preferenceStore.setValue(TABLE_COLUMN_SORT_UP, isUp); |
| 78 | + preferences.setSortDirectionUp(isUp); |
149 | 79 | } |
150 | 80 |
|
151 | 81 | public String groupingColumnName() { |
152 | | - return preferenceStore.getString(GROUPING_COLUMN); |
| 82 | + return preferences.getGroupingColumn(); |
153 | 83 | } |
154 | 84 |
|
155 | 85 | public void groupingColumnName(String columnName) { |
156 | | - preferenceStore.setValue(GROUPING_COLUMN, columnName); |
| 86 | + preferences.setGroupingColumn(columnName); |
157 | 87 | } |
158 | 88 | } |
0 commit comments