Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / ProjectViewFactory.java @ 9407

History | View | Annotate | Download (7.85 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.cit.gvsig.project.documents.view.gui.View;
24
import com.iver.utiles.XMLEntity;
25

    
26

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

    
35

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

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

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

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

    
77
        return vista;
78
    }
79

    
80
    /**
81
     * Create a new ProjectView.
82
     *
83
     * @param baseName name
84
     *
85
     * @return ProjectView.
86
     */
87
    private static ProjectView createView(String viewName) {
88
            ProjectView v = new ProjectView();
89
                MapContext viewMapContext = new MapContext(new ViewPort(Project
90
                                .getDefaultProjection()));
91
                ViewPort vp = viewMapContext.getViewPort();
92
                vp.setBackColor(View.getDefaultBackColor());
93
                vp.setDistanceUnits(Project.getDefaultDistanceUnits());
94
                vp.setMapUnits(Project.getDefaultMapUnits());
95

    
96
                v.setMapContext(viewMapContext);
97
                v.setMapOverViewContext(new MapContext(null));
98

    
99
                /*
100
                 * jaume. ?no puedo definir color de fondo en localizador?
101
                 *
102
                 * v.getMapOverViewContext().setProjection(v.getMapContext().getProjection());
103
                 * v.getMapOverViewContext(). getViewPort(). setBackColor(
104
                 * Project.getDefaultMapOverViewBackColor() );
105
                 *
106
                 */
107
                v.setName(viewName);
108
                v.setCreationDate(DateFormat.getInstance().format(new Date()));
109

    
110
        return v;
111
    }
112

    
113
    /**
114
     *
115
     *
116
     * @param project DOCUMENT ME!
117
     * @param viewName DOCUMENT ME!
118
     *
119
     * @return DOCUMENT ME!
120
     */
121
    private boolean existViewName(Project project, String viewName) {
122
        ArrayList viewList = project.getDocumentsByType(getRegisterName());
123

    
124
        for (int i = 0; i < viewList.size(); i++) {
125
            ProjectView pv = (ProjectView) viewList.get(i);
126
            String title = pv.getName();
127

    
128
            if (title.compareTo(viewName) == 0) {
129
                return true;
130
            }
131
        }
132

    
133
        return false;
134
    }
135

    
136
    /**
137
     * Returns the name of registration in the point of extension.
138
     *
139
     * @return Name of registration
140
     */
141
    public String getRegisterName() {
142
        return registerName;
143
    }
144

    
145
    /**
146
     * Returns the name of ProjectDocument.
147
     *
148
     * @return Name of ProjectDocument.
149
     */
150
    public String getNameType() {
151
        return PluginServices.getText(this, "Vista");
152
    }
153

    
154
    /**
155
     * Registers in the points of extension the Factory with alias.
156
     *
157
     */
158
    public static void register() {
159
        register(registerName, new ProjectViewFactory(),
160
            "com.iver.cit.gvsig.project.ProjectView");
161

    
162
        registerAction(registerName,"copy",new CopyDocumentContextMenuAction());
163
        registerAction(registerName,"cut",new CutDocumentContextMenuAction());
164
        registerAction(registerName,"paste",new PasteDocumentContextMenuAction());
165
    }
166

    
167
    /**
168
     * Returns the priority of de ProjectDocument.
169
     *
170
     * @return Priority.
171
     */
172
    public int getPriority() {
173
        return 0;
174
    }
175

    
176
        public boolean resolveImportXMLConflicts(XMLEntity root, Project project, Hashtable conflicts) {
177
                Hashtable viewsConflits = (Hashtable)conflicts.get(this.getRegisterName());
178
                Hashtable tablesConflits = (Hashtable)conflicts.get(ProjectTableFactory.registerName);
179
                XMLEntity xmlTables = root.firstChild("type",ProjectTableFactory.registerName);
180

    
181

    
182
                if (viewsConflits != null && viewsConflits.size() > 0) {
183
                        int option = JOptionPane.showConfirmDialog(
184
                                        (Component)PluginServices.getMainFrame(),
185
                                        "<html>"+
186
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
187
                                                PluginServices.getText(this,"debera_introducir_nombres_para_las_vistas_a_pegar") + "<br>" +
188
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
189
                                                PluginServices.getText(this,"desea_continuar") +
190
                                        "</html>",
191
                                        PluginServices.getText(this,"pegar_vistas"),
192
                                        JOptionPane.YES_NO_OPTION
193
                                        );
194
                        if (option != JOptionPane.YES_OPTION) {
195
                                return false;
196
                        }
197
                        Enumeration en = viewsConflits.elements();
198
                        while (en.hasMoreElements()) {
199
                                XMLEntity view = (XMLEntity)en.nextElement();
200
                                String newName = JOptionPane.showInputDialog(
201
                                                (Component)PluginServices.getMainFrame(),
202
                                                "<html>"+
203
                                                        PluginServices.getText(this,"introduzca_nuevo_nombre_para_la_vista") +" "+  view.getStringProperty("name") + ":" +
204
                                                "</html>", //Mensaje
205
                                                view.getStringProperty("name") //Valor por defecto
206
                                                );
207
                                if (newName == null) {
208
                                        JOptionPane.showMessageDialog(
209
                                                        (Component)PluginServices.getMainFrame(),
210
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
211
                                                        PluginServices.getText(this,"pegar_vistas"),//titulo
212
                                                        JOptionPane.ERROR_MESSAGE
213
                                                        );
214
                                } else if (newName.equalsIgnoreCase(view.getStringProperty("name")) ) {
215
                                        JOptionPane.showMessageDialog(
216
                                                        (Component)PluginServices.getMainFrame(),
217
                                                        "<html>"+
218
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
219
                                                                PluginServices.getText(this,"nombre_no_valido")+
220
                                                        "</html>",//Mensaje
221
                                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
222
                                                        JOptionPane.ERROR_MESSAGE
223
                                                        );
224
                                        return false;
225
                                }
226
                                view.setName(newName);
227
                        }
228
                        if (xmlTables != null) xmlTables.removeAllChildren();
229
                        tablesConflits = null;
230
                }
231

    
232
                if (tablesConflits != null && tablesConflits.size() > 0) {
233
                        int option = JOptionPane.showConfirmDialog(
234
                                        (Component)PluginServices.getMainFrame(),
235
                                        "<html>" +
236
                                                PluginServices.getText(this,"conflicto_de_nombres_de_tablas_al_pegar") + "<br>" +
237
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
238
                                                PluginServices.getText(this,"desea_continuar") +
239
                                        "</html>", //Mensaje
240
                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
241
                                        JOptionPane.YES_NO_OPTION
242
                                        );
243
                        if (option != JOptionPane.YES_OPTION) {
244
                                return false;
245
                        }
246
                        xmlTables.removeAllChildren();
247
                }
248

    
249
                return true;
250
        }
251
}