import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineStyleEvent;
import org.eclipse.swt.custom.LineStyleListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTResourceManager;
/**
* 关键字高亮编辑器。This class is a simple customized widget that wrappes a {@link org.eclipse.swt.custom.StyledText StyledText}.
* It consumes a keyword array and highlight them.
* @author ggfan@amarsoft
*
*/
public class KeywordsHighlightingEditor extends Composite{
private Color color = SWTResourceManager.getColor(SWT.COLOR_BLUE);
private Color variableColor = SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
private String[] keywords;
private StyledText st;
public void setKeywordsColor(Color color){
this.color = color;
}
public void setKeywordsBgColor(Color color){
}
public IObservableValue observerContent(){
return SWTObservables.observeText(st, SWT.Modify);
}
public KeywordsHighlightingEditor(Composite parent, String[] keywords) {
super(parent, SWT.NONE);
this.keywords = keywords;
this.setLayout(new FillLayout());
st = new StyledText(this, SWT.WRAP | SWT.BORDER | SWT.V_SCROLL);
// 禁止回车键换行
st.addVerifyKeyListener(new VerifyKeyListener(){
public void verifyKey(VerifyEvent event) {
if(event.keyCode == SWT.CR){
event.doit = false;
}
}
});
// Tab键失去焦点而不是插入制表符
st.addTraverseListener(new TraverseListener(){
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
e.doit = true;
}
}
});
st.addLineStyleListener(new SQLSegmentLineStyleListener());
}
private class SQLSegmentLineStyleListener implements LineStyleListener {
@Override
public void lineGetStyle(LineStyleEvent event) {
if(keywords == null || keywords.length == 0){
return;
}
List<StyleRange> styles = new ArrayList<StyleRange>();
int start = 0;
int length = event.lineText.length();
while (start < length) {
if (Character.isLetter(event.lineText.charAt(start))) {
StringBuffer buf = new StringBuffer();
int i = start;
for (; i < length && Character.isLetter(event.lineText.charAt(i)); i++) {
buf.append(event.lineText.charAt(i));
}
if(Arrays.asList(keywords).contains(buf.toString())) {
styles.add(new StyleRange(event.lineOffset + start, i - start, color, null, SWT.BOLD));
}
start = i;
}else if (event.lineText.charAt(start) == '#') {
StringBuffer buf = new StringBuffer();
buf.append('#');
int i = start + 1;
for (; i < length && Character.isLetter(event.lineText.charAt(i)); i++) {
buf.append(event.lineText.charAt(i));
}
if(buf.toString().matches("#[a-zA-Z]+\\d?")) {
styles.add(new StyleRange(event.lineOffset + start, i - start, variableColor, null, SWT.NORMAL));
}
start = i;
}
else{
start ++;
}
}
event.styles = (StyleRange[]) styles.toArray(new StyleRange[0]);
}
}
}
posted @
2011-10-12 10:42 West Farmer 阅读(924) |
评论 (0) |
编辑 收藏
http://stackoverflow.com
这个是英文的,比起国内的的一些编程问答网站不知道要强多少倍。
国内的问答类网站,各种答非所问,各种闲聊,各种复制粘贴。
do not google it before you think about it deep enough
do not ask before you google it
posted @
2011-10-11 17:02 West Farmer 阅读(178) |
评论 (0) |
编辑 收藏
有的时候应用程序会hold一个对象实例,随着时间的推移,该对象所含的数据可能发生变化(比如调用setter方法改变一个属性的值)。
那么如何明确相比于一个特定的时刻,某个对象实例中的数据发生了变化呢?
方法肯定不止一种,我的方法是:
public static String hashOf(Serializable object) throws IOException, NoSuchAlgorithmException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(baos);
oo.writeObject(object);
oo.flush();
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] data = baos.toByteArray();
oo.close();
baos.close();
messageDigest.update(data, 0, data.length);
BigInteger hash = new BigInteger(1, messageDigest.digest());
return String.format("%1$032X", hash);
}
说白了就是把一个对象实例看作byte数组,然后对这个byte数组计算MD5,如果MD5值一样就表示所含数据一致。
MD5算法不是完美的,但是在实际应用中已经足够的,你也可以使用CRC32。
欢迎指正。
posted @
2011-10-11 16:51 West Farmer 阅读(308) |
评论 (0) |
编辑 收藏
摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->package amarsoft.rcp.base.widgets;import java.io.File;import java.io.FileInputStream;im...
阅读全文
posted @
2011-10-11 16:28 West Farmer 阅读(1538) |
评论 (2) |
编辑 收藏