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

History | View | Annotate | Download (5.67 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.app.project.documents.view.expressionevaluator.SavedPointsElementFactory;
39
import org.gvsig.app.project.documents.view.expressionevaluator.ViewElementFactory;
40
import org.gvsig.expressionevaluator.ExpressionEvaluatorLibrary;
41
import org.gvsig.fmap.mapcontext.MapContextLibrary;
42
import org.gvsig.fmap.mapcontext.MapContextLocator;
43
import org.gvsig.fmap.mapcontext.MapContextManager;
44
import org.gvsig.fmap.mapcontrol.MapControlLibrary;
45
import org.gvsig.i18n.Messages;
46
import org.gvsig.installer.lib.api.InstallerLibrary;
47
import org.gvsig.tools.library.AbstractLibrary;
48
import org.gvsig.tools.library.LibraryException;
49
import org.gvsig.tools.locator.ReferenceNotRegisteredException;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

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

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

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

    
87
                MapContextManager mapContextManager = MapContextLocator.getMapContextManager();
88

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

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

    
147

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

    
166

    
167
}