Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.shp.app / org.gvsig.shp.app.mainplugin / src / main / java / org / gvsig / shp / saveEncodingExtension.java @ 43246

History | View | Annotate | Download (4.55 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.shp;
25

    
26
import java.util.ArrayList;
27
import java.util.List;
28
import javax.swing.JOptionPane;
29
import org.gvsig.andami.plugins.Extension;
30
import org.gvsig.app.ApplicationLocator;
31
import org.gvsig.app.ApplicationManager;
32
import org.gvsig.app.project.documents.view.ViewDocument;
33
import org.gvsig.app.project.documents.view.ViewManager;
34
import org.gvsig.fmap.dal.DataStore;
35
import org.gvsig.fmap.mapcontext.MapContext;
36
import org.gvsig.fmap.mapcontext.layers.FLayer;
37
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
38
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
39

    
40
public class saveEncodingExtension extends Extension {
41

    
42
    @Override
43
    public void initialize() {
44
    }
45

    
46
    @Override
47
    public void execute(String actionCommand) {
48
        FLayer[] layers = getActiveLayers();
49
        if( layers == null ) {
50
            return;
51
        }
52
        List<FLayer> problems = new ArrayList<>();
53
        for( FLayer layer : layers ) {
54
            if( layer instanceof SingleLayer ) {
55
                DataStore ds = ((SingleLayer) layer).getDataStore();
56
                try {
57
                    ds.invokeDynMethod("saveEncoding", null);
58
                } catch (DynMethodNotSupportedException ex) {
59
                    // Do nothing
60
                } catch (Throwable ex) {
61
                    logger.warn("Can't save encoding of layer '" + layer.getName() + "'.", ex);
62
                    problems.add(layer);
63
                }
64
            }
65
        }
66
        if( !problems.isEmpty() ) {
67
            StringBuilder builder = new StringBuilder();
68
            builder.append("\n");
69
            for( FLayer problem : problems ) {
70
                builder.append("- ").append(problem.getName()).append("\n");
71
            }
72
            ApplicationManager application = ApplicationLocator.getManager();
73
            application.messageDialog(
74
                "_There_have_been_problems_saving_the_encoding_of_the_following_layers:",
75
                new String[]{builder.toString()},
76
                "_Problems_saving_encoding_of_layer",
77
                JOptionPane.WARNING_MESSAGE,
78
                "_Problems_saving_encoding_of_layer"
79
            );
80

    
81
        }
82
    }
83

    
84
    @Override
85
    public boolean isEnabled() {
86
        FLayer[] layers = getActiveLayers();
87
        if( layers == null ) {
88
            return false;
89
        }
90
        for( FLayer layer : layers ) {
91
            try {
92
                if( layer instanceof SingleLayer ) {
93
                    DataStore ds = ((SingleLayer) layer).getDataStore();
94
                    if( ds.hasDynMethod("saveEncoding") ) {
95
                        return true;
96
                    }
97
                }
98
            } catch (Throwable ex) {
99
            }
100
        }
101
        return false;
102
    }
103

    
104
    @Override
105
    public boolean isVisible() {
106
        FLayer[] layers = getActiveLayers();
107
        if( layers == null ) {
108
            return false;
109
        }
110
        for( FLayer layer : layers ) {
111
            try {
112
                if( layer instanceof SingleLayer ) {
113
                    return true;
114
                }
115
            } catch (Throwable ex) {
116
            }
117
        }
118
        return false;
119
    }
120

    
121
    private FLayer[] getActiveLayers() {
122
        ApplicationManager application = ApplicationLocator.getManager();
123
        ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
124
        if( viewdoc == null ) {
125
            return null;
126
        }
127
        MapContext map = viewdoc.getMapContext();
128
        if( !map.hasActiveLayers() ) {
129
            return null;
130
        }
131
        FLayer[] layers = viewdoc.getMapContext().getLayers().getActives();
132
        return layers;
133
    }
134
}