diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/viewer/FileViewer.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/viewer/FileViewer.java index 2a3cac77..1571232d 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/viewer/FileViewer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/viewer/FileViewer.java @@ -3,13 +3,11 @@ package the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer; import java.awt.BorderLayout; import java.awt.Font; import java.awt.image.BufferedImage; -import java.io.File; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import org.apache.commons.io.FilenameUtils; -import org.fife.ui.rsyntaxtextarea.FileTypeUtil; import org.imgscalr.Scalr; import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.Configuration; @@ -21,6 +19,7 @@ import the.bytecode.club.bytecodeviewer.resources.Resource; import the.bytecode.club.bytecodeviewer.resources.ResourceContainer; import the.bytecode.club.bytecodeviewer.resources.ResourceType; import the.bytecode.club.bytecodeviewer.util.MiscUtils; +import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage; /*************************************************************************** * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * @@ -144,7 +143,7 @@ public class FileViewer extends ResourceViewer } textArea.setCodeFoldingEnabled(true); - textArea.setSyntaxEditingStyle(FileTypeUtil.get().guessContentType(new File(nameLowerCase))); + SyntaxLanguage.setLanguage(textArea, nameLowerCase); textArea.setText(contentsAsString); textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, (int) BytecodeViewer.viewer.fontSpinner.getValue())); textArea.setCaretPosition(0); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java index 4bdfe063..2ecfa99e 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java @@ -15,7 +15,6 @@ import javax.swing.JPanel; import me.konloch.kontainer.io.DiskReader; import me.konloch.kontainer.io.DiskWriter; import org.apache.commons.compress.utils.FileNameUtils; -import org.fife.ui.rsyntaxtextarea.FileTypeUtil; import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.Configuration; import the.bytecode.club.bytecodeviewer.gui.components.FileChooser; @@ -27,6 +26,7 @@ import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJMenu; import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJMenuItem; import the.bytecode.club.bytecodeviewer.util.DialogUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils; +import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage; import static the.bytecode.club.bytecodeviewer.Constants.fs; import static the.bytecode.club.bytecodeviewer.Constants.tempDirectory; @@ -88,7 +88,7 @@ public class PluginWriter extends JFrame area.setOnCtrlS(this::save); area.setText(content); area.setCaretPosition(0); - area.setSyntaxEditingStyle(FileTypeUtil.get().guessContentType(new File(pluginName))); + SyntaxLanguage.setLanguage(area, pluginName); content = null; JButton run = new JButton("Run"); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/SyntaxLanguage.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/SyntaxLanguage.java index 60aab8dc..1206e9af 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/SyntaxLanguage.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/SyntaxLanguage.java @@ -1,6 +1,9 @@ package the.bytecode.club.bytecodeviewer.util; +import java.io.File; import java.util.function.BiFunction; +import org.fife.ui.rsyntaxtextarea.FileTypeUtil; +import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; import org.fife.ui.rsyntaxtextarea.SyntaxConstants; /*************************************************************************** @@ -23,11 +26,8 @@ import org.fife.ui.rsyntaxtextarea.SyntaxConstants; /** * @author ThexXTURBOXx - * @deprecated See {@link org.fife.ui.rsyntaxtextarea.FileTypeUtil#guessContentType(java.io.File)} */ -@Deprecated -public enum SyntaxLanguage -{ +public enum SyntaxLanguage { XML(SyntaxConstants.SYNTAX_STYLE_XML, (n, c) -> n.endsWith(".xml") || c.startsWith(" n.endsWith(".py") || n.endsWith(".python")), @@ -69,6 +69,8 @@ public enum SyntaxLanguage public static final SyntaxLanguage[] VALUES = values(); + private static final FileTypeUtil FILE_TYPE_UTIL = FileTypeUtil.get(); + private final BiFunction criteria; private final String syntaxConstant; @@ -86,6 +88,10 @@ public enum SyntaxLanguage return syntaxConstant; } + /** + * @deprecated See {@link #setLanguage(String, RSyntaxTextArea)}. + */ + @Deprecated public static SyntaxLanguage detectLanguage(String fileName, String content) { for (SyntaxLanguage lang : VALUES) { if (lang.isLanguage(fileName, content)) { @@ -94,4 +100,12 @@ public enum SyntaxLanguage } return NONE; } + + public static void setLanguage(RSyntaxTextArea area, String fileName) { + String type = FILE_TYPE_UTIL.guessContentType(new File(fileName)); + if (type == null || type.equals(SyntaxConstants.SYNTAX_STYLE_NONE)) { + type = FILE_TYPE_UTIL.guessContentType(area); + } + area.setSyntaxEditingStyle(type); + } }