Commit | Line | Data |
---|---|---|
10dd1e38 NR |
1 | package be.nikiroo.jvcard.tui.windows; |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.Arrays; | |
5 | import java.util.LinkedList; | |
6 | import java.util.List; | |
7 | ||
8 | import jexer.TAction; | |
9 | import jexer.TApplication; | |
10 | import jexer.TKeypress; | |
11 | import jexer.TWindow; | |
12 | import be.nikiroo.jvcard.Card; | |
13 | import be.nikiroo.jvcard.Contact; | |
14 | import be.nikiroo.jvcard.resources.DisplayBundle; | |
15 | import be.nikiroo.jvcard.resources.DisplayOption; | |
16 | ||
17 | public class TuiContactListWindow extends TuiBrowserWindow { | |
18 | private TApplication app; | |
19 | private Card card; | |
20 | private String filter; | |
21 | private List<String> formats; | |
22 | private int selectedFormat; | |
23 | private String format; | |
24 | ||
25 | public TuiContactListWindow(TApplication app, Card card) { | |
26 | super(app, "Contacts", false); | |
27 | ||
28 | this.app = app; | |
29 | this.card = card; | |
30 | this.selectedFormat = -1; | |
31 | ||
32 | DisplayBundle map = new DisplayBundle(); | |
33 | formats = new LinkedList<String>(); | |
34 | for (String format : map.getString(DisplayOption.CONTACT_LIST_FORMAT) | |
35 | .split(",")) { | |
36 | formats.add(format); | |
37 | } | |
38 | ||
39 | addKeyBinding(TKeypress.kbTab, new TAction() { | |
40 | @Override | |
41 | public void DO() { | |
42 | switchFormat(); | |
43 | } | |
44 | }); | |
45 | ||
46 | addKeyBinding(TKeypress.kbQ, new TAction() { | |
47 | @Override | |
48 | public void DO() { | |
49 | close(); | |
50 | } | |
51 | }); | |
52 | ||
53 | switchFormat(); | |
54 | setCard(card); | |
55 | } | |
56 | ||
57 | @Override | |
58 | public void onAction(int selectedLine, int selectedColumn) { | |
59 | try { | |
60 | @SuppressWarnings("unused") | |
61 | TWindow w = new TuiContactWindow(app, card.get(selectedLine)); | |
62 | } catch (IndexOutOfBoundsException e) { | |
63 | setMessage("Fail to get contact", true); | |
64 | } | |
65 | } | |
66 | ||
67 | private void setCard(Card card) { | |
68 | List<String> headers = new ArrayList<String>(); | |
69 | for (String field : format.split("\\|")) { | |
70 | headers.add(field); | |
71 | } | |
72 | ||
73 | List<List<String>> dataLines = new ArrayList<List<String>>(); | |
74 | if (card != null) { | |
75 | for (Contact c : card) { | |
76 | if (filter == null | |
77 | || c.toString(format, "|").toLowerCase() | |
78 | .contains(filter.toLowerCase())) { | |
79 | dataLines.add(Arrays.asList(c.toStringArray(format, | |
80 | getWidth(), true))); | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | setData(headers, dataLines); | |
86 | } | |
87 | ||
88 | private void switchFormat() { | |
89 | if (formats.size() == 0) | |
90 | return; | |
91 | ||
92 | selectedFormat++; | |
93 | if (selectedFormat >= formats.size()) { | |
94 | selectedFormat = 0; | |
95 | } | |
96 | ||
97 | format = formats.get(selectedFormat); | |
98 | ||
99 | setCard(card); | |
100 | } | |
101 | ||
102 | // TODO | |
103 | private void setMessage(String message, boolean error) { | |
104 | if (error) { | |
105 | System.err.println(message); | |
106 | } else { | |
107 | System.out.println(message); | |
108 | } | |
109 | } | |
110 | } |