svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / develtools / InfoPanel.java @ 47423
History | View | Annotate | Download (6.03 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.app.extension.develtools; |
25 | |||
26 | import java.awt.BorderLayout; |
||
27 | import java.awt.Dimension; |
||
28 | import java.awt.event.ActionEvent; |
||
29 | import java.awt.event.ActionListener; |
||
30 | import java.io.File; |
||
31 | import java.io.FileWriter; |
||
32 | import java.io.IOException; |
||
33 | 41442 | jjdelcerro | import java.net.URL; |
34 | 40435 | jjdelcerro | |
35 | import javax.swing.BorderFactory; |
||
36 | import javax.swing.Box; |
||
37 | import javax.swing.BoxLayout; |
||
38 | import javax.swing.JButton; |
||
39 | import javax.swing.JPanel; |
||
40 | import javax.swing.JScrollPane; |
||
41 | import javax.swing.JTextPane; |
||
42 | 41442 | jjdelcerro | import javax.swing.event.HyperlinkEvent; |
43 | import javax.swing.event.HyperlinkListener; |
||
44 | 47423 | jjdelcerro | import org.gvsig.andami.PluginsLocator; |
45 | import org.gvsig.andami.actioninfo.ActionInfo; |
||
46 | import org.gvsig.andami.actioninfo.ActionInfoManager; |
||
47 | 40435 | jjdelcerro | |
48 | import org.gvsig.app.ApplicationLocator; |
||
49 | 47423 | jjdelcerro | import static org.gvsig.app.extension.develtools.IconThemeDevelTool.LOGGER; |
50 | import org.gvsig.desktopopen.DesktopOpen; |
||
51 | import org.gvsig.tools.ToolsLocator; |
||
52 | import org.gvsig.tools.folders.FoldersManager; |
||
53 | 40435 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
54 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
55 | 47423 | jjdelcerro | import org.gvsig.tools.util.ToolsUtilLocator; |
56 | 40435 | jjdelcerro | import org.slf4j.Logger; |
57 | import org.slf4j.LoggerFactory; |
||
58 | |||
59 | public class InfoPanel extends JPanel { |
||
60 | private static Logger logger = LoggerFactory.getLogger(InfoPanel.class); |
||
61 | |||
62 | |||
63 | private static final long serialVersionUID = 7164653790859770568L; |
||
64 | |||
65 | private JButton accept = null; |
||
66 | private JButton copy = null; |
||
67 | private JTextPane text = null; |
||
68 | |||
69 | public static void showPanel(String title, WindowManager.MODE mode, |
||
70 | String html) {
|
||
71 | 41442 | jjdelcerro | JPanel panel = new InfoPanel(html, null); |
72 | 40435 | jjdelcerro | WindowManager wm = ToolsSwingLocator.getWindowManager(); |
73 | wm.showWindow(panel, title, mode); |
||
74 | } |
||
75 | 41441 | jjdelcerro | |
76 | 40435 | jjdelcerro | |
77 | 41442 | jjdelcerro | public static void showPanel(String title, WindowManager.MODE mode, |
78 | String html, HyperlinkListener hyperlinkListener) { |
||
79 | JPanel panel = new InfoPanel(html,hyperlinkListener); |
||
80 | WindowManager wm = ToolsSwingLocator.getWindowManager(); |
||
81 | wm.showWindow(panel, title, mode); |
||
82 | } |
||
83 | |||
84 | |||
85 | 40435 | jjdelcerro | public static void save2file(String name, String contents) { |
86 | File file;
|
||
87 | try {
|
||
88 | file = File.createTempFile("gvsig-" + name + "-", ".html"); |
||
89 | FileWriter fwriter = new FileWriter(file); |
||
90 | fwriter.append(contents); |
||
91 | fwriter.close(); |
||
92 | } catch (IOException e) { |
||
93 | logger.warn("Can't save contents to temp file gvsig-" + name, e);
|
||
94 | } |
||
95 | } |
||
96 | 41442 | jjdelcerro | |
97 | public InfoPanel(final String html, HyperlinkListener hyperlinkListener) { |
||
98 | 40435 | jjdelcerro | this.setLayout(new BorderLayout()); |
99 | this.setPreferredSize(new Dimension(500, 300)); |
||
100 | this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); |
||
101 | |||
102 | text = new JTextPane(); |
||
103 | text.setContentType("text/html");
|
||
104 | text.setEditable(false);
|
||
105 | text.setText(html); |
||
106 | text.setCaretPosition(0);
|
||
107 | 41442 | jjdelcerro | if( hyperlinkListener!=null ) { |
108 | text.addHyperlinkListener(hyperlinkListener); |
||
109 | } |
||
110 | 40435 | jjdelcerro | |
111 | JScrollPane scrollPane = new JScrollPane(text); |
||
112 | scrollPane.setPreferredSize(new Dimension(500, 220)); |
||
113 | |||
114 | accept = new JButton("Accept"); |
||
115 | accept.addActionListener(new ActionListener() { |
||
116 | public void actionPerformed(ActionEvent arg0) { |
||
117 | setVisible(false);
|
||
118 | } |
||
119 | }); |
||
120 | copy = new JButton("Copy to clipboard"); |
||
121 | copy.addActionListener(new ActionListener() { |
||
122 | public void actionPerformed(ActionEvent arg0) { |
||
123 | 47413 | fdiaz | ToolsSwingLocator.getToolsSwingManager().putInClipboard( |
124 | text.getText(), |
||
125 | "html"
|
||
126 | ); |
||
127 | 40435 | jjdelcerro | } |
128 | }); |
||
129 | |||
130 | JPanel buttonsPanel = new JPanel(); |
||
131 | buttonsPanel.setLayout(new BoxLayout(buttonsPanel, |
||
132 | BoxLayout.LINE_AXIS));
|
||
133 | buttonsPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); |
||
134 | |||
135 | buttonsPanel.add(Box.createHorizontalGlue());
|
||
136 | buttonsPanel.add(copy); |
||
137 | buttonsPanel.add(accept); |
||
138 | |||
139 | this.add(scrollPane, BorderLayout.CENTER); |
||
140 | this.add(buttonsPanel, BorderLayout.SOUTH); |
||
141 | this.setVisible(true); |
||
142 | } |
||
143 | |||
144 | 47423 | jjdelcerro | public static void showInBrowser(String name, String title, String html) { |
145 | try {
|
||
146 | FoldersManager fm = ToolsLocator.getFoldersManager(); |
||
147 | File f = fm.createTemporaryFile(name+".html", html); |
||
148 | DesktopOpen desktopOpen = ToolsUtilLocator.getToolsUtilManager().createDesktopOpen(); |
||
149 | desktopOpen.browse(f.toURI()); |
||
150 | } catch (Exception ex) { |
||
151 | LOGGER.warn("Can't show in desktop browser", ex);
|
||
152 | showInInternalViewer(name, title, html); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | public static void showInInternalViewer(String name, String title, String html) { |
||
157 | InfoPanel.save2file(name, html); |
||
158 | InfoPanel.showPanel(title, WindowManager.MODE.WINDOW, html); |
||
159 | } |
||
160 | |||
161 | public static void showInPDFViewer(String name, String title, String html) { |
||
162 | ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
||
163 | ActionInfo showPdf = actionManager.getAction("show-pdf");
|
||
164 | if (showPdf == null) { |
||
165 | showInInternalViewer(name, title, html); |
||
166 | } else {
|
||
167 | showPdf.execute( |
||
168 | new Object[]{ |
||
169 | "--title=", title,
|
||
170 | "--mode=", WindowManager.MODE.WINDOW,
|
||
171 | "--source=", html
|
||
172 | } |
||
173 | ); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | 40435 | jjdelcerro | } |