Revision 2886 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/webbrowser/impl/DefaultWebBrowserPanel.java

View differences:

DefaultWebBrowserPanel.java
1
package org.gvsig.webbrowser2.impl;
1
package org.gvsig.webbrowser.impl;
2 2

  
3 3
import java.awt.BorderLayout;
4 4
import java.awt.Dimension;
......
8 8
import java.io.IOException;
9 9
import java.net.MalformedURLException;
10 10
import java.net.URL;
11
import java.util.ArrayList;
12
import java.util.List;
13 11
import java.util.Objects;
14
import org.gvsig.webbrowser2.WebBrowserPanel;
12
import org.gvsig.webbrowser.WebBrowserPanel;
15 13
import javax.swing.ImageIcon;
16 14
import javax.swing.JComponent;
17 15
import javax.swing.JEditorPane;
18 16
import javax.swing.JScrollPane;
19 17
import javax.swing.JTextField;
18
import javax.swing.SwingUtilities;
19
import org.gvsig.tools.swing.api.ToolsSwingUtils;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
20 22

  
23
@SuppressWarnings("UseSpecificCatch")
21 24
public class DefaultWebBrowserPanel extends DefaultWebBrowserPanelView implements WebBrowserPanel {
22

  
23
    private class History {
24
        private final List<URL> history;
25
        private int pos;
26
        private URL current;
27
        
28
        public History() {
29
            this.history = new ArrayList<>();
30
            this.pos = 0;
31
        }
32
        
33
        public void previous() {
34
            if( pos>1 ) {
35
                pos--;
36
                current = history.get(pos-1);
37
            }
38
        }
39
        
40
        public void next() {
41
            
42
        }
43
        
44
        public void go(URL url) {
45
            while (pos < history.size())
46
                history.remove(history.size() - 1);
47
            history.add(url);
48
            pos++;            
49
            current = url;
50
        }
51
        
52
        public URL get() {
53
            return this.current;
54
        }
55
    }
56 25
    
57
    private JComponent browser;
58
    private History history;
26
    protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultWebBrowserPanel.class);
27
    
28
    
29
    protected WebBrowserPanel.WebBrowser browser;
59 30

  
60 31
    public DefaultWebBrowserPanel() {
61 32
        this.initComponents();
62 33
    }
63 34

  
64
    protected JComponent createBrowserPanel() {
65
        return new JEditorPane();
35
    protected WebBrowserPanel.WebBrowser createBrowserPanel() {
36
        return new WebBrowserPanel.WebBrowser() {
37
            private JEditorPane panel = new JEditorPane();
38
            private WebBrowserHistoryImpl history = new WebBrowserHistoryImpl();
39
            @Override
40
            public void setPage(URL url) throws IOException {
41
                this.history.go(url);
42
                this.panel.setPage(url);
43
            }
44

  
45
            @Override
46
            public String getPage() {
47
                URL url = this.history.get();
48
                return url.toString();
49
            }
50

  
51
            @Override
52
            public String getTitle() {
53
                return "";
54
            }
55

  
56
            @Override
57
            public void goPrevious() {
58
                this.history.previous();
59
            }
60

  
61
            @Override
62
            public void goNext() {
63
                this.history.next();
64
            }
65

  
66
            @Override
67
            public void find(String text, boolean backwards) {
68
            }
69

  
70
            @Override
71
            public void setContent(String content, String contentType) {
72
                this.panel.setText(content);
73
            }
74

  
75
            @Override
76
            public JComponent asJComponent() {
77
                return this.panel;
78
            }
79
        };
66 80
    }
67 81
    
68
    protected JEditorPane getBrowser() {
69
        return (JEditorPane) this.browser;
82
    protected WebBrowserPanel.WebBrowser getBrowser() {
83
        return this.browser;
70 84
    }
71 85
    
72
    private void initComponents() {
86
    private void initComponents() {        
73 87
        this.browser = this.createBrowserPanel();
74
        this.containerBrowser.setLayout(new BorderLayout());
75 88
        JScrollPane scrollPanel = new JScrollPane(
76
                this.browser,
77
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS, 
78
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
89
                this.browser.asJComponent(),
90
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
91
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
79 92
        );
93
        this.containerBrowser.setLayout(new BorderLayout());
80 94
        this.containerBrowser.add(scrollPanel, BorderLayout.CENTER);
81

  
82
        this.btnPrevious.setIcon(imageIcon("images/arrow_left.png"));
83
        this.btnNext.setIcon(imageIcon("images/arrow_right.png"));
84
        this.btnRefresh.setIcon(imageIcon("images/arrow_refresh.png"));
85
        this.btnConfig.setIcon(imageIcon("images/add.png"));
86
        this.btnSearchPrevious.setIcon(imageIcon("images/arrow_up.png"));
87
        this.btnSearchNext.setIcon(imageIcon("images/arrow_down.png"));
88
        this.imgSearch.setIcon(imageIcon("images/search.png"));
89

  
90
        this.txtURL.addKeyListener(new KeyAdapter() {
91
            @Override
92
            public void keyPressed(KeyEvent keyEvent) {
93
                if( keyEvent.getKeyChar() == '\n' ) {
94
                    setPage(txtURL.getText());
95
        try {
96
            this.txtURL.addKeyListener(new KeyAdapter() {
97
                @Override
98
                public void keyPressed(KeyEvent keyEvent) {
99
                    if( keyEvent.getKeyChar() == '\n' ) {
100
                        setPage(txtURL.getText());
101
                    }
95 102
                }
96
            }
97
        });
98
        this.txtSearch.addKeyListener(new KeyAdapter() {
99
            @Override
100
            public void keyPressed(KeyEvent event) {
101
                JTextField jtext = txtSearch;
102
                if( event.getKeyChar() == 0x1b ) { // ESC
103
                  jtext.setText("");
104
                  find("", true);
105
                } else if( event.getKeyChar() == '\n' ) {
106
                  find(jtext.getText(), true);
103
            });
104
            this.txtSearch.addKeyListener(new KeyAdapter() {
105
                @Override
106
                public void keyPressed(KeyEvent event) {
107
                    JTextField jtext = txtSearch;
108
                    if( event.getKeyChar() == 0x1b ) { // ESC
109
                      jtext.setText("");
110
                      find("", true);
111
                    } else if( event.getKeyChar() == '\n' ) {
112
                      find(jtext.getText(), true);
113
                    }
107 114
                }
108
            }
109
        });
110
        this.btnSearchPrevious.addActionListener((ActionEvent e) -> {
111
            find(txtSearch.getText(),false);
112
        });
113
        this.btnSearchNext.addActionListener((ActionEvent e) -> {
114
            find(txtSearch.getText(),true);
115
        });
116
        this.btnPrevious.addActionListener((ActionEvent e) -> {
117
            goPrevious();
118
        });
119
        this.btnNext.addActionListener((ActionEvent e) -> {
120
            goNext();
121
        });
122
        this.setPreferredSize(new Dimension(600, 500));
115
            });
116
            this.btnSearchPrevious.addActionListener((ActionEvent e) -> {
117
                find(txtSearch.getText(),false);
118
            });
119
            this.btnSearchNext.addActionListener((ActionEvent e) -> {
120
                find(txtSearch.getText(),true);
121
            });
122
            this.btnPrevious.addActionListener((ActionEvent e) -> {
123
                this.browser.goPrevious();
124
            });
125
            this.btnNext.addActionListener((ActionEvent e) -> {
126
                this.browser.goNext();
127
            });
128
            this.setPreferredSize(new Dimension(600, 500));
129
        } catch(Exception ex) {
130
            LOGGER.warn("Can't initialize browser panel", ex);
131
        }
123 132
    }
124 133

  
125
    private ImageIcon imageIcon(String res) {
126
        URL url = this.getClass().getResource(res);
127
        if( url == null ) {
128
            return new ImageIcon();
129
        }
130
        return new ImageIcon(url);
131
    }
132
    
133 134
    public void find(String text, boolean forward) {
134 135
        
135 136
    }
136 137
    
137
    public void goNext() {
138
        this.history.next();
139
    }
140
    
141
    public void goPrevious() {
142
        this.history.previous();
143
    }
144

  
145 138
    @Override
146 139
    public void setPage(String url) {
147 140
        try {
......
154 147
    @Override
155 148
    public void setPage(URL url) {
156 149
        this.txtURL.setText(url.toString());
157
        this.history.go(url);
158
        try {
159
            this.getBrowser().setPage(url);
160
        } catch (IOException ex) {
161
            
162
        }
150
        SwingUtilities.invokeLater(() -> {
151
            try {
152
                this.getBrowser().setPage(url);
153
            } catch (Exception ex) {
154
                LOGGER.warn("Can't load page from url '"+url+"'.", ex);
155
            }
156
        });
163 157
    }
164 158

  
165 159
    @Override
166 160
    public String getPage() {
167
        URL url = this.history.get();
168
        return Objects.toString(url,"");
161
        return this.getBrowser().getPage();
169 162
    }
170 163

  
171 164
    @Override
172 165
    public void setContent(final String content, final String contentType) {
173
        this.getBrowser().setText(content);
166
        this.getBrowser().setContent(content,contentType);
174 167
    }
175 168

  
176 169
    @Override
177 170
    public String getTitle() {
178
        return "Unknown";
171
        return this.getBrowser().getTitle();
179 172
    }
180 173

  
181 174
    @Override
182 175
    public JComponent asJComponent() {
183 176
        return this;
184 177
    }
178

  
179
    @Override
180
    public ImageIcon loadImage(String imageName) {
181
        return ToolsSwingUtils.loadImage(this, imageName);
182
    }
183
    
184

  
185
    @Override
186
    public void setStatus(String msg) {
187
        this.lblStatus.setText(msg);
188
    }
189

  
190
    @Override
191
    public void setURLText(String url) {
192
        this.txtURL.setText(url);
193
    }
194
    
195
    public static final void selfRegister() {
196
        ToolsSwingUtils.registerIcons(
197
                DefaultWebBrowserPanel.class, 
198
                "images", 
199
                "webbrowser", 
200
                new String[] { "webbrowser", "webbrowser-config" },
201
                new String[] { "webbrowser", "webbrowser-next" },
202
                new String[] { "webbrowser", "webbrowser-previous" },
203
                new String[] { "webbrowser", "webbrowser-refresh" },
204
                new String[] { "webbrowser", "webbrowser-search" },
205
                new String[] { "webbrowser", "webbrowser-search-next" },
206
                new String[] { "webbrowser", "webbrowser-search-previous" }
207
        );
208
    }
209
    
185 210
}

Also available in: Unified diff