Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 57 58 59 60 61 62 < 63 > 64 65 66 67 68 69 .. 166 >> Next

057 public SwinginAmazonSearch() throws ParserConfigurationException
058 {
059 super("Amazon Search Tool");
060
061 dbf = DocumentBuilderFactory.newInstance();
062 db = dbf.newDocumentBuilder();
063
064 // USE a vertical box for north Panel
065 Box northWithTitle = new Box(BoxLayout.Y_AXIS);
066 this.getContentPane().add("North", northWithTitle);
Listing 22.1 (continued)
166 Item 22
067
068 // add label first
069 // add label up north
070 JPanel title = new JPanel(new FlowLayout(FlowLayout.CENTER));
071 JLabel titleLabel = new JLabel("Amazon.com Search 2
Assistant");
072 titleLabel.setForeground(Color.green.darker());
073 title.add(titleLabel);
074 northWithTitle.add(title);
075
076 Box northPanel = new Box(BoxLayout.Y_AXIS);
077 northWithTitle.add(northPanel); 2
078 northPanel.setBorder(new TitledBorder(new EtchedBorder(),
"Search Terms"));
079
080 // add operation drop down
081 JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT)) ;
082 northpanel.add(panel1);
083 panel1.add(new JLabel("Operation:"));
084 opsCombo.setEditable(false);
085 panel1.add(opsCombo);
086
// removed adding most of the GUI components for brevity
// removed adding status panel and WindowListener
132
133 this.setSize(600,400);
134 this.setLocation(100,100);
135 this.setVisible(true);
136
137 }
Listing 22.1 (continued)
The constructor (lines 57 to 137) instantiates the components, groups them in JPanels, adds them to LayoutManagers, and then sizes and shows the window. Unfortunately, the GUI components in the J2ME platform are different from (but similar to) those used in Swing:
139: public void actionPerformed(ActionEvent aevt)
140: {
141: String command = aevt.getActionCommand();
142: if (command.equals(CMD_SEARCH))
143: {
144: // check we have the valid parameters\
145: String targets = targetTF.getText();
146: if (targets.length() == 0)
147: {
148: status.setText("'Search For' text field cannot be 2
empty. );
149: }
Listing 22.1 (continued)
J2ME Performance and Pitfalls 167
150 else
151 {
152 try
153 {
154 page = 1; // reset
155 doAmazonSearch(page, targets);
156 } catch (MalformedURLException mue)
// removed exception handling, simply displayed an error message
164 }
165 }
166 else if (command.equals(CMD_DETAILS))
167 {
168 // popup a new window with the details for this product
169 String selectedProductName = (String) 2
results.getSelectedValue();
170
171 // get the parent ELEMENT node of the node with a 2
ProductName with this Value
172 if (productNodes != null)
173 {
174 Node n = 2
findNodeWithContent(productNodes,selectedProductName);
175
176 // get the Details element parent
177 Node parent = n.getParentNode();
178
179 if (debug) System.out.println("parent: " + 2
parent.getNodeName());
180 if (parent != null)
181 {
182 // display a Details Window
183 new DetailsDialog(this, parent, 2
selectedProductName);
184 }
185 }
186 else
187 {
188 status.setText("Internal error. Try another search.' );
189 }
190 }
// removed handling NEXT_TEN for brevity
207 }
Listing 22.1 (continued)
The Action event handler responds to the various button clicks generated by the GUI. Notice that I use the String.equals() method to determine which button was pressed. This will need to be changed, as string comparisons are slow. The two key events to respond to are the search request (line 142) and a details request (line 166). To respond to either event, some parameters are gathered (like the search term, line 145, or the XML node to report details on, line 174), and then doAmazonSearch() is invoked (line 155) or a new DetailsDialog window instantiated (line 183). Notice
168 Item 22
the DetailsDialog is instantiated as an anonymous reference and not saved in a variable for reuse. Such an assumption of abundant memory and trust in the efficiency of the garbage collector is a pitfall for J2ME programming:
209: private void doAmazonSearch(int page, String targets) throws 2
Exception
210: {
211: getter newBaseURL(); // reset
212:
213: // get the operation
214: String op = (String) opsCombo.getSelectedItem();
215:
216: // get the mode
217: String mode = (String) modeCombo.getSelectedItem();
218:
219: status setText("Contacting Amazon.com...");
220:
221: getter addOperation(op, targets);
222: getter e) d (mod e( d o 3 d a
223: getter addTypeClite");
224: getter addPageC" + page);
225: getter t( or F d d a
226:
227: // GET it
228: String response = getter.httpGet();
229: if (response != null && response.length() > 0)
230: status.setText("Received results! Formatting ...");
231:
232: if (debug) System.out.println("response: " + response);
233:
234: // parse the XML, extract ProductNames
235: String [] productNames = null;
Previous << 1 .. 57 58 59 60 61 62 < 63 > 64 65 66 67 68 69 .. 166 >> Next