Statistics
| Revision:

root / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / ProjectTableFactory.java @ 7979

History | View | Annotate | Download (10.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 com.hardcode.driverManager.DriverLoadException;
12
import com.hardcode.gdbms.engine.data.DataSource;
13
import com.hardcode.gdbms.engine.data.DataSourceFactory;
14
import com.hardcode.gdbms.engine.data.NoSuchTableException;
15
import com.hardcode.gdbms.engine.data.driver.DBDriver;
16
import com.hardcode.gdbms.engine.data.driver.DriverException;
17
import com.hardcode.gdbms.engine.data.driver.FileDriver;
18
import com.iver.andami.PluginServices;
19
import com.iver.andami.messages.NotificationManager;
20
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
21
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
22
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
23
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
24
import com.iver.cit.gvsig.project.Project;
25
import com.iver.cit.gvsig.project.ProjectFactory;
26
import com.iver.cit.gvsig.project.documents.ProjectDocument;
27
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory;
28
import com.iver.cit.gvsig.project.documents.contextMenu.actions.CopyDocumentContextMenuAction;
29
import com.iver.cit.gvsig.project.documents.contextMenu.actions.CutDocumentContextMenuAction;
30
import com.iver.cit.gvsig.project.documents.contextMenu.actions.PasteDocumentContextMenuAction;
31
import com.iver.cit.gvsig.project.documents.gui.FOpenDialog;
32
import com.iver.cit.gvsig.project.documents.gui.FileOpenDialog;
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

    
37

    
38
/**
39
 * Factory of Table.
40
 *
41
 * @author Vicente Caballero Navarro
42
 */
43
public class ProjectTableFactory extends ProjectDocumentFactory {
44
    public static String registerName = "ProjectTable";
45

    
46
    /**
47
     * Returns image of button.
48
     *
49
     * @return Image button.
50
     */
51
    public ImageIcon getButtonIcon() {
52
        return new ImageIcon(this.getClass().getClassLoader().getResource("images/tablas.png"));
53
    }
54

    
55
    /**
56
     * Returns image of selected button.
57
     *
58
     * @return Image button.
59
     */
60
    public ImageIcon getSelectedButtonIcon() {
61
        return new ImageIcon(this.getClass().getClassLoader().getResource("images/tablas_sel.png"));
62
    }
63

    
64
    /**
65
     * Introduce a gui to be able from the characteristics that we want a ProjectDocument
66
     *
67
     * @param project present Project.
68
     *
69
     * @return new ProjectDocument.
70
     */
71
    public ProjectDocument createFromGUI(Project project) {
72
        try {
73
            FOpenDialog fopen = new FOpenDialog(PluginServices.getText(this,
74
                        "Nueva_tabla"));
75
            FileOpenDialog fod = new FileOpenDialog(new Class[] { FileDriver.class },
76
                    false, PluginServices.getText(this, "Tablas"));
77
            DataBaseOpenDialog dbod = new DataBaseOpenDialog();
78
            dbod.setClasses(new Class[] { DBDriver.class });
79
            fopen.addTab(PluginServices.getText(this, "Fichero"), fod);
80
            fopen.addTab(PluginServices.getText(this, "base_datos"), dbod);
81
            PluginServices.getMDIManager().addWindow(fopen);
82

    
83
            if (fopen.isAccepted()) {
84
                JPanel panel = fopen.getSelectedTab();
85

    
86
                if (panel instanceof FileOpenDialog) {
87
                    File[] files = fod.getFiles();
88
                    String[] driverNames = fod.getDriverNames();
89

    
90
                    for (int i = 0; i < files.length; i++) {
91
                        String name = files[i].getName();
92

    
93
                        LayerFactory.getDataSourceFactory().addFileDataSource(driverNames[i],
94
                            name, files[i].getAbsolutePath());
95

    
96
                        DataSource dataSource = LayerFactory.getDataSourceFactory()
97
                                                            .createRandomDataSource(name,
98
                                DataSourceFactory.AUTOMATIC_OPENING);
99
                        SelectableDataSource sds = new SelectableDataSource(dataSource);
100
                        EditableAdapter auxea = new EditableAdapter();
101
                        auxea.setOriginalDataSource(sds);
102

    
103
                        // TODO: fjp: ESTO HAY QUE REVISARLO.
104
                        // Por ahora, para obtener un driver que sirva para esta
105
                        // fuente de datos, compruebo que implementa IWriteable.
106
                        // IWriter writer = (IWriter) LayerFactory.getWM().getWriter(driverNames[i]);
107
                        //                                Driver drv = LayerFactory.getDM().getDriver(driverNames[i]);
108
                        //                                if (drv instanceof IWriter)
109
                        //                                {
110
                        //                                        auxea.setWriter((IWriter) drv);
111
                        //                                }
112
                        ProjectTable projectTable = ProjectFactory.createTable(name,
113
                                auxea);
114

    
115
                        projectTable.setProjectDocumentFactory(this);
116

    
117
                        Table t = new Table();
118
                        t.setModel(projectTable);
119
                        PluginServices.getMDIManager().addWindow(t);
120

    
121
                        return projectTable;
122
                    }
123
                } else if (panel instanceof DataBaseOpenDialog) {
124
                    String driverName = dbod.getDriverName();
125
                    int port = -1;
126

    
127
                    try {
128
                        port = Integer.parseInt(dbod.getPort());
129
                    } catch (NumberFormatException e) {
130
                    }
131

    
132
                    String name = dbod.getHost() + "/" + dbod.getDataBase();
133

    
134
                    if (port != -1) {
135
                        name = dbod.getHost() + ":" + port + "/" +
136
                            dbod.getDataBase();
137
                    }
138

    
139
                    String user = dbod.getUser().trim();
140
                    String password = dbod.getPassword();
141

    
142
                    if (user.equals("")) {
143
                        user = null;
144
                        password = null;
145
                    }
146

    
147
                    LayerFactory.getDataSourceFactory().addDBDataSourceByTable(name,
148
                        dbod.getHost(), port, user, password,
149
                        dbod.getDataBase(), dbod.getTable(), driverName);
150

    
151
                    DataSource dataSource = LayerFactory.getDataSourceFactory()
152
                                                        .createRandomDataSource(name,
153
                            DataSourceFactory.AUTOMATIC_OPENING);
154
                    SelectableDataSource sds = new SelectableDataSource(dataSource);
155
                    EditableAdapter auxea = new EditableAdapter();
156

    
157
                    // TODO: fjp: ESTO HAY QUE REVISARLO.
158
                    // Por ahora, para obtener un driver que sirva para esta
159
                    // fuente de datos, compruebo que implementa IWriteable.
160
                    // IWriter writer = (IWriter) LayerFactory.getWM().getWriter(driverNames[i]);
161
                    //Driver drv = LayerFactory.getDM().getDriver(driverName);
162
                    //                        if (drv instanceof IWriter)
163
                    //                        {
164
                    //                                auxea.setWriter((IWriter) drv);
165
                    //                        }
166
                    auxea.setOriginalDataSource(sds);
167

    
168
                    ProjectTable projectTable = ProjectFactory.createTable(name,
169
                            auxea);
170
                    projectTable.setProjectDocumentFactory(this);
171

    
172
                    Table t = new Table();
173
                    t.setModel(projectTable);
174
                    PluginServices.getMDIManager().addWindow(t);
175

    
176
                    return projectTable;
177
                }
178
            }
179
        } catch (DriverLoadException e) {
180
            NotificationManager.addError("Error al cargar los drivers", e);
181
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
182
            e.printStackTrace();
183
        } catch (NoSuchTableException e) {
184
            e.printStackTrace();
185
        }
186

    
187
        return null;
188
    }
189

    
190
    /**
191
     * Create a new ProjectTable
192
     *
193
     * @param baseName name
194
     *
195
     * @return ProjectTable.
196
     */
197
    public static ProjectTable createTable(String name, IEditableSource es)
198
        throws com.hardcode.gdbms.engine.data.driver.DriverException {
199
        ProjectTable t = new ProjectTable();
200

    
201
        if (es != null) {
202
            t.setModel(es);
203

    
204
            try {
205
                t.createAlias();
206
            } catch (DriverLoadException e) {
207
                e.printStackTrace();
208
            }
209
        }
210

    
211
        t.setName(name);
212
        t.setCreationDate(DateFormat.getInstance().format(new Date()));
213
        ProjectTable.numTables++;
214

    
215
        return t;
216
    }
217

    
218
    /**
219
     * Returns the name of registration in the point of extension.
220
     *
221
     * @return Name of registration
222
     */
223
    public String getRegisterName() {
224
        return registerName;
225
    }
226

    
227
    /**
228
     * Registers in the points of extension the Factory with alias.
229
     *
230
     */
231
    public static void register() {
232
        register(registerName, new ProjectTableFactory(),
233
            "com.iver.cit.gvsig.project.ProjectTable");
234

    
235
        registerAction(registerName,"copy",new CopyDocumentContextMenuAction());
236
        registerAction(registerName,"cut",new CutDocumentContextMenuAction());
237
        registerAction(registerName,"paste",new PasteDocumentContextMenuAction());
238
    }
239

    
240
    /**
241
     * Returns the name of ProjectDocument.
242
     *
243
     * @return Name of ProjectDocument.
244
     */
245
    public String getNameType() {
246
        return PluginServices.getText(this, "Tabla");
247
    }
248

    
249

    
250
    /**
251
     * Create a new ProjectDocument.
252
     *
253
     * @param project Opened project.
254
     *
255
     * @return ProjectDocument.
256
     */
257
    public ProjectDocument create(Project project) {
258
        ProjectTable pt = null;
259

    
260
        try {
261
            pt = ProjectTableFactory.createTable("", null);
262
        } catch (DriverException e) {
263
            e.printStackTrace();
264
        }
265
        pt.setProject(project,0);
266

    
267
        pt.setProjectDocumentFactory(this);
268

    
269
        return pt;
270
    }
271

    
272
    /**
273
     * Returns the priority of de ProjectDocument.
274
     *
275
     * @return Priority.
276
     */
277
    public int getPriority() {
278
        return 1;
279
    }
280

    
281
        public boolean resolveImportXMLConflicts(XMLEntity root, Project project, Hashtable conflicts) {
282
                return true;
283
        }
284
}