Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / ApplicationLibrary.java @ 40558

History | View | Annotate | Download (4.64 KB)

1
/**
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
package org.gvsig.app;
25

    
26
import java.io.File;
27
import java.io.IOException;
28

    
29
import javax.swing.JOptionPane;
30

    
31
import org.apache.commons.io.FileUtils;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.PluginsLocator;
37
import org.gvsig.app.extension.InitializeApplicationExtension;
38
import org.gvsig.app.project.DefaultProject;
39
import org.gvsig.fmap.mapcontext.MapContextLocator;
40
import org.gvsig.fmap.mapcontrol.MapControl;
41
import org.gvsig.i18n.Messages;
42
import org.gvsig.installer.lib.api.InstallerLibrary;
43
import org.gvsig.symbology.SymbologyLocator;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.library.AbstractLibrary;
46
import org.gvsig.tools.library.LibraryException;
47
import org.gvsig.tools.locator.ReferenceNotRegisteredException;
48

    
49
/**
50
 * @author 2008-2009 jmvivo
51
 * @author 2009- jjdelcerro
52
 * 
53
 */
54
public class ApplicationLibrary extends AbstractLibrary {
55

    
56
    private static final Logger logger =
57
        LoggerFactory.getLogger(ApplicationLibrary.class);
58
    
59
    @Override
60
    public void doRegistration() {
61
        registerAsAPI(ApplicationLibrary.class);
62
        require(MapControl.class);
63
        require(InstallerLibrary.class);
64
    }
65
        
66
        @Override
67
        protected void doInitialize() throws LibraryException {
68
                // Do nothing
69
        }
70

    
71
        @Override
72
        protected void doPostInitialize() throws LibraryException {
73
                ApplicationManager manager = ApplicationLocator.getManager();
74
                if (manager == null) {
75
                        throw new ReferenceNotRegisteredException(
76
                                        ApplicationLocator.APPGVSIG_MANAGER_NAME, ApplicationLocator
77
                                                        .getInstance());
78
                }
79
                DefaultProject.registerPersistent();
80
                
81
                /*
82
                 * Copy symbol folders from this plugin's 'Symbols' folder
83
                 * to user's 'Symbols' folder unless a folder with same name
84
                 * exists.
85
                 */
86
                PluginServices ps = PluginsLocator.getManager().getPlugin(
87
                    InitializeApplicationExtension.class);
88
                File folder = ps.getPluginDirectory();
89
                String from_folder = folder.getAbsolutePath() + File.separator
90
                    + "Symbols";
91
                String to_folder = MapContextLocator.getSymbolManager().
92
                    getSymbolPreferences().getSymbolLibraryPath();
93
                
94
                try {
95
                    copyMissingFolders(new File(from_folder), new File(to_folder));
96
                } catch (IOException ioe) {
97
                    ApplicationLocator.getManager().message(
98
                        Messages.getText("_Unable_to_copy_symbols_from_main_plugin"),
99
                        JOptionPane.ERROR_MESSAGE);
100
                    
101
                }
102
                
103
                
104
                
105
        }
106

    
107
    private void copyMissingFolders(File fromf, File tof)
108
        throws IOException {
109
        
110
        if (fromf == null || tof == null) {
111
            return;
112
        }
113
        
114
        if (!fromf.exists() || !fromf.isDirectory() ||
115
            (tof.exists() && tof.isFile())) {
116
            return;
117
        }
118
        
119
        if (!tof.exists()) {
120
            tof.mkdirs();
121
        }
122
        
123
        File[] from_subs = fromf.listFiles();
124
        for (int i=0; i<from_subs.length; i++) {
125
            if (!folderIsOneIn(from_subs[i], tof)) {
126
                logger.info("Copying symbols subfolder: " + from_subs[i].getName());
127
                FileUtils.copyDirectoryToDirectory(from_subs[i], tof);
128
            } else {
129
                logger.info("Symbols subfolder already exists: " + from_subs[i].getName());
130
            }
131
        }
132
    }
133

    
134

    
135
    /**
136
     * Tells whether <folder> is a folder and <tofolder>
137
     * has a file or subfolder with the same short name as 
138
     * <folder>
139
     *  
140
     */
141
    private boolean folderIsOneIn(File folder, File tofolder) {
142
        
143
        if (!folder.isDirectory()) {
144
            return false;
145
        }
146
        
147
        String name = folder.getName();
148
        String tname = tofolder.getAbsolutePath() + File.separator + name;
149
        File tfile = new File(tname);
150
        return tfile.exists();
151
    }
152

    
153

    
154
}