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

Commit

Permalink
Merge pull request #94 from markiewb/feature/JDK7support
Browse files Browse the repository at this point in the history
Rewrote Java 8 code to be compatible with Java 7
  • Loading branch information
bennycode committed Jan 30, 2016
2 parents efde593 + f738fee commit d4611d0
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 152 deletions.
57 changes: 29 additions & 28 deletions nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<org-netbeans-modules-editor-indent.CodeStyle.usedProfile>project</org-netbeans-modules-editor-indent.CodeStyle.usedProfile>
<org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>2</org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>2</org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>
<de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>true</de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterActiveProfile/>
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterLocation/>
<de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>false</de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>false</de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>
<de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>false</de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>
<de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>true</de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>
</properties>
</project-shared-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<org-netbeans-modules-editor-indent.CodeStyle.usedProfile>project</org-netbeans-modules-editor-indent.CodeStyle.usedProfile>
<org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>2</org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>2</org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>
<de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>true</de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterActiveProfile/>
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterLocation/>
<de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>false</de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>false</de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>
<de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>false</de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>
<de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>true</de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>
<netbeans.hint.jdkPlatform>JDK_1.7</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.mozilla.universalchardet.UniversalDetector;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
Expand Down Expand Up @@ -163,9 +164,49 @@ private static String readFirstLineWithSeparator(FileObject fo, Charset charset)
return firstLine;
}

public static String trimTrailingWhitespace(Stream<String> lines, String lineEnding) {
return lines.map((String content) -> {
return content.replaceAll("\\s+$", "");
}).collect(Collectors.joining(lineEnding));
public static String trimTrailingWhitespace(Collection<String> lines, String lineEnding) {
StringBuilder sb = new StringBuilder();
for (String content : lines) {
sb.append(content.replaceAll("\\s+$", ""));
sb.append(lineEnding);
}
return sb.toString().trim();
}

public static String trimTrailingWhitespace(String text, String lineEnding) {
List<String> lines = readLines(text);
return trimTrailingWhitespace(lines, lineEnding);
}

public static String replaceLineEndings(Collection<String> lines, String lineEnding) {
StringBuilder sb = new StringBuilder();
for (String content : lines) {
sb.append(content);
sb.append(lineEnding);
}
return sb.toString().trim();
}

public static String replaceLineEndings(String text, String lineEnding) {
List<String> lines = readLines(text);
return replaceLineEndings(lines, lineEnding);
}

public static List<String> readLines(String text) {
List<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new StringReader(text))) {

try {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
lines.add(line);
}
reader.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
return lines;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ public class FileObjectWriter {

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

public static synchronized void writeWithAtomicAction(DataObject dataObject, Charset cs, String content) {
public static synchronized void writeWithAtomicAction(final DataObject dataObject, final Charset cs, final String content) {
try {
FileObject fo = dataObject.getPrimaryFile();
EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class);
NbDocument.runAtomicAsUser(cookie.openDocument(), () -> {
try (Writer out = new OutputStreamWriter(fo.getOutputStream(), cs)) {
LOG.log(Level.INFO, "\u00ac Writing file");
out.write(content);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
final FileObject fo = dataObject.getPrimaryFile();
final EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class);
NbDocument.runAtomicAsUser(cookie.openDocument(), new Runnable() {
@Override
public void run() {
try (Writer out = new OutputStreamWriter(fo.getOutputStream(), cs)) {
LOG.log(Level.INFO, "\u00ac Writing file");
out.write(content);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
});
} catch (BadLocationException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public static void writeOnFileWithLines(File file, Charset charset, List<String>
}
}

public static void writeWithEditorKit(FileInfo info)
public static void writeWithEditorKit(final FileInfo info)
throws FileAccessException, IOException {

EditorCookie cookie = info.getCookie();
StyledDocument openedDocument = cookie.openDocument();
EditorKit kit = getEditorKit(info.getDataObject());
final EditorCookie cookie = info.getCookie();
final StyledDocument openedDocument = cookie.openDocument();
final EditorKit kit = getEditorKit(info.getDataObject());

try (InputStream is = new ByteArrayInputStream(info.getContentAsBytes())) {
// Backup caret position
Expand All @@ -125,49 +125,55 @@ public static void writeWithEditorKit(FileInfo info)
LOG.log(Level.WARNING, "Could not get Caret");
return;
}
int caretPosition = info.getCurrentCaretPosition();
Runnable runner = () -> {
NbDocument.runAtomic(openedDocument, () -> {
try {
// Wipe document
cookie.getDocument().remove(0, cookie.getDocument().getLength());

LOG.log(Level.INFO, "Write to \"is\": {0}", is);
LOG.log(Level.INFO, "Write to \"document\": {0}", cookie.getDocument());

// Read input stream into the document (which is a "write" operation)
try (Reader reader = new InputStreamReader(is, info.getCharset())) {
kit.read(reader, cookie.getDocument(), 0);
}
cookie.saveDocument();

info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name());
final int caretPosition = info.getCurrentCaretPosition();
Runnable runner = new Runnable() {
@Override
public void run() {
NbDocument.runAtomic(openedDocument, new Runnable() {
@Override
public void run() {
try {
// Wipe document
cookie.getDocument().remove(0, cookie.getDocument().getLength());

// Reset caret positon
// caretPosition -= info.getCaretOffset();
if (caretPosition < cookie.getDocument().getLength()) {
LOG.log(Level.INFO, "Moving caret position from {0} to: {1} / {2}",
new Object[]{info.getCaretOffset(), caretPosition, cookie.getDocument().getLength()});
caret.setDot(caretPosition);
}
LOG.log(Level.INFO, "Write to \"is\": {0}", is);
LOG.log(Level.INFO, "Write to \"document\": {0}", cookie.getDocument());

// Reformat code (to apply ident size & styles)
// TODO: Do this only if CodeStylePreferences have been changed
Reformat reformat = Reformat.get(cookie.getDocument());
reformat.lock();
try {
reformat.reformat(0, cookie.getDocument().getLength());
} catch (BadLocationException ex) {
LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage());
} finally {
reformat.unlock();
// Save document after reformat
// Read input stream into the document (which is a "write" operation)
try (Reader reader = new InputStreamReader(is, info.getCharset())) {
kit.read(reader, cookie.getDocument(), 0);
}
cookie.saveDocument();

info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name());

// Reset caret positon
// caretPosition -= info.getCaretOffset();
if (caretPosition < cookie.getDocument().getLength()) {
LOG.log(Level.INFO, "Moving caret position from {0} to: {1} / {2}",
new Object[]{info.getCaretOffset(), caretPosition, cookie.getDocument().getLength()});
caret.setDot(caretPosition);
}

// Reformat code (to apply ident size & styles)
// TODO: Do this only if CodeStylePreferences have been changed
Reformat reformat = Reformat.get(cookie.getDocument());
reformat.lock();
try {
reformat.reformat(0, cookie.getDocument().getLength());
} catch (BadLocationException ex) {
LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage());
} finally {
reformat.unlock();
// Save document after reformat
cookie.saveDocument();
}
} catch (BadLocationException | IOException ex) {
LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage());
}
} catch (BadLocationException | IOException ex) {
LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage());
}
});
}
};

if (SwingUtilities.isEventDispatchThread()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ public class Installer extends ModuleInstall {
@Override
public void restored() {
if (detectOldJava()) {
String title = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-title");
String message = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-message");
int messageType = NotifyDescriptor.ERROR_MESSAGE;
final String title = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-title");
final String message = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-message");
final int messageType = NotifyDescriptor.ERROR_MESSAGE;

ActionListener actionListener = (ActionEvent e) -> {
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, messageType));
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, messageType));
}
};
NotificationDisplayer.getDefault().notify(title, new MetalIconFactory.FileIcon16(), message, actionListener);
}
Expand Down Expand Up @@ -51,11 +54,11 @@ public static int getMinor() {
return 0;
}
}

/**
* Example: Patch version is "0_31" in Java 1.8.0_31
*
* @return
*
* @return
*/
public static String getPatch() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,21 @@ public FileInfo(DataObject dataObject) {
}

public Caret getCaret() {
Runnable runner = () -> {
NbDocument.runAtomic(cookie.getDocument(), () -> {
JEditorPane pane = cookie.getOpenedPanes()[0];
if (pane != null) {
currentCaret = pane.getCaret();
} else {
LOG.log(Level.SEVERE, "Could not get JEditorPane for Document");
}
});
Runnable runner = new Runnable() {
@Override
public void run() {
NbDocument.runAtomic(cookie.getDocument(), new Runnable() {
@Override
public void run() {
JEditorPane pane = cookie.getOpenedPanes()[0];
if (pane != null) {
currentCaret = pane.getCaret();
} else {
LOG.log(Level.SEVERE, "Could not get JEditorPane for Document");
}
}
});
}
};
if (SwingUtilities.isEventDispatchThread()) {
runner.run();
Expand Down
Loading

0 comments on commit d4611d0

Please sign in to comment.