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;
|
---|
12 |
|
---|
13 | import org.expeditee.gui.Browser;
|
---|
14 | import org.expeditee.gui.DisplayIO;
|
---|
15 | import org.expeditee.gui.Frame;
|
---|
16 | import org.expeditee.gui.FrameGraphics;
|
---|
17 | import org.expeditee.gui.FrameIO;
|
---|
18 | import org.expeditee.gui.FrameMouseActions;
|
---|
19 | import org.expeditee.gui.FrameUtils;
|
---|
20 | import org.expeditee.gui.FreeItems;
|
---|
21 | import org.expeditee.gui.MessageBay;
|
---|
22 | import org.expeditee.io.WebParser;
|
---|
23 | import org.expeditee.items.Item;
|
---|
24 | import org.expeditee.items.Picture;
|
---|
25 | import org.expeditee.items.Text;
|
---|
26 | import org.expeditee.items.widgets.InteractiveWidget;
|
---|
27 | import org.expeditee.items.widgets.JfxBrowser;
|
---|
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;
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * A list of Actions used with the JFX Browser widget
|
---|
37 | *
|
---|
38 | */
|
---|
39 | public class JfxBrowserActions {
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Launches items.widgets.JfxBrowser and uses Text item as URL.
|
---|
43 | * @param text Text item which passes contents as URL for browser.
|
---|
44 | * @throws Exception
|
---|
45 | */
|
---|
46 | public static void startBrowser(Item text) throws Exception {
|
---|
47 | if (!(text instanceof Text)) {
|
---|
48 | MessageBay.errorMessage("Must be a text item.");
|
---|
49 | return;
|
---|
50 | }
|
---|
51 | if(text.getLink() != null) {
|
---|
52 | MessageBay.errorMessage("Text item cannot have link.");
|
---|
53 | return;
|
---|
54 | }
|
---|
55 |
|
---|
56 | Text wt = new Text("@iw:org.expeditee.items.widgets.JfxBrowser"); // create new text item for browser widget
|
---|
57 |
|
---|
58 | if(FreeItems.textOnlyAttachedToCursor()) { // navigates to url specified by the text item
|
---|
59 | wt.appendText(":" + text.getText());
|
---|
60 | } else {
|
---|
61 | wt.appendText(":http://www.waikato.ac.nz");
|
---|
62 | }
|
---|
63 |
|
---|
64 | FreeItems.getInstance().clear(); // remove url text from cursor
|
---|
65 |
|
---|
66 | wt.setParent(DisplayIO.getCurrentFrame()); // set parent of text source for InteractiveWidget.createWidget()
|
---|
67 | wt.setXY(FrameMouseActions.getX(), FrameMouseActions.getY());
|
---|
68 |
|
---|
69 | // create widget from text item
|
---|
70 | JfxBrowser browser = (JfxBrowser) InteractiveWidget.createWidget(wt);
|
---|
71 |
|
---|
72 | FrameMouseActions.pickup(browser.getItems()); // attach browser widget to mouse
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Text item becomes link to new frame containing items.widgets.JfxBrowser and uses Text item as URL for browser.
|
---|
77 | * @param text Text item which passes contents as URL for browser and becomes link to the browser's new frame.
|
---|
78 | * @throws Exception
|
---|
79 | */
|
---|
80 | public static void startBrowserNewFrame(Item text) throws Exception {
|
---|
81 | if (!(text instanceof Text)) {
|
---|
82 | MessageBay.errorMessage("Must be a text item.");
|
---|
83 | return;
|
---|
84 | }
|
---|
85 | if(text.getLink() != null) { // text item can't already have a link
|
---|
86 | MessageBay.errorMessage("Text item already has link.");
|
---|
87 | return;
|
---|
88 | }
|
---|
89 |
|
---|
90 | // If no text with url is passed to action create a new text item with http://www.waikato.ac.nz for a default url
|
---|
91 | if(!FreeItems.textOnlyAttachedToCursor()) {
|
---|
92 | text = DisplayIO.getCurrentFrame().addText(FrameMouseActions.getX(), FrameMouseActions.getY(),
|
---|
93 | NetworkSettings.HomePage.get(), null);
|
---|
94 | text.setParent(DisplayIO.getCurrentFrame()); // set parent of text source for InteractiveWidget.createWidget()
|
---|
95 | FrameMouseActions.pickup(text); // Attach new text link to cursor
|
---|
96 | }
|
---|
97 |
|
---|
98 | // Create JfxBrowser widget on a new frame
|
---|
99 | Frame frame = FrameIO.CreateNewFrame(text); // create new frame for browser
|
---|
100 | frame.removeAllItems(frame.getItems());
|
---|
101 | text.setLink("" + frame.getNumber()); // link this text item to new frame
|
---|
102 |
|
---|
103 | // Create widget via text annotation
|
---|
104 | Text wt = frame.addText(0, 0, "@iw: org.expeditee.items.widgets.JfxBrowser "
|
---|
105 | + Browser._theBrowser.getContentPane().getWidth() + " " + Browser._theBrowser.getContentPane().getHeight()
|
---|
106 | + " : " + text.getText(), null);
|
---|
107 |
|
---|
108 | InteractiveWidget.createWidget(wt);
|
---|
109 |
|
---|
110 | FrameIO.SaveFrame(frame); // save frame to disk
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static void parsePage(Item text) throws Exception {
|
---|
114 | if (!(text instanceof Text) || !FreeItems.textOnlyAttachedToCursor()) {
|
---|
115 | MessageBay.errorMessage("Must provide a text item.");
|
---|
116 | return;
|
---|
117 | }
|
---|
118 | if(text.getLink() != null) { // text item can't already have a link
|
---|
119 | MessageBay.errorMessage("Text item already has link.");
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | // Create JfxBrowser widget on a new frame
|
---|
124 | Frame frame = FrameIO.CreateNewFrame(text); // create new frame for browser
|
---|
125 | text.setLink("" + frame.getNumber()); // link this text item to new frame
|
---|
126 | frame.addText(100, 100, "test", null);
|
---|
127 |
|
---|
128 | WebParser.parseURL(text.getText(), frame);
|
---|
129 | System.out.println(text.getText());
|
---|
130 | }
|
---|
131 |
|
---|
132 | public static void gotoURL(Text link, String URL) {
|
---|
133 | Frame frame = FrameIO.CreateNewFrame(link);
|
---|
134 | link.setAction(null);
|
---|
135 | link.setLink("" + frame.getNumber());
|
---|
136 |
|
---|
137 | WebParser.parseURL(URL, frame);
|
---|
138 |
|
---|
139 | frame.change();
|
---|
140 |
|
---|
141 | FrameIO.SaveFrame(frame);
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Creates a frame containing a JFXBrowser pointed to the specified URL, then navigates to that frame
|
---|
146 | *
|
---|
147 | * @param link
|
---|
148 | * Item that will link to the new frame
|
---|
149 | * @param url
|
---|
150 | * URL to load in the Web Browser of the new frame
|
---|
151 | * @throws Exception
|
---|
152 | */
|
---|
153 | public static void createFrameWithBrowser(Text link, String url) throws Exception {
|
---|
154 | Frame frame = FrameIO.CreateNewFrame(link);
|
---|
155 | frame.removeAllItems(frame.getItems());
|
---|
156 | link.setLink("" + frame.getNumber());
|
---|
157 | link.setAction(null);
|
---|
158 |
|
---|
159 | // Create widget via text annotation
|
---|
160 | Text wt = frame.addText(10, 10, "@iw: org.expeditee.items.widgets.JfxBrowser " + (int) (FrameGraphics.getMaxFrameSize().getWidth() * 0.9) + " "
|
---|
161 | + (int) (FrameGraphics.getMaxFrameSize().getHeight() * 0.9) + " : " + url, null);
|
---|
162 |
|
---|
163 | FrameIO.SaveFrame(frame);
|
---|
164 |
|
---|
165 | FrameUtils.DisplayFrame(link.getAbsoluteLink());
|
---|
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&no_redirect=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 |
|
---|
272 | Picture pic = null;
|
---|
273 |
|
---|
274 | if (!picSource.equals("")) {
|
---|
275 | pic = WebParser.getImageFromUrl(picSource, null, DisplayIO.getCurrentFrame(), 0, 0, 50, null, null,
|
---|
276 | null, null, null, 0, 0);
|
---|
277 | items.add(pic);
|
---|
278 | items.add(pic.getSource());
|
---|
279 | }
|
---|
280 |
|
---|
281 | titleItem.setPosition(FrameMouseActions.getPosition());
|
---|
282 | mainTextItem.setPosition(titleItem.getX(), titleItem.getY() + titleItem.getBoundsHeight());
|
---|
283 | sourceItem.setPosition(mainTextItem.getX(), mainTextItem.getY() + mainTextItem.getBoundsHeight());
|
---|
284 | ddgLink.setPosition(sourceItem.getX() + sourceItem.getBoundsWidth(), sourceItem.getY());
|
---|
285 |
|
---|
286 | if (pic != null) {
|
---|
287 | pic.getSource().setPosition(titleItem.getX() - 55, titleItem.getY() - 14);
|
---|
288 | }
|
---|
289 |
|
---|
290 | items.add(titleItem);
|
---|
291 | items.add(mainTextItem);
|
---|
292 | items.add(sourceItem);
|
---|
293 | items.add(ddgLink);
|
---|
294 | FrameMouseActions.pickup(items);
|
---|
295 | } catch (IOException e) {
|
---|
296 | MessageBay.displayMessage("Problem loading results");
|
---|
297 | e.printStackTrace();
|
---|
298 | } catch (ParseException e) {
|
---|
299 | e.printStackTrace();
|
---|
300 | }
|
---|
301 | }
|
---|
302 | }).start();
|
---|
303 | }
|
---|
304 | }
|
---|