1
+ package src .com .mateo .notepad ;
2
+
3
+ import java .awt .Color ;
4
+ import java .awt .Dimension ;
5
+ import java .awt .Event ;
6
+ import java .awt .Font ;
7
+ import java .awt .Toolkit ;
8
+ import java .awt .event .ActionEvent ;
9
+ import java .awt .event .ActionListener ;
10
+ import java .awt .event .KeyEvent ;
11
+ import java .io .BufferedWriter ;
12
+ import java .io .File ;
13
+ import java .io .FileNotFoundException ;
14
+ import java .io .FileReader ;
15
+ import java .io .FileWriter ;
16
+ import java .util .Scanner ;
17
+ import javax .swing .JEditorPane ;
18
+ import javax .swing .JFileChooser ;
19
+ import javax .swing .JFrame ;
20
+ import javax .swing .JMenu ;
21
+ import javax .swing .JMenuBar ;
22
+ import javax .swing .JMenuItem ;
23
+ import javax .swing .JPanel ;
24
+ import javax .swing .KeyStroke ;
25
+ import javax .swing .text .BadLocationException ;
26
+ import javax .swing .ImageIcon ;
27
+ import java .awt .event .KeyAdapter ;
28
+ import javax .swing .JLabel ;
29
+
30
+ @ SuppressWarnings ("serial" )
31
+ public class NotePad extends JFrame {
32
+
33
+ private JMenuBar menuBar ;
34
+ private JLabel WordCount ;
35
+ private JEditorPane Pane ;
36
+ private JFileChooser fc ;
37
+ private Scanner sc ;
38
+
39
+ private String FONT = "Comic Sans MS" ;
40
+ private int SIZE = 12 ;
41
+
42
+ static String [] WORDS = new String [10 ];
43
+ static int COUNTER = 0 ;
44
+
45
+ static int WC ; // Word Count
46
+
47
+ static String INITIALFILEPATH = "C:\\ Users\\ 1ceballom\\ Desktop\\ " ;
48
+ static String FILEPATH = INITIALFILEPATH ;
49
+
50
+ static String TMP ;
51
+
52
+ // Constructor
53
+ public NotePad () {
54
+ this .setIconImage (Toolkit .getDefaultToolkit ().getImage ("icons/main.PNG" ));
55
+
56
+ this .setTitle ("Mateo's Notepad" );
57
+ this .setSize (500 , 400 );
58
+ // menu generate method
59
+ generateMenu ();
60
+ this .setJMenuBar (menuBar );
61
+
62
+ // pane with null layout
63
+ JPanel contentPane = new JPanel (null );
64
+ contentPane .setPreferredSize (new Dimension (500 , 400 ));
65
+ contentPane .setBackground (new Color (192 , 192 , 192 ));
66
+
67
+ // adding panel to JFrame and seting of window position and close
68
+ // operation
69
+ getContentPane ().add (contentPane );
70
+
71
+ Pane = new JEditorPane ();
72
+ Pane .addKeyListener (new KeyAdapter () {
73
+ @ Override
74
+ public void keyPressed (KeyEvent arg0 ) {
75
+ // TODO
76
+ WORDS = Pane .getText ().split (" " ); // split the elements in the
77
+ // array by spaces
78
+ if (Pane .getText ().equals ("" )) {
79
+ WC = 0 ; // 0 word count when no words
80
+ } else {
81
+ WC = WORDS .length ; // as many words as the length of the //
82
+ // array
83
+ }
84
+ WordCount .setText ("Word Count: " + WC );
85
+ // System.out.println(WORDS.length);
86
+ }
87
+
88
+ });
89
+
90
+ Pane .setBounds (0 , 0 , 500 , 377 );
91
+ Pane .setEnabled (true );
92
+ Pane .setFont (new Font (FONT , 0 , SIZE ));
93
+ Pane .setVisible (true );
94
+
95
+ // adding components to contentPane panel
96
+ contentPane .add (Pane );
97
+
98
+ WordCount = new JLabel ("Word Count: 0" );
99
+ WordCount .setBounds (10 , 376 , 150 , 24 );
100
+ contentPane .add (WordCount );
101
+ this .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
102
+ this .setLocationRelativeTo (null );
103
+ this .pack ();
104
+ this .setVisible (true );
105
+ }
106
+
107
+ // method for generate menu
108
+ public void generateMenu () {
109
+ menuBar = new JMenuBar ();
110
+
111
+ JMenu mnFile = new JMenu ("File" );
112
+ JMenu mnEdit = new JMenu ("Edit" );
113
+
114
+ JMenuItem open = new JMenuItem ("Open " );
115
+ open .setIcon (new ImageIcon ("icons/open.png" ));
116
+ open .setAccelerator (KeyStroke .getKeyStroke (KeyEvent .VK_O , Event .CTRL_MASK ));
117
+
118
+ JMenuItem save = new JMenuItem ("Save " );
119
+ save .setIcon (new ImageIcon ("icons/save.png" ));
120
+ save .setAccelerator (KeyStroke .getKeyStroke (KeyEvent .VK_S , Event .CTRL_MASK ));
121
+
122
+ JMenuItem quit = new JMenuItem ("Quit " );
123
+ quit .setIcon (new ImageIcon ("icons/quit.png" ));
124
+ quit .setAccelerator (KeyStroke .getKeyStroke (KeyEvent .VK_Q , Event .CTRL_MASK ));
125
+
126
+ JMenuItem undo = new JMenuItem ("Undo " );
127
+ undo .setIcon (new ImageIcon ("icons/undo.png" ));
128
+ undo .setAccelerator (KeyStroke .getKeyStroke (KeyEvent .VK_Z , Event .CTRL_MASK ));
129
+
130
+ JMenuItem redo = new JMenuItem ("Redo " );
131
+ redo .setIcon (new ImageIcon ("icons/redo.png" ));
132
+ redo .setAccelerator (KeyStroke .getKeyStroke (KeyEvent .VK_Y , Event .CTRL_MASK ));
133
+
134
+ fc = new JFileChooser ();
135
+ fc .setCurrentDirectory (new File (System .getProperty ("user.home" )));
136
+ // fc.setDialogTitle("FileChooser");
137
+ fc .setFileSelectionMode (JFileChooser .FILES_AND_DIRECTORIES );
138
+
139
+ // Setings action for menu item
140
+ // Call defined method
141
+ open .addActionListener (new ActionListener () {
142
+ public void actionPerformed (ActionEvent evt ) {
143
+ open (evt );
144
+ }
145
+ });
146
+
147
+ // Setings action for menu item
148
+ // Call defined method
149
+ save .addActionListener (new ActionListener () {
150
+ public void actionPerformed (ActionEvent evt ) {
151
+ save (evt );
152
+ }
153
+ });
154
+
155
+ // Setings action for menu item
156
+ // Call defined method
157
+ quit .addActionListener (new ActionListener () {
158
+ public void actionPerformed (ActionEvent evt ) {
159
+ quit (evt );
160
+ }
161
+ });
162
+
163
+ ////////////////
164
+ undo .addActionListener (new ActionListener () {
165
+ public void actionPerformed (ActionEvent evt ) {
166
+ undo (evt );
167
+ }
168
+
169
+ });
170
+
171
+ redo .addActionListener (new ActionListener () {
172
+ public void actionPerformed (ActionEvent evt ) {
173
+ redo (evt );
174
+ }
175
+ });
176
+
177
+ mnFile .add (open );
178
+ mnFile .add (save );
179
+ mnFile .addSeparator ();
180
+ mnFile .add (quit );
181
+
182
+ mnEdit .add (undo );
183
+ mnEdit .add (redo );
184
+
185
+ menuBar .add (mnFile );
186
+ menuBar .add (mnEdit );
187
+ }
188
+
189
+ // Method for Open from menuFile
190
+ private void open (ActionEvent evt ) {
191
+ fc .setDialogTitle ("Open" );
192
+ fc .showOpenDialog (this );
193
+ openFile ();
194
+ }
195
+
196
+ private void openFile () {
197
+ try {
198
+ sc = new Scanner (new FileReader (fc .getSelectedFile ()));
199
+ } catch (FileNotFoundException e ) {
200
+ // TODO Auto-generated catch block
201
+ e .printStackTrace ();
202
+ }
203
+ String c = "" ;
204
+ while (sc .hasNext ()) {
205
+ c = sc .next ();
206
+ }
207
+ try {
208
+ Pane .getDocument ().insertString (Pane .getDocument ().getLength (), c , null );
209
+ } catch (BadLocationException e ) {
210
+ // TODO Auto-generated catch block
211
+ e .printStackTrace ();
212
+ }
213
+ sc .close ();
214
+ }
215
+
216
+ // Method for Save from menuFile
217
+ private void save (ActionEvent evt ) {
218
+ fc .setDialogTitle ("Save" );
219
+ fc .showSaveDialog (null );
220
+
221
+ FILEPATH = INITIALFILEPATH ;
222
+ if (FILEPATH != INITIALFILEPATH ) {
223
+ try (BufferedWriter MYFILE = new BufferedWriter (new FileWriter (FILEPATH ))) { // (FILEPATH,true)
224
+ // to
225
+ // append
226
+ // to
227
+ // the
228
+ // file
229
+ for (int ITEM = 0 ; ITEM < COUNTER ; ITEM ++) {
230
+ MYFILE .write (WORDS [ITEM ]);
231
+ MYFILE .newLine ();
232
+ }
233
+ MYFILE .close ();
234
+ } catch (Exception e ) {
235
+ System .out .println (e );
236
+ return ;
237
+ }
238
+ }
239
+ }
240
+
241
+ // Method for Exit from menuFile
242
+ private void quit (ActionEvent evt ) {
243
+ System .exit (0 );
244
+ }
245
+
246
+ private void undo (ActionEvent evt ) {
247
+ TMP = WORDS [WORDS .length - 1 ];
248
+ // System.out.println(TMP);
249
+ String [] TEXT = new String [WORDS .length - 1 ];
250
+ System .arraycopy (WORDS , 0 , TEXT , 0 , TEXT .length );
251
+ TEXT = WORDS ;
252
+
253
+ for (int i = 0 ; i < TEXT .length ; i ++) {
254
+ try {
255
+ Pane .getDocument ().remove (WORDS .length -1 , TEXT .length -1 );
256
+ } catch (BadLocationException e ) {
257
+ // TODO Auto-generated catch block
258
+ e .printStackTrace ();
259
+ }
260
+
261
+ }
262
+ System .out .println (WORDS .length );
263
+ WC = WORDS .length ;
264
+ }
265
+
266
+ private void redo (ActionEvent evt ) {
267
+ // TODO
268
+ }
269
+
270
+ public static void main (String [] args ) {
271
+ // System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
272
+ javax .swing .SwingUtilities .invokeLater (new Runnable () {
273
+ public void run () {
274
+ new NotePad ();
275
+ }
276
+ });
277
+ }
278
+ }
0 commit comments