Statistics
| Revision:

gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.swing / org.gvsig.vcsgis.swing.impl / src / main / java / org / gvsig / vcsgis / swing / impl / initserver / VCSGisJInitServerImpl.java @ 2728

History | View | Annotate | Download (5.07 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (c) 2007-2020 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.vcsgis.swing.impl.initserver;
25

    
26
import java.awt.Dimension;
27
import javax.swing.JComponent;
28
import javax.swing.event.ChangeEvent;
29
import org.gvsig.vcsgis.lib.vcsgisadmin;
30
import org.gvsig.vcsgis.swing.VCSGisJInitServer;
31
import org.gvsig.fmap.dal.DALLocator;
32
import org.gvsig.fmap.dal.DataManager;
33
import org.gvsig.fmap.dal.exception.InitializeException;
34
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
35
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
36
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
37
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
38
import org.gvsig.fmap.dal.swing.DALSwingLocator;
39
import org.gvsig.tools.swing.api.Component;
40
import org.gvsig.tools.swing.api.ListElement;
41
import org.gvsig.tools.swing.api.ToolsSwingLocator;
42
import org.gvsig.tools.swing.api.ToolsSwingManager;
43
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
44
import org.gvsig.tools.swing.api.windowmanager.Dialog;
45
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
46
import org.slf4j.LoggerFactory;
47

    
48
/**
49
 *
50
 * @author gvSIG Team
51
 */
52
public class VCSGisJInitServerImpl extends VCSGisJInitServerView
53
        implements Component, VCSGisJInitServer {
54

    
55
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisJInitServerImpl.class);
56

    
57
    private PickerController<JDBCServerExplorerParameters> connectionPicker;
58
    private Dialog dialog;
59
    private JDBCServerExplorerParameters connection;
60
    private JDBCServerExplorer server;
61

    
62
    public VCSGisJInitServerImpl() {
63
        translate();
64

    
65
        initializeComponents();
66
    }
67

    
68
    private void initializeComponents() {
69

    
70
        this.connectionPicker = DALSwingLocator.getSwingManager().createJDBCConnectionPickerController(
71
                this.cboConnections,
72
                this.btnAddConnection
73
        );
74
        this.connectionPicker.addChangeListener((ChangeEvent e) -> {
75
            connection = null;
76
            server = null;
77
            doUpdateEnableComponents();
78
        });
79

    
80
        this.setPreferredSize(
81
                new Dimension(520, 80)
82
        );
83

    
84
    }
85

    
86
    @Override
87
    public JComponent asJComponent() {
88
        return this;
89
    }
90

    
91
    @Override
92
    public void setDialog(Dialog dialog) {
93
        this.dialog = dialog;
94
        this.doUpdateEnableComponents();
95
    }
96

    
97
    private void doUpdateEnableComponents() {
98
        boolean enableInit = (this.getServer() != null);
99
        if (dialog != null) {
100
            this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK,
101
                    enableInit
102
            );
103
        }
104
    }
105

    
106
    @Override
107
    public int initialize() {
108
        if (getServer() != null) {
109
            return vcsgisadmin.init_repository(server);
110
        }
111
        return -1;
112
    }
113

    
114
    @Override
115
    public JDBCServerExplorerParameters getConnection() {
116
        if (this.connection == null) {
117
            this.connection = this.connectionPicker.get();
118
        }
119
        return this.connection;
120
    }
121
    
122
    @Override
123
    public String getConnectionLabel() {
124
        String label = "";
125
        if (this.connection != null) {
126
            ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) cboConnections.getSelectedItem();
127
            label = item.getLabel();
128
        }
129
        return label;
130
    }
131

    
132
    @Override
133
    public JDBCServerExplorer getServer() {
134
        if (this.server == null) {
135
            try {
136
                DataManager dataManager = DALLocator.getDataManager();
137
                if (getConnection() != null) {
138
                    this.server = (JDBCServerExplorer) dataManager.openServerExplorer(
139
                            getConnection().getExplorerName(), getConnection()
140
                    );
141
                }
142
            } catch (InitializeException | ProviderNotRegisteredException | ValidateDataParametersException ex) {
143
                LOGGER.warn("Can't open server " + getConnection().getExplorerName());
144
            }
145
        }
146
        return this.server;
147
    }
148

    
149
    private void translate() {
150
        ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
151
        swingManager.translate(this.lblConnection);
152
    }
153

    
154
}