上文回顧:
定制Eclipse IDE之功能篇(一)
這一篇文章將記錄一些Eclipse插件小功能,Smart but Useful。
一、設置工作空間 文本文件的編碼
解決辦法:
在org.eclipse.ui.startup拓展里執行這一句(只需執行一次):
ResourcesPlugin.getPlugin().getPluginPreferences().setValue("encoding", "UTF-8");
二、默認顯示行號
解決辦法:
在org.eclipse.ui.startup拓展里執行這一句(只需執行一次):
EditorsPlugin.getDefault().getPreferenceStore().setValue("lineNumberRuler", "true");
三、Combo控件的顯示label獲取value
有可能我們要在Combo控件顯示label,但獲取值的時候拿到value。
解決辦法:
設置label和value:
Combo combo = (Combo)control; combo.removeAll(); for (int i = 0; i < list.size(); i++) { DeviceInfo obj=list.get(i); combo.add(obj.getName()); //label combo.setData(i +"", obj.getSerialNumber()); //value }
獲取value:
String key = "" + comboDevice.getSelectionIndex();
String value= String.valueOf(comboDevice.getData(key));
四、寫文件,生成文件編碼問題
一開始我這樣寫文件,但發現另外插件讀取這文件時(以UTF-8 ),亂碼了(檢查生成的文件編碼是ANSI):
PrintWriter pw = new PrintWriter(new FileWriter(filePath)); pw.print(content); pw.close();
解決辦法;
OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"); outputStream.write(content); outputStream.close();
五、System.out.println可以在控制台顯示
如果你的插件沒有做什么處理,那你插件里面System.out.println輸出的內容是不會在控制台顯示。
解決辦法:
import java.io.PrintStream; import org.eclipse.ui.console.ConsolePlugin; import org.eclipse.ui.console.IConsole; import org.eclipse.ui.console.IConsoleFactory; import org.eclipse.ui.console.IConsoleManager; import org.eclipse.ui.console.MessageConsole; import org.eclipse.ui.console.MessageConsoleStream; public class ConsoleFactory implements IConsoleFactory { static MessageConsole console = new MessageConsole("console log",null); public void openConsole() { showConsole(); } public static void showConsole() { if (console != null) { IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager(); IConsole[] existing = manager.getConsoles(); boolean exists = false; for (int i = 0; i < existing.length; i++) { if (console == existing[i]) exists = true; } if (!exists) { manager.addConsoles(new IConsole[] { console }); } manager.showConsoleView(console); MessageConsoleStream stream = console.newMessageStream(); System.setOut(new PrintStream(stream)); } } public static void closeConsole() { IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager(); if (console != null) { manager.removeConsoles(new IConsole[] { console }); } } public static MessageConsole getConsole() { return console; } }
這個是我從網上找到的類,另外在System.out.println之前調用ConsoleFactory.showConsole();
六、隱藏quickAccess
有時我們並不想顯示右上角那個quickAccess,我們想辦法去隱藏,並不是說手動關閉。
解決辦法:
在org.eclipse.ui.startup拓展里執行(每次打開eclipse都執行):
UIJob jobH = new UIJob("hide quick access") { @Override public IStatus runInUIThread(IProgressMonitor monitor) { IWorkbenchWindow window = PlatformUI.getWorkbench() .getActiveWorkbenchWindow(); if (window == null) return Status.CANCEL_STATUS; if (window instanceof WorkbenchWindow) { MTrimBar topTrim = ((WorkbenchWindow) window).getTopTrim(); for (MTrimElement element : topTrim.getChildren()) { if ("SearchField".equals(element.getElementId())) { Control contorl = (Control) element.getWidget(); contorl.setVisible(false); break; } } } return Status.OK_STATUS; } }; jobH.schedule(0L);
PS:沒有找到一勞永逸的辦法,網上傳說的用樣式可以隱藏是不行的。
七、文件自動更新
我這里說的是文件自動更新,並不是說eclipse自動更新,可以說只是更新部分eclipse內容。這里主要談的是一種簡單檢查更新的辦法,無后端服務實現。
服務端:
僅僅只是在服務器里面放這些資源,而里面的版本由一個version.properties決定,每一個版本對應一條記錄。
客戶端:
每次打開eclipse時,自動去下載遠端version.properties文件,比對本地的version.properties文件。當有新的版本或者版本后面的時間戳有變更時候下載覆蓋本地的文件。
功能篇就先到這里,其他篇章待續。
本文為原創文章,轉載請保留原出處,方便溯源,如有錯誤地方,謝謝指正。
本文地址 : http://www.cnblogs.com/lovesong/p/4694522.html
本文地址 : http://www.cnblogs.com/lovesong/p/4694522.html
