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