Skip to content
This repository was archived by the owner on Apr 10, 2021. It is now read-only.

Commit d4611d0

Browse files
committed
Merge pull request #94 from markiewb/feature/JDK7support
Rewrote Java 8 code to be compatible with Java 7
2 parents efde593 + f738fee commit d4611d0

File tree

11 files changed

+202
-152
lines changed

11 files changed

+202
-152
lines changed

nb-configuration.xml

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<project-shared-configuration>
3-
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
4-
<org-netbeans-modules-editor-indent.CodeStyle.usedProfile>project</org-netbeans-modules-editor-indent.CodeStyle.usedProfile>
5-
<org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>2</org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>
6-
<org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>2</org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>
7-
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
8-
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
9-
<org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>
10-
<org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>
11-
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>
12-
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>
13-
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>
14-
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>
15-
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>
16-
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-line-wrap>
17-
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAnnotations>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAnnotations>
18-
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>
19-
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>
20-
<de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>true</de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>
21-
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterActiveProfile/>
22-
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterLocation/>
23-
<de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>false</de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>
24-
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>false</de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>
25-
<de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>false</de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>
26-
<de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>true</de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>
27-
</properties>
28-
</project-shared-configuration>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
4+
<org-netbeans-modules-editor-indent.CodeStyle.usedProfile>project</org-netbeans-modules-editor-indent.CodeStyle.usedProfile>
5+
<org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>2</org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>
6+
<org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>2</org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>
7+
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
8+
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
9+
<org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>
10+
<org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>
11+
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>
12+
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>
13+
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>
14+
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>
15+
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>
16+
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-line-wrap>
17+
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAnnotations>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAnnotations>
18+
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>
19+
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>
20+
<de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>true</de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>
21+
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterActiveProfile/>
22+
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterLocation/>
23+
<de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>false</de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>
24+
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>false</de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>
25+
<de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>false</de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>
26+
<de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>true</de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>
27+
<netbeans.hint.jdkPlatform>JDK_1.7</netbeans.hint.jdkPlatform>
28+
</properties>
29+
</project-shared-configuration>

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@
244244
<artifactId>maven-compiler-plugin</artifactId>
245245
<version>3.1</version>
246246
<configuration>
247-
<source>1.8</source>
248-
<target>1.8</target>
247+
<source>1.7</source>
248+
<target>1.7</target>
249249
</configuration>
250250
</plugin>
251251
<plugin>

src/main/java/com/welovecoding/nbeditorconfig/io/reader/FileInfoReader.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import java.io.IOException;
88
import java.io.InputStream;
99
import java.io.InputStreamReader;
10+
import java.io.StringReader;
1011
import java.nio.charset.Charset;
1112
import java.nio.charset.StandardCharsets;
12-
import java.util.logging.Level;
13+
import java.util.ArrayList;
14+
import java.util.Collection;
15+
import java.util.List;
1316
import java.util.logging.Logger;
14-
import java.util.stream.Collectors;
15-
import java.util.stream.Stream;
1617
import org.mozilla.universalchardet.UniversalDetector;
1718
import org.openide.filesystems.FileObject;
1819
import org.openide.util.Exceptions;
@@ -163,9 +164,49 @@ private static String readFirstLineWithSeparator(FileObject fo, Charset charset)
163164
return firstLine;
164165
}
165166

166-
public static String trimTrailingWhitespace(Stream<String> lines, String lineEnding) {
167-
return lines.map((String content) -> {
168-
return content.replaceAll("\\s+$", "");
169-
}).collect(Collectors.joining(lineEnding));
167+
public static String trimTrailingWhitespace(Collection<String> lines, String lineEnding) {
168+
StringBuilder sb = new StringBuilder();
169+
for (String content : lines) {
170+
sb.append(content.replaceAll("\\s+$", ""));
171+
sb.append(lineEnding);
172+
}
173+
return sb.toString().trim();
174+
}
175+
176+
public static String trimTrailingWhitespace(String text, String lineEnding) {
177+
List<String> lines = readLines(text);
178+
return trimTrailingWhitespace(lines, lineEnding);
179+
}
180+
181+
public static String replaceLineEndings(Collection<String> lines, String lineEnding) {
182+
StringBuilder sb = new StringBuilder();
183+
for (String content : lines) {
184+
sb.append(content);
185+
sb.append(lineEnding);
186+
}
187+
return sb.toString().trim();
188+
}
189+
190+
public static String replaceLineEndings(String text, String lineEnding) {
191+
List<String> lines = readLines(text);
192+
return replaceLineEndings(lines, lineEnding);
193+
}
194+
195+
public static List<String> readLines(String text) {
196+
List<String> lines = new ArrayList<>();
197+
try (BufferedReader reader = new BufferedReader(new StringReader(text))) {
198+
199+
try {
200+
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
201+
lines.add(line);
202+
}
203+
reader.close();
204+
} catch (IOException ex) {
205+
Exceptions.printStackTrace(ex);
206+
}
207+
} catch (IOException ex) {
208+
Exceptions.printStackTrace(ex);
209+
}
210+
return lines;
170211
}
171212
}

src/main/java/com/welovecoding/nbeditorconfig/io/writer/FileObjectWriter.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ public class FileObjectWriter {
2525

2626
private static final Logger LOG = Logger.getLogger(FileObjectWriter.class.getName());
2727

28-
public static synchronized void writeWithAtomicAction(DataObject dataObject, Charset cs, String content) {
28+
public static synchronized void writeWithAtomicAction(final DataObject dataObject, final Charset cs, final String content) {
2929
try {
30-
FileObject fo = dataObject.getPrimaryFile();
31-
EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class);
32-
NbDocument.runAtomicAsUser(cookie.openDocument(), () -> {
33-
try (Writer out = new OutputStreamWriter(fo.getOutputStream(), cs)) {
34-
LOG.log(Level.INFO, "\u00ac Writing file");
35-
out.write(content);
36-
} catch (IOException ex) {
37-
Exceptions.printStackTrace(ex);
30+
final FileObject fo = dataObject.getPrimaryFile();
31+
final EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class);
32+
NbDocument.runAtomicAsUser(cookie.openDocument(), new Runnable() {
33+
@Override
34+
public void run() {
35+
try (Writer out = new OutputStreamWriter(fo.getOutputStream(), cs)) {
36+
LOG.log(Level.INFO, "\u00ac Writing file");
37+
out.write(content);
38+
} catch (IOException ex) {
39+
Exceptions.printStackTrace(ex);
40+
}
3841
}
3942
});
4043
} catch (BadLocationException ex) {

src/main/java/com/welovecoding/nbeditorconfig/io/writer/StyledDocumentWriter.java

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ public static void writeOnFileWithLines(File file, Charset charset, List<String>
111111
}
112112
}
113113

114-
public static void writeWithEditorKit(FileInfo info)
114+
public static void writeWithEditorKit(final FileInfo info)
115115
throws FileAccessException, IOException {
116116

117-
EditorCookie cookie = info.getCookie();
118-
StyledDocument openedDocument = cookie.openDocument();
119-
EditorKit kit = getEditorKit(info.getDataObject());
117+
final EditorCookie cookie = info.getCookie();
118+
final StyledDocument openedDocument = cookie.openDocument();
119+
final EditorKit kit = getEditorKit(info.getDataObject());
120120

121121
try (InputStream is = new ByteArrayInputStream(info.getContentAsBytes())) {
122122
// Backup caret position
@@ -125,49 +125,55 @@ public static void writeWithEditorKit(FileInfo info)
125125
LOG.log(Level.WARNING, "Could not get Caret");
126126
return;
127127
}
128-
int caretPosition = info.getCurrentCaretPosition();
129-
Runnable runner = () -> {
130-
NbDocument.runAtomic(openedDocument, () -> {
131-
try {
132-
// Wipe document
133-
cookie.getDocument().remove(0, cookie.getDocument().getLength());
134-
135-
LOG.log(Level.INFO, "Write to \"is\": {0}", is);
136-
LOG.log(Level.INFO, "Write to \"document\": {0}", cookie.getDocument());
137-
138-
// Read input stream into the document (which is a "write" operation)
139-
try (Reader reader = new InputStreamReader(is, info.getCharset())) {
140-
kit.read(reader, cookie.getDocument(), 0);
141-
}
142-
cookie.saveDocument();
143-
144-
info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name());
128+
final int caretPosition = info.getCurrentCaretPosition();
129+
Runnable runner = new Runnable() {
130+
@Override
131+
public void run() {
132+
NbDocument.runAtomic(openedDocument, new Runnable() {
133+
@Override
134+
public void run() {
135+
try {
136+
// Wipe document
137+
cookie.getDocument().remove(0, cookie.getDocument().getLength());
145138

146-
// Reset caret positon
147-
// caretPosition -= info.getCaretOffset();
148-
if (caretPosition < cookie.getDocument().getLength()) {
149-
LOG.log(Level.INFO, "Moving caret position from {0} to: {1} / {2}",
150-
new Object[]{info.getCaretOffset(), caretPosition, cookie.getDocument().getLength()});
151-
caret.setDot(caretPosition);
152-
}
139+
LOG.log(Level.INFO, "Write to \"is\": {0}", is);
140+
LOG.log(Level.INFO, "Write to \"document\": {0}", cookie.getDocument());
153141

154-
// Reformat code (to apply ident size & styles)
155-
// TODO: Do this only if CodeStylePreferences have been changed
156-
Reformat reformat = Reformat.get(cookie.getDocument());
157-
reformat.lock();
158-
try {
159-
reformat.reformat(0, cookie.getDocument().getLength());
160-
} catch (BadLocationException ex) {
161-
LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage());
162-
} finally {
163-
reformat.unlock();
164-
// Save document after reformat
142+
// Read input stream into the document (which is a "write" operation)
143+
try (Reader reader = new InputStreamReader(is, info.getCharset())) {
144+
kit.read(reader, cookie.getDocument(), 0);
145+
}
165146
cookie.saveDocument();
147+
148+
info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name());
149+
150+
// Reset caret positon
151+
// caretPosition -= info.getCaretOffset();
152+
if (caretPosition < cookie.getDocument().getLength()) {
153+
LOG.log(Level.INFO, "Moving caret position from {0} to: {1} / {2}",
154+
new Object[]{info.getCaretOffset(), caretPosition, cookie.getDocument().getLength()});
155+
caret.setDot(caretPosition);
156+
}
157+
158+
// Reformat code (to apply ident size & styles)
159+
// TODO: Do this only if CodeStylePreferences have been changed
160+
Reformat reformat = Reformat.get(cookie.getDocument());
161+
reformat.lock();
162+
try {
163+
reformat.reformat(0, cookie.getDocument().getLength());
164+
} catch (BadLocationException ex) {
165+
LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage());
166+
} finally {
167+
reformat.unlock();
168+
// Save document after reformat
169+
cookie.saveDocument();
170+
}
171+
} catch (BadLocationException | IOException ex) {
172+
LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage());
166173
}
167-
} catch (BadLocationException | IOException ex) {
168-
LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage());
169174
}
170175
});
176+
}
171177
};
172178

173179
if (SwingUtilities.isEventDispatchThread()) {

src/main/java/com/welovecoding/nbeditorconfig/listener/Installer.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ public class Installer extends ModuleInstall {
1616
@Override
1717
public void restored() {
1818
if (detectOldJava()) {
19-
String title = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-title");
20-
String message = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-message");
21-
int messageType = NotifyDescriptor.ERROR_MESSAGE;
19+
final String title = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-title");
20+
final String message = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-message");
21+
final int messageType = NotifyDescriptor.ERROR_MESSAGE;
2222

23-
ActionListener actionListener = (ActionEvent e) -> {
24-
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, messageType));
23+
ActionListener actionListener = new ActionListener() {
24+
@Override
25+
public void actionPerformed(ActionEvent e) {
26+
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, messageType));
27+
}
2528
};
2629
NotificationDisplayer.getDefault().notify(title, new MetalIconFactory.FileIcon16(), message, actionListener);
2730
}
@@ -51,11 +54,11 @@ public static int getMinor() {
5154
return 0;
5255
}
5356
}
54-
57+
5558
/**
5659
* Example: Patch version is "0_31" in Java 1.8.0_31
57-
*
58-
* @return
60+
*
61+
* @return
5962
*/
6063
public static String getPatch() {
6164
try {

src/main/java/com/welovecoding/nbeditorconfig/processor/FileInfo.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,21 @@ public FileInfo(DataObject dataObject) {
4242
}
4343

4444
public Caret getCaret() {
45-
Runnable runner = () -> {
46-
NbDocument.runAtomic(cookie.getDocument(), () -> {
47-
JEditorPane pane = cookie.getOpenedPanes()[0];
48-
if (pane != null) {
49-
currentCaret = pane.getCaret();
50-
} else {
51-
LOG.log(Level.SEVERE, "Could not get JEditorPane for Document");
52-
}
53-
});
45+
Runnable runner = new Runnable() {
46+
@Override
47+
public void run() {
48+
NbDocument.runAtomic(cookie.getDocument(), new Runnable() {
49+
@Override
50+
public void run() {
51+
JEditorPane pane = cookie.getOpenedPanes()[0];
52+
if (pane != null) {
53+
currentCaret = pane.getCaret();
54+
} else {
55+
LOG.log(Level.SEVERE, "Could not get JEditorPane for Document");
56+
}
57+
}
58+
});
59+
}
5460
};
5561
if (SwingUtilities.isEventDispatchThread()) {
5662
runner.run();

0 commit comments

Comments
 (0)