본문 바로가기

개발

Java 자바 스윙 Swing이나 AWT의 이벤트 어댑터, 리스너 연동 연결 구현시 귀찮음을 해결해 주는 오픈소스 ActionConnector

* 자바 스윙이나 AWT의 이벤트 어댑터, 리스너 연결시 귀찮음을 해결해 주는 오픈소스 ActionConnector
* 간단하게 connect만을 통해 해결할 수 있다. 샘플만으로는 너무 간단하다.
* 소스를 직접 보완을 해 보겠지만, 가능하다면 윈도 어댑터나 액션 리스너 사용의 귀찮음을 줄여주는데 확실히 도움이 될 듯 하다.

* 예제 소스)
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.JMenuItem;
import org.openide.awt.Actions;
import org.openide.awt.Actions.ButtonActionConnector;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(
        service = ButtonActionConnector.class,
        position = 100)

public class MyButtonActionConnector implements ButtonActionConnector {

   @Override
   public boolean connect(AbstractButton button, Action action) {
      String text = (String)action.getValue("displayName");

      if (text != null) {
         button.setAction(action);
         button.setText(Actions.cutAmpersand(text));
         String desc = (String)action.getValue(Action.SHORT_DESCRIPTION);

         if (desc != null) {
            button.setToolTipText(desc);
         } else {
            button.setToolTipText((String)action.getValue(Action.NAME));
         }
         return true;
      }
      return false;
   }

   @Override
   public boolean connect(JMenuItem item, Action action, boolean popup) {
       return false; // use default implementation
   }
}

추가내용 설명)
org.openide.awt / Interface Actions.ButtonActionConnector

Enclosing class:Actions

public static interface Actions.ButtonActionConnector

SPI for supplying alternative implementation of connection between actions and presenters.
The implementations of this interface are being looked up in the default lookup. If there is no implemenation in the lookup the default implementation is used.
Since:org.openide.awt 6.9See Also:Lookup.getDefault()
  
Method Summary
boolean connect(AbstractButton button, Action action)
Connects the action to the supplied button.
 
boolean connect(JMenuItem item, Action action, boolean popup)
Connects the action to the supplied JMenuItem.
  
Method Detail

boolean connect(AbstractButton button, Action action)

Connects the action to the supplied button.
Returns:true if the connection was successful and no further actions are needed. If false is returned the default connect implementation is called
 
boolean connect(JMenuItem item, Action action, boolean popup)

Connects the action to the supplied JMenuItem.
Returns:true if the connection was successful and no further actions are needed. If false is returned the default connect implementation is called

Further reading:
http://bits.netbeans.org/dev/javadoc/org-openide-awt/org/openide/awt/Actions.ButtonActionConnector.html