Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / ProjectViewFactory.java @ 7758

History | View | Annotate | Download (7.42 KB)

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

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

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

    
13
import com.iver.andami.PluginServices;
14
import com.iver.cit.gvsig.fmap.MapContext;
15
import com.iver.cit.gvsig.fmap.ViewPort;
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.table.ProjectTableFactory;
23
import com.iver.utiles.XMLEntity;
24

    
25

    
26
/**
27
 * Factory of View.
28
 *
29
 * @author Vicente Caballero Navarro
30
 */
31
public class ProjectViewFactory extends ProjectDocumentFactory {
32
    public static String registerName = "ProjectView";
33

    
34

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

    
44
    /**
45
     * Returns image of selected button.
46
     *
47
     * @return Image button.
48
     */
49
    public ImageIcon getSelectedButtonIcon() {
50
        return new ImageIcon(this.getClass().getClassLoader().getResource("images/Vista_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 viewName = "";
62
        String aux = PluginServices.getText(this, "untitled");
63
        viewName = aux + " - " + ProjectView.numViews++;
64

    
65
        if (project != null) {
66
            // Buscamos si alguna vista ya ten?a este nombre:
67
            while (existViewName(project, viewName)) {
68
                viewName = aux + " - " + ProjectView.numViews++;
69
            }
70
        }
71

    
72
        ProjectView vista = createView(viewName);
73
        vista.setProject(project, 0);
74
        vista.setProjectDocumentFactory(this);
75

    
76
        return vista;
77
    }
78

    
79
    /**
80
     * Create a new ProjectView.
81
     *
82
     * @param baseName name
83
     *
84
     * @return ProjectView.
85
     */
86
    private static ProjectView createView(String viewName) {
87
        ProjectView v = new ProjectView();
88
        v.setMapContext(new MapContext(
89
                new ViewPort(Project.getDefaultProjection())));
90
        v.setMapOverViewContext(new MapContext(null));
91
        v.getMapOverViewContext().setProjection(v.getMapContext().getProjection());
92
        v.setName(viewName);
93
        v.setCreationDate(DateFormat.getInstance().format(new Date()));
94

    
95
        return v;
96
    }
97

    
98
    /**
99
     *
100
     *
101
     * @param project DOCUMENT ME!
102
     * @param viewName DOCUMENT ME!
103
     *
104
     * @return DOCUMENT ME!
105
     */
106
    private boolean existViewName(Project project, String viewName) {
107
        ArrayList viewList = project.getDocumentsByType(getRegisterName());
108

    
109
        for (int i = 0; i < viewList.size(); i++) {
110
            ProjectView pv = (ProjectView) viewList.get(i);
111
            String title = pv.getName();
112

    
113
            if (title.compareTo(viewName) == 0) {
114
                return true;
115
            }
116
        }
117

    
118
        return false;
119
    }
120

    
121
    /**
122
     * Returns the name of registration in the point of extension.
123
     *
124
     * @return Name of registration
125
     */
126
    public String getRegisterName() {
127
        return registerName;
128
    }
129

    
130
    /**
131
     * Returns the name of ProjectDocument.
132
     *
133
     * @return Name of ProjectDocument.
134
     */
135
    public String getNameType() {
136
        return PluginServices.getText(this, "Vista");
137
    }
138

    
139
    /**
140
     * Registers in the points of extension the Factory with alias.
141
     *
142
     */
143
    public static void register() {
144
        register(registerName, new ProjectViewFactory(),
145
            "com.iver.cit.gvsig.project.ProjectView");
146
        
147
        registerAction(registerName,"copy",new CopyDocumentContextMenuAction());
148
        registerAction(registerName,"cut",new CutDocumentContextMenuAction());
149
        registerAction(registerName,"paste",new PasteDocumentContextMenuAction());
150
    }
151

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

    
161
        public boolean resolveImportXMLConflicts(XMLEntity root, Project project, Hashtable conflicts) {
162
                Hashtable viewsConflits = (Hashtable)conflicts.get(this.getRegisterName());
163
                Hashtable tablesConflits = (Hashtable)conflicts.get(ProjectTableFactory.registerName);
164
                XMLEntity xmlTables = root.firstChild("type",ProjectTableFactory.registerName);
165
                
166
                
167
                if (viewsConflits != null && viewsConflits.size() > 0) {
168
                        int option = JOptionPane.showConfirmDialog(
169
                                        (Component)PluginServices.getMainFrame(),
170
                                        "<html>"+
171
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
172
                                                PluginServices.getText(this,"debera_introducir_nombres_para_las_vistas_a_pegar") + "<br>" +
173
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
174
                                                PluginServices.getText(this,"desea_continuar") +
175
                                        "</html>",
176
                                        PluginServices.getText(this,"pegar_vistas"),
177
                                        JOptionPane.YES_NO_OPTION
178
                                        );
179
                        if (option != JOptionPane.YES_OPTION) {
180
                                return false;
181
                        }
182
                        Enumeration en = viewsConflits.elements();
183
                        while (en.hasMoreElements()) {
184
                                XMLEntity view = (XMLEntity)en.nextElement();
185
                                String newName = JOptionPane.showInputDialog(
186
                                                (Component)PluginServices.getMainFrame(),
187
                                                "<html>"+
188
                                                        PluginServices.getText(this,"introduzca_nuevo_nombre_para_la_vista") +" "+  view.getStringProperty("name") + ":" +
189
                                                "</html>", //Mensaje
190
                                                view.getStringProperty("name") //Valor por defecto
191
                                                );
192
                                if (newName == null) {
193
                                        JOptionPane.showMessageDialog(
194
                                                        (Component)PluginServices.getMainFrame(),
195
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
196
                                                        PluginServices.getText(this,"pegar_vistas"),//titulo
197
                                                        JOptionPane.ERROR_MESSAGE
198
                                                        );
199
                                } else if (newName.equalsIgnoreCase(view.getStringProperty("name")) ) {
200
                                        JOptionPane.showMessageDialog(
201
                                                        (Component)PluginServices.getMainFrame(),
202
                                                        "<html>"+
203
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
204
                                                                PluginServices.getText(this,"nombre_no_valido")+
205
                                                        "</html>",//Mensaje
206
                                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
207
                                                        JOptionPane.ERROR_MESSAGE
208
                                                        );
209
                                        return false;
210
                                }
211
                                view.setName(newName);
212
                        }
213
                        if (xmlTables != null) xmlTables.removeAllChildren();
214
                        tablesConflits = null;
215
                }
216

    
217
                if (tablesConflits != null && tablesConflits.size() > 0) {
218
                        int option = JOptionPane.showConfirmDialog(
219
                                        (Component)PluginServices.getMainFrame(),
220
                                        "<html>" +
221
                                                PluginServices.getText(this,"conflicto_de_nombres_de_tablas_al_pegar") + "<br>" +
222
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
223
                                                PluginServices.getText(this,"desea_continuar") +
224
                                        "</html>", //Mensaje
225
                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
226
                                        JOptionPane.YES_NO_OPTION
227
                                        );
228
                        if (option != JOptionPane.YES_OPTION) {
229
                                return false;
230
                        }
231
                        xmlTables.removeAllChildren();
232
                }
233
                
234
                return true;
235
        }
236
}