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