Statistics
| Revision:

root / org.gvsig.hyperlink.app / trunk / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / AbstractHyperLinkPanel.java @ 1082

History | View | Annotate | Download (3.61 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.hyperlink.app.extension;
23

    
24
import java.io.File;
25
import java.io.IOException;
26
import java.net.URI;
27
import java.net.URISyntaxException;
28
import java.util.logging.Level;
29

    
30
import javax.swing.JPanel;
31

    
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.andami.messages.NotificationManager;
34
import org.gvsig.tools.util.URLUtils;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
/**
39
 * This class extends JPanel and implements IExtensioBuilder. Provides the
40
 * methods that will be reimplemented by the descendant class. Creates a panel
41
 * that shows the content of a URI. The necessary code that allows to show the
42
 * content of the URI is provided by the descendant class. Implmenting
43
 * IExtenssionBuilder this class and its the descendant provides a point of
44
 * extension for other extensions.
45
 */
46
public abstract class AbstractHyperLinkPanel extends JPanel {
47

    
48
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractHyperLinkPanel.class);
49

    
50
    protected LinkTarget document;
51

    
52
    public AbstractHyperLinkPanel(LinkTarget linkTarget) {
53
        super();
54
        document = linkTarget;
55
    }
56

    
57
    public LinkTarget getLinkTarget() {
58
        return document;
59
    }
60

    
61
    /**
62
     * Tries to make an absolute url from a relative one, and returns true if
63
     * the URL is valid. false otherwise
64
     *
65
     * @return
66
     */
67
    protected boolean checkAndNormalizeURI() {
68
        if (document == null) {
69
            LOGGER.warn("Hyperlink linked field does not exits");
70
            return false;
71
        } else if (document.getURL() != null) {
72
            try {
73
                if (!document.getURL().toURI().isAbsolute()) {
74
                    try {
75
                        // try as a relative path
76
                        File file = this.document.toFile();
77
                        if (file == null) {
78
                            LOGGER.warn("Hyperlink can't get file from '" + this.document.getURL() + "'.");
79
                            return false;
80
                        }
81
                        file = file.getCanonicalFile();
82
                        if (!file.exists()) {
83
                            LOGGER.warn("Hyperlink linked field, file '"+file+"' not exits");
84
                            return false;
85
                        }
86
                        document.setURL(URLUtils.toURL(file));
87
                        return true;
88
                    } catch (IOException e) {
89
                        LOGGER.warn("Hyperlink linked field does not exits");
90
                        return false;
91
                    }
92
                }
93
            } catch (URISyntaxException ex) {
94
                LOGGER.warn("Hyperlink linked field does not exits");
95
                return false;
96
            }
97
        }
98
        return true;
99
    }
100
}