1
2
3
4
5
6
7
8
9
10
11 package de.funfried.netbeans.plugins.external.formatter.java.eclipse;
12
13 import java.util.SortedSet;
14 import java.util.prefs.Preferences;
15
16 import javax.swing.text.StyledDocument;
17
18 import org.apache.commons.lang3.tuple.Pair;
19 import org.netbeans.api.annotations.common.NonNull;
20
21 import de.funfried.netbeans.plugins.external.formatter.eclipse.AbstractEclipseFormatJob;
22 import de.funfried.netbeans.plugins.external.formatter.exceptions.CannotLoadConfigurationException;
23 import de.funfried.netbeans.plugins.external.formatter.exceptions.ConfigReadException;
24 import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
25 import de.funfried.netbeans.plugins.external.formatter.exceptions.ProfileNotFoundException;
26 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
27
28
29
30
31
32
33
34
35 class EclipseFormatJob extends AbstractEclipseFormatJob {
36
37 private final EclipseJavaFormatterWrapper formatter;
38
39
40
41
42
43
44
45
46 EclipseFormatJob(StyledDocument document, EclipseJavaFormatterWrapper formatter, SortedSet<Pair<Integer, Integer>> changedElements) {
47 super(document, changedElements);
48
49 this.formatter = formatter;
50 }
51
52
53
54
55 @Override
56 protected String getFormattedContent(Preferences pref, String formatterFile, String formatterProfile, String code)
57 throws ConfigReadException, ProfileNotFoundException, CannotLoadConfigurationException, FormattingFailedException {
58 String sourceLevel = pref.get(EclipseJavaFormatterSettings.SOURCELEVEL, "");
59
60 SortedSet<Pair<Integer, Integer>> regions = getFormatableSections(code);
61
62 return formatter.format(formatterFile, formatterProfile, code, getLineFeed(pref), sourceLevel, regions);
63 }
64
65
66
67
68 @Override
69 protected String getFormatterFile(Preferences pref) {
70 return EclipseJavaFormatterSettings.getEclipseFormatterFile(pref, document);
71 }
72
73
74
75
76 @Override
77 protected String getFormatterProfile(Preferences pref) {
78 return pref.get(EclipseJavaFormatterSettings.ACTIVE_PROFILE, "");
79 }
80
81
82
83
84 @Override
85 protected String getLineFeed(Preferences pref) {
86 String lineFeedSetting = pref.get(EclipseJavaFormatterSettings.LINEFEED, "");
87 return Settings.getLineFeed(lineFeedSetting, System.getProperty("line.separator"));
88 }
89
90
91
92
93 @NonNull
94 @Override
95 protected String getNotificationMessageForEclipseFormatterConfigurationFileType(String formatterFile, String formatterProfile) {
96 String msg = "";
97 if (EclipseJavaFormatterSettings.isWorkspaceMechanicFile(formatterFile)) {
98
99 msg = String.format("Using %s", formatterFile);
100 } else if (EclipseJavaFormatterSettings.isXMLConfigurationFile(formatterFile)) {
101
102 msg = String.format("Using profile '%s' from %s", formatterProfile, formatterFile);
103 } else if (EclipseJavaFormatterSettings.isProjectSetting(formatterFile)) {
104
105 msg = String.format("Using %s", formatterFile);
106 }
107
108 return msg;
109 }
110 }