serviceImplementation="com.huachao.plugin.util.GlobalVar"
serviceInterface="com.huachao.plugin.util.GlobalVar"
/>
这样写完以后,在获取数据的时候,直接如下:
privateGlobalVar.Statestate=GlobalVar.getInstance().state;
//state.IS_ENABLE
//state.IS_RANDOM
3.编写Action
主要包含2个Action:
EnableAction和RandomColorAction。
EnableAction用于设置插件的开启或关闭,RandomColorAction用于设置是否使用随机颜色。
由于二者功能类似,我们只看看EnableAction的实现:
/**
*Createdbyhuachaoon2016/12/27.
*/
publicclassEnableActionextendsAnAction{
privateGlobalVar.Statestate=GlobalVar.getInstance().state;
@Override
publicvoidupdate(AnActionEvente){
Projectproject=e.getData(PlatformDataKeys.PROJECT);
Editoreditor=e.getData(PlatformDataKeys.EDITOR);
if(editor==null||project==null){
e.getPresentation().setEnabled(false);
}else{
JComponentcomponent=editor.getContentComponent();
if(component==null){
e.getPresentation().setEnabled(false);
}else{
e.getPresentation().setEnabled(true);
}
}
updateState(e.getPresentation());
}
@Override
publicvoidactionPerformed(AnActionEvente){
Projectproject=e.getData(PlatformDataKeys.PROJECT);
Editoreditor=e.getData(PlatformDataKeys.EDITOR);
if(editor==null||project==null){
return;
}
JComponentcomponent=editor.getContentComponent();
if(component==null)
return;
state.IS_ENABLE=!
state.IS_ENABLE;
updateState(e.getPresentation());
//只要点击Enable项,就把缓存中所有的文本清理
CharPanel.getInstance(component).clearAllStr();
GlobalVar.registerDocumentListener(project,editor,state.IS_ENABLE);
}
privatevoidupdateState(Presentationpresentation){
if(state.IS_ENABLE){
presentation.setText("Enable");
presentation.setIcon(AllIcons.General.InspectionsOK);
}else{
presentation.setText("Disable");
presentation.setIcon(AllIcons.Actions.Cancel);
}
}
}
代码比较简单,跟前面几篇文章中写的很相似。
只需注意一下actionPerformed函数中调用了两个函数:
CharPanel.getInstance(component).clearAllStr();
GlobalVar.registerDocumentListener(project,editor,state.IS_ENABLE);
CharPanel对象中的clearAllStr()函数后面介绍,只需知道它是将缓存中的所有动画对象清除。
GlobalVar对象中的registerDocumentListener()函数是添加DocumentListener监听器。
实现本文效果的中枢是DocumentListener监听器,是通过监听文本内容发生变化来获取实现字符动画效果的数据。
因此应应可能早地将DocumentListener监听器加入,而DocumentListener监听器加入的时刻包括:
用户点击Action、用户敲入字符。
也就是说,多个地方都存在添加DocumentListener监听器的可能。
因此把这个函数抽出来,加入到GlobalVar中,具体实现如下:
privatestaticAmazingDocumentListeneramazingDocumentListener=null;
publicstaticvoidregisterDocumentListener(Projectproject,Editoreditor,booleanisFromEnableAction){
if(!
hasAddListener||isFromEnableAction){
hasAddListener=rue;
JComponentcomponent=editor.getContentComponent();
if(component==null)
return;
if(amazingDocumentListener==null){
amazingDocumentListener=newAmazingDocumentListener(project);
Documentdocument=editor.getDocument();
document.addDocumentListener(amazingDocumentListener);
}
Threadthread=newThread(CharPanel.getInstance(component));
thread.start();
}
}
可以看到,一旦DocumentListener监听器被加入,就会开启一个线程,这个线程是一直执行,实现动画效果。
DocumentListener监听器只需加入一次即可。
4.实现动画
前面多次使用到了CharPanel对象,CharPanel对象就是用于实现动画效果。
先源码:
packagecom.huachao.plugin.util;
importcom.huachao.plugin.Entity.CharObj;
importjavax.swing.*;
importjava.awt.*;
importjava.util.*;
importjava.util.List;
/**
*Createdbyhuachaoon2016/12/27.
*/
publicclassCharPanelimplementsRunnable{
privateJComponentmComponent;
privatePointmCurPosition;
privateSetcharSet=newHashSet();
privateListbufferList=newArrayList();
privateGlobalVar.Statestate=GlobalVar.getInstance().state;
publicvoidsetComponent(JComponentcomponent){
mComponent=component;
}
publicvoidrun(){
while(state.IS_ENABLE){
if(GlobalVar.font!
=null){
synchronized(bufferList){
charSet.addAll(bufferList);
bufferList.clear();
}
draw();
intminFontSize=GlobalVar.font.getSize();
//修改各个Label的属性,使之能以动画形式出现和消失
Iteratorit=charSet.iterator();
while(it.hasNext()){
CharObjobj=it.next();
if(obj.isAdd()){//如果是添加到文本框
if(obj.getSize()<=minFontSize){//当字体大小到达最小后,使之消失
mComponent.remove(obj.getLabel());
it.remove();
}else{//否则,继续减小
intsize=obj.getSize()-6minFontSize:
(obj.getSize()-6);
obj.setSize(size);
}
}else{//如果是从文本框中删除
Pointp=obj.getPosition();
if(p.y<=0||obj.getSize()<=0){//如果到达最底下,则清理
mComponent.remove(obj.getLabel());
it.remove();
}else{
p.y=p.y-10;
intsize=obj.getSize()-1<0?
0:
(obj.getSize()-1);
obj.setSize(size);
}
}
}
}
try{
if(charSet.isEmpty()){
synchronized(charSet){
charSet.wait();
}
}
Thread.currentThread().sleep(50);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
//绘制文本,本质上只是修改各个文本的位置和字体大小
privatevoiddraw(){
if(mComponent==null)
return;
for(CharObjobj:
charSet){
JLabellabel=obj.getLabel();
Fontfont=newFont(GlobalVar.font.getName(),GlobalVar.font.getStyle(),obj.getSize());
label.setFont(font);
FontMetricsmetrics=label.getFontMetrics(label.getFont());
inttextH=metrics.getHeight();//字符串的高,只和字体有关
inttextW=metrics.stringWidth(label.getText());//字符串的宽
label.setBounds(obj.getPosition().x,obj.getPosition().y-(textH-GlobalVar.minTextHeight),textW,textH);
}
mComponent.invalidate();
}
publicvoidclearAllStr(){
synchronized(bufferList){
bufferList.clear();
charSet.clear();
IteratorsetIt=charSet.iterator();
while(setIt.hasNext()){
CharObjobj=setIt.next();
mComponent.remove(obj.getLabel());
}
IteratorbufferIt=bufferList.iterator();
while(bufferIt.hasNext()){
CharObjobj=bufferIt.next();
mComponent.remove(obj.getLabel());
}
}
}
//单例模式,静态内部类
privatestaticclassSingletonHolder{
//静态初始化器,由JVM来保证线程安全
privatestaticCharPanelinstance=newCharPanel();
}
//返回单例对象
publicstaticCharPanelgetInstance(JComponentcomponent){
if(nent!
=null){
SingletonHolder.instance.mComponent=component;
}
returnSingletonHolder.instance;
}
//由光标监听器回调,由此可动态获取当前光标位置
publicvoidsetPosition(Pointposition){
this.mCurPosition=position;
}
/**
*将字符串添加到列表中。
*
*@isAdd如果为true表示十新增字符串,否则为被删除字符串
*@str字符串
*/
publicvoidaddStrToList(Stringstr,booleanisAdd){
if(mComponent!
=null&&mCurPosition!
=null){
CharObjcharObj=newCharObj(mCurPosition.y);
JLabellabel=newJLabel(str);
charObj.setStr(str);
charObj.setAdd(isAdd);
charObj.setLabel(label);
if(isAdd)
charObj.setSize(60);
else
charObj.setSize(GlobalVar.font.getSize());
charObj.setPosition(mCurPosition);
if(state.IS_RANDOM){
label.setForeground(randomColor());
}else{
label.setForeground(GlobalVar.defaultForgroundColor);
}
synchronized(bufferList){
bufferList.add(charObj);
}
if(charSet.isEmpty()){
synchronized(charSet){
charSet.notify();
}
}
mComponent.add(label);
}
}
//以下用于产生随机颜色
privatestaticfinalColor[]COLORS={Color.GREEN,Color.BLACK,Color.BLUE,Color.ORANGE,Color.YELLOW,Color.RED,Color.CYAN,Color.MAGENTA};
privateColorrandomColor(){
intmax=COLORS.length;
intindex=newRandom().nextInt(max);
returnCOLORS[index];
}
}
解释一下两个关键函数run()和draw()。
run()函数是开启新线程开始执行的函数,它的实现是一个循环,当插件开启时会一直循环运行。
CharPanel使用了2个集合来保持用户删除或者添加的字符串