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