View Javadoc

1   /*
2    * Copyright 2004-2005 Andreas Andreou
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 net.sf.tapfx.components.outlookbar;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.tapestry.*;
21  import org.apache.tapestry.engine.IEngineService;
22  import org.apache.tapestry.engine.ILink;
23  
24  import net.sf.tapfx.components.TapFXBaseComponent;
25  
26  
27  /***
28   * This provides many services and renders
29   * an icon as well as its label.
30   * You might imagine this where the Mail,
31   * or Calendar icons in Outlook are.
32   * <p/>
33   * Yuck.. I know this could be prettier.
34   *
35   * @author Joshua Long (josh@joshlong.com)
36   */
37  public abstract class OutlookLink extends TapFXBaseComponent/*
38  implements IDirect */
39  {
40      private static final Log log =
41              LogFactory.getLog(OutlookPane.class);
42  
43      private String prefix;
44  
45      public static final String SELECTED_OUTLOOK_LINK_ID =
46              "SELECTED_OUTLOOK_LINK_ID_";
47  
48      public abstract String getPageName();
49      public abstract void setPageName(String page);
50  
51      public abstract String getDirectUrl();
52      public abstract void setDirectUrl(String directUrl);
53  
54      public abstract IActionListener getListener();
55      public abstract void setListener(IActionListener directLink);
56  
57      public abstract IAsset getImage();
58      public abstract void setImage(IAsset asset);
59  
60      public abstract String getImageUrl();
61      public abstract void setImageUrl(String imageUrl);
62  
63  
64      public abstract Object getParameters();
65      public abstract void setParameters(Object parameters);
66  
67  
68      public abstract String getSelectedId();
69      public abstract void setSelectedId(String selectedId);
70  
71      public abstract boolean isRestart();
72      public abstract void setRestart(boolean restart);
73  
74      public boolean isStateful()
75      {
76          return false;
77      }
78  
79      public String getPrefix()
80      {
81          return prefix;
82      }
83  
84      public void trigger(IRequestCycle cycle)
85      {
86          IActionListener listener = getListener();
87          Object[] params = cycle.getServiceParameters();
88          for (int i = 0; i < params.length; i++)
89          {
90              log.debug("Inside trigger(), the param is " + params[i]);
91          }
92          if (listener == null)
93          {
94              throw Tapestry.createRequiredParameterException(this,
95                      "directLink");
96          }
97          listener.actionTriggered(this, cycle);
98          if (OutlookBar.getOutlookBar(cycle).getForceSession())
99          {
100             cycle.getRequestContext().createSession().setAttribute(OutlookLink.SELECTED_OUTLOOK_LINK_ID, params[0]);
101         }
102     }
103 
104     public void gotoPageAction(IRequestCycle cycle)
105     {
106 
107         String id = OutlookBar.getOutlookBarAggregateId(cycle);
108         String pn = getPageName();
109 
110         if (OutlookBar.getOutlookBar(cycle).getForceSession())
111         {
112             cycle.getRequestContext().createSession().setAttribute(OutlookLink.SELECTED_OUTLOOK_LINK_ID, id);
113         }
114         log.debug("goto setting memory: page is =" + pn + "selected id=" + id);
115 
116         if (this.getPageName() != null)
117         {
118             log.debug("goto Forwarding to a page");
119             IPage page = cycle.getPage(getPageName());
120             cycle.activate(page);
121         }
122         else
123         {
124             log.debug("goto Invoking listener");
125             Object params = getParameters();
126             Object[] paramArray = null;
127             if (params instanceof Object[])
128             {
129             	paramArray = (Object[]) params;
130             }
131             else if (params != null)
132             {
133             	paramArray = new Object[] { params };
134             }
135             cycle.setServiceParameters(paramArray);            
136             getListener().actionTriggered(this, cycle);
137         }
138     }
139 
140     protected void setupComponent(IRequestCycle cycle)
141     {
142         if (cycle.isRewinding())
143         {
144             return;
145         }
146 
147         prefix = OutlookBar.getOutlookBar(cycle).getPrefix();
148 
149         IAsset img = getImage();
150         String url = img.buildURL(cycle);
151         setImageUrl(url);
152 
153         String id = OutlookBar.getOutlookBarAggregateId(cycle);
154         setSelectedId(id);
155         /* log("the id in link_setup() is " + id); */
156         Object[] params = {id};
157         IEngineService service =
158                 cycle.getEngine().getService(Tapestry.DIRECT_SERVICE);
159         ILink link = service.getLink(cycle, this, params);
160         setDirectUrl(link.getURL());
161 
162         /***
163          *   service =
164          cycle.getEngine().getService(Tapestry.ACTION_SERVICE );
165          service.getLink(cycle, this, new Object[]{});
166 
167          /// Object id = cycle.getAttribute(OutlookBar.AGGREGATE_ID);
168          //  String pn = getPageName();
169          //  log("the page name for this link is: " + pn + "; and the
170          selectedId is " + id);
171          //
172          cycle.getRequestContext().createSession().setAttribute(OutlookLink.SELECTED_OUTLOOK_LINK_ID, id);
173          */
174         if (isRestart())
175             log.debug("This is a restart link");
176         /*     if(getPageName() == null && getListener() == null)
177                  setRestart(true) ;*/
178     }
179 
180 }
181