1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.widgets;
13 import java.util.List;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.DisposeEvent;
16 import org.eclipse.swt.events.DisposeListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.RowLayout;
20 import org.eclipse.swt.widgets.Combo;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Label;
23 import sk.baka.ikslibs.ref.EntityManager;
24 import sk.uniba.euromath.editor.lang.Messages;
25 /***
26 * Shows list of entities.
27 * @author Martin Vysny
28 */
29 public class EntityList extends AbstractUserInputWidget {
30 /***
31 * Here all controls will be placed.
32 */
33 protected final Composite composite;
34 /***
35 * Entity names list we can select an item from.
36 */
37 protected final List< ? extends String> entityNames;
38 /***
39 * The entity manager.
40 */
41 protected final EntityManager entityManager;
42 /***
43 * Constructs the instance.
44 * @param parent the parent component.
45 * @param entityManager an entity manager for the document.
46 * @param entityNames the list of entities that can be picked from.
47 */
48 public EntityList(Composite parent, EntityManager entityManager,
49 List< ? extends String> entityNames) {
50 super();
51 this.entityManager = entityManager;
52 this.entityNames = entityNames;
53
54 composite = new Composite(parent, SWT.NONE);
55 RowLayout shellLayout = new RowLayout();
56 shellLayout.type = SWT.VERTICAL;
57 shellLayout.fill = true;
58 composite.setLayout(shellLayout);
59 new Label(composite, SWT.NONE).setText(Messages
60 .getString("CHOOSE_ENTITY"));
61 entitySelector = new Combo(composite, SWT.READ_ONLY);
62
63
64 String[] entitiesDisplay = new String[entityNames.size()];
65 for (int i = 0; i < entityNames.size(); i++) {
66 String entityDisplay = entityNames.get(i);
67 entityDisplay += ": '"
68 + entityManager.getEntityValue(entityDisplay) + "'";
69 entitiesDisplay[i] = entityDisplay;
70 }
71 entitySelector.setItems(entitiesDisplay);
72 entitySelector.addSelectionListener(new SelectionAdapter() {
73
74
75
76
77 @Override
78 public void widgetSelected(SelectionEvent e) {
79 fillData();
80 fireDataModified();
81 }
82 });
83 entitySelector.addDisposeListener(new DisposeListener() {
84
85
86
87
88 public void widgetDisposed(DisposeEvent e) {
89 fillData();
90 }
91 });
92 }
93 /***
94 * Combo with all entities.
95 */
96 protected final Combo entitySelector;
97
98
99
100
101 public Composite getComposite() {
102 return composite;
103 }
104 /***
105 * Fills <code>_entityName</code> from the controls, setting
106 * <code>null</code> and an error message if no entity is selected. The
107 * combobox must not yet be disposed.
108 */
109 protected void fillData() {
110 lastMessages = null;
111 int selected = entitySelector.getSelectionIndex();
112 _entityName = (selected < 0) ? null : EntityList.this.entityNames
113 .get(selected);
114 if (selected < 0) {
115 lastMessages = MessageLevelEnum.ERROR.setMessage(lastMessages,
116 Messages.getString("SELECT_ENTITY"));
117 }
118 }
119 /***
120 * Returns actually selected entity name. Sets last error property if no
121 * entity is selected.
122 * @return entity name or <code>null</code> if no entity is selected.
123 */
124 public String getEntityName() {
125 return _entityName;
126 }
127 /***
128 * Entity name is stored here.
129 */
130 protected String _entityName = null;
131 /***
132 * Messages from last data retrieval.
133 */
134 protected ValidityMessages lastMessages = null;
135
136
137
138
139 public ValidityMessages getMessages() {
140 return lastMessages;
141 }
142
143
144
145
146 public String getState() {
147 return getEntityName();
148 }
149
150
151
152
153 public Class< ? > getStateClass() {
154 return String.class;
155 }
156
157
158
159
160 public void setState(Object model) {
161 final String newEntityName = (String) model;
162 final int index = entityNames.indexOf(newEntityName);
163 if (index < 0)
164 return;
165 _entityName = newEntityName;
166 if (!entitySelector.isDisposed())
167 entitySelector.select(index);
168 }
169 }