1
2
3
4
5
6
7
8
9
10 package de.funfried.netbeans.plugins.external.formatter.java.palantir;
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.openide.awt.NotificationDisplayer;
21 import org.openide.awt.StatusDisplayer;
22
23 import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
24 import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
25 import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
26 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
27
28
29
30
31
32
33
34 class PalantirFormatJob extends AbstractFormatJob {
35
36 private final PalantirJavaFormatterWrapper formatter;
37
38
39
40
41
42
43
44
45 PalantirFormatJob(StyledDocument document, PalantirJavaFormatterWrapper formatter, SortedSet<Pair<Integer, Integer>> changedElements) {
46 super(document, changedElements);
47
48 this.formatter = formatter;
49 }
50
51
52
53
54 @Override
55 public void format() throws BadLocationException {
56 Preferences pref = Settings.getActivePreferences(document);
57
58 String code = getCode();
59
60 SortedSet<Pair<Integer, Integer>> regions = getFormatableSections(code);
61
62 try {
63 String formattedContent = formatter.format(code, regions);
64 if (setFormattedCode(code, formattedContent)) {
65 SwingUtilities.invokeLater(() -> {
66 if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
67 NotificationDisplayer.getDefault().notify("Format using Palantir formatter", Icons.ICON_EXTERNAL, "", null);
68 }
69
70 StatusDisplayer.getDefault().setStatusText("Format using Palantir formatter");
71 });
72 }
73 } catch (FormattingFailedException ex) {
74 SwingUtilities.invokeLater(() -> {
75 StatusDisplayer.getDefault().setStatusText("Failed to format using Palantir formatter: " + ex.getMessage());
76 });
77
78 throw ex;
79 }
80 }
81
82 public void organizeImports() throws BadLocationException {
83 Preferences pref = Settings.getActivePreferences(document);
84
85 String code = getCode();
86
87 try {
88 String formattedContent = formatter.organizeImports(code);
89
90 if (setFormattedCode(code, formattedContent)) {
91 SwingUtilities.invokeLater(() -> {
92 if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
93 NotificationDisplayer.getDefault().notify("Organizing imports using Palantir formatter", Icons.ICON_EXTERNAL, "", null);
94 }
95
96 StatusDisplayer.getDefault().setStatusText("Organizing imports using Palantir formatter");
97 });
98 }
99 } catch (FormattingFailedException ex) {
100 SwingUtilities.invokeLater(() -> {
101 StatusDisplayer.getDefault().setStatusText("Failed to organize imports using Palantir formatter: " + ex.getMessage());
102 });
103
104 throw ex;
105 }
106 }
107 }