Added saving/opening, and conditional quitting.
This commit is contained in:
parent
9976b96c18
commit
b00c9c40e7
5 changed files with 127 additions and 39 deletions
|
@ -1,6 +1,6 @@
|
|||
# README #
|
||||
|
||||
This README would normally document whatever steps are necessary to get your application up and running.
|
||||
This is a scuffed project
|
||||
|
||||
### What is this repository for? ###
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@ import com.okseby.win.Window;
|
|||
public class Main {
|
||||
static Window w;
|
||||
|
||||
static String title = "Scrypt ";
|
||||
static double version = 0.1;
|
||||
static String title = "Scrypt";
|
||||
static String version = "v0.2";
|
||||
static Dimension winSize = new Dimension(1280, 720);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -1,7 +1,21 @@
|
|||
package com.okseby.io;
|
||||
|
||||
import java.io.FileReader;
|
||||
import com.okseby.win.Window;
|
||||
|
||||
public class Open {
|
||||
public void readFile(String path) {
|
||||
|
||||
static FileReader fr;
|
||||
|
||||
public static void readFile(String resource) {
|
||||
try {
|
||||
fr = new FileReader(resource);
|
||||
|
||||
Window.textArea.read(fr, null);
|
||||
|
||||
fr.close();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,22 @@
|
|||
package com.okseby.io;
|
||||
|
||||
import java.io.FileWriter;
|
||||
|
||||
import com.okseby.win.Window;
|
||||
|
||||
public class Save {
|
||||
public Save() {
|
||||
|
||||
static FileWriter fw;
|
||||
|
||||
public static void saveFile(String resource) {
|
||||
try {
|
||||
fw = new FileWriter(resource);
|
||||
|
||||
fw.write(Window.textArea.getText());
|
||||
|
||||
fw.close();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,10 +7,12 @@ import java.awt.event.ActionEvent;
|
|||
import java.awt.event.ActionListener;
|
||||
import javax.swing.Box;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.border.EtchedBorder;
|
||||
|
@ -21,77 +23,133 @@ import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
|
|||
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
|
||||
import org.fife.ui.rtextarea.RTextScrollPane;
|
||||
|
||||
import com.okseby.io.Open;
|
||||
import com.okseby.io.Save;
|
||||
import com.okseby.utils.Providers;
|
||||
|
||||
public class Window implements Runnable {
|
||||
public static RSyntaxTextArea textArea;
|
||||
public static boolean isFileOpened;
|
||||
|
||||
JFrame window;
|
||||
JMenuBar menuBar;
|
||||
JMenu fileMenu;
|
||||
JMenu editMenu;
|
||||
JFileChooser browser;
|
||||
JButton quitButton;
|
||||
JMenuItem fileMenuItem1, fileMenuItem2, fileMenuItem3;
|
||||
JPanel fileExplorer;
|
||||
JPanel mainPanel;
|
||||
RSyntaxTextArea textArea;
|
||||
RTextScrollPane scrollPane;
|
||||
CompletionProvider completionProvider;
|
||||
AutoCompletion autoComplete;
|
||||
|
||||
|
||||
String title;
|
||||
String p;
|
||||
int status;
|
||||
double version;
|
||||
Dimension winSize;
|
||||
|
||||
Object[] options = {"Save", "Discard"};
|
||||
|
||||
public void setup() {
|
||||
menuBar.add(fileMenu);
|
||||
menuBar.add(editMenu);
|
||||
menuBar.add(Box.createHorizontalGlue());
|
||||
menuBar.add(quitButton);
|
||||
|
||||
|
||||
fileMenu.add(fileMenuItem1);
|
||||
fileMenu.add(fileMenuItem2);
|
||||
fileMenu.add(fileMenuItem3);
|
||||
|
||||
quitButton.setContentAreaFilled(false);
|
||||
quitButton.setBorderPainted(false);
|
||||
quitButton.setFocusable(false);
|
||||
|
||||
quitButton.addMouseListener(new java.awt.event.MouseAdapter() {
|
||||
public void mouseEntered(java.awt.event.MouseEvent evt) {
|
||||
quitButton.setContentAreaFilled(true);
|
||||
quitButton.setBackground(Color.RED);
|
||||
}
|
||||
|
||||
public void mouseExited(java.awt.event.MouseEvent evt) {
|
||||
quitButton.setContentAreaFilled(false);
|
||||
quitButton.setBackground(UIManager.getColor("control"));
|
||||
}
|
||||
});
|
||||
|
||||
quitButton.addActionListener(new ActionListener() {
|
||||
browser.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.exit(0);
|
||||
if (e.getActionCommand().equals(javax.swing.JFileChooser.APPROVE_SELECTION)) {
|
||||
p = browser.getSelectedFile().getPath();
|
||||
|
||||
if (browser.getDialogType() == JFileChooser.OPEN_DIALOG) {
|
||||
Open.readFile(p);
|
||||
}
|
||||
|
||||
isFileOpened = true;
|
||||
window.setTitle(title + " - " + browser.getSelectedFile().getName());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fileMenuItem1.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
status = browser.showOpenDialog(window);
|
||||
}
|
||||
});
|
||||
|
||||
fileMenuItem2.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (isFileOpened) {
|
||||
Save.saveFile(p);
|
||||
} else {
|
||||
status = browser.showSaveDialog(window);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fileMenuItem3.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
textArea.setText(null);
|
||||
isFileOpened = false;
|
||||
window.setTitle(title);
|
||||
}
|
||||
});
|
||||
|
||||
quitButton.setContentAreaFilled(false);
|
||||
quitButton.setBorderPainted(false);
|
||||
quitButton.setFocusable(false);
|
||||
|
||||
quitButton.addMouseListener(new java.awt.event.MouseAdapter() {
|
||||
public void mouseEntered(java.awt.event.MouseEvent evt) {
|
||||
quitButton.setContentAreaFilled(true);
|
||||
quitButton.setBackground(Color.RED);
|
||||
}
|
||||
|
||||
public void mouseExited(java.awt.event.MouseEvent evt) {
|
||||
quitButton.setContentAreaFilled(false);
|
||||
quitButton.setBackground(UIManager.getColor("control"));
|
||||
}
|
||||
});
|
||||
|
||||
quitButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (isFileOpened || !textArea.getText().isBlank()) {
|
||||
JOptionPane.showOptionDialog(window, "Do you want to save your work?", "Unsaved changes!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]);
|
||||
} else {
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fileExplorer.setBorder(new EtchedBorder());
|
||||
fileExplorer.setBackground(new Color(0, 0, 0));
|
||||
fileExplorer.setPreferredSize(new Dimension(200, 200));
|
||||
|
||||
|
||||
mainPanel.setLayout(new BorderLayout());
|
||||
mainPanel.setBorder(new EtchedBorder());
|
||||
mainPanel.add(scrollPane);
|
||||
|
||||
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
|
||||
|
||||
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CPLUSPLUS);
|
||||
textArea.setCodeFoldingEnabled(true);
|
||||
|
||||
|
||||
LanguageSupportFactory.get().register(textArea);
|
||||
|
||||
|
||||
autoComplete.install(textArea);
|
||||
|
||||
|
||||
window.add(fileExplorer, BorderLayout.WEST);
|
||||
window.add(mainPanel);
|
||||
|
||||
|
||||
window.setTitle(title);
|
||||
window.setSize(winSize);
|
||||
window.setJMenuBar(menuBar);
|
||||
|
@ -99,7 +157,7 @@ public class Window implements Runnable {
|
|||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
public void build() {
|
||||
window = new JFrame();
|
||||
menuBar = new JMenuBar();
|
||||
|
@ -109,6 +167,7 @@ public class Window implements Runnable {
|
|||
fileMenuItem1 = new JMenuItem("Open");
|
||||
fileMenuItem2 = new JMenuItem("Save");
|
||||
fileMenuItem3 = new JMenuItem("Close File");
|
||||
browser = new JFileChooser("");
|
||||
fileExplorer = new JPanel();
|
||||
mainPanel = new JPanel();
|
||||
textArea = new RSyntaxTextArea();
|
||||
|
@ -122,9 +181,9 @@ public class Window implements Runnable {
|
|||
build();
|
||||
setup();
|
||||
}
|
||||
|
||||
public Window(String title, double version, Dimension winSize) {
|
||||
this.title = title + version;
|
||||
|
||||
public Window(String title, String version, Dimension winSize) {
|
||||
this.title = title + " " + version;
|
||||
this.winSize = winSize;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue