Project

General

Profile

Download (7.05 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2016 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.ui.dialog.selection;
10

    
11

    
12

    
13
import java.text.Collator;
14
import java.util.Comparator;
15

    
16
import org.eclipse.jface.viewers.ILabelProvider;
17
import org.eclipse.jface.viewers.LabelProvider;
18
import org.eclipse.swt.widgets.Control;
19
import org.eclipse.swt.widgets.Shell;
20
import org.eclipse.swt.widgets.Text;
21

    
22
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
23
import eu.etaxonomy.cdm.api.service.IAgentService;
24
import eu.etaxonomy.cdm.model.agent.AgentBase;
25
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
26
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
27
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
28
import eu.etaxonomy.taxeditor.newWizard.NewPersonWizard;
29
import eu.etaxonomy.taxeditor.newWizard.NewTeamWizard;
30
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
31
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
32
import eu.etaxonomy.taxeditor.store.CdmStore;
33

    
34
/**
35
 * @author k.luther
36
 * @date 25.05.2016
37
 *
38
 */
39
public class NomenclaturalAuthorSelectionDialog extends AgentSelectionDialog {
40

    
41

    
42
    /**
43
     * @param shell
44
     * @param conversation
45
     * @param title
46
     * @param multi
47
     * @param settings
48
     * @param agent
49
     */
50
    protected NomenclaturalAuthorSelectionDialog(Shell shell, ConversationHolder conversation, String title,
51
            boolean multi, String settings, AgentBase agent, boolean teamMemberSelection) {
52
        super(shell, conversation, title, multi, settings, agent, teamMemberSelection);
53
        // TODO Auto-generated constructor stub
54
    }
55

    
56
    @Override
57
    protected void search() {
58
        Control control =getSearchField();
59
        String pattern = null;
60
        if (control != null){
61
            pattern = ((Text)control).getText();
62
        }
63
        if (pattern == null || pattern.equals("?")){
64
            model = CdmStore.getService(IAgentService.class).getUuidAndAbbrevTitleCache(null, null, null);
65
        }else{
66
            model = CdmStore.getService(IAgentService.class).getUuidAndAbbrevTitleCache(limitOfInitialElements, pattern, null);
67
        }
68

    
69
    }
70

    
71
    /** {@inheritDoc} */
72
    @Override
73
    protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
74
        if(TEAM.equals(parameter)){
75
            return new NewTeamWizard(true);
76
        }
77
        else if(PERSON.equals(parameter)){
78
            return new NewPersonWizard();
79
        }
80
        else{
81
            throw new IllegalArgumentException("Could not determine the desired wizard.");
82
        }
83
    }
84
    /**
85
	 *
86
	 * @return
87
	 */
88
    @Override
89
	protected ILabelProvider createListLabelProvider() {
90
		return new FilteredCdmResourceAbbrevLabelProvider();
91
	}
92

    
93
    /**
94
     * <p>select</p>
95
     *
96
     * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
97
     * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
98
     * @param entity a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
99
     * @return a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
100
     */
101
    public static AgentBase select(Shell shell, ConversationHolder conversation, AgentBase entity, boolean teamMemberSelection) {
102
        NomenclaturalAuthorSelectionDialog dialog = new NomenclaturalAuthorSelectionDialog(shell, conversation,
103
                "Choose Agent", false, NomenclaturalAuthorSelectionDialog.class.getCanonicalName(), entity, teamMemberSelection);
104
        return getSelectionFromDialog(dialog);
105
    }
106

    
107
    /**
108
     * <p>getTitle</p>
109
     *
110
     * @param cdmObject a T object.
111
     * @return a {@link java.lang.String} object.
112
     */
113
    @Override
114
    protected String getTitle(AgentBase cdmObject) {
115
        if(cdmObject == null){
116
            return "";
117
        }
118

    
119
        if (cdmObject instanceof TeamOrPersonBase) {
120
            return ((TeamOrPersonBase) cdmObject).getNomenclaturalTitle();
121
        } else if (cdmObject instanceof AgentBase){
122
            return ((TeamOrPersonBase) cdmObject).getTitleCache();
123
        }
124

    
125
        throw new IllegalArgumentException("Generic method only" +
126
                " supports cdmObject of type IIdentifiableEntity." +
127
                " Please implement specific method in subclass.");
128
    }
129

    
130

    
131

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

    
151
	/** {@inheritDoc} */
152
//	@Override
153
//	protected ItemsFilter createFilter() {
154
//		return new ItemsFilter() {
155
//
156
//			/**
157
//			 * Always returns false to enforce refiltering even if the pattern is equal
158
//			 */
159
//			@Override
160
//			public boolean equalsFilter(ItemsFilter filter) {
161
//				return false;
162
//			}
163
//
164
//			@Override
165
//			public boolean isConsistentItem(Object item) {
166
//				return false;
167
//			}
168
//
169
//			@Override
170
//			public boolean matchItem(Object item) {
171
//				String textTitleCache = null;
172
//				String abbrevTitleCache = null;
173
//				if(item instanceof UuidAndTitleCache){
174
//					if (((UuidAndTitleCache) item).getAbbrevTitleCache() != null){
175
//						abbrevTitleCache = ((UuidAndTitleCache) item).getAbbrevTitleCache();
176
//					}
177
//					if (((UuidAndTitleCache) item).getTitleCache() != null ){
178
//						textTitleCache = ((UuidAndTitleCache) item).getTitleCache();
179
//					}
180
//				}else if(item instanceof String){
181
//					textTitleCache = (String) item;
182
//				}
183
//
184
//				return  (matches(textTitleCache) || matches(abbrevTitleCache)) ;
185
//
186
//			}
187
//
188
//		};
189
//	}
190

    
191
	@Override
192
	protected Comparator getItemsComparator() {
193
		return new Comparator<UuidAndTitleCache>() {
194
			@Override
195
			public int compare(UuidAndTitleCache entity1,
196
					UuidAndTitleCache entity2) {
197

    
198
			    if (entity1.getUuid().equals(entity2.getUuid())){
199
			        return 0;
200
			    }
201
				Collator collator = Collator.getInstance();
202
				String compareString1 = "";
203
				if (entity1.getAbbrevTitleCache() != null){
204
					compareString1 = entity1.getAbbrevTitleCache();
205
				}
206
				if (entity1.getTitleCache() != null){
207
					compareString1 += entity1.getTitleCache();
208
				}
209

    
210

    
211
				String compareString2 = "";
212
				if (entity2.getAbbrevTitleCache() != null){
213
					compareString2 = entity2.getAbbrevTitleCache();
214
				}
215
				if (entity2.getTitleCache() != null){
216
					compareString2 += entity2.getTitleCache();
217
				}
218

    
219

    
220
				int result =collator.compare(compareString1, compareString2);
221
				if (result == 0){
222
				    result = entity1.getUuid().compareTo(entity2.getUuid());
223
				}
224
				return result;
225
			}
226
		};
227
	}
228

    
229

    
230
}
(20-20/38)