be5750fcc3b27c1c4ab66e398d319805046c91e1
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / NomenclaturalReferenceSelectionDialog.java
1 package eu.etaxonomy.taxeditor.ui.dialog.selection;
2
3 import java.text.Collator;
4 import java.util.Comparator;
5
6 import org.eclipse.core.runtime.Assert;
7 import org.eclipse.core.runtime.ListenerList;
8 import org.eclipse.jface.viewers.IColorProvider;
9 import org.eclipse.jface.viewers.IFontProvider;
10 import org.eclipse.jface.viewers.ILabelDecorator;
11 import org.eclipse.jface.viewers.ILabelProvider;
12 import org.eclipse.jface.viewers.ILabelProviderListener;
13 import org.eclipse.jface.viewers.LabelProvider;
14 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
15 import org.eclipse.jface.viewers.StyledCellLabelProvider;
16 import org.eclipse.jface.viewers.StyledString;
17 import org.eclipse.jface.viewers.ViewerCell;
18 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.swt.widgets.Text;
29 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
30 import org.eclipse.ui.internal.WorkbenchImages;
31
32 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
33 import eu.etaxonomy.cdm.api.service.IReferenceService;
34 import eu.etaxonomy.cdm.model.reference.Reference;
35 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
36 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
37 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
38 import eu.etaxonomy.taxeditor.store.CdmStore;
39 import eu.etaxonomy.taxeditor.ui.dialog.selection.CdmFilteredItemsSelectionDialog.ItemsFilter;
40 import eu.etaxonomy.taxeditor.ui.dialog.selection.CdmFilteredItemsSelectionDialog.ItemsListSeparator;
41
42 public class NomenclaturalReferenceSelectionDialog extends
43 ReferenceSelectionDialog {
44
45 protected NomenclaturalReferenceSelectionDialog(Shell shell,
46 ConversationHolder conversation, String title, boolean multi,
47 Reference reference) {
48 super(shell, conversation, title, multi, reference);
49 // TODO Auto-generated constructor stub
50 }
51
52 /* (non-Javadoc)
53 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#initModel()
54 */
55 /** {@inheritDoc} */
56 @Override
57 protected void initModel() {
58 Control control = getPatternControl();
59 String pattern = null;
60 if (control != null){
61 pattern = ((Text)control).getText();
62 }
63
64
65 model = CdmStore.getService(IReferenceService.class).getUuidAndAbbrevTitleCache(limitOfInitialElements, pattern);
66 }
67
68 /**
69 *
70 * @return
71 */
72 @Override
73 protected ILabelProvider createListLabelProvider() {
74 return new FilteredCdmResourceAbbrevLabelProvider();
75 }
76
77 /**
78 * <p>select</p>
79 *
80 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
81 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
82 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
83 * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
84 */
85
86 public static Reference select(Shell shell, ConversationHolder conversation, Reference reference) {
87 NomenclaturalReferenceSelectionDialog dialog = new NomenclaturalReferenceSelectionDialog(shell, conversation,
88 "Choose a reference", false, reference);
89 return getSelectionFromDialog(dialog);
90 }
91
92 /* (non-Javadoc)
93 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
94 */
95 /** {@inheritDoc} */
96 @Override
97 protected ItemsFilter createFilter() {
98 return new ItemsFilter() {
99
100 /**
101 * Always returns false to enforce refiltering even if the pattern is equal
102 */
103 @Override
104 public boolean equalsFilter(ItemsFilter filter) {
105 return false;
106 }
107
108 @Override
109 public boolean isConsistentItem(Object item) {
110 return false;
111 }
112
113 @Override
114 public boolean matchItem(Object item) {
115 return true;
116 // String text = null;
117 // if(item instanceof UuidAndTitleCache){
118 // if (((UuidAndTitleCache) item).getAbbrevTitleCache() != null){
119 // text = ((UuidAndTitleCache) item).getAbbrevTitleCache();
120 // } else{
121 // text = ((UuidAndTitleCache) item).getTitleCache();
122 // }
123 // }else if(item instanceof String){
124 // text = (String) item;
125 // }
126 // return text != null ? matches(text) : false;
127 }
128
129 };
130 }
131
132
133
134 public class FilteredCdmResourceAbbrevLabelProvider extends LabelProvider {
135 @Override
136 public String getText(Object element) {
137 if (element == null) {
138 return null;
139 }
140 UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
141 String titleCache = uuidAndTitleCache.getTitleCache();
142 String abbrevTitleCache = uuidAndTitleCache.getAbbrevTitleCache();
143 if (abbrevTitleCache == null){
144 abbrevTitleCache = "no abbrev title available";
145 }
146 abbrevTitleCache += " - " + titleCache;
147 if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
148 abbrevTitleCache += " ["+uuidAndTitleCache.getId()+"]";
149 }
150 return abbrevTitleCache;
151 }
152 };
153
154 @Override
155 protected Comparator getItemsComparator() {
156 return new Comparator<UuidAndTitleCache>() {
157 @Override
158 public int compare(UuidAndTitleCache entity1,
159 UuidAndTitleCache entity2) {
160 Collator collator = Collator.getInstance();
161 String compareString1 = null;
162 if (entity1.getAbbrevTitleCache() == null){
163 compareString1 = entity1.getTitleCache();
164 }else{
165 compareString1 = entity1.getAbbrevTitleCache();
166 }
167 String compareString2 = null;
168 if (entity2.getAbbrevTitleCache() == null){
169 compareString2 = entity2.getTitleCache();
170 }else{
171 compareString2 = entity2.getAbbrevTitleCache();
172 }
173 return collator.compare(compareString1, compareString2);
174 }
175 };
176 }
177
178
179
180 }