From 64b9c6199b76d78c96045bcebbbdee63862e8ec5 Mon Sep 17 00:00:00 2001 From: azert Date: Thu, 31 Oct 2024 00:42:38 +0100 Subject: [PATCH 01/28] Rewriting the code from 0 --- .idea/.gitignore | 3 + .idea/misc.xml | 6 + .idea/modules.xml | 8 ++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + TextEditor-Java.iml | 11 ++ out/production/TextEditor-Java/.gitignore | 24 ++++ .../TextEditor-Java/.idea/.gitignore | 3 + out/production/TextEditor-Java/.idea/misc.xml | 6 + .../TextEditor-Java/.idea/modules.xml | 8 ++ .../TextEditor-Java/.idea/uiDesigner.xml | 124 ++++++++++++++++++ out/production/TextEditor-Java/.idea/vcs.xml | 6 + out/production/TextEditor-Java/ReadMe.md | 17 +++ .../TextEditor-Java/TextEditor-Java.iml | 11 ++ src/CustomMenuBar.java | 34 +++++ src/CustomWindowListener.java | 34 +++++ src/ExmlEditor.java | 20 +++ 17 files changed, 445 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 TextEditor-Java.iml create mode 100644 out/production/TextEditor-Java/.gitignore create mode 100644 out/production/TextEditor-Java/.idea/.gitignore create mode 100644 out/production/TextEditor-Java/.idea/misc.xml create mode 100644 out/production/TextEditor-Java/.idea/modules.xml create mode 100644 out/production/TextEditor-Java/.idea/uiDesigner.xml create mode 100644 out/production/TextEditor-Java/.idea/vcs.xml create mode 100644 out/production/TextEditor-Java/ReadMe.md create mode 100644 out/production/TextEditor-Java/TextEditor-Java.iml create mode 100644 src/CustomMenuBar.java create mode 100644 src/CustomWindowListener.java create mode 100644 src/ExmlEditor.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e9f4691 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/TextEditor-Java.iml b/TextEditor-Java.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/TextEditor-Java.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/TextEditor-Java/.gitignore b/out/production/TextEditor-Java/.gitignore new file mode 100644 index 0000000..524f096 --- /dev/null +++ b/out/production/TextEditor-Java/.gitignore @@ -0,0 +1,24 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* diff --git a/out/production/TextEditor-Java/.idea/.gitignore b/out/production/TextEditor-Java/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/out/production/TextEditor-Java/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/TextEditor-Java/.idea/misc.xml b/out/production/TextEditor-Java/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/out/production/TextEditor-Java/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/TextEditor-Java/.idea/modules.xml b/out/production/TextEditor-Java/.idea/modules.xml new file mode 100644 index 0000000..e9f4691 --- /dev/null +++ b/out/production/TextEditor-Java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/TextEditor-Java/.idea/uiDesigner.xml b/out/production/TextEditor-Java/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/out/production/TextEditor-Java/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/TextEditor-Java/.idea/vcs.xml b/out/production/TextEditor-Java/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/out/production/TextEditor-Java/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/TextEditor-Java/ReadMe.md b/out/production/TextEditor-Java/ReadMe.md new file mode 100644 index 0000000..bc1727c --- /dev/null +++ b/out/production/TextEditor-Java/ReadMe.md @@ -0,0 +1,17 @@ +# TextEditor (java) Modifications + +This is a simple text editor. Using it as a starting point, it needs a bunch of functionality additions. + +## Additions + +Things to add: + +- find/replace text implementation +- when you exit, you must NOT lose un-saved changes +- add a new window, scrollable, with some editor help documentation in it. +- implement Word Wrap (yeah, look it up) +- add a font panel, so you can change the font you edit in. + - make sure not only new files are in th enew font, but existing windows too. +- add a way to print the current file. +- add a way to set the page in portrait or landscape mode +- Make your About Dialog snazzy! diff --git a/out/production/TextEditor-Java/TextEditor-Java.iml b/out/production/TextEditor-Java/TextEditor-Java.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/out/production/TextEditor-Java/TextEditor-Java.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java new file mode 100644 index 0000000..fb5ba30 --- /dev/null +++ b/src/CustomMenuBar.java @@ -0,0 +1,34 @@ +package src; + +import java.awt.*; + +public class CustomMenuBar extends MenuBar { + public CustomMenuBar() { + Menu filemenu = new Menu("File"); + MenuComponent[] filemenuitems = {new MenuItem("New"), new MenuItem("New window"), new MenuSpace(),new MenuItem("Open"), new MenuItem("Save"), new MenuItem("Save as"), new MenuItem("Print")}; + Menu editmenu = new Menu("Edit"); + Menu selectionmenu = new Menu("Selection"); + Menu appearancemenu = new Menu("Appearance"); + Menu securitymenu = new Menu("Security"); + Menu toolsmenu = new Menu("Tools"); + Menu helpmenu = new Menu("Help"); + for (MenuComponent menuComponent : filemenuitems) { + if (menuComponent instanceof MenuSpace) { + filemenu.addSeparator(); + } else { //Avoiding a ClassCastException + filemenu.add((MenuItem) menuComponent); + } + } + this.add(filemenu); + this.add(editmenu); + this.add(selectionmenu); + this.add(appearancemenu); + this.add(securitymenu); + this.add(toolsmenu); + this.add(helpmenu); + } +} + +class MenuSpace extends MenuComponent { + public MenuSpace() {} +} \ No newline at end of file diff --git a/src/CustomWindowListener.java b/src/CustomWindowListener.java new file mode 100644 index 0000000..8c63816 --- /dev/null +++ b/src/CustomWindowListener.java @@ -0,0 +1,34 @@ +package src; + +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; + +public class CustomWindowListener implements WindowListener { + public void windowOpened(WindowEvent e) { + + } + + public void windowClosing(WindowEvent e) { + System.exit(0); + } + + public void windowClosed(WindowEvent e) { + + } + + public void windowIconified(WindowEvent e) { + + } + + public void windowDeiconified(WindowEvent e) { + + } + + public void windowActivated(WindowEvent e) { + + } + + public void windowDeactivated(WindowEvent e) { + + } +} diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java new file mode 100644 index 0000000..9cb09ac --- /dev/null +++ b/src/ExmlEditor.java @@ -0,0 +1,20 @@ +package src; + +import java.awt.*; + +public class ExmlEditor extends Frame { + public ExmlEditor() { + this.setTitle("Notepad - Untitled"); + this.setSize(800, 600); + this.setBackground(new Color(234, 234, 234)); + this.addWindowListener(new CustomWindowListener()); + TextArea textArea = new TextArea(); + this.add(textArea); + this.setMenuBar(new CustomMenuBar()); + this.setVisible(true); + } + + public static void main(String[] args) { + new ExmlEditor(); + } +} \ No newline at end of file From b4ed8dfd0c352dc1e975bbaf58d1824322c2018d Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:40:43 +0100 Subject: [PATCH 02/28] Update changelog.md --- changelog.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog.md diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..74d6c41 --- /dev/null +++ b/changelog.md @@ -0,0 +1,4 @@ +Rewriting code from start + +Adding unachieved features : +- \ No newline at end of file From a2ae0012b6f4e727057bf11f3fe381c794a7a7a9 Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:46:17 +0100 Subject: [PATCH 03/28] Update .gitignore --- .gitignore | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.gitignore b/.gitignore index 524f096..fa82fd8 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,13 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* replay_pid* + +#Intellij Idea +out/ + +#Visual Studio Code +.vscode/ + +#Eclipse +.classpath +.project \ No newline at end of file From e5b231b6796f83c873cebbce5e766defe237fd82 Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:46:40 +0100 Subject: [PATCH 04/28] Update .gitignore --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index fa82fd8..edbda87 100644 --- a/.gitignore +++ b/.gitignore @@ -30,5 +30,5 @@ out/ .vscode/ #Eclipse -.classpath -.project \ No newline at end of file +*.classpath +*.project \ No newline at end of file From 323c7786f6e18cd40f3d8a0cc0f25eb991a63e42 Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:51:30 +0100 Subject: [PATCH 05/28] Update CustomMenuBar.java --- src/CustomMenuBar.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index fb5ba30..b17946b 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -5,8 +5,9 @@ public class CustomMenuBar extends MenuBar { public CustomMenuBar() { Menu filemenu = new Menu("File"); - MenuComponent[] filemenuitems = {new MenuItem("New"), new MenuItem("New window"), new MenuSpace(),new MenuItem("Open"), new MenuItem("Save"), new MenuItem("Save as"), new MenuItem("Print")}; + MenuComponent[] filemenuitems = {new MenuItem("New"), new MenuItem("New window"), new MenuSpace(),new MenuItem("Open"), new MenuItem("Save"), new MenuItem("Save as"), new MenuItem("Print"), new MenuItem("Close"), new MenuItem("Close Window"), new MenuSpace(), new MenuItem("Exit")}; Menu editmenu = new Menu("Edit"); + MenuComponent[] editmenuitems = {new MenuItem("Undo"), new MenuItem("Redo"), new MenuSpace() Menu selectionmenu = new Menu("Selection"); Menu appearancemenu = new Menu("Appearance"); Menu securitymenu = new Menu("Security"); From 290d2b1215a27d3efb304e2107c0fdfd8dfc1af1 Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:56:24 +0100 Subject: [PATCH 06/28] Update CustomMenuBar.java --- src/CustomMenuBar.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index b17946b..7158863 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -7,7 +7,7 @@ public CustomMenuBar() { Menu filemenu = new Menu("File"); MenuComponent[] filemenuitems = {new MenuItem("New"), new MenuItem("New window"), new MenuSpace(),new MenuItem("Open"), new MenuItem("Save"), new MenuItem("Save as"), new MenuItem("Print"), new MenuItem("Close"), new MenuItem("Close Window"), new MenuSpace(), new MenuItem("Exit")}; Menu editmenu = new Menu("Edit"); - MenuComponent[] editmenuitems = {new MenuItem("Undo"), new MenuItem("Redo"), new MenuSpace() + MenuComponent[] editmenuitems = {new MenuItem("Undo"), new MenuItem("Redo"), new MenuSpace(), new MenuItem("Copy"), new MenuItem("Cut"), new MenuItem("Paste"), new MenuSpace(), new MenuItem("Delete")}; Menu selectionmenu = new Menu("Selection"); Menu appearancemenu = new Menu("Appearance"); Menu securitymenu = new Menu("Security"); From 88db5a158f49216cc1185f899f3d2738c37dfdf3 Mon Sep 17 00:00:00 2001 From: E-xml Date: Sun, 3 Nov 2024 12:37:21 +0100 Subject: [PATCH 07/28] Last commit before school --- out/production/TextEditor-Java/.gitignore | 10 +++ src/CustomMenuBar.java | 33 ++++++- src/CustomOptionPane.java | 100 ++++++++++++++++++++++ src/CustomWindowListener.java | 13 ++- src/ExmlEditor.java | 22 ++++- 5 files changed, 171 insertions(+), 7 deletions(-) create mode 100644 src/CustomOptionPane.java diff --git a/out/production/TextEditor-Java/.gitignore b/out/production/TextEditor-Java/.gitignore index 524f096..edbda87 100644 --- a/out/production/TextEditor-Java/.gitignore +++ b/out/production/TextEditor-Java/.gitignore @@ -22,3 +22,13 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* replay_pid* + +#Intellij Idea +out/ + +#Visual Studio Code +.vscode/ + +#Eclipse +*.classpath +*.project \ No newline at end of file diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index 7158863..0ac9205 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -1,13 +1,14 @@ package src; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; public class CustomMenuBar extends MenuBar { - public CustomMenuBar() { + public CustomMenuBar(ExmlEditor parent) { Menu filemenu = new Menu("File"); - MenuComponent[] filemenuitems = {new MenuItem("New"), new MenuItem("New window"), new MenuSpace(),new MenuItem("Open"), new MenuItem("Save"), new MenuItem("Save as"), new MenuItem("Print"), new MenuItem("Close"), new MenuItem("Close Window"), new MenuSpace(), new MenuItem("Exit")}; + MenuComponent[] filemenuitems = {new MenuItem("New"), new MenuItem("New window"), new MenuSpace(),new MenuItem("Open"), new MenuItem("Save"), new MenuItem("Save as"), new MenuItem("Print"), new MenuItem("Close"), new MenuItem("Close window"), new MenuSpace(), new MenuItem("Exit")}; Menu editmenu = new Menu("Edit"); - MenuComponent[] editmenuitems = {new MenuItem("Undo"), new MenuItem("Redo"), new MenuSpace(), new MenuItem("Copy"), new MenuItem("Cut"), new MenuItem("Paste"), new MenuSpace(), new MenuItem("Delete")}; Menu selectionmenu = new Menu("Selection"); Menu appearancemenu = new Menu("Appearance"); Menu securitymenu = new Menu("Security"); @@ -18,6 +19,7 @@ public CustomMenuBar() { filemenu.addSeparator(); } else { //Avoiding a ClassCastException filemenu.add((MenuItem) menuComponent); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent).getLabel(), parent)); } } this.add(filemenu); @@ -32,4 +34,29 @@ public CustomMenuBar() { class MenuSpace extends MenuComponent { public MenuSpace() {} +} + +class CustomActionListener implements ActionListener { + ExmlEditor parent = null; + String name = ""; + + public CustomActionListener(String name, ExmlEditor parent) { + this.name = name; + this.parent = parent; + } + + public void actionPerformed(ActionEvent e) { + switch (name) { + case "New": + if (!parent.isFileSaved()) { + + } + + + case "New window": new ExmlEditor(); break; + case "Close window": parent.dispose(); break; + case "Exit": System.exit(0); + default: throw new IllegalArgumentException("You might have forget to update this block"); + } + } } \ No newline at end of file diff --git a/src/CustomOptionPane.java b/src/CustomOptionPane.java new file mode 100644 index 0000000..1ed3dac --- /dev/null +++ b/src/CustomOptionPane.java @@ -0,0 +1,100 @@ +package src; + +import java.awt.*; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; + +public class CustomOptionPane extends Frame { + + public static final int YESNOOption = 0; + public static final int YESNOCANCELOption = 1; + public static final int OKOption = 2; + public static final int OKNOOption = 3; + public static final int ANYWAYCANCELOption = 4; + public static final int CUSTOMOption = 5; + + private String title = "Choosing option"; + private String label = "Tlkqisnbutgorezuctgocrezoguoczreouygecdrtb nyhoe"; + + public CustomOptionPane() { + this.setSize(300, 200); + this.setBackground(new Color(234, 234, 234)); + this.setResizable(false); + this.setLocationRelativeTo(null); + this.setAlwaysOnTop(true); + this.setTitle(title); + this.setLayout(null); + + } + + private String SplitText(String text) { + String FinalText = ""; + for (int i = 0; i != text.length(); i++) { + if ((i+1)%48 == 0) { + FinalText += text.substring(0, i+1); + text = text.substring(i+1); + } + } + + return FinalText; + } + + public void showChoiceDialog(int state) { + final boolean[] isClosed = {false}; + this.addWindowListener(new WindowListener() { + @Override + public void windowOpened(WindowEvent e) { + + } + + @Override + public void windowClosing(WindowEvent e) { + isClosed[0] = true; + dispose(); + } + + @Override + public void windowClosed(WindowEvent e) { + + } + + @Override + public void windowIconified(WindowEvent e) { + + } + + @Override + public void windowDeiconified(WindowEvent e) { + + } + + @Override + public void windowActivated(WindowEvent e) { + + } + + @Override + public void windowDeactivated(WindowEvent e) { + + } + }); + + switch (state) { + case 0: + Label lbl = new Label(label); + lbl.setBounds(10, 35, 300,25); + this.add(lbl); + + } + + this.setVisible(true); + + if (isClosed[0]) { + return; + } + } + + public static void main(String[] args) { + new CustomOptionPane().showChoiceDialog(YESNOOption); + } +} diff --git a/src/CustomWindowListener.java b/src/CustomWindowListener.java index 8c63816..7cdc61c 100644 --- a/src/CustomWindowListener.java +++ b/src/CustomWindowListener.java @@ -1,15 +1,26 @@ package src; +import java.awt.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class CustomWindowListener implements WindowListener { + private ExmlEditor window = null; + + public CustomWindowListener(ExmlEditor window) { + this.window = window; + } + public void windowOpened(WindowEvent e) { } public void windowClosing(WindowEvent e) { - System.exit(0); + if (!window.isFileSaved()) { + System.out.println("Not saved"); + } + + window.dispose(); } public void windowClosed(WindowEvent e) { diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index 9cb09ac..5f4e3ed 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -1,19 +1,35 @@ package src; import java.awt.*; +import java.io.File; public class ExmlEditor extends Frame { + static File OpenedFile = null; + static TextArea textArea = new TextArea(); + static String TextContent = ""; + public ExmlEditor() { this.setTitle("Notepad - Untitled"); this.setSize(800, 600); this.setBackground(new Color(234, 234, 234)); - this.addWindowListener(new CustomWindowListener()); - TextArea textArea = new TextArea(); + this.addWindowListener(new CustomWindowListener(this)); this.add(textArea); - this.setMenuBar(new CustomMenuBar()); + this.setMenuBar(new CustomMenuBar(this)); this.setVisible(true); } + public void setTextContent(String text) { + TextContent = text; + } + + public boolean isFileSaved() { + if (OpenedFile == null) { + return textArea.getText().isEmpty(); + } else { + return textArea.getText().equals(TextContent); + } + } + public static void main(String[] args) { new ExmlEditor(); } From 723cfb4949329918a6c59516ddbc9caed004ac20 Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Mon, 4 Nov 2024 06:34:17 +0100 Subject: [PATCH 08/28] Update CustomOptionPane.java --- src/CustomOptionPane.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CustomOptionPane.java b/src/CustomOptionPane.java index 1ed3dac..6a21705 100644 --- a/src/CustomOptionPane.java +++ b/src/CustomOptionPane.java @@ -81,7 +81,7 @@ public void windowDeactivated(WindowEvent e) { switch (state) { case 0: - Label lbl = new Label(label); + Label lbl = new Label(SplitText(label)); lbl.setBounds(10, 35, 300,25); this.add(lbl); From ff610402e2191b84c9b7bc169b27b33b1bee70d9 Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Mon, 4 Nov 2024 08:51:51 +0100 Subject: [PATCH 09/28] Update .gitignore --- .gitignore | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index edbda87..ce8e466 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,12 @@ -# Compiled class file +# Compiled class files *.class -# Log file +# Log files *.log +# IML files +*.iml + # BlueJ files *.ctxt @@ -23,12 +26,12 @@ hs_err_pid* replay_pid* -#Intellij Idea +# Intellij Idea out/ -#Visual Studio Code +# Visual Studio Code .vscode/ -#Eclipse +# Eclipse *.classpath *.project \ No newline at end of file From 3ea15076ce5627a99c7a56a275b153cea091c133 Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Mon, 4 Nov 2024 10:39:51 +0100 Subject: [PATCH 10/28] Update CustomOptionPane --- src/CustomOptionPane.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/CustomOptionPane.java b/src/CustomOptionPane.java index 6a21705..14f414f 100644 --- a/src/CustomOptionPane.java +++ b/src/CustomOptionPane.java @@ -82,8 +82,12 @@ public void windowDeactivated(WindowEvent e) { switch (state) { case 0: Label lbl = new Label(SplitText(label)); - lbl.setBounds(10, 35, 300,25); + lbl.setBounds(10, 35, 300, 25); this.add(lbl); + Button Yes = new Button("Yes"); + Button No = new Button("No"); + Yes.setBounds(20, 105, 100, 25); + No.setBounds(180, 105, 100, 25); } From ec6a0ccb751f90d92e29d5af43c197ad79d74729 Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Mon, 4 Nov 2024 11:31:35 +0100 Subject: [PATCH 11/28] Update CustomOptionPane.java --- src/CustomOptionPane.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/CustomOptionPane.java b/src/CustomOptionPane.java index 14f414f..1067160 100644 --- a/src/CustomOptionPane.java +++ b/src/CustomOptionPane.java @@ -6,6 +6,11 @@ public class CustomOptionPane extends Frame { + Button Yes = new Button("Yes"); + Button No = new Button("No"); + Yes.setBounds(20, 105, 100, 25); + No.setBounds(180, 105, 100, 25); + public static final int YESNOOption = 0; public static final int YESNOCANCELOption = 1; public static final int OKOption = 2; @@ -84,11 +89,7 @@ public void windowDeactivated(WindowEvent e) { Label lbl = new Label(SplitText(label)); lbl.setBounds(10, 35, 300, 25); this.add(lbl); - Button Yes = new Button("Yes"); - Button No = new Button("No"); - Yes.setBounds(20, 105, 100, 25); - No.setBounds(180, 105, 100, 25); - + this.add(Yes); } this.setVisible(true); From 457c26cf78a79e9b8183050cdab1691e9e5a80fc Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Mon, 4 Nov 2024 11:59:42 +0100 Subject: [PATCH 12/28] Update CustomOptionPane.java --- src/CustomOptionPane.java | 87 ++++++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 15 deletions(-) diff --git a/src/CustomOptionPane.java b/src/CustomOptionPane.java index 1067160..0e3f59e 100644 --- a/src/CustomOptionPane.java +++ b/src/CustomOptionPane.java @@ -1,25 +1,24 @@ package src; import java.awt.*; -import java.awt.event.WindowEvent; -import java.awt.event.WindowListener; +import java.awt.event.*; public class CustomOptionPane extends Frame { - Button Yes = new Button("Yes"); - Button No = new Button("No"); - Yes.setBounds(20, 105, 100, 25); - No.setBounds(180, 105, 100, 25); - public static final int YESNOOption = 0; public static final int YESNOCANCELOption = 1; public static final int OKOption = 2; - public static final int OKNOOption = 3; + public static final int OKCancelOption = 3; public static final int ANYWAYCANCELOption = 4; - public static final int CUSTOMOption = 5; - private String title = "Choosing option"; - private String label = "Tlkqisnbutgorezuctgocrezoguoczreouygecdrtb nyhoe"; + public static final int YESOption = 0; + public static final int NOOption = 1; + public static final int CancelOption = 2; + public static final int OKOption = 3; + public static final int ANYWAYOption = 4; + + private String title = "CustomOptionPane.title"; + private String label = "CustomOptionPane.label"; public CustomOptionPane() { this.setSize(300, 200); @@ -44,7 +43,7 @@ private String SplitText(String text) { return FinalText; } - public void showChoiceDialog(int state) { + public int showChoiceDialog(int state) { final boolean[] isClosed = {false}; this.addWindowListener(new WindowListener() { @Override @@ -85,11 +84,69 @@ public void windowDeactivated(WindowEvent e) { }); switch (state) { + Label lbl = new Label(SplitText(label)); + lbl.setBounds(10, 35, 300, 25); + this.add(lbl); + + Button Yes = new Button("Yes"); + Yes.setBounds(20, 105, 100, 25); + Yes.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return YESOption; + } + } + Button No = new Button("No"); + No.setBounds(180, 105, 100, 25); + No.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return NOOption; + } + } + case 0: - Label lbl = new Label(SplitText(label)); - lbl.setBounds(10, 35, 300, 25); - this.add(lbl); this.add(Yes); + this.add(No); + break; + + case 1: + Button Yes = new Button("Yes"); + Yes.setBounds(10, 105, 75, 25); + Button No = new Button("No"); + No.setBounds(102, 105, 75, 25); + Button Cancel = new Button("Cancel"); + Cancel.setBounds(215, 105, 75, 25); + this.add(Yes); + this.add(No); + this.add(Cancel); + break; + + case 2: + Button OK = new Button("OK"); + OK.setBounds(50, 105, 100, 25); + this.add(OK); + break; + + case 3: + Button OK = new Button("OK"); + Yes.setBounds(20, 105, 100, 25); + Button Cancel = new Button("Cancel"); + No.setBounds(180, 105, 100, 25); + this.add(OK); + this.add(Cancel); + break; + + case 4: + Button Anyway = new Button("Do anyway"); + Yes.setBounds(20, 105, 100, 25); + Button Cancel = new Button("Cancel"); + No.setBounds(180, 105, 100, 25); + this.add(Anyway); + this.add(Cancel); + break; + + default: throw new IllegalArgumentException("State " + state + " doesn't exit"); } this.setVisible(true); From 90d88f4de4d8a5da763f895c872ac2ea3c82914e Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:05:25 +0100 Subject: [PATCH 13/28] Update --- src/CustomMenuBar.java | 6 +- src/CustomOptionPane.java | 131 ++++++++++++++++++++++++++------------ 2 files changed, 94 insertions(+), 43 deletions(-) diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index 0ac9205..ea8f41c 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -49,7 +49,11 @@ public void actionPerformed(ActionEvent e) { switch (name) { case "New": if (!parent.isFileSaved()) { - + CustomOptionPane optionPane = new CustomOptionPane( + if (optionPane.showChoiceDialog(1) == CustomOptionPane.YESOption) { + + } + ) } diff --git a/src/CustomOptionPane.java b/src/CustomOptionPane.java index 0e3f59e..3b96d20 100644 --- a/src/CustomOptionPane.java +++ b/src/CustomOptionPane.java @@ -20,7 +20,9 @@ public class CustomOptionPane extends Frame { private String title = "CustomOptionPane.title"; private String label = "CustomOptionPane.label"; - public CustomOptionPane() { + public CustomOptionPane(String title, String label) { + this.title = title; + this.label = label; this.setSize(300, 200); this.setBackground(new Color(234, 234, 234)); this.setResizable(false); @@ -47,9 +49,7 @@ public int showChoiceDialog(int state) { final boolean[] isClosed = {false}; this.addWindowListener(new WindowListener() { @Override - public void windowOpened(WindowEvent e) { - - } + public void windowOpened(WindowEvent e) {} @Override public void windowClosing(WindowEvent e) { @@ -58,29 +58,19 @@ public void windowClosing(WindowEvent e) { } @Override - public void windowClosed(WindowEvent e) { - - } + public void windowClosed(WindowEvent e) {} @Override - public void windowIconified(WindowEvent e) { - - } + public void windowIconified(WindowEvent e) {} @Override - public void windowDeiconified(WindowEvent e) { - - } + public void windowDeiconified(WindowEvent e) {} @Override - public void windowActivated(WindowEvent e) { - - } + public void windowActivated(WindowEvent e) {} @Override - public void windowDeactivated(WindowEvent e) { - - } + public void windowDeactivated(WindowEvent e) {} }); switch (state) { @@ -96,6 +86,7 @@ public void actionPerformed(ActionEvent e) { return YESOption; } } + Button No = new Button("No"); No.setBounds(180, 105, 100, 25); No.addActionListener(new ActionListener()) { @@ -104,55 +95,111 @@ public void actionPerformed(ActionEvent e) { return NOOption; } } - + + Button Yes1 = new Button("Yes"); + Yes1.setBounds(10, 105, 75, 25); + Yes1.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return YESOption; + } + } + + Button No1 = new Button("No"); + No1.setBounds(102, 105, 75, 25); + No1.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return NOOption; + } + } + + Button Cancel = new Button("Cancel"); + Cancel.setBounds(215, 105, 75, 25); + Cancel.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return CancelOption; + } + } + + Button OK = new Button("OK"); + OK.setBounds(50, 105, 100, 25); + OK.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return OKOption; + } + } + + Button OK1 = new Button("OK"); + OK1.setBounds(20, 105, 100, 25); + OK1.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return OKOption; + } + } + + Button Cancel1 = new Button("Cancel"); + Cancel1.setBounds(180, 105, 100, 25); + Cancel1.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return CancelOption; + } + } + + Button Anyway = new Button("Do anyway"); + Anyway.setBounds(20, 105, 100, 25); + Anyway.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return ANYWAYOption; + } + } + + Button Cancel2 = new Button("Cancel"); + Cancel2.setBounds(180, 105, 100, 25); + Cancel2.addActionListener(new ActionListener()) { + @Override + public void actionPerformed(ActionEvent e) { + return CancelOption; + } + } + case 0: this.add(Yes); this.add(No); break; case 1: - Button Yes = new Button("Yes"); - Yes.setBounds(10, 105, 75, 25); - Button No = new Button("No"); - No.setBounds(102, 105, 75, 25); - Button Cancel = new Button("Cancel"); - Cancel.setBounds(215, 105, 75, 25); - this.add(Yes); - this.add(No); + this.add(Yes1); + this.add(No1); this.add(Cancel); break; case 2: - Button OK = new Button("OK"); - OK.setBounds(50, 105, 100, 25); this.add(OK); break; case 3: - Button OK = new Button("OK"); - Yes.setBounds(20, 105, 100, 25); - Button Cancel = new Button("Cancel"); - No.setBounds(180, 105, 100, 25); - this.add(OK); - this.add(Cancel); + this.add(OK1); + this.add(Cancel1); break; case 4: - Button Anyway = new Button("Do anyway"); - Yes.setBounds(20, 105, 100, 25); - Button Cancel = new Button("Cancel"); - No.setBounds(180, 105, 100, 25); this.add(Anyway); this.add(Cancel); break; - default: throw new IllegalArgumentException("State " + state + " doesn't exit"); + default: throw new IllegalArgumentException("State " + state + " doesn't exit. Must be between 0 and 4"); } this.setVisible(true); if (isClosed[0]) { - return; + return CancelOption; } } From 17dfb7cbacde7dc75a5f34377677b663e5e9020f Mon Sep 17 00:00:00 2001 From: azert Date: Wed, 6 Nov 2024 16:32:28 +0100 Subject: [PATCH 14/28] Updating - CustomMenuBar.java - CustomOptionPane.java --- out/production/TextEditor-Java/.gitignore | 13 +- src/CustomMenuBar.java | 15 +- src/CustomOptionPane.java | 212 ++++++++++++---------- 3 files changed, 135 insertions(+), 105 deletions(-) diff --git a/out/production/TextEditor-Java/.gitignore b/out/production/TextEditor-Java/.gitignore index edbda87..ce8e466 100644 --- a/out/production/TextEditor-Java/.gitignore +++ b/out/production/TextEditor-Java/.gitignore @@ -1,9 +1,12 @@ -# Compiled class file +# Compiled class files *.class -# Log file +# Log files *.log +# IML files +*.iml + # BlueJ files *.ctxt @@ -23,12 +26,12 @@ hs_err_pid* replay_pid* -#Intellij Idea +# Intellij Idea out/ -#Visual Studio Code +# Visual Studio Code .vscode/ -#Eclipse +# Eclipse *.classpath *.project \ No newline at end of file diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index ea8f41c..9559613 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -46,14 +46,19 @@ public CustomActionListener(String name, ExmlEditor parent) { } public void actionPerformed(ActionEvent e) { + CustomOptionPane optionPane = new CustomOptionPane(); switch (name) { case "New": if (!parent.isFileSaved()) { - CustomOptionPane optionPane = new CustomOptionPane( - if (optionPane.showChoiceDialog(1) == CustomOptionPane.YESOption) { - - } - ) + optionPane.setTitle("Modications aren't saved"); + optionPane.setLabel("Your modifications aren't saved. Would you save them ?"); + switch(optionPane.showChoiceDialog(CustomOptionPane.YESNOCANCELOption)) { + case CustomOptionPane.YES: assert true; break; + case CustomOptionPane.NO: assert true; break; + case CustomOptionPane.CANCEL: assert true; break; + + } + } diff --git a/src/CustomOptionPane.java b/src/CustomOptionPane.java index 3b96d20..f8c0a84 100644 --- a/src/CustomOptionPane.java +++ b/src/CustomOptionPane.java @@ -1,5 +1,6 @@ package src; +import javax.swing.*; import java.awt.*; import java.awt.event.*; @@ -11,14 +12,15 @@ public class CustomOptionPane extends Frame { public static final int OKCancelOption = 3; public static final int ANYWAYCANCELOption = 4; - public static final int YESOption = 0; - public static final int NOOption = 1; - public static final int CancelOption = 2; - public static final int OKOption = 3; - public static final int ANYWAYOption = 4; + public static final int YES = 0; + public static final int NO = 1; + public static final int CANCEL = 2; + public static final int OK = 3; + public static final int ANYWAY = 4; private String title = "CustomOptionPane.title"; private String label = "CustomOptionPane.label"; + private int res = CANCEL; public CustomOptionPane(String title, String label) { this.title = title; @@ -30,30 +32,48 @@ public CustomOptionPane(String title, String label) { this.setAlwaysOnTop(true); this.setTitle(title); this.setLayout(null); + JLabel lbl = new JLabel(SplitText(label), JLabel.CENTER); + lbl.setBounds(10, 65, 285, 25); + this.add(lbl); + } + public CustomOptionPane() { + this.setSize(300, 200); + this.setBackground(new Color(234, 234, 234)); + this.setResizable(false); + this.setLocationRelativeTo(null); + this.setAlwaysOnTop(true); + this.setTitle(title); + this.setLayout(null); + JLabel lbl = new JLabel(SplitText(label), JLabel.CENTER); + lbl.setBounds(10, 65, 285, 25); + this.add(lbl); } private String SplitText(String text) { - String FinalText = ""; - for (int i = 0; i != text.length(); i++) { - if ((i+1)%48 == 0) { - FinalText += text.substring(0, i+1); - text = text.substring(i+1); + StringBuilder output = new StringBuilder(); + output.append(""); + + int length = text.length(); + for (int i = 0; i < length; i += 47) { + output.append(text, i, Math.min(length, i + 47)); + if (i + 47 < length) { + output.append("
"); } } - return FinalText; + output.append(""); + return output.toString(); } public int showChoiceDialog(int state) { - final boolean[] isClosed = {false}; + this.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) {} @Override public void windowClosing(WindowEvent e) { - isClosed[0] = true; dispose(); } @@ -73,100 +93,97 @@ public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} }); - switch (state) { - Label lbl = new Label(SplitText(label)); - lbl.setBounds(10, 35, 300, 25); - this.add(lbl); - - Button Yes = new Button("Yes"); - Yes.setBounds(20, 105, 100, 25); - Yes.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return YESOption; - } + Button Yes = new Button("Yes"); + Yes.setBounds(20, 150, 100, 25); + Yes.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(0); } + }); - Button No = new Button("No"); - No.setBounds(180, 105, 100, 25); - No.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return NOOption; - } + Button No = new Button("No"); + No.setBounds(180, 150, 100, 25); + No.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(1); } + }); - Button Yes1 = new Button("Yes"); - Yes1.setBounds(10, 105, 75, 25); - Yes1.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return YESOption; - } + Button Yes1 = new Button("Yes"); + Yes1.setBounds(20, 150, 75, 25); + Yes1.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(0); } + }); - Button No1 = new Button("No"); - No1.setBounds(102, 105, 75, 25); - No1.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return NOOption; - } + Button No1 = new Button("No"); + No1.setBounds(112, 150, 75, 25); + No1.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(1); } + }); - Button Cancel = new Button("Cancel"); - Cancel.setBounds(215, 105, 75, 25); - Cancel.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return CancelOption; - } + Button Cancel = new Button("Cancel"); + Cancel.setBounds(205, 150, 75, 25); + Cancel.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(2); } + }); - Button OK = new Button("OK"); - OK.setBounds(50, 105, 100, 25); - OK.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return OKOption; - } + Button OK = new Button("OK"); + OK.setBounds(100, 150, 100, 25); + OK.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(3); } + }); - Button OK1 = new Button("OK"); - OK1.setBounds(20, 105, 100, 25); - OK1.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return OKOption; - } + Button OK1 = new Button("OK"); + OK1.setBounds(40, 150, 100, 25); + OK1.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(3); } + }); - Button Cancel1 = new Button("Cancel"); - Cancel1.setBounds(180, 105, 100, 25); - Cancel1.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return CancelOption; - } + Button Cancel1 = new Button("Cancel"); + Cancel1.setBounds(160, 150, 100, 25); + Cancel1.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(2); } + }); - Button Anyway = new Button("Do anyway"); - Anyway.setBounds(20, 105, 100, 25); - Anyway.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return ANYWAYOption; - } + Button Anyway = new Button("Do anyway"); + Anyway.setBounds(40, 150, 100, 25); + Anyway.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(4); } + }); - Button Cancel2 = new Button("Cancel"); - Cancel2.setBounds(180, 105, 100, 25); - Cancel2.addActionListener(new ActionListener()) { - @Override - public void actionPerformed(ActionEvent e) { - return CancelOption; - } + Button Cancel2 = new Button("Cancel"); + Cancel2.setBounds(160, 150, 100, 25); + Cancel2.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + changeResTo(2); } + }); + + switch (state) { case 0: this.add(Yes); @@ -190,20 +207,25 @@ public void actionPerformed(ActionEvent e) { case 4: this.add(Anyway); - this.add(Cancel); + this.add(Cancel2); break; default: throw new IllegalArgumentException("State " + state + " doesn't exit. Must be between 0 and 4"); } this.setVisible(true); + return res; + } - if (isClosed[0]) { - return CancelOption; - } + private void changeResTo(int res) { + this.res = res; } - public static void main(String[] args) { - new CustomOptionPane().showChoiceDialog(YESNOOption); + public void setTitle(String title) { + this.title = title; + } + + public void setLabel(String label) { + this.label = label; } -} +} \ No newline at end of file From fdb797c063b2c055b41c6e48afa7fd2f87a1ea67 Mon Sep 17 00:00:00 2001 From: azert Date: Sun, 10 Nov 2024 00:08:05 +0100 Subject: [PATCH 15/28] Adding features and completing Menu "File" --- .idea/artifacts/TextEditor_Java_jar.xml | 8 + src/CustomKeyListener.java | 51 ++++++ src/CustomMenuBar.java | 39 +++- src/CustomOptionPane.java | 231 ------------------------ src/CustomWindowListener.java | 16 +- src/ExmlEditor.java | 74 +++++++- 6 files changed, 167 insertions(+), 252 deletions(-) create mode 100644 .idea/artifacts/TextEditor_Java_jar.xml create mode 100644 src/CustomKeyListener.java delete mode 100644 src/CustomOptionPane.java diff --git a/.idea/artifacts/TextEditor_Java_jar.xml b/.idea/artifacts/TextEditor_Java_jar.xml new file mode 100644 index 0000000..b9403f3 --- /dev/null +++ b/.idea/artifacts/TextEditor_Java_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/TextEditor_Java_jar + + + + + \ No newline at end of file diff --git a/src/CustomKeyListener.java b/src/CustomKeyListener.java new file mode 100644 index 0000000..513582d --- /dev/null +++ b/src/CustomKeyListener.java @@ -0,0 +1,51 @@ +package src; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class CustomKeyListener implements KeyListener { + private final ExmlEditor window; + private boolean flagA = false; + + public CustomKeyListener(ExmlEditor window) { + this.window = window; + } + + @Override + public void keyTyped(KeyEvent e) { + if (!window.isFileSaved()) { + if (!flagA) { + window.setTitle(window.getTitle() + "*"); + flagA = true; + } + } else { + if (window.getOpenedFile() == null) { + window.setTitle("Notepad - Untitled"); + } else { + window.setTitle("Notepad - " + window.getOpenedFile().getName()); + } + flagA = false; + } + } + + @Override + public void keyPressed(KeyEvent e) { + } + + @Override + public void keyReleased(KeyEvent e) { + if (!window.isFileSaved()) { + if (!flagA) { + window.setTitle(window.getTitle() + "*"); + flagA = true; + } + } else { + if (window.getOpenedFile() == null) { + window.setTitle("Notepad - Untitled"); + } else { + window.setTitle("Notepad - " + window.getOpenedFile().getName()); + } + flagA = false; + } + } +} diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index 9559613..35cb490 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -1,5 +1,6 @@ package src; +import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -46,22 +47,40 @@ public CustomActionListener(String name, ExmlEditor parent) { } public void actionPerformed(ActionEvent e) { - CustomOptionPane optionPane = new CustomOptionPane(); switch (name) { case "New": if (!parent.isFileSaved()) { - optionPane.setTitle("Modications aren't saved"); - optionPane.setLabel("Your modifications aren't saved. Would you save them ?"); - switch(optionPane.showChoiceDialog(CustomOptionPane.YESNOCANCELOption)) { - case CustomOptionPane.YES: assert true; break; - case CustomOptionPane.NO: assert true; break; - case CustomOptionPane.CANCEL: assert true; break; - + switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { + case JOptionPane.YES_OPTION: parent.SaveAs(); parent.textArea.setText(""); parent.OpenedFile = null; parent.TextContent = ""; break; + case JOptionPane.NO_OPTION: parent.textArea.setText(""); parent.OpenedFile = null; parent.TextContent = ""; break; + case JOptionPane.CANCEL_OPTION: break; } - + } else { + parent.textArea.setText(""); + } + parent.TextContent = ""; + break; + case "Open": + if (!parent.isFileSaved()) { + switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { + case JOptionPane.YES_OPTION: parent.SaveAs(); parent.Open(); break; + case JOptionPane.NO_OPTION: parent.textArea.setText(""); parent.OpenedFile = null; parent.TextContent = ""; break; + case JOptionPane.CANCEL_OPTION: break; + } + } else { + parent.Open(); } - + break; + case "Save": + if (parent.OpenedFile == null) { + parent.SaveAs(); + } else { + parent.Save(); + } + break; + case "Save as": parent.SaveAs(); break; + case "Print": parent.printText(); break; case "New window": new ExmlEditor(); break; case "Close window": parent.dispose(); break; case "Exit": System.exit(0); diff --git a/src/CustomOptionPane.java b/src/CustomOptionPane.java deleted file mode 100644 index f8c0a84..0000000 --- a/src/CustomOptionPane.java +++ /dev/null @@ -1,231 +0,0 @@ -package src; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.*; - -public class CustomOptionPane extends Frame { - - public static final int YESNOOption = 0; - public static final int YESNOCANCELOption = 1; - public static final int OKOption = 2; - public static final int OKCancelOption = 3; - public static final int ANYWAYCANCELOption = 4; - - public static final int YES = 0; - public static final int NO = 1; - public static final int CANCEL = 2; - public static final int OK = 3; - public static final int ANYWAY = 4; - - private String title = "CustomOptionPane.title"; - private String label = "CustomOptionPane.label"; - private int res = CANCEL; - - public CustomOptionPane(String title, String label) { - this.title = title; - this.label = label; - this.setSize(300, 200); - this.setBackground(new Color(234, 234, 234)); - this.setResizable(false); - this.setLocationRelativeTo(null); - this.setAlwaysOnTop(true); - this.setTitle(title); - this.setLayout(null); - JLabel lbl = new JLabel(SplitText(label), JLabel.CENTER); - lbl.setBounds(10, 65, 285, 25); - this.add(lbl); - } - - public CustomOptionPane() { - this.setSize(300, 200); - this.setBackground(new Color(234, 234, 234)); - this.setResizable(false); - this.setLocationRelativeTo(null); - this.setAlwaysOnTop(true); - this.setTitle(title); - this.setLayout(null); - JLabel lbl = new JLabel(SplitText(label), JLabel.CENTER); - lbl.setBounds(10, 65, 285, 25); - this.add(lbl); - } - - private String SplitText(String text) { - StringBuilder output = new StringBuilder(); - output.append(""); - - int length = text.length(); - for (int i = 0; i < length; i += 47) { - output.append(text, i, Math.min(length, i + 47)); - if (i + 47 < length) { - output.append("
"); - } - } - - output.append(""); - return output.toString(); - } - - public int showChoiceDialog(int state) { - - this.addWindowListener(new WindowListener() { - @Override - public void windowOpened(WindowEvent e) {} - - @Override - public void windowClosing(WindowEvent e) { - dispose(); - } - - @Override - public void windowClosed(WindowEvent e) {} - - @Override - public void windowIconified(WindowEvent e) {} - - @Override - public void windowDeiconified(WindowEvent e) {} - - @Override - public void windowActivated(WindowEvent e) {} - - @Override - public void windowDeactivated(WindowEvent e) {} - }); - - Button Yes = new Button("Yes"); - Yes.setBounds(20, 150, 100, 25); - Yes.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(0); - } - }); - - Button No = new Button("No"); - No.setBounds(180, 150, 100, 25); - No.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(1); - } - }); - - Button Yes1 = new Button("Yes"); - Yes1.setBounds(20, 150, 75, 25); - Yes1.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(0); - } - }); - - Button No1 = new Button("No"); - No1.setBounds(112, 150, 75, 25); - No1.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(1); - } - }); - - Button Cancel = new Button("Cancel"); - Cancel.setBounds(205, 150, 75, 25); - Cancel.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(2); - } - }); - - Button OK = new Button("OK"); - OK.setBounds(100, 150, 100, 25); - OK.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(3); - } - }); - - Button OK1 = new Button("OK"); - OK1.setBounds(40, 150, 100, 25); - OK1.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(3); - } - }); - - Button Cancel1 = new Button("Cancel"); - Cancel1.setBounds(160, 150, 100, 25); - Cancel1.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(2); - } - }); - - Button Anyway = new Button("Do anyway"); - Anyway.setBounds(40, 150, 100, 25); - Anyway.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(4); - } - }); - - Button Cancel2 = new Button("Cancel"); - Cancel2.setBounds(160, 150, 100, 25); - Cancel2.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - changeResTo(2); - } - }); - - switch (state) { - - case 0: - this.add(Yes); - this.add(No); - break; - - case 1: - this.add(Yes1); - this.add(No1); - this.add(Cancel); - break; - - case 2: - this.add(OK); - break; - - case 3: - this.add(OK1); - this.add(Cancel1); - break; - - case 4: - this.add(Anyway); - this.add(Cancel2); - break; - - default: throw new IllegalArgumentException("State " + state + " doesn't exit. Must be between 0 and 4"); - } - - this.setVisible(true); - return res; - } - - private void changeResTo(int res) { - this.res = res; - } - - public void setTitle(String title) { - this.title = title; - } - - public void setLabel(String label) { - this.label = label; - } -} \ No newline at end of file diff --git a/src/CustomWindowListener.java b/src/CustomWindowListener.java index 7cdc61c..d3d521c 100644 --- a/src/CustomWindowListener.java +++ b/src/CustomWindowListener.java @@ -1,6 +1,6 @@ package src; -import java.awt.*; +import javax.swing.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; @@ -16,11 +16,21 @@ public void windowOpened(WindowEvent e) { } public void windowClosing(WindowEvent e) { + boolean CancelPressed = false; + if (!window.isFileSaved()) { - System.out.println("Not saved"); + switch (JOptionPane.showOptionDialog(window, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { + case JOptionPane.YES_OPTION: window.SaveAs(); window.textArea.setText(""); window.OpenedFile = null; window.TextContent = ""; break; + case JOptionPane.NO_OPTION: window.textArea.setText(""); window.OpenedFile = null; window.TextContent = ""; break; + case JOptionPane.CANCEL_OPTION: CancelPressed = true; break; + } } - window.dispose(); + if (!CancelPressed) { + window.dispose(); + } else { + CancelPressed = false; + } } public void windowClosed(WindowEvent e) { diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index 5f4e3ed..77d3110 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -1,27 +1,28 @@ package src; import java.awt.*; -import java.io.File; +import java.awt.print.PageFormat; +import java.awt.print.Printable; +import java.awt.print.PrinterException; +import java.awt.print.PrinterJob; +import java.io.*; public class ExmlEditor extends Frame { - static File OpenedFile = null; - static TextArea textArea = new TextArea(); - static String TextContent = ""; + File OpenedFile = null; + TextArea textArea = new TextArea(); + String TextContent = ""; public ExmlEditor() { this.setTitle("Notepad - Untitled"); this.setSize(800, 600); this.setBackground(new Color(234, 234, 234)); this.addWindowListener(new CustomWindowListener(this)); + textArea.addKeyListener(new CustomKeyListener(this)); this.add(textArea); this.setMenuBar(new CustomMenuBar(this)); this.setVisible(true); } - public void setTextContent(String text) { - TextContent = text; - } - public boolean isFileSaved() { if (OpenedFile == null) { return textArea.getText().isEmpty(); @@ -30,6 +31,63 @@ public boolean isFileSaved() { } } + public void SaveAs() { + FileDialog fileDialog = new FileDialog(this, "Save", FileDialog.SAVE); + fileDialog.setVisible(true); + OpenedFile = new File(fileDialog.getDirectory()+ "\\" + fileDialog.getFile()); + this.setTitle("Notepad - " + OpenedFile.getName()); + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(OpenedFile)); + writer.write(textArea.getText()); + writer.close(); + TextContent = textArea.getText(); + } catch (IOException e) { + e.fillInStackTrace(); + } + } + + public void Save() { + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(OpenedFile)); + writer.write(textArea.getText()); + writer.close(); + TextContent = textArea.getText(); + this.setTitle("Notepad - " + OpenedFile.getName()); + } catch (IOException e) { + e.fillInStackTrace(); + } + } + + public void Open() { + FileDialog fileDialog = new FileDialog(this, "Open", FileDialog.LOAD); + fileDialog.setVisible(true); + OpenedFile = new File(fileDialog.getDirectory()+ "\\" + fileDialog.getFile()); + this.setTitle("Notepad - " + OpenedFile.getName()); + try { + StringBuilder text = new StringBuilder(); + BufferedReader reader = new BufferedReader(new FileReader(OpenedFile)); + String line = reader.readLine(); + while (line != null) { + text.append(line).append("\n"); + line = reader.readLine(); + } + + reader.close(); + textArea.setText(text.substring(0, text.toString().length()-1)); + TextContent = textArea.getText(); + } catch (IOException e) { + e.fillInStackTrace(); + } + } + + public String getTextContent() { + return TextContent; + } + + public File getOpenedFile() { + return OpenedFile; + } + public static void main(String[] args) { new ExmlEditor(); } From ae908bdfe297dd3ee7471671c4ef2b3079306a25 Mon Sep 17 00:00:00 2001 From: azert Date: Mon, 11 Nov 2024 19:39:45 +0100 Subject: [PATCH 16/28] Adding features on "Edit" menu --- ...{TextEditor_Java_jar.xml => Indev_1_0.xml} | 4 +- META-INF/MANIFEST.MF | 3 + src/CustomKeyListener.java | 4 + src/CustomMenuBar.java | 57 +++++- src/ExmlEditor.java | 166 +++++++++++++++--- 5 files changed, 198 insertions(+), 36 deletions(-) rename .idea/artifacts/{TextEditor_Java_jar.xml => Indev_1_0.xml} (58%) create mode 100644 META-INF/MANIFEST.MF diff --git a/.idea/artifacts/TextEditor_Java_jar.xml b/.idea/artifacts/Indev_1_0.xml similarity index 58% rename from .idea/artifacts/TextEditor_Java_jar.xml rename to .idea/artifacts/Indev_1_0.xml index b9403f3..39ea8f3 100644 --- a/.idea/artifacts/TextEditor_Java_jar.xml +++ b/.idea/artifacts/Indev_1_0.xml @@ -1,6 +1,6 @@ - - $PROJECT_DIR$/out/artifacts/TextEditor_Java_jar + + $PROJECT_DIR$/out/artifacts/Indev_1_0 diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000..161feed --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: src.ExmlEditor + diff --git a/src/CustomKeyListener.java b/src/CustomKeyListener.java index 513582d..8852053 100644 --- a/src/CustomKeyListener.java +++ b/src/CustomKeyListener.java @@ -47,5 +47,9 @@ public void keyReleased(KeyEvent e) { } flagA = false; } + + if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_COMMA || e.getKeyCode() == KeyEvent.VK_PERIOD) { + window.AppendText(); + } } } diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index 35cb490..bec2822 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -4,12 +4,14 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; public class CustomMenuBar extends MenuBar { public CustomMenuBar(ExmlEditor parent) { Menu filemenu = new Menu("File"); - MenuComponent[] filemenuitems = {new MenuItem("New"), new MenuItem("New window"), new MenuSpace(),new MenuItem("Open"), new MenuItem("Save"), new MenuItem("Save as"), new MenuItem("Print"), new MenuItem("Close"), new MenuItem("Close window"), new MenuSpace(), new MenuItem("Exit")}; + MenuComponent[] filemenuitems = {new MenuItem("New", new MenuShortcut(KeyEvent.VK_N, false)), new MenuItem("New window"), new MenuSpace(),new MenuItem("Open", new MenuShortcut(KeyEvent.VK_O, false)), new MenuItem("Save", new MenuShortcut(KeyEvent.VK_S, false)), new MenuItem("Save as", new MenuShortcut(KeyEvent.VK_S, true)), new MenuItem("Print"), new MenuItem("Close window", new MenuShortcut(KeyEvent.VK_F4, true)), new MenuSpace(), new MenuItem("Exit", new MenuShortcut(KeyEvent.VK_F4, false))}; Menu editmenu = new Menu("Edit"); + MenuComponent[] editmenuitems = {new MenuItem("Undo", new MenuShortcut(KeyEvent.VK_Z, false)), new MenuItem("Redo", new MenuShortcut(KeyEvent.VK_Y, false)), new MenuSpace(),new MenuItem("Copy", new MenuShortcut(KeyEvent.VK_C, false)), new MenuItem("Cut", new MenuShortcut(KeyEvent.VK_X, false)), new MenuItem("Paste", new MenuShortcut(KeyEvent.VK_V, false)), new MenuSpace(), new MenuItem("Delete", new MenuShortcut(KeyEvent.VK_DELETE, false)), new MenuSpace(), new Menu("Insert"), new MenuItem("Replace", new MenuShortcut(KeyEvent.VK_R, false))}; Menu selectionmenu = new Menu("Selection"); Menu appearancemenu = new Menu("Appearance"); Menu securitymenu = new Menu("Security"); @@ -18,11 +20,20 @@ public CustomMenuBar(ExmlEditor parent) { for (MenuComponent menuComponent : filemenuitems) { if (menuComponent instanceof MenuSpace) { filemenu.addSeparator(); - } else { //Avoiding a ClassCastException + } else if (menuComponent instanceof MenuItem){ //Avoiding a ClassCastException filemenu.add((MenuItem) menuComponent); ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent).getLabel(), parent)); } } + + for (MenuComponent menuComponent : editmenuitems) { + if (menuComponent instanceof MenuSpace) { + editmenu.addSeparator(); + } else if (menuComponent instanceof MenuItem){ //Avoiding a ClassCastException + editmenu.add((MenuItem) menuComponent); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent).getLabel(), parent)); + } + } this.add(filemenu); this.add(editmenu); this.add(selectionmenu); @@ -58,8 +69,10 @@ public void actionPerformed(ActionEvent e) { } else { parent.textArea.setText(""); } + parent.setTitle("Notepad - Untitled"); parent.TextContent = ""; break; + case "New window": new ExmlEditor(); break; case "Open": if (!parent.isFileSaved()) { switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { @@ -80,10 +93,42 @@ public void actionPerformed(ActionEvent e) { } break; case "Save as": parent.SaveAs(); break; - case "Print": parent.printText(); break; - case "New window": new ExmlEditor(); break; - case "Close window": parent.dispose(); break; - case "Exit": System.exit(0); + case "Print": + if (parent.OpenedFile == null) { + parent.SaveAs(); + } else { + parent.Save(); + } + parent.print(); + break; + case "Close window": + if (!parent.isFileSaved()) { + switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { + case JOptionPane.YES_OPTION: parent.SaveAs(); break; + case JOptionPane.NO_OPTION: parent.dispose(); break; + case JOptionPane.CANCEL_OPTION: break; + } + } else { + parent.dispose(); + } + break; + case "Exit": + if (!parent.isFileSaved()) { + switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { + case JOptionPane.YES_OPTION: parent.SaveAs(); System.exit(0); break; + case JOptionPane.NO_OPTION: System.exit(0); break; + case JOptionPane.CANCEL_OPTION: break; + } + } else { + System.exit(0); + } + + case "Undo": parent.Undo(); break; + case "Redo": parent.Redo(); break; + case "Copy" : parent.Copy(); break; + case "Cut" : parent.Cut(); break; + case "Paste" : parent.Paste(); break; + case "Delete" : parent.Delete(); break; default: throw new IllegalArgumentException("You might have forget to update this block"); } } diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index 77d3110..2dfe3a1 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -1,16 +1,21 @@ package src; import java.awt.*; -import java.awt.print.PageFormat; -import java.awt.print.Printable; -import java.awt.print.PrinterException; -import java.awt.print.PrinterJob; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.font.*; +import java.awt.print.*; import java.io.*; +import java.text.*; +import java.util.*; public class ExmlEditor extends Frame { File OpenedFile = null; TextArea textArea = new TextArea(); String TextContent = ""; + public Stack undoStack = new Stack<>(); + public Stack redoStack = new Stack<>(); public ExmlEditor() { this.setTitle("Notepad - Untitled"); @@ -20,6 +25,7 @@ public ExmlEditor() { textArea.addKeyListener(new CustomKeyListener(this)); this.add(textArea); this.setMenuBar(new CustomMenuBar(this)); + AppendText(); this.setVisible(true); } @@ -34,15 +40,17 @@ public boolean isFileSaved() { public void SaveAs() { FileDialog fileDialog = new FileDialog(this, "Save", FileDialog.SAVE); fileDialog.setVisible(true); - OpenedFile = new File(fileDialog.getDirectory()+ "\\" + fileDialog.getFile()); - this.setTitle("Notepad - " + OpenedFile.getName()); - try { - BufferedWriter writer = new BufferedWriter(new FileWriter(OpenedFile)); - writer.write(textArea.getText()); - writer.close(); - TextContent = textArea.getText(); - } catch (IOException e) { - e.fillInStackTrace(); + if (!(fileDialog.getDirectory() == null || fileDialog.getFile() == null)) { + OpenedFile = new File(fileDialog.getDirectory()+ "\\" + fileDialog.getFile()); + this.setTitle("Notepad - " + OpenedFile.getName()); + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(OpenedFile)); + writer.write(textArea.getText()); + writer.close(); + TextContent = textArea.getText(); + } catch (IOException e) { + e.fillInStackTrace(); + } } } @@ -61,22 +69,24 @@ public void Save() { public void Open() { FileDialog fileDialog = new FileDialog(this, "Open", FileDialog.LOAD); fileDialog.setVisible(true); - OpenedFile = new File(fileDialog.getDirectory()+ "\\" + fileDialog.getFile()); - this.setTitle("Notepad - " + OpenedFile.getName()); - try { - StringBuilder text = new StringBuilder(); - BufferedReader reader = new BufferedReader(new FileReader(OpenedFile)); - String line = reader.readLine(); - while (line != null) { - text.append(line).append("\n"); - line = reader.readLine(); - } + if (!(fileDialog.getDirectory() == null || fileDialog.getFile() == null)) { + OpenedFile = new File(fileDialog.getDirectory() + "\\" + fileDialog.getFile()); + this.setTitle("Notepad - " + OpenedFile.getName()); + try { + StringBuilder text = new StringBuilder(); + BufferedReader reader = new BufferedReader(new FileReader(OpenedFile)); + String line = reader.readLine(); + while (line != null) { + text.append(line).append("\n"); + line = reader.readLine(); + } - reader.close(); - textArea.setText(text.substring(0, text.toString().length()-1)); - TextContent = textArea.getText(); - } catch (IOException e) { - e.fillInStackTrace(); + reader.close(); + textArea.setText(text.substring(0, text.toString().length() - 1)); + TextContent = textArea.getText(); + } catch (IOException e) { + e.fillInStackTrace(); + } } } @@ -88,7 +98,107 @@ public File getOpenedFile() { return OpenedFile; } + public void print() { + PrinterJob job = PrinterJob.getPrinterJob(); + job.setPrintable(new Printable() { + @Override + public int print(Graphics g, PageFormat pf, int page) { + if (page > 0) { + return NO_SUCH_PAGE; + } + + Graphics2D g2d = (Graphics2D) g; + g2d.translate(pf.getImageableX(), pf.getImageableY()); + + String text = TextContent; + Font font = new Font("Serif", Font.PLAIN, 12); + g2d.setFont(font); + + AttributedString attributedText = new AttributedString(text); + attributedText.addAttribute(TextAttribute.FONT, font); + AttributedCharacterIterator iterator = attributedText.getIterator(); + + int y = 0; + int lineHeight = g2d.getFontMetrics().getHeight(); + LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, g2d.getFontRenderContext()); + + float wrappingWidth = (float) pf.getImageableWidth(); + + while (measurer.getPosition() < iterator.getEndIndex()) { + TextLayout layout = measurer.nextLayout(wrappingWidth); + y += (int) layout.getAscent(); + layout.draw(g2d, 0, y); + y += (int) (layout.getDescent() + layout.getLeading()); + } + + return PAGE_EXISTS; + } + }); + + boolean doPrint = job.printDialog(); + if (doPrint) { + try { + job.print(); + } catch (PrinterException e) { + e.fillInStackTrace(); + } + } + } + + public void AppendText() { + undoStack.push(textArea.getText()); + redoStack.clear(); + } + + public void Undo() { + if (!undoStack.isEmpty()) { + redoStack.push(textArea.getText()); + textArea.setText(undoStack.pop()); + } + } + + public void Redo() { + if (!redoStack.isEmpty()) { + undoStack.push(textArea.getText()); + textArea.setText(redoStack.pop()); + } + } + + public void Copy() { + if (!textArea.getSelectedText().isEmpty()) { + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(textArea.getSelectedText()), null); + } else { + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(textArea.getText()), null); + } + } + + public void Cut() { + if (!textArea.getSelectedText().isEmpty()) { + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(textArea.getSelectedText()), null); + textArea.setText(textArea.getText().substring(0, textArea.getSelectionStart()) + textArea.getText().substring(textArea.getSelectionEnd())); + } else { + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(textArea.getText()), null); + textArea.setText(""); + } + } + + public void Paste() { + try { + textArea.insert((String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor), textArea.getCaretPosition()); + } catch (UnsupportedFlavorException | IOException e) { + e.fillInStackTrace(); + } + } + public static void main(String[] args) { new ExmlEditor(); } + + public void Delete() { + if (!textArea.getSelectedText().isEmpty()) { + textArea.setText(textArea.getText().substring(0, textArea.getSelectionStart()) + textArea.getText().substring(textArea.getSelectionEnd())); + } else { + textArea.setText(""); + } + } } \ No newline at end of file From 240fee090c3778911f6c04af30171e81b947e4cc Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:57:33 +0100 Subject: [PATCH 17/28] Update ExmlEditor.java --- src/ExmlEditor.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index 2dfe3a1..767b0fd 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -1,9 +1,7 @@ package src; import java.awt.*; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.datatransfer.*; import java.awt.font.*; import java.awt.print.*; import java.io.*; @@ -201,4 +199,4 @@ public void Delete() { textArea.setText(""); } } -} \ No newline at end of file +} From 8688ef658a829f491f0e9ff15603b10ed93889ed Mon Sep 17 00:00:00 2001 From: azert Date: Wed, 13 Nov 2024 21:09:52 +0100 Subject: [PATCH 18/28] Adding features on "Edit" menu --- src/CustomMenuBar.java | 42 +++++++++++++++++++------ src/ExmlEditor.java | 69 +++++++++++++++++++++++++++++++++++++++--- src/Test.java | 29 ++++++++++++++++++ 3 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 src/Test.java diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index bec2822..6daf568 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -11,7 +11,7 @@ public CustomMenuBar(ExmlEditor parent) { Menu filemenu = new Menu("File"); MenuComponent[] filemenuitems = {new MenuItem("New", new MenuShortcut(KeyEvent.VK_N, false)), new MenuItem("New window"), new MenuSpace(),new MenuItem("Open", new MenuShortcut(KeyEvent.VK_O, false)), new MenuItem("Save", new MenuShortcut(KeyEvent.VK_S, false)), new MenuItem("Save as", new MenuShortcut(KeyEvent.VK_S, true)), new MenuItem("Print"), new MenuItem("Close window", new MenuShortcut(KeyEvent.VK_F4, true)), new MenuSpace(), new MenuItem("Exit", new MenuShortcut(KeyEvent.VK_F4, false))}; Menu editmenu = new Menu("Edit"); - MenuComponent[] editmenuitems = {new MenuItem("Undo", new MenuShortcut(KeyEvent.VK_Z, false)), new MenuItem("Redo", new MenuShortcut(KeyEvent.VK_Y, false)), new MenuSpace(),new MenuItem("Copy", new MenuShortcut(KeyEvent.VK_C, false)), new MenuItem("Cut", new MenuShortcut(KeyEvent.VK_X, false)), new MenuItem("Paste", new MenuShortcut(KeyEvent.VK_V, false)), new MenuSpace(), new MenuItem("Delete", new MenuShortcut(KeyEvent.VK_DELETE, false)), new MenuSpace(), new Menu("Insert"), new MenuItem("Replace", new MenuShortcut(KeyEvent.VK_R, false))}; + MenuComponent[] editmenuitems = {new MenuItem("Undo", new MenuShortcut(KeyEvent.VK_Z, false)), new MenuItem("Redo", new MenuShortcut(KeyEvent.VK_Y, false)), new MenuSpace(),new MenuItem("Copy", new MenuShortcut(KeyEvent.VK_C, false)), new MenuItem("Cut", new MenuShortcut(KeyEvent.VK_X, false)), new MenuItem("Paste", new MenuShortcut(KeyEvent.VK_V, false)), new MenuItem("Delete", new MenuShortcut(KeyEvent.VK_DELETE, false)), new MenuSpace(), new MenuItem("Insert here"), new MenuItem("Insert values"), new MenuSpace(), new MenuItem("Replace", new MenuShortcut(KeyEvent.VK_R, false))}; Menu selectionmenu = new Menu("Selection"); Menu appearancemenu = new Menu("Appearance"); Menu securitymenu = new Menu("Security"); @@ -20,18 +20,18 @@ public CustomMenuBar(ExmlEditor parent) { for (MenuComponent menuComponent : filemenuitems) { if (menuComponent instanceof MenuSpace) { filemenu.addSeparator(); - } else if (menuComponent instanceof MenuItem){ //Avoiding a ClassCastException + } else if (menuComponent instanceof MenuItem) { filemenu.add((MenuItem) menuComponent); - ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent).getLabel(), parent)); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); } } for (MenuComponent menuComponent : editmenuitems) { if (menuComponent instanceof MenuSpace) { editmenu.addSeparator(); - } else if (menuComponent instanceof MenuItem){ //Avoiding a ClassCastException + } else if (menuComponent instanceof MenuItem) { editmenu.add((MenuItem) menuComponent); - ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent).getLabel(), parent)); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); } } this.add(filemenu); @@ -50,15 +50,15 @@ public MenuSpace() {} class CustomActionListener implements ActionListener { ExmlEditor parent = null; - String name = ""; + MenuItem menuItem = null; - public CustomActionListener(String name, ExmlEditor parent) { - this.name = name; + public CustomActionListener(MenuItem menuItem, ExmlEditor parent) { + this.menuItem = menuItem; this.parent = parent; } public void actionPerformed(ActionEvent e) { - switch (name) { + switch (menuItem.getLabel()) { case "New": if (!parent.isFileSaved()) { switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { @@ -77,7 +77,7 @@ public void actionPerformed(ActionEvent e) { if (!parent.isFileSaved()) { switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { case JOptionPane.YES_OPTION: parent.SaveAs(); parent.Open(); break; - case JOptionPane.NO_OPTION: parent.textArea.setText(""); parent.OpenedFile = null; parent.TextContent = ""; break; + case JOptionPane.NO_OPTION: parent.textArea.setText(""); parent.OpenedFile = null; parent.TextContent = ""; parent.Open(); break; case JOptionPane.CANCEL_OPTION: break; } } else { @@ -129,6 +129,28 @@ public void actionPerformed(ActionEvent e) { case "Cut" : parent.Cut(); break; case "Paste" : parent.Paste(); break; case "Delete" : parent.Delete(); break; + case "Insert here" : + String res = JOptionPane.showInputDialog(parent, "What would you insert ?"); + if (res == null) { + break; + } else { + parent.Insert(res); + } + break; + case "Insert values" : + JPanel p = new JPanel(); + p.setLayout(new GridLayout(1, 1)); + String[] options = {"File name", "File size", "File path", "Epoch", "Time stamp", "Year", "Month", "Week", "Day", "Day of the week", "Hour", "Minute", "Second", "Random char", "Username", "Ipv4", "Ipv6"}; + JComboBox comboBox = new JComboBox<>(options); + p.add(comboBox); + + if (JOptionPane.showConfirmDialog(null, p, "Veuillez sélectionner une option", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { + parent.Insert(parent.getKey((String) comboBox.getSelectedItem())); + } + + + break; + default: throw new IllegalArgumentException("You might have forget to update this block"); } } diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index 2dfe3a1..c164022 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -1,5 +1,6 @@ package src; +import javax.swing.*; import java.awt.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; @@ -8,6 +9,7 @@ import java.awt.print.*; import java.io.*; import java.text.*; +import java.time.Instant; import java.util.*; public class ExmlEditor extends Frame { @@ -16,6 +18,8 @@ public class ExmlEditor extends Frame { String TextContent = ""; public Stack undoStack = new Stack<>(); public Stack redoStack = new Stack<>(); + public String[] days = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; + public String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; public ExmlEditor() { this.setTitle("Notepad - Untitled"); @@ -82,7 +86,9 @@ public void Open() { } reader.close(); - textArea.setText(text.substring(0, text.toString().length() - 1)); + if (!text.toString().isEmpty()) { + textArea.setText(text.substring(0, text.toString().length() - 1)); + } TextContent = textArea.getText(); } catch (IOException e) { e.fillInStackTrace(); @@ -180,25 +186,80 @@ public void Cut() { Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(textArea.getText()), null); textArea.setText(""); } + AppendText(); } public void Paste() { try { textArea.insert((String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor), textArea.getCaretPosition()); + AppendText(); } catch (UnsupportedFlavorException | IOException e) { e.fillInStackTrace(); } - } - public static void main(String[] args) { - new ExmlEditor(); } public void Delete() { + AppendText(); if (!textArea.getSelectedText().isEmpty()) { textArea.setText(textArea.getText().substring(0, textArea.getSelectionStart()) + textArea.getText().substring(textArea.getSelectionEnd())); } else { textArea.setText(""); } } + + public void Insert(String text) { + int pos = textArea.getCaretPosition(); + textArea.insert(text, pos); + AppendText(); + textArea.setCaretPosition(pos); + } + + public String getKey(String key) { + switch (key) { + case "File name": + try { + return OpenedFile.getName(); + } catch (Exception e) { + return "Unavailable"; + } + case "File size": + return String.valueOf(textArea.getText().getBytes().length); + case "File path": + try { + return OpenedFile.getAbsolutePath(); + } catch (Exception e) { + return "Unavailable"; + } + case "Epoch": + return String.valueOf(Instant.now().getEpochSecond()); + case "Time stamp": + return String.valueOf(Instant.now()).replace('T', ' ').replace('Z', (char) 0); + case "Year": + return String.valueOf(new GregorianCalendar().get(GregorianCalendar.YEAR)); + case "Month": + return months[new GregorianCalendar().get(GregorianCalendar.MONTH)]; + case "Week": + return String.valueOf(new GregorianCalendar().get(GregorianCalendar.WEEK_OF_YEAR)); + case "Day": + return String.valueOf(new GregorianCalendar().get(GregorianCalendar.DAY_OF_MONTH)); + case "Day of the week": + return days[new GregorianCalendar().get(GregorianCalendar.DAY_OF_WEEK)] + ", " + new GregorianCalendar().get(GregorianCalendar.DAY_OF_MONTH); + case "Hour": + return String.valueOf(new GregorianCalendar().get(GregorianCalendar.HOUR_OF_DAY)); + case "Minute": + return String.valueOf(new GregorianCalendar().get(GregorianCalendar.MINUTE)); + case "Second": + return String.valueOf(new GregorianCalendar().get(GregorianCalendar.SECOND)); + case "Random char": + return String.valueOf((char) new Random().nextInt()); + + } + + return null; + } + + public static void main(String[] args) { + new ExmlEditor(); + } } \ No newline at end of file diff --git a/src/Test.java b/src/Test.java new file mode 100644 index 0000000..499f91b --- /dev/null +++ b/src/Test.java @@ -0,0 +1,29 @@ +package src; + +import javax.swing.*; + +public class Test { + public static void main(String[] args) { + // Créer un panneau pour contenir la combobox + JPanel panel = new JPanel(); + + // Création de la combobox avec des options + JLabel comboBoxLabel = new JLabel("Sélectionnez une option :"); + String[] options = {"Option 1", "Option 2", "Option 3"}; + JComboBox comboBox = new JComboBox<>(options); + + // Ajouter les composants au panneau + panel.add(comboBoxLabel); + panel.add(comboBox); + + // Afficher la boîte de dialogue + int result = JOptionPane.showConfirmDialog(null, panel, "Veuillez sélectionner une option", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); + + if (result == JOptionPane.OK_OPTION) { + String selectedOption = (String) comboBox.getSelectedItem(); + System.out.println("Vous avez sélectionné : " + selectedOption); + } else { + System.out.println("La sélection a été annulée."); + } + } +} From 260d559e941e4d1eee3a19e38049eeb777be749b Mon Sep 17 00:00:00 2001 From: azert Date: Thu, 14 Nov 2024 19:05:08 +0100 Subject: [PATCH 19/28] Adding features on "Edit" menu --- src/ExmlEditor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index c164022..8dbd788 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -252,7 +252,7 @@ public String getKey(String key) { case "Second": return String.valueOf(new GregorianCalendar().get(GregorianCalendar.SECOND)); case "Random char": - return String.valueOf((char) new Random().nextInt()); + return String.valueOf((char) new Random().nextInt(0, 127)); } From 3f4bf209387cee6f1eec21d4e4d7c48255c20e21 Mon Sep 17 00:00:00 2001 From: azert Date: Thu, 14 Nov 2024 19:58:41 +0100 Subject: [PATCH 20/28] Adding features on "Edit" menu --- src/CustomMenuBar.java | 13 +++++++++- src/ExmlEditor.java | 54 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index 6daf568..555a9c1 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -144,13 +144,24 @@ public void actionPerformed(ActionEvent e) { JComboBox comboBox = new JComboBox<>(options); p.add(comboBox); - if (JOptionPane.showConfirmDialog(null, p, "Veuillez sélectionner une option", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { + if (JOptionPane.showConfirmDialog(null, p, "Select a value to insert", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { parent.Insert(parent.getKey((String) comboBox.getSelectedItem())); } break; + case "Replace": + JPanel p2 = new JPanel(); + p2.setLayout(new GridLayout(2, 1, 20, 40)); + JTextField oldSequence = new JTextField(); + p2.add(oldSequence); + JTextField newSequence = new JTextField(); + p2.add(newSequence); + if(JOptionPane.showConfirmDialog(null, p2, "Replace", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { + parent.Replace(oldSequence.getText(), newSequence.getText()); + } + break; default: throw new IllegalArgumentException("You might have forget to update this block"); } } diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index 8dbd788..17f1d08 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -1,6 +1,5 @@ package src; -import javax.swing.*; import java.awt.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; @@ -8,6 +7,9 @@ import java.awt.font.*; import java.awt.print.*; import java.io.*; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URL; import java.text.*; import java.time.Instant; import java.util.*; @@ -253,13 +255,61 @@ public String getKey(String key) { return String.valueOf(new GregorianCalendar().get(GregorianCalendar.SECOND)); case "Random char": return String.valueOf((char) new Random().nextInt(0, 127)); + case "Username" : + return System.getenv("USERNAME"); + case "Ipv4": + try { + URL url = new URI("https://api.ipify.org").toURL(); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); - } + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String inputLine; + StringBuilder response = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + + in.close(); + + return response.toString(); + + } catch (Exception e) { + e.fillInStackTrace(); + } + + case "Ipv6": + try { + URL url = new URI("https://api6.ipify.org").toURL(); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String inputLine; + StringBuilder response = new StringBuilder(); + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + + in.close(); + + return response.toString(); + + } catch (Exception e) { + e.fillInStackTrace(); + } + + } return null; } public static void main(String[] args) { new ExmlEditor(); } + + public void Replace(String oldSequence, String newSequence) { + + } } \ No newline at end of file From 7345b2e611a71964f8d5b7b58a8d4d1aede9392a Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Mon, 18 Nov 2024 06:38:19 +0100 Subject: [PATCH 21/28] Update ExmlEditor.java --- src/ExmlEditor.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index 17f1d08..304867c 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -1,15 +1,11 @@ package src; import java.awt.*; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.datatransfer.*; import java.awt.font.*; import java.awt.print.*; import java.io.*; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URL; +import java.net.*; import java.text.*; import java.time.Instant; import java.util.*; From 25bf64c3feddaebd7cbf176b7db30467fd3ca85a Mon Sep 17 00:00:00 2001 From: azert Date: Wed, 20 Nov 2024 16:18:52 +0100 Subject: [PATCH 22/28] Finishing features for Edit + Starting Selection features --- src/CustomMenuBar.java | 32 +++++++++++++++++++++++++++++++- src/ExmlEditor.java | 31 ++++++++++++++++++++++++++++--- src/Test.java | 29 ----------------------------- 3 files changed, 59 insertions(+), 33 deletions(-) delete mode 100644 src/Test.java diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index 555a9c1..3a69339 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -5,6 +5,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; +import java.util.Objects; public class CustomMenuBar extends MenuBar { public CustomMenuBar(ExmlEditor parent) { @@ -13,6 +14,7 @@ public CustomMenuBar(ExmlEditor parent) { Menu editmenu = new Menu("Edit"); MenuComponent[] editmenuitems = {new MenuItem("Undo", new MenuShortcut(KeyEvent.VK_Z, false)), new MenuItem("Redo", new MenuShortcut(KeyEvent.VK_Y, false)), new MenuSpace(),new MenuItem("Copy", new MenuShortcut(KeyEvent.VK_C, false)), new MenuItem("Cut", new MenuShortcut(KeyEvent.VK_X, false)), new MenuItem("Paste", new MenuShortcut(KeyEvent.VK_V, false)), new MenuItem("Delete", new MenuShortcut(KeyEvent.VK_DELETE, false)), new MenuSpace(), new MenuItem("Insert here"), new MenuItem("Insert values"), new MenuSpace(), new MenuItem("Replace", new MenuShortcut(KeyEvent.VK_R, false))}; Menu selectionmenu = new Menu("Selection"); + MenuComponent[] selectionmenuitems = {new MenuItem("Select All", new MenuShortcut(KeyEvent.VK_A, false)), new MenuSpace(), new MenuItem("Copy line up"), new MenuItem("Copy line down"), new MenuItem("Move line up"), new MenuItem("Move line down"), new MenuItem("Duplicate line")}; Menu appearancemenu = new Menu("Appearance"); Menu securitymenu = new Menu("Security"); Menu toolsmenu = new Menu("Tools"); @@ -34,6 +36,16 @@ public CustomMenuBar(ExmlEditor parent) { ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); } } + + for (MenuComponent menuComponent : selectionmenuitems) { + if (menuComponent instanceof MenuSpace) { + selectionmenu.addSeparator(); + } else if (menuComponent instanceof MenuItem) { + selectionmenu.add((MenuItem) menuComponent); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); + } + } + this.add(filemenu); this.add(editmenu); this.add(selectionmenu); @@ -145,7 +157,7 @@ public void actionPerformed(ActionEvent e) { p.add(comboBox); if (JOptionPane.showConfirmDialog(null, p, "Select a value to insert", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { - parent.Insert(parent.getKey((String) comboBox.getSelectedItem())); + parent.Insert(parent.getKey((String) Objects.requireNonNull(comboBox.getSelectedItem()))); } @@ -162,6 +174,24 @@ public void actionPerformed(ActionEvent e) { parent.Replace(oldSequence.getText(), newSequence.getText()); } break; + case "Select All": + parent.SelectAll(); + break; + case "Copy line up": + parent.CopyLineUp(); + break; + case "Copy line down": + parent.CopyLineDown(); + break; + case "Move line up": + parent.MoveLineUp(); + break; + case "Move line down": + parent.MoveLineDown(); + break; + case "Duplicate line": + parent.DuplicateLine(); + break; default: throw new IllegalArgumentException("You might have forget to update this block"); } } diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index 304867c..d3a6bd0 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -301,11 +301,36 @@ public String getKey(String key) { return null; } - public static void main(String[] args) { - new ExmlEditor(); + public void Replace(String oldSequence, String newSequence) { + textArea.setText(textArea.getText().replace(oldSequence, newSequence)); } - public void Replace(String oldSequence, String newSequence) { + public void SelectAll() { + textArea.setSelectionStart(0); + textArea.setSelectionEnd(textArea.getText().length()); + } + + public void CopyLineUp() { + System.out.println(textArea.getCaretPosition()); + } + + public void CopyLineDown() { + + } + + public void MoveLineUp() { } + + public void MoveLineDown() { + + } + + public void DuplicateLine() { + + } + + public static void main(String[] args) { + new ExmlEditor(); + } } \ No newline at end of file diff --git a/src/Test.java b/src/Test.java deleted file mode 100644 index 499f91b..0000000 --- a/src/Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package src; - -import javax.swing.*; - -public class Test { - public static void main(String[] args) { - // Créer un panneau pour contenir la combobox - JPanel panel = new JPanel(); - - // Création de la combobox avec des options - JLabel comboBoxLabel = new JLabel("Sélectionnez une option :"); - String[] options = {"Option 1", "Option 2", "Option 3"}; - JComboBox comboBox = new JComboBox<>(options); - - // Ajouter les composants au panneau - panel.add(comboBoxLabel); - panel.add(comboBox); - - // Afficher la boîte de dialogue - int result = JOptionPane.showConfirmDialog(null, panel, "Veuillez sélectionner une option", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); - - if (result == JOptionPane.OK_OPTION) { - String selectedOption = (String) comboBox.getSelectedItem(); - System.out.println("Vous avez sélectionné : " + selectedOption); - } else { - System.out.println("La sélection a été annulée."); - } - } -} From 8d76af5e1298b1984130a28c348ec7c8bfb04e4b Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Wed, 20 Nov 2024 16:34:00 +0100 Subject: [PATCH 23/28] Update ReadMe.md --- ReadMe.md | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index bc1727c..65feb2f 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,17 +1,5 @@ -# TextEditor (java) Modifications +# Update alpha 1.0 -This is a simple text editor. Using it as a starting point, it needs a bunch of functionality additions. +I modify a lot on your project, the code is kinda... mess but i'll try to update it sometimes. I wrote the code from the beginning by reprogramming the editor you already did. -## Additions - -Things to add: - -- find/replace text implementation -- when you exit, you must NOT lose un-saved changes -- add a new window, scrollable, with some editor help documentation in it. -- implement Word Wrap (yeah, look it up) -- add a font panel, so you can change the font you edit in. - - make sure not only new files are in th enew font, but existing windows too. -- add a way to print the current file. -- add a way to set the page in portrait or landscape mode -- Make your About Dialog snazzy! +I added a lot of features, here a complete list of them : \ No newline at end of file From 53cdcbcb55bf53eae7eed969c4be3ea32e4e7d47 Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Thu, 21 Nov 2024 09:08:20 +0100 Subject: [PATCH 24/28] Update ReadMe.md --- ReadMe.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ReadMe.md b/ReadMe.md index 65feb2f..1cbda50 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -2,4 +2,7 @@ I modify a lot on your project, the code is kinda... mess but i'll try to update it sometimes. I wrote the code from the beginning by reprogramming the editor you already did. -I added a lot of features, here a complete list of them : \ No newline at end of file +I added a lot of features, here a complete list of them : + +- New +- From e1d0f4b1753207ade02b680e420f2a17fe722946 Mon Sep 17 00:00:00 2001 From: azert Date: Fri, 22 Nov 2024 19:52:20 +0100 Subject: [PATCH 25/28] Finishing Selection Features --- src/CustomMenuBar.java | 18 +++++++++++------- src/ExmlEditor.java | 29 ++++++++++++++++++----------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index 3a69339..672964a 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -14,8 +14,9 @@ public CustomMenuBar(ExmlEditor parent) { Menu editmenu = new Menu("Edit"); MenuComponent[] editmenuitems = {new MenuItem("Undo", new MenuShortcut(KeyEvent.VK_Z, false)), new MenuItem("Redo", new MenuShortcut(KeyEvent.VK_Y, false)), new MenuSpace(),new MenuItem("Copy", new MenuShortcut(KeyEvent.VK_C, false)), new MenuItem("Cut", new MenuShortcut(KeyEvent.VK_X, false)), new MenuItem("Paste", new MenuShortcut(KeyEvent.VK_V, false)), new MenuItem("Delete", new MenuShortcut(KeyEvent.VK_DELETE, false)), new MenuSpace(), new MenuItem("Insert here"), new MenuItem("Insert values"), new MenuSpace(), new MenuItem("Replace", new MenuShortcut(KeyEvent.VK_R, false))}; Menu selectionmenu = new Menu("Selection"); - MenuComponent[] selectionmenuitems = {new MenuItem("Select All", new MenuShortcut(KeyEvent.VK_A, false)), new MenuSpace(), new MenuItem("Copy line up"), new MenuItem("Copy line down"), new MenuItem("Move line up"), new MenuItem("Move line down"), new MenuItem("Duplicate line")}; + MenuComponent[] selectionmenuitems = {new MenuItem("Select All", new MenuShortcut(KeyEvent.VK_A, false)), new MenuSpace(), new MenuItem("Copy line up"), new MenuItem("Copy line down"), new MenuItem("Duplicate line")}; Menu appearancemenu = new Menu("Appearance"); + MenuComponent[] apperancemenuitems = {new MenuItem("Select All", new MenuShortcut(KeyEvent.VK_A, false)), new MenuSpace(), new MenuItem("Copy line up"), new MenuItem("Copy line down"), new MenuItem("Duplicate line")}; Menu securitymenu = new Menu("Security"); Menu toolsmenu = new Menu("Tools"); Menu helpmenu = new Menu("Help"); @@ -46,6 +47,15 @@ public CustomMenuBar(ExmlEditor parent) { } } + for (MenuComponent menuComponent : apperancemenuitems) { + if (menuComponent instanceof MenuSpace) { + appearancemenu.addSeparator(); + } else if (menuComponent instanceof MenuItem) { + appearancemenu.add((MenuItem) menuComponent); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); + } + } + this.add(filemenu); this.add(editmenu); this.add(selectionmenu); @@ -183,12 +193,6 @@ public void actionPerformed(ActionEvent e) { case "Copy line down": parent.CopyLineDown(); break; - case "Move line up": - parent.MoveLineUp(); - break; - case "Move line down": - parent.MoveLineDown(); - break; case "Duplicate line": parent.DuplicateLine(); break; diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index d3a6bd0..e474198 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -311,23 +311,30 @@ public void SelectAll() { } public void CopyLineUp() { - System.out.println(textArea.getCaretPosition()); + int start = textArea.getText().lastIndexOf("\n", textArea.getCaretPosition() - 1) + 1; + int end = textArea.getText().indexOf("\n", textArea.getCaretPosition()); + if (end == -1) end = textArea.getText().length(); + if (!textArea.getText().substring(start, end).isEmpty()) { + textArea.insert(textArea.getText().substring(start, end) + "\n", start); + } } public void CopyLineDown() { - - } - - public void MoveLineUp() { - - } - - public void MoveLineDown() { - + int start = textArea.getText().lastIndexOf("\n", textArea.getCaretPosition() - 1) + 1; + int end = textArea.getText().indexOf("\n", textArea.getCaretPosition()); + if (end == -1) end = textArea.getText().length(); + if (!textArea.getText().substring(start, end).isEmpty()) { + textArea.insert("\n" + textArea.getText().substring(start, end), end); + } } public void DuplicateLine() { - + int start = textArea.getText().lastIndexOf("\n", textArea.getCaretPosition() - 1) + 1; + int end = textArea.getText().indexOf("\n", textArea.getCaretPosition()); + if (end == -1) end = textArea.getText().length(); + if (!textArea.getText().substring(start, end).isEmpty()) { + textArea.insert("\n" + textArea.getText().substring(start, end), end); + } } public static void main(String[] args) { From eb38017fb1e36b335811069bc2f0222f9bddd4ae Mon Sep 17 00:00:00 2001 From: E-xml <155318575+E-xml@users.noreply.github.com> Date: Sun, 24 Nov 2024 16:01:32 +0100 Subject: [PATCH 26/28] Update CustomMenuBar.java --- src/CustomMenuBar.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index 672964a..5023ee2 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -2,9 +2,7 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; +import java.awt.event.*; import java.util.Objects; public class CustomMenuBar extends MenuBar { From 1f6cfca55551009e84f44f15de94da10d334ab13 Mon Sep 17 00:00:00 2001 From: azert Date: Sun, 24 Nov 2024 21:06:33 +0100 Subject: [PATCH 27/28] Finishing. --- out/production/TextEditor-Java/ReadMe.md | 19 +- src/CustomMenuBar.java | 464 ++++++++++++++++------- src/ExmlEditor.java | 14 + 3 files changed, 356 insertions(+), 141 deletions(-) diff --git a/out/production/TextEditor-Java/ReadMe.md b/out/production/TextEditor-Java/ReadMe.md index bc1727c..1cbda50 100644 --- a/out/production/TextEditor-Java/ReadMe.md +++ b/out/production/TextEditor-Java/ReadMe.md @@ -1,17 +1,8 @@ -# TextEditor (java) Modifications +# Update alpha 1.0 -This is a simple text editor. Using it as a starting point, it needs a bunch of functionality additions. +I modify a lot on your project, the code is kinda... mess but i'll try to update it sometimes. I wrote the code from the beginning by reprogramming the editor you already did. -## Additions +I added a lot of features, here a complete list of them : -Things to add: - -- find/replace text implementation -- when you exit, you must NOT lose un-saved changes -- add a new window, scrollable, with some editor help documentation in it. -- implement Word Wrap (yeah, look it up) -- add a font panel, so you can change the font you edit in. - - make sure not only new files are in th enew font, but existing windows too. -- add a way to print the current file. -- add a way to set the page in portrait or landscape mode -- Make your About Dialog snazzy! +- New +- diff --git a/src/CustomMenuBar.java b/src/CustomMenuBar.java index 672964a..ac70378 100644 --- a/src/CustomMenuBar.java +++ b/src/CustomMenuBar.java @@ -2,9 +2,7 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; +import java.awt.event.*; import java.util.Objects; public class CustomMenuBar extends MenuBar { @@ -16,16 +14,15 @@ public CustomMenuBar(ExmlEditor parent) { Menu selectionmenu = new Menu("Selection"); MenuComponent[] selectionmenuitems = {new MenuItem("Select All", new MenuShortcut(KeyEvent.VK_A, false)), new MenuSpace(), new MenuItem("Copy line up"), new MenuItem("Copy line down"), new MenuItem("Duplicate line")}; Menu appearancemenu = new Menu("Appearance"); - MenuComponent[] apperancemenuitems = {new MenuItem("Select All", new MenuShortcut(KeyEvent.VK_A, false)), new MenuSpace(), new MenuItem("Copy line up"), new MenuItem("Copy line down"), new MenuItem("Duplicate line")}; - Menu securitymenu = new Menu("Security"); - Menu toolsmenu = new Menu("Tools"); + MenuComponent[] appearancemenuitems = {new MenuItem("Background color"), new MenuItem("Font color"), new CheckboxMenuItem("Title bar", true)}; Menu helpmenu = new Menu("Help"); + MenuComponent[] helpmenuitems = {new MenuItem("About creator"), new MenuItem("About the project"), new MenuItem("Report issues")}; for (MenuComponent menuComponent : filemenuitems) { if (menuComponent instanceof MenuSpace) { filemenu.addSeparator(); } else if (menuComponent instanceof MenuItem) { filemenu.add((MenuItem) menuComponent); - ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); } } @@ -34,7 +31,7 @@ public CustomMenuBar(ExmlEditor parent) { editmenu.addSeparator(); } else if (menuComponent instanceof MenuItem) { editmenu.add((MenuItem) menuComponent); - ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); } } @@ -43,25 +40,31 @@ public CustomMenuBar(ExmlEditor parent) { selectionmenu.addSeparator(); } else if (menuComponent instanceof MenuItem) { selectionmenu.add((MenuItem) menuComponent); - ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); } } - for (MenuComponent menuComponent : apperancemenuitems) { + for (MenuComponent menuComponent : appearancemenuitems) { if (menuComponent instanceof MenuSpace) { appearancemenu.addSeparator(); + } else if (menuComponent instanceof CheckboxMenuItem) { + appearancemenu.add((CheckboxMenuItem) menuComponent); + ((CheckboxMenuItem) menuComponent).addActionListener(new CustomActionListener(((CheckboxMenuItem) menuComponent), parent)); } else if (menuComponent instanceof MenuItem) { appearancemenu.add((MenuItem) menuComponent); - ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); } } + for (MenuComponent menuComponent : helpmenuitems) { + helpmenu.add((MenuItem) menuComponent); + ((MenuItem) menuComponent).addActionListener(new CustomActionListener(((MenuItem) menuComponent), parent)); + } + this.add(filemenu); this.add(editmenu); this.add(selectionmenu); this.add(appearancemenu); - this.add(securitymenu); - this.add(toolsmenu); this.add(helpmenu); } } @@ -70,133 +73,340 @@ class MenuSpace extends MenuComponent { public MenuSpace() {} } -class CustomActionListener implements ActionListener { +class CustomActionListener implements ActionListener { ExmlEditor parent = null; - MenuItem menuItem = null; + T menuItem = null; - public CustomActionListener(MenuItem menuItem, ExmlEditor parent) { + public CustomActionListener(T menuItem, ExmlEditor parent) { this.menuItem = menuItem; this.parent = parent; } public void actionPerformed(ActionEvent e) { - switch (menuItem.getLabel()) { - case "New": - if (!parent.isFileSaved()) { - switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { - case JOptionPane.YES_OPTION: parent.SaveAs(); parent.textArea.setText(""); parent.OpenedFile = null; parent.TextContent = ""; break; - case JOptionPane.NO_OPTION: parent.textArea.setText(""); parent.OpenedFile = null; parent.TextContent = ""; break; - case JOptionPane.CANCEL_OPTION: break; + if (menuItem instanceof CheckboxMenuItem) { + switch (((CheckboxMenuItem) menuItem).getLabel()) { + case "Title bar": + parent.showTitleBar(((CheckboxMenuItem) menuItem).getState()); + } + } else if (menuItem instanceof MenuItem) { + switch (((MenuItem) menuItem).getLabel()) { + case "New": + if (!parent.isFileSaved()) { + switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { + case JOptionPane.YES_OPTION: + parent.SaveAs(); + parent.textArea.setText(""); + parent.OpenedFile = null; + parent.TextContent = ""; + break; + case JOptionPane.NO_OPTION: + parent.textArea.setText(""); + parent.OpenedFile = null; + parent.TextContent = ""; + break; + case JOptionPane.CANCEL_OPTION: + break; + } + } else { + parent.textArea.setText(""); } - } else { - parent.textArea.setText(""); - } - parent.setTitle("Notepad - Untitled"); - parent.TextContent = ""; - break; - case "New window": new ExmlEditor(); break; - case "Open": - if (!parent.isFileSaved()) { - switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { - case JOptionPane.YES_OPTION: parent.SaveAs(); parent.Open(); break; - case JOptionPane.NO_OPTION: parent.textArea.setText(""); parent.OpenedFile = null; parent.TextContent = ""; parent.Open(); break; - case JOptionPane.CANCEL_OPTION: break; + parent.setTitle("Notepad - Untitled"); + parent.TextContent = ""; + break; + case "New window": + new ExmlEditor(); + break; + case "Open": + if (!parent.isFileSaved()) { + switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { + case JOptionPane.YES_OPTION: + parent.SaveAs(); + parent.Open(); + break; + case JOptionPane.NO_OPTION: + parent.textArea.setText(""); + parent.OpenedFile = null; + parent.TextContent = ""; + parent.Open(); + break; + case JOptionPane.CANCEL_OPTION: + break; + } + } else { + parent.Open(); } - } else { - parent.Open(); - } - break; - case "Save": - if (parent.OpenedFile == null) { - parent.SaveAs(); - } else { - parent.Save(); - } - break; - case "Save as": parent.SaveAs(); break; - case "Print": - if (parent.OpenedFile == null) { + break; + case "Save": + if (parent.OpenedFile == null) { + parent.SaveAs(); + } else { + parent.Save(); + } + break; + case "Save as": parent.SaveAs(); - } else { - parent.Save(); - } - parent.print(); - break; - case "Close window": - if (!parent.isFileSaved()) { - switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { - case JOptionPane.YES_OPTION: parent.SaveAs(); break; - case JOptionPane.NO_OPTION: parent.dispose(); break; - case JOptionPane.CANCEL_OPTION: break; + break; + case "Print": + if (parent.OpenedFile == null) { + parent.SaveAs(); + } else { + parent.Save(); } - } else { - parent.dispose(); - } - break; - case "Exit": - if (!parent.isFileSaved()) { - switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { - case JOptionPane.YES_OPTION: parent.SaveAs(); System.exit(0); break; - case JOptionPane.NO_OPTION: System.exit(0); break; - case JOptionPane.CANCEL_OPTION: break; + parent.print(); + break; + case "Close window": + if (!parent.isFileSaved()) { + switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { + case JOptionPane.YES_OPTION: + parent.SaveAs(); + break; + case JOptionPane.NO_OPTION: + parent.dispose(); + break; + case JOptionPane.CANCEL_OPTION: + break; + } + } else { + parent.dispose(); + } + break; + case "Exit": + if (!parent.isFileSaved()) { + switch (JOptionPane.showOptionDialog(parent, "The current file isn't saved, would save it ?", "Save ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null)) { + case JOptionPane.YES_OPTION: + parent.SaveAs(); + System.exit(0); + break; + case JOptionPane.NO_OPTION: + System.exit(0); + break; + case JOptionPane.CANCEL_OPTION: + break; + } + } else { + System.exit(0); } - } else { - System.exit(0); - } - - case "Undo": parent.Undo(); break; - case "Redo": parent.Redo(); break; - case "Copy" : parent.Copy(); break; - case "Cut" : parent.Cut(); break; - case "Paste" : parent.Paste(); break; - case "Delete" : parent.Delete(); break; - case "Insert here" : - String res = JOptionPane.showInputDialog(parent, "What would you insert ?"); - if (res == null) { - break; - } else { - parent.Insert(res); - } - break; - case "Insert values" : - JPanel p = new JPanel(); - p.setLayout(new GridLayout(1, 1)); - String[] options = {"File name", "File size", "File path", "Epoch", "Time stamp", "Year", "Month", "Week", "Day", "Day of the week", "Hour", "Minute", "Second", "Random char", "Username", "Ipv4", "Ipv6"}; - JComboBox comboBox = new JComboBox<>(options); - p.add(comboBox); - - if (JOptionPane.showConfirmDialog(null, p, "Select a value to insert", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { - parent.Insert(parent.getKey((String) Objects.requireNonNull(comboBox.getSelectedItem()))); - } - - - break; - - case "Replace": - JPanel p2 = new JPanel(); - p2.setLayout(new GridLayout(2, 1, 20, 40)); - JTextField oldSequence = new JTextField(); - p2.add(oldSequence); - JTextField newSequence = new JTextField(); - p2.add(newSequence); - if(JOptionPane.showConfirmDialog(null, p2, "Replace", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { - parent.Replace(oldSequence.getText(), newSequence.getText()); - } - break; - case "Select All": - parent.SelectAll(); - break; - case "Copy line up": - parent.CopyLineUp(); - break; - case "Copy line down": - parent.CopyLineDown(); - break; - case "Duplicate line": - parent.DuplicateLine(); - break; - default: throw new IllegalArgumentException("You might have forget to update this block"); + + case "Undo": + parent.Undo(); + break; + case "Redo": + parent.Redo(); + break; + case "Copy": + parent.Copy(); + break; + case "Cut": + parent.Cut(); + break; + case "Paste": + parent.Paste(); + break; + case "Delete": + parent.Delete(); + break; + case "Insert here": + String res = JOptionPane.showInputDialog(parent, "What would you insert ?"); + if (res == null) { + break; + } else { + parent.Insert(res); + } + break; + case "Insert values": + JPanel p = new JPanel(); + p.setLayout(new GridLayout(1, 1)); + String[] options = {"File name", "File size", "File path", "Epoch", "Time stamp", "Year", "Month", "Week", "Day", "Day of the week", "Hour", "Minute", "Second", "Random char", "Username", "Ipv4", "Ipv6"}; + JComboBox comboBox = new JComboBox<>(options); + p.add(comboBox); + + if (JOptionPane.showConfirmDialog(null, p, "Select a value to insert", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { + parent.Insert(parent.getKey((String) Objects.requireNonNull(comboBox.getSelectedItem()))); + } + + + break; + + case "Replace": + JPanel p2 = new JPanel(); + p2.setLayout(new GridLayout(2, 1, 20, 40)); + JTextField oldSequence = new JTextField(); + p2.add(oldSequence); + JTextField newSequence = new JTextField(); + p2.add(newSequence); + if (JOptionPane.showConfirmDialog(null, p2, "Replace", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { + parent.Replace(oldSequence.getText(), newSequence.getText()); + } + break; + case "Select All": + parent.SelectAll(); + break; + case "Copy line up": + parent.CopyLineUp(); + break; + case "Copy line down": + parent.CopyLineDown(); + break; + case "Duplicate line": + parent.DuplicateLine(); + break; + case "Background color": + parent.BGColor(); + break; + case "Font color": + parent.FGColor(); + break; + case "About creator": + Frame aboutcframe = new Frame(); + aboutcframe.setSize(450, 225); + aboutcframe.setResizable(false); + aboutcframe.setLocationRelativeTo(parent); + aboutcframe.setLayout(new GridLayout(4, 1)); + aboutcframe.add(new Label("Created by ZCW-Java91. (https://www.github.com/ZCW-Java91)")); + aboutcframe.add(new Label("Forked by E-xml. (https://www.github.com/E-xml)")); + aboutcframe.add(new Label("Mail : azertypatata8@gmail.com")); + aboutcframe.add(new Label("Youtube : https://www.youtube.com/@TheCpuguy239")); + aboutcframe.setTitle("About creator"); + aboutcframe.addWindowListener(new WindowListener() { + @Override + public void windowOpened(WindowEvent e) { + + } + + @Override + public void windowClosing(WindowEvent e) { + aboutcframe.dispose(); + } + + @Override + public void windowClosed(WindowEvent e) { + + } + + @Override + public void windowIconified(WindowEvent e) { + + } + + @Override + public void windowDeiconified(WindowEvent e) { + + } + + @Override + public void windowActivated(WindowEvent e) { + + } + + @Override + public void windowDeactivated(WindowEvent e) { + + } + }); + aboutcframe.setVisible(true); + break; + case "About the project": + Frame aboutpframe = new Frame(); + aboutpframe.setSize(450, 225); + aboutpframe.setResizable(false); + aboutpframe.setLocationRelativeTo(parent); + aboutpframe.setLayout(new GridLayout(4, 1)); + aboutpframe.add(new Label("Project by ZCW-Java91. (https://www.github.com/ZCW-Java91/TextEditor-Java)")); + aboutpframe.add(new Label("Remake by E-xml (https://www.github.com/E-xml/TextEditor-Java)")); + aboutpframe.add(new Label("Notepad V1.0.0")); + aboutpframe.add(new Label("JDK 21 needed for work on this code")); + aboutpframe.setTitle("About the project"); + aboutpframe.addWindowListener(new WindowListener() { + @Override + public void windowOpened(WindowEvent e) { + + } + + @Override + public void windowClosing(WindowEvent e) { + aboutpframe.dispose(); + } + + @Override + public void windowClosed(WindowEvent e) { + + } + + @Override + public void windowIconified(WindowEvent e) { + + } + + @Override + public void windowDeiconified(WindowEvent e) { + + } + + @Override + public void windowActivated(WindowEvent e) { + + } + + @Override + public void windowDeactivated(WindowEvent e) { + + } + }); + aboutpframe.setVisible(true); + break; + case "Report issues": + Frame report = new Frame(); + report.setSize(450, 225); + report.setResizable(false); + report.setLocationRelativeTo(parent); + report.setLayout(new GridLayout(4, 1)); + report.add(new Label("Issues detected ?")); + report.add(new Label("Original project : https://github.com/ZCW-Java91/TextEditor-Java/issues")); + report.add(new Label("Remake project : https://github.com/E-xml/TextEditor-Java/issues")); + report.setTitle("About the project"); + report.addWindowListener(new WindowListener() { + @Override + public void windowOpened(WindowEvent e) { + + } + + @Override + public void windowClosing(WindowEvent e) { + report.dispose(); + } + + @Override + public void windowClosed(WindowEvent e) { + + } + + @Override + public void windowIconified(WindowEvent e) { + + } + + @Override + public void windowDeiconified(WindowEvent e) { + + } + + @Override + public void windowActivated(WindowEvent e) { + + } + + @Override + public void windowDeactivated(WindowEvent e) { + + } + }); + report.setVisible(true); + break; + + default: + throw new IllegalArgumentException("You might have forget to update this block"); + } } } } \ No newline at end of file diff --git a/src/ExmlEditor.java b/src/ExmlEditor.java index e474198..0975854 100644 --- a/src/ExmlEditor.java +++ b/src/ExmlEditor.java @@ -1,5 +1,6 @@ package src; +import javax.swing.*; import java.awt.*; import java.awt.datatransfer.*; import java.awt.font.*; @@ -18,6 +19,7 @@ public class ExmlEditor extends Frame { public Stack redoStack = new Stack<>(); public String[] days = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; public String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; + public boolean wordWrap = false; public ExmlEditor() { this.setTitle("Notepad - Untitled"); @@ -337,6 +339,18 @@ public void DuplicateLine() { } } + public void showTitleBar(boolean state) { + this.setUndecorated(!state); + } + + public void BGColor() { + textArea.setBackground(JColorChooser.showDialog(this, "Select a color", textArea.getBackground())); + } + + public void FGColor() { + textArea.setForeground(JColorChooser.showDialog(this, "Select a color", textArea.getForeground())); + } + public static void main(String[] args) { new ExmlEditor(); } From 4623579673e79831fe4a7f9c299f8d53aadf5c6b Mon Sep 17 00:00:00 2001 From: azert Date: Sun, 24 Nov 2024 21:11:31 +0100 Subject: [PATCH 28/28] Finishing. --- ReadMe.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 1cbda50..7295e86 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,8 +1,7 @@ -# Update alpha 1.0 +# Update V1.0.0 -I modify a lot on your project, the code is kinda... mess but i'll try to update it sometimes. I wrote the code from the beginning by reprogramming the editor you already did. +Updating [ZCW-Java91 project](https://github.com/ZCW-Java91/TextEditor-Java) -I added a lot of features, here a complete list of them : +V1.0.0 -- New -- +My very first notepad in java \ No newline at end of file