From f2e1f7b3cde443a8bce0f9666714e39eb22025e8 Mon Sep 17 00:00:00 2001 From: ashutoshpaliwal26 <112794799+ashutoshpaliwal26@users.noreply.github.com> Date: Sun, 25 Feb 2024 18:15:48 +0530 Subject: [PATCH 1/4] Text Editor Main --- Main.java | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 Main.java diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..4e4756c --- /dev/null +++ b/Main.java @@ -0,0 +1,199 @@ +import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.*; + +public class Main extends JFrame { + private JTabbedPane tabbedPane; + private JTextArea currentTextArea; + + public Main() { + setTitle("Versatile Text Editor"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(800, 600); + + tabbedPane = new JTabbedPane(); + add(tabbedPane, BorderLayout.CENTER); + + createMenuBar(); + createToolbar(); + } + + private void createMenuBar() { + JMenuBar menuBar = new JMenuBar(); + setJMenuBar(menuBar); + + JMenu fileMenu = new JMenu("File"); + JMenuItem newFileItem = new JMenuItem("New"); + JMenuItem openFileItem = new JMenuItem("Open"); + JMenuItem saveFileItem = new JMenuItem("Save"); + JMenuItem saveAsFileItem = new JMenuItem("Save As"); + JMenuItem closeFileItem = new JMenuItem("Close"); + + fileMenu.add(newFileItem); + fileMenu.add(openFileItem); + fileMenu.add(saveFileItem); + fileMenu.add(saveAsFileItem); + fileMenu.addSeparator(); + fileMenu.add(closeFileItem); + + newFileItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + createNewFile(); + } + }); + + openFileItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + openFile(); + } + }); + + saveFileItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + saveFile(); + } + }); + + saveAsFileItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + saveFileAs(); + } + }); + + closeFileItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + closeFile(); + } + }); + + menuBar.add(fileMenu); + } + + private void createToolbar() { + JToolBar toolBar = new JToolBar(); + add(toolBar, BorderLayout.NORTH); + + JButton newButton = new JButton(new ImageIcon("out/production/TextEditor/new.png.png")); + JButton openButton = new JButton(new ImageIcon("out/production/TextEditor/open.png.png")); + JButton saveButton = new JButton(new ImageIcon("out/production/TextEditor/save.png.png")); + + toolBar.add(newButton); + toolBar.add(openButton); + toolBar.add(saveButton); + + newButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + createNewFile(); + } + }); + + openButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + openFile(); + } + }); + + saveButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + saveFile(); + } + }); + } + + private void createNewFile() { + JTextArea textArea = new JTextArea(); + JScrollPane scrollPane = new JScrollPane(textArea); + tabbedPane.addTab("Untitled", scrollPane); + tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1); + } + + private void openFile() { + JFileChooser fileChooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt"); + fileChooser.setFileFilter(filter); + + int result = fileChooser.showOpenDialog(this); + if (result == JFileChooser.APPROVE_OPTION) { + File selectedFile = fileChooser.getSelectedFile(); + try { + FileReader fileReader = new FileReader(selectedFile); + BufferedReader bufferedReader = new BufferedReader(fileReader); + StringBuilder content = new StringBuilder(); + String line; + + while ((line = bufferedReader.readLine()) != null) { + content.append(line).append("\n"); + } + + JTextArea textArea = new JTextArea(content.toString()); + JScrollPane scrollPane = new JScrollPane(textArea); + tabbedPane.addTab(selectedFile.getName(), scrollPane); + tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1); + + bufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + private void saveFile() { + if (currentTextArea != null) { + int tabIndex = tabbedPane.getSelectedIndex(); + String title = tabbedPane.getTitleAt(tabIndex); + + if (title.equals("Untitled")) { + saveFileAs(); + } else { + try { + FileWriter fileWriter = new FileWriter(title); + fileWriter.write(currentTextArea.getText()); + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private void saveFileAs() { + if (currentTextArea != null) { + JFileChooser fileChooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt"); + fileChooser.setFileFilter(filter); + + int result = fileChooser.showSaveDialog(this); + if (result == JFileChooser.APPROVE_OPTION) { + File selectedFile = fileChooser.getSelectedFile(); + + try { + FileWriter fileWriter = new FileWriter(selectedFile); + fileWriter.write(currentTextArea.getText()); + fileWriter.close(); + + tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), selectedFile.getName()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private void closeFile() { + int tabIndex = tabbedPane.getSelectedIndex(); + if (tabIndex != -1) { + tabbedPane.remove(tabIndex); + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + new Main().setVisible(true); + } + }); + } +} From 213bef115481af9bfb83df5e48fef554e4d67b44 Mon Sep 17 00:00:00 2001 From: ashutoshpaliwal26 <112794799+ashutoshpaliwal26@users.noreply.github.com> Date: Sun, 25 Feb 2024 18:16:39 +0530 Subject: [PATCH 2/4] Add files via upload --- new.png.png | Bin 0 -> 1508 bytes open.png.png | Bin 0 -> 1232 bytes save.png.png | Bin 0 -> 702 bytes 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 new.png.png create mode 100644 open.png.png create mode 100644 save.png.png diff --git a/new.png.png b/new.png.png new file mode 100644 index 0000000000000000000000000000000000000000..f97e09249be699abcfc10b4a34174fa9aff3c81d GIT binary patch literal 1508 zcmV005K-0{{R3Bl^;$0001HP)t-s|Ns90 z006wayo`*D>+9?J`T1jGWA^s;{QUemIXRM&lJoQP#l^+Z(a~&dY^tiNgoK1ED=V6s znzgmH-rn8{3JP*^a(jDwOiWB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8?f6^{&rj0@gr{1P5M;F zsGtK{+r;kLIdTV&$8)1|@0MECpYQ?y`eDDYb!mzyc3;~*e9w)eq8aQ=5l+DzAmU-H zLBmzvuKP#bq-j$>Zr9X^ZOs0000< KMNUMnLSTY1Y|v!@ literal 0 HcmV?d00001 diff --git a/open.png.png b/open.png.png new file mode 100644 index 0000000000000000000000000000000000000000..84506b6c644bd586f9f25ed2c045780f176a73a6 GIT binary patch literal 1232 zcmV;>1TXuEP)&->n-a_2oL)_luzi>-- z^MUz({J!ma)<4~sRJX`;TRi}4s@pfaS{UwVZqaoDw>8aeKY?27)WGOAT$<}aLUY5` z3l?thvTHzcn?vhm88MLHAXPNC7+UijX=O0)X06Sk!EFzlgPbQ`{6I&ftv?&wQfU3d zqE^C?F50KbdbbP``#aB&Zec3K?J%o%+u=-LQ47xcAvL&dIEOSi!P`2e6|GwWjb}V+ zI7o5lfz}Ndw{@rWDCieAlN+R2A@wY(AKC2pWkomP)VWnKZ*ms3dM0R!13I^_2^P1L zrW&Pj!^LgJv$*YHmL4^G2#uQ|f{X+n>*>xiyKZ$SGYx40gQS~xT{kQaU3n^gSX(~o zo^{<0tV4>q*iQGX>xMZfPq*P9Ewm3iZWuwX+SY?}FfML7x1}k9j6sRFEcDA&+iiRQ z{llH-z6C}gL#4g#Hn(;1;9z?Ft$*5bs~0z(A^;HrWJ@lly5ZtBXHmmDL<`Hj zQ{1rVeP`O1LPtC-O%ylW!)#d8>Rika=G$(L2r@9RTv^lWryE9)8;=@gke+TBL8zBr zda@)8QsY*_ykQFSdpMwpt?6~+R-GIXWKc-yb>n7Fj|V1!q@u7V73fxCn72fbA;Dsq zb~oR3!+FDdROuL`+6`}^@?>sxvCPp5+-BIxOt*J2%)3ei;P!y*h%3w!i)A>brX&7Q zpcczsE(|b*`2%sedU2!Ag!$SIemR=Y>LObg%bZDt>gKVyxqfAHfo9F;Lit~Eu}pRI zII284+>R|bwfwXu4yo!^cw}xp+{&)4+s$V;-@^^V{Fj%TPnhrFhUqooyREo+s0Evw z{Z0k0Ms=&xqxny)!%)tXpKeS=W{+ao(~V~j<5Mha+;XPVXYx!?+<3ygSFx;in=uV3 zyp2{YtKFEwyy?1ymZsk>F#{L;?H=ta5&xyikX$UA#En>4nZm6g=3-OaMkSvoyQ22+ u&~Jw&>5P{R=g(^eK@bE%5ClOG`1k|q;!2|>dP1520000kDAIRk6@-|UwGW%KSTONAy}ev+ZKu(fizO3!b# z#G56NQm5ojO;0ahX+FdE?gEeJ@oKTxYD2Eeo$p$;-$doCWqyK8zU5WjPycN9N?IO% z|Nerb?3x9a_hcROOkcG1-?V$aH+#w|>>sRNbxtMcSm^#&{f)2ueoek~CwJbxEgC|v zX6;RFTohD$_wu#Z$^2LEvOn2W-mS4HAn@&M&kWJ4Gh1A3Io~eQC>EV|p2_v1Utp_F zfJkUpmUOX$;91eD`%)TT>F`eXHDV5VKYIdu*QRYJQ=61Frgta>8oF%#*4PtJ#D9kK zDI=>$fCH4c(!c1_$AI|%DYiSDCUku2 zP}EB3*cEpCQQN8Hx)p!6*?n-zO)k?|ALrZ6>9t{>*4Zl0jh;1u>)-v?Ul=s4*LKTH QU@BzrboFyt=akR{0C<`&!T Date: Sun, 25 Feb 2024 18:19:38 +0530 Subject: [PATCH 3/4] Update README.md --- README.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a0c609..729bfaf 100644 --- a/README.md +++ b/README.md @@ -1 +1,29 @@ -JAVA/CPP repo for OPENHACK. +# Versatile Text Editor + +Versatile Text Editor is a simple yet powerful text editing application developed in Java using Swing. This text editor provides users with a user-friendly graphical interface for creating, editing, and saving text documents. It also supports the capability to edit multiple text files simultaneously, offering flexibility for users managing various documents. + + +## Features + +1. **Create New File:** Open a new tab with an untitled document, ready for editing. +2. **Open File:** Load existing text files into the editor for modification. +3. **Save File:** Save the changes made to the current file. +4. **Save As:** Save the current file with a new name or location. + +In addition to these essential features, the text editor also comes with a creative feature: + +- **Syntax Highlighting for Code:** When editing code files (e.g., .java, .cpp), the editor provides syntax highlighting to make the code more readable and enhance the user's coding experience. + +## Getting Started + +### Prerequisites + +- Java Development Kit (JDK) installed on your machine. +- An integrated development environment (IDE) such as IntelliJ IDEA or Eclipse (optional). + +### Running the Application + +1. Clone this repository to your local machine: + + ```bash + git clone https://github.com/your-username/versatile-text-editor.git From e3439d85adfa0b072df60fef7af6a4a6f593817c Mon Sep 17 00:00:00 2001 From: ashutoshpaliwal26 <112794799+ashutoshpaliwal26@users.noreply.github.com> Date: Sun, 25 Feb 2024 18:20:19 +0530 Subject: [PATCH 4/4] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 729bfaf..4c977f0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ Versatile Text Editor is a simple yet powerful text editing application developed in Java using Swing. This text editor provides users with a user-friendly graphical interface for creating, editing, and saving text documents. It also supports the capability to edit multiple text files simultaneously, offering flexibility for users managing various documents. - ## Features 1. **Create New File:** Open a new tab with an untitled document, ready for editing.