Skip to content

Instantly share code, notes, and snippets.

@bmcculley
Created April 3, 2015 23:06
Show Gist options
  • Save bmcculley/2f7409ce8ca4da52d7a2 to your computer and use it in GitHub Desktop.
Save bmcculley/2f7409ce8ca4da52d7a2 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.python.core.*;
import org.python.util.PythonInterpreter;
public class Document extends JFrame implements ActionListener
{
private JTextArea ta;
private int count;
private JMenuBar menuBar;
private JMenu fileM,editM,helpM,scriptsM;
private JScrollPane scpane;
private JMenuItem exitI,cutI,copyI,pasteI,selectI,saveI,loadI,addScriptI,revScriptI,aboutI;
private String pad;
private JToolBar toolBar;
public Document()
{
super("jTxt+");
setSize(600, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
count = 0;
pad = " ";
ta = new JTextArea(); //textarea
menuBar = new JMenuBar(); //menubar
fileM = new JMenu("File"); //file menu
editM = new JMenu("Edit"); //edit menu
scriptsM = new JMenu("Scripts"); //scripts menu
helpM = new JMenu("Help"); //view menu
scpane = new JScrollPane(ta); //scrollpane and add textarea to scrollpane
exitI = new JMenuItem("Exit");
cutI = new JMenuItem("Cut");
copyI = new JMenuItem("Copy");
pasteI = new JMenuItem("Paste");
selectI = new JMenuItem("Select All"); //menuitems
saveI = new JMenuItem("Save"); //menuitems
loadI = new JMenuItem("Load"); //menuitems
aboutI = new JMenuItem("About"); //menuitems
addScriptI = new JMenuItem("Add new");
revScriptI = new JMenuItem("Reverse Text");
toolBar = new JToolBar();
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
setJMenuBar(menuBar);
menuBar.add(fileM);
menuBar.add(editM);
menuBar.add(scriptsM);
menuBar.add(helpM);
// file menu items
fileM.add(saveI);
fileM.add(loadI);
fileM.add(exitI);
// edit menu items
editM.add(cutI);
editM.add(copyI);
editM.add(pasteI);
editM.add(selectI);
// scripts menu items
scriptsM.add(addScriptI);
scriptsM.add(revScriptI);
// help menu items
helpM.add(aboutI);
// keyboard shortcuts
saveI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
loadI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
cutI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
copyI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
pasteI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
selectI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
pane.add(scpane,BorderLayout.CENTER);
pane.add(toolBar,BorderLayout.SOUTH);
saveI.addActionListener(this);
loadI.addActionListener(this);
exitI.addActionListener(this);
cutI.addActionListener(this);
copyI.addActionListener(this);
pasteI.addActionListener(this);
selectI.addActionListener(this);
aboutI.addActionListener(this);
revScriptI.addActionListener(this);
//setText();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JMenuItem choice = (JMenuItem) e.getSource();
if (choice == saveI)
{
//not yet implmented
}
else if (choice == exitI)
System.exit(0);
else if (choice == cutI)
{
pad = ta.getSelectedText();
ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
}
else if (choice == copyI)
pad = ta.getSelectedText();
else if (choice == pasteI)
ta.insert(pad, ta.getCaretPosition());
else if (choice == selectI)
ta.selectAll();
else if (choice == revScriptI)
{
String txt = RunScript(ta.getText());
setText(txt);
}
else if (e.getSource() == aboutI)
{
// show dialog box
JOptionPane.showMessageDialog(this,
"jTxt+ a simple text editor");
}
}
public void setText(String TextToSet)
{
ta.setText(TextToSet);
}
public String RunScript(String sendText) {
PythonInterpreter interpreter = new PythonInterpreter();
String scriptname = "script.py";
interpreter.execfile(scriptname);
PyObject someFunc = interpreter.get("helloWorld");
PyObject result = someFunc.__call__(new PyString(sendText));
String realResult = (String) result.__tojava__(String.class);
return realResult;
}
public static void main(String[] args)
{
new Document();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment