View Javadoc

1   /*
2    * Created on Mar 18, 2005. Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
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  		// create components
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")); //$NON-NLS-1$
61  		entitySelector = new Combo(composite, SWT.READ_ONLY);
62  		// fill the entitySelector with entities that can be inserted at
63  		// specified pointer
64  		String[] entitiesDisplay = new String[entityNames.size()];
65  		for (int i = 0; i < entityNames.size(); i++) {
66  			String entityDisplay = entityNames.get(i);
67  			entityDisplay += ": '" //$NON-NLS-1$
68  					+ entityManager.getEntityValue(entityDisplay) + "'"; //$NON-NLS-1$
69  			entitiesDisplay[i] = entityDisplay;
70  		}
71  		entitySelector.setItems(entitiesDisplay);
72  		entitySelector.addSelectionListener(new SelectionAdapter() {
73  			/*
74  			 * (non-Javadoc)
75  			 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
76  			 */
77  			@Override
78  			public void widgetSelected(SelectionEvent e) {
79  				fillData();
80  				fireDataModified();
81  			}
82  		});
83  		entitySelector.addDisposeListener(new DisposeListener() {
84  			/*
85  			 * (non-Javadoc)
86  			 * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
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  	 * (non-Javadoc)
99  	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#getComposite()
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")); //$NON-NLS-1$
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 	 * (non-Javadoc)
137 	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#getLastError()
138 	 */
139 	public ValidityMessages getMessages() {
140 		return lastMessages;
141 	}
142 	/*
143 	 * (non-Javadoc)
144 	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#getModel()
145 	 */
146 	public String getState() {
147 		return getEntityName();
148 	}
149 	/*
150 	 * (non-Javadoc)
151 	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#getModelClass()
152 	 */
153 	public Class< ? > getStateClass() {
154 		return String.class;
155 	}
156 	/*
157 	 * (non-Javadoc)
158 	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#setModel(java.lang.Object)
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 }