1 | package org.expeditee.items.widgets;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import javax.swing.SwingUtilities;
|
---|
6 |
|
---|
7 | import org.expeditee.gui.MessageBay;
|
---|
8 | import org.expeditee.items.Text;
|
---|
9 |
|
---|
10 | import chrriis.common.UIUtils;
|
---|
11 | import chrriis.dj.nativeswing.NativeSwing;
|
---|
12 | import chrriis.dj.nativeswing.swtimpl.NativeInterface;
|
---|
13 | import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
|
---|
14 | import com.sun.jna.Native;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This class is used to create an embedded web browser
|
---|
18 | * widget to be used within Expeditee.
|
---|
19 | * @author kgas1 - 31/01/2012
|
---|
20 | **/
|
---|
21 | public class WebBrowser extends DataFrameWidget {
|
---|
22 |
|
---|
23 | private JWebBrowser _webBrowser;
|
---|
24 |
|
---|
25 | String[] _args = null;
|
---|
26 |
|
---|
27 | public WebBrowser(Text source, String[] args) {
|
---|
28 |
|
---|
29 | //pass the JWebBrowser a constructor to destroy on finalization so the
|
---|
30 | //web browser can be reconstructed every time we leave the frame that it is
|
---|
31 | //located on and want to go back to that frame. Also allows
|
---|
32 | //us to move the browser around the frame and resize it. - kgas1 31/01/2012.
|
---|
33 |
|
---|
34 | super(source, new JWebBrowser(JWebBrowser.destroyOnFinalization(),JWebBrowser.constrainVisibility()),
|
---|
35 | 100, 500, -1, 100, 300, -1);
|
---|
36 |
|
---|
37 |
|
---|
38 | NativeSwing.initialize();
|
---|
39 | UIUtils.setPreferredLookAndFeel();
|
---|
40 | NativeInterface.open();
|
---|
41 | _webBrowser = (JWebBrowser) _swingComponent;
|
---|
42 |
|
---|
43 | if(args != null){
|
---|
44 | _args = args.clone();
|
---|
45 |
|
---|
46 | for(int i = 0; i < args.length; i++){
|
---|
47 | String[] split = args[i].split("=");
|
---|
48 |
|
---|
49 | if(split[0].equals("locationBar")){
|
---|
50 | _webBrowser.setLocationBarVisible(Boolean.parseBoolean(split[1]));
|
---|
51 | }
|
---|
52 | else if(split[0].equals("buttonBar")){
|
---|
53 | _webBrowser.setButtonBarVisible(Boolean.parseBoolean(split[1]));
|
---|
54 | }
|
---|
55 | else if(split[0].equals("menuBar")){
|
---|
56 | _webBrowser.setMenuBarVisible(Boolean.parseBoolean(split[1]));
|
---|
57 | }
|
---|
58 | else if(split[0].equals("statusBar")){
|
---|
59 | _webBrowser.setStatusBarVisible(Boolean.parseBoolean(split[1]));
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | final String url;
|
---|
65 |
|
---|
66 | List<String> data = getData();
|
---|
67 |
|
---|
68 | if(data != null && data.size() > 0) {
|
---|
69 | url = data.get(0);
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | System.out.println("no data can be found");
|
---|
73 | url = "http://www.waikato.ac.nz";
|
---|
74 | }
|
---|
75 |
|
---|
76 | navigate(url);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | protected String[] getArgs() {
|
---|
82 | return _args;
|
---|
83 |
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void navigate(final String url) {
|
---|
87 | try
|
---|
88 | {
|
---|
89 | SwingUtilities.invokeLater(new Runnable() {
|
---|
90 | public void run(){
|
---|
91 | getSource().setData(url);
|
---|
92 | _webBrowser.navigate(url);
|
---|
93 | }
|
---|
94 | });
|
---|
95 |
|
---|
96 | }
|
---|
97 | catch(Exception e) {
|
---|
98 | MessageBay.errorMessage(e.getMessage());
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public void setSourceData(List<String> data) {
|
---|
105 | super.setSourceData(data);
|
---|
106 | if(data != null && data.size() > 0) {
|
---|
107 | navigate(data.get(0));
|
---|
108 | }
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | protected List<String> getData() {
|
---|
115 | return _textRepresentation.getData();
|
---|
116 | }
|
---|
117 |
|
---|
118 | } |
---|