Project

General

Profile

Download (6.22 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.ui.dialog.selection;
2

    
3
import java.text.Collator;
4
import java.util.Comparator;
5

    
6
import org.eclipse.jface.viewers.ILabelProvider;
7
import org.eclipse.jface.viewers.LabelProvider;
8
import org.eclipse.swt.widgets.Control;
9
import org.eclipse.swt.widgets.Shell;
10
import org.eclipse.swt.widgets.Text;
11

    
12
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
13
import eu.etaxonomy.cdm.api.service.IReferenceService;
14
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
15
import eu.etaxonomy.cdm.model.reference.Reference;
16
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
17
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
18
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
19
import eu.etaxonomy.taxeditor.store.CdmStore;
20

    
21
public class NomenclaturalReferenceSelectionDialog extends
22
		ReferenceSelectionDialog {
23

    
24
    private TeamOrPersonBase author;
25
    private boolean firstCall = true;
26

    
27
	protected NomenclaturalReferenceSelectionDialog(Shell shell,
28
			ConversationHolder conversation, String title, boolean multi,
29
			Reference reference, boolean inReference, TeamOrPersonBase author) {
30
		super(shell, conversation, title, multi, reference);
31
		this.author = author;
32
		firstCall = true;
33

    
34
	}
35

    
36
	/* (non-Javadoc)
37
	 * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#search
38
	 */
39
	/** {@inheritDoc} */
40
	@Override
41
	protected void search() {
42
	    Control control =getSearchField();
43
        String pattern = null;
44
        if (control != null){
45
            pattern = ((Text)control).getText();
46

    
47
        }
48
        if (firstCall && author != null){
49
            pattern = author.getNomenclaturalTitle();
50
            if (control!= null){
51
                firstCall = false;
52
                ((Text)control).setText(pattern);
53
            }
54

    
55
        }
56

    
57
        if (pattern == null || pattern.equals("?")){
58
            model = CdmStore.getService(IReferenceService.class).getUuidAndAbbrevTitleCache(null, null);
59
        }else{
60
            model = CdmStore.getService(IReferenceService.class).getUuidAndAbbrevTitleCache(limitOfInitialElements, pattern);
61
        }
62
	}
63

    
64
	/**
65
	 *
66
	 * @return
67
	 */
68
   @Override
69
	protected ILabelProvider createListLabelProvider() {
70
		return new FilteredCdmResourceAbbrevLabelProvider();
71
	}
72

    
73
	/**
74
	 * <p>select</p>
75
	 *
76
	 * @param reference a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
77
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
78
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
79
	 * @return a {@link eu.etaxonomy.cdm.model.reference.ReferenceBase} object.
80
	 */
81

    
82
	public static Reference select(Shell shell, ConversationHolder conversation, Reference reference, TeamOrPersonBase author) {
83
		NomenclaturalReferenceSelectionDialog dialog = new NomenclaturalReferenceSelectionDialog(shell, conversation,
84
				"Choose a reference", isInReference, reference, false, author);
85
		return getSelectionFromDialog(dialog);
86
	}
87

    
88
	/* (non-Javadoc)
89
	 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
90
	 */
91
	/** {@inheritDoc} */
92
//	@Override
93
//	protected ItemsFilter createFilter() {
94
//		return new ItemsFilter() {
95
//
96
//			/**
97
//			 * Always returns false to enforce refiltering even if the pattern is equal
98
//			 */
99
//			@Override
100
//			public boolean equalsFilter(ItemsFilter filter) {
101
//				return false;
102
//			}
103
//
104
//			@Override
105
//			public boolean isConsistentItem(Object item) {
106
//				return false;
107
//			}
108
//
109
//			@Override
110
//			public boolean matchItem(Object item) {
111
//				return true;
112
////				String text = null;
113
////				if(item instanceof UuidAndTitleCache){
114
////					if (((UuidAndTitleCache) item).getAbbrevTitleCache() != null){
115
////						text = ((UuidAndTitleCache) item).getAbbrevTitleCache();
116
////					} else{
117
////						text = ((UuidAndTitleCache) item).getTitleCache();
118
////					}
119
////				}else if(item instanceof String){
120
////					text = (String) item;
121
////				}
122
////				return text != null ? matches(text) : false;
123
//			}
124
//
125
//		};
126
//	}
127

    
128

    
129

    
130
	public class FilteredCdmResourceAbbrevLabelProvider extends LabelProvider {
131
		@Override
132
		public String getText(Object element) {
133
			if (element == null) {
134
				return null;
135
			}
136
			UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
137
			String titleCache = uuidAndTitleCache.getTitleCache();
138
			String abbrevTitleCache = uuidAndTitleCache.getAbbrevTitleCache();
139
			if (!abbrevTitleCache.equals(titleCache)){
140
				abbrevTitleCache += " - " + titleCache;
141
			}
142
			if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
143
				abbrevTitleCache += " ["+uuidAndTitleCache.getId()+"]";
144
			}
145
            return abbrevTitleCache;
146
		}
147
	};
148

    
149
	@Override
150
    protected Comparator getItemsComparator() {
151
        return new Comparator<UuidAndTitleCache>() {
152
            @Override
153
            public int compare(UuidAndTitleCache entity1,
154
                    UuidAndTitleCache entity2) {
155

    
156
                if (entity1.getUuid().equals(entity2.getUuid())){
157
                    return 0;
158
                }
159
                Collator collator = Collator.getInstance();
160
                String compareString1 = "";
161
                if (entity1.getAbbrevTitleCache() != null){
162
                    compareString1 = entity1.getAbbrevTitleCache();
163
                }
164
                if (entity1.getTitleCache() != null){
165
                    compareString1 += entity1.getTitleCache();
166
                }
167

    
168

    
169
                String compareString2 = "";
170
                if (entity2.getAbbrevTitleCache() != null){
171
                    compareString2 = entity2.getAbbrevTitleCache();
172
                }
173
                if (entity2.getTitleCache() != null){
174
                    compareString2 += entity2.getTitleCache();
175
                }
176

    
177

    
178
                int result =collator.compare(compareString1, compareString2);
179
                if (result == 0){
180
                    result = entity1.getUuid().compareTo(entity2.getUuid());
181
                }
182
                return result;
183
            }
184
        };
185
    }
186
	@Override
187
	protected String getTitle(Reference cdmObject) {
188
        if(cdmObject == null){
189
            return "";
190
        }
191
        if (cdmObject.getAbbrevTitle() != null){
192
            return cdmObject.getAbbrevTitleCache();
193
        }else{
194
            return cdmObject.getTitleCache();
195
        }
196

    
197

    
198

    
199
    }
200

    
201

    
202

    
203

    
204
}
(23-23/38)