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

Last change on this file since 845 was 845, checked in by ngw8, 10 years ago

Web parser now gives converted links an action that creates a new frame containing a web browser with the appropriate URL

File size: 5.5 KB
Line 
1package org.expeditee.actions;
2
3import org.expeditee.gui.Browser;
4import org.expeditee.gui.DisplayIO;
5import org.expeditee.gui.Frame;
6import org.expeditee.gui.FrameGraphics;
7import org.expeditee.gui.FrameIO;
8import org.expeditee.gui.FrameMouseActions;
9import org.expeditee.gui.FrameUtils;
10import org.expeditee.gui.FreeItems;
11import org.expeditee.gui.MessageBay;
12import org.expeditee.io.WebParser;
13import org.expeditee.items.Item;
14import org.expeditee.items.Text;
15import org.expeditee.items.widgets.InteractiveWidget;
16import org.expeditee.items.widgets.InteractiveWidgetInitialisationFailedException;
17import org.expeditee.items.widgets.InteractiveWidgetNotAvailableException;
18import org.expeditee.items.widgets.JfxBrowser;
19import org.expeditee.settings.network.NetworkSettings;
20
21
22
23/**
24 * A list of Actions used with the JFX Browser widget
25 *
26 */
27public class JfxBrowserActions {
28
29 /**
30 * Launches items.widgets.JfxBrowser and uses Text item as URL.
31 * @param text Text item which passes contents as URL for browser.
32 * @throws Exception
33 */
34 public static void startBrowser(Item text) throws Exception {
35 if (!(text instanceof Text)) {
36 MessageBay.errorMessage("Must be a text item.");
37 return;
38 }
39 if(text.getLink() != null) {
40 MessageBay.errorMessage("Text item cannot have link.");
41 return;
42 }
43
44 Text wt = new Text("@iw:org.expeditee.items.widgets.JfxBrowser"); // create new text item for browser widget
45
46 if(FreeItems.textOnlyAttachedToCursor()) { // navigates to url specified by the text item
47 wt.appendText(":" + text.getText());
48 } else {
49 wt.appendText(":http://www.waikato.ac.nz");
50 }
51
52 FreeItems.getInstance().clear(); // remove url text from cursor
53
54 wt.setParent(DisplayIO.getCurrentFrame()); // set parent of text source for InteractiveWidget.createWidget()
55 wt.setXY(FrameMouseActions.getX(), FrameMouseActions.getY());
56
57 // create widget from text item
58 JfxBrowser browser = (JfxBrowser) InteractiveWidget.createWidget(wt);
59
60 FrameMouseActions.pickup(browser.getItems()); // attach browser widget to mouse
61 }
62
63 /**
64 * Text item becomes link to new frame containing items.widgets.JfxBrowser and uses Text item as URL for browser.
65 * @param text Text item which passes contents as URL for browser and becomes link to the browser's new frame.
66 * @throws Exception
67 */
68 public static void startBrowserNewFrame(Item text) throws Exception {
69 if (!(text instanceof Text)) {
70 MessageBay.errorMessage("Must be a text item.");
71 return;
72 }
73 if(text.getLink() != null) { // text item can't already have a link
74 MessageBay.errorMessage("Text item already has link.");
75 return;
76 }
77
78 // If no text with url is passed to action create a new text item with http://www.waikato.ac.nz for a default url
79 if(!FreeItems.textOnlyAttachedToCursor()) {
80 text = DisplayIO.getCurrentFrame().addText(FrameMouseActions.getX(), FrameMouseActions.getY(),
81 NetworkSettings.HomePage.get(), null);
82 text.setParent(DisplayIO.getCurrentFrame()); // set parent of text source for InteractiveWidget.createWidget()
83 FrameMouseActions.pickup(text); // Attach new text link to cursor
84 }
85
86 // Create JfxBrowser widget on a new frame
87 Frame frame = FrameIO.CreateNewFrame(text); // create new frame for browser
88 frame.removeAllItems(frame.getItems());
89 text.setLink("" + frame.getNumber()); // link this text item to new frame
90
91 // Create widget via text annotation
92 Text wt = frame.addText(0, 0, "@iw: org.expeditee.items.widgets.JfxBrowser "
93 + Browser._theBrowser.getContentPane().getWidth() + " " + Browser._theBrowser.getContentPane().getHeight()
94 + " : " + text.getText(), null);
95
96 InteractiveWidget.createWidget(wt);
97
98 FrameIO.SaveFrame(frame); // save frame to disk
99 }
100
101 public static void parsePage(Item text) throws Exception {
102 if (!(text instanceof Text) || !FreeItems.textOnlyAttachedToCursor()) {
103 MessageBay.errorMessage("Must provide a text item.");
104 return;
105 }
106 if(text.getLink() != null) { // text item can't already have a link
107 MessageBay.errorMessage("Text item already has link.");
108 return;
109 }
110
111 // Create JfxBrowser widget on a new frame
112 Frame frame = FrameIO.CreateNewFrame(text); // create new frame for browser
113 text.setLink("" + frame.getNumber()); // link this text item to new frame
114 frame.addText(100, 100, "test", null);
115
116 WebParser.parseURL(text.getText(), frame);
117 System.out.println(text.getText());
118 }
119
120 public static void gotoURL(Text link, String URL) {
121 Frame frame = FrameIO.CreateNewFrame(link);
122 link.setAction(null);
123 link.setLink("" + frame.getNumber());
124
125 WebParser.parseURL(URL, frame);
126
127 frame.change();
128
129 FrameIO.SaveFrame(frame);
130 }
131
132 /**
133 * Creates a frame containing a JFXBrowser pointed to the specified URL, then navigates to that frame
134 *
135 * @param link
136 * Item that will link to the new frame
137 * @param url
138 * URL to load in the Web Browser of the new frame
139 * @throws Exception
140 */
141 public static void createFrameWithBrowser(Text link, String url) throws Exception {
142 Frame frame = FrameIO.CreateNewFrame(link);
143 frame.removeAllItems(frame.getItems());
144 link.setLink("" + frame.getNumber());
145 link.setAction(null);
146
147 // Create widget via text annotation
148 Text wt = frame.addText(10, 10, "@iw: org.expeditee.items.widgets.JfxBrowser " + (int) (FrameGraphics.getMaxFrameSize().getWidth() * 0.9) + " "
149 + (int) (FrameGraphics.getMaxFrameSize().getHeight() * 0.9) + " : " + url, null);
150
151 InteractiveWidget.createWidget(wt);
152
153 FrameIO.SaveFrame(frame);
154
155 FrameUtils.DisplayFrame(link.getAbsoluteLink());
156 }
157}
Note: See TracBrowser for help on using the repository browser.