1
2
3
4
5
6
7
8
9
10 package de.funfried.netbeans.plugins.external.formatter.xml.revelc;
11
12 import java.util.prefs.Preferences;
13
14 import javax.swing.SwingUtilities;
15 import javax.swing.text.BadLocationException;
16 import javax.swing.text.StyledDocument;
17
18 import org.netbeans.editor.BaseDocument;
19 import org.openide.awt.NotificationDisplayer;
20 import org.openide.awt.StatusDisplayer;
21
22 import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
23 import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
24 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
25 import net.revelc.code.formatter.xml.lib.FormattingPreferences;
26
27
28
29
30
31
32
33 class RevelcFormatJob extends AbstractFormatJob {
34
35 private final RevelcXmlFormatterWrapper formatter;
36
37
38
39
40
41
42
43 RevelcFormatJob(StyledDocument document, RevelcXmlFormatterWrapper formatter) {
44 super(document, null);
45
46 this.formatter = formatter;
47 }
48
49
50
51
52 @Override
53 public void format() throws BadLocationException {
54 Preferences pref = Settings.getActivePreferences(document);
55
56 boolean tabInsteadOfSpaces = pref.getBoolean(RevelcXmlFormatterSettings.TAB_INSTEAD_OF_SPACES, true);
57 boolean splitMultiAttrs = pref.getBoolean(RevelcXmlFormatterSettings.SPLIT_MULTI_ATTRIBUTES, false);
58 boolean wrapLongLines = pref.getBoolean(RevelcXmlFormatterSettings.WRAP_LONG_LINES, true);
59 int tabWidth = pref.getInt(RevelcXmlFormatterSettings.TAB_WIDTH, 4);
60 int maxLineLength = pref.getInt(RevelcXmlFormatterSettings.MAX_LINE_LENGTH, 120);
61 String wellFormedValidation = pref.get(RevelcXmlFormatterSettings.WELL_FORMED_VALIDATION, FormattingPreferences.WARN);
62 String lineFeedSetting = pref.get(RevelcXmlFormatterSettings.LINEFEED, "");
63
64 FormattingPreferences prefs = new FormattingPreferences();
65 prefs.setMaxLineLength(maxLineLength);
66 prefs.setSplitMultiAttrs(splitMultiAttrs);
67 prefs.setTabInsteadOfSpaces(tabInsteadOfSpaces);
68 prefs.setTabWidth(tabWidth);
69 prefs.setWellFormedValidation(wellFormedValidation);
70 prefs.setWrapLongLines(wrapLongLines);
71
72
73 String lineFeed = Settings.getLineFeed(lineFeedSetting, System.getProperty("line.separator"));
74 if (null != lineFeed) {
75 document.putProperty(BaseDocument.READ_LINE_SEPARATOR_PROP, lineFeed);
76 document.putProperty(BaseDocument.WRITE_LINE_SEPARATOR_PROP, lineFeed);
77 }
78
79 String code = getCode();
80
81 String formattedContent = formatter.format(code, lineFeed, prefs);
82 if (setFormattedCode(code, formattedContent)) {
83 SwingUtilities.invokeLater(() -> {
84 if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
85 NotificationDisplayer.getDefault().notify("Format using revelc XML formatter", Icons.ICON_REVELC, "", null);
86 }
87
88 StatusDisplayer.getDefault().setStatusText("Format using revelc XML formatter");
89 });
90 }
91 }
92 }