1
2
3
4
5
6
7
8
9
10 package de.funfried.netbeans.plugins.external.formatter.sql.jsqlformatter;
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.manticore.jsqlformatter.JSQLFormatter;
24 import com.manticore.jsqlformatter.JSQLFormatter.FormattingOption;
25
26 import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
27 import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
28 import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
29 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
30
31
32
33
34
35
36
37 class JSQLFormatterJob extends AbstractFormatJob {
38
39 private final JSQLFormatterWrapper formatter;
40
41
42
43
44
45
46
47
48 JSQLFormatterJob(StyledDocument document, JSQLFormatterWrapper formatter, SortedSet<Pair<Integer, Integer>> changedElements) {
49 super(document, changedElements);
50
51 this.formatter = formatter;
52 }
53
54
55
56
57 @Override
58 public void format() throws BadLocationException {
59 Preferences pref = Settings.getActivePreferences(document);
60
61 String code = getCode();
62
63 try {
64
65
66 String formattedContent = formatter.format(code, getOptions(pref));
67
68 if (setFormattedCode(code, formattedContent)) {
69 SwingUtilities.invokeLater(() -> {
70 if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
71 NotificationDisplayer.getDefault().notify("Format using JSQLFormatter", Icons.ICON_MANTICORE, "", null);
72 }
73 StatusDisplayer.getDefault().setStatusText("Format using JSQLFormatter");
74 });
75 }
76 } catch (FormattingFailedException ex) {
77 SwingUtilities.invokeLater(() -> {
78 StatusDisplayer.getDefault().setStatusText("Failed to format using JSQLFormatter formatter: " + ex.getMessage());
79 });
80
81 throw ex;
82 }
83 }
84
85 private String[] getOptions(Preferences pref) {
86 int i = 0;
87 String[] options = new String[FormattingOption.values().length];
88 options[i++] = toOption(pref, FormattingOption.OUTPUT_FORMAT, JSQLFormatter.getOutputFormat());
89 options[i++] = toOption(pref, FormattingOption.KEYWORD_SPELLING, JSQLFormatter.getKeywordSpelling());
90 options[i++] = toOption(pref, FormattingOption.FUNCTION_SPELLING, JSQLFormatter.getFunctionSpelling());
91 options[i++] = toOption(pref, FormattingOption.OBJECT_SPELLING, JSQLFormatter.getObjectSpelling());
92 options[i++] = toOption(pref, FormattingOption.INDENT_WIDTH, JSQLFormatter.getIndentWidth());
93 options[i++] = toOption(pref, FormattingOption.SEPARATION, JSQLFormatter.getSeparation());
94 options[i++] = toOption(pref, FormattingOption.SQUARE_BRACKET_QUOTATION, JSQLFormatter.getSquaredBracketQuotation());
95
96 return options;
97 }
98
99 private <E extends Enum<E>> String toOption(Preferences pref, JSQLFormatter.FormattingOption option, E defaultValue) {
100 return toOption(pref, option, String.valueOf(defaultValue));
101 }
102
103 private String toOption(Preferences pref, JSQLFormatter.FormattingOption option, String defaultValue) {
104 return option.toString() + "=" + pref.get(option.toString(), defaultValue);
105 }
106
107 private String toOption(Preferences pref, JSQLFormatter.FormattingOption option, int defaultValue) {
108 return String.valueOf(option) + "=" + pref.getInt(String.valueOf(option), defaultValue);
109 }
110 }