source: trunk/src/org/expeditee/actions/JfxBrowserActions.java@ 1102

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

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