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 / VCSWorkspacePickerControllerImpl.java @ 2381
History | View | Annotate | Download (3.62 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.vcsgis.swing.impl; |
7 |
|
8 |
import java.awt.Component; |
9 |
import java.awt.event.ActionEvent; |
10 |
import java.util.List; |
11 |
import javax.swing.ComboBoxModel; |
12 |
import javax.swing.DefaultListCellRenderer; |
13 |
import javax.swing.JButton; |
14 |
import javax.swing.JComboBox; |
15 |
import javax.swing.JLabel; |
16 |
import javax.swing.JList; |
17 |
import org.gvsig.vcsgis.lib.VCSGisLocator; |
18 |
import org.gvsig.vcsgis.lib.VCSGisManager; |
19 |
import org.gvsig.vcsgis.lib.VCSGisWorkspace; |
20 |
import org.gvsig.vcsgis.swing.VCSWorkspacePickerController; |
21 |
import org.gvsig.tools.swing.api.ListElement; |
22 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
23 |
|
24 |
/**
|
25 |
*
|
26 |
* @author fdiaz
|
27 |
*/
|
28 |
public class VCSWorkspacePickerControllerImpl extends AbstractPickerController<VCSGisWorkspace> implements VCSWorkspacePickerController { |
29 |
|
30 |
private final JComboBox cboWorkspaces; |
31 |
private final JButton btnInitWorkspace; |
32 |
|
33 |
public VCSWorkspacePickerControllerImpl(JComboBox cboWorkspaces, JButton btnInitWorkspace) { |
34 |
this.cboWorkspaces = cboWorkspaces;
|
35 |
this.btnInitWorkspace = btnInitWorkspace;
|
36 |
|
37 |
this.fillWorkspaces(this.cboWorkspaces); |
38 |
this.btnInitWorkspace.addActionListener((ActionEvent e) -> { |
39 |
//TODO: Not implemented yet
|
40 |
}); |
41 |
|
42 |
this.cboWorkspaces.setRenderer(new DefaultListCellRenderer() { |
43 |
@Override
|
44 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
45 |
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
46 |
label.setIcon(null);
|
47 |
try {
|
48 |
VCSGisWorkspace workspace = (VCSGisWorkspace) value; |
49 |
label.setText(workspace.getLabel()); |
50 |
} catch (Exception ex) { |
51 |
// Do nothing, ignore
|
52 |
} |
53 |
return label;
|
54 |
} |
55 |
}); |
56 |
|
57 |
|
58 |
|
59 |
} |
60 |
|
61 |
@Override
|
62 |
public VCSGisWorkspace get() {
|
63 |
return (VCSGisWorkspace) cboWorkspaces.getSelectedItem();
|
64 |
} |
65 |
|
66 |
@Override
|
67 |
public void set(VCSGisWorkspace value) { |
68 |
ComboBoxModel<ListElement<VCSGisWorkspace>> model = this.cboWorkspaces.getModel(); |
69 |
for (int i = 0; i < model.getSize(); i++) { |
70 |
VCSGisWorkspace item = model.getElementAt(i).getValue(); |
71 |
if( item == value ) {
|
72 |
this.cboWorkspaces.setSelectedIndex(i);
|
73 |
return;
|
74 |
} |
75 |
} |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public void coerceAndSet(Object value) { |
80 |
VCSGisWorkspace workspace; |
81 |
try {
|
82 |
workspace = (VCSGisWorkspace) value; |
83 |
} catch(Exception ex) { |
84 |
throw new IllegalArgumentException("Can't coerce value to vcsgisWorkspace", ex); |
85 |
} |
86 |
this.set(workspace);
|
87 |
} |
88 |
|
89 |
@Override
|
90 |
public void setEnabled(boolean enabled) { |
91 |
this.cboWorkspaces.setEnabled(enabled);
|
92 |
this.btnInitWorkspace.setEnabled(enabled);
|
93 |
} |
94 |
|
95 |
@Override
|
96 |
public boolean isEnabled() { |
97 |
return this.cboWorkspaces.isEnabled(); |
98 |
} |
99 |
|
100 |
private void fillWorkspaces(JComboBox cboWorkspaces) { |
101 |
VCSGisManager manager = VCSGisLocator.getVCSGisManager(); |
102 |
List<VCSGisWorkspace> workspaces = manager.getWorkspaces();
|
103 |
|
104 |
cboWorkspaces.removeAllItems(); |
105 |
for (VCSGisWorkspace workspace : workspaces) {
|
106 |
cboWorkspaces.addItem(workspace); |
107 |
} |
108 |
cboWorkspaces.setSelectedIndex(-1);
|
109 |
|
110 |
} |
111 |
|
112 |
} |