All pastes #1654111 Raw Edit

Someone

public text v1 · immutable
#1654111 ·published 2009-11-03 01:32 UTC
rendered paste body
import java.io.*;
import java.util.*;

public class DisplayEditor {

	static MessageLoop messages = new MessageLoop();

	// MessageLoopIterator iter = (MessageLoopIterator) messages.iterator();

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException {

		if (args.length == 0) {
			// yeah, here's our scanner
			Scanner in = new Scanner(System.in);
			System.out.print("enter command (? for help)>");
			String input = in.nextLine();

			while (!(input.equals("q"))) {
				
				try {
					cmdProc(input);
				} catch (StringIndexOutOfBoundsException e) {}
				System.out.print("enter command (? for help)>");
				input = in.nextLine();
			}
			if (input.equals("q")) {
				System.out.println("quit");
				in.close();
				System.out.close();
			}

		}
		// Oh, thanks for the args. Now we'll process your input file.
		else {
			File inpFile = new File(args[0]);

			// If for some reason it's not there or can't be read.
			if (!inpFile.exists() || !inpFile.canRead()) {
				System.out.println("Invalid input file!");
				System.exit(1);
			}

			Scanner in = new Scanner(inpFile);

			try {
				while (in.hasNextLine()) {
					String temp = in.next();
					try {
						cmdProc(temp);
					} catch (StringIndexOutOfBoundsException e) {}
				}
			}finally {
				in.close();
			}

		}
	}

	// If the message loop is empty, display "no messages to save". Otherwise,
	// save all the messages to file named filename, one message per line
	// starting with the current message (and proceeding forward through the
	// loop). If filename already exists, display
	// "warning: file already exists, will be overwritten" before saving the
	// messages. If filename cannot be written to, display "unable to save".
	public static void save(String filename) throws FileNotFoundException {

		File dstFile = new File(filename);
		DblListnode curr;
		try {
			curr = (DblListnode) messages.getCurrent();

		if (messages.size() == 0) {
			System.out.println("no messages to save");
		} else {
			if (dstFile.exists()) {
				System.out.println("warning: file already exists, will be overwritten");
			}
			if (dstFile.isDirectory() || (dstFile.exists() && !dstFile.canWrite())) {
				System.out.println("unable to save");
			} else {
			
			PrintStream print = new PrintStream(dstFile);
			for (int i = 0; i < messages.size(); i++) {
				print.println(curr.getData());
				//System.out.println(curr.getData()); debugging shtuffs
				curr = curr.getNext();
			}
			print.close();
			}
			
		}
		} catch (EmptyLoopException e) {
			System.out.println("EmptyLoopException getCurrent");
		}

	}

	// (Note that this command is a lower-case L, not the number one.) If a file
	// named filename does not exist or cannot be read from, display
	// "unable to load". Otherwise, load the messages from filename in the order
	// they are given, and set the current message to be the first message read
	// from the file. You may assume that there is one message per line, that
	// there are no blank lines, and that the file is not empty (i.e., it has at
	// least one line).
	public static void load(String filename) {

		File srcFile = new File(filename);

		if (!srcFile.exists() || !srcFile.canRead() || !srcFile.isFile()) {
			System.out.println("unable to load");
		} else {
			Scanner inFile;
			try {
				inFile = new Scanner(srcFile);

				while (inFile.hasNext()) {
					String line = inFile.nextLine();
					addMsg(line);
				}
				inFile.close();
				messages.forward();
				
			} catch (FileNotFoundException e) {
				System.out.println("FileNotFoundException");
			}

		}

	}

	public static void displayMsgs() {

		MessageLoopIterator iter = (MessageLoopIterator) messages.iterator();

		if (messages.size() == 0) {
			System.out.println("no messages");
		} else {
			// iter.next();
			for (int count = 0; count < messages.size(); count++) {

				System.out.println(iter.next().getData()); // make sure it
				// starts
				// at the first one
			}

		}
	}

	public static void nextMsg() {
		if(messages.size()==0){
			System.out.println("no messages");
		} else{
			messages.forward();
			displayContext();
		}
	}

	public static void previousMsg() {
		if(messages.size()==0){
			System.out.println("no messages");
		} else{
			messages.back();
			displayContext();
		}
	}

	public static void jumpAround(int n) {

		if (messages.size() == 0) {
			System.out.println("no messges");
		} else {
			if( n >= 0) {
				for (int i = 0; i < n; i++)
					messages.forward();
			}
			if (n <= 0) {
				for (int i = 0; i > n; i--)
					messages.back();
			}
			displayContext();
		}
		
	}

	public static void deleteCurrentMsg() {
		try {
			if(messages.size() == 0){
				System.out.println("no messages");
			}
			else if (messages.size()==1){
				messages.removeCurrent();
				System.out.println("no messages");
			}
			else{
				messages.removeCurrent();
				displayContext();
			}
		} catch (EmptyLoopException e) {
			System.out.println("EmptyLoopException");
		}
	}

	// If the message loop is empty, add the new message to the loop and make it
	// the current message. Otherwise, add the new message immediately after the
	// current message and make the new message the new current message. In
	// either case, display the current context (see note below).
	public static void addMsg(String msg) {

		if (messages.size() <= 0) {
			messages.addBefore(msg);

		} else {

			messages.addAfter(msg);
		}

	}

	public static void insertMsg(String msg) {

		if (messages.size() <= 0) {
			messages.addBefore(msg);
		} else {

			messages.addBefore(msg);
			// messages.back();
		}
	}
	
	// MessageLoopIterator iter = (MessageLoopIterator) messages.iterator();
	// iterate thru until found record times you iterated, then forward() or
	// back() that number of times
	
	public static void findMsg(String msg) {
		if (messages.size()==0){
			System.out.println("no messages");
		}
		else{
			boolean found = false;
			Iterator<DblListnode> iter = messages.iterator();
			for (int i = 0; i<messages.size() && !found; i++){
				String item = (String) iter.next().getData(); 
				if (item.startsWith(msg)){
					found = true;
					for(int j = 0; j<i; j++){
						messages.forward();
					}
					displayContext();
				}
				
			}
			if(!found){
				System.out.println("not found");
			}
		}


	}

	public static void replaceMsg(String msg) {

		if (messages.size() == 0) {
			System.out.println("no messages");
		} else {
			try {
				messages.removeCurrent();
			} catch (EmptyLoopException e) {
				System.out.println("EmptyLoopException");
			}
			messages.back();
			messages.addAfter(msg);
			displayContext();
		}

	}

	public static void displayContext() {
		try {
			DblListnode curr = (DblListnode) messages.getCurrent();

			if (messages.size() == 1) {
				System.out.println("*** " + curr.getData() + " ***");
			}

			if (messages.size() == 2) {
				System.out.println("*** " + curr.getData() + " ***");
				System.out.println(curr.getNext().getData());
			}

			if (messages.size() > 2) {
				System.out.println(curr.getPrev().getData());
				System.out.println("*** " + curr.getData() + " ***");
				System.out.println(curr.getNext().getData());
			}

		} catch (EmptyLoopException e) {
			System.out.println("EmptyLoopException");
		}

	}
	
	public static void cmdProc (String input) {

		if (input.equals("?")) {
			System.out.println("s (save)   l (load)      " +
					"d (display) \nn (next)   p (previous)  " +
					"j (jump)\nx (delete) a (add after) i " +
					"(insert before)\nf (find)   r (replace)   " +
					"q (quit)\n");
		} else if (input.charAt(0) == 's' && input.charAt(1) == ' ') {
				String filename = (input.substring(2));
				try {
					save(filename);
				} catch (FileNotFoundException e) {
				}
		} else if (input.charAt(0) == 'l' && input.charAt(1) == ' ') {
			String filename = (input.substring(2));
			load(filename);
		} else if (input.charAt(0) == 'd') {
			displayMsgs();
		} else if (input.charAt(0) == 'n') {
			nextMsg();
		} else if (input.charAt(0) == 'p') {
			previousMsg();
		} else if (input.charAt(0) == 'j' && input.charAt(1) == ' ') {
			String temp = input.substring(2);
			if (checkInputForInt(temp)){
				int n = Integer.parseInt(temp);
				jumpAround(n);
			} else{
				System.out.println("invalid command");
			}
		} else if (input.charAt(0) == 'x') {
			// delete
			deleteCurrentMsg();
		} else if (input.charAt(0) == 'a' && input.charAt(1) == ' ') {
			// add
			String temp = input.substring(2);
			if (temp.length() >= 1) {
				addMsg(temp);
				displayContext();
			}

		} else if (input.charAt(0) == 'i' && input.charAt(1) == ' ') {
			String temp = input.substring(2);
			if (temp.length() >= 1) {
				insertMsg(temp);
				displayContext();
			}
		} else if (input.charAt(0) == 'f' && input.charAt(1) == ' ') {
			String temp = input.substring(2);
			if (temp.length() >= 1) {
				findMsg(temp);
			}
		} else if (input.charAt(0) == 'r' && input.charAt(1) == ' ') {
			replaceMsg(input.substring(2));
			
		} else if (input.equals("cntx")) { //used by us to see the current context
			displayContext();
		} else{
			System.out.println("invalid command");
		}
	}
	
	public static boolean checkInputForInt(String temp){
		if (temp.charAt(0) == '-'){
			temp = temp.substring(1);
		}
		if (temp.length() == 0){
			return false;
		}
		for (int i = 0; i<temp.length(); i++){
			char c = temp.charAt(i);
			System.out.println(c);
			if(c != '1' && c != '2' && c != '3' && c != '4' 
				&& c != '5' && c != '6' && c != '7' && 
					c != '8' && c != '9' && c != '0'){
				return false;
			}
		}
		return true;
	}
}