Statistics
| Revision:

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 @ 2323

History | View | Annotate | Download (2.31 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
        this.txtFormula.setLineWrap(true);
31
        this.txtFormula.setEditable(false);
32
        DefaultListModel<LabeledValue<T>> model = new DefaultListModel<>();
33
        for (T value : history) {
34
            String label;
35
            if( value instanceof LabeledValue ) {
36
                label = ((LabeledValue)value).getLabel();
37
            } else {
38
                label = value.toString();
39
            }
40
            label = StringUtils.abbreviate(label, 45);
41
            model.addElement(new LabeledValueImpl<>(label, value));
42
        }
43
        this.currentValue = null;
44
        this.lstHistory.setModel(model);
45
        this.lstHistory.addListSelectionListener(new ListSelectionListener() {
46
            @Override
47
            public void valueChanged(ListSelectionEvent e) {
48
                if( e.getValueIsAdjusting()) {
49
                    return;
50
                }
51
                LabeledValue<T> value = (LabeledValue<T>) lstHistory.getSelectedValue();
52
                if( value == null ) {
53
                    return;
54
                }
55
                currentValue = value.getValue();
56
                String label;
57
                if( currentValue instanceof LabeledValue ) {
58
                    label = ((LabeledValue)currentValue).getLabel();
59
                } else {
60
                    label = currentValue.toString();
61
                }
62
                txtFormula.setText(label);
63
                txtFormula.setCaretPosition(0);
64
            }
65
        });
66
        this.setPreferredSize(new Dimension(500, 400));
67
    }
68
    
69
    public T getSelectedValue() {
70
        return this.currentValue;
71
    }
72
}