1
2
3
4
5
6
7
8
9
10 package de.funfried.netbeans.plugins.external.formatter.java.spring;
11
12 import java.util.SortedSet;
13 import java.util.prefs.Preferences;
14
15 import javax.swing.SwingUtilities;
16 import javax.swing.text.BadLocationException;
17 import javax.swing.text.StyledDocument;
18
19 import org.apache.commons.lang3.tuple.Pair;
20 import org.netbeans.editor.BaseDocument;
21 import org.openide.awt.NotificationDisplayer;
22 import org.openide.awt.StatusDisplayer;
23
24 import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
25 import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
26 import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
27 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
28
29
30
31
32
33
34
35 class SpringFormatJob extends AbstractFormatJob {
36
37 private final SpringJavaFormatterWrapper formatter;
38
39
40
41
42
43
44
45
46 SpringFormatJob(StyledDocument document, SpringJavaFormatterWrapper formatter, SortedSet<Pair<Integer, Integer>> changedElements) {
47 super(document, changedElements);
48
49 this.formatter = formatter;
50 }
51
52
53
54
55 @Override
56 public void format() throws BadLocationException {
57 Preferences pref = Settings.getActivePreferences(document);
58
59 String lineFeedSetting = pref.get(SpringJavaFormatterSettings.LINEFEED, "");
60 String lineFeed = Settings.getLineFeed(lineFeedSetting, System.getProperty("line.separator"));
61
62
63 if (null != lineFeed) {
64 document.putProperty(BaseDocument.READ_LINE_SEPARATOR_PROP, lineFeed);
65 document.putProperty(BaseDocument.WRITE_LINE_SEPARATOR_PROP, lineFeed);
66 }
67
68 String code = getCode();
69
70 SortedSet<Pair<Integer, Integer>> regions = getFormatableSections(code);
71
72 try {
73 String formattedContent = formatter.format(code, lineFeed, regions);
74 if (setFormattedCode(code, formattedContent)) {
75 SwingUtilities.invokeLater(() -> {
76 if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
77 NotificationDisplayer.getDefault().notify("Format using Spring formatter", Icons.ICON_SPRING, "", null);
78 }
79
80 StatusDisplayer.getDefault().setStatusText("Format using Spring formatter");
81 });
82 }
83 } catch (FormattingFailedException ex) {
84 SwingUtilities.invokeLater(() -> {
85 StatusDisplayer.getDefault().setStatusText("Failed to format using Spring formatter: " + ex.getMessage());
86 });
87
88 throw ex;
89 }
90 }
91 }