Welcome to Dream.In.Code
Become a Java Expert!

Join 150,170 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,290 people online right now. Registration is fast and FREE... Join Now!




check saving ?

 
Reply to this topicStart new topic

check saving ?

mahdisama
25 Aug, 2008 - 05:15 AM
Post #1

New D.I.C Head
*

Joined: 18 Aug, 2008
Posts: 4

Hi all.I have working on the notepad project.
I want when i close the window or press new fileitem,My program do check that my file are saved or no ?
this is my code for new,save and opening file and my save and open work correctly.
please help me if anyone know what i should to do ?
CODE

package model;

import notepad.NotepadMain;
import java.util.*;
import java.awt.*;

import java.io.*;
import javax.swing.*;
import javax.swing.text.*;



public class Model {

    private String s = "", fileName,
    lineReader;
    protected NotepadMain main;
  // private boolean Save=false,Exist=false;

    public Model(NotepadMain main) {
        this.main = main;

    }

    public void newFile(JTextComponent textArea,JFrame parent){

        if(textArea.getText().compareTo("")==0){
             parent.setTitle("Untiteled_Notepad");
         }
         else if(textArea.getText().compareTo("")!= 0){
             parent.setTitle("Untiteled_Notepad");
             main.view.swing_Configuration.getDoc().removeUndoableEditListener(main.view.listener);
             textArea.setDocument(new PlainDocument());
             main.view.swing_Configuration.getDoc().addUndoableEditListener(main.view.listener);


         }


    }

    public void readFile(FileDialog fileOpenDialog,JFrame parent) {
      
        if ((fileName = fileOpenDialog.getFile()) != null) {

            parent.setTitle(fileName + "_Notepad");

          //  Exist=true;
            try {

                main.view.swing_Configuration.getDoc().removeUndoableEditListener(main.view.listener);

                BufferedReader reader=new BufferedReader(new FileReader(fileOpenDialog.getDirectory() +
                           fileOpenDialog.getFile()));

                while ((lineReader = reader.readLine()) != null)
                    main.view.swing_Configuration.getTextArea().append(
                            lineReader + "\n");
                reader.close();
                main.view.swing_Configuration.getDoc().addUndoableEditListener(main.view.listener);

            } catch (IOException ex) {
                main.view.showError(ex, main.view.swing_Configuration);
            }


        }
    }

    public void writeFile(FileDialog fileSaveDialog,JFrame parent) {

     //   Save=true;
        try{


            fileSaveDialog.setTitle("Save As");
            String s=main.view.swing_Configuration.getTextArea().getText();
            char buffer[]=new char[s.length()];
            s.getChars(0,s.length(),buffer,0);
            BufferedWriter writer=new BufferedWriter(new FileWriter(fileSaveDialog.getDirectory()+fileSaveDialog.getFile()));
            writer.write(buffer);
            main.view.swing_Configuration.setTitle(fileSaveDialog.getFile()+"_notepad");
            writer.flush();
            writer.close();

        }catch(IOException es){

            main.view.showError(es,parent);
        }

    }
    

  

    public boolean IsSave() {
        return Save;
    }

    public boolean IsExist() {
        return Exist;
    }


}


User is offlineProfile CardPM
+Quote Post

1lacca
RE: Check Saving ?
25 Aug, 2008 - 05:42 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Register a listener on your JTextArea so if it's content changes, it'll set a state variable, and when you click close, or whatever, it'll check this variable, and warns the user. Save should clear this variable.
User is offlineProfile CardPM
+Quote Post

mahdisama
RE: Check Saving ?
25 Aug, 2008 - 06:46 AM
Post #3

New D.I.C Head
*

Joined: 18 Aug, 2008
Posts: 4

QUOTE(1lacca @ 25 Aug, 2008 - 06:42 AM) *

Register a listener on your JTextArea so if it's content changes, it'll set a state variable, and when you click close, or whatever, it'll check this variable, and warns the user. Save should clear this variable.

tnx freind.
Would you mind to give me a sample to do it ?

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:07AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month