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