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 @ 43987

History | View | Annotate | Download (5.4 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.gvsig.andami.PluginServices;
33
import org.gvsig.andami.PluginsLocator;
34
import org.gvsig.app.extension.InitializeApplicationExtension;
35
import org.gvsig.app.gui.preferencespage.AppSymbolPreferences;
36
import org.gvsig.app.project.DefaultProject;
37
import org.gvsig.app.project.ProjectSymbolTable;
38
import org.gvsig.expressionevaluator.ExpressionEvaluatorLibrary;
39
import org.gvsig.fmap.mapcontext.MapContextLibrary;
40
import org.gvsig.fmap.mapcontext.MapContextLocator;
41
import org.gvsig.fmap.mapcontext.MapContextManager;
42
import org.gvsig.fmap.mapcontrol.MapControlLibrary;
43
import org.gvsig.i18n.Messages;
44
import org.gvsig.installer.lib.api.InstallerLibrary;
45
import org.gvsig.tools.library.AbstractLibrary;
46
import org.gvsig.tools.library.LibraryException;
47
import org.gvsig.tools.locator.ReferenceNotRegisteredException;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

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

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

    
75
        @Override
76
        protected void doPostInitialize() throws LibraryException {
77
                ApplicationManager manager = ApplicationLocator.getManager();
78
                if (manager == null) {
79
                        throw new ReferenceNotRegisteredException(
80
                                        ApplicationLocator.APPGVSIG_MANAGER_NAME, ApplicationLocator
81
                                                        .getInstance());
82
                }
83
                DefaultProject.registerPersistent();
84

    
85
                MapContextManager mapContextManager = MapContextLocator.getMapContextManager();
86

    
87
                PluginServices plugin = PluginsLocator.getManager().getPlugin(InitializeApplicationExtension.class);
88
                mapContextManager.setColorTableLibraryFolder(new File(plugin.getPluginHomeFolder(),"colortable"));
89
                
90
                MapContextLocator.getSymbolManager().setSymbolPreferences(new AppSymbolPreferences());
91
                
92
                installBaseSymbols();
93
                ProjectSymbolTable.selfRegister();
94
        }
95

    
96
        /**
97
         * Copy symbol folders from this plugin's 'Symbols' folder
98
         * to user's 'Symbols' folder unless a folder with same name
99
         * exists.
100
         */
101
        private void installBaseSymbols() {
102
                PluginServices ps = PluginsLocator.getManager().getPlugin(InitializeApplicationExtension.class);
103
                File from_folder = new File( ps.getPluginDirectory(), "Symbols");
104
                String to_folder = MapContextLocator.getSymbolManager().
105
                            getSymbolPreferences().getSymbolLibraryPath();
106
                try {
107
                    copyMissingFolders(from_folder, new File(to_folder));
108
                } catch (IOException ioe) {
109
                    ApplicationLocator.getManager().message(
110
                        Messages.getText("_Unable_to_copy_symbols_from_main_plugin"),
111
                        JOptionPane.ERROR_MESSAGE);
112
                }
113
        
114
        }
115
        
116
    private void copyMissingFolders(File fromf, File tof)
117
        throws IOException {
118
        
119
        if (fromf == null || tof == null) {
120
            return;
121
        }
122
        
123
        if (!fromf.exists() || !fromf.isDirectory() ||
124
            (tof.exists() && tof.isFile())) {
125
            return;
126
        }
127
        
128
        if (!tof.exists()) {
129
            tof.mkdirs();
130
        }
131
        
132
        File[] from_subs = fromf.listFiles();
133
        for (int i=0; i<from_subs.length; i++) {
134
            if (!folderIsOneIn(from_subs[i], tof)) {
135
                logger.info("Copying symbols subfolder: " + from_subs[i].getName());
136
                FileUtils.copyDirectoryToDirectory(from_subs[i], tof);
137
            } else {
138
                logger.info("Symbols subfolder already exists: " + from_subs[i].getName());
139
            }
140
        }
141
    }
142

    
143

    
144
    /**
145
     * Tells whether <folder> is a folder and <tofolder>
146
     * has a file or subfolder with the same short name as 
147
     * <folder>
148
     *  
149
     */
150
    private boolean folderIsOneIn(File folder, File tofolder) {
151
        
152
        if (!folder.isDirectory()) {
153
            return false;
154
        }
155
        
156
        String name = folder.getName();
157
        String tname = tofolder.getAbsolutePath() + File.separator + name;
158
        File tfile = new File(tname);
159
        return tfile.exists();
160
    }
161

    
162

    
163
}