1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.StreamConnection;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.FlowFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;

public class Main extends UiApplication{
	private ProvidersScreen _providersScreen;
	private OfferingsScreen _offeringsScreen;
	private FlowFieldManager _fieldManager;
	private String pc_client_url;
    private Vector providers;
	
	public static void main(String[] args){
		 Main theApp = new Main();
	        theApp.enterEventDispatcher();
	}
	
	public Main(){
		_providersScreen = new ProvidersScreen();
		_offeringsScreen = new OfferingsScreen();
    	_fieldManager = new FlowFieldManager();
        pc_client_url = "http://10.252.9.109:9000";
        providers = new Vector();

    	_providersScreen.add(_fieldManager);
    	getContent();
		pushScreen(_providersScreen);		
	}
	
	public Vector getHTML(){
        Vector images = new Vector();
    	 StreamConnection s = null;
         try {
			s = (StreamConnection)Connector.open(pc_client_url+"/providers.xml");
			System.out.println("~~~~~~~~Connection to: " + pc_client_url + "/providers.xml");
			HttpConnection httpConn = (HttpConnection)s;                        
	         
	         int status = httpConn.getResponseCode();
              System.out.println("~~~~~~~~~~~~~~" + status);	         
	         if (status == HttpConnection.HTTP_OK)
	         {

	           System.out.println("~~~~~200 OK");
	             InputStream input = s.openInputStream();
	            
	             DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	             DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	             Document doc = docBuilder.parse(input);
	             doc.getDocumentElement ().normalize ();
	             NodeList listOfProviders = doc.getElementsByTagName("provider");
	             int totalProviders = listOfProviders.getLength();
	             for(int i=0; i<listOfProviders.getLength() ; i++){
	            	// System.out.println("~~~~~Looping in getHTML");
	                 Node firstPersonNode = listOfProviders.item(i);
	                 if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
	                     Element firstPersonElement = (Element)firstPersonNode;
	                     NodeList firstNameList = firstPersonElement.getElementsByTagName("name");
	                     Element firstNameElement = (Element)firstNameList.item(0);
	                     
	                     NodeList firstImageList = firstPersonElement.getElementsByTagName("image");
	                     Element firstImageElement = (Element)firstImageList.item(0);


	                     System.out.println("~~~~~~" + firstPersonElement.toString());
                         System.out.println("~~~~~~~~~~~~~~~Adding image URL.");
	                     images.addElement(firstImageElement.getChildNodes().item(0).getNodeValue().trim());
	                     providers.addElement(firstNameElement.getChildNodes().item(0).getNodeValue().trim());
	                 }//end of if clause


	             }//end of for loop with s var
	         }//end if status is ok

		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("~~~~I caught an IOException");
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			System.out.println("~~~~~I caught a ParserConfigurationException");
			e.printStackTrace();
		} catch (SAXException e) {
			System.out.println("~~~~I caught a SAXException");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    	return images;
    }


/*
 * When this method is called, it goes to pc_client_url/providers.xml, and grabs the images
 * for the providers, turns them into an Encoded Image, and then adds it to the mainscreen
 * to be displayed.     
 */
    public void getContent(){
       Vector encoded_image_fields = new Vector();
       Vector image_urls = getHTML();

    	//add the code to call providers.xml, then iterate over the xml i get, adding images.
    	//crud! there's no arraylist!?
    	System.out.print("~~~~~~~~~~~");
    	//this for loop is empty. crud.
    	for(int i = 0; i < image_urls.size(); i++){
//    		text_fields.insertElementAt(new RichTextField(), i);
//    		RichTextField temp = (RichTextField)text_fields.elementAt(i);
//    		temp.setText(image_urls.elementAt(i).toString());
//    		mscreen.add((RichTextField) text_fields.elementAt(i));
//    		mscreen.add(new SeparatorField());
    		System.out.println("~~~~Adding: " + image_urls.elementAt(i).toString());
    		encoded_image_fields.insertElementAt(new BitmapField(), i);
            byte[] data = new byte[100000];
            StringBuffer raw = new StringBuffer();
            try {
            	System.out.println(pc_client_url + "/images/providers/" + image_urls.elementAt(i).toString());
				StreamConnection s = (StreamConnection)Connector.open(pc_client_url + "/images/providers/" + image_urls.elementAt(i).toString());
				HttpConnection c = (HttpConnection)s;
				InputStream i2 = c.openInputStream();
                int len = 0;
				while ( -1 != (len = i2.read(data)) )
                {                   
                    raw.append(new String(data, 0, len));
                }   
                c.close();
                s.close();
                i2.close();
				//Now, I need to store the input stream's content into a Byte array.
            } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            byte[] content = raw.toString().getBytes();
            //EncodedImage image = EncodedImage.createEncodedImage(content, 0, content.length);
            //_bitmap.setImage(image);
    		BitmapField temp = (BitmapField)encoded_image_fields.elementAt(i);
    		temp.setImage(EncodedImage.createEncodedImage(content, 0, content.length));
    		_fieldManager.add((BitmapField) encoded_image_fields.elementAt(i));
    		ButtonField but_temp = new ButtonField(providers.elementAt(i).toString());
    		but_temp.setChangeListener(listenerUS);
    		_fieldManager.add(but_temp);
    		_fieldManager.add(new SeparatorField());
    		
    		//System.out.println("~~~~~~~~~~" + image_urls.elementAt(i).toString());

    	}//end for loop

    	
    }

    FieldChangeListener listenerUS = new FieldChangeListener() {
    	public void fieldChanged(Field field, int context) {
    	System.out.println("US was pressed");
    	pushScreen(_offeringsScreen);
    	}
    	};
	
	private class OfferingsScreen extends MainScreen{
		public OfferingsScreen(){
			RichTextField text = new RichTextField();
			text.setText("You did it!");
			add(text);
		}
	}
	
	private class ProvidersScreen extends MainScreen{
		public ProvidersScreen(){
		
		}

	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}