67e595243f5792e23d259d5f81a1a0c6c2004e22
1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.util
.LinkedList
;
6 import be
.nikiroo
.jvcard
.Card
;
7 import be
.nikiroo
.jvcard
.Contact
;
8 import be
.nikiroo
.jvcard
.i18n
.Trans
;
9 import be
.nikiroo
.jvcard
.tui
.KeyAction
.DataType
;
10 import be
.nikiroo
.jvcard
.tui
.KeyAction
.Mode
;
12 import com
.googlecode
.lanterna
.gui2
.ActionListBox
;
13 import com
.googlecode
.lanterna
.gui2
.Direction
;
14 import com
.googlecode
.lanterna
.gui2
.Interactable
;
15 import com
.googlecode
.lanterna
.gui2
.LinearLayout
;
16 import com
.googlecode
.lanterna
.gui2
.TextGUIGraphics
;
17 import com
.googlecode
.lanterna
.gui2
.AbstractListBox
.ListItemRenderer
;
18 import com
.googlecode
.lanterna
.input
.KeyType
;
20 public class ContactList
extends MainContent
implements Runnable
{
22 private ActionListBox lines
;
24 private List
<String
> formats
= new LinkedList
<String
>();
25 private int selectedFormat
= -1;
26 private String format
= "";
28 public ContactList(Card card
) {
29 super(Direction
.VERTICAL
);
31 // TODO: should get that in an INI file
32 formats
.add("NICKNAME@3|FN@+|EMAIL@30");
33 formats
.add("FN@+|EMAIL@40");
36 lines
= new ActionListBox();
39 .setListItemRenderer(new ListItemRenderer
<Runnable
, ActionListBox
>() {
41 * This is the main drawing method for a single list box
42 * item, it applies the current theme to setup the colors
43 * and then calls {@code getLabel(..)} and draws the result
44 * using the supplied {@code TextGUIGraphics}. The graphics
45 * object is created just for this item and is restricted so
46 * that it can only draw on the area this item is occupying.
47 * The top-left corner (0x0) should be the starting point
48 * when drawing the item.
51 * Graphics object to draw with
53 * List box we are drawing an item from
55 * Index of the item we are drawing
57 * The item we are drawing
59 * Will be set to {@code true} if the item is
60 * currently selected, otherwise {@code false},
61 * but please notice what context 'selected'
62 * refers to here (see {@code setSelectedIndex})
64 * Will be set to {@code true} if the list box
65 * currently has input focus, otherwise {@code
68 public void drawItem(TextGUIGraphics graphics
,
69 ActionListBox listBox
, int index
, Runnable item
,
70 boolean selected
, boolean focused
) {
72 if (selected
&& focused
) {
74 .setForegroundColor(UiColors
.Element
.CONTACT_LINE_SELECTED
75 .getForegroundColor());
77 .setBackgroundColor(UiColors
.Element
.CONTACT_LINE_SELECTED
78 .getBackgroundColor());
81 .setForegroundColor(UiColors
.Element
.CONTACT_LINE
82 .getForegroundColor());
84 .setBackgroundColor(UiColors
.Element
.CONTACT_LINE
85 .getBackgroundColor());
88 String label
= getLabel(listBox
, index
, item
);
89 // label = TerminalTextUtils.fitString(label,
90 // graphics.getSize().getColumns());
92 Contact c
= ContactList
.this.card
.getContacts().get(
95 // we could use: " ", "┃", "│"...
96 //TODO: why +5 ?? padding problem?
97 label
= c
.toString(format
, " ┃ ", lines
.getSize().getColumns() + 5);
99 graphics
.putString(0, 0, label
);
103 addComponent(lines
, LinearLayout
104 .createLayoutData(LinearLayout
.Alignment
.Fill
));
109 private void switchFormat() {
110 if (formats
.size() == 0)
114 if (selectedFormat
>= formats
.size()) {
118 format
= formats
.get(selectedFormat
);
124 public void setCard(Card card
) {
129 for (int i
= 0; i
< card
.getContacts().size(); i
++) {
130 lines
.addItem("[contact line]", this);
134 lines
.setSelectedIndex(0);
139 // TODO: item selected.
140 // should we do something?
144 public String
getExitWarning() {
145 if (card
!= null && card
.isDirty()) {
146 return "Some of your contact information is not saved";
152 public List
<KeyAction
> getKeyBindings() {
153 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
156 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS
, 'e',
157 Trans
.StringId
.KEY_ACTION_EDIT_CONTACT
) {
159 public Object
getObject() {
160 int index
= lines
.getSelectedIndex();
161 return card
.getContacts().get(index
);
164 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS
, KeyType
.Enter
,
165 Trans
.StringId
.KEY_ACTION_VIEW_CONTACT
) {
167 public Object
getObject() {
168 int index
= lines
.getSelectedIndex();
169 return card
.getContacts().get(index
);
172 actions
.add(new KeyAction(Mode
.SWICTH_FORMAT
, KeyType
.Tab
,
173 Trans
.StringId
.KEY_ACTION_SWITCH_FORMAT
) {
175 public boolean onAction() {
184 public DataType
getDataType() {
185 return DataType
.CARD
;
188 public Mode
getMode() {
189 return Mode
.CONTACT_LIST
;
193 public String
move(int x
, int y
) {
194 lines
.setSelectedIndex(lines
.getSelectedIndex() + x
);
200 public String
getTitle() {
201 // TODO Auto-generated method stub