I've made an IDE in Java, using swing. It uses a couple of resources (prefs.json and help.txt) and some imported .jar files for reading the JSON. I know the code's quite long, but the answer doesn't have to be a very detailed one.
package io.j4cobgarby.github;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import org.json.JSONException;
import org.json.JSONObject;
public class Main {
private JFrame frame;
private JTextField textField;
public static String currentFile = null;
private static java.util.Scanner scanner;
private JTextField cmd2Run;
JSONObject prefs = null;
String osType = "Unknown";
static JTextArea area = new JTextArea();
static JLabel filenameLabel = new JLabel("");
private JTextField status;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*
* @throws IOException
* @throws JSONException
*/
public Main() throws IOException, JSONException {
initialize();
String prefsText = convertStreamToString(Main.class.getResourceAsStream("prefs.json"));
osType = (System.getProperty("os.name").startsWith("Windows") ? "win" : "unix");
System.out.println(osType);
try {
prefs = new JSONObject(prefsText);
} catch (JSONException e) {
e.printStackTrace();
}
String pyCommandWin = (String) prefs.get("pyCommandWin");
String pyCommandUnix = (String) prefs.get("pyCommandUnix");
String lastFile = (String) prefs.get("lastFile");
int tabSize = prefs.getInt("tabSize");
area.setTabSize(tabSize);
area.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
JTextArea editArea = (JTextArea) e.getSource();
int linenum = 1;
int columnnum = 1;
try {
int caretpos = editArea.getCaretPosition();
linenum = editArea.getLineOfOffset(caretpos);
columnnum = caretpos - editArea.getLineStartOffset(linenum);
linenum += 1;
} catch (Exception ex) {
}
updateStatus(linenum, columnnum);
}
});
frame.getContentPane().add(area, BorderLayout.CENTER);
status = new JTextField();
frame.getContentPane().add(status, BorderLayout.SOUTH);
updateStatus(1, 1);
System.out.println(System.getProperty("os.name") + " is " + osType);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
} catch (InstantiationException e2) {
e2.printStackTrace();
} catch (IllegalAccessException e2) {
e2.printStackTrace();
} catch (UnsupportedLookAndFeelException e2) {
e2.printStackTrace();
}
frame.setTitle("Python IDE");
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
Component horizontalStrut_2 = Box.createHorizontalStrut(20);
toolBar.add(horizontalStrut_2);
area.setFont(new Font("Courier New", Font.PLAIN, 12));
// area.setText(convertStreamToString(Main.class.getResourceAsStream("help.txt")));
area.setText((lastFile.isEmpty()) ? convertStreamToString(Main.class.getResourceAsStream("help.txt"))
: readFile(lastFile, Charset.defaultCharset()));
frame.getContentPane().add(area, BorderLayout.CENTER);
filenameLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
filenameLabel.setToolTipText("File name");
toolBar.add(filenameLabel);
filenameLabel.setText("Untitled");
Component horizontalStrut_6 = Box.createHorizontalStrut(20);
toolBar.add(horizontalStrut_6);
JButton btnRun = new JButton("Run in python");
btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// cmd2Run.setText(pyCommandWin + " " + currentFile);
runPython((osType == "win") ? pyCommandWin : pyCommandUnix);
}
});
toolBar.add(btnRun);
Component horizontalStrut_1 = Box.createHorizontalStrut(20);
toolBar.add(horizontalStrut_1);
Component horizontalStrut_7 = Box.createHorizontalStrut(2);
toolBar.add(horizontalStrut_7);
JButton btnRunThisCommand = new JButton("Run this command");
btnRunThisCommand
.setToolTipText("Use this for running a script in a different language, or for command line args.");
btnRunThisCommand.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// format command so that text within braces is checked
Runtime rt = Runtime.getRuntime();
try {
rt.exec("cmd.exe /c start cmd.exe /k \"" + cmd2Run.getText() + "\"");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
toolBar.add(btnRunThisCommand);
cmd2Run = new JTextField();
cmd2Run.setToolTipText("Use this for running a script in a different language, or for command line args.");
cmd2Run.setHorizontalAlignment(SwingConstants.LEFT);
cmd2Run.setFont(new Font("Courier New", Font.PLAIN, 11));
toolBar.add(cmd2Run);
cmd2Run.setColumns(5);
Component horizontalStrut = Box.createHorizontalStrut(300);
toolBar.add(horizontalStrut);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setText(new File(System.getProperty("user.home")).toString());
toolBar.add(textField);
textField.setColumns(10);
JButton btnLoadFile = new JButton("Load file");
btnLoadFile.setHorizontalAlignment(SwingConstants.RIGHT);
btnLoadFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadFile(textField.getText());
}
});
toolBar.add(btnLoadFile);
JToolBar toolBar_1 = new JToolBar();
toolBar_1.setFloatable(false);
frame.getContentPane().add(toolBar_1, BorderLayout.SOUTH);
status = new JTextField();
status.setEditable(false);
toolBar_1.add(status);
status.setColumns(10);
JButton btnSettings = new JButton("Settings");
btnSettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
File file = new File("src/io/j4cobgarby/github/prefs.json");
loadFile(file.getAbsolutePath());
}
});
toolBar_1.add(btnSettings);
JScrollPane scrollPane = new JScrollPane(area);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmSaveCurrentFile = new JMenuItem(new AbstractAction("Save file") {
public void actionPerformed(ActionEvent e) {
saveFile(currentFile);
}
});
mntmSaveCurrentFile.setText("Save current file");
mnFile.add(mntmSaveCurrentFile);
JMenuItem mntmSaveUnderDifferent = new JMenuItem(new AbstractAction("Save file as..") {
public void actionPerformed(ActionEvent e) {
JTextField saveFileName = new JTextField(System.getProperty("user.home"));
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("What would you like to save this file as?"));
panel.add(saveFileName);
int result = JOptionPane.showConfirmDialog(null, panel, "Save as..", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
// On OK
saveFile(saveFileName.getText());
} else {
System.out.println("Cancelled");
}
}
});
mntmSaveUnderDifferent.setText("Save as..");
mnFile.add(mntmSaveUnderDifferent);
JMenuItem mntmCreateNewFile = new JMenuItem(new AbstractAction("Create new file") {
public void actionPerformed(ActionEvent e) {
JTextField newFilePath = new JTextField(System.getProperty("user.home"));
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("Path for new file:"));
panel.add(newFilePath);
int result = JOptionPane.showConfirmDialog(null, panel, "New file", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
// On OK
newFile(newFilePath.getText());
} else {
System.out.println("Cancelled");
}
}
});
mnFile.add(mntmCreateNewFile);
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 938, 618);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
if (JOptionPane.showConfirmDialog(frame, "Are you sure you wish to exit?", "Quit",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
try {
System.out.println("Exiting");
System.out.println(prefs.toString(4));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);
}
}
});
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
/*
* _ _ _ __ _ _
*| | | | | | / _| | | (_) _
*| |__| | ___| |_ __ ___ _ __ | |_ _ _ _ __ | |_ _ ___ _ __ ___(_)
*| __ |/ _ \ | '_ \ / _ \ '__| | _| | | | '_ \| __| |/ _ \| '_ \/ __|
*| | | | __/ | |_) | __/ | | | | |_| | | | | |_| | (_) | | | \__ \_
*|_| |_|\___|_| .__/ \___|_| |_| \__,_|_| |_|\__|_|\___/|_| |_|___(_)
* | |
* |_|
*/
/*
* Create a new file from a String path
*/
private static void newFile(String path) {
File file = new File(path);
try {
file.createNewFile();
loadFile(path);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Save a file to a String path
*/
private static void saveFile(String saveTo) {
FileWriter fwrite = null;
try {
fwrite = new FileWriter(new File(saveTo), false);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
fwrite.write(area.getText());
fwrite.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Saved " + currentFile);
}
/*
* Used with the caret position display; the actual output
*/
private void updateStatus(int linenumber, int columnnumber) {
status.setText(linenumber + ":" + columnnumber);
}
/*
* Loads a file to JTextArea area from a String path
*/
static void loadFile(String filepath) {
try {
area.setText(readFile(filepath, Charset.defaultCharset()));
filenameLabel.setText(new File(filepath).getName());
currentFile = filepath;
} catch (IOException e1) {
e1.printStackTrace();
}
}
/*
* Reads a file from a String path with Charset encoding and returns it as a
* String
*/
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
/*
* Returns a string of an InputStream as a string
*/
static String convertStreamToString(java.io.InputStream is) {
scanner = new java.util.Scanner(is);
java.util.Scanner s = scanner.useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
/*
* Runs the current file as Python in cmd.
*/
public void runPython(String cmdPrefix) {
Runtime rt = Runtime.getRuntime();
String pyCmd = cmdPrefix + " " + currentFile;
System.out.println(osType);
try {
rt.exec("cmd.exe /c start cmd.exe /k \"" + pyCmd + "\"");
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Shows a popup
*/
@SuppressWarnings("unused")
private static void addPopup(final Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
If (understandably) no one wants the read all of this, I'm mainly just looking at the naming conventions and also the way I've used swing, as this is my first Swing project.