林海學苑

          java學習交流

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            12 隨筆 :: 0 文章 :: 1 評論 :: 0 Trackbacks
          這兩天在因工作上的要求,在學習使用StyledText,網上搜了些相關資料,但都不很全,其中八進制的實現內容助理(1. 自動完成) ”見http://www.cnblogs.com/bjzhanghao/archive/2007/09/28/908648.html
          還是很有用的,還有增加重做,取消功能等,還有著色等,我這里綜合了一下,自己寫了一個測試類,實現了代碼助理和著色,加了一個右鍵菜單,完成結果圖如下:

          代碼如下:(utf - 8)
          package study.plug.wang.popup.dialog;

          import java.util.ArrayList;
          import java.util.Arrays;
          import java.util.HashMap;
          import java.util.List;
          import java.util.Vector;

          import org.eclipse.jface.action.Action;
          import org.eclipse.jface.action.StatusLineManager;
          import org.eclipse.jface.action.ToolBarManager;
          import org.eclipse.jface.dialogs.MessageDialog;
          import org.eclipse.jface.text.Document;
          import org.eclipse.jface.text.IDocument;
          import org.eclipse.jface.text.ITextViewer;
          import org.eclipse.jface.text.TextViewer;
          import org.eclipse.jface.text.contentassist.CompletionProposal;
          import org.eclipse.jface.text.contentassist.ContentAssistant;
          import org.eclipse.jface.text.contentassist.ICompletionProposal;
          import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
          import org.eclipse.jface.text.contentassist.IContentAssistant;
          import org.eclipse.jface.text.contentassist.IContextInformation;
          import org.eclipse.jface.text.contentassist.IContextInformationValidator;
          import org.eclipse.jface.text.source.ISourceViewer;
          import org.eclipse.jface.text.source.SourceViewer;
          import org.eclipse.jface.text.source.SourceViewerConfiguration;
          import org.eclipse.jface.window.ApplicationWindow;
          import org.eclipse.swt.SWT;
          import org.eclipse.swt.awt.SWT_AWT;
          import org.eclipse.swt.custom.CTabFolder;
          import org.eclipse.swt.custom.CTabItem;
          import org.eclipse.swt.custom.SashForm;
          import org.eclipse.swt.custom.StyleRange;
          import org.eclipse.swt.custom.StyledText;
          import org.eclipse.swt.custom.VerifyKeyListener;
          import org.eclipse.swt.events.SelectionEvent;
          import org.eclipse.swt.events.SelectionListener;
          import org.eclipse.swt.events.VerifyEvent;
          import org.eclipse.swt.graphics.Color;
          import org.eclipse.swt.graphics.Point;
          import org.eclipse.swt.layout.FillLayout;
          import org.eclipse.swt.layout.GridData;
          import org.eclipse.swt.layout.GridLayout;
          import org.eclipse.swt.widgets.Composite;
          import org.eclipse.swt.widgets.Control;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Menu;
          import org.eclipse.swt.widgets.MenuItem;
          import org.eclipse.swt.widgets.Shell;

          /**
           * StyledText測試類
           *
           * 
          @author
           *
           
          */
          public class TextStyledText extends ApplicationWindow {

           
          private StyledText styledText;
           
          private Action actionRun;
           
          private SourceViewer sourceViewer_source;
           
          private List<String> optionList = new ArrayList<String>();

           
          /**
            * Create the application window
            
          */
           
          public TextStyledText() {
            
          super(null);
            createActions();
            addToolBar(SWT.FLAT 
          | SWT.WRAP);
            addMenuBar();
            addStatusLine();
           }

           
          /**
            * Create contents of the application window
            *
            * 
          @param parent
            
          */
           @Override
           
          protected Control createContents(Composite parent) {
            Composite container 
          = new Composite(parent, SWT.NONE);
            container.setLayout(
          new FillLayout());

            
          final SashForm sashForm = new SashForm(container, SWT.BORDER);
            sashForm.setOrientation(SWT.VERTICAL);

            sourceViewer_source 
          = new SourceViewer(sashForm, null, SWT.V_SCROLL | SWT.BORDER);

            sourceViewer_source.setDocument(
          new Document(""));
            sourceViewer_source.setEditable(
          true);

            
          final CTabFolder tabFolder = new CTabFolder(sashForm, SWT.NONE);

            
          final CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
            tabItem.setText(
          "結果");

            
          final Composite composite = new Composite(tabFolder, SWT.NONE);
            composite.setLayout(
          new GridLayout());
            tabItem.setControl(composite);

            
          final TextViewer textViewer_trage = new TextViewer(composite, SWT.BORDER);
            styledText 
          = textViewer_trage.getTextWidget();
            styledText.setLayoutData(
          new GridData(SWT.FILL, SWT.FILL, truetrue));
            sashForm.setWeights(
          new int[] { 11 });
            
          //

            initData();
            initListener();

            
          return container;
           }

           
          private void initData() {
            String[] options 
          = new String[] { "From""To""Table""order" };
            Arrays.sort(options);
          // 排序一下
            for (String option : options) {
             optionList.add(option.toLowerCase());
            }
            
          /*
             * 自動完成功能一般在以下兩種條件下彈出一個小窗口向用戶提示當前可供選擇的選項,一是用戶按下指定的組合鍵時,二是用戶輸入了特定的字符時, SourceViewer支持這兩種觸發(fā)方式。在程序里使用SourceViewer和使用一般控件沒有很大的分別,只是SourceViewer
             * 是StyledText的包裝,所以一些操作要通過getTextWidget()完成,如下所示:
             
          */
            
          // Configure source viewer, add content assistant support
            sourceViewer_source.configure(new SourceViewerConfiguration() {
             @Override
             
          public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
              ContentAssistant assistant 
          = new ContentAssistant();
              IContentAssistProcessor cap 
          = new MyContentAssistProcessor();
              assistant.setContentAssistProcessor(cap, IDocument.DEFAULT_CONTENT_TYPE);
              assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
              assistant.enableAutoActivation(
          true);
              
          return assistant;
             }
            });

           }

           
          private void initListener() {
            sourceViewer_source.appendVerifyKeyListener(
          new VerifyKeyListener() {
             
          public void verifyKey(VerifyEvent event) {
              
          // Check for Alt+/
              if (event.stateMask == SWT.ALT && event.character == '/') {
               
          // Check if source viewer is able to perform operation
               if (sourceViewer_source.canDoOperation(SourceViewer.CONTENTASSIST_PROPOSALS))
                
          // Perform operation
                sourceViewer_source.doOperation(SourceViewer.CONTENTASSIST_PROPOSALS);
               
          // Veto this key press to avoid further processing
               event.doit = false;
              } 
          else if (event.character == ' ') {// 輸入空格后著色
               String text = sourceViewer_source.getTextWidget().getText();
               
          if (text.length() > 0) {
                
          //text = text.substring(text.length() - 1);
                int p = text.lastIndexOf(" ");
                String rengText 
          = text;
                
          if (p != -1) {
                 rengText 
          = text.substring(p + 1);
                 p 
          = p+1;
                }
          else{
                 p 
          = 0;
                }
                
          if (rengText.length() > 0 && optionList.contains(rengText.toLowerCase())) {
                 Color red 
          = Display.getDefault().getSystemColor(SWT.COLOR_RED);
                 StyleRange sr 
          = new StyleRange(p, rengText.length(), red, null);
                 sourceViewer_source.getTextWidget().setStyleRange(sr);
                }
               }
              }
              
             }
            });
            Menu menu 
          = new Menu(sourceViewer_source.getTextWidget());
            MenuItem menuItem 
          = new MenuItem(menu, SWT.PUSH);
            menuItem.setText(
          "執(zhí)行");
            menuItem.addSelectionListener(
          new SelectionListener() {

             
          public void widgetDefaultSelected(SelectionEvent e) {
             }

             
          public void widgetSelected(SelectionEvent e) {
              runAction();
             }

            });

            sourceViewer_source.getTextWidget().setMenu(menu);
           }

           
          private void runAction() {
            MessageDialog.openInformation(getShell(), 
          "信息""還沒有做呢,在這增加自己的功能");
           }

           
          /**
            * Create the actions
            
          */
           
          private void createActions() {

            actionRun 
          = new Action("執(zhí)行") {
             
          public void run() {
              runAction();
             }
            };
            
          // Create the actions
           }

           
          /**
            * Create the toolbar manager
            *
            * 
          @return the toolbar manager
            
          */
           @Override
           
          protected ToolBarManager createToolBarManager(int style) {
            ToolBarManager toolBarManager 
          = new ToolBarManager(style);

            toolBarManager.add(actionRun);
            
          return toolBarManager;
           }

           
          /**
            * Create the status line manager
            *
            * 
          @return the status line manager
            
          */
           @Override
           
          protected StatusLineManager createStatusLineManager() {
            StatusLineManager statusLineManager 
          = new StatusLineManager();
            statusLineManager.setMessage(
          null"");
            
          return statusLineManager;
           }

           
          /**
            * Launch the application
            *
            * 
          @param args
            
          */
           
          public static void main(String args[]) {
            
          try {
             TextStyledText window 
          = new TextStyledText();
             window.setBlockOnOpen(
          true);
             window.open();
             Display.getCurrent().dispose();
            } 
          catch (Exception e) {
             e.printStackTrace();
            }
           }

           
          /**
            * Configure the shell
            *
            * 
          @param newShell
            
          */
           @Override
           
          protected void configureShell(Shell newShell) {
            
          super.configureShell(newShell);
            newShell.setText(
          "Text StyledText ");
           }

           
          /**
            * Return the initial size of the window
            
          */
           @Override
           
          protected Point getInitialSize() {
            
          return new Point(500375);
           }

           
          /**
            * 結果數組將作為彈出提示窗口里的選項
            *
            * 
          @author 王林海
            *
            
          */
           
          class MyContentAssistProcessor implements IContentAssistProcessor {

            
          public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {

             
          try {
              String text 
          = viewer.getTextWidget().getText();
              String[] options 
          = optionList.toArray(new String[optionList.size()]);

              
          if (text != null && text.length() > 0) {
               
          int p = text.lastIndexOf(" ");
               
          if (p != -1) {
                text 
          = text.substring(p + 1);
               }
               List
          <String> s = new ArrayList<String>();
               HashMap
          <String, String> h = new HashMap<String, String>();
               Vector
          <String> v = new Vector<String>(1);
               
          for (String option : options) {
                
          if (text.length() < option.length() && option.substring(0, text.length()).equalsIgnoreCase(text)) {
                 
          // v.add(option.substring(text.length()));
                 v.add(option);
                }
               }
               
          if (v.size() > 0) {
                options 
          = v.toArray(new String[v.size()]);
               } 
          else {
                
          return null;
               }
              }

              
          // Dynamically generate proposal
              ArrayList result = new ArrayList();
              
          for (int i = 0; i < options.length; i++) {
               
          // int len = options[i].length() - text.length();
               CompletionProposal proposal = new CompletionProposal(options[i], offset - text.length(), text.length(), options[i].length());
               result.add(proposal);
              }
              
          return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]);
             } 
          catch (Exception e) {
              e.printStackTrace();
             }
             
          return null;
            }

            
          public char[] getCompletionProposalAutoActivationCharacters() {
             
          return new char[] { 46 };// 點"."
            }

            
          public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
             
          return null;
            }

            
          public char[] getContextInformationAutoActivationCharacters() {
             
          return null;
            }

            
          public IContextInformationValidator getContextInformationValidator() {
             
          return null;
            }

            
          public String getErrorMessage() {
             
          return null;
            }

           }
          }

          posted on 2008-04-24 23:04 林海 閱讀(2986) 評論(0)  編輯  收藏 所屬分類: eclipse (插件)

          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
           
          主站蜘蛛池模板: 张家界市| 田东县| 大荔县| 璧山县| 白河县| 濮阳县| 昌江| 通城县| 汾阳市| 永平县| 奈曼旗| 浪卡子县| 佛教| 德清县| 寿宁县| 渑池县| 分宜县| 来安县| 肇源县| 泸州市| 横峰县| 肥西县| 龙井市| 武夷山市| 和顺县| 伊通| 绵阳市| 靖安县| 策勒县| 佛冈县| 庄河市| 奇台县| 晋宁县| 武邑县| 资阳市| 翁牛特旗| 三江| 凤城市| 泗阳县| 海阳市| 黄冈市|