Blame view

readline/src/pad/prac1/Line.java 2.85 KB
1
2
package pad.prac1;
3
4
5
import java.util.Observable;

public class Line extends Observable
6
7
8
9
{
	private String line;
	private int cursorPosition;
	private int writeMode;
10
	private Console console;
11
12
	public Line(Console cons)
13
	{
14
		console = cons;
15
		line = "";
16
		writeMode = EditableBufferedReader.INSERT;
17
		cursorPosition = 1;
18
		addObserver(console);
19
20
21
22
23
24
25
26
27
28
29
30
	}

	public int length()
	{
		return line.length();
	}

	public int getCursorPosition()
	{
		return cursorPosition;
	}
31
	public void toggleMode()
32
	{
33
		writeMode = 1 - writeMode;
34
35
	}
36
	private void sendCommand(int type, int value)
37
	{
38
39
		setChanged();
		notifyObservers(new Command(type,value));
40
41
	}
42
	private void sendCommand(int type)
43
	{
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
		setChanged();
		notifyObservers(new Command(type));
	}

	private void insertCharAt(char c, int pos)
	{
		if(pos == 0)
		{
			String s = "";
			s += c;
			line = s + line;
		}
		else if(pos == line.length())
		{
			line += c;
		}
		else
		{
			String s = "";
			s += c;
			line = line.substring(0, pos).concat(s).concat(line.substring(pos,line.length()));
		}
66
67
	}
68
	private void removeCharAt(int pos)
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	{
		if(pos >= 0)
		{
			if(pos == 0)
			{
				line = line.substring(1);
			}
			else if(pos == line.length()-1)
			{
				line = line.substring(0, line.length()-1);
			}
			else
			{
				line = line.substring(0, pos).concat(line.substring(pos+1,line.length()));
			}
		}
	}
87
	public void addChar(char c)
88
	{
89
		if(c == EditableBufferedReader.RETURN_KEY)
90
		{
91
92
			insertCharAt((char)EditableBufferedReader.LINE_FEED,cursorPosition-1);
			sendCommand(Command.INSERT_CHAR,EditableBufferedReader.LINE_FEED);
93
			sendCommand(Command.INSERT_CHAR,EditableBufferedReader.RETURN_KEY);
94
95
			cursorPosition++;
			return;
96
		}
97
		switch(writeMode)
98
		{
99
100
101
102
			case EditableBufferedReader.INSERT:
				sendCommand(Command.INSERT_CHAR,(int)c);
				insertCharAt(c, cursorPosition-1);
				cursorPosition++;
103
				break;
104
105
106
107
108
109
110
111
112
113
114

			case EditableBufferedReader.OVERWRITE:
				if(cursorPosition != line.length()+1)
				{
					removeCharAt(cursorPosition-1);
					sendCommand(Command.DELETE_CHAR);
				}
				sendCommand(Command.INSERT_CHAR,(int)c);
				insertCharAt(c, cursorPosition-1);
				cursorPosition++;
			break;
115
		}
116
117
118
119
120
	}

	public void delChar(int mode)
	{
		switch(mode)
121
		{
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
			case EditableBufferedReader.DEL_BACKWARD:
				if(cursorPosition != 1)
				{
					sendCommand(Command.MOVE_CURSOR,cursorPosition-1);
					sendCommand(Command.DELETE_CHAR);
					removeCharAt(cursorPosition-2);
					cursorPosition--;
				}
			break;
			case EditableBufferedReader.DEL_FORWARD:
				if(cursorPosition != (line.length()+1))
				{
					removeCharAt(cursorPosition-1);
					sendCommand(Command.DELETE_CHAR,1);
				}
			break;
		}
	}

	public void setCursorTo(int pos)
	{
		if((pos <= line.length()+1) && (pos >= 1) && (pos != cursorPosition))
		{
			sendCommand(Command.MOVE_CURSOR,pos);
			cursorPosition = pos;
147
148
149
150
151
152
153
154
155
		}
	}

	public String toString()
	{
		return line;
	}

}