root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / ProjectTableFactory.java @ 33261
History | View | Annotate | Download (12.3 KB)
1 |
package com.iver.cit.gvsig.project.documents.table; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.text.DateFormat; |
5 |
import java.util.Date; |
6 |
import java.util.Hashtable; |
7 |
|
8 |
import javax.swing.ImageIcon; |
9 |
import javax.swing.JPanel; |
10 |
|
11 |
import org.gvsig.tools.file.PathGenerator; |
12 |
|
13 |
import com.hardcode.driverManager.DriverLoadException; |
14 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
15 |
import com.hardcode.gdbms.engine.data.DataSource; |
16 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
17 |
import com.hardcode.gdbms.engine.data.NoSuchTableException; |
18 |
import com.hardcode.gdbms.engine.data.driver.DBDriver; |
19 |
import com.iver.andami.PluginServices; |
20 |
import com.iver.andami.messages.NotificationManager; |
21 |
import com.iver.cit.gvsig.AddLayer; |
22 |
import com.iver.cit.gvsig.addlayer.AddLayerDialog; |
23 |
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard; |
24 |
import com.iver.cit.gvsig.fmap.edition.EditableAdapter; |
25 |
import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
26 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
27 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
28 |
import com.iver.cit.gvsig.fmap.layers.VectorialFileAdapter; |
29 |
import com.iver.cit.gvsig.project.Project; |
30 |
import com.iver.cit.gvsig.project.ProjectFactory; |
31 |
import com.iver.cit.gvsig.project.documents.ProjectDocument; |
32 |
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory; |
33 |
import com.iver.cit.gvsig.project.documents.contextMenu.actions.CopyDocumentContextMenuAction; |
34 |
import com.iver.cit.gvsig.project.documents.contextMenu.actions.CutDocumentContextMenuAction; |
35 |
import com.iver.cit.gvsig.project.documents.contextMenu.actions.PasteDocumentContextMenuAction; |
36 |
import com.iver.cit.gvsig.project.documents.table.gui.DataBaseOpenDialog; |
37 |
import com.iver.cit.gvsig.project.documents.table.gui.Table; |
38 |
import com.iver.utiles.XMLEntity; |
39 |
import com.iver.utiles.extensionPoints.ExtensionPoints; |
40 |
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
41 |
|
42 |
|
43 |
/**
|
44 |
* Factory of Table.
|
45 |
*
|
46 |
* @author Vicente Caballero Navarro
|
47 |
*/
|
48 |
public class ProjectTableFactory extends ProjectDocumentFactory { |
49 |
public static String registerName = "ProjectTable"; |
50 |
|
51 |
static {
|
52 |
// A�adimos nuestra extension para el tratamiento de la apertura de ficheros
|
53 |
// dentro de gvSIG
|
54 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
55 |
extensionPoints.add("FileTableOpenDialog", "FileOpenTable", new TableFileOpen()); |
56 |
} |
57 |
|
58 |
/**
|
59 |
* Returns image of button.
|
60 |
*
|
61 |
* @return Image button.
|
62 |
*/
|
63 |
public ImageIcon getButtonIcon() { |
64 |
return PluginServices.getIconTheme().get("document-table-icon"); |
65 |
} |
66 |
|
67 |
/**
|
68 |
* Returns image of selected button.
|
69 |
*
|
70 |
* @return Image button.
|
71 |
*/
|
72 |
public ImageIcon getSelectedButtonIcon() { |
73 |
return PluginServices.getIconTheme().get("document-table-icon-sel"); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* Introduce a gui to be able from the characteristics that we want a ProjectDocument
|
78 |
*
|
79 |
* @param project present Project.
|
80 |
*
|
81 |
* @return new ProjectDocument.
|
82 |
*/
|
83 |
public ProjectDocument createFromGUI(Project project) {
|
84 |
try {
|
85 |
AddLayerDialog fopen = new AddLayerDialog(PluginServices.getText(this, |
86 |
"Nueva_tabla"));
|
87 |
FileOpenWizard fod = new FileOpenWizard("FileTableOpenDialog", false); |
88 |
fod.setTitle(PluginServices.getText(this, "Tablas")); |
89 |
|
90 |
DataBaseOpenDialog dbod = new DataBaseOpenDialog(fopen);
|
91 |
dbod.setClasses(new Class[] { DBDriver.class }); |
92 |
fopen.addWizardTab(PluginServices.getText(this, "Fichero"), fod); |
93 |
fopen.addTab(PluginServices.getText(this, "base_datos"), dbod); |
94 |
PluginServices.getMDIManager().addWindow(fopen); |
95 |
|
96 |
|
97 |
if (fopen.isAccepted()) {
|
98 |
JPanel panel = fopen.getSelectedTab();
|
99 |
|
100 |
if (panel instanceof FileOpenWizard) { |
101 |
File[] files = fod.getFiles(); |
102 |
String[] driverNames = fod.getDriverNames(); |
103 |
ProjectTable tableToReturn = null;
|
104 |
if(files.length > 0){ |
105 |
ProjectTable[] auxTables = new ProjectTable[files.length-1]; |
106 |
|
107 |
for (int i = 0; i < files.length; i++) { |
108 |
String name = files[i].getName();
|
109 |
|
110 |
LayerFactory.getDataSourceFactory().addFileDataSource(driverNames[i], |
111 |
name, files[i].getAbsolutePath()); |
112 |
|
113 |
DataSource dataSource = LayerFactory.getDataSourceFactory()
|
114 |
.createRandomDataSource(name, |
115 |
DataSourceFactory.AUTOMATIC_OPENING); |
116 |
SelectableDataSource sds = new SelectableDataSource(dataSource);
|
117 |
EditableAdapter auxea = new EditableAdapter();
|
118 |
auxea.setOriginalDataSource(sds); |
119 |
|
120 |
// TODO: fjp: ESTO HAY QUE REVISARLO.
|
121 |
// Por ahora, para obtener un driver que sirva para esta
|
122 |
// fuente de datos, compruebo que implementa IWriteable.
|
123 |
// IWriter writer = (IWriter) LayerFactory.getWM().getWriter(driverNames[i]);
|
124 |
// Driver drv = LayerFactory.getDM().getDriver(driverNames[i]);
|
125 |
// if (drv instanceof IWriter)
|
126 |
// {
|
127 |
// auxea.setWriter((IWriter) drv);
|
128 |
// }
|
129 |
ProjectTable projectTable = ProjectFactory.createTable(name, |
130 |
auxea); |
131 |
|
132 |
projectTable.setProjectDocumentFactory(this);
|
133 |
|
134 |
Table t = new Table();
|
135 |
t.setModel(projectTable); |
136 |
PluginServices.getMDIManager().addWindow(t); |
137 |
|
138 |
|
139 |
if(i==0) |
140 |
tableToReturn = projectTable; |
141 |
else
|
142 |
auxTables[i-1] = projectTable;
|
143 |
} |
144 |
if(files.length > 1) { |
145 |
for(int i=auxTables.length-1;i>=0;i--) { |
146 |
project.addDocument(auxTables[i]); |
147 |
} |
148 |
} |
149 |
} |
150 |
return tableToReturn;
|
151 |
|
152 |
} else if (panel instanceof DataBaseOpenDialog) { |
153 |
String driverName = dbod.getDriverName();
|
154 |
int port = -1; |
155 |
|
156 |
try {
|
157 |
port = Integer.parseInt(dbod.getPort());
|
158 |
} catch (NumberFormatException e) { |
159 |
} |
160 |
|
161 |
String name = dbod.getHost() + "/" + dbod.getDataBase(); |
162 |
|
163 |
if (port != -1) { |
164 |
name = dbod.getHost() + ":" + port + "/" + |
165 |
dbod.getDataBase(); |
166 |
} |
167 |
|
168 |
String user = dbod.getUser().trim();
|
169 |
String password = dbod.getPassword();
|
170 |
|
171 |
if (user.equals("")) { |
172 |
user = null;
|
173 |
password = null;
|
174 |
} |
175 |
|
176 |
name = name + " Table:" + dbod.getTable();
|
177 |
|
178 |
LayerFactory.getDataSourceFactory().addDBDataSourceByTable(name, |
179 |
dbod.getHost(), port, user, password, |
180 |
dbod.getDataBase(), dbod.getTable(), driverName); |
181 |
|
182 |
DataSource dataSource = LayerFactory.getDataSourceFactory()
|
183 |
.createRandomDataSource(name, |
184 |
DataSourceFactory.AUTOMATIC_OPENING); |
185 |
SelectableDataSource sds = new SelectableDataSource(dataSource);
|
186 |
EditableAdapter auxea = new EditableAdapter();
|
187 |
|
188 |
// TODO: fjp: ESTO HAY QUE REVISARLO.
|
189 |
// Por ahora, para obtener un driver que sirva para esta
|
190 |
// fuente de datos, compruebo que implementa IWriteable.
|
191 |
// IWriter writer = (IWriter) LayerFactory.getWM().getWriter(driverNames[i]);
|
192 |
//Driver drv = LayerFactory.getDM().getDriver(driverName);
|
193 |
// if (drv instanceof IWriter)
|
194 |
// {
|
195 |
// auxea.setWriter((IWriter) drv);
|
196 |
// }
|
197 |
auxea.setOriginalDataSource(sds); |
198 |
|
199 |
ProjectTable projectTable = ProjectFactory.createTable(name, |
200 |
auxea); |
201 |
projectTable.setProjectDocumentFactory(this);
|
202 |
|
203 |
Table t = new Table();
|
204 |
t.setModel(projectTable); |
205 |
PluginServices.getMDIManager().addWindow(t); |
206 |
|
207 |
return projectTable;
|
208 |
} |
209 |
} |
210 |
} catch (DriverLoadException e) {
|
211 |
NotificationManager.addError("Error al cargar los drivers", e);
|
212 |
} catch (NoSuchTableException e) {
|
213 |
e.printStackTrace(); |
214 |
} catch (ReadDriverException e) {
|
215 |
e.printStackTrace(); |
216 |
} |
217 |
|
218 |
return null; |
219 |
} |
220 |
|
221 |
/**
|
222 |
* Create a new ProjectTable
|
223 |
*
|
224 |
* @param baseName name
|
225 |
*
|
226 |
* @return ProjectTable.
|
227 |
*/
|
228 |
public static ProjectTable createTable(String name, IEditableSource es) { |
229 |
ProjectTable t = new ProjectTable();
|
230 |
|
231 |
if (es != null) { |
232 |
t.setModel(es); |
233 |
|
234 |
try {
|
235 |
t.createAlias(); |
236 |
} catch (ReadDriverException e) {
|
237 |
e.printStackTrace(); |
238 |
} |
239 |
} |
240 |
|
241 |
t.setName(name); |
242 |
t.setCreationDate(DateFormat.getInstance().format(new Date())); |
243 |
int numTables=((Integer)ProjectDocument.NUMS.get(registerName)).intValue(); |
244 |
ProjectDocument.NUMS.put(registerName,new Integer(numTables++)); |
245 |
|
246 |
return t;
|
247 |
} |
248 |
|
249 |
/**
|
250 |
* Returns the name of registration in the point of extension.
|
251 |
*
|
252 |
* @return Name of registration
|
253 |
*/
|
254 |
public String getRegisterName() { |
255 |
return registerName;
|
256 |
} |
257 |
|
258 |
/**
|
259 |
* Registers in the points of extension the Factory with alias.
|
260 |
*
|
261 |
*/
|
262 |
public static void register() { |
263 |
register(registerName, new ProjectTableFactory(),
|
264 |
"com.iver.cit.gvsig.project.ProjectTable");
|
265 |
|
266 |
registerAction(registerName,"copy",new CopyDocumentContextMenuAction()); |
267 |
registerAction(registerName,"cut",new CutDocumentContextMenuAction()); |
268 |
registerAction(registerName,"paste",new PasteDocumentContextMenuAction()); |
269 |
|
270 |
PluginServices.getIconTheme().registerDefault( |
271 |
"document-table-icon",
|
272 |
AddLayer.class.getClassLoader().getResource("images/tablas.png")
|
273 |
);//
|
274 |
PluginServices.getIconTheme().registerDefault( |
275 |
"document-table-icon-sel",
|
276 |
AddLayer.class.getClassLoader().getResource("images/tablas_sel.png")
|
277 |
); |
278 |
|
279 |
PluginServices.getIconTheme().registerDefault( |
280 |
"edit-copy",
|
281 |
AddLayer.class.getClassLoader().getResource("images/editcopy.png")
|
282 |
);//
|
283 |
PluginServices.getIconTheme().registerDefault( |
284 |
"edit-cut",
|
285 |
AddLayer.class.getClassLoader().getResource("images/editcut.png")
|
286 |
); |
287 |
PluginServices.getIconTheme().registerDefault( |
288 |
"edit-paste",
|
289 |
AddLayer.class.getClassLoader().getResource("images/editpaste.png")
|
290 |
); |
291 |
PluginServices.getIconTheme().registerDefault( |
292 |
"edit-delete",
|
293 |
AddLayer.class.getClassLoader().getResource("images/editdelete.png")
|
294 |
); |
295 |
} |
296 |
|
297 |
/**
|
298 |
* Returns the name of ProjectDocument.
|
299 |
*
|
300 |
* @return Name of ProjectDocument.
|
301 |
*/
|
302 |
public String getNameType() { |
303 |
return PluginServices.getText(this, "Tabla"); |
304 |
} |
305 |
|
306 |
|
307 |
/**
|
308 |
* Create a new ProjectDocument.
|
309 |
*
|
310 |
* @param project Opened project.
|
311 |
*
|
312 |
* @return ProjectDocument.
|
313 |
*/
|
314 |
public ProjectDocument create(Project project) {
|
315 |
ProjectTable pt = null;
|
316 |
|
317 |
pt = ProjectTableFactory.createTable("", null); |
318 |
pt.setProject(project,0);
|
319 |
|
320 |
pt.setProjectDocumentFactory(this);
|
321 |
|
322 |
return pt;
|
323 |
} |
324 |
|
325 |
/**
|
326 |
* Returns the priority of de ProjectDocument.
|
327 |
*
|
328 |
* @return Priority.
|
329 |
*/
|
330 |
public int getPriority() { |
331 |
return 1; |
332 |
} |
333 |
|
334 |
public boolean resolveImportXMLConflicts(XMLEntity root, Project project, Hashtable conflicts) { |
335 |
return true; |
336 |
} |
337 |
} |