The Artima Developer Community
Sponsored Link

Java Answers Forum
Creating text editor....

3 replies on 1 page. Most recent reply: May 6, 2003 2:31 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
chaos7

Posts: 2
Nickname: chaos7
Registered: May, 2003

Creating text editor.... Posted: May 2, 2003 11:48 PM
Reply to this message Reply
Advertisement
Hi. I was wondering if someone could help me here, i don't know where to start. I've got this project to complete and basically have to create a text editor. Its pretty much completed but just had to add the "open", "save", "cut", "copy", "paste" options. I just wanted to know if someone could help me with the few of the above commands - as i dont know where to place them in the code. Have copied 2 of the .java files that i need to add to. If someone can just lead me to the right path that would be great!!

----First is Editor.java---

/*
* comp285 Editor class
*/
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Ian A. Mason.
* @see CenteredFrame
* @see java.awt.Frame
* @version 1.1
* @since 13/03/03
*/


class Editor extends CenteredFrame {
/**
* A boolean flag used to turn on/off error messaging to System.err.
* A static flag, accessed via the class, not an instance!!
* 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;
/**
* The labels for the items in the file pulldown menu.
* Static data, accessed via the class, not an instance!!
* 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" };
/**
* The labels for the items in the edit pulldown menu.
* Static data, accessed via the class, not an instance!!
* 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 = new MenuItem(menuLabels);
menu.add(menuItems);
menuItems.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);
}
/**
* 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>
}
}




/**
* This method opens a file on the local file system and returns its contents
* as a String
*/
protected String openFile(String filename) {

String results = "";
String line;

// Create an instance of the File class

File in_file = new File(filename);

System.out.println("Imported File Length:"+ in_file.length());

// Open the file only if it is less than 45K

if(in_file.length() < 46080) {

try {

//Chain a high level input stream (LineNumberReader) to a low
//level input stream (FileReader)

LineNumberReader inputFile = new LineNumberReader(new FileReader(in_file));

//Read the contents

while((line = inputFile.readLine()) != null) {
results += line + "\r\n";

}

// Close the input streams

inputFile.close();

}

// Handle various error conditions

catch(FileNotFoundException fnfe) {

results = "Unable to find the file " + filename;
fileOpened = false;

}

catch(IOException ioe) {

results = "General Input/Output Error";
fileOpened = false;

}

catch(Exception e) {

results = "An Unknown Error Occurred.";
fileOpened = false;

}

}

else {

results = in_file.getAbsolutePath() + " is too large for this application to open.";
fileOpened = false;

}

return results;


}

-------The second is Edithandler.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.1
* @since 13/03/03
* @see java.awt.event.ActionListener
* @see java.awt.event.ItemListener
*/
class EditorMenuHandler implements ActionListener, ItemListener {
/**
* 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
*/
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);
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.
}
}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Creating text editor.... Posted: May 3, 2003 12:54 PM
Reply to this message Reply
You are missing the code for the CenteredFrame class.

Your code will not compile without it.

chaos7

Posts: 2
Nickname: chaos7
Registered: May, 2003

Re: Creating text editor.... Posted: May 6, 2003 2:11 AM
Reply to this message Reply
yeah i already have centredframe.java file...but i dont know how to create 'cut' 'copy' 'paste' commands....??????

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Creating text editor.... Posted: May 6, 2003 2:31 PM
Reply to this message Reply
Clipboard myclipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
 
 
/**   cut deletes selected text and stores the text into a string variable
  */
  public void cut(){
    selectedtext = textArea.getSelectedText();
    if (selectedtext != null){
      StringSelection clipboardstring = new StringSelection(selectedtext);
      myclipboard.setContents(clipboardstring,clipboardstring);
      int startmark = textArea.getSelectionStart();    
      int endmark = textArea.getSelectionEnd();
      textArea.replaceRange("",startmark,endmark);
    }    
  }
 
  /**  copy() stores the selected text into a string variable
  */
  public void copy(){
    selectedtext = textArea.getSelectedText();
    if (selectedtext != null){
      StringSelection clipboardstring = new StringSelection(selectedtext);
      myclipboard.setContents(clipboardstring,clipboardstring);
    }
  }
  
  /**  paste() inserts the string stored from copy or cut at
  *  the current caret position
  */
  public void paste(){
    Transferable clipboarddata = myclipboard.getContents(this);
    try{
      selectedtext =  (String) clipboarddata.getTransferData(DataFlavor.stringFlavor);
    }catch(Exception e){
      System.err.println("Exception: " + e.getMessage());      
    }
    textArea.insert(selectedtext,textArea.getCaretPosition());
  }
 

Flat View: This topic has 3 replies on 1 page
Topic: help using an iterator with a linked list Previous Topic   Next Topic Topic: Address Book

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use