1 | /**
|
---|
2 | * JfxBrowserActions.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.actions;
|
---|
20 |
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.io.InputStreamReader;
|
---|
23 | import java.net.HttpURLConnection;
|
---|
24 | import java.net.MalformedURLException;
|
---|
25 | import java.net.URI;
|
---|
26 | import java.net.URISyntaxException;
|
---|
27 | import java.net.URL;
|
---|
28 | import java.net.URLEncoder;
|
---|
29 | import java.util.ArrayList;
|
---|
30 |
|
---|
31 | import org.expeditee.gui.Browser;
|
---|
32 | import org.expeditee.gui.DisplayIO;
|
---|
33 | import org.expeditee.gui.Frame;
|
---|
34 | import org.expeditee.gui.FrameGraphics;
|
---|
35 | import org.expeditee.gui.FrameIO;
|
---|
36 | import org.expeditee.gui.FrameMouseActions;
|
---|
37 | import org.expeditee.gui.FrameUtils;
|
---|
38 | import org.expeditee.gui.FreeItems;
|
---|
39 | import org.expeditee.gui.MessageBay;
|
---|
40 | import org.expeditee.io.WebParser;
|
---|
41 | import org.expeditee.items.Item;
|
---|
42 | import org.expeditee.items.Picture;
|
---|
43 | import org.expeditee.items.Text;
|
---|
44 | import org.expeditee.items.widgets.InteractiveWidget;
|
---|
45 | import org.expeditee.items.widgets.JfxBrowser;
|
---|
46 | import org.expeditee.settings.exploratorysearch.ExploratorySearchSettings;
|
---|
47 | import org.expeditee.settings.network.NetworkSettings;
|
---|
48 | import org.json.simple.JSONObject;
|
---|
49 | import org.json.simple.parser.JSONParser;
|
---|
50 | import org.json.simple.parser.ParseException;
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * A list of Actions used with the JFX Browser widget
|
---|
56 | *
|
---|
57 | */
|
---|
58 | public class JfxBrowserActions {
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Launches items.widgets.JfxBrowser and uses Text item as URL.
|
---|
62 | * @param text Text item which passes contents as URL for browser.
|
---|
63 | * @throws Exception
|
---|
64 | */
|
---|
65 | public static void startBrowser(Item text) throws Exception {
|
---|
66 | if (!(text instanceof Text)) {
|
---|
67 | MessageBay.errorMessage("Must be a text item.");
|
---|
68 | return;
|
---|
69 | }
|
---|
70 | if(text.getLink() != null) {
|
---|
71 | MessageBay.errorMessage("Text item cannot have link.");
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | Text wt = new Text("@iw:org.expeditee.items.widgets.JfxBrowser"); // create new text item for browser widget
|
---|
76 |
|
---|
77 | if(FreeItems.textOnlyAttachedToCursor()) { // navigates to url specified by the text item
|
---|
78 | wt.appendText(":" + text.getText());
|
---|
79 | } else {
|
---|
80 | wt.appendText(":http://www.waikato.ac.nz");
|
---|
81 | }
|
---|
82 |
|
---|
83 | FreeItems.getInstance().clear(); // remove url text from cursor
|
---|
84 |
|
---|
85 | wt.setParent(DisplayIO.getCurrentFrame()); // set parent of text source for InteractiveWidget.createWidget()
|
---|
86 | wt.setXY(FrameMouseActions.getX(), FrameMouseActions.getY());
|
---|
87 |
|
---|
88 | // create widget from text item
|
---|
89 | JfxBrowser browser = (JfxBrowser) InteractiveWidget.createWidget(wt);
|
---|
90 |
|
---|
91 | FrameMouseActions.pickup(browser.getItems()); // attach browser widget to mouse
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Text item becomes link to new frame containing items.widgets.JfxBrowser and uses Text item as URL for browser.
|
---|
96 | * @param text Text item which passes contents as URL for browser and becomes link to the browser's new frame.
|
---|
97 | * @throws Exception
|
---|
98 | */
|
---|
99 | public static void startBrowserNewFrame(Item text) throws Exception {
|
---|
100 | if (!(text instanceof Text)) {
|
---|
101 | MessageBay.errorMessage("Must be a text item.");
|
---|
102 | return;
|
---|
103 | }
|
---|
104 | if(text.getLink() != null) { // text item can't already have a link
|
---|
105 | MessageBay.errorMessage("Text item already has link.");
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | // If no text with url is passed to action create a new text item with http://www.waikato.ac.nz for a default url
|
---|
110 | if(!FreeItems.textOnlyAttachedToCursor()) {
|
---|
111 | text = DisplayIO.getCurrentFrame().addText(FrameMouseActions.getX(), FrameMouseActions.getY(),
|
---|
112 | NetworkSettings.HomePage.get(), null);
|
---|
113 | text.setParent(DisplayIO.getCurrentFrame()); // set parent of text source for InteractiveWidget.createWidget()
|
---|
114 | FrameMouseActions.pickup(text); // Attach new text link to cursor
|
---|
115 | }
|
---|
116 |
|
---|
117 | // Create JfxBrowser widget on a new frame
|
---|
118 | Frame frame = FrameIO.CreateNewFrame(text); // create new frame for browser
|
---|
119 | frame.removeAllItems(frame.getItems());
|
---|
120 | text.setLink("" + frame.getNumber()); // link this text item to new frame
|
---|
121 |
|
---|
122 | // Create widget via text annotation
|
---|
123 | Text wt = frame.addText(0, 0, "@iw: org.expeditee.items.widgets.JfxBrowser "
|
---|
124 | + ("--anchorLeft 0 --anchorRight 0 --anchorTop 0 --anchorBottom 0 ")
|
---|
125 | + Browser._theBrowser.getContentPane().getWidth() + " " + Browser._theBrowser.getContentPane().getHeight()
|
---|
126 | + " : " + text.getText(), null);
|
---|
127 |
|
---|
128 | InteractiveWidget.createWidget(wt);
|
---|
129 |
|
---|
130 | FrameIO.SaveFrame(frame); // save frame to disk
|
---|
131 | }
|
---|
132 |
|
---|
133 | public static void parsePage(Item text) throws Exception {
|
---|
134 | if (!(text instanceof Text) || !FreeItems.textOnlyAttachedToCursor()) {
|
---|
135 | MessageBay.errorMessage("Must provide a text item.");
|
---|
136 | return;
|
---|
137 | }
|
---|
138 | if(text.getLink() != null) { // text item can't already have a link
|
---|
139 | MessageBay.errorMessage("Text item already has link.");
|
---|
140 | return;
|
---|
141 | }
|
---|
142 |
|
---|
143 | // Create JfxBrowser widget on a new frame
|
---|
144 | Frame frame = FrameIO.CreateNewFrame(text); // create new frame for browser
|
---|
145 | text.setLink("" + frame.getNumber()); // link this text item to new frame
|
---|
146 | frame.addText(100, 100, "test", null);
|
---|
147 |
|
---|
148 | WebParser.parseURL(text.getText(), frame);
|
---|
149 | System.out.println(text.getText());
|
---|
150 | }
|
---|
151 |
|
---|
152 | public static void gotoURL(Text link, String URL) {
|
---|
153 | Frame frame = FrameIO.CreateNewFrame(link);
|
---|
154 | link.setAction(null);
|
---|
155 | link.setLink("" + frame.getNumber());
|
---|
156 |
|
---|
157 | WebParser.parseURL(URL, frame);
|
---|
158 |
|
---|
159 | frame.change();
|
---|
160 |
|
---|
161 | FrameIO.SaveFrame(frame);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Creates a frame containing a JFXBrowser pointed to the specified URL, then navigates to that frame
|
---|
166 | *
|
---|
167 | * @param link
|
---|
168 | * Item that will link to the new frame
|
---|
169 | * @param url
|
---|
170 | * URL to load in the Web Browser of the new frame
|
---|
171 | * @throws Exception
|
---|
172 | */
|
---|
173 | public static void createFrameWithBrowser(Item link, String url) throws Exception {
|
---|
174 | Frame frame = FrameIO.CreateNewFrame(link);
|
---|
175 | frame.removeAllItems(frame.getItems());
|
---|
176 |
|
---|
177 | link.setLink("" + frame.getNumber());
|
---|
178 | link.setAction(null);
|
---|
179 |
|
---|
180 | // Create widget via text annotation
|
---|
181 | frame.addText(10, 10, "@iw: org.expeditee.items.widgets.JfxBrowser " + (int) (FrameGraphics.getMaxFrameSize().getWidth() * 0.9) + " "
|
---|
182 | + (int) (FrameGraphics.getMaxFrameSize().getHeight() * 0.9) + " : " + url, null);
|
---|
183 |
|
---|
184 | FrameIO.SaveFrame(frame);
|
---|
185 |
|
---|
186 | FrameUtils.DisplayFrame(link.getAbsoluteLink());
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Uses the DuckDuckGo search API to provide short answers (e.g. definitions
|
---|
191 | *
|
---|
192 | * @param input
|
---|
193 | */
|
---|
194 | public static void askTheDuck(Text input) {
|
---|
195 |
|
---|
196 | final String query = input.getText();
|
---|
197 | System.out.println(query);
|
---|
198 | input.delete();
|
---|
199 |
|
---|
200 |
|
---|
201 | final JSONParser parser = new JSONParser();
|
---|
202 |
|
---|
203 |
|
---|
204 | new Thread(new Runnable() {
|
---|
205 |
|
---|
206 | @Override
|
---|
207 | public void run() {
|
---|
208 | try {
|
---|
209 | String queryForUrl = URLEncoder.encode(query.trim(), "UTF-8");
|
---|
210 |
|
---|
211 | HttpURLConnection connection = (HttpURLConnection) (new URL("http://api.duckduckgo.com/?q=" + queryForUrl + "&format=json&no_html=1&no_redirect=1&t=expeditee").openConnection());
|
---|
212 |
|
---|
213 | Object parsedObject = parser.parse(new InputStreamReader(connection.getInputStream()));
|
---|
214 |
|
---|
215 | JSONObject jsonObject = (JSONObject) parsedObject;
|
---|
216 |
|
---|
217 | String abstractText = (String) jsonObject.get("AbstractText");
|
---|
218 | String definition = (String) jsonObject.get("Definition");
|
---|
219 | String answer = (String) jsonObject.get("Answer");
|
---|
220 |
|
---|
221 | String title = "";
|
---|
222 | String mainText = "";
|
---|
223 | String sourceUrl = "";
|
---|
224 | String sourceName = "";
|
---|
225 |
|
---|
226 | if (!abstractText.equals("")) {
|
---|
227 |
|
---|
228 | sourceUrl = (String) jsonObject.get("AbstractURL");
|
---|
229 | sourceName = "Via " + (String) jsonObject.get("AbstractSource");
|
---|
230 | title = (String) jsonObject.get("Heading");
|
---|
231 | mainText = abstractText;
|
---|
232 |
|
---|
233 | } else if (!definition.equals("")) {
|
---|
234 |
|
---|
235 | sourceUrl = (String) jsonObject.get("DefinitionURL");
|
---|
236 | sourceName = "Via " + (String) jsonObject.get("DefinitionSource");
|
---|
237 | title = query;
|
---|
238 | mainText = definition;
|
---|
239 |
|
---|
240 | } else if (!answer.equals("")) {
|
---|
241 |
|
---|
242 | sourceUrl = (String) jsonObject.get("AbstractURL");
|
---|
243 | sourceName = "Via " + (String) jsonObject.get("AbstractSource");
|
---|
244 | title = (String) jsonObject.get("AnswerType") + " " + query;
|
---|
245 | mainText = answer;
|
---|
246 |
|
---|
247 | } else {
|
---|
248 | title = query;
|
---|
249 |
|
---|
250 | mainText = "No instant answer available";
|
---|
251 |
|
---|
252 | sourceUrl = (String) jsonObject.get("Redirect");
|
---|
253 | sourceName = "View Search Results";
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (sourceUrl.equals("")) {
|
---|
257 | sourceUrl = "http://duckduckgo.com/?q=" + queryForUrl;
|
---|
258 | }
|
---|
259 |
|
---|
260 | String picSource = (String) jsonObject.get("Image");
|
---|
261 |
|
---|
262 | // List to hold the items that will be attached to the cursor
|
---|
263 | ArrayList<Item> items = new ArrayList<Item>();
|
---|
264 |
|
---|
265 | Text titleItem = new Text(title);
|
---|
266 | Text mainTextItem = new Text(mainText);
|
---|
267 | Text sourceItem = new Text(sourceName);
|
---|
268 | Text ddgLink = new Text("Results from DuckDuckGo");
|
---|
269 |
|
---|
270 | sourceItem.setAction("createFrameWithBrowser " + sourceUrl);
|
---|
271 | ddgLink.setAction("createFrameWithBrowser " + "http://duckduckgo.com/?q=" + queryForUrl);
|
---|
272 |
|
---|
273 | titleItem.setFamily("sans-serif");
|
---|
274 | titleItem.setSize(16);
|
---|
275 | titleItem.setFontStyle("bold");
|
---|
276 | titleItem.setWidth(400);
|
---|
277 |
|
---|
278 | mainTextItem.setFamily("sans-serif");
|
---|
279 | mainTextItem.setSize(14);
|
---|
280 | mainTextItem.setWidth(400);
|
---|
281 |
|
---|
282 | sourceItem.setFamily("sans-serif");
|
---|
283 | sourceItem.setSize(12);
|
---|
284 | sourceItem.setWidth(400);
|
---|
285 | sourceItem.setFontStyle("italic");
|
---|
286 |
|
---|
287 | ddgLink.setFamily("sans-serif");
|
---|
288 | ddgLink.setSize(12);
|
---|
289 | ddgLink.setWidth(400);
|
---|
290 | ddgLink.setFontStyle("italic");
|
---|
291 |
|
---|
292 |
|
---|
293 | Picture pic = null;
|
---|
294 |
|
---|
295 | if (!picSource.equals("")) {
|
---|
296 | pic = WebParser.getImageFromUrl(picSource, null, DisplayIO.getCurrentFrame(), 0, 0, 50, null, null,
|
---|
297 | null, null, null, 0, 0);
|
---|
298 | items.add(pic);
|
---|
299 | items.add(pic.getSource());
|
---|
300 | }
|
---|
301 |
|
---|
302 | titleItem.setPosition(FrameMouseActions.getPosition());
|
---|
303 | mainTextItem.setPosition(titleItem.getX(), titleItem.getY() + titleItem.getBoundsHeight());
|
---|
304 | sourceItem.setPosition(mainTextItem.getX(), mainTextItem.getY() + mainTextItem.getBoundsHeight());
|
---|
305 | ddgLink.setPosition(sourceItem.getX() + sourceItem.getBoundsWidth(), sourceItem.getY());
|
---|
306 |
|
---|
307 | if (pic != null) {
|
---|
308 | pic.getSource().setPosition(titleItem.getX() - 55, titleItem.getY() - 14);
|
---|
309 | }
|
---|
310 |
|
---|
311 | items.add(titleItem);
|
---|
312 | items.add(mainTextItem);
|
---|
313 | items.add(sourceItem);
|
---|
314 | items.add(ddgLink);
|
---|
315 | FrameMouseActions.pickup(items);
|
---|
316 | } catch (IOException e) {
|
---|
317 | MessageBay.displayMessage("Problem loading results");
|
---|
318 | e.printStackTrace();
|
---|
319 | } catch (ParseException e) {
|
---|
320 | e.printStackTrace();
|
---|
321 | }
|
---|
322 | }
|
---|
323 | }).start();
|
---|
324 | }
|
---|
325 | }
|
---|