Revision 47284

View differences:

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/DefaultDALSwingLibrary.java
57 57
import org.gvsig.fmap.dal.swing.impl.featuretype.DefaultFeatureAttributesSelectionPanel;
58 58
import org.gvsig.fmap.dal.swing.impl.featuretype.DefaultFeatureTypePanel;
59 59
import org.gvsig.fmap.dal.swing.impl.featuretype.FeatureTypeEditorFactory;
60
import org.gvsig.fmap.dal.swing.impl.jdbc.JDBCConnectionPickerController;
60
import org.gvsig.fmap.dal.swing.impl.jdbc.JDBCConnectionPickerControllerImpl;
61 61
import org.gvsig.fmap.dal.swing.impl.searchPostProcess.distinctOn.CountAggregateOperation.CountAggregateOperationFactory;
62 62
import org.gvsig.fmap.dal.swing.impl.searchPostProcess.distinctOn.DistinctOn;
63 63
import org.gvsig.fmap.dal.swing.impl.searchPostProcess.distinctOn.DistinctOnFactory;
......
130 130
            
131 131
            DefaultSearchPanel.selfRegister();
132 132
            
133
            JDBCConnectionPickerController.selfRegister();
133
            JDBCConnectionPickerControllerImpl.selfRegister();
134 134
            DefaultJFeaturesForm.selfRegister();
135 135
            DefaultSearchParameters.registerPersistence();
136 136

  
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
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 java.util.ArrayList;
9
import java.util.List;
10
import java.util.function.Consumer;
11
import javax.swing.ComboBoxModel;
12
import javax.swing.JButton;
13
import javax.swing.JComboBox;
14
import javax.swing.SwingUtilities;
15
import org.apache.commons.lang3.StringUtils;
16
import org.gvsig.fmap.dal.DALLocator;
17
import org.gvsig.fmap.dal.DataManager;
18
import org.gvsig.fmap.dal.DataServerExplorerParameters;
19
import org.gvsig.fmap.dal.DataServerExplorerPool;
20
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry;
21
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
22
import org.gvsig.fmap.dal.swing.DALSwingLocator;
23
import static org.gvsig.fmap.dal.swing.impl.DefaultDALSwingLibrary.LIBRARY_NAME;
24
import org.gvsig.fmap.dal.swing.impl.featuretype.DefaultFeatureAttributesSelectionPanel;
25
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog;
26
import org.gvsig.tools.locator.ReferenceNotRegisteredException;
27
import org.gvsig.tools.swing.api.ListElement;
28
import org.gvsig.tools.swing.api.ToolsSwingLocator;
29
import org.gvsig.tools.swing.api.ToolsSwingUtils;
30
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController;
31
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
32
import org.gvsig.tools.swing.icontheme.IconTheme;
33
import org.gvsig.tools.util.CompareUtils;
34
import org.gvsig.tools.util.CompareUtils.NullSafeComparator;
35

  
36
/**
37
 *
38
 * @author jjdelcerro
39
 */
40
public class JDBCConnectionPickerController 
41
        extends AbstractPickerController<JDBCServerExplorerParameters>
42
        implements PickerController<JDBCServerExplorerParameters>
43
    {
44

  
45
    private final JComboBox cboConnection;
46
    private final JButton btnConnection;
47

  
48
    public JDBCConnectionPickerController(JComboBox cboConnection, JButton btnConnection) {
49
        this.btnConnection = btnConnection;
50
        this.cboConnection = cboConnection;
51
    
52
        this.fillConnections(this.cboConnection);
53
        this.btnConnection.addActionListener((ActionEvent e) -> {
54
            doAddConnection();
55
        });
56
        if( "...".equals(this.btnConnection.getText()) ) {
57
            this.btnConnection.setText("");
58
        }
59
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
60
        this.btnConnection.setIcon(theme.get("picker-dbconnection"));
61
        
62
        this.cboConnection.addItemListener((ItemEvent e) -> {
63
            if(!this.isEnabled()){
64
                return;
65
            }
66
            if (e.getStateChange() == ItemEvent.SELECTED) {
67
                SwingUtilities.invokeLater(() -> { fireChangeEvent(); });
68
            }
69
        });
70
    }
71
    
72
    private void fillConnections(JComboBox combo) {
73
        DataManager dataManager = DALLocator.getDataManager();
74
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
75

  
76
        DataServerExplorerParameters params;
77
        combo.removeAllItems();
78

  
79
        List<DataServerExplorerPoolEntry>entries = new ArrayList<>();
80
        pool.iterator().forEachRemaining((DataServerExplorerPoolEntry e) -> {entries.add(e); });
81
        entries.sort((DataServerExplorerPoolEntry o1, DataServerExplorerPoolEntry o2) -> { 
82
            return StringUtils.compare(o1.getName(), o2.getName()); 
83
        });
84
        
85
        for (DataServerExplorerPoolEntry entry : entries) {
86
            if (entry.getExplorerParameters() instanceof JDBCServerExplorerParameters) {
87
                JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters();
88
                combo.addItem(
89
                        new ListElement<>(entry.getName(), dbParams)
90
                );
91
            }
92
        }
93
        combo.setSelectedIndex(-1);
94
    }
95

  
96
    private void doAddConnection() {
97
        JDBCConnectionDialog dialog = DALSwingLocator.getSwingManager().createJDBCConectionDialog();
98
        dialog.showDialog();
99
        if (dialog.isCanceled()) {
100
            return;
101
        }
102
//        DataManager dataManager = DALLocator.getDataManager();
103
//        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
104

  
105
        JDBCServerExplorerParameters connectionParameters = dialog.getServerExplorerParameters();
106
//        pool.add(dialog.getConnectionName(), connectionParameters);
107
        this.fillConnections(this.cboConnection);
108
        ListElement.setSelected(cboConnection, connectionParameters);
109
    }
110
    
111
    @Override
112
    public JDBCServerExplorerParameters get() {
113
        ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
114
        if (item == null) {
115
            return null;
116
        }
117
        JDBCServerExplorerParameters conn = item.getValue();
118
        return conn;
119
    }
120

  
121
    @Override
122
    public void set(JDBCServerExplorerParameters value) {
123
        if(value == null){
124
            this.cboConnection.setSelectedIndex(-1);
125
            this.cboConnection.setSelectedItem(null);
126
            return;
127
        }
128
        ComboBoxModel<ListElement<JDBCServerExplorerParameters>> model = this.cboConnection.getModel();
129
        for (int i = 0; i < model.getSize(); i++) {
130
            JDBCServerExplorerParameters params = model.getElementAt(i).getValue();
131
            if( params == value ) {
132
                this.cboConnection.setSelectedIndex(i);
133
                return;
134
            }
135
        }
136
        for (int i = 0; i < model.getSize(); i++) {
137
            JDBCServerExplorerParameters params = model.getElementAt(i).getValue();
138
            if( StringUtils.equals(params.getUrl(), value.getUrl())){
139
                this.cboConnection.setSelectedIndex(i);
140
                return;
141
            }
142
        }
143
        //Si llega aqu? a?adimos un nuevo item
144
        cboConnection.addItem(
145
                new ListElement<>(value.getHost()+":"+value.getDBName(), value)
146
        );
147
        this.cboConnection.setSelectedIndex(this.cboConnection.getModel().getSize()-1);
148
    }
149

  
150
    @Override
151
    public void coerceAndSet(Object value) {
152
        JDBCServerExplorerParameters params;
153
        try {
154
            params = (JDBCServerExplorerParameters) value;
155
        } catch(Exception ex) {
156
            throw new IllegalArgumentException("Can't coerce value to JDBCServerExplorerParameters", ex);
157
        }
158
        this.set(params);
159
    }
160

  
161
    @Override
162
    public void setEnabled(boolean enabled) {
163
        this.cboConnection.setEnabled(enabled);
164
        this.btnConnection.setEnabled(enabled);
165
    }
166

  
167
    @Override
168
    public boolean isEnabled() {
169
        return this.cboConnection.isEnabled();
170
    }
171
    
172
    public static void selfRegister() {
173
        boolean n = ToolsSwingUtils.registerIcons( 
174
                JDBCConnectionPickerController.class,
175
                null,
176
                LIBRARY_NAME,
177
                new String[]{ "picker", "picker-dbconnection"}
178
        );            
179
    }
180
    
181
    
182
}
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/JDBCConnectionPickerControllerImpl.java
1
package org.gvsig.fmap.dal.swing.impl.jdbc;
2

  
3
import org.gvsig.fmap.dal.swing.jdbc.JDBCServerExplorerParametersController;
4
import java.awt.event.ActionEvent;
5
import java.awt.event.ActionListener;
6
import java.awt.event.ItemEvent;
7
import java.awt.event.ItemListener;
8
import java.net.URL;
9
import java.util.ArrayList;
10
import java.util.List;
11
import java.util.function.Consumer;
12
import javax.swing.ComboBoxModel;
13
import javax.swing.JButton;
14
import javax.swing.JComboBox;
15
import javax.swing.SwingUtilities;
16
import org.apache.commons.lang3.StringUtils;
17
import org.gvsig.fmap.dal.DALLocator;
18
import org.gvsig.fmap.dal.DataManager;
19
import org.gvsig.fmap.dal.DataServerExplorerParameters;
20
import org.gvsig.fmap.dal.DataServerExplorerPool;
21
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry;
22
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
23
import org.gvsig.fmap.dal.swing.DALSwingLocator;
24
import static org.gvsig.fmap.dal.swing.impl.DefaultDALSwingLibrary.LIBRARY_NAME;
25
import org.gvsig.fmap.dal.swing.impl.featuretype.DefaultFeatureAttributesSelectionPanel;
26
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog;
27
import org.gvsig.tools.locator.ReferenceNotRegisteredException;
28
import org.gvsig.tools.swing.api.ListElement;
29
import org.gvsig.tools.swing.api.ToolsSwingLocator;
30
import org.gvsig.tools.swing.api.ToolsSwingUtils;
31
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController;
32
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
33
import org.gvsig.tools.swing.icontheme.IconTheme;
34
import org.gvsig.tools.util.CompareUtils;
35
import org.gvsig.tools.util.CompareUtils.NullSafeComparator;
36

  
37
/**
38
 *
39
 * @author jjdelcerro
40
 */
41
public class JDBCConnectionPickerControllerImpl 
42
        extends AbstractPickerController<JDBCServerExplorerParameters>
43
        implements JDBCServerExplorerParametersController
44
    {
45

  
46
    private final JComboBox cboConnection;
47
    private final JButton btnConnection;
48

  
49
    public JDBCConnectionPickerControllerImpl(JComboBox cboConnection, JButton btnConnection) {
50
        this.btnConnection = btnConnection;
51
        this.cboConnection = cboConnection;
52
    
53
        this.fillConnections(this.cboConnection);
54
        this.btnConnection.addActionListener((ActionEvent e) -> {
55
            doAddConnection();
56
        });
57
        if( "...".equals(this.btnConnection.getText()) ) {
58
            this.btnConnection.setText("");
59
        }
60
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
61
        this.btnConnection.setIcon(theme.get("picker-dbconnection"));
62
        
63
        this.cboConnection.addItemListener((ItemEvent e) -> {
64
            if(!this.isEnabled()){
65
                return;
66
            }
67
            if (e.getStateChange() == ItemEvent.SELECTED) {
68
                SwingUtilities.invokeLater(() -> { fireChangeEvent(); });
69
            }
70
        });
71
    }
72
    
73
    private void fillConnections(JComboBox combo) {
74
        DataManager dataManager = DALLocator.getDataManager();
75
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
76

  
77
        DataServerExplorerParameters params;
78
        combo.removeAllItems();
79

  
80
        List<DataServerExplorerPoolEntry>entries = new ArrayList<>();
81
        pool.iterator().forEachRemaining((DataServerExplorerPoolEntry e) -> {entries.add(e); });
82
        entries.sort((DataServerExplorerPoolEntry o1, DataServerExplorerPoolEntry o2) -> { 
83
            return StringUtils.compare(o1.getName(), o2.getName()); 
84
        });
85
        
86
        for (DataServerExplorerPoolEntry entry : entries) {
87
            if (entry.getExplorerParameters() instanceof JDBCServerExplorerParameters) {
88
                JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters();
89
                combo.addItem(
90
                        new ListElement<>(entry.getName(), dbParams)
91
                );
92
            }
93
        }
94
        combo.setSelectedIndex(-1);
95
    }
96

  
97
    private void doAddConnection() {
98
        JDBCConnectionDialog dialog = DALSwingLocator.getSwingManager().createJDBCConectionDialog();
99
        dialog.showDialog();
100
        if (dialog.isCanceled()) {
101
            return;
102
        }
103
//        DataManager dataManager = DALLocator.getDataManager();
104
//        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
105

  
106
        JDBCServerExplorerParameters connectionParameters = dialog.getServerExplorerParameters();
107
//        pool.add(dialog.getConnectionName(), connectionParameters);
108
        this.fillConnections(this.cboConnection);
109
        ListElement.setSelected(cboConnection, connectionParameters);
110
    }
111
    
112
    @Override
113
    public JDBCServerExplorerParameters get() {
114
        ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
115
        if (item == null) {
116
            return null;
117
        }
118
        JDBCServerExplorerParameters conn = item.getValue();
119
        return conn;
120
    }
121

  
122
    @Override
123
    public String getName() {
124
        ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
125
        if (item == null) {
126
            return null;
127
        }
128
        String name = item.getLabel();
129
        return name;
130
    }
131

  
132
    @Override
133
    public void set(JDBCServerExplorerParameters value) {
134
        if(value == null){
135
            this.cboConnection.setSelectedIndex(-1);
136
            this.cboConnection.setSelectedItem(null);
137
            return;
138
        }
139
        ComboBoxModel<ListElement<JDBCServerExplorerParameters>> model = this.cboConnection.getModel();
140
        for (int i = 0; i < model.getSize(); i++) {
141
            JDBCServerExplorerParameters params = model.getElementAt(i).getValue();
142
            if( params == value ) {
143
                this.cboConnection.setSelectedIndex(i);
144
                return;
145
            }
146
        }
147
        for (int i = 0; i < model.getSize(); i++) {
148
            JDBCServerExplorerParameters params = model.getElementAt(i).getValue();
149
            if( StringUtils.equals(params.getUrl(), value.getUrl())){
150
                this.cboConnection.setSelectedIndex(i);
151
                return;
152
            }
153
        }
154
        //Si llega aqu? a?adimos un nuevo item
155
        cboConnection.addItem(
156
                new ListElement<>(value.getHost()+":"+value.getDBName(), value)
157
        );
158
        this.cboConnection.setSelectedIndex(this.cboConnection.getModel().getSize()-1);
159
    }
160

  
161
    @Override
162
    public void coerceAndSet(Object value) {
163
        JDBCServerExplorerParameters params;
164
        try {
165
            params = (JDBCServerExplorerParameters) value;
166
        } catch(Exception ex) {
167
            throw new IllegalArgumentException("Can't coerce value to JDBCServerExplorerParameters", ex);
168
        }
169
        this.set(params);
170
    }
171

  
172
    @Override
173
    public void setEnabled(boolean enabled) {
174
        this.cboConnection.setEnabled(enabled);
175
        this.btnConnection.setEnabled(enabled);
176
    }
177

  
178
    @Override
179
    public boolean isEnabled() {
180
        return this.cboConnection.isEnabled();
181
    }
182
    
183
    public static void selfRegister() {
184
        boolean n = ToolsSwingUtils.registerIcons(JDBCConnectionPickerControllerImpl.class,
185
                null,
186
                LIBRARY_NAME,
187
                new String[]{ "picker", "picker-dbconnection"}
188
        );            
189
    }
190
    
191
    
192
}

Also available in: Unified diff