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.machobar;
17  
18  import org.apache.tapestry.IRequestCycle;
19  import org.apache.tapestry.Tapestry;
20  import org.apache.tapestry.IAsset;
21  
22  import net.sf.tapfx.components.TapFXBaseComponent;
23  
24  /***
25   * @author Andreas Andreou
26   */
27  public abstract class MachoItem extends TapFXBaseComponent
28  {
29      private int index;
30      private int size;
31      private String prefix;
32  
33      public int getIndex()
34      {
35          return index;
36      }
37  
38      public String getPrefix()
39      {
40          return prefix;
41      }
42  
43      public int getSize()
44      {
45          return size;
46      }
47  
48      public abstract IAsset getIcon();
49      public abstract IAsset getLargeIcon();
50  
51      protected void setupComponent(IRequestCycle cycle)
52      {
53          MachoBar bar = MachoBar.getMachoBar(cycle);
54          if (null == bar)
55          { // TODO: this does not catch the case when the Item is AFTER a Bar
56              throw Tapestry.createRequiredParameterException(this,
57                      "You need to provide a @MachoBar around this component!");
58          }
59          prefix = bar.getPrefix();
60          size = bar.getItemSize();
61  
62          String imageURL = getIcon().buildURL(cycle);
63          bar.addAssetUrl(imageURL);
64          if (getLargeIcon() != null)
65          {
66              imageURL = getLargeIcon().buildURL(cycle);
67          }
68          bar.addLargeAssetUrl(imageURL);
69          // get the index of the item (after having added the images url)
70          index = bar.getItemsCount();
71      }
72  }