/
NavigationPanel.java
132 lines (108 loc) · 4.31 KB
/
NavigationPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package im.jeanfrancois.etsmaps.ui;
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.swing.EventComboBoxModel;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.util.concurrent.ReadWriteLock;
import com.google.inject.Inject;
import im.jeanfrancois.etsmaps.model.Landmark;
import im.jeanfrancois.etsmaps.model.Leg;
import im.jeanfrancois.etsmaps.model.NavigableMap;
import im.jeanfrancois.etsmaps.model.Route;
import net.miginfocom.swing.MigLayout;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Comparator;
/**
* Navigation panel that contains all the UI controls to search for
* landmarks and navigate between them.
*
* @author jfim
*/
public class NavigationPanel extends JPanel {
private EventList<Leg> routeLegs = new BasicEventList<Leg>();
private NavigableMap map;
private MapDisplayComponent mapDisplayComponent;
private JComboBox originComboBox;
private JComboBox destinationComboBox;
@Inject
public NavigationPanel(NavigableMap navigableMap, MapDisplayComponent mapDisplayComponent) {
map = navigableMap;
this.mapDisplayComponent = mapDisplayComponent;
setLayout(new MigLayout("wrap 2", "[][grow, fill]", "[][][][][][grow,fill]"));
add(new JLabel(new ImageIcon(NavigationPanel.class.getClassLoader().getResource("etsmaps-logo.png"))), "span");
EventList<Landmark> landmarks = new BasicEventList<Landmark>();
landmarks.addAll(map.getLandmarks());
EventList<Landmark> sortedLandmarks = new SortedList<Landmark>(landmarks, new Comparator<Landmark>() {
@Override
public int compare(Landmark o1, Landmark o2) {
return o1.getName().compareTo(o2.getName());
}
});
KeyListener keyListener = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
navigate();
}
}
};
add(new JLabel("De"));
originComboBox = new JComboBox(new EventComboBoxModel<Landmark>(sortedLandmarks));
add(originComboBox);
AutoCompleteDecorator.decorate(originComboBox);
originComboBox.getEditor().getEditorComponent().addKeyListener(keyListener);
add(new JLabel("Vers"));
destinationComboBox = new JComboBox(new EventComboBoxModel<Landmark>(sortedLandmarks));
add(destinationComboBox);
AutoCompleteDecorator.decorate(destinationComboBox);
destinationComboBox.getEditor().getEditorComponent().addKeyListener(keyListener);
final JButton button = new JButton("Naviguer");
add(button, "span 2, alignx right");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
navigate();
}
});
add(new JLabel("Directions"), "span 2");
final JTable table = new JTable(new EventTableModel<Leg>(routeLegs,
new TableFormat<Leg>() {
public int getColumnCount() {
return 2;
}
public String getColumnName(int i) {
if(i == 0)
return "Direction";
return "Distance";
}
public Object getColumnValue(Leg leg, int i) {
if (i == 0) {
return leg.getDescription();
}
NumberFormat formatter = new DecimalFormat("0.0");
return formatter.format(leg.getLengthInMetres()) + " m";
}
}));
add(new JScrollPane(table), "width 100%, span 2");
}
public void navigate() {
Route route = map.getRouteBetweenLandmarks((Landmark) originComboBox.getSelectedItem(),
(Landmark) destinationComboBox.getSelectedItem());
ArrayList<Leg> legs = new ArrayList<Leg>();
for (int i = 0; i < route.getLegCount(); ++i) {
legs.add(route.getLeg(i));
}
ReadWriteLock lock = routeLegs.getReadWriteLock();
lock.writeLock().lock();
routeLegs.clear();
routeLegs.addAll(legs);
lock.writeLock().unlock();
mapDisplayComponent.overlayRoute(route);
}
}