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
  • 相关阅读:
    scala之伴生对象的继承
    scala之伴生对象说明
    “Failed to install the following Android SDK packages as some licences have not been accepted” 错误
    PATH 环境变量重复问题解决
    Ubuntu 18.04 配置java环境
    JDBC的基本使用2
    DCL的基本语法(授权)
    ZJNU 1374
    ZJNU 2184
    ZJNU 1334
  • 原文地址:https://www.cnblogs.com/lovesong/p/4694522.html
Copyright © 2011-2022 走看看