关闭控制台的自动切换按钮

使用launch创建一个控制台后,公司提出新需求希望初始化关闭下图两个按钮,一个是当别的控制发生变化是自动切换控制台,第二个是当出现错误时自动切换错误视图,找到源码之后其实改源码试最方便,但是考虑到之后版本的升级,最好不改动源码,所以使用了反射来完成(对我这种半路出家的 ,工作以来就没写过反射,其实也很简单,只是有时候自己把他想难了)

插入部分代码:

private static ProcessConsolePageParticipant getProcessConsolePageParticipant(){
        DebugContextManager mananger =(DebugContextManager) DebugUITools.getDebugContextManager();
        Map<IWorkbenchWindow, DebugWindowContextService> list =(Map<IWorkbenchWindow, DebugWindowContextService>)reflexField(mananger,"fServices");
        DebugWindowContextService service = null;
        for (Map.Entry<IWorkbenchWindow, DebugWindowContextService> entry : list.entrySet()) {
            service = entry.getValue();
        }
        Map<String, ListenerList<IDebugContextListener>> map = (Map<String, ListenerList<IDebugContextListener>>)reflexField(service,"fListenersByPartId");
        ProcessConsolePageParticipant process = null;
        for (Map.Entry<String, ListenerList<IDebugContextListener>> entry : map.entrySet()) {
            ListenerList<IDebugContextListener> list1 = entry.getValue();
            Iterator<IDebugContextListener> iter = list1.iterator();
            while(iter.hasNext()){
                //如果存在,则调用next实现迭代
                IDebugContextListener iListener=(IDebugContextListener)iter.next();  
                if(iListener instanceof ProcessConsolePageParticipant){
                    process = (ProcessConsolePageParticipant)iListener;
                    break;
                }
            }
        }
        
        return process;
    }
    
    private static void runRemoveAction(){
        ProcessConsolePageParticipant process = getProcessConsolePageParticipant();
        ConsoleRemoveLaunchAction removeAction =(ConsoleRemoveLaunchAction)reflexField(process,"fRemoveTerminated");
        removeAction.run();
    }
    /**
     * 修改Action的状态
     */
    private static void modifyActionState(){
        ProcessConsolePageParticipant process = getProcessConsolePageParticipant();
        ShowStandardOutAction outAction =(ShowStandardOutAction)reflexField(process,"fStdOut");
        outAction.setChecked(false);
        ShowStandardErrorAction errorAction =(ShowStandardErrorAction)reflexField(process,"fStdErr");
        errorAction.setChecked(false);
    }
    
    private static Object reflexField(Object obj,String fieldName){
        Object val = null;
         try {
             Class clazz = obj.getClass(); 
             Field field = clazz.getDeclaredField(fieldName);
             field.setAccessible(true);
             val = field.get(obj);
            
         } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
         } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return val;
    }

猜你喜欢

转载自www.cnblogs.com/yaolei0422/p/10007958.html