zoukankan      html  css  js  c++  java
  • 定制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
  • 相关阅读:
    Ado.Net 实体框架学习笔记3
    Ado.Net 实体框架学习笔记1
    PV3D的小练习~太阳系八大行星
    AS3数组的应用,flash制作流星雨~
    电脑安全措施小贴士(摘)
    Windows下MySql批处理命令
    命令行批量改文件名
    汉字转拼音(asp)(摘录)
    sql server login与user的区别(摘)
    MySql四舍五入
  • 原文地址:https://www.cnblogs.com/lovesong/p/4694522.html
Copyright © 2011-2022 走看看