java swt 快捷键,为SWT的StyledText添加Undo/Redo操作以及对快捷键动作绑定的支持

1 importorg.eclipse.core.commands.ExecutionException;2 importorg.eclipse.core.commands.operations.AbstractOperation;3 importorg.eclipse.core.commands.operations.IOperationHistory;4 importorg.eclipse.core.commands.operations.IUndoContext;5 importorg.eclipse.core.commands.operations.ObjectUndoContext;6 importorg.eclipse.core.commands.operations.OperationHistoryFactory;7 importorg.eclipse.core.runtime.IAdaptable;8 importorg.eclipse.core.runtime.IProgressMonitor;9 importorg.eclipse.core.runtime.IStatus;10 importorg.eclipse.core.runtime.Status;11 importorg.eclipse.swt.custom.ExtendedModifyEvent;12 importorg.eclipse.swt.custom.ExtendedModifyListener;13 importorg.eclipse.swt.custom.StyledText;14 15 /**16 * 管理Undo操作,用于监听文本域的文本改变事件,生成Undo操作并记录。
17 *18 *@authorqujinlong19 */20 publicclassUndoManager21 {22 /*23 * 用于存储历史Undo操作,每改变一次文本内容,就将构造一个Undo操作存入OperationHistory中。24 */25 privatefinalIOperationHistory opHistory;26 27 /*28 * Undo操作上下文,一般用于在OperationHistory中查找当前文本框的Undo操作。29 */30 privateIUndoContext undoContext=null;31 32 /*33 * 所要监听的需要实现Undo操作的文本框。34 */35 privateStyledText styledText=null;36 37 privateintundoLevel=0;38 39 publicUndoManager(intundoLevel)40 {41 opHistory=OperationHistoryFactory.getOperationHistory();42 43 setMaxUndoLevel(undoLevel);44 }45 46 publicvoidsetMaxUndoLevel(intundoLevel)47 {48 this.undoLevel=Math.max(0, undoLevel);49 50 if(isConnected())51 opHistory.setLimit(undoContext,this.undoLevel);52 }53 54 publicbooleanisConnected()55 {56 returnstyledText!=null;57 }58 59 /*60 * 将Undo管理器与指定的StyledText文本框相关联。61 */62 publicvoidconnect(StyledText styledText)63 {64 if(!isConnected()&&styledText!=null)65 {66 this.styledText=styledText;67 68 if(undoContext==null)69 undoContext=newObjectUndoContext(this);70 71 opHistory.setLimit(undoContext, undoLevel);72 opHistory.dispose(undoContext,true,true,false);73 74 addListeners();75 }76 }77 78 publicvoiddisconnect()79 {80 if(isConnected())81 {82 removeListeners();83 84 styledText=null;85 86 opHistory.dispose(undoContext,true,true,true);87 88 undoContext=null;89 }90 }91 92 privateExtendedModifyListener extendedModifyListener=null;93 94 privatebooleanisUndoing=false;95 96 /*97 * 向Styled中注册监听文本改变的监听器。98 *99 * 如果文本改变,就构造一个Undo操作压入Undo操作栈中。100 */101 privatevoidaddListeners()102 {103 if(styledText!=null)104 {105 extendedModifyListener=newExtendedModifyListener() {106 publicvoidmodifyText(ExtendedModifyEvent event)107 {108 if(isUndoing)109 return;110 111 String newText=styledText.getText().substring(event.start,112 event.start+event.length);113 114 UndoableOperation operation=newUndoableOperation(undoContext);115 116 operation.set(event.start, newText, event.replacedText);117 118 opHistory.add(operation);119 }120 };121 122 styledText.addExtendedModifyListener(extendedModifyListener);123 }124 }125 126 privatevoidremoveListeners()127 {128 if(styledText!=null)129 {130 if(extendedModifyListener!=null)131 {132 styledText.removeExtendedModifyListener(extendedModifyListener);133 134 extendedModifyListener=null;135 }136 }137 }138 139 publicvoidredo()140 {141 if(isConnected())142 {143 try144 {145 opHistory.redo(undoContext,null,null);146 }147 catch(ExecutionException ex)148 {149 }150 }151 }152 153 publicvoidundo()154 {155 if(isConnected())156 {157 try158 {159 opHistory.undo(undoContext,null,null);160 }161 catch(ExecutionException ex)162 {163 }164 }165 }166 167 /*168 * Undo操作用于记录StyledText的文本被改变时的相关数据。169 *170 * 比如文本框中本来的文本为111222333,如果此时选中222替换为444(用复制粘帖的方法),171 *172 * 则Undo操作中记录的相关数据为: startIndex = 3; newText = 444; replacedText = 222;173 */174 privateclassUndoableOperationextendsAbstractOperation175 {176 //记录Undo操作时,被替换文本的开始索引177 protectedintstartIndex=-1;178 179 //新输入的文本180 protectedString newText=null;181 182 //被替换掉的文本183 protectedString replacedText=null;184 185 publicUndoableOperation(IUndoContext context)186 {187 super("Undo-Redo");188 189 addContext(context);190 }191 192 /*193 * 设置Undo操作中要存储的相关数据。194 */195 publicvoidset(intstartIndex, String newText, String replacedText)196 {197 this.startIndex=startIndex;198 199 this.newText=newText;200 this.replacedText=replacedText;201 }202 203 /*204 * (non-Javadoc)205 *206 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor,207 *      org.eclipse.core.runtime.IAdaptable)208 */209 publicIStatus undo(IProgressMonitor monitor, IAdaptable info)210 throwsExecutionException211 {212 isUndoing=true;213 styledText.replaceTextRange(startIndex, newText.length(), replacedText);214 isUndoing=false;215 216 returnStatus.OK_STATUS;217 }218 219 /*220 * (non-Javadoc)221 *222 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor,223 *      org.eclipse.core.runtime.IAdaptable)224 */225 publicIStatus redo(IProgressMonitor monitor, IAdaptable info)226 throwsExecutionException227 {228 isUndoing=true;229 styledText.replaceTextRange(startIndex, replacedText.length(), newText);230 isUndoing=false;231 232 returnStatus.OK_STATUS;233 }234 235 /*236 * (non-Javadoc)237 *238 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor,239 *      org.eclipse.core.runtime.IAdaptable)240 */241 publicIStatus execute(IProgressMonitor monitor, IAdaptable info)242 throwsExecutionException243 {244 returnStatus.OK_STATUS;245 }246 }247 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值