diff --git a/src/main/java/istlab/KisoJikken/Test/StyleTestDemo.java b/src/main/java/istlab/KisoJikken/Test/StyleTestDemo.java new file mode 100644 index 0000000..29ff72f --- /dev/null +++ b/src/main/java/istlab/KisoJikken/Test/StyleTestDemo.java @@ -0,0 +1,153 @@ +package istlab.KisoJikken.Test; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import javax.swing.plaf.FontUIResource; + +import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; +import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; +import org.fife.ui.rsyntaxtextarea.SyntaxConstants; +import org.fife.ui.rsyntaxtextarea.SyntaxScheme; +import org.fife.ui.rsyntaxtextarea.Token; +import org.fife.ui.rsyntaxtextarea.TokenMaker; +import org.fife.ui.rsyntaxtextarea.modes.JavaTokenMaker; +import org.fife.ui.rsyntaxtextarea.modes.PlainTextTokenMaker; +import org.fife.ui.rtextarea.RTextScrollPane; +import org.mdkt.compiler.InMemoryJavaCompiler; + +public class StyleTestDemo extends JFrame { + + RSyntaxTextArea textArea; + + public StyleTestDemo() { + + JPanel contentPane = new JPanel(new BorderLayout()); + RSyntaxDocument doc = new RSyntaxDocument("text/java"); + TokenMaker tokenMaker = new PlainTextTokenMaker(); + doc.setSyntaxStyle(tokenMaker); + textArea = new RSyntaxTextArea(doc); + // textArea.setCurrentLineHighlightColor(null); // カーソル行のハイライト色 + textArea.setHighlightCurrentLine(false); // カーソル行のハイライトを消す + textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); + + SyntaxScheme scheme = textArea.getSyntaxScheme(); + scheme.getStyle(Token.COMMENT_EOL).background = Color.cyan; + scheme.getStyle(Token.COMMENT_DOCUMENTATION).background = Color.cyan.brighter(); + scheme.getStyle(Token.COMMENT_KEYWORD).background = Color.green; + scheme.getStyle(Token.COMMENT_MARKUP).background = Color.gray; + scheme.getStyle(Token.COMMENT_MULTILINE).background = Color.yellow; + + textArea.setText("initial text\ncomment // com"); + textArea.revalidate(); + + // textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE); + textArea.setCodeFoldingEnabled(true); + textArea.setFont(new FontUIResource("sansserif", Font.PLAIN, 20)); + contentPane.add(new RTextScrollPane(textArea,false)); // 行番号を消す + + setContentPane(contentPane); + setTitle("MySyntax Demo"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + + JPanel north = new JPanel(); + JButton loadB = new JButton("Load"); + loadB.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + InputStream is = getClass().getResourceAsStream("/Hello.java"); + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + StringBuilder sb = new StringBuilder(); + String line; + try { + while((line=br.readLine())!=null){ + sb.append(line+"\n"); + } + } catch (IOException e1) { + e1.printStackTrace(); + } + textArea.setText(sb.toString()); + + } + }); + north.add(loadB); + + JButton saveB = new JButton("Save"); + saveB.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.out.println(textArea.getText()); + } + }); + north.add(saveB); + + JButton execB = new JButton("Exec"); + execB.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Class> helloClass; + try { + helloClass = InMemoryJavaCompiler.newInstance().compile("Hello", textArea.getText()); + System.out.println(helloClass.getClass().getName()); + + System.out.println(helloClass.getDeclaredConstructor().newInstance().toString()); + } catch (Exception e1) { + e1.printStackTrace(); + } + + } + }); + north.add(execB); + // JOptionPane.showMessageDialog(null, "test"); + + JButton javaB = new JButton("Java"); + javaB.addActionListener(e -> { + doc.setSyntaxStyle(new JavaTokenMaker()); + textArea.setSyntaxEditingStyle("text/c"); + // doc.setSyntaxStyle("text/java"); + // textArea.setDocument(doc); + } ); + north.add(javaB); + + JButton plainB = new JButton("Plain"); + javaB.addActionListener(e -> { + doc.setSyntaxStyle(new PlainTextTokenMaker()); + textArea.setSyntaxEditingStyle("text/plain"); + // doc.setSyntaxStyle("text/plain"); + // textArea.setDocument(doc); + } ); + north.add(plainB); + + + add(north, BorderLayout.NORTH); + pack(); + setLocationRelativeTo(null); + + + } + + public static void main(String[] args) { + // Instantiate GUI on the EDT. + SwingUtilities.invokeLater(() -> { + try { + String laf = UIManager.getSystemLookAndFeelClassName(); + UIManager.setLookAndFeel(laf); + } catch (Exception e) { + /* Never happens */ } + new StyleTestDemo().setVisible(true); + }); + } + +} \ No newline at end of file diff --git a/src/main/java/istlab/KisoJikken/Test/SyntaxSchemeDemo.java b/src/main/java/istlab/KisoJikken/Test/SyntaxSchemeDemo.java new file mode 100644 index 0000000..615a25a --- /dev/null +++ b/src/main/java/istlab/KisoJikken/Test/SyntaxSchemeDemo.java @@ -0,0 +1,158 @@ +package istlab.KisoJikken.Test; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; + +import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; +import javax.swing.WindowConstants; + +import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; +import org.fife.ui.rsyntaxtextarea.SyntaxConstants; +import org.fife.ui.rsyntaxtextarea.SyntaxScheme; +import org.fife.ui.rsyntaxtextarea.Theme; +import org.fife.ui.rsyntaxtextarea.Token; +import org.fife.ui.rtextarea.RTextScrollPane; + +/** + * A simple example showing how to modify the fonts and colors used in an + * RSyntaxTextArea. There are two methods to do this - via the Java API, and via + * an XML file. The latter method is preferred since it's more modular, and + * provides a way for your users to customize RSTA in your application. + *
+ * + * This example uses RSyntaxTextArea 3.1.4. + */ +public final class SyntaxSchemeDemo extends JFrame implements ActionListener { + + private static final long serialVersionUID = 1L; + + private RSyntaxTextArea textArea; + + private static final String TEXT = "public class ExampleSource {\n\n" + + " // Check out the crazy modified styles!\n" + + " public static void main(String[] args) {\n" + + " System.out.println(\"Hello, world!\");\n" + + " }\n\n" + + "}\n"; + + private SyntaxSchemeDemo() { + + JPanel cp = new JPanel(new BorderLayout()); + + textArea = new RSyntaxTextArea(20, 60); + textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); + textArea.setCodeFoldingEnabled(true); + textArea.setAntiAliasingEnabled(true); + RTextScrollPane sp = new RTextScrollPane(textArea); + cp.add(sp); + + textArea.setText(TEXT); + + JMenuBar mb = new JMenuBar(); + JMenu menu = new JMenu("File"); + mb.add(menu); + JMenuItem changeStyleProgrammaticallyItem = new JMenuItem( + "Change Style Programmatically"); + changeStyleProgrammaticallyItem + .setActionCommand("ChangeProgrammatically"); + changeStyleProgrammaticallyItem.addActionListener(this); + menu.add(changeStyleProgrammaticallyItem); + JMenuItem changeStyleViaThemesItem = new JMenuItem( + "Change Style via Theme XML"); + changeStyleViaThemesItem.setActionCommand("ChangeViaThemes"); + changeStyleViaThemesItem.addActionListener(this); + menu.add(changeStyleViaThemesItem); + setJMenuBar(mb); + + setContentPane(cp); + setTitle("Syntax Scheme Demo"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + pack(); + setLocationRelativeTo(null); + + } + + /** + * Listens for the selection of a menu item and performs an action + * accordingly. + */ + @Override + public void actionPerformed(ActionEvent e) { + String command = e.getActionCommand(); + if ("ChangeProgrammatically".equals(command)) { + changeStyleProgrammatically(); + } else if ("ChangeViaThemes".equals(command)) { + changeStyleViaThemeXml(); + } + } + + /** + * Changes the styles used in the editor programmatically. + */ + private void changeStyleProgrammatically() { + + // Set the font for all token types. + setFont(textArea, new Font("Comic Sans MS", Font.PLAIN, 16)); + + // Change a few things here and there. + SyntaxScheme scheme = textArea.getSyntaxScheme(); + scheme.getStyle(Token.RESERVED_WORD).background = Color.pink; + scheme.getStyle(Token.DATA_TYPE).foreground = Color.blue; + scheme.getStyle(Token.LITERAL_STRING_DOUBLE_QUOTE).underline = true; + scheme.getStyle(Token.COMMENT_EOL).font = new Font("Georgia", + Font.ITALIC, 12); + scheme.getStyle(Token.COMMENT_EOL).background = Color.cyan; + + textArea.revalidate(); + + } + + /** + * Changes the styles used by the editor via an XML file specification. This + * method is preferred because of its ease and modularity. + */ + private void changeStyleViaThemeXml() { + try { + Theme theme = Theme.load(getClass().getResourceAsStream( + "/org/fife/ui/rsyntaxtextarea/themes/eclipse.xml")); + theme.apply(textArea); + } catch (IOException ioe) { // Never happens + ioe.printStackTrace(); + } + } + + /** + * Set the font for all token types. + * + * @param textArea The text area to modify. + * @param font The font to use. + */ + private static void setFont(RSyntaxTextArea textArea, Font font) { + if (font != null) { + SyntaxScheme ss = textArea.getSyntaxScheme(); + ss = (SyntaxScheme) ss.clone(); + for (int i = 0; i < ss.getStyleCount(); i++) { + if (ss.getStyle(i) != null) { + ss.getStyle(i).font = font; + } + } + textArea.setSyntaxScheme(ss); + textArea.setFont(font); + } + } + + public static void main(String[] args) { + // Start all Swing applications on the EDT. + SwingUtilities.invokeLater(() -> new SyntaxSchemeDemo().setVisible(true)); + } + +} \ No newline at end of file