root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / ProjectMapFactory.java @ 7768
History | View | Annotate | Download (7.85 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.gui.layout.Layout; |
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.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 |
ProjectMap map = createMap(PluginServices.getText(this, "untitled")); |
62 |
map.setModel(new Layout());
|
63 |
map.getModel().setProjectMap(map); |
64 |
map.setProject(project, 0);
|
65 |
map.setProjectDocumentFactory(this);
|
66 |
|
67 |
return map;
|
68 |
} |
69 |
|
70 |
/**
|
71 |
* Returns the name of registration in the point of extension.
|
72 |
*
|
73 |
* @return Name of registration
|
74 |
*/
|
75 |
public String getRegisterName() { |
76 |
return registerName;
|
77 |
} |
78 |
|
79 |
/**
|
80 |
* Returns the name of ProjectDocument.
|
81 |
*
|
82 |
* @return Name of ProjectDocument.
|
83 |
*/
|
84 |
public String getNameType() { |
85 |
return PluginServices.getText(this, "Mapa"); |
86 |
} |
87 |
|
88 |
/**
|
89 |
* Create a ne ProjectMap
|
90 |
*
|
91 |
* @param baseName name
|
92 |
*
|
93 |
* @return ProjectMap.
|
94 |
*/
|
95 |
public static ProjectMap createMap(String baseName) { |
96 |
ProjectMap m = new ProjectMap();
|
97 |
m.setName(baseName + " - " + ProjectMap.numMaps);
|
98 |
m.setCreationDate(DateFormat.getInstance().format(new Date())); |
99 |
ProjectMap.numMaps++; |
100 |
|
101 |
return m;
|
102 |
} |
103 |
|
104 |
/**
|
105 |
* Registers in the points of extension the Factory with alias.
|
106 |
*
|
107 |
*/
|
108 |
public static void register() { |
109 |
register(registerName, new ProjectMapFactory(),
|
110 |
"com.iver.cit.gvsig.project.ProjectMap");
|
111 |
|
112 |
registerAction(registerName,"copy",new CopyDocumentContextMenuAction()); |
113 |
registerAction(registerName,"cut",new CutDocumentContextMenuAction()); |
114 |
registerAction(registerName,"paste",new PasteDocumentContextMenuAction()); |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Returns the priority of de ProjectDocument.
|
119 |
*
|
120 |
* @return Priority.
|
121 |
*/
|
122 |
public int getPriority() { |
123 |
return 2; |
124 |
} |
125 |
|
126 |
public boolean resolveImportXMLConflicts(XMLEntity root, Project project, Hashtable conflicts) { |
127 |
XMLEntity xmlMaps = root.firstChild("type",this.getRegisterName()); |
128 |
XMLEntity xmlViews = root.firstChild("type",ProjectViewFactory.registerName);
|
129 |
XMLEntity xmlTables = root.firstChild("type",ProjectTableFactory.registerName);
|
130 |
|
131 |
Hashtable mapsConflits = (Hashtable)conflicts.get(ProjectMapFactory.registerName); |
132 |
|
133 |
Hashtable viewsConflits = (Hashtable)conflicts.get(ProjectViewFactory.registerName); |
134 |
|
135 |
Hashtable tablesConflits = (Hashtable)conflicts.get(ProjectTableFactory.registerName); |
136 |
|
137 |
|
138 |
if (mapsConflits != null && mapsConflits.size() > 0) { |
139 |
int option = JOptionPane.showConfirmDialog( |
140 |
(Component)PluginServices.getMainFrame(),
|
141 |
"<html>"+
|
142 |
PluginServices.getText(this,"conflicto_de_nombres_de_mapas_al_pegar") + "<br>" + |
143 |
PluginServices.getText(this,"debera_introducir_nombres_para_los_mapas_a_pegar") + "<br>" + |
144 |
"</html>", //Mensaje |
145 |
PluginServices.getText(this,"pegar_mapas"),//titulo |
146 |
JOptionPane.YES_NO_OPTION
|
147 |
); |
148 |
if (option != JOptionPane.YES_OPTION) { |
149 |
return false; |
150 |
} |
151 |
Enumeration en = mapsConflits.elements();
|
152 |
while (en.hasMoreElements()) {
|
153 |
XMLEntity map = (XMLEntity)en.nextElement(); |
154 |
String newName = JOptionPane.showInputDialog( |
155 |
(Component)PluginServices.getMainFrame(),
|
156 |
"<html>"+
|
157 |
PluginServices.getText(this,"nuevo_nombre_para_el_mapa") +" "+ map.getStringProperty("name") + ":" + |
158 |
"</html>", //Mensaje |
159 |
map.getStringProperty("name") //Valor por defecto |
160 |
); |
161 |
if (newName == null) { |
162 |
JOptionPane.showMessageDialog(
|
163 |
(Component)PluginServices.getMainFrame(),
|
164 |
"<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje |
165 |
PluginServices.getText(this,"pegar_mapas"),//titulo |
166 |
JOptionPane.ERROR_MESSAGE
|
167 |
); |
168 |
} else if (newName.equalsIgnoreCase(map.getStringProperty("name")) ) { |
169 |
JOptionPane.showMessageDialog(
|
170 |
(Component)PluginServices.getMainFrame(),
|
171 |
"<html>"+
|
172 |
PluginServices.getText(this,"operacion_cancelada") +":<br>" + |
173 |
PluginServices.getText(this,"nombre_no_valido")+ |
174 |
"</html>",//Mensaje |
175 |
PluginServices.getText(this,"pegar_mapas"),//titulo |
176 |
JOptionPane.ERROR_MESSAGE
|
177 |
); |
178 |
return false; |
179 |
} |
180 |
map.setName(newName); |
181 |
} |
182 |
} |
183 |
|
184 |
if (viewsConflits != null && viewsConflits.size() > 0) { |
185 |
int option = JOptionPane.showConfirmDialog( |
186 |
(Component)PluginServices.getMainFrame(),
|
187 |
"<html>"+
|
188 |
PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" + |
189 |
PluginServices.getText(this,"no_se_pegaran_las_vistas_del_conflicto") + "<br>" + |
190 |
PluginServices.getText(this,"desea_continuar") + |
191 |
"</html>",
|
192 |
PluginServices.getText(this,"pegar_mapas"),//titulo |
193 |
JOptionPane.YES_NO_OPTION
|
194 |
); |
195 |
if (option != JOptionPane.YES_OPTION) { |
196 |
return false; |
197 |
} |
198 |
// Eliminamos las vistas del xml que no vamos a importar
|
199 |
|
200 |
// Esto me devuelve los indices en orden inverso
|
201 |
int[] indexes = this.getIndexOfConflict(viewsConflits); |
202 |
for (int i=0;i < indexes.length;i++) { |
203 |
xmlViews.removeChild(indexes[i]); |
204 |
} |
205 |
viewsConflits = null;
|
206 |
|
207 |
} |
208 |
|
209 |
|
210 |
if (tablesConflits != null && tablesConflits.size() > 0) { |
211 |
int option = JOptionPane.showConfirmDialog( |
212 |
(Component)PluginServices.getMainFrame(),
|
213 |
"<html>" +
|
214 |
PluginServices.getText(this,"conflito_de_nombres_de_tablas_al_pegar") + "<br>" + |
215 |
PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" + |
216 |
PluginServices.getText(this,"desea_continuar") + |
217 |
"</html>", //Mensaje |
218 |
PluginServices.getText(this,"pegar_mapas"), |
219 |
JOptionPane.YES_NO_OPTION
|
220 |
); |
221 |
if (option != JOptionPane.YES_OPTION) { |
222 |
return false; |
223 |
} |
224 |
xmlTables.removeAllChildren(); |
225 |
} |
226 |
|
227 |
|
228 |
return true; |
229 |
} |
230 |
|
231 |
/**
|
232 |
* Devuelve las claves de conflits ordenados
|
233 |
* en orden inverso. Las claves se esperan que
|
234 |
* sean instancias de Integer
|
235 |
*/
|
236 |
private int[] getIndexOfConflict(Hashtable conflits) { |
237 |
Object[] tmpArray = conflits.keySet().toArray(); |
238 |
Arrays.sort(tmpArray,new Comparator() { |
239 |
public int compare(Object o1, Object o2) { |
240 |
return ((Integer)o2).intValue() - ((Integer)o1).intValue(); |
241 |
} |
242 |
} |
243 |
); |
244 |
int[] indexes = new int[] {tmpArray.length}; |
245 |
for (int i = 0;i< tmpArray.length;i++) { |
246 |
indexes[i] = ((Integer)tmpArray[i]).intValue();
|
247 |
} |
248 |
return indexes;
|
249 |
|
250 |
|
251 |
} |
252 |
} |