下面采用line span的数据结构来实现一个简易的编辑器。
实现后的编辑器具有的功能如下:1、输入和删除字符 2、正常的换行和鼠标响应 3、正常的上下左右键控制4、保存功能。
很多功能还没有实现,计划在以后实现的包括1、鼠标拖拽选择文本 2、文本的复制粘贴 3、对某一个具体编程语言的高亮,以及语法纠错 4、滚动条 5、对中文的支持
采用了Swing编写程序,在写之前看到好多人说Swing过时了之类的,一度冲动的想学一下javaFX,后来想到了没有什么框架能一直存在,主要的是算法。于是还是用Swing写了。
整体结构很简单,一个JFrame内置一个自定义的JComponent,最终效果:
没错,我准备做一个verilog编辑器。
首先让程序在屏幕中间显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screen_width=(int)screenSize.getWidth();
int screen_height=(int)screenSize.getHeight();
this.setBounds(screen_width>>2, screen_height>>2, screen_width>>1, screen_height>>1);
所有的文字按照line span结构存在一个linkedlist当中。当打开一个文件时候,我们需要将文件读入
line_number=new LinkedList<>();
//if the file is given;
if(string!=null)
{
//read the file to the linkedlist
file_name=string;
File f= new File(string);
String str;
if(f.exists())
{
try {
BufferedReader reader=new BufferedReader(new FileReader(f));
while((str=reader.readLine())!=null) line_number.add(str);
reader.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else
{
try {
f.createNewFile();
line_number.add("");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}else//if is a new page
{
line_number.add("");
}
光标由一个线程控制,每隔一段时间就闪一下,当然,移动光标时候要让他常亮
Runnable change_cursor_state=new Runnable() {
@Override
public void run() {
int i=0;
while(true)
{
try {
Thread.sleep(1);
if(lock)
{
//if move the cursor,recount the delay time
lock=false;
i=0;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
if(i>300)
{
//change the state of the cursor
i=0;
if(cursor_state) cursor_state=false;
else cursor_state=true;
MainComponent.this.repaint();
}
//MainComponent.this.validate();
}
}
};
编辑器被设计为可以滚动的状态,滚动时的偏移值需要被保存下来,同时光标相对空间的位置也要被记录下来,两者相加可以得到光标相对于文本的绝对位置。由于目前这个编辑器只支持英文,同时字体采用了等宽的宋体,所以控件可通过直接的单击计算出字符的相对位置。相对位置在大多数情况下不变,只有在编辑器大小改变的时候可能改变。每次编辑器大小改变的时候。都要重新计算一些必要的参数
@Override
public void invalidate() {
//System.out.println("invalidate");
//char length and width
char_height=getGraphics().getFontMetrics().getHeight();
char_width=getGraphics().getFontMetrics().charWidth('w');
//editor length and width
bound_rect=getBounds();
//how many chars can show at height and length
height_char_number=bound_rect.height/char_height;
width_char_number=bound_rect.width/char_width;
super.invalidate();
}
绘制的时候,根据屏幕的偏移值进行绘制。不在空间内的文本可以不去绘制。
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(bound_rect.x, bound_rect.y, bound_rect.width, bound_rect.height);
//System.out.println("paint");
g.setFont(new Font("宋体", Font.PLAIN, 18));
g.setColor(Color.WHITE);
int y=-show_height;
int x=-show_width;
for(Iterator<String> iterator=line_number.iterator();iterator.hasNext();)
{
String string=iterator.next();
y++;
//if the data is on the screen
if(y>0)
{
g.drawString(string, x*char_width, y*char_height);
}
}
//draw the cursor
if(cursor_state)
{
g.drawLine(char_width*cursor_width, cursor_height*char_height+2, char_width*cursor_width, cursor_height*char_height+char_height+2);
}
}
然后是对鼠标和键盘的监听,光标的位置是相对位置,鼠标单击的也是相对位置,但是键盘的按键输入需要在绝对位置输入,只要做好了转换,便很简单了
@Override
public void mouseClicked(MouseEvent e) {
// System.out.println(e.getX()/char_width+","+e.getY()/char_height);
//which used to decide the cursor's position and assure cursor not overboard
int temp_height=e.getY()/char_height+show_height;
if(temp_height>line_number.size()-1) temp_height=line_number.size()-1;
int temp_width=e.getX()/char_width+show_width;
int compare_width=line_number.get(temp_height).length();
//according absolute position to calculate the relative position
if(temp_width>compare_width)
{
if(compare_width>width_char_number-1)
{
cursor_width=width_char_number-1;
show_width=compare_width-cursor_width;
}else
{
show_width=0;
cursor_width=compare_width;
}
}else
{
cursor_width=temp_width-show_width;
}
cursor_height=temp_height-show_height;
up_down=false;
cursor_state=true;
lock=true;
repaint();
}
最后文件需要保存,根据打开的是新文件还是存在的文件,分别进行不同的操作
public void save_file()
{
if(file_name!=null)
{
try {
BufferedWriter writer=new BufferedWriter(new FileWriter(new File(file_name)));
for(Iterator<String> iterator=line_number.iterator();iterator.hasNext();)
{
String string=iterator.next();
writer.write(string);
writer.newLine();
}
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else
{
JFileChooser chooser=new JFileChooser();
int returnVal=chooser.showSaveDialog(new JPanel());
if(returnVal==JFileChooser.APPROVE_OPTION) {
file_name=chooser.getSelectedFile().getPath();
File file=new File(file_name);
if(file.exists()) file.delete();
try {
file.createNewFile();
BufferedWriter writer=new BufferedWriter(new FileWriter(file));
for(Iterator<String> iterator=line_number.iterator();iterator.hasNext();)
{
String string=iterator.next();
writer.write(string);
writer.newLine();
}
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
至此,简易的文本编辑器便实现了。