Project

General

Profile

Download (7.04 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.service.IAgentService;
23
import eu.etaxonomy.cdm.model.agent.AgentBase;
24
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
25
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
26
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
27
import eu.etaxonomy.taxeditor.newWizard.NewPersonWizard;
28
import eu.etaxonomy.taxeditor.newWizard.NewTeamWizard;
29
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31
import eu.etaxonomy.taxeditor.store.CdmStore;
32

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

    
40

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

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

    
70
    }
71

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

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

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

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

    
127
        throw new IllegalArgumentException("Generic method only" +
128
                " supports cdmObject of type IIdentifiableEntity." +
129
                " Please implement specific method in subclass.");
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 (!titleCache.equals(abbrevTitleCache)){
144
				abbrevTitleCache += " - " + titleCache;
145
			}
146
			if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
147
				abbrevTitleCache += " ["+uuidAndTitleCache.getId()+"]";
148
			}
149
            return abbrevTitleCache;
150
		}
151
	};
152

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

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

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

    
212

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

    
221

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

    
231

    
232
}
(21-21/39)