1
2
3
4
5
6
7
8
9
10
11 package de.funfried.netbeans.plugins.external.formatter.json.jackson;
12
13 import java.util.prefs.Preferences;
14
15 import javax.swing.text.Document;
16 import javax.swing.text.StyledDocument;
17
18 import org.netbeans.api.annotations.common.CheckForNull;
19 import org.netbeans.api.annotations.common.NonNull;
20 import org.netbeans.api.project.Project;
21 import org.openide.util.NbBundle;
22 import org.openide.util.lookup.ServiceProvider;
23
24 import de.funfried.netbeans.plugins.external.formatter.FormatJob;
25 import de.funfried.netbeans.plugins.external.formatter.FormatterService;
26 import de.funfried.netbeans.plugins.external.formatter.json.base.AbstractJsonFormatterService;
27 import de.funfried.netbeans.plugins.external.formatter.json.jackson.ui.JacksonJsonFormatterOptionsPanel;
28 import de.funfried.netbeans.plugins.external.formatter.ui.options.FormatterOptionsPanel;
29 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
30
31
32
33
34
35
36 @NbBundle.Messages({
37 "FormatterName=Jackson Json Code Formatter"
38 })
39 @ServiceProvider(service = FormatterService.class, position = 500)
40 public class JacksonJsonFormatterService extends AbstractJsonFormatterService {
41
42 public static final String ID = "jackson-json-formatter";
43
44
45 private final JacksonJsonFormatterWrapper formatter = new JacksonJsonFormatterWrapper();
46
47
48
49
50 @NonNull
51 @Override
52 public String getDisplayName() {
53 return NbBundle.getMessage(JacksonJsonFormatterService.class, "FormatterName");
54 }
55
56
57
58
59 @NonNull
60 @Override
61 public String getId() {
62 return ID;
63 }
64
65
66
67
68 @Override
69 public FormatterOptionsPanel createOptionsPanel(Project project) {
70 return new JacksonJsonFormatterOptionsPanel(project);
71 }
72
73
74
75
76 @CheckForNull
77 @Override
78 public Integer getContinuationIndentSize(Document document) {
79 if (document == null) {
80 return null;
81 }
82
83 Integer ret = null;
84
85 Preferences preferences = Settings.getActivePreferences(document);
86 if (isUseFormatterIndentationSettings(preferences)) {
87 ret = preferences.getInt(JacksonJsonFormatterSettings.INDENT_SIZE, 2);
88 }
89
90 return ret;
91 }
92
93
94
95
96 @CheckForNull
97 @Override
98 public Integer getIndentSize(Document document) {
99 if (document == null) {
100 return null;
101 }
102
103 Integer ret = null;
104
105 Preferences preferences = Settings.getActivePreferences(document);
106 if (isUseFormatterIndentationSettings(preferences)) {
107 ret = preferences.getInt(JacksonJsonFormatterSettings.INDENT_SIZE, 2);
108 }
109
110 return ret;
111 }
112
113
114
115
116
117
118
119
120
121
122 @CheckForNull
123 @Override
124 public Integer getRightMargin(Document document) {
125 if (document == null) {
126 return null;
127 }
128
129 return 0;
130 }
131
132
133
134
135 @Override
136 protected FormatJob getFormatJob(StyledDocument document) {
137 return new JacksonFormatJob(document, formatter);
138 }
139
140
141
142
143 @CheckForNull
144 @Override
145 public Integer getSpacesPerTab(Document document) {
146 if (document == null) {
147 return null;
148 }
149
150 Integer ret = null;
151
152 Preferences preferences = Settings.getActivePreferences(document);
153 if (isUseFormatterIndentationSettings(preferences)) {
154 if (!isExpandTabToSpaces(document, preferences) && preferences.getBoolean(Settings.OVERRIDE_TAB_SIZE, true)) {
155 ret = preferences.getInt(Settings.OVERRIDE_TAB_SIZE_VALUE, 4);
156 } else {
157 ret = preferences.getInt(JacksonJsonFormatterSettings.SPACES_PER_TAB, 2);
158 }
159 }
160
161 return ret;
162 }
163
164
165
166
167 @CheckForNull
168 @Override
169 public Boolean isExpandTabToSpaces(Document document) {
170 if (document == null) {
171 return null;
172 }
173
174 return isExpandTabToSpaces(document, Settings.getActivePreferences(document));
175 }
176
177 private Boolean isExpandTabToSpaces(Document document, Preferences preferences) {
178 if (document == null || preferences == null) {
179 return null;
180 }
181
182 Boolean ret = null;
183
184 if (isUseFormatterIndentationSettings(preferences)) {
185 ret = preferences.getBoolean(JacksonJsonFormatterSettings.EXPAND_TABS_TO_SPACES, true);
186 }
187
188 return ret;
189 }
190
191
192
193
194
195
196
197
198
199
200 private boolean isUseFormatterIndentationSettings(Preferences prefs) {
201 return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
202 }
203 }