*/
package eu.etaxonomy.taxeditor.ui.section;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.EnumSet;
+import java.util.List;
import java.util.Observable;
import java.util.Observer;
protected Control createToolbar() {
ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
- Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
+ Action addAction = new Action("Add", IAction.AS_PUSH_BUTTON){
@Override
public void run() {
- ELEMENT element = createNewElement();
- if(element != null){
- addElement(element);
- if(! getSection().isExpanded()) {
- getSection().setExpanded(true);
- }
- internalUpdateSection(true);
- }
+ ELEMENT element = createNewElement();
+ if(element != null){
+ addElement(element);
+ if(! getSection().isExpanded()) {
+ getSection().setExpanded(true);
+ }
+ internalUpdateSection(true);
+ }
}
};
addAction.setImageDescriptor(new ImageDescriptor() {
});
addAction.setToolTipText(getTooltipString());
+ Action browseAction = null;
+ if(allowAddExisting()){
+ browseAction = new Action("Browse", IAction.AS_PUSH_BUTTON){
+ @Override
+ public void run() {
+ ELEMENT element = addExisting();
+ if(element != null){
+ addElement(element);
+ if(! getSection().isExpanded()) {
+ getSection().setExpanded(true);
+ }
+ internalUpdateSection(true);
+ }
+ }
+ };
+ browseAction.setImageDescriptor(new ImageDescriptor() {
+
+ @Override
+ public ImageData getImageData() {
+ return ImageResources.getImage(ImageResources.BROWSE_ICON).getImageData();
+ }
+ });
+ browseAction.setToolTipText("Browse");
+ }
+
toolBarManager.add(addAction);
+ if(browseAction!=null){
+ toolBarManager.add(browseAction);
+ }
return toolBarManager.createControl(this);
}
* Override in subclasses if you want to have a different behaviour.
*/
protected void setSectionTitle() {
- if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
+ Collection<ELEMENT> collection = getCollection(getEntity());
+ if(collection != null && collection.size() > 0){
this.setText(getTitleString() + " +");
}else{
this.setText(getTitleString());
*/
private void renderContent(boolean forceExpansion)
{
- Collection<ELEMENT> elements = getCollection(getEntity());
+ List<ELEMENT> elements = new ArrayList<>(getCollection(getEntity()));
+ Collections.sort(elements, getComparator());
if(elements == null || elements.isEmpty()){
createEmptyContent();
return entityCollectionElement;
}
+ /**
+ * Returns the {@link Comparator} specific for the ELEMENTs
+ * which is used to sort the elements
+ * @return the comparator for ELEMENT
+ */
+ public abstract Comparator<ELEMENT> getComparator();
+
/**
* Get the specific collection of this entity
*
*/
public abstract Collection<ELEMENT> getCollection(ENTITY entity);
+
/**
* Create a new Element for this collection
*
*/
public abstract void addElement(ELEMENT element);
+ /**
+ * Add an existing element to the entities collection.
+ * @return the existing element
+ */
+ public abstract ELEMENT addExisting();
+
+ /**
+ * If <code>true</code> the section will also display
+ * a browse icon to choose from existing elements.
+ * <br>
+ * <b>Note:</b> when returning true you have to make sure
+ * to implement the {@link #addExisting()} method
+ * @return true if existing entities can be added;
+ * false otherwise
+ */
+ public abstract boolean allowAddExisting();
+
/**
* Remove an element from the entities collection
*