Skip to content

Commit f1df2b0

Browse files
Second verison completed
1 parent ef90c92 commit f1df2b0

File tree

6 files changed

+99
-7
lines changed

6 files changed

+99
-7
lines changed

Diff for: libs/gson.jar

291 KB
Binary file not shown.

Diff for: src/main/java/com/thinking/machines/notepad/Notepad.java

+7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ private void addEventListeners()
148148
textArea.getDocument().addUndoableEditListener(ev->{
149149
undoManager.addEdit(ev.getEdit());
150150
editMenuManager.setEnabledUndoMenuItem(undoManager.canUndo());
151+
// Reset Redo (New edits clear the redo stack)
152+
editMenuManager.setEnabledRedoMenuItem(false);
151153
}
152154
);
153155

@@ -278,6 +280,11 @@ private boolean isClipboardAvailable()
278280
return false;
279281
}
280282
}
283+
public void discardUndoManagerAllEdits()
284+
{
285+
undoManager.discardAllEdits();
286+
editMenuManager.setEnabledUndoMenuItem(undoManager.canUndo());
287+
}
281288
public void setStatusBarPanelVisibility(boolean visible)
282289
{
283290
statusPanel.setVisible(visible);

Diff for: src/main/java/com/thinking/machines/notepad/io/FileHandler.java

+66-3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void setExtension()
7979
this.filePath=this.filePath+"."+this.extension;
8080
this.baseFileName=this.file.getName()+"."+this.extension;
8181
}
82+
8283
}
8384
public void setDisplayFileName()
8485
{
@@ -262,11 +263,13 @@ public boolean saveAs(Notepad.Counter c)
262263
if(choice==JOptionPane.YES_OPTION)
263264
{
264265
this.file=selectedFile;
265-
saveFile(c);
266266
this.filePath=selectedFile.getAbsolutePath();
267267
this.fileName=selectedFile.getName();
268+
this.encoding=Charset.forName("UTF-8");
269+
this.encoding=EncodingDetector.detectEncoding(file);
268270
setExtension();
269271
setDisplayFileName();
272+
saveFile(c);
270273
notepad.setFileName(this.filePath);
271274
notepad.setTitle(this.displayFileName+" - Danipad");
272275
saved=true;
@@ -284,6 +287,10 @@ else if(choice==JOptionPane.NO_OPTION)
284287
this.filePath=selectedFile.getAbsolutePath();
285288
setExtension();
286289
setDisplayFileName();
290+
this.file=new File(this.filePath);
291+
this.file.createNewFile();
292+
this.encoding=Charset.forName("UTF-8");
293+
this.encoding=EncodingDetector.detectEncoding(file);
287294
saveFile(c);
288295
notepad.setFileName(this.filePath);
289296
notepad.setTitle(this.displayFileName+" - Danipad");
@@ -294,6 +301,7 @@ else if(choice==JOptionPane.NO_OPTION)
294301
{
295302
saved=false;
296303
JOptionPane.showMessageDialog(notepad,"Failed to save the file. Please check permissions and try again.", "Save Error",JOptionPane.ERROR_MESSAGE);
304+
System.out.println(exception);
297305
LogException.log(exception);
298306
}
299307

@@ -312,22 +320,38 @@ public void openFile(Notepad.Counter c)
312320
}
313321
else
314322
{
323+
this.file=new File(filePath);
324+
boolean success=setupFileLocation();
315325
setExtension();
316326
setDisplayFileName();
317327
this.file=new File(filePath);
318328

319-
if(!this.file.exists())
329+
330+
if(!success)
331+
{
332+
this.filePath="Untitled";
333+
this.displayFileName=null;
334+
this.extension=null;
335+
this.encoding=Charset.forName("UTF-8");
336+
}
337+
338+
else if(!this.file.exists())
320339
{
340+
321341
int option=JOptionPane.showConfirmDialog(notepad,"Cannot find the "+filePath+" file.\n\nDo you want to create a new file?","Danipad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
322342
if(option==JOptionPane.YES_OPTION)
323343
{
324344
this.file.createNewFile();
325345
textArea.setText("");
346+
this.encoding=EncodingDetector.detectEncoding(file);
326347
//new file created
327348
}
328349
else if(option==JOptionPane.NO_OPTION)
329350
{
330351
this.filePath="Untitled";
352+
this.displayFileName=null;
353+
this.extension=null;
354+
this.encoding=Charset.forName("UTF-8");
331355
}
332356
else if(option==JOptionPane.CANCEL_OPTION)
333357
{
@@ -385,6 +409,8 @@ protected void done()
385409
{
386410
if(bufferedReader!=null) bufferedReader.close();
387411
c.suppressChangeEvents=false;
412+
//to ensure the newly loaded content isn't treated as an "Undoable" change
413+
notepad.discardUndoManagerAllEdits();
388414
}catch(IOException ioException)
389415
{
390416
LogException.log(ioException);
@@ -395,7 +421,9 @@ protected void done()
395421
worker.execute();
396422

397423
}
398-
notepad.setTitle(this.displayFileName+"- Danipad");
424+
if(this.displayFileName!=null)notepad.setTitle(this.displayFileName+"- Danipad");
425+
else notepad.setTitle("Untitled - Danipad");
426+
399427
}
400428
notepad.setFileName(this.filePath);
401429
}catch(IOException exception)
@@ -406,6 +434,41 @@ protected void done()
406434
LogException.log(exception);
407435
}
408436

437+
}
438+
private boolean setupFileLocation()
439+
{
440+
File parentDir=this.file.getParentFile();
441+
if(parentDir==null) return true;
442+
if(this.file.exists())
443+
{
444+
if(this.file.isDirectory())
445+
{
446+
JOptionPane.showMessageDialog(notepad,"The given path refers to a directory not a file. Cannot create file","Error",JOptionPane.ERROR_MESSAGE);
447+
return false;
448+
}
449+
}
450+
else
451+
{
452+
if(parentDir!=null && parentDir.exists())
453+
{
454+
//file dont exists create file
455+
}
456+
else
457+
{
458+
//parent dir not exists create the missing directories
459+
if(parentDir!=null && parentDir.mkdir())
460+
{
461+
return true;
462+
}
463+
else
464+
{
465+
//failed
466+
JOptionPane.showMessageDialog(notepad,"The System cannot find the specified path","Warning",JOptionPane.WARNING_MESSAGE);
467+
return false;
468+
}
469+
}
470+
}
471+
return true;
409472
}
410473
public String getCurrentEncoding()
411474
{

Diff for: src/main/java/com/thinking/machines/notepad/managers/EditMenuManager.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thinking.machines.notepad.managers;
1+
package com.thinking.machines.notepad.managers;
22
import com.thinking.machines.notepad.Notepad;
33
import com.thinking.machines.notepad.io.*;
44
import com.thinking.machines.notepad.models.*;
@@ -22,7 +22,7 @@ public class EditMenuManager
2222
private UndoManager undoManager;
2323
private SearchManager searchManager;
2424
private JMenu editMenu;
25-
private JMenuItem undoMenuItem;
25+
private JMenuItem undoMenuItem,redoMenuItem;
2626
private JMenuItem cutMenuItem,copyMenuItem,pasteMenuItem;
2727
private JMenuItem deleteMenuItem,findMenuItem,findNextMenuItem,findPreviousMenuItem;
2828
private JMenuItem replaceMenuItem,goToMenuItem,selectAllMenuItem;
@@ -50,6 +50,7 @@ public EditMenuManager(Notepad notepad,JTextArea textArea,JScrollPane scrollPane
5050
private void initComponents()
5151
{
5252
undoMenuItem=new JMenuItem("Undo");
53+
redoMenuItem=new JMenuItem("Redo");
5354
cutMenuItem=new JMenuItem("Cut");
5455
copyMenuItem=new JMenuItem("Copy");
5556
pasteMenuItem=new JMenuItem("Paste");
@@ -66,6 +67,7 @@ private void initComponents()
6667
private void addMenuItems()
6768
{
6869
editMenu.add(undoMenuItem);
70+
editMenu.add(redoMenuItem);
6971
editMenu.add(cutMenuItem);
7072
editMenu.add(copyMenuItem);
7173
editMenu.add(pasteMenuItem);
@@ -88,6 +90,7 @@ private void addMenuItems()
8890
private void bindShortcutKeys()
8991
{
9092
undoMenuItem.setAccelerator(KeyStroke.getKeyStroke('Z',Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
93+
redoMenuItem.setAccelerator(KeyStroke.getKeyStroke('Y',Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
9194
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke('X',Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
9295
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke('C',Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
9396
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke('V',Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
@@ -105,11 +108,27 @@ private void addEventListeners()
105108
undoMenuItem.addActionListener(ev->{
106109
if(undoManager.canUndo())
107110
{
111+
int oldPos=textArea.getCaretPosition();
108112
undoManager.undo();
113+
int newPos=textArea.getCaretPosition();
114+
textArea.select(Math.min(oldPos,newPos),Math.max(oldPos,newPos));
115+
redoMenuItem.setEnabled(true);
109116
undoMenuItem.setEnabled(undoManager.canUndo());
110117
}
111118
});
112119

120+
redoMenuItem.addActionListener(ev->{
121+
if(undoManager.canRedo())
122+
{
123+
int oldPos=textArea.getCaretPosition();
124+
undoManager.redo();
125+
int newPos=textArea.getCaretPosition();
126+
textArea.select(Math.min(oldPos,newPos),Math.max(oldPos,newPos));
127+
undoMenuItem.setEnabled(undoManager.canUndo());
128+
redoMenuItem.setEnabled(undoManager.canRedo());
129+
}
130+
});
131+
113132
cutMenuItem.addActionListener((ev)->{
114133
textArea.cut();
115134
});
@@ -687,7 +706,10 @@ public void setEnabledUndoMenuItem(boolean enable)
687706
{
688707
undoMenuItem.setEnabled(enable);
689708
}
690-
709+
public void setEnabledRedoMenuItem(boolean enable)
710+
{
711+
redoMenuItem.setEnabled(enable);
712+
}
691713
public void setEnabledCutMenuItem(boolean enable)
692714
{
693715
cutMenuItem.setEnabled(enable);

Diff for: src/main/resources/icons/icon.ico

99.1 KB
Binary file not shown.

Diff for: testcase/date1.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import java.text.*;
33
class psp
44
{
5-
public static void main(String gg[])
5+
public static void wewemain(String gg[])
66
{
77
String date="01/"+gg[0]+"/"+gg[1];
88
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");

0 commit comments

Comments
 (0)