블로그 이미지

GUEST

eclipse 2010. 8. 16. 18:31

eclipse command

rcp나 plugin에서 기능을 추가할 때 action이나 command를 사용한다.
action은 코딩이 간단하지만 UI와 handler가 1:1로 밀결합되고 image icon등이 고정되는 등의 단점이 있어서 command를 주로 사용한다.

plugin에서 command를 추가할 때는 3가지를 고려한다.

command
UI
handler

command
command는 기능에 대한 선언적 정의(declarative definition)이다.
command id를 선언하고 이 id를 menu와 handler에서 command id 항목으로 지정한다.
defaultHandler를 가질 수 있고 하나의 command에 여러 개의 handler를 지정할 수 있다.
그러나 한 시점에 하나의 handler만 활성화된다.

UI
menu를 추가한다. eclipse에서 추가한 기능을 사용하기 위한 interface 역할을 한다.
일반 메뉴, toolbar, view에서의 메뉴, view에서의 toolbar등이 된다.

handler
command의 동작을 구현한다.
activeWhen을 이용하여 활성화되는 시점을 결정할 수 있다.


eclipse toolbar에 올 수 있는 버튼이나 radio 등을 생각해 보자.
이러한 component들은 선택된 상태(또는 체크)에 대한 상태값이 필요하다.
상태값 - state 에 대한 것들은 command에서 지정한다.

example01 - state
<command 
      defaultHandler="com.eclipse_tips.commandstate.BoldHandler" 
      id="com.eclipse-tips.commandState.boldCommand" 
      name="Bold"> 
   <state 
         class="org.eclipse.ui.handlers.RegistryToggleState:true
         id="org.eclipse.ui.commands.toggleState"> 
   </state> 
</command> 

위와 같이 <state> 구분 안에서 eclipse에서 기정의해 놓은 class와 id를 사용할 수 있다. 소스에서는 아래와 같이 사용한다.

public Object execute(ExecutionEvent event) throws ExecutionException { 
     Command command = event.getCommand(); 
     boolean oldValue = HandlerUtil.toggleCommandState(command); 
     // use the old value and perform the operation

    return null; 
}


cf) 기본적으로 command의 state를 update하는 것은 handler의 책임이다. handler는 state를 알고 있으면서 외부에 알려줄 수 있어야 한다.
cf) ...RegistryToggleState:true 이렇게 사용할 수 있는 것은 RegistryToggleState가 IExecutableExtension 인터페이스를 구현하고 있기 때문이다.
':' 뒤의 값은 초기화할 때 사용한다.
또한 PersistentState를 상속받기 때문에 elipse session간 상태값이 남게 된다. eclipse를 재기동해도 state의 최종값이 반영되게 된다.


상황에 따라 다른 handler를 사용할 수 있는 것이 command의 장점이라고 했다. 만일 상황에 따라 다른 state를 사용해야 한다면 command를 <commandParameter>를 이용하여 나누어준다.

example02 - parameterize
<command 
      defaultHandler="com.eclipse_tips.commandstate.AlignHandler" 
      id="com.eclipse-tips.commandState.alignCommand" 
      name="Align Command"> 
   <commandParameter 
         id="org.eclipse.ui.commands.radioStateParameter
         name="State" 
         optional="false"> 
   </commandParameter> 
   <state 
         class="org.eclipse.ui.handlers.RadioState:left" 
         id="org.eclipse.ui.commands.radioState"> 
   </state> 
</command>

code에서는 아래와 같이 사용한다.

public Object execute(ExecutionEvent event) throws ExecutionException { 
    if(HandlerUtil.matchesRadioState(event)) 
        return null; // we are already in the updated state - do nothing 
    String currentState = event.getParameter(RadioState.PARAMETER_ID); 
    // perform task for current state 
    if(currentState.equals("left")) 
    // perform left alignment 
    else if(currentState.equals("center")) 
    // perform center alignment 
    // and so on ... 

    // and finally update the current state 
    HandlerUtil.updateRadioState(event.getCommand(), currentState);

    return null; 
}

,
TOTAL TODAY