Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / ProjectMapFactory.java @ 9690

History | View | Annotate | Download (8.89 KB)

1
package com.iver.cit.gvsig.project.documents.layout;
2

    
3
import java.awt.Component;
4
import java.text.DateFormat;
5
import java.util.Arrays;
6
import java.util.Comparator;
7
import java.util.Date;
8
import java.util.Enumeration;
9
import java.util.Hashtable;
10

    
11
import javax.swing.ImageIcon;
12
import javax.swing.JOptionPane;
13

    
14
import com.iver.andami.PluginServices;
15
import com.iver.cit.gvsig.project.Project;
16
import com.iver.cit.gvsig.project.documents.ProjectDocument;
17
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory;
18
import com.iver.cit.gvsig.project.documents.contextMenu.actions.CopyDocumentContextMenuAction;
19
import com.iver.cit.gvsig.project.documents.contextMenu.actions.CutDocumentContextMenuAction;
20
import com.iver.cit.gvsig.project.documents.contextMenu.actions.PasteDocumentContextMenuAction;
21
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
22
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
23
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
24
import com.iver.utiles.XMLEntity;
25

    
26

    
27
/**
28
 * Factory of maps.
29
 *
30
 * @author Vicente Caballero Navarro
31
 */
32
public class ProjectMapFactory extends ProjectDocumentFactory {
33
    public static String registerName = "ProjectMap";
34

    
35
    /**
36
     * Returns image of button.
37
     *
38
     * @return Image button.
39
     */
40
    public ImageIcon getButtonIcon() {
41
        return new ImageIcon(ProjectMap.class.getClassLoader().getResource("images/mapas.png"));
42
    }
43

    
44
    /**
45
     * Returns image of selected button.
46
     *
47
     * @return Image button.
48
     */
49
    public ImageIcon getSelectedButtonIcon() {
50
        return new ImageIcon(ProjectMap.class.getClassLoader().getResource("images/mapas_sel.png"));
51
    }
52

    
53
    /**
54
     * Create a new ProjectDocument.
55
     *
56
     * @param project Opened project.
57
     *
58
     * @return ProjectDocument.
59
     */
60
//    public ProjectDocument create(Project project) {
61
//        String layoutName = "";
62
//        String aux = PluginServices.getText(this, "untitled");
63
//        layoutName = aux + " - " + ProjectMap.numMaps++;
64
//
65
//        if (project != null) {
66
//            // Buscamos si alguna vista ya ten?a este nombre:
67
//            while (existLayoutName(project, layoutName)) {
68
//                layoutName = aux + " - " + ProjectView.numViews++;
69
//            }
70
//        }
71
//
72
//        ProjectMap map = createMap(layoutName);
73
//        map.setModel(new Layout());
74
//        map.getModel().setProjectMap(map);
75
//        map.setProject(project, 0);
76
//        map.setProjectDocumentFactory(this);
77
//
78
//        return map;
79
//    }
80
    public ProjectDocument create(Project project) {
81
            String mapName = "";
82
            String aux = PluginServices.getText(this, "untitled");
83
            int numMaps=((Integer)ProjectDocument.NUMS.get(registerName)).intValue();
84
            mapName = aux + " - " + numMaps++;
85
            if (project != null) {
86
            while (existName(project, mapName)) {
87
                mapName = aux + " - " + numMaps++;
88
            }
89
        }
90
            ProjectDocument.NUMS.put(registerName,new Integer(numMaps));
91
        ProjectMap map = createMap(mapName);
92
        map.setModel(new Layout());
93
        map.getModel().setProjectMap(map);
94
        map.setProject(project, 0);
95
        map.setProjectDocumentFactory(this);
96

    
97
        return map;
98
    }
99
   /**
100
     * Returns the name of registration in the point of extension.
101
     *
102
     * @return Name of registration
103
     */
104
    public String getRegisterName() {
105
        return registerName;
106
    }
107

    
108
    /**
109
     * Returns the name of ProjectDocument.
110
     *
111
     * @return Name of ProjectDocument.
112
     */
113
    public String getNameType() {
114
        return PluginServices.getText(this, "Mapa");
115
    }
116

    
117
    /**
118
     * Create a ne ProjectMap
119
     *
120
     * @param baseName name
121
     *
122
     * @return ProjectMap.
123
     */
124
    private static ProjectMap createMap(String baseName) {
125
        ProjectMap m = new ProjectMap();
126
        m.setName(baseName);
127
        m.setCreationDate(DateFormat.getInstance().format(new Date()));
128
        return m;
129
    }
130

    
131
    /**
132
     * Registers in the points of extension the Factory with alias.
133
     *
134
     */
135
    public static void register() {
136
        register(registerName, new ProjectMapFactory(),
137
            "com.iver.cit.gvsig.project.ProjectMap");
138

    
139
        registerAction(registerName,"copy",new CopyDocumentContextMenuAction());
140
        registerAction(registerName,"cut",new CutDocumentContextMenuAction());
141
        registerAction(registerName,"paste",new PasteDocumentContextMenuAction());
142
    }
143

    
144
    /**
145
     * Returns the priority of de ProjectDocument.
146
     *
147
     * @return Priority.
148
     */
149
    public int getPriority() {
150
        return 2;
151
    }
152

    
153
        public boolean resolveImportXMLConflicts(XMLEntity root, Project project, Hashtable conflicts) {
154
                XMLEntity xmlMaps = root.firstChild("type",this.getRegisterName());
155
                XMLEntity xmlViews = root.firstChild("type",ProjectViewFactory.registerName);
156
                XMLEntity xmlTables = root.firstChild("type",ProjectTableFactory.registerName);
157

    
158
                Hashtable mapsConflits = (Hashtable)conflicts.get(ProjectMapFactory.registerName);
159

    
160
                Hashtable viewsConflits = (Hashtable)conflicts.get(ProjectViewFactory.registerName);
161

    
162
                Hashtable tablesConflits = (Hashtable)conflicts.get(ProjectTableFactory.registerName);
163

    
164

    
165
                if (mapsConflits != null && mapsConflits.size() > 0) {
166
                        int option = JOptionPane.showConfirmDialog(
167
                                        (Component)PluginServices.getMainFrame(),
168
                                        "<html>"+
169
                                                PluginServices.getText(this,"conflicto_de_nombres_de_mapas_al_pegar") + "<br>" +
170
                                                PluginServices.getText(this,"debera_introducir_nombres_para_los_mapas_a_pegar") + "<br>" +
171
                                        "</html>", //Mensaje
172
                                        PluginServices.getText(this,"pegar_mapas"),//titulo
173
                                        JOptionPane.YES_NO_OPTION
174
                                        );
175
                        if (option != JOptionPane.YES_OPTION) {
176
                                return false;
177
                        }
178
                        Enumeration en = mapsConflits.elements();
179
                        while (en.hasMoreElements()) {
180
                                XMLEntity map = (XMLEntity)en.nextElement();
181
                                String newName = JOptionPane.showInputDialog(
182
                                                (Component)PluginServices.getMainFrame(),
183
                                                "<html>"+
184
                                                        PluginServices.getText(this,"nuevo_nombre_para_el_mapa") +" "+  map.getStringProperty("name") + ":" +
185
                                            "</html>", //Mensaje
186
                                                map.getStringProperty("name") //Valor por defecto
187
                                                );
188
                                if (newName == null) {
189
                                        JOptionPane.showMessageDialog(
190
                                                        (Component)PluginServices.getMainFrame(),
191
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
192
                                                        PluginServices.getText(this,"pegar_mapas"),//titulo
193
                                                        JOptionPane.ERROR_MESSAGE
194
                                                        );
195
                                } else if (newName.equalsIgnoreCase(map.getStringProperty("name")) ) {
196
                                        JOptionPane.showMessageDialog(
197
                                                        (Component)PluginServices.getMainFrame(),
198
                                                        "<html>"+
199
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
200
                                                                PluginServices.getText(this,"nombre_no_valido")+
201
                                                        "</html>",//Mensaje
202
                                                        PluginServices.getText(this,"pegar_mapas"),//titulo
203
                                                        JOptionPane.ERROR_MESSAGE
204
                                                        );
205
                                        return false;
206
                                }
207
                                map.setName(newName);
208
                        }
209
                }
210

    
211
                if (viewsConflits != null && viewsConflits.size() > 0) {
212
                        int option = JOptionPane.showConfirmDialog(
213
                                        (Component)PluginServices.getMainFrame(),
214
                                        "<html>"+
215
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
216
                                                PluginServices.getText(this,"no_se_pegaran_las_vistas_del_conflicto") + "<br>" +
217
                                                PluginServices.getText(this,"desea_continuar") +
218
                                        "</html>",
219
                                        PluginServices.getText(this,"pegar_mapas"),//titulo
220
                                        JOptionPane.YES_NO_OPTION
221
                                        );
222
                        if (option != JOptionPane.YES_OPTION) {
223
                                return false;
224
                        }
225
                        // Eliminamos las vistas del xml que no vamos a importar
226

    
227
                        // Esto me devuelve los indices en orden inverso
228
                        int[] indexes = this.getIndexOfConflict(viewsConflits);
229
                        for (int i=0;i < indexes.length;i++) {
230
                                xmlViews.removeChild(indexes[i]);
231
                        }
232
                        viewsConflits = null;
233

    
234
                }
235

    
236

    
237
                if (tablesConflits != null && tablesConflits.size() > 0) {
238
                        int option = JOptionPane.showConfirmDialog(
239
                                        (Component)PluginServices.getMainFrame(),
240
                                        "<html>" +
241
                                                PluginServices.getText(this,"conflito_de_nombres_de_tablas_al_pegar") + "<br>" +
242
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
243
                                                PluginServices.getText(this,"desea_continuar") +
244
                                        "</html>", //Mensaje
245
                                        PluginServices.getText(this,"pegar_mapas"),
246
                                        JOptionPane.YES_NO_OPTION
247
                                        );
248
                        if (option != JOptionPane.YES_OPTION) {
249
                                return false;
250
                        }
251
                        xmlTables.removeAllChildren();
252
                }
253

    
254

    
255
                return true;
256
        }
257

    
258
        /**
259
         * Devuelve las claves de conflits ordenados
260
         * en orden inverso. Las claves se esperan que
261
         * sean instancias de Integer
262
         */
263
        private int[] getIndexOfConflict(Hashtable conflits) {
264
                Object[] tmpArray = conflits.keySet().toArray();
265
                Arrays.sort(tmpArray,new Comparator() {
266
                        public int compare(Object o1, Object o2) {
267
                                return ((Integer)o2).intValue() - ((Integer)o1).intValue();
268
                        }
269
                }
270
                );
271
                int[] indexes = new int[] {tmpArray.length};
272
                for (int i = 0;i< tmpArray.length;i++) {
273
                        indexes[i] = ((Integer)tmpArray[i]).intValue();
274
                }
275
                return indexes;
276

    
277

    
278
        }
279
}