View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.jface;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.jelly.JellyTagException;
21  import org.apache.commons.jelly.MissingAttributeException;
22  import org.apache.commons.jelly.XMLOutput;
23  import org.apache.commons.jelly.tags.core.UseBeanTag;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.eclipse.jface.action.Action;
27  import org.eclipse.jface.action.IContributionManager;
28  import org.eclipse.swt.widgets.Event;
29  
30  /***
31   * This tag creates an JFace Action
32   *
33   * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster</a>
34   */
35  public class ActionTag extends UseBeanTag {
36  
37      /***
38       *  Implementing Action class
39       */
40      class ActionImpl extends Action {
41          public void runWithEvent(Event event) {
42              try {
43                  context.setVariable(var, event);
44                  invokeBody(output);
45              } catch (Exception e) {
46                  log.error(
47                      "Caught exception: "
48                          + e
49                          + " while processing event: "
50                          + event,
51                      e);
52              }
53          }
54      }
55  
56      /*** The Log to which logging calls will be made. */
57      private static final Log log = LogFactory.getLog(ActionTag.class);
58      private XMLOutput output;
59      private String var = "event";
60  
61      /***
62       * @param arg0
63       */
64      public ActionTag(Class arg0) {
65          super(arg0);
66      }
67  
68      /***
69       * @return IContributionManager
70       */
71      protected IContributionManager getParentContributionManager() {
72          MenuManagerTag tag =
73              (MenuManagerTag) findAncestorWithClass(MenuManagerTag.class);
74          if (tag != null) {
75              return tag.getMenuManager();
76          }
77          return null;
78      }
79  
80      /***
81        * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
82        */
83      public void doTag(XMLOutput output)
84          throws MissingAttributeException, JellyTagException {
85  
86          Map attributes = getAttributes();
87  
88          Action action = new ActionImpl();
89          setBeanProperties(action, attributes);
90  
91          IContributionManager cm = getParentContributionManager();
92          if (cm != null) {
93              cm.add(action);
94          }
95  
96          this.output = output;
97      }
98  
99  }