Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package editor;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.InputEvent;
- import java.awt.event.KeyEvent;
- import java.awt.event.WindowEvent;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.util.Scanner;
- import javax.swing.Action;
- import javax.swing.ActionMap;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- 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.JScrollPane;
- import javax.swing.JSplitPane;
- import javax.swing.JTabbedPane;
- import javax.swing.JTextArea;
- import javax.swing.JToolBar;
- import javax.swing.KeyStroke;
- import javax.swing.event.DocumentEvent;
- import javax.swing.event.DocumentListener;
- import javax.swing.text.DefaultEditorKit;
- import javax.swing.text.Element;
- public class Frame extends JFrame {
- public JFrame mainFrame;
- public JMenuBar menu;
- public JMenu file;
- public JMenu function;
- public JMenu edit;
- public JMenu font;
- public JMenu fontDecor;
- public JMenu fontSize;
- public JMenu help;
- public JMenuItem fileNew;
- public JMenuItem fileOpen;
- public JMenuItem fileSave;
- public JMenuItem fileSaveAs;
- public JMenuItem fileExit;
- public JMenuItem functionTokenize;
- public JMenuItem functionCompile;
- public JMenuItem functionExecute;
- public JMenuItem editToggleEditable;
- public JMenuItem animation;
- public JMenuItem fontArial;
- public JMenuItem fontSerif;
- public JMenuItem fontComicSansMS;
- public JMenuItem fontConsolas;
- public JMenuItem fontDecorPlain;
- public JMenuItem fontDecorBold;
- public JMenuItem fontDecorItalic;
- public JMenuItem fontDecorBoldItalic;
- public JMenuItem fontSizeSmall;
- public JMenuItem fontSizeNormal;
- public JMenuItem fontSizeBig;
- public JMenuItem fontSizeHuge;
- public JMenuItem helpAbout;
- public JMenuItem helpHelp;
- public JTextArea field;
- public JTextArea lineSet;
- public JScrollPane scroller;
- public JScrollPane scroll;
- public JLabel status;
- public String fontF = "Arial";
- public int fontD = Font.PLAIN;
- public int fontS = 14;
- public Font origFont;
- public JFileChooser fileSelect;
- public File currentFile;
- public JFileChooser dialog = new JFileChooser(System.getProperty("user.dir"));
- public Frame() {
- menu = new JMenuBar();
- setJMenuBar(menu);
- field = new JTextArea();
- status = new JLabel("Welcome to Text Editor!");
- fileSelect = new JFileChooser();
- origFont = (new Font(fontF, fontD, fontS));
- field.setFont(origFont);
- file = new JMenu("File");
- function = new JMenu("Function");
- edit = new JMenu("Edit");
- font = new JMenu("Font");
- fontDecor = new JMenu("Font Decorations");
- fontSize = new JMenu("Font Size");
- help = new JMenu("Help");
- ActionMap m = field.getActionMap();
- Action Cut = m.get(DefaultEditorKit.cutAction);
- Action Copy = m.get(DefaultEditorKit.copyAction);
- Action Paste = m.get(DefaultEditorKit.pasteAction);
- edit.add(Cut);edit.add(Copy);edit.add(Paste);
- edit.getItem(0).setText("Cut");
- edit.getItem(1).setText("Copy");
- edit.getItem(2).setText("Paste");
- fileNew = new JMenuItem("New");
- fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
- fileOpen = new JMenuItem("Open...");
- fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
- fileSave = new JMenuItem("Save");
- fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
- fileSaveAs = new JMenuItem("Save As...");
- fileSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK+InputEvent.ALT_MASK));
- fileExit = new JMenuItem("Exit");
- fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK));
- file.add(fileNew);
- file.add(fileOpen);
- file.add(fileSave);
- file.add(fileSaveAs);
- file.add(fileExit);
- functionTokenize = new JMenuItem("Tokenize");
- functionCompile = new JMenuItem("Compile");
- functionExecute = new JMenuItem("Execute");
- function.add(functionTokenize);
- function.add(functionCompile);
- function.add(functionExecute);
- editToggleEditable = new JMenuItem("Toggle Editable...");
- animation = new JMenuItem("View Animation");
- edit.add(editToggleEditable);
- edit.add(animation);
- fontArial = new JMenuItem("Arial");
- fontSerif = new JMenuItem("Serif");
- fontComicSansMS = new JMenuItem("ComicSansMS");
- fontConsolas = new JMenuItem("Consolas");
- font.add(fontArial);
- font.add(fontSerif);
- font.add(fontComicSansMS);
- font.add(fontConsolas);
- fontDecorPlain = new JMenuItem("Plain");
- fontDecorBold = new JMenuItem("Bold");
- fontDecorItalic = new JMenuItem("Italic");
- fontDecorBoldItalic = new JMenuItem("Bold & Italic");
- fontDecor.add(fontDecorPlain);
- fontDecor.add(fontDecorBold);
- fontDecor.add(fontDecorItalic);
- fontDecor.add(fontDecorBoldItalic);
- fontSizeSmall = new JMenuItem("Small");
- fontSizeNormal = new JMenuItem("Normal");
- fontSizeBig = new JMenuItem("Big");
- fontSizeHuge = new JMenuItem("Huge");
- fontSize.add(fontSizeSmall);
- fontSize.add(fontSizeNormal);
- fontSize.add(fontSizeBig);
- fontSize.add(fontSizeHuge);
- helpAbout = new JMenuItem("About...");
- helpHelp = new JMenuItem("Help...");
- help.add(helpAbout);
- help.add(helpHelp);
- menu.add(file);
- menu.add(function);
- menu.add(edit);
- menu.add(font);
- menu.add(fontDecor);
- menu.add(fontSize);
- menu.add(help);
- field.setLineWrap(true);
- field.setWrapStyleWord(true);
- scroller = new JScrollPane(field);
- add(scroller, BorderLayout.CENTER);
- add(status, BorderLayout.SOUTH);
- JToolBar tool = new JToolBar();
- add(tool,BorderLayout.NORTH);
- JButton cut = tool.add(Cut), copy = tool.add(Copy),paste = tool.add(Paste);
- cut.setText(null); cut.setIcon(new ImageIcon(getClass().getResource("/images/cut.png")));
- cut.setToolTipText("Cut");cut.setSize(100, 100);
- copy.setText(null); copy.setIcon(new ImageIcon(getClass().getResource("/images/copy.png")));
- copy.setToolTipText("Copy");copy.setSize(100, 100);
- paste.setText(null); paste.setIcon(new ImageIcon(getClass().getResource("/images/paste.png")));
- paste.setToolTipText("Paste");paste.setSize(100, 100);
- tool.addSeparator();
- fileNew = new JMenuItem("New");
- fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
- fileOpen = new JMenuItem("Open...");
- fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
- fileSave = new JMenuItem("Save");
- fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
- animation = new JMenuItem("Animation");
- tool.add(fileNew);tool.add(fileOpen);tool.add(fileSave);tool.add(animation);
- createLineNumTop();
- splitWindow();
- fileNew.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if (JOptionPane.showConfirmDialog(null, "Are you sure you want to create a new file?") == 0) {
- field.setText("");
- status.setText("Generated New File...");
- }
- }
- });
- fileOpen.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if (JOptionPane.showConfirmDialog(null,
- "Are you sure you want to delete the current file?") == 0) {
- int openResult = fileSelect.showOpenDialog(null);
- if (openResult == fileSelect.APPROVE_OPTION) {
- openFile(fileSelect.getSelectedFile());
- status.setText("Opened File...");
- }
- }
- }
- });
- fileSave.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if(currentFile == null){
- int saveResult = fileSelect.showSaveDialog(null);
- if(saveResult == fileSelect.APPROVE_OPTION){
- saveFile(fileSelect.getSelectedFile(), field.getText());
- }
- } else {
- saveFile(currentFile, field.getText());
- }
- status.setText("File Saved...");
- }
- });
- fileSaveAs.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- int saveResult = fileSelect.showSaveDialog(null);
- saveFile(fileSelect.getSelectedFile(), field.getText());
- status.setText("File Saved...");
- }
- });
- fileExit.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if(JOptionPane.showConfirmDialog(null, "Are you sure you wish to exit without saving?") == 0){
- closeWindow();
- }
- }
- });
- editToggleEditable.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if(field.isEditable()){
- field.setEditable(false);
- status.setText("Field is No Longer Editable...");
- } else if(!field.isEditable()){
- field.setEditable(true);
- status.setText("Field is Now Editable...");
- }
- }
- });
- animation.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- JFrame animation = new JFrame("Animation");
- animation.pack();
- animation.setVisible(true);
- animation.setSize(800, 600);
- animation.setBackground(Color.WHITE);
- animation.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- }
- }
- );
- fontArial.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontF = "Arial";
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Changed to: Arial");
- }
- });
- fontSerif.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontF = "Serif";
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Changed to: Serif");
- }
- });
- fontComicSansMS.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontF = "ComicSansMS";
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Changed to: ComicSansMS");
- }
- });
- fontConsolas.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontF = "Consolas";
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Changed to: Consolas");
- }
- });
- fontDecorPlain.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontD = Font.PLAIN;
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Decoration Set to Plain...");
- }
- });
- fontDecorBold.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontD = Font.BOLD;
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Decoration Set to Bold...");
- }
- });
- fontDecorItalic.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontD = Font.ITALIC;
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Decoration Set to Italic...");
- }
- });
- fontDecorBoldItalic.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontD = Font.BOLD + Font.ITALIC;
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Decoration Set to Bold & Italic...");
- }
- });
- fontSizeSmall.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontS = 14;
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Size Set to SMALL...");
- }
- });
- fontSizeNormal.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontS = 18;
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Size Set to NORMAL...");
- }
- });
- fontSizeBig.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontS = 24;
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Size Set to BIG...");
- }
- });
- fontSizeHuge.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- fontS = 36;
- field.setFont(new Font(fontF, fontD, fontS));
- status.setText("Font Size Set to HUGE...");
- }
- });
- helpAbout.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- JOptionPane.showMessageDialog(null, "Tanatip Iamjunchai 5510405627");
- }
- });
- helpHelp.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- JOptionPane.showMessageDialog(null, "Help is coming soon. . .","HELP", JOptionPane.WARNING_MESSAGE);
- // helpF = new HelpFrame();
- //
- // helpF.setSize(300, 100);
- // helpF.setVisible(true);
- // helpF.setResizable(false);
- // helpF.setLocationRelativeTo(null);
- }
- });
- }
- public void createLineNumTop(){
- //field = new JTextArea();
- add(field);
- scroll = new JScrollPane(field);
- add(scroll);
- lineSet = new JTextArea("1");
- lineSet.setBackground(Color.lightGray);
- lineSet.setColumns(3);
- lineSet.setForeground(Color.BLACK);
- lineSet.setEditable(false);
- field.getDocument().addDocumentListener(new DocumentListener(){
- public String getText(){
- int caretPosition = field.getDocument().getLength();
- Element root = field.getDocument().getDefaultRootElement();
- String text = "1" + System.getProperty("line.separator");
- for(int i = 2; i < root.getElementIndex( caretPosition ) + 2; i++){
- text += i + System.getProperty("line.separator");
- }
- return text;
- }
- @Override
- public void changedUpdate(DocumentEvent de) {
- lineSet.setText(getText());
- }
- @Override
- public void insertUpdate(DocumentEvent de) {
- lineSet.setText(getText());
- }
- @Override
- public void removeUpdate(DocumentEvent de) {
- lineSet.setText(getText());
- }
- });
- scroll.setRowHeaderView(lineSet);
- scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- }
- public JTabbedPane createTabbedPane(){
- JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT);
- JTextArea error = new JTextArea();
- error.setBackground(new Color(255,102,102));
- error.setEditable(false);
- JScrollPane errorScroll = new JScrollPane(error);
- tabbedPane.addTab("Error",errorScroll);
- JTextArea symbol = new JTextArea();
- symbol.setBackground(new Color(255,204,0));
- symbol.setEditable(false);
- JScrollPane symbolScroll = new JScrollPane(symbol);
- tabbedPane.addTab("Symbol",symbolScroll);
- JTextArea compile = new JTextArea();
- compile.setBackground(new Color(153,255,102));
- compile.setEditable(false);
- JScrollPane compileScroll = new JScrollPane(compile);
- tabbedPane.addTab("Compile",compileScroll);
- JTextArea execute = new JTextArea();
- execute.setBackground(new Color(102,102,255));
- execute.setEditable(false);
- JScrollPane executeScroll = new JScrollPane(execute);
- tabbedPane.addTab("Execute",executeScroll);
- return tabbedPane;
- }
- public void splitWindow(){
- JSplitPane spliter = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scroll,createTabbedPane());
- spliter.setDividerLocation(500);
- spliter.setBackground(Color.WHITE);
- spliter.setOneTouchExpandable(true);
- add(spliter);
- }
- public void openFile(File file) {
- if (file.canRead()) {
- String filePath = file.getPath();
- String fileContents = "";
- if (filePath.endsWith(".txt")) {
- try {
- Scanner scan = new Scanner(new FileInputStream(file));
- while (scan.hasNextLine()) {
- fileContents += scan.nextLine();
- }
- scan.close();
- } catch (FileNotFoundException e) {
- }
- field.setText(fileContents);
- setTitle("Text Editor" + filePath);
- currentFile = file;
- } else {
- JOptionPane.showMessageDialog(null,"That file type is not supported!\nOnly .txt files are supported.");
- }
- } else JOptionPane.showMessageDialog(null, "Could not open file...");
- }
- public void saveFile(File file, String contents){
- BufferedWriter writer = null;
- String filePath = file.getPath();
- if(!filePath.endsWith(".txt")){
- filePath += ".txt";
- }
- try{
- writer = new BufferedWriter(new FileWriter(filePath));
- writer.write(contents);
- writer.close();
- field.setText(contents);
- setTitle("Text Editor" + filePath);
- currentFile = file;
- } catch(Exception e) {
- }
- }
- public void closeWindow(){
- WindowEvent close = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
- Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(close);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement