View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.rcp.binding;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.eclipse.core.commands.AbstractHandler;
22  import org.eclipse.core.commands.ExecutionEvent;
23  import org.eclipse.core.commands.ExecutionException;
24  import org.eclipse.core.commands.HandlerEvent;
25  import org.eclipse.core.commands.IHandler;
26  import org.eclipse.swt.widgets.Event;
27  import org.eclipse.swt.widgets.Listener;
28  import org.seasar.uruma.log.UrumaLogger;
29  import org.seasar.uruma.util.AssertionUtil;
30  
31  /**
32   * 任意のメソッドを呼び出すことができる、汎用的な {@link IHandler} の実装クラスです。<br />
33   * 
34   * @author y-komori
35   */
36  public class GenericHandler extends AbstractHandler {
37      protected static final UrumaLogger logger = UrumaLogger
38              .getLogger(GenericHandler.class);
39  
40      protected List<Listener> listeners = new ArrayList<Listener>();
41  
42      protected boolean enabled = true;
43  
44      /*
45       * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
46       */
47      @Override
48      public Object execute(final ExecutionEvent event) throws ExecutionException {
49          int size = listeners.size();
50          for (int i = 0; i < size; i++) {
51              try {
52                  listeners.get(i).handleEvent((Event) event.getTrigger());
53              } catch (Throwable ex) {
54                  logger.log(ex);
55              }
56          }
57          return null;
58      }
59  
60      /**
61       * {@link Listener} を追加します。<br />
62       * 
63       * @param listener
64       *            {@link Listener} オブジェクト
65       */
66      public void addListener(final Listener listener) {
67          AssertionUtil.assertNotNull("listener", listener);
68          this.listeners.add(listener);
69      }
70  
71      /*
72       * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
73       */
74      @Override
75      public boolean isEnabled() {
76          return enabled;
77      }
78  
79      /**
80       * ハンドラのイネーブル状態を変更します。<br />
81       * 
82       * @param enabled
83       *            <code>true</code> の場合、イネーブル。<code>false</code>
84       *            の場合、ディスエーブル。
85       */
86      public void setEnabled(final boolean enabled) {
87          if (this.enabled != enabled) {
88              this.enabled = enabled;
89              fireHandlerChanged(new HandlerEvent(this, true, false));
90          }
91      }
92  }