├── Amazing-Mode.jar ├── AmazingModePreview.gif ├── out └── production │ └── Amazing-Mode │ ├── com │ └── huachao │ │ └── plugin │ │ ├── Entity │ │ └── CharObj.class │ │ ├── util │ │ ├── CharPanel.class │ │ ├── GlobalVar.class │ │ ├── GlobalVar$State.class │ │ └── CharPanel$SingletonHolder.class │ │ ├── action │ │ ├── EnableAction.class │ │ └── RandomColorAction.class │ │ └── listener │ │ ├── AmazingCaretListener.class │ │ ├── AmazingDocumentListener.class │ │ └── AmazingTypedActionHandler.class │ └── META-INF │ └── plugin.xml ├── .idea ├── vcs.xml ├── modules.xml ├── misc.xml ├── uiDesigner.xml └── workspace.xml ├── README.md ├── Amazing-Mode.iml ├── src └── com │ └── huachao │ └── plugin │ ├── listener │ ├── AmazingCaretListener.java │ ├── AmazingTypedActionHandler.java │ └── AmazingDocumentListener.java │ ├── Entity │ └── CharObj.java │ ├── action │ ├── RandomColorAction.java │ └── EnableAction.java │ └── util │ ├── GlobalVar.java │ └── CharPanel.java └── resources └── META-INF └── plugin.xml /Amazing-Mode.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/Amazing-Mode.jar -------------------------------------------------------------------------------- /AmazingModePreview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/AmazingModePreview.gif -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/Entity/CharObj.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/Entity/CharObj.class -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/util/CharPanel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/util/CharPanel.class -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/util/GlobalVar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/util/GlobalVar.class -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/action/EnableAction.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/action/EnableAction.class -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/util/GlobalVar$State.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/util/GlobalVar$State.class -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/action/RandomColorAction.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/action/RandomColorAction.class -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/listener/AmazingCaretListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/listener/AmazingCaretListener.class -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/util/CharPanel$SingletonHolder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/util/CharPanel$SingletonHolder.class -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/listener/AmazingDocumentListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/listener/AmazingDocumentListener.class -------------------------------------------------------------------------------- /out/production/Amazing-Mode/com/huachao/plugin/listener/AmazingTypedActionHandler.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huachao1001/Amazing-Mode/HEAD/out/production/Amazing-Mode/com/huachao/plugin/listener/AmazingTypedActionHandler.class -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Amazing-Mode 2 | 3 | Amazing-Mode for IDEA 4 | 5 | ![效果预览](https://github.com/huachao1001/Amazing-Mode/blob/master/AmazingModePreview.gif?raw=true) 6 | 7 | # 使用 8 | 9 | 安装 Amazing-Mode 之后 点击AmazingMode > disable/enable 就可以关闭或者开启插件 10 | 11 | 12 | # 下载 13 | 14 | git clone https://github.com/huachao1001/Amazing-Mode.git 15 | 16 | # 安装 17 | 18 | ## 下载jar包 19 | 20 | [Download Amazing-Mode.jar](https://raw.githubusercontent.com/huachao1001/Amazing-Mode/master/Amazing-Mode.jar) 21 | 22 | ## 安装 Plugin jar包 23 | 24 | Preferences/Plugins 25 | -> Install plugin from disk 26 | -> 选择 jar文件 27 | -> Apply 28 | -> 重启编辑器 29 | -------------------------------------------------------------------------------- /Amazing-Mode.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/com/huachao/plugin/listener/AmazingCaretListener.java: -------------------------------------------------------------------------------- 1 | package com.huachao.plugin.listener; 2 | 3 | 4 | import com.huachao.plugin.util.CharPanel; 5 | import com.intellij.openapi.editor.LogicalPosition; 6 | import com.intellij.openapi.editor.event.CaretEvent; 7 | import com.intellij.openapi.editor.event.CaretListener; 8 | 9 | import java.awt.*; 10 | 11 | /** 12 | * Created by huachao on 2016/12/27. 13 | */ 14 | public class AmazingCaretListener implements CaretListener { 15 | 16 | @Override 17 | public void caretPositionChanged(CaretEvent caretEvent) { 18 | LogicalPosition logicalPosition = caretEvent.getNewPosition(); 19 | Point position = caretEvent.getEditor().logicalPositionToXY(logicalPosition); 20 | CharPanel.getInstance(null).setPosition(position); 21 | } 22 | 23 | @Override 24 | public void caretAdded(CaretEvent caretEvent) { 25 | 26 | } 27 | 28 | @Override 29 | public void caretRemoved(CaretEvent caretEvent) { 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/com/huachao/plugin/Entity/CharObj.java: -------------------------------------------------------------------------------- 1 | package com.huachao.plugin.Entity; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | 6 | /** 7 | * 用于封装一个字符对象 8 | *

9 | * Created by huachao on 2016/12/27. 10 | */ 11 | public class CharObj { 12 | private int initY; 13 | private boolean isAdd; 14 | private String str; 15 | private int size; 16 | private JLabel label; 17 | private Point position; 18 | 19 | public CharObj(int initY) { 20 | this.initY = initY; 21 | } 22 | 23 | public int getInitY() { 24 | return initY; 25 | } 26 | 27 | public Point getPosition() { 28 | return position; 29 | } 30 | 31 | public void setPosition(Point position) { 32 | this.position = position; 33 | } 34 | 35 | public int getSize() { 36 | return size; 37 | } 38 | 39 | public void setSize(int size) { 40 | this.size = size; 41 | } 42 | 43 | public void setLabel(JLabel label) { 44 | this.label = label; 45 | } 46 | 47 | public JLabel getLabel() { 48 | return label; 49 | } 50 | 51 | public String getStr() { 52 | return str; 53 | } 54 | 55 | public void setStr(String str) { 56 | this.str = str; 57 | } 58 | 59 | public void setAdd(boolean add) { 60 | isAdd = add; 61 | } 62 | 63 | public boolean isAdd() { 64 | return isAdd; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/huachao/plugin/listener/AmazingTypedActionHandler.java: -------------------------------------------------------------------------------- 1 | package com.huachao.plugin.listener; 2 | 3 | import com.huachao.plugin.util.CharPanel; 4 | import com.huachao.plugin.util.GlobalVar; 5 | import com.intellij.CommonBundle; 6 | import com.intellij.openapi.actionSystem.DataContext; 7 | import com.intellij.openapi.actionSystem.PlatformDataKeys; 8 | import com.intellij.openapi.editor.Editor; 9 | import com.intellij.openapi.editor.actionSystem.EditorActionManager; 10 | import com.intellij.openapi.editor.actionSystem.TypedAction; 11 | import com.intellij.openapi.editor.actionSystem.TypedActionHandler; 12 | import com.intellij.openapi.project.Project; 13 | import org.jetbrains.annotations.NotNull; 14 | 15 | /** 16 | * Created by huachao on 2016/12/27. 17 | */ 18 | public class AmazingTypedActionHandler implements TypedActionHandler { 19 | private TypedActionHandler mLastHandler; 20 | 21 | public AmazingTypedActionHandler(TypedActionHandler lastHandler) { 22 | mLastHandler = lastHandler; 23 | } 24 | 25 | @Override 26 | public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) { 27 | if (!GlobalVar.hasAddListener) { 28 | Project project = PlatformDataKeys.PROJECT.getData(dataContext); 29 | Editor editor1 = PlatformDataKeys.EDITOR.getData(dataContext); 30 | GlobalVar.registerDocumentListener(project, editor, false); 31 | } 32 | if (mLastHandler != null) 33 | mLastHandler.execute(editor, c, dataContext); 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | com.huachao.plugin.AmazingMode 3 | AmazingMode 4 | 1.0 5 | HuaChao 6 | 7 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 24 | 25 | 26 | 27 | 31 | 32 | 33 | 34 | 35 | 36 | 38 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /out/production/Amazing-Mode/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | com.huachao.plugin.AmazingMode 3 | AmazingMode 4 | 1.0 5 | HuaChao 6 | 7 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 24 | 25 | 26 | 27 | 31 | 32 | 33 | 34 | 35 | 36 | 38 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/com/huachao/plugin/listener/AmazingDocumentListener.java: -------------------------------------------------------------------------------- 1 | package com.huachao.plugin.listener; 2 | 3 | 4 | import com.huachao.plugin.util.CharPanel; 5 | import com.huachao.plugin.util.GlobalVar; 6 | import com.intellij.openapi.editor.CaretModel; 7 | import com.intellij.openapi.editor.Editor; 8 | import com.intellij.openapi.editor.event.DocumentEvent; 9 | import com.intellij.openapi.editor.event.DocumentListener; 10 | import com.intellij.openapi.fileEditor.FileEditorManager; 11 | import com.intellij.openapi.project.Project; 12 | 13 | import javax.swing.*; 14 | 15 | /** 16 | * Created by huachao on 2016/12/27. 17 | */ 18 | public class AmazingDocumentListener implements DocumentListener { 19 | private Project mProject; 20 | private Editor mEditor; 21 | 22 | private AmazingCaretListener caretListener; 23 | 24 | 25 | public AmazingDocumentListener(Project project) { 26 | mProject = project; 27 | //添加光标移动监听器 28 | caretListener = new AmazingCaretListener(); 29 | } 30 | 31 | @Override 32 | public void beforeDocumentChange(DocumentEvent documentEvent) { 33 | 34 | 35 | if (mEditor == null) { 36 | mEditor = FileEditorManager.getInstance(mProject).getSelectedTextEditor(); 37 | if (mEditor == null) 38 | return; 39 | } 40 | 41 | //添加光标移动监听器 42 | CaretModel caretModel = mEditor.getCaretModel(); 43 | caretModel.addCaretListener(caretListener); 44 | 45 | //更新全局变量 46 | GlobalVar.updateGlobalVar(mEditor); 47 | 48 | 49 | JComponent editorComponent = mEditor.getContentComponent(); 50 | CharPanel charPanel = CharPanel.getInstance(editorComponent); 51 | //删除字符串 52 | String deleteStr = documentEvent.getOldFragment().toString().trim(); 53 | if (deleteStr.length() > 0) { 54 | charPanel.addStrToList(deleteStr, false); 55 | } 56 | //添加字符串 57 | String newStr = documentEvent.getNewFragment().toString().trim(); 58 | if (newStr.length() > 0) { 59 | charPanel.addStrToList(newStr, true); 60 | } 61 | } 62 | 63 | @Override 64 | public void documentChanged(DocumentEvent documentEvent) { 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/com/huachao/plugin/action/RandomColorAction.java: -------------------------------------------------------------------------------- 1 | package com.huachao.plugin.action; 2 | 3 | import com.huachao.plugin.util.GlobalVar; 4 | import com.intellij.icons.AllIcons; 5 | import com.intellij.openapi.actionSystem.AnAction; 6 | import com.intellij.openapi.actionSystem.AnActionEvent; 7 | import com.intellij.openapi.actionSystem.PlatformDataKeys; 8 | import com.intellij.openapi.actionSystem.Presentation; 9 | import com.intellij.openapi.editor.Editor; 10 | import com.intellij.openapi.project.Project; 11 | 12 | import javax.swing.*; 13 | 14 | /** 15 | * Created by HuaChao on 2016/12/27. 16 | */ 17 | public class RandomColorAction extends AnAction { 18 | private GlobalVar.State state = GlobalVar.getInstance().state; 19 | 20 | @Override 21 | public void update(AnActionEvent e) { 22 | Project project = e.getData(PlatformDataKeys.PROJECT); 23 | Editor editor = e.getData(PlatformDataKeys.EDITOR); 24 | if (editor == null || project == null) { 25 | e.getPresentation().setEnabled(false); 26 | } else { 27 | JComponent component = editor.getContentComponent(); 28 | if (component == null) { 29 | e.getPresentation().setEnabled(false); 30 | } else { 31 | e.getPresentation().setEnabled(true); 32 | } 33 | } 34 | updateState(e.getPresentation()); 35 | } 36 | 37 | @Override 38 | public void actionPerformed(AnActionEvent e) { 39 | Project project = e.getData(PlatformDataKeys.PROJECT); 40 | Editor editor = e.getData(PlatformDataKeys.EDITOR); 41 | if (editor == null || project == null) { 42 | return; 43 | } 44 | JComponent component = editor.getContentComponent(); 45 | if (component == null) 46 | return; 47 | state.IS_RANDOM = !state.IS_RANDOM; 48 | updateState(e.getPresentation()); 49 | 50 | GlobalVar.registerDocumentListener(project, editor,false); 51 | } 52 | 53 | private void updateState(Presentation presentation) { 54 | 55 | if (state.IS_RANDOM) { 56 | presentation.setIcon(AllIcons.General.InspectionsOK); 57 | } else { 58 | presentation.setIcon(AllIcons.Actions.Cancel); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/com/huachao/plugin/action/EnableAction.java: -------------------------------------------------------------------------------- 1 | package com.huachao.plugin.action; 2 | 3 | import com.btr.proxy.util.PlatformUtil; 4 | import com.huachao.plugin.listener.AmazingDocumentListener; 5 | import com.huachao.plugin.util.CharPanel; 6 | import com.huachao.plugin.util.GlobalVar; 7 | import com.intellij.icons.AllIcons; 8 | import com.intellij.openapi.actionSystem.*; 9 | import com.intellij.openapi.editor.Document; 10 | import com.intellij.openapi.editor.Editor; 11 | import com.intellij.openapi.editor.colors.EditorFontType; 12 | import com.intellij.openapi.fileEditor.FileEditorManager; 13 | import com.intellij.openapi.project.Project; 14 | import com.intellij.ui.ComboBoxCompositeEditor; 15 | 16 | import javax.swing.*; 17 | import java.awt.*; 18 | 19 | /** 20 | * Created by huachao on 2016/12/27. 21 | */ 22 | public class EnableAction extends AnAction { 23 | private GlobalVar.State state = GlobalVar.getInstance().state; 24 | 25 | public EnableAction() { 26 | 27 | } 28 | 29 | @Override 30 | public void update(AnActionEvent e) { 31 | Project project = e.getData(PlatformDataKeys.PROJECT); 32 | Editor editor = e.getData(PlatformDataKeys.EDITOR); 33 | if (editor == null || project == null) { 34 | e.getPresentation().setEnabled(false); 35 | } else { 36 | JComponent component = editor.getContentComponent(); 37 | if (component == null) { 38 | e.getPresentation().setEnabled(false); 39 | } else { 40 | e.getPresentation().setEnabled(true); 41 | } 42 | } 43 | updateState(e.getPresentation()); 44 | } 45 | 46 | @Override 47 | public void actionPerformed(AnActionEvent e) { 48 | Project project = e.getData(PlatformDataKeys.PROJECT); 49 | Editor editor = e.getData(PlatformDataKeys.EDITOR); 50 | if (editor == null || project == null) { 51 | return; 52 | } 53 | JComponent component = editor.getContentComponent(); 54 | if (component == null) 55 | return; 56 | state.IS_ENABLE = !state.IS_ENABLE; 57 | updateState(e.getPresentation()); 58 | 59 | //只要点击Enable项,就把缓存中所有的文本清理 60 | CharPanel.getInstance(component).clearAllStr(); 61 | 62 | GlobalVar.registerDocumentListener(project, editor, state.IS_ENABLE); 63 | } 64 | 65 | 66 | private void updateState(Presentation presentation) { 67 | 68 | if (state.IS_ENABLE) { 69 | presentation.setText("Enable"); 70 | presentation.setIcon(AllIcons.General.InspectionsOK); 71 | } else { 72 | presentation.setText("Disable"); 73 | presentation.setIcon(AllIcons.Actions.Cancel); 74 | } 75 | } 76 | 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/com/huachao/plugin/util/GlobalVar.java: -------------------------------------------------------------------------------- 1 | package com.huachao.plugin.util; 2 | 3 | import com.huachao.plugin.listener.AmazingDocumentListener; 4 | import com.huachao.plugin.listener.AmazingTypedActionHandler; 5 | import com.intellij.openapi.components.PersistentStateComponent; 6 | import com.intellij.openapi.components.ServiceManager; 7 | import com.intellij.openapi.components.State; 8 | import com.intellij.openapi.components.Storage; 9 | import com.intellij.openapi.editor.Document; 10 | import com.intellij.openapi.editor.Editor; 11 | import com.intellij.openapi.editor.actionSystem.EditorActionManager; 12 | import com.intellij.openapi.editor.actionSystem.TypedAction; 13 | import com.intellij.openapi.editor.actionSystem.TypedActionHandler; 14 | import com.intellij.openapi.editor.colors.EditorFontType; 15 | import com.intellij.openapi.project.Project; 16 | import org.jetbrains.annotations.Nullable; 17 | 18 | import javax.swing.*; 19 | import java.awt.*; 20 | 21 | /** 22 | * 配置文件 23 | * Created by huachao on 2016/12/27. 24 | */ 25 | @State( 26 | name = "amazing-mode", 27 | storages = { 28 | @Storage( 29 | id = "amazing-mode", 30 | file = "$APP_CONFIG$/amazing-mode_setting.xml" 31 | ) 32 | } 33 | ) 34 | public class GlobalVar implements PersistentStateComponent { 35 | //全局共享 36 | public static Font font; 37 | public static Color defaultForgroundColor; 38 | public static int minTextHeight; 39 | public static boolean hasAddListener = false; 40 | 41 | 42 | private static AmazingTypedActionHandler typedActionHandler; 43 | 44 | static { 45 | //添加按键监听 46 | EditorActionManager actionManager = EditorActionManager.getInstance(); 47 | TypedAction typedAction = actionManager.getTypedAction(); 48 | TypedActionHandler lastHandler = typedAction.getHandler(); 49 | typedActionHandler = new AmazingTypedActionHandler(lastHandler); 50 | typedAction.setupHandler(typedActionHandler); 51 | } 52 | 53 | //当Editor有可能发生变化时,应当调用一次updateGlobalVar 54 | public static void updateGlobalVar(Editor editor) { 55 | font = editor.getColorsScheme().getFont(EditorFontType.PLAIN); 56 | defaultForgroundColor = editor.getColorsScheme().getDefaultForeground(); 57 | minTextHeight = new Label("A").getFontMetrics(GlobalVar.font).getHeight(); 58 | } 59 | 60 | private static AmazingDocumentListener amazingDocumentListener = null; 61 | 62 | public static void registerDocumentListener(Project project, Editor editor, boolean isFromEnableAction) { 63 | if (!hasAddListener || isFromEnableAction) { 64 | hasAddListener = true; 65 | JComponent component = editor.getContentComponent(); 66 | if (component == null) 67 | return; 68 | if (amazingDocumentListener == null) { 69 | 70 | amazingDocumentListener = new AmazingDocumentListener(project); 71 | Document document = editor.getDocument(); 72 | document.addDocumentListener(amazingDocumentListener); 73 | } 74 | 75 | Thread thread = new Thread(CharPanel.getInstance(component)); 76 | thread.start(); 77 | } 78 | } 79 | 80 | 81 | //--------------------// 82 | //以下为本地状态保存相关代码 83 | public static final class State { 84 | public boolean IS_ENABLE; 85 | public boolean IS_RANDOM; 86 | } 87 | 88 | @Nullable 89 | @Override 90 | public State getState() { 91 | return this.state; 92 | } 93 | 94 | @Override 95 | public void loadState(State state) { 96 | this.state = state; 97 | } 98 | 99 | public State state = new State(); 100 | 101 | public GlobalVar() { 102 | 103 | state.IS_ENABLE = false; 104 | state.IS_RANDOM = false; 105 | } 106 | 107 | public static GlobalVar getInstance() { 108 | return ServiceManager.getService(GlobalVar.class); 109 | } 110 | 111 | } 112 | 113 | -------------------------------------------------------------------------------- /src/com/huachao/plugin/util/CharPanel.java: -------------------------------------------------------------------------------- 1 | package com.huachao.plugin.util; 2 | 3 | import com.huachao.plugin.Entity.CharObj; 4 | 5 | import javax.swing.*; 6 | import java.awt.*; 7 | import java.util.*; 8 | import java.util.List; 9 | 10 | /** 11 | * Created by huachao on 2016/12/27. 12 | */ 13 | public class CharPanel implements Runnable { 14 | private JComponent mComponent; 15 | private Point mCurPosition; 16 | private Set charSet = new HashSet(); 17 | private List bufferList = new ArrayList(); 18 | 19 | 20 | private GlobalVar.State state = GlobalVar.getInstance().state; 21 | 22 | 23 | public void setComponent(JComponent component) { 24 | mComponent = component; 25 | } 26 | 27 | 28 | public void run() { 29 | while (state.IS_ENABLE) { 30 | if (GlobalVar.font != null) { 31 | synchronized (bufferList) { 32 | charSet.addAll(bufferList); 33 | bufferList.clear(); 34 | } 35 | draw(); 36 | int minFontSize = GlobalVar.font.getSize(); 37 | 38 | //修改各个Label的属性,使之能以动画形式出现和消失 39 | Iterator it = charSet.iterator(); 40 | while (it.hasNext()) { 41 | CharObj obj = it.next(); 42 | if (obj.isAdd()) {//如果是添加到文本框 43 | if (obj.getSize() <= minFontSize) {//当字体大小到达最小后,使之消失 44 | mComponent.remove(obj.getLabel()); 45 | it.remove(); 46 | } else {//否则,继续减小 47 | int size = obj.getSize() - 6 < minFontSize ? minFontSize : (obj.getSize() - 6); 48 | obj.setSize(size); 49 | } 50 | } else {//如果是从文本框中删除 51 | Point p = obj.getPosition(); 52 | if (p.y <= 0 || obj.getSize() <= 0) {//如果到达最底下,则清理 53 | mComponent.remove(obj.getLabel()); 54 | it.remove(); 55 | } else { 56 | p.y = p.y - 10; 57 | int size = obj.getSize() - 1 < 0 ? 0 : (obj.getSize() - 1); 58 | obj.setSize(size); 59 | } 60 | } 61 | } 62 | 63 | } 64 | try { 65 | if (charSet.isEmpty()) { 66 | synchronized (charSet) { 67 | charSet.wait(); 68 | } 69 | } 70 | Thread.currentThread().sleep(50); 71 | } catch (InterruptedException e) { 72 | e.printStackTrace(); 73 | } 74 | } 75 | } 76 | 77 | //绘制文本,本质上只是修改各个文本的位置和字体大小 78 | private void draw() { 79 | if (mComponent == null) 80 | return; 81 | 82 | for (CharObj obj : charSet) { 83 | JLabel label = obj.getLabel(); 84 | 85 | Font font = new Font(GlobalVar.font.getName(), GlobalVar.font.getStyle(), obj.getSize()); 86 | 87 | label.setFont(font); 88 | FontMetrics metrics = label.getFontMetrics(label.getFont()); 89 | int textH = metrics.getHeight(); //字符串的高, 只和字体有关 90 | int textW = metrics.stringWidth(label.getText()); //字符串的宽 91 | label.setBounds(obj.getPosition().x, obj.getPosition().y - (textH - GlobalVar.minTextHeight), textW, textH); 92 | } 93 | mComponent.invalidate(); 94 | } 95 | 96 | public void clearAllStr() { 97 | synchronized (bufferList) { 98 | bufferList.clear(); 99 | charSet.clear(); 100 | 101 | Iterator setIt = charSet.iterator(); 102 | while (setIt.hasNext()) { 103 | CharObj obj = setIt.next(); 104 | mComponent.remove(obj.getLabel()); 105 | } 106 | 107 | Iterator bufferIt = bufferList.iterator(); 108 | while (bufferIt.hasNext()) { 109 | CharObj obj = bufferIt.next(); 110 | mComponent.remove(obj.getLabel()); 111 | } 112 | 113 | System.out.println("清理 "); 114 | 115 | } 116 | } 117 | 118 | //单例模式,静态内部类 119 | private static class SingletonHolder { 120 | //静态初始化器,由JVM来保证线程安全 121 | private static CharPanel instance = new CharPanel(); 122 | } 123 | 124 | //返回单例对象 125 | public static CharPanel getInstance(JComponent component) { 126 | if (component != null) { 127 | SingletonHolder.instance.mComponent = component; 128 | } 129 | return SingletonHolder.instance; 130 | } 131 | 132 | //由光标监听器回调,由此可动态获取当前光标位置 133 | public void setPosition(Point position) { 134 | this.mCurPosition = position; 135 | } 136 | 137 | /** 138 | * 将字符串添加到列表中。 139 | * 140 | * @isAdd 如果为true表示十新增字符串,否则为被删除字符串 141 | * @str 字符串 142 | */ 143 | public void addStrToList(String str, boolean isAdd) { 144 | if (mComponent != null && mCurPosition != null) { 145 | 146 | CharObj charObj = new CharObj(mCurPosition.y); 147 | JLabel label = new JLabel(str); 148 | charObj.setStr(str); 149 | charObj.setAdd(isAdd); 150 | charObj.setLabel(label); 151 | if (isAdd) 152 | charObj.setSize(60); 153 | else 154 | charObj.setSize(GlobalVar.font.getSize()); 155 | charObj.setPosition(mCurPosition); 156 | if (state.IS_RANDOM) { 157 | label.setForeground(randomColor()); 158 | } else { 159 | label.setForeground(GlobalVar.defaultForgroundColor); 160 | } 161 | synchronized (bufferList) { 162 | bufferList.add(charObj); 163 | } 164 | if (charSet.isEmpty()) { 165 | synchronized (charSet) { 166 | charSet.notify(); 167 | } 168 | } 169 | 170 | mComponent.add(label); 171 | } 172 | } 173 | 174 | //以下用于产生随机颜色 175 | private static final Color[] COLORS = {Color.GREEN, Color.BLACK, Color.BLUE, Color.ORANGE, Color.YELLOW, Color.RED, Color.CYAN, Color.MAGENTA}; 176 | 177 | private Color randomColor() { 178 | int max = COLORS.length; 179 | int index = new Random().nextInt(max); 180 | return COLORS[index]; 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 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 | 147 | 148 | 154 | 155 | 156 | 157 | mCurPosition 158 | mProject 159 | 160 | 161 | 162 | 164 | 165 | 168 | 169 | 170 | 184 | 185 | 186 | 192 | 193 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 222 | 223 | 226 | 227 | 228 | 229 | 232 | 233 | 236 | 237 | 240 | 241 | 244 | 245 | 248 | 249 | 250 | 251 | 254 | 255 | 258 | 259 | 262 | 263 | 266 | 267 | 270 | 271 | 272 | 273 | 276 | 277 | 280 | 281 | 284 | 285 | 288 | 289 | 292 | 293 | 294 | 295 | 298 | 299 | 302 | 303 | 306 | 307 | 310 | 311 | 314 | 315 | 316 | 317 | 320 | 321 | 324 | 325 | 328 | 329 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 350 | 351 | 352 | 378 | 379 | 380 | 405 | 406 | 413 | 414 | 415 | 428 | 429 | 430 | 437 | 440 | 442 | 443 | 444 | 445 | 446 | 447 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 486 | 487 | 488 | 499 | 500 | 501 | 511 | 512 | 519 | 520 | 521 | 522 | 540 | 547 | 548 | 549 | 554 | 555 | 556 | 557 | 558 | 559 | 561 | 562 | 563 | 564 | 1482803335162 565 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 617 | 618 | 619 | 620 | 621 | 622 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | --------------------------------------------------------------------------------