View Javadoc

1   package org.informagen.echo.webcontainer;
2   
3   // Custom Component
4   import org.informagen.echo.app.CapacityBar;
5   
6   // Echo Component Framework
7   import nextapp.echo.app.Component;
8   import nextapp.echo.app.Color;
9   import nextapp.echo.app.Extent;
10  import nextapp.echo.app.util.Context;
11  
12  // Echo WebContainer Framework
13  import nextapp.echo.webcontainer.AbstractComponentSynchronizePeer;
14  import nextapp.echo.webcontainer.Service;
15  import nextapp.echo.webcontainer.ServerMessage;
16  import nextapp.echo.webcontainer.WebContainerServlet;
17  import nextapp.echo.webcontainer.service.JavaScriptService;
18  
19  import java.lang.StringBuffer;
20  import java.util.Collections;
21  import java.util.List;
22  
23  public class CapacityBarPeer extends AbstractComponentSynchronizePeer {
24  
25      private static final String REGISTRY_KEY = "Informagen.CapacityBar";
26      private static final String JAVASCRIPT_PATH = "org/informagen/echo/resource/CapacityBar.js";
27      
28      static {
29          Service service = JavaScriptService.forResource(REGISTRY_KEY, JAVASCRIPT_PATH);
30          WebContainerServlet.getServiceRegistry().add(service);
31      }
32  
33      public CapacityBarPeer() {
34          super();
35          
36          // This essentially registers 'PropertyChange' events with the JavaScript client
37          addOutputProperty(CapacityBar.PROPERTY_WIDTH);
38          addOutputProperty(CapacityBar.PROPERTY_HEIGHT);
39          addOutputProperty(CapacityBar.PROPERTY_SHOW_TICKS);
40          addOutputProperty(CapacityBar.PROPERTY_COLORS);
41          addOutputProperty(CapacityBar.PROPERTY_VALUES);
42          addOutputProperty(CapacityBar.PROPERTY_REFLECTIVITY);
43      }
44  
45      // Intialize and invoke superclass initialization -------------------------------------
46  
47      public void init(Context context, Component component) {
48          super.init(context, component);
49          ServerMessage serverMessage = (ServerMessage) context.get(ServerMessage.class);
50          serverMessage.addLibrary(REGISTRY_KEY);
51      }
52  
53      // Abstract methods from 'ComponentSynchronizePeer' -----------------------------------
54      //  Return application class and registry unique name
55      
56      public Class getComponentClass() {
57          return CapacityBar.class;
58      }
59  
60      public String getClientComponentType(boolean shortType) {
61          return REGISTRY_KEY;
62      }
63  
64  
65      /**
66       *  The WebContainer will invoke this peer method in order to respond to 'PropertyChange'
67       *      events in the Java which need to be passed to the JavaScript client.
68       *
69       *  NB: We are sending the string equivalent to the JS client because as of this writing
70       *        a 'SerialPropertyPeer' class has not been written for the Java Double class or
71       *        Java List class.
72       *      On the JS client side which uses an 'eval' function to properly converts this
73       *        incoming string into the approriate JS datatype.
74       */
75   
76      public Object getOutputProperty(Context context, Component component, String propertyName, int propertyIndex) {
77          
78          if (propertyName.equals(CapacityBar.PROPERTY_WIDTH)) {
79              Extent width = ((CapacityBar)component).getWidth();
80              return new Integer(width.getValue()); 
81          } else if (propertyName.equals(CapacityBar.PROPERTY_HEIGHT)) {
82              Extent height = ((CapacityBar)component).getHeight();
83              return new Integer(height.getValue()); 
84          } else if (propertyName.equals(CapacityBar.PROPERTY_SHOW_TICKS)) {
85              boolean showTicks = ((CapacityBar)component).isShowTicks();
86              return (showTicks ? Boolean.TRUE : Boolean.FALSE); 
87          } else if (propertyName.equals(CapacityBar.PROPERTY_REFLECTIVITY)) {
88              double reflectivity = ((CapacityBar)component).getReflectivity();
89              return new Double(reflectivity); 
90          } else if (propertyName.equals(CapacityBar.PROPERTY_CORNER_RADIUS)) {
91              double cornerRadius = ((CapacityBar)component).getCornerRadius();
92              return Double.toString(cornerRadius); 
93          } else if (propertyName.equals(CapacityBar.PROPERTY_TICK_SPACING)) {
94              Number tickSpacing = ((CapacityBar)component).getTickSpacing();
95              return (tickSpacing == null) ? null : tickSpacing.toString();
96          } else if (propertyName.equals(CapacityBar.PROPERTY_COLORS)) {
97              List<Color> colors = ((CapacityBar)component).getColors();
98              return createColorsArray(colors);
99          } else if (propertyName.equals(CapacityBar.PROPERTY_VALUES)) {
100             List<Number> values = ((CapacityBar)component).getValues();
101             return createValuesArray(values);
102         } else 
103             return super.getOutputProperty(context, component, propertyName, propertyIndex);
104     }
105     
106     
107     private String createColorsArray(List<Color> colors) {
108     
109         if(colors == null)
110             return null;
111     
112         if(colors.equals(Collections.EMPTY_LIST))
113             return null;
114     
115         if(colors.isEmpty())
116             return null;
117     
118         StringBuffer buffer = new StringBuffer();
119         
120         buffer.append("[");
121         
122         for(Color color : colors) {
123             buffer.append("'#");
124             buffer.append(toHex(color.getRed()));
125             buffer.append(toHex(color.getGreen()));
126             buffer.append(toHex(color.getBlue()));
127             buffer.append("'");          
128             buffer.append(",");          
129         }
130     
131         buffer.setLength(buffer.length() - 1);
132         buffer.append("]");
133         
134         return buffer.toString();
135     }
136 
137     private String toHex(int value) {
138         return ((value < 16) ? "0": "") + Integer.toHexString(value);
139     }
140     
141 
142     private String createValuesArray(List<Number> values) {
143 
144         if(values == null)
145             return null;
146     
147         if(values.equals(Collections.EMPTY_LIST))
148             return null;
149     
150         if(values.isEmpty())
151             return null;
152 
153         StringBuffer buffer = new StringBuffer();
154         
155         buffer.append("[");
156         
157         for(Number value : values) {
158             buffer.append(value);
159             buffer.append(",");          
160         }
161     
162         buffer.setLength(buffer.length() - 1);
163         buffer.append("]");
164         
165         return buffer.toString();
166     }
167 
168 }