Text Editor - Open

So close, yet so far... or hopefully so close?

Page 1 of 1

4 Replies - 1044 Views - Last Post: 14 March 2010 - 10:32 PM Rate Topic: -----

#1 hawkysu  Icon User is offline

  • I am a spoon
  • member icon

Reputation: 5
  • View blog
  • Posts: 1,432
  • Joined: 20-February 09

Text Editor - Open

Posted 14 March 2010 - 09:39 PM

Ok, so I think I'm close with getting this part to work... not actually sure. I really don't know much about Java, but I also really need to get this project out of the way.

the hard part is, realizing I can't use a lot of the simple commands because I'm not allowed to alter "too much" (how much is too much?)* of the code provided.
Anyways, here's the overall problem: I need to create a text editor in Java. Not using Jpanel (I don't think) so it's a little tougher. I need to get the follow commands: Open, Save, Search, Cut, Copy, and Paste.
The current problem: I don't even know if I'm on the right path for the Open command. I think I am.
The Error:
Cannot make a static reference to the non-static method Open(SimpleFileReader) from the type Editor EditorMenuHandler.java /Editor/src line 55 Java Problem

And the code:
EditorMenuHandler.java
/* 
 * comp285 EditorMenuHandler class
 */
import java.awt.*;
import java.awt.event.*;

/**
 * The EditorMenuHandler that handles the events generated by the 
 * menu of the Editor class.
 * @author   Ian A Mason.
 * @version  1.0 beta
 * @date 7/02/01
 * @see  java.awt.event.ActionListener
 * @see  java.awt.event.ItemListener
 */
class EditorMenuHandler implements ActionListener, ItemListener {
	   int value; 
	   int option; 
	   String content = null; 
	   String name = null; 
	   String word; 
	   String str;

    /**
     * This is the name of the Editor instance whose events this EditorMenuHandler instance is
     * listening to. It will need to ask it to perform certain tasks according to what 
     * type of event it hears has happened.
     */
    private Editor editor;
    /**
     * This constructs a EditorMenuHandler instance who handles the events of the
     * particular Editor instance.
     * 
     */
    protected EditorMenuHandler(Editor editor){ this.editor =  editor; }
    /**
     * This here is where all the events of interest get handled. It will be here
     * that you will have to ask  the editor to do the appropriate things.
     * @see  java.awt.event.ActionListener
     */
    @SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent ae){
	FileDialog filedialog;
	final SearchDialog searchDialog;
	String arg = (String)ae.getActionCommand();
	// the Open ... case
	if(arg.equals(Editor.fileLabels[0])){
	    if(Editor.VERBOSE)
		System.err.println(Editor.fileLabels[0] + 
				   " has been selected");
	    filedialog = new FileDialog(editor, "Open File Dialog", FileDialog.LOAD); 
		SimpleFileReader read= SimpleFileReader.openFileForReading(filedialog.getFile()+
				filedialog.getDirectory());

			Editor.Open(read);
	    filedialog.show();
	    if(Editor.VERBOSE){ 
		System.err.println("Exited filedialog.setVisible(true);");
		System.err.println("Open file = " + filedialog.getFile());
		System.err.println("Open directory = " + filedialog.getDirectory());

	    }
	}
	//the Save ... case
	if(arg.equals(Editor.fileLabels[1])){
	    if(Editor.VERBOSE)
		System.err.println(Editor.fileLabels[1] + 
				   " has been selected");
	    filedialog = new FileDialog(editor, "Save File Dialog", FileDialog.SAVE); 
	    filedialog.show();
	    if(Editor.VERBOSE){
		System.err.println("Exited filedialog.setVisible(true);");
		System.err.println("Save file = " + filedialog.getFile());
		System.err.println("Save directory = " + filedialog.getDirectory());
	    }
	}
	//the Search ... case
	if(arg.equals(Editor.fileLabels[2])){
	    if(Editor.VERBOSE)
		System.err.println(Editor.fileLabels[2] + 
				   " has been selected");
	    searchDialog = new SearchDialog(editor); 
	    searchDialog.show();
	    if(Editor.VERBOSE)
		System.err.println("searchDialog.show(); has exited");
	}
	//the Quit ... case
	if(arg.equals(Editor.fileLabels[3])){
	    if(Editor.VERBOSE)
		System.err.println(Editor.fileLabels[3] + 
				   " has been selected");
	    System.exit(0);

	}
	//the Cut case	
	if(arg.equals(Editor.editLabels[0])){
	    if(Editor.VERBOSE)
		System.err.println(Editor.editLabels[0] + 
				   " has been selected");
	}
	//the Copy case
	if(arg.equals(Editor.editLabels[1])){
	    if(Editor.VERBOSE)
		System.err.println(Editor.editLabels[1] + 
				   " has been selected");
	}
	//the Paste case
	if(arg.equals(Editor.editLabels[2])){
	    if(Editor.VERBOSE)
		System.err.println(Editor.editLabels[2] +  
				   " has been selected");
	}
    }
    /**
     * This needs to be here since we need to implement the ItemListener 
     * interface
     * @see  java.awt.event.ItemListener
     */
    public void itemStateChanged(ItemEvent ie){
	//shouldn't need to do anything here.
    }
}


With the problem at:
// the Open ... case
	if(arg.equals(Editor.fileLabels[0])){
	    if(Editor.VERBOSE)
		System.err.println(Editor.fileLabels[0] + 
				   " has been selected");
	    filedialog = new FileDialog(editor, "Open File Dialog", FileDialog.LOAD); 
		SimpleFileReader read= SimpleFileReader.openFileForReading(filedialog.getFile()+
				filedialog.getDirectory());

			Editor.Open(read);
	    filedialog.show();
	    if(Editor.VERBOSE){ 
		System.err.println("Exited filedialog.setVisible(true);");
		System.err.println("Open file = " + filedialog.getFile());
		System.err.println("Open directory = " + filedialog.getDirectory());

	    }


But that assumes that my Open method even works :/

Editor.java
/*
 * comp285 Editor class
 */
import java.awt.*;
import java.awt.event.*;
/** 
 * 
 * @author   Ian A Mason.
 * @see          CenteredFrame
 * @see          java.awt.Frame
 * @version  1.0 beta
 * @date 7/02/01
 */


class Editor extends CenteredFrame {
    /**
     * A static flag, accessed via the class, not an instance!!
     * A boolean flag used to turn on/off error messaging to System.err.
     * This protected constant can be used by the other classes in this
     * application. You can turn it off once you think your program
     * is ready to ship!
     */
    protected static final boolean VERBOSE = true;
    /** 
     * Static data, accessed via the class, not an instance!!
     * The labels for the items in the file pulldown menu.
     * This protected constant can be used by the other classes in this
     * application. These are used by the EditorMenuHandler object to 
     * decide which item has been selected.
     */
    protected static final  String[] fileLabels = 
    { "Open ...", "Save ...", "Search ...", "Quit ..." };
    /** 
     * Static data, accessed via the class, not an instance!!
     * The labels for the items in the edit pulldown menu.
     * This protected constant can be used by the other classes in this
     * application. These are used by the EditorMenuHandler object to 
     * decide which item has been selected.
     */
    protected static final  String[] editLabels = 
    { "Cut", "Copy", "Paste"};
    /**
     * The TextArea instance textArea is the little workhorse of the editor.
     * <em>Note that it is private, and must remain so!</em> Only the editor object
     * is permitted to talk to this object.
     * @see          java.awt.TextArea
     * @see          java.awt.TextComponent
     */
    private final  TextArea     textArea = new  TextArea("", 40, 80, TextArea.SCROLLBARS_BOTH); 
    /**
     * The MenuBar instance menuBar is the toplevel widget at the top of the editor
     * that contains the pull down menus. <em>Note that it is private, and must 
     * remain so! Only the editor object is permitted to talk to this object.</em>
     * @see          java.awt.MenuBar
     */
    private final  MenuBar       menuBar = new  MenuBar();
    /**
     * The file menu is the thing that one clicks on to pull down the menu items.
     * @see          java.awt.Menu
     */
    private   final  Menu         fileMenu = new  Menu("File");
    /**
     * The items in the pull down file menu belong to this array. Its length is determined
     * by the static final array of file item labels.
     * @see          java.awt.MenuItem
     */
    private   final  MenuItem[]   fileItem = new  MenuItem[fileLabels.length];
    /**
     * The edit menu is the thing that one clicks on to pull down the menu items.
     * @see          java.awt.Menu
     */
    private final  Menu         editMenu = new  Menu("Edit");
    /**
     * The items in the pull down edit menu belong to this array. Its length is determined
     * by the static final array of edit item labels.
     * @see          java.awt.MenuItem
     */
    private final  MenuItem[]   editItem = new  MenuItem[editLabels.length];
    /**
     * This is the name we use to refer to the  object that handles the editors
     * events. Though we will not actually ever send it any messages, just merely
     * register it with Java as a listener to the appropriate events.
     * 
     * @see          EditorMenuHandler
     */
    private EditorMenuHandler menuHandler;
    /**
     * An auxiliary procedure for initializing the pull down menus. It eliminates
     * a small amount of code duplication.
     */
    private void initMenu(MenuItem[]  menuItems, 
			  String[] menuLabels, 
			  Menu     menu,
			  EditorMenuHandler menuHandler,
			  MenuBar       menuBar){
	for(int i = 0; i < menuItems.length; i++){
	    menuItems[i] = new MenuItem(menuLabels[i]);
	    menu.add(menuItems[i]);
	    menuItems[i].addActionListener(menuHandler);
	}
	menuBar.add(menu);
    }
    /**
     * The private Editor object constructor is where most of the work gets done.
     * Making the CenteredFrame part using the super construct (i.e by calling the
     * CenteredFrame constructor. It also makes the other 
     * important toplevel object, the menuHandler.
     * It also must make
     * all the awt components that are part of the editor: the text area, the 
     * pull down menus, and register the menuHandler with the widgets that it 
     * needs to listen to (the events that they generate).
     */
    private Editor(){
	super("Text Editor");
	menuHandler = new EditorMenuHandler(this);
	textArea.setFont(new Font("SansSerif", Font.PLAIN, 15));
	setMenuBar(menuBar);
	// make the pull down file menu
	initMenu(fileItem,fileLabels,fileMenu,menuHandler,menuBar);
	// make the pull down edit menu
	initMenu(editItem,editLabels,editMenu,menuHandler,menuBar);
	//set the layout manager to be a BorderLayout object
	setLayout(new BorderLayout());
	//put the textArea in the center
	add(textArea, BorderLayout.CENTER);
	//validate the layout
	validate();
	//make the editor visible
	setVisible(true);
    }

    public void Open(SimpleFileReader read){
    	textArea.replaceRange(read.readLine(), 0, 3200);
    	
    }
    
    /**
     * The main method that creates an Editor instance, and
     * thus starts the whole kit and kaboodle.
     */
    
    public static void main(String[] args){
	Editor editor = new Editor();
	//the reason this doesn't exit immediately is because
	//this is actually a multithreaded application. The other
	//thread sitting in the background is the <em>event handler thread</em>
    }
}


With the Open Method:
    public void Open(SimpleFileReader read){
    	textArea.replaceRange(read.readLine(), 0, 3200);
    	
    }



And the SimpleFileReader.java
/* comp285 SimpleFileReader.java
 */
import java.io.*;
/**
 * SimpleFileReader is a small class to wrap around the usual FileReader
 * to shield you from the exception handling which we haven't yet gotten
 * to in class. 
 * <P>It has just three methods of note: one to open a new file for reading,
 * one to read line from an open file, and one to close the file when done.
 * <P>Here is a simple example that shows using the SimpleFileReader to
 * to display the contents of a file on the console:
 * <PRE>
 *    SimpleFileReader reader = SimpleFileReader.openFileForReading("letter.txt");
 *    if (reader == null) {
 *        System.out.println("Couldn't open file!");
 *        return;
 *    }
 *    String line;
 *    while ((line = reader.readLine()) != null)
 *        System.out.println(line);
 *    reader.close();
 * </PRE>
 * <P>You don't need to make any changes.
 *
 *
 * @see           java.io.FileReader
 * @see          java.io.BufferedReader
 * @version      1.1 10/01/99
 * @author       Julie Zelenski
 */
public class SimpleFileReader
{

	/**
	 * Opens a new file for reading. The filename can either be a relative
	 * path, which will be relative to the working directory of the program
	 * when started, or an absolute path. If the file exists and can be
	 * opened, a new SimpleFileReader is returned. If the file cannot be
	 * opened (for any reason: wrong name, wrong path, lack of permissions, etc.)
	 * null is returned.
	 */
	public static SimpleFileReader openFileForReading(String filename)
	{
		try {
			return new SimpleFileReader(new BufferedReader(new FileReader(filename)));
		} catch(IOException e) {	
			return null;
		}	
	}
	
	
	/**
	 * Reads the next line from the open file. Returns the entire contents
	 * of the line as one string, excluding the newline at the end.
	 * If at EOF and no more lines to read, null is returned. null is also
	 * returned on any I/O error.
	 */
	public String readLine()
	{
		try {
			return reader.readLine();
		} catch (IOException e) {
			return null;
		}
	}

	/**
	 * Closes the file when done reading.  You should close a reader when
	 * you are finished to release the OS resources for use by others.
	 */
	public void close()
	{
		try {
			reader.close();
		} catch (IOException e) {}
	}
	
	/**
	 * Constructor is private so that only means to create a new reader
	 * is through the static method which does error checking.
	 */
	private SimpleFileReader(BufferedReader reader) 
	{
		this.reader = reader;
	}
	
	private BufferedReader reader;

}



Thank you in advance for your help!

If you have any questions about my questions, please let me know.

----------
* To quote from the Prompt: "There are essentially two top level objects in the design of this widget: the editor object (an instance of the Editor class); and the menuHandler object (which is an instance of the EditMenuHandler class. These are the two classes you will have to elaborate upon. There are a few other classes hanging around, but you should not have to tinker with them at all."

Is This A Good Question/Topic? 0
  • +

Replies To: Text Editor - Open

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Text Editor - Open

Posted 14 March 2010 - 09:50 PM

Alright, your issue is, well trying to access a non-static method in a static fashion. Static access is where you can call the class method, but that method is the exact same for every object of that type. You have:
    public void Open(SimpleFileReader read){
        textArea.replaceRange(read.readLine(), 0, 3200);
        
    }



Because there is no "static" keyword (there shouldn't be, you were right about that), you cannot access it like this:
    Editor.Open(read);



because that is accessing it through the Editor CLASS, not an object. What you have to do is pass the Editor OBJECT to that method to use.

Here are two tutorials I wrote about static methods:
http://www.dreaminco...&showentry=2019
http://www.dreaminco...topic158726.htm
Was This Post Helpful? 1
  • +
  • -

#3 hawkysu  Icon User is offline

  • I am a spoon
  • member icon

Reputation: 5
  • View blog
  • Posts: 1,432
  • Joined: 20-February 09

Re: Text Editor - Open

Posted 14 March 2010 - 09:55 PM

So, I should use:
editor.Open(read);


Which gives me no errors.

Thanks. *runs program*
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Text Editor - Open

Posted 14 March 2010 - 09:56 PM

No prob.
Was This Post Helpful? 0
  • +
  • -

#5 hawkysu  Icon User is offline

  • I am a spoon
  • member icon

Reputation: 5
  • View blog
  • Posts: 1,432
  • Joined: 20-February 09

Re: Text Editor - Open

Posted 14 March 2010 - 10:32 PM

Ok, So more problems (Fun for the whole family!).

The program is unable to open the file. Whenever I select one, It goes:
yay x file has been selected
yay x file belongs to here
boo Couldn't open file

I've tried doing this a couple different ways:
 SimpleFileReader read= SimpleFileReader.openFileForReading(filedialog.getFile());

SimpleFileReader read= SimpleFileReader.openFileForReading(filedialog.getFile()+
				filedialog.getDirectory());

SimpleFileReader read= SimpleFileReader.openFileForReading("EditorMenuHandler.java");


to no success. What am I doing wrong?

Oh yea:
My edits to the Open case:
	// the Open ... case
	if(arg.equals(Editor.fileLabels[0])){
	    if(Editor.VERBOSE)
		System.err.println(Editor.fileLabels[0] + 
				   " has been selected");
	    filedialog = new FileDialog(editor, "Open File Dialog", FileDialog.LOAD); 

	    filedialog.show();
	    if(Editor.VERBOSE){ 
		System.err.println("Exited filedialog.setVisible(true);");
		System.err.println("Open file = " + filedialog.getFile());
		System.err.println("Open directory = " + filedialog.getDirectory());
		
		SimpleFileReader read= SimpleFileReader.openFileForReading("EditorMenuHandler.java");
		if(read == null){
			System.out.println("Couldn't Open File");
			return;
		}
		editor.Open(read);
	    }
	}


This post has been edited by hawkysu: 14 March 2010 - 10:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1