Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / jdbc / JDBCConnectionPickerController.java @ 44591

History | View | Annotate | Download (5.42 KB)

1
package org.gvsig.fmap.dal.swing.impl.jdbc;
2

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import java.awt.event.ItemEvent;
6
import java.awt.event.ItemListener;
7
import java.net.URL;
8
import javax.swing.ComboBoxModel;
9
import javax.swing.JButton;
10
import javax.swing.JComboBox;
11
import org.gvsig.fmap.dal.DALLocator;
12
import org.gvsig.fmap.dal.DataManager;
13
import org.gvsig.fmap.dal.DataServerExplorerParameters;
14
import org.gvsig.fmap.dal.DataServerExplorerPool;
15
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry;
16
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
17
import org.gvsig.fmap.dal.swing.DALSwingLocator;
18
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog;
19
import org.gvsig.tools.swing.api.ListElement;
20
import org.gvsig.tools.swing.api.ToolsSwingLocator;
21
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController;
22
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
23
import org.gvsig.tools.swing.icontheme.IconTheme;
24

    
25
/**
26
 *
27
 * @author jjdelcerro
28
 */
29
public class JDBCConnectionPickerController 
30
        extends AbstractPickerController<JDBCServerExplorerParameters>
31
        implements PickerController<JDBCServerExplorerParameters>
32
    {
33

    
34
    private final JComboBox cboConnection;
35
    private final JButton btnConnection;
36

    
37
    public JDBCConnectionPickerController(JComboBox cboConnection, JButton btnConnection) {
38
        this.btnConnection = btnConnection;
39
        this.cboConnection = cboConnection;
40
    
41
        this.fillConnections(this.cboConnection);
42
        this.btnConnection.addActionListener(new ActionListener() {
43
            @Override
44
            public void actionPerformed(ActionEvent e) {
45
                doAddConnection();
46
            }
47
        });
48
        if( "...".equals(this.btnConnection.getText()) ) {
49
            this.btnConnection.setText("");
50
        }
51
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
52
        this.btnConnection.setIcon(theme.get("database-connection-add"));
53
        
54
        this.cboConnection.addItemListener(new ItemListener() {
55
            @Override
56
            public void itemStateChanged(ItemEvent e) {
57
                if (e.getStateChange() == ItemEvent.SELECTED) {
58
                    fireChangeEvent();
59
                }
60
            }
61
        });
62
    }
63
    
64
    private void fillConnections(JComboBox combo) {
65
        DataManager dataManager = DALLocator.getDataManager();
66
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
67

    
68
        DataServerExplorerParameters params;
69
        combo.removeAllItems();
70
        for (DataServerExplorerPoolEntry entry : pool) {
71
            if (entry.getExplorerParameters() instanceof JDBCServerExplorerParameters) {
72
                JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters();
73
                combo.addItem(
74
                        new ListElement<>(entry.getName(), dbParams)
75
                );
76
            }
77
        }
78
        combo.setSelectedIndex(-1);
79
    }
80

    
81
    private void doAddConnection() {
82
        JDBCConnectionDialog dialog = DALSwingLocator.getSwingManager().createJDBCConectionDialog();
83
        dialog.showDialog();
84
        if (dialog.isCanceled()) {
85
            return;
86
        }
87
        DataManager dataManager = DALLocator.getDataManager();
88
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
89

    
90
        pool.add(dialog.getConnectionName(), dialog.getServerExplorerParameters());
91
        this.fillConnections(this.cboConnection);
92
        ListElement.setSelected(cboConnection, dialog);
93
    }
94
    
95
    @Override
96
    public JDBCServerExplorerParameters get() {
97
        ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
98
        if (item == null) {
99
            return null;
100
        }
101
        JDBCServerExplorerParameters conn = item.getValue();
102
        return conn;
103
    }
104

    
105
    @Override
106
    public void set(JDBCServerExplorerParameters value) {
107
        ComboBoxModel<ListElement<JDBCServerExplorerParameters>> model = this.cboConnection.getModel();
108
        for (int i = 0; i < model.getSize(); i++) {
109
            JDBCServerExplorerParameters params = model.getElementAt(i).getValue();
110
            if( params == value ) {
111
                this.cboConnection.setSelectedIndex(i);
112
                return;
113
            }
114
        }
115
    }
116

    
117
    @Override
118
    public void coerceAndSet(Object value) {
119
        JDBCServerExplorerParameters params;
120
        try {
121
            params = (JDBCServerExplorerParameters) value;
122
        } catch(Exception ex) {
123
            throw new IllegalArgumentException("Can't coerce value to JDBCServerExplorerParameters", ex);
124
        }
125
        this.set(params);
126
    }
127

    
128
    @Override
129
    public void setEnabled(boolean enabled) {
130
        this.cboConnection.setEnabled(enabled);
131
        this.btnConnection.setEnabled(enabled);
132
    }
133

    
134
    @Override
135
    public boolean isEnabled() {
136
        return this.cboConnection.isEnabled();
137
    }
138
    
139
    public static void selfRegister() {
140
        String[][] iconNames = new String[][]{
141
            new String[]{"dalswing", "database-connection-add"}
142
        };
143
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
144
        for (String[] icon : iconNames) {
145
            URL url = JDBCConnectionPickerController.class.getResource(icon[1] + ".png");
146
            theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
147
        }
148
        
149
    }
150
    
151
    
152
}