Statistics
| Revision:

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

History | View | Annotate | Download (9.02 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.ArrayList;
6
import java.util.Arrays;
7
import java.util.Comparator;
8
import java.util.Date;
9
import java.util.Enumeration;
10
import java.util.Hashtable;
11

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

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

    
27

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

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

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

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

    
88
        return map;
89
    }
90
    private boolean existLayoutName(Project project, String layoutName) {
91
        ArrayList viewList = project.getDocumentsByType(getRegisterName());
92

    
93
        for (int i = 0; i < viewList.size(); i++) {
94
            ProjectMap pm = (ProjectMap) viewList.get(i);
95
            String title = pm.getName();
96

    
97
            if (title.compareTo(layoutName) == 0) {
98
                return true;
99
            }
100
        }
101

    
102
        return false;
103
    }
104
    /**
105
     * Returns the name of registration in the point of extension.
106
     *
107
     * @return Name of registration
108
     */
109
    public String getRegisterName() {
110
        return registerName;
111
    }
112

    
113
    /**
114
     * Returns the name of ProjectDocument.
115
     *
116
     * @return Name of ProjectDocument.
117
     */
118
    public String getNameType() {
119
        return PluginServices.getText(this, "Mapa");
120
    }
121

    
122
    /**
123
     * Create a ne ProjectMap
124
     *
125
     * @param baseName name
126
     *
127
     * @return ProjectMap.
128
     */
129
    public static ProjectMap createMap(String baseName) {
130
        ProjectMap m = new ProjectMap();
131
        m.setName(baseName + " - " + ProjectMap.numMaps);
132
        m.setCreationDate(DateFormat.getInstance().format(new Date()));
133
        ProjectMap.numMaps++;
134
        return m;
135
    }
136

    
137
    /**
138
     * Registers in the points of extension the Factory with alias.
139
     *
140
     */
141
    public static void register() {
142
        register(registerName, new ProjectMapFactory(),
143
            "com.iver.cit.gvsig.project.ProjectMap");
144

    
145
        registerAction(registerName,"copy",new CopyDocumentContextMenuAction());
146
        registerAction(registerName,"cut",new CutDocumentContextMenuAction());
147
        registerAction(registerName,"paste",new PasteDocumentContextMenuAction());
148
    }
149

    
150
    /**
151
     * Returns the priority of de ProjectDocument.
152
     *
153
     * @return Priority.
154
     */
155
    public int getPriority() {
156
        return 2;
157
    }
158

    
159
        public boolean resolveImportXMLConflicts(XMLEntity root, Project project, Hashtable conflicts) {
160
                XMLEntity xmlMaps = root.firstChild("type",this.getRegisterName());
161
                XMLEntity xmlViews = root.firstChild("type",ProjectViewFactory.registerName);
162
                XMLEntity xmlTables = root.firstChild("type",ProjectTableFactory.registerName);
163

    
164
                Hashtable mapsConflits = (Hashtable)conflicts.get(ProjectMapFactory.registerName);
165

    
166
                Hashtable viewsConflits = (Hashtable)conflicts.get(ProjectViewFactory.registerName);
167

    
168
                Hashtable tablesConflits = (Hashtable)conflicts.get(ProjectTableFactory.registerName);
169

    
170

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

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

    
233
                        // Esto me devuelve los indices en orden inverso
234
                        int[] indexes = this.getIndexOfConflict(viewsConflits);
235
                        for (int i=0;i < indexes.length;i++) {
236
                                xmlViews.removeChild(indexes[i]);
237
                        }
238
                        viewsConflits = null;
239

    
240
                }
241

    
242

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

    
260

    
261
                return true;
262
        }
263

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

    
283

    
284
        }
285
}