package kwic.es;
import java.util.ArrayList;
@SuppressWarnings("unchecked")
public class LineStorage{
//存储的是ArrayList类的对象,每个对象又封装了一个ArrayList对象,存储一个字符串中的单词序列
//存储格式:行、单词、字母三级结构,即字符串序号,单词序号,字母序号三级读写结构
private ArrayList lines_ = new ArrayList();
public void setChar(char c, int position, int word, int line){
ArrayList current_line = (ArrayList) lines_.get(line);
String current_word = (String) current_line.get(word);
char[] chars = current_word.toCharArray();
chars[position] = c;
current_word = new String(chars);
current_line.set(word, current_word);
}
public char getChar(int position, int word, int line){
return ((String) ((ArrayList) lines_.get(line)).get(word)).charAt(position);
}
public void addChar(char c, int word, int line){
ArrayList current_line = (ArrayList) lines_.get(line);
String current_word = (String) current_line.get(word);
char[] chars = new char[current_word.length() + 1];
current_word.getChars(0, chars.length - 1, chars, 0);
chars[chars.length - 1] = c;
current_word = new String(chars);
current_line.set(word, current_word);
}
public void deleteChar(int position, int word, int line){
ArrayList current_line = (ArrayList) lines_.get(line);
String current_word = (String) current_line.get(word);
char[] chars = new char[current_word.length() - 1];
current_word.getChars(0, position, chars, 0);
current_word.getChars(position + 1, chars.length + 1, chars, position);
current_word = new String(chars);
current_line.set(word, current_word);
}
public int getCharCount(int word, int line){
return ((String) ((ArrayList) lines_.get(line)).get(word)).length();
}
public void setWord(char[] chars, int word, int line){
setWord(new String(chars), word, line);
}
public void setWord(String chars, int word, int line){
ArrayList current_line = (ArrayList) lines_.get(line);
current_line.set(word, chars);
}
public String getWord(int word, int line){
return (String) ((ArrayList) lines_.get(line)).get(word);
}
public void addWord(char[] chars, int line){
addWord(new String(chars), line);
}
public void addWord(String chars, int line){
ArrayList current_line = (ArrayList) lines_.get(line);
current_line.add(chars);
}
public void addEmptyWord(int line){
ArrayList current_line = (ArrayList) lines_.get(line);
current_line.add(new String());
}
public void deleteWord(int word, int line){
ArrayList current_line = (ArrayList) lines_.get(line);
current_line.remove(word);
}
public int getWordCount(int line){
return ((ArrayList) lines_.get(line)).size();
}
public void setLine(char[][] words, int line){
String[] tmp = new String[words.length];
for(int i = 0; i < words.length; i++)
tmp[i] = new String(words[i]);
setLine(tmp, line);
}
public void setLine(String[] words, int line){
ArrayList current_line = (ArrayList) lines_.get(line);
current_line.clear();
for(int i = 0; i < words.length; i++)
current_line.add(words[i]);
}
public void insertLine(char[][] words, int line){
String[] tmp = new String[words.length];
for(int i = 0; i < words.length; i++)
tmp[i] = new String(words[i]);
insertLine(tmp, line);
}
public void insertLine(String[] words, int line){
ArrayList current_line = new ArrayList();
for(int i = 0; i < words.length; i++)
current_line.add(words[i]);
lines_.add(line, current_line);
}
public String[] getLine(int line){
ArrayList current_line = (ArrayList) lines_.get(line);
String[] tmp = new String[current_line.size()];
for(int i = 0; i < tmp.length; i++)
tmp[i] = (String) current_line.get(i);
return tmp;
}
public String getLineAsString(int line){
ArrayList current_line = (ArrayList) lines_.get(line);
int size = current_line.size();
int length = 0;
for(int i = 0; i < size; i++)
length += getWord(i, line).length();
length += size - 1;
char[] tmp = new char[length];
int count = 0;
for(int i = 0; i < size; i++){
getWord(i, line).getChars(0, getWord(i, line).length(), tmp, count);
count += getWord(i, line).length();
if(i != (size - 1))
tmp[count++] = ' '
}
return new String(tmp);
}
public void addLine(char[][] words){
String[] tmp = new String[words.length];
for(int i = 0; i < words.length; i++)
tmp[i] = new String(words[i]);
addLine(tmp);
}
public void addLine(String[] words){
ArrayList current_line = new ArrayList();
for(int i = 0; i < words.length; i++)
current_line.add(words[i]);
lines_.add(current_line);
}
public void addEmptyLine(){
ArrayList current_line = new ArrayList();
lines_.add(current_line);
}
public void deleteLine(int line){
lines_.remove(line);
}
public int getLineCount(){
return lines_.size();
}
}
package kwic.es;
//事件消息类,LineStorage类对象改变时触发,包装了事件类型和一个字符串参数
public class LineStorageChangeEvent{
public static final int ADD = 0;
public static final int DELETE = 1;
public static final int INSERT = 2;
private int type_;
private String arg_;
public LineStorageChangeEvent(int type){
type_ = type;
}
public LineStorageChangeEvent(int type, String arg){
type_ = type;
arg_ = arg;
}
public void setType(int type){
type_ = type;
}
public int getType(){
return type_;
}
public void setArg(String arg){
arg_ = arg;
}
public String getArg(){
return arg_;
}
}
package kwic.es;
import java.util.Observable;
public class LineStorageWrapper extends Observable{
private LineStorage lines_ = new LineStorage();
public void insertLine(String[] words, int index){
lines_.insertLine(words, index);
String line = lines_.getLineAsString(index);
LineStorageChangeEvent event = new LineStorageChangeEvent(LineStorageChangeEvent.INSERT, line);
setChanged();
notifyObservers(event);
}
public void deleteLine(int index){
String line = lines_.getLineAsString(index);
lines_.deleteLine(index);
LineStorageChangeEvent event = new LineStorageChangeEvent(LineStorageChangeEvent.DELETE, line);
setChanged();
notifyObservers(event);
}
public void addLine(String[] words){
lines_.addLine(words);
LineStorageChangeEvent event = new LineStorageChangeEvent(LineStorageChangeEvent.ADD);
setChanged();
notifyObservers(event);
}
//自己增加的方法,以字符串为依据删除行
public void deleteLine(String line){
for(int i=0;i<lines_.getLineCount();i++){
if(lines_.getLineAsString(i).equals(line)){
lines_.deleteLine(i);
}
}
LineStorageChangeEvent event = new LineStorageChangeEvent(LineStorageChangeEvent.DELETE);
setChanged();
notifyObservers(event);
}
//自己增加的方法,根据字符串数组删除行
public void deleteLine(String[] str){
String[] s;
for(int i=0;i<lines_.getLineCount();i++){
s=lines_.getLine(i);
boolean stat=true;
if(str.length!=s.length)
stat=false;
else{
for(int j=0;j<s.length;j++){
if(!(s[j].equals(str[j])))
stat=false;
}
}
if(stat==true){
String line = lines_.getLineAsString(i);
LineStorageChangeEvent event = new LineStorageChangeEvent(LineStorageChangeEvent.DELETE,line);
lines_.deleteLine(i);
setChanged();
notifyObservers(event);
}
}
}
public String getLineAsString(int index){
return lines_.getLineAsString(index);
}
public String[] getLine(int index){
return lines_.getLine(index);
}
public int getLineCount(){
return lines_.getLineCount();
}
}