Changeset 855
- Timestamp:
- 02/07/14 12:36:57 (10 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/actions/JfxBrowserActions.java
r848 r855 1 1 package org.expeditee.actions; 2 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 import java.net.HttpURLConnection; 6 import java.net.MalformedURLException; 7 import java.net.URI; 8 import java.net.URISyntaxException; 9 import java.net.URL; 10 import java.net.URLEncoder; 11 import java.util.ArrayList; 2 12 3 13 import org.expeditee.gui.Browser; … … 12 22 import org.expeditee.io.WebParser; 13 23 import org.expeditee.items.Item; 24 import org.expeditee.items.Picture; 14 25 import org.expeditee.items.Text; 15 26 import org.expeditee.items.widgets.InteractiveWidget; 16 import org.expeditee.items.widgets.InteractiveWidgetInitialisationFailedException;17 import org.expeditee.items.widgets.InteractiveWidgetNotAvailableException;18 27 import org.expeditee.items.widgets.JfxBrowser; 19 28 import org.expeditee.settings.network.NetworkSettings; 29 import org.json.simple.JSONObject; 30 import org.json.simple.parser.JSONParser; 31 import org.json.simple.parser.ParseException; 20 32 21 33 … … 153 165 FrameUtils.DisplayFrame(link.getAbsoluteLink()); 154 166 } 167 168 /** 169 * Uses the DuckDuckGo search API to provide short answers (e.g. definitions 170 * 171 * @param input 172 */ 173 public static void askTheDuck(Text input) { 174 175 final String query = input.getText(); 176 System.out.println(query); 177 input.delete(); 178 179 180 final JSONParser parser = new JSONParser(); 181 182 183 new Thread(new Runnable() { 184 185 @Override 186 public void run() { 187 try { 188 String queryForUrl = URLEncoder.encode(query.trim(), "UTF-8"); 189 190 HttpURLConnection connection = (HttpURLConnection) (new URL("http://api.duckduckgo.com/?q=" + queryForUrl + "&format=json&no_html=1&t=expeditee").openConnection()); 191 192 Object parsedObject = parser.parse(new InputStreamReader(connection.getInputStream())); 193 194 JSONObject jsonObject = (JSONObject) parsedObject; 195 196 String abstractText = (String) jsonObject.get("AbstractText"); 197 String definition = (String) jsonObject.get("Definition"); 198 String answer = (String) jsonObject.get("Answer"); 199 200 String title = ""; 201 String mainText = ""; 202 String sourceUrl = ""; 203 String sourceName = ""; 204 205 if (!abstractText.equals("")) { 206 207 sourceUrl = (String) jsonObject.get("AbstractURL"); 208 sourceName = "Via " + (String) jsonObject.get("AbstractSource"); 209 title = (String) jsonObject.get("Heading"); 210 mainText = abstractText; 211 212 } else if (!definition.equals("")) { 213 214 sourceUrl = (String) jsonObject.get("DefinitionURL"); 215 sourceName = "Via " + (String) jsonObject.get("DefinitionSource"); 216 title = query; 217 mainText = definition; 218 219 } else if (!answer.equals("")) { 220 221 sourceUrl = (String) jsonObject.get("AbstractURL"); 222 sourceName = "Via " + (String) jsonObject.get("AbstractSource"); 223 title = (String) jsonObject.get("AnswerType") + " " + query; 224 mainText = answer; 225 226 } else { 227 title = query; 228 229 mainText = "No instant answer available"; 230 231 sourceUrl = (String) jsonObject.get("Redirect"); 232 sourceName = "View Search Results"; 233 } 234 235 if (sourceUrl.equals("")) { 236 sourceUrl = "http://duckduckgo.com/?q=" + queryForUrl; 237 } 238 239 String picSource = (String) jsonObject.get("Image"); 240 241 // List to hold the items that will be attached to the cursor 242 ArrayList<Item> items = new ArrayList<Item>(); 243 244 Text titleItem = new Text(title); 245 Text mainTextItem = new Text(mainText); 246 Text sourceItem = new Text(sourceName); 247 Text ddgLink = new Text("Results from DuckDuckGo"); 248 249 sourceItem.setAction("createFrameWithBrowser " + sourceUrl); 250 ddgLink.setAction("createFrameWithBrowser " + "http://duckduckgo.com/?q=" + queryForUrl); 251 252 titleItem.setFamily("sans-serif"); 253 titleItem.setSize(16); 254 titleItem.setFontStyle("bold"); 255 titleItem.setWidth(400); 256 257 mainTextItem.setFamily("sans-serif"); 258 mainTextItem.setSize(14); 259 mainTextItem.setWidth(400); 260 261 sourceItem.setFamily("sans-serif"); 262 sourceItem.setSize(12); 263 sourceItem.setWidth(400); 264 sourceItem.setFontStyle("italic"); 265 266 ddgLink.setFamily("sans-serif"); 267 ddgLink.setSize(12); 268 ddgLink.setWidth(400); 269 ddgLink.setFontStyle("italic"); 270 271 titleItem.setPosition(FrameMouseActions.getPosition()); 272 273 if (!picSource.equals("")) { 274 Picture pic = WebParser.getImageFromUrl(picSource, null, DisplayIO.getCurrentFrame(), titleItem.getX() - 55, titleItem.getY() - 14, 50, null, null, 275 null, null, null, 0, 0); 276 items.add(pic); 277 items.add(pic.getSource()); 278 } 279 280 mainTextItem.setPosition(titleItem.getX(), titleItem.getY() + titleItem.getBoundsHeight()); 281 sourceItem.setPosition(mainTextItem.getX(), mainTextItem.getY() + mainTextItem.getBoundsHeight()); 282 ddgLink.setPosition(sourceItem.getX() + sourceItem.getBoundsWidth(), sourceItem.getY()); 283 284 items.add(titleItem); 285 items.add(mainTextItem); 286 items.add(sourceItem); 287 items.add(ddgLink); 288 FrameMouseActions.pickup(items); 289 } catch (MalformedURLException e) { 290 // TODO Auto-generated catch block 291 e.printStackTrace(); 292 } catch (IOException e) { 293 // TODO Auto-generated catch block 294 e.printStackTrace(); 295 } catch (ParseException e) { 296 // TODO Auto-generated catch block 297 e.printStackTrace(); 298 } 299 } 300 }).start(); 301 } 155 302 }
Note:
See TracChangeset
for help on using the changeset viewer.