Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / ProjectTableFactory.java @ 9833

History | View | Annotate | Download (10.5 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.MANUAL_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
                    name = name + " Table:" + dbod.getTable(); 
148

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

    
153
                    DataSource dataSource = LayerFactory.getDataSourceFactory()
154
                                                        .createRandomDataSource(name,
155
                            DataSourceFactory.MANUAL_OPENING);
156
                    SelectableDataSource sds = new SelectableDataSource(dataSource);
157
                    EditableAdapter auxea = new EditableAdapter();
158

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

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

    
174
                    Table t = new Table();
175
                    t.setModel(projectTable);
176
                    PluginServices.getMDIManager().addWindow(t);
177

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

    
189
        return null;
190
    }
191

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

    
203
        if (es != null) {
204
            t.setModel(es);
205

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

    
213
        t.setName(name);
214
        t.setCreationDate(DateFormat.getInstance().format(new Date()));
215
        int numTables=((Integer)ProjectDocument.NUMS.get(registerName)).intValue();
216
        ProjectDocument.NUMS.put(registerName,new Integer(numTables++));
217

    
218
        return t;
219
    }
220

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

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

    
238
        registerAction(registerName,"copy",new CopyDocumentContextMenuAction());
239
        registerAction(registerName,"cut",new CutDocumentContextMenuAction());
240
        registerAction(registerName,"paste",new PasteDocumentContextMenuAction());
241
    }
242

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

    
252

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

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

    
270
        pt.setProjectDocumentFactory(this);
271

    
272
        return pt;
273
    }
274

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

    
284
        public boolean resolveImportXMLConflicts(XMLEntity root, Project project, Hashtable conflicts) {
285
                return true;
286
        }
287
}