1
2
3
4
5
6
7
8
9
10
11 package de.funfried.netbeans.plugins.external.formatter.xml.revelc;
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.ui.options.FormatterOptionsPanel;
27 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
28 import de.funfried.netbeans.plugins.external.formatter.xml.base.AbstractXmlFormatterService;
29 import de.funfried.netbeans.plugins.external.formatter.xml.revelc.ui.RevelcXmlFormatterOptionsPanel;
30
31
32
33
34
35
36 @NbBundle.Messages({
37 "FormatterName=revelc.net XML Code Formatter"
38 })
39 @ServiceProvider(service = FormatterService.class, position = 500)
40 public class RevelcXmlFormatterService extends AbstractXmlFormatterService {
41
42 public static final String ID = "revelc-xml-formatter";
43
44
45 private final RevelcXmlFormatterWrapper formatter = new RevelcXmlFormatterWrapper();
46
47
48
49
50 @NonNull
51 @Override
52 public String getDisplayName() {
53 return NbBundle.getMessage(RevelcXmlFormatterService.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 RevelcXmlFormatterOptionsPanel(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 if (preferences.getBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, true)) {
88 ret = 1;
89 } else {
90 ret = preferences.getInt(RevelcXmlFormatterSettings.TAB_WIDTH, 4);
91 }
92
93 Integer indentSize = getIndentSize(document);
94 if (indentSize != null) {
95 ret *= indentSize;
96 }
97 }
98
99 return ret;
100 }
101
102
103
104
105 @CheckForNull
106 @Override
107 public Integer getIndentSize(Document document) {
108 if (document == null) {
109 return null;
110 }
111
112 Integer ret = null;
113
114 Preferences preferences = Settings.getActivePreferences(document);
115 if (isUseFormatterIndentationSettings(preferences)) {
116 if (preferences.getBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, true)) {
117 ret = 1;
118 } else {
119 ret = preferences.getInt(RevelcXmlFormatterSettings.TAB_WIDTH, 4);
120 }
121 }
122
123 return ret;
124 }
125
126
127
128
129 @CheckForNull
130 @Override
131 public Integer getRightMargin(Document document) {
132 if (document == null) {
133 return null;
134 }
135
136 Preferences preferences = Settings.getActivePreferences(document);
137 return preferences.getInt(RevelcXmlFormatterSettings.MAX_LINE_LENGTH, 120);
138 }
139
140
141
142
143 @Override
144 protected FormatJob getFormatJob(StyledDocument document) {
145 return new RevelcFormatJob(document, formatter);
146 }
147
148
149
150
151 @CheckForNull
152 @Override
153 public Integer getSpacesPerTab(Document document) {
154 if (document == null) {
155 return null;
156 }
157
158 Integer ret = null;
159
160 Preferences preferences = Settings.getActivePreferences(document);
161 if (isUseFormatterIndentationSettings(preferences)) {
162 if (!isExpandTabToSpaces(document, preferences) && preferences.getBoolean(Settings.OVERRIDE_TAB_SIZE, true)) {
163 ret = preferences.getInt(Settings.OVERRIDE_TAB_SIZE_VALUE, 4);
164 } else {
165 ret = preferences.getInt(RevelcXmlFormatterSettings.TAB_WIDTH, 4);
166 }
167 }
168
169 return ret;
170 }
171
172
173
174
175 @CheckForNull
176 @Override
177 public Boolean isExpandTabToSpaces(Document document) {
178 if (document == null) {
179 return null;
180 }
181
182 return isExpandTabToSpaces(document, Settings.getActivePreferences(document));
183 }
184
185 private Boolean isExpandTabToSpaces(Document document, Preferences preferences) {
186 if (document == null || preferences == null) {
187 return null;
188 }
189
190 Boolean ret = null;
191
192 if (isUseFormatterIndentationSettings(preferences)) {
193 ret = !preferences.getBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, true);
194 }
195
196 return ret;
197 }
198
199
200
201
202
203
204
205
206
207
208 private boolean isUseFormatterIndentationSettings(Preferences prefs) {
209 return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
210 }
211 }