8.插件的國際化,可以參考nwpu.cdcsp.sbpel.diagram.part中messages.java的做法。
9.Text自動提示功能
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.fieldassist.AutoCompleteField;
import org.eclipse.jface.fieldassist.ComboContentAdapter;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
?
public class LaunchApp {
??? protected Shell shell;
?
??? private Text nameT;
??? private Combo cityC;
??? private Text remarksT;
?
??? /**
???? * Launch the application
???? * @param args
???? */
??? public static void main(String[] args) {
??????? try {
??????????? LaunchApp window = new LaunchApp();
??????????? window.open();
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??? }
?
??? /**
???? * Open the window
???? */
??? public void open() {
??????? final Display display = Display.getDefault();
??????? createContents();
??????? shell.open();
??????? shell.layout();
??????? while (!shell.isDisposed()) {
??????????? if (!display.readAndDispatch())
??????????????? display.sleep();
??????? }
??? }
?
??? /**
???? * Create contents of the window
???? */
??? protected void createContents() {
??????? shell = new Shell();
??????? final GridLayout gridLayout = new GridLayout();
??????? gridLayout.numColumns = 2;
??????? shell.setLayout(gridLayout);
??????? shell.setSize(226, 122);
??????? shell.setText("Field Assist");
?
??????? final Label nameL = new Label(shell, SWT.NONE);
??????? nameL.setText("姓名");
?
??????? nameT = new Text(shell, SWT.BORDER);
??????? nameT.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
?
??????? final Label cityL = new Label(shell, SWT.NONE);
??????? cityL.setText("城市");
?
??????? cityC = new Combo(shell, SWT.NONE);
???????cityC.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
???????
??????? final Label remarksL = new Label(shell, SWT.NONE);
??????? remarksL.setText("備注");
?
??????? remarksT = new Text(shell, SWT.BORDER);
??????? remarksT.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
???????
??????? //
??????? Dialog.applyDialogFont(this.shell);
???????
??????? //
??????? this.addNameTextFieldAssist();
??????? this.addCityComboFieldAssist();
??????? this.addRemarksTextFieldAssist();
??? }
?
??? /**
???? * 給名稱Text添加自動完成功能
???? */
??? private void addNameTextFieldAssist() {
??????? // 讓text可以進行代碼提示. 提示內容為: "aa", "BB", "無敵".
??????? // 注意: 不區分大小寫. [如: 輸入'b', 內容中會出現"BB"]
??????? new AutoCompleteField(nameT, new TextContentAdapter(), new String[]{"aa", "BB", "無敵"});
??? }
???
??? /**
???? * 給城市Combo添加自動完成功能
???? */
??? private void addCityComboFieldAssist() {
??????? // 讓combo可以代碼提示. 提示內容為: "BeiJing", "南京", "北京"
??????? new AutoCompleteField(cityC, new ComboContentAdapter(), new String[] {"BeiJing", "南京", "北京"});
??? }
???
??? /**
???? * 給備注Text添加自動完成功能
???? */
??? private void addRemarksTextFieldAssist() {
??????? // 下面使用ContentProposalAdapter,而沒有繼續使用AutoCompleteField.
???????// [去查看代碼你會發現:AutoCompleteFiled實現和下面的代碼幾乎一樣. ]
??????? // AutoCompleteFiled使用的同樣就將傳入的String[]去構造一個SimpleContentProposalProvider.
??????? // 但是,AutoCompleteFiled內部的ContentProposalAdapter是無法從外部得到的.
??????? // 所以,為了能夠自定義ContentProposalAdapter, 還必須將AutoCompleteField內部實現的代碼在外部再寫一遍.
??????? KeyStroke keyStroke = null; // null 表示不接受快捷鍵
??????? try {
??????????? keyStroke = KeyStroke.getInstance("Ctrl+1"); // 在text上按Ctrl+1彈出popup的shell.
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? ContentProposalAdapter adapter = new ContentProposalAdapter(remarksT, new TextContentAdapter(), new SimpleContentProposalProvider(new String[] {"one", "two", "three"}), keyStroke, new char[] {'.', ' '});
??????? adapter.setAutoActivationDelay(200); // 延時200ms
??????? adapter.setPropagateKeys(true); // 如果用戶的輸入值在內容列表中[比如輸入'o',而內容中有'one'],則彈出popup的shell
??????? adapter.setFilterStyle(ContentProposalAdapter.FILTER_CUMULATIVE); // 用戶同步輸入的內容也過濾列表[如:用戶輸入'o',則彈出popup的shell中的內容列表被過濾,其中都是'o'開頭的, 再輸入一個'n', 則內容列表中被過濾,只有以'on'開頭的]
??????? adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_INSERT); // 回寫插入
//??????? adapter.setLabelProvider(new LabelProvider() { // 可以不用指定LabelProvider. 如果指定,則不僅僅可以顯示Text, 還可以顯示Image.
//??????????? @Override
//??????????? public String getText(Object element) {
//??????????????? IContentProposal proposal = (IContentProposal) element;
//??????????????? return "XX" + proposal.getContent();
//??????????? }
//??????????? @Override
//??????????? public Image getImage(Object element) {
//??????????????? return super.getImage(element);
//??????????? }
//??????? });
???????
??? ????// 上面的代碼中使用的是SimpleContentProposalProvider, 則會用每個String去構造默認的一個IContentProposal,
??????? // 具體邏輯見: SimpleContentProposalProvider.makeContentProposal
???????
??????? // 請注意: 可以不用設置setLabelProvider的, 那么將會直接從IContentProposal中取label或content顯示.
??????? // 有labelProvider則從labelProvider得到在內容list中顯示的值.
??????? // 具體邏輯見: ContentProposalAdapter.getString()方法
//??????? if (labelProvider == null) {
//??????????? return proposal.getLabel() == null ? proposal.getContent() : proposal.getLabel();
//??????? }
//?????? ?return labelProvider.getText(proposal);
???????
??????? // 同樣的, 如果你添加了labelProvider, 那么也可以給每個IContentProposal返回Image.
??????? // 具體邏輯見: ContentProposalAdapter.getImage()方法
???????
??? }
?
???
??? // ContentProposalAdapter.setAutoActivationDelay 彈出popup的延遲時間
???
??? // ContentProposalAdapter.setPropagateKeys(true);
??? // 說明: 如果用戶敲入的字母在內容列表內時,是否彈出popup內容列表.
??? // true 彈出. 用戶輸入'o'也會彈出popup的shell. 輸入'.'也會彈出.
??? // false 不彈出. 用戶只有輸入'.'才彈出popup的shell. 輸入'o'等,不彈出.
???
??? // ContentProposalAdapter.setFilterStyle(ContentProposalAdapter.FILTER_*);
??? // 作用: 在用戶敲入字母的時候是否過濾popup彈出的shell里面的內容.
??? // ContentProposalAdapter.FILTER_NONE 不過濾. 說明: 下面的內容列表永遠不變.
??? // ContentProposalAdapter.FILTER_CHARACTER 只用一個輸入字符為條件過濾下面的內容列表. 說明:在輸入多個字符后,下面的內容列表會被清空.
??? // ContentProposalAdapter.FILTER_CUMULATIVE 隨著用戶輸入不停的過濾下面的內容列表. 注意在3.4后被@deprecated了. 說明: 隨著用戶的輸入,下面的內容一直在過濾
???
??? // ContentProposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_*);
??? // 說明: 用戶從popup的shell中得到的內容怎么回寫到控件上.
??? // ContentProposalAdapter.PROPOSAL_INSERT 插入.
??? // ContentProposalAdapter.PROPOSAL_REPLACE 覆蓋.
??? // ContentProposalAdapter.PROPOSAL_IGNORE 忽略. 應該叫追加比較合適.
?
?? ?
??? // TextContentAdapter只可以用于Text.
??? // ComboContentAdapter只可以用于Combo.
??? // 所以, 對于StyledText或Snipper等都需要自定義ContentAdapter.
???
}