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 / extension / InitializeApplicationExtension.java @ 44276

History | View | Annotate | Download (15 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 41576 jjdelcerro
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 40558 jjdelcerro
 *
11 41576 jjdelcerro
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 40558 jjdelcerro
 *
16 41576 jjdelcerro
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 40558 jjdelcerro
 *
20 41576 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40558 jjdelcerro
 */
23 40435 jjdelcerro
package org.gvsig.app.extension;
24
25 43095 jjdelcerro
import java.awt.GridBagConstraints;
26
import java.beans.PropertyVetoException;
27 40435 jjdelcerro
import java.io.BufferedReader;
28 44238 jjdelcerro
import java.io.File;
29 40435 jjdelcerro
import java.io.InputStream;
30
import java.io.InputStreamReader;
31
import java.io.StringWriter;
32 41735 jjdelcerro
import java.util.HashMap;
33
import java.util.Map;
34 40435 jjdelcerro
import java.util.Properties;
35 44238 jjdelcerro
import java.util.prefs.Preferences;
36 41620 jjdelcerro
import javax.swing.JOptionPane;
37 41706 jjdelcerro
import org.apache.commons.lang3.StringUtils;
38 40435 jjdelcerro
39
import org.gvsig.andami.IconThemeHelper;
40 41620 jjdelcerro
import org.gvsig.andami.PluginServices;
41 40435 jjdelcerro
import org.gvsig.andami.PluginsLocator;
42
import org.gvsig.andami.PluginsManager;
43 41620 jjdelcerro
import org.gvsig.andami.actioninfo.ActionInfoManager;
44 40435 jjdelcerro
import org.gvsig.andami.plugins.Extension;
45 43095 jjdelcerro
import org.gvsig.andami.ui.mdiManager.IWindow;
46 40435 jjdelcerro
import org.gvsig.app.ApplicationLocator;
47
import org.gvsig.app.ApplicationManager;
48 44238 jjdelcerro
import static org.gvsig.app.extension.ProjectExtension.PROJECT_FILE_CHOOSER_ID;
49 43095 jjdelcerro
import org.gvsig.app.project.ProjectManager;
50
import org.gvsig.app.project.ProjectPreferences;
51
import org.gvsig.app.project.documents.view.ViewDocument;
52
import org.gvsig.app.project.documents.view.ViewManager;
53
import org.gvsig.app.project.documents.view.gui.IView;
54 40435 jjdelcerro
import org.gvsig.app.project.documents.view.toolListeners.InfoListener;
55
import org.gvsig.app.util.BaseOpenErrorHandler;
56
import org.gvsig.fmap.dal.DALLocator;
57 44129 jjdelcerro
import org.gvsig.fmap.dal.DataManager;
58 40435 jjdelcerro
import org.gvsig.fmap.dal.OpenErrorHandler;
59 44189 jjdelcerro
import org.gvsig.fmap.dal.feature.AbstractDataProfile;
60 44253 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
61 40435 jjdelcerro
import org.gvsig.fmap.dal.resource.ResourceManager;
62
import org.gvsig.fmap.dal.resource.exception.DisposeResorceManagerException;
63 44238 jjdelcerro
import static org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanelController.OPEN_LAYER_FILE_CHOOSER_ID;
64 40435 jjdelcerro
import org.gvsig.installer.lib.api.InstallerLocator;
65
import org.gvsig.installer.lib.api.InstallerManager;
66
import org.gvsig.installer.lib.api.PackageInfo;
67 41620 jjdelcerro
import org.gvsig.tools.ToolsLocator;
68
import org.gvsig.tools.dynobject.DynObject;
69 44253 jjdelcerro
import org.gvsig.tools.dynobject.Tags;
70 44238 jjdelcerro
import org.gvsig.tools.folders.FoldersManager;
71 41620 jjdelcerro
import org.gvsig.tools.i18n.I18nManager;
72 44129 jjdelcerro
import org.gvsig.tools.swing.api.SimpleImage;
73
import org.gvsig.tools.swing.api.ToolsSwingLocator;
74
import org.gvsig.tools.swing.api.ToolsSwingManager;
75 40435 jjdelcerro
import org.slf4j.Logger;
76
import org.slf4j.LoggerFactory;
77
78
public class InitializeApplicationExtension extends Extension {
79 41576 jjdelcerro
80 40435 jjdelcerro
    private static final Logger logger = LoggerFactory
81
            .getLogger(InitializeApplicationExtension.class);
82
83 44238 jjdelcerro
    // Copiadas de org.gvsig.coreplugin.PreferencesExtension
84
    public static final String PROJECTS_FOLDER_PROPERTY_NAME = "ProjectsFolder";
85
    public static final String DATA_FOLDER_PROPERTY_NAME = "DataFolder";
86
    public static final String TEMPLATES_FOLDER_PROPERTY_NAME = "TemplatesFolder";
87
    public static final String SYMBOL_LIBRARY_FOLDER_PROPERTY_NAME = "SymbolLibraryFolder";
88 40435 jjdelcerro
89 44238 jjdelcerro
        private OpenErrorHandler openErrorHandler = null;
90
91 44189 jjdelcerro
    private static class SimpleImageDataProfile extends AbstractDataProfile {
92
93
        public SimpleImageDataProfile() {
94
            super("Image", SimpleImage.class);
95
        }
96
97
        @Override
98 44253 jjdelcerro
        public Object createData(Object data, Tags tags) {
99 44189 jjdelcerro
            final ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
100
            SimpleImage image = toolsSwingManager.createSimpleImage(data);
101
            return image;
102
        }
103
    }
104
105 43095 jjdelcerro
    @Override
106 41576 jjdelcerro
    public void initialize() {
107 42775 jjdelcerro
        DALLocator.registerFeatureTypeDefinitionsManager(DefaultFeatureTypeDefinitionsManager.class);
108 44129 jjdelcerro
109
        DataManager dataManager = DALLocator.getDataManager();
110 44189 jjdelcerro
        dataManager.registerDataProfile(new SimpleImageDataProfile());
111 44129 jjdelcerro
112 41576 jjdelcerro
        InfoListener.initializeExtensionPoint();
113 44238 jjdelcerro
114
        initLastPath();
115 41576 jjdelcerro
        registerIcons();
116 40435 jjdelcerro
117 41576 jjdelcerro
    }
118 44238 jjdelcerro
119
    private void initLastPath() {
120
        Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
121
        FoldersManager folderManager = ToolsLocator.getFoldersManager();
122
        String v;
123
124
        v = prefs.get(DATA_FOLDER_PROPERTY_NAME, folderManager.getHome().getAbsolutePath());
125
        folderManager.setLastPath(OPEN_LAYER_FILE_CHOOSER_ID, new File(v));
126 40435 jjdelcerro
127 44238 jjdelcerro
        v = prefs.get(PROJECTS_FOLDER_PROPERTY_NAME, folderManager.getHome().getAbsolutePath());
128
        folderManager.setLastPath(PROJECT_FILE_CHOOSER_ID, new File(v));
129
130
        v = prefs.get("LegendsFolder", folderManager.getHome().getAbsolutePath());
131
        folderManager.setLastPath("LegendsFolder", new File(v));
132
    }
133
134 43095 jjdelcerro
    @Override
135 41576 jjdelcerro
    public void postInitialize() {
136 43095 jjdelcerro
        PluginsManager pluginManager = PluginsLocator.getManager();
137
138 40435 jjdelcerro
        openErrorHandler = new BaseOpenErrorHandler();
139
        ApplicationLocator.getManager().getDataManager().setOpenErrorHandler(openErrorHandler);
140 41564 jjdelcerro
141 41576 jjdelcerro
        DALLocator.getResourceManager().startResourceCollector(
142
                3 * (60 * 1000), // minutes --> miliseconds
143
                null);
144 43095 jjdelcerro
145 41913 jjdelcerro
        // showAddOnManagerOnStartIfNeed();
146 43095 jjdelcerro
        ProjectPreferences projectPreferences = ApplicationLocator.getProjectManager().getProjectPreferences();
147
        if (projectPreferences.getOpenWithANewMaximizedView()) {
148
            pluginManager.addStartupTask(
149
                    "showNewViewMaximized",
150
                    new ShowNewMaximizedView(),
151
                    true,
152 44075 jjdelcerro
                    750
153 43095 jjdelcerro
            );
154
        }
155 44075 jjdelcerro
        pluginManager.addStartupTask("OpenLayerOnStart", new OpenLayerOnStart(), true, 740);
156
157
        pluginManager.addStartupTask("ApplicationStartedMessage", new Runnable() {
158
            @Override
159
            public void run() {
160
                I18nManager i18n = ToolsLocator.getI18nManager();
161
                ApplicationManager application = ApplicationLocator.getManager();
162
                application.message(
163
                        i18n.getTranslation("StatusBar.Aplicacion_iniciada"),
164
                        JOptionPane.INFORMATION_MESSAGE
165
                );
166
            }
167
        }, true, -1);
168 43095 jjdelcerro
169 41913 jjdelcerro
    }
170
171
    private void showAddOnManagerOnStartIfNeed() {
172 41620 jjdelcerro
        PluginsManager pluginManager = PluginsLocator.getManager();
173
        PluginServices plugin = pluginManager.getPlugin(this);
174
        DynObject pluginProperties = plugin.getPluginProperties();
175 43095 jjdelcerro
        Map<String, Boolean> showAddOnmanagerOnStart = null;
176 41735 jjdelcerro
        try {
177 43095 jjdelcerro
            showAddOnmanagerOnStart = (Map<String, Boolean>) pluginProperties.getDynValue("showAddOnmanagerOnStart");
178
        } catch (Exception ex) {
179 41735 jjdelcerro
            // Do nothing
180
        }
181 43095 jjdelcerro
        if (showAddOnmanagerOnStart == null) {
182
            showAddOnmanagerOnStart = new HashMap<>();
183
            pluginProperties.setDynValue("showAddOnmanagerOnStart", showAddOnmanagerOnStart);
184 41735 jjdelcerro
        }
185 43095 jjdelcerro
        Boolean showAddOnmanagerOnStartForCurrentVersion = showAddOnmanagerOnStart.get(pluginManager.getApplicationVersion().fullFormat());
186
187
        if (showAddOnmanagerOnStartForCurrentVersion == null || showAddOnmanagerOnStartForCurrentVersion) {
188 41620 jjdelcerro
            pluginManager.addStartupTask(
189 43095 jjdelcerro
                    "showAddOnmanagerOnStart",
190 41620 jjdelcerro
                    new ShowAddOnManagerOnStart(),
191 43095 jjdelcerro
                    true,
192 41620 jjdelcerro
                    500
193
            );
194
        }
195 41576 jjdelcerro
    }
196 43095 jjdelcerro
197
    private static class ShowNewMaximizedView implements Runnable {
198
199
        @Override
200
        public void run() {
201
            ApplicationManager application = ApplicationLocator.getManager();
202
            ProjectManager projectManager = application.getProjectManager();
203
204
            ViewManager viewManager = (ViewManager) projectManager.getDocumentManager(ViewManager.TYPENAME);
205
            ViewDocument view = (ViewDocument) viewManager.createDocument();
206
            projectManager.getCurrentProject().add(view);
207
208
            IView viewWindow = (IView) viewManager.getMainWindow(view);
209
210
            application.getUIManager().addWindow(viewWindow, GridBagConstraints.CENTER);
211
            try {
212
                application.getUIManager().setMaximum((IWindow) viewWindow, true);
213
            } catch (PropertyVetoException e) {
214
                logger.info("Can't maximize view.", e);
215
            }
216
        }
217
218
    }
219
220 41620 jjdelcerro
    private static class ShowAddOnManagerOnStart implements Runnable {
221
222 43095 jjdelcerro
        @Override
223 41620 jjdelcerro
        public void run() {
224
            ApplicationManager application = ApplicationLocator.getManager();
225
            I18nManager i18nManager = ToolsLocator.getI18nManager();
226
            PluginsManager pluginManager = PluginsLocator.getManager();
227
            PluginServices plugin = pluginManager.getPlugin(this);
228
            DynObject pluginProperties = plugin.getPluginProperties();
229
230 41723 jjdelcerro
            String msg = i18nManager.getTranslation("_Instalar_complementos_adicionales_no_disponibles_durante_el_proceso_de_instalacion");
231 41620 jjdelcerro
            int resp = application.confirmDialog(
232 43095 jjdelcerro
                    msg,
233
                    i18nManager.getTranslation("_Finalizando_proceso_de_instalacion"),
234
                    JOptionPane.YES_NO_OPTION,
235 41620 jjdelcerro
                    JOptionPane.QUESTION_MESSAGE
236
            );
237 43095 jjdelcerro
            if (resp == JOptionPane.YES_OPTION) {
238 41620 jjdelcerro
                ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
239
                actionManager.execute(
240
                        "tools-addonsmanager",
241 43095 jjdelcerro
                        new Object[]{"skipBundleSelection"}
242 41620 jjdelcerro
                );
243
            }
244 43095 jjdelcerro
            Map<String, Boolean> showAddOnmanagerOnStart = (Map<String, Boolean>) pluginProperties.getDynValue("showAddOnmanagerOnStart");
245 41735 jjdelcerro
            showAddOnmanagerOnStart.put(pluginManager.getApplicationVersion().fullFormat(), Boolean.FALSE);
246 41620 jjdelcerro
        }
247
    }
248 43095 jjdelcerro
249 41576 jjdelcerro
    private void registerIcons() {
250
        IconThemeHelper.registerIcon("action", "edit-clear", this);
251
        IconThemeHelper.registerIcon("action", "edit-copy", this);
252
        IconThemeHelper.registerIcon("action", "edit-cut", this);
253
        IconThemeHelper.registerIcon("action", "edit-delete", this);
254
        IconThemeHelper.registerIcon("action", "edit-find", this);
255
        IconThemeHelper.registerIcon("action", "edit-find-replace", this);
256
        IconThemeHelper.registerIcon("action", "edit-paste", this);
257
        IconThemeHelper.registerIcon("action", "edit-redo", this);
258
        IconThemeHelper.registerIcon("action", "edit-select-all", this);
259
        IconThemeHelper.registerIcon("action", "edit-undo", this);
260
        IconThemeHelper.registerIcon("action", "edit-undo-redo-actions", this);
261
        IconThemeHelper.registerIcon("action", "document-print", this);
262 40435 jjdelcerro
263 41576 jjdelcerro
        IconThemeHelper.registerIcon("toolbar-go", "go-next", this);
264
        IconThemeHelper.registerIcon("toolbar-go", "go-previous", this);
265
        IconThemeHelper.registerIcon("toolbar-go", "go-next-fast", this);
266
        IconThemeHelper.registerIcon("toolbar-go", "go-previous-fast", this);
267
        IconThemeHelper.registerIcon("toolbar-go", "go-first", this);
268
        IconThemeHelper.registerIcon("toolbar-go", "go-last", this);
269 40435 jjdelcerro
270 41576 jjdelcerro
    }
271 40435 jjdelcerro
272 43095 jjdelcerro
    @Override
273 41576 jjdelcerro
    public void execute(String actionCommand) {
274 40435 jjdelcerro
275 41576 jjdelcerro
    }
276 40435 jjdelcerro
277 43095 jjdelcerro
    @Override
278 41576 jjdelcerro
    public boolean isEnabled() {
279
        return false;
280
    }
281 40435 jjdelcerro
282 43095 jjdelcerro
    @Override
283 41576 jjdelcerro
    public boolean isVisible() {
284
        return false;
285
    }
286 40435 jjdelcerro
287 41576 jjdelcerro
    private void addToLogInfo() {
288
        String info[] = this.getStringInfo().split("\n");
289 43095 jjdelcerro
        for (String line : info) {
290
            logger.info(line);
291 41576 jjdelcerro
        }
292
    }
293 40435 jjdelcerro
294 41576 jjdelcerro
    public String getStringInfo() {
295
        ApplicationManager application = ApplicationLocator.getManager();
296
        PluginsManager pluginmgr = PluginsLocator.getManager();
297
        InstallerManager installmgr = InstallerLocator.getInstallerManager();
298 40435 jjdelcerro
299 41576 jjdelcerro
        StringWriter writer = new StringWriter();
300 40435 jjdelcerro
301 41576 jjdelcerro
        Properties props = System.getProperties();
302 40435 jjdelcerro
303 41576 jjdelcerro
        // OS information
304
        String osName = props.getProperty("os.name");
305
        writer.write("OS\n");
306
        writer.write("    name   : " + osName + "\n");
307
        writer.write("    arch   : " + props.get("os.arch") + "\n");
308
        writer.write("    version: " + props.get("os.version") + "\n");
309
        if (osName.startsWith("Linux")) {
310
            try {
311
                String[] command = {"lsb_release", "-a"};
312
                Process p = Runtime.getRuntime().exec(command);
313
                InputStream is = p.getInputStream();
314
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
315
                String line;
316
                while ((line = reader.readLine()) != null) {
317
                    writer.write("    " + line + "\n");
318
                }
319
            } catch (Exception ex) {
320
                writer.write("Can't get detailled os information (lsb_release -a).");
321
            }
322
        }
323 40435 jjdelcerro
324 41576 jjdelcerro
        // JRE information
325
        writer.write("JRE\n");
326
        writer.write("    vendor : " + props.get("java.vendor") + "\n");
327
        writer.write("    version: " + props.get("java.version") + "\n");
328
        writer.write("    home   : " + props.get("java.home") + "\n");
329 40435 jjdelcerro
330 41576 jjdelcerro
        writer.write("gvSIG\n");
331
        writer.write("    version                 : " + application.getVersion().getLongFormat() + "\n");
332
        writer.write("    locale language         : " + application.getLocaleLanguage() + "\n");
333
        writer.write("    application forlder     : " + pluginmgr.getApplicationFolder() + "\n");
334
        writer.write("    install forlder         : " + pluginmgr.getInstallFolder() + "\n");
335
        writer.write("    application home forlder: " + pluginmgr.getApplicationHomeFolder() + "\n");
336 41706 jjdelcerro
        writer.write("    plugins forlder         : " + StringUtils.join(pluginmgr.getPluginsFolders()) + "\n");
337 41576 jjdelcerro
338
        try {
339 41706 jjdelcerro
            PackageInfo[] pkgs = installmgr.getInstalledPackages();
340 41576 jjdelcerro
            writer.write("Installed packages\n");
341 43095 jjdelcerro
            for (PackageInfo pkg : pkgs) {
342 41576 jjdelcerro
                writer.write("    ");
343 43095 jjdelcerro
                writer.write(pkg.toStringCompact());
344 41576 jjdelcerro
                writer.write("\n");
345
            }
346
        } catch (Throwable e) {
347
            writer.write("Can't get installed package information.");
348
        }
349
        return writer.toString();
350
    }
351
352 43095 jjdelcerro
    @Override
353 41576 jjdelcerro
    public void terminate() {
354
        ResourceManager resMan = DALLocator.getResourceManager();
355
        resMan.stopResourceCollector();
356
        try {
357
            resMan.dispose();
358
        } catch (DisposeResorceManagerException e) {
359
            logger.warn(e.getMessageStack());
360
        }
361
        super.terminate();
362
    }
363 40435 jjdelcerro
}