gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / bookmarkshistory / DefaultHistoryPanel.java @ 1990
History | View | Annotate | Download (2.23 KB)
1 |
package org.gvsig.tools.swing.impl.bookmarkshistory; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import javax.swing.DefaultListModel; |
5 |
import javax.swing.event.ListSelectionEvent; |
6 |
import javax.swing.event.ListSelectionListener; |
7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import org.gvsig.tools.bookmarksandhistory.History; |
9 |
import org.gvsig.tools.util.LabeledValue; |
10 |
import org.gvsig.tools.util.LabeledValueImpl; |
11 |
|
12 |
/**
|
13 |
*
|
14 |
* @author jjdelcerro
|
15 |
* @param <T>
|
16 |
*/
|
17 |
public class DefaultHistoryPanel<T> |
18 |
extends DefaultHistoryPanelView
|
19 |
{ |
20 |
|
21 |
private T currentValue;
|
22 |
private final History<T> history; |
23 |
|
24 |
public DefaultHistoryPanel(History<T> history) {
|
25 |
this.history = history;
|
26 |
this.initComponents();
|
27 |
} |
28 |
|
29 |
private void initComponents() { |
30 |
DefaultListModel<LabeledValue<T>> model = new DefaultListModel<>(); |
31 |
for (T value : history) {
|
32 |
String label;
|
33 |
if( value instanceof LabeledValue ) { |
34 |
label = ((LabeledValue)value).getLabel(); |
35 |
} else {
|
36 |
label = value.toString(); |
37 |
} |
38 |
label = StringUtils.abbreviate(label, 45);
|
39 |
model.addElement(new LabeledValueImpl<>(label, value));
|
40 |
} |
41 |
this.currentValue = null; |
42 |
this.lstHistory.setModel(model);
|
43 |
this.lstHistory.addListSelectionListener(new ListSelectionListener() { |
44 |
@Override
|
45 |
public void valueChanged(ListSelectionEvent e) { |
46 |
if( e.getValueIsAdjusting()) {
|
47 |
return;
|
48 |
} |
49 |
LabeledValue<T> value = (LabeledValue<T>) lstHistory.getSelectedValue(); |
50 |
if( value == null ) { |
51 |
return;
|
52 |
} |
53 |
currentValue = value.getValue(); |
54 |
String label;
|
55 |
if( currentValue instanceof LabeledValue ) { |
56 |
label = ((LabeledValue)currentValue).getLabel(); |
57 |
} else {
|
58 |
label = currentValue.toString(); |
59 |
} |
60 |
txtFormula.setText(label); |
61 |
txtFormula.setCaretPosition(0);
|
62 |
} |
63 |
}); |
64 |
this.setPreferredSize(new Dimension(400, 300)); |
65 |
} |
66 |
|
67 |
public T getSelectedValue() {
|
68 |
return this.currentValue; |
69 |
} |
70 |
} |