1
2
3
4
5
6
7
8
9
10
11 package de.funfried.netbeans.plugins.external.formatter.ui.options;
12
13 import java.util.prefs.Preferences;
14
15 import javax.swing.text.Document;
16
17 import org.apache.commons.lang3.StringUtils;
18 import org.netbeans.api.project.FileOwnerQuery;
19 import org.netbeans.api.project.Project;
20 import org.netbeans.api.project.ProjectUtils;
21 import org.netbeans.editor.BaseDocument;
22 import org.netbeans.modules.editor.NbEditorUtilities;
23 import org.openide.filesystems.FileObject;
24 import org.openide.loaders.DataObject;
25 import org.openide.util.NbPreferences;
26
27
28
29
30
31
32
33 public class Settings {
34
35
36
37
38
39 public static final String ENABLED_FORMATTER_PREFIX = Settings.ENABLED_FORMATTER + ".";
40
41
42
43
44
45
46
47
48 @Deprecated
49 public static final String ENABLED_FORMATTER = "enabledFormatter";
50
51
52
53
54
55
56 public static final String DEFAULT_FORMATTER = "netbeans-formatter";
57
58
59 public static final String ENABLE_USE_OF_INDENTATION_SETTINGS = "enableIndentationSettings";
60
61
62 public static final String ENABLE_USE_OF_EDITOR_CONFIG = "enableEditorConfig";
63
64
65 public static final String OVERRIDE_TAB_SIZE = "overrideTabSize";
66
67
68 public static final String OVERRIDE_TAB_SIZE_VALUE = "overrideTabSizeValue";
69
70
71 public static final String SHOW_NOTIFICATIONS = "showNotifications";
72
73
74 public static final String USE_PROJECT_SETTINGS = "useProjectSettings";
75
76
77
78
79 private Settings() {
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93 public static Preferences getActivePreferences(Document document) {
94 Preferences globalPreferences = NbPreferences.forModule(ExternalFormatterPanel.class);
95 if (document != null) {
96 DataObject dataObj = NbEditorUtilities.getDataObject(document);
97 if (dataObj != null) {
98 FileObject primaryFile = dataObj.getPrimaryFile();
99 if (primaryFile != null) {
100 Project project = FileOwnerQuery.getOwner(primaryFile);
101 if (null != project) {
102 Preferences projectPreferences = ProjectUtils.getPreferences(project, ExternalFormatterPanel.class, true);
103 if (projectPreferences.getBoolean(USE_PROJECT_SETTINGS, false)) {
104 return projectPreferences;
105 }
106 }
107 }
108 }
109 }
110
111 return globalPreferences;
112 }
113
114
115
116
117
118
119
120
121
122
123 public static String getLineFeed(String lineFeedSetting, String fallback) {
124 String linefeed = fallback;
125
126 boolean usePlatformLinefeed = StringUtils.isBlank(lineFeedSetting);
127 if (!usePlatformLinefeed) {
128 switch (lineFeedSetting) {
129 case "\\n":
130 linefeed = BaseDocument.LS_LF;
131 break;
132 case "\\r":
133 linefeed = BaseDocument.LS_CR;
134 break;
135 case "\\r\\n":
136 linefeed = BaseDocument.LS_CRLF;
137 break;
138 default:
139 linefeed = null;
140 break;
141 }
142 }
143
144 return linefeed;
145 }
146 }