West Farm
          吾本布衣,生于川北,躬耕于代碼的田地上。
          posts - 16,  comments - 15,  trackbacks - 0

          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 閱讀(935) | 評論 (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 閱讀(192) | 評論 (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 閱讀(315) | 評論 (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 閱讀(1544) | 評論 (2)編輯 收藏
          僅列出標題
          共2頁: 上一頁 1 2 

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 盐源县| 大英县| 米林县| 江达县| 泰兴市| 贺州市| 永年县| 宁国市| 丰县| 洪洞县| 宾阳县| 古蔺县| 南康市| 大城县| 永平县| 铜山县| 保亭| 平武县| 沂水县| 万全县| 桂林市| 广元市| 南丰县| 嘉定区| 从江县| 无极县| 新野县| 佛学| 诸暨市| 张北县| 赤峰市| 新干县| 望江县| 淮南市| 寿光市| 邯郸县| 阿拉尔市| 元朗区| 汉阴县| 潜江市| 高陵县|