ref #5828 abbrevTitle and titlecache are displayed in nomenclatural selectionDialogs
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / NomenclaturalAuthorSelectionDialog.java
1 // $Id$
2 /**
3 * Copyright (C) 2016 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.ui.dialog.selection;
11
12
13
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.core.runtime.ListenerList;
16 import org.eclipse.jface.viewers.IColorProvider;
17 import org.eclipse.jface.viewers.IFontProvider;
18 import org.eclipse.jface.viewers.ILabelDecorator;
19 import org.eclipse.jface.viewers.ILabelProvider;
20 import org.eclipse.jface.viewers.ILabelProviderListener;
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
23 import org.eclipse.jface.viewers.StyledCellLabelProvider;
24 import org.eclipse.jface.viewers.StyledString;
25 import org.eclipse.jface.viewers.ViewerCell;
26 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.Color;
29 import org.eclipse.swt.graphics.Font;
30 import org.eclipse.swt.graphics.GC;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.graphics.Rectangle;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
38 import org.eclipse.ui.internal.WorkbenchImages;
39
40 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
41 import eu.etaxonomy.cdm.api.service.IAgentService;
42 import eu.etaxonomy.cdm.model.agent.AgentBase;
43 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
44 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
45 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
46 import eu.etaxonomy.taxeditor.newWizard.NewPersonWizard;
47 import eu.etaxonomy.taxeditor.newWizard.NewTeamWizard;
48 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
49 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
50 import eu.etaxonomy.taxeditor.store.CdmStore;
51 import eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog.FilteredCdmResourceLabelProvider;
52 import eu.etaxonomy.taxeditor.ui.dialog.selection.CdmFilteredItemsSelectionDialog.ItemsListSeparator;
53
54 /**
55 * @author k.luther
56 * @date 25.05.2016
57 *
58 */
59 public class NomenclaturalAuthorSelectionDialog extends AgentSelectionDialog {
60
61
62 /**
63 * @param shell
64 * @param conversation
65 * @param title
66 * @param multi
67 * @param settings
68 * @param agent
69 */
70 protected NomenclaturalAuthorSelectionDialog(Shell shell, ConversationHolder conversation, String title,
71 boolean multi, String settings, AgentBase agent, boolean teamMemberSelection) {
72 super(shell, conversation, title, multi, settings, agent, teamMemberSelection);
73 // TODO Auto-generated constructor stub
74 }
75
76 @Override
77 protected void initModel() {
78 Control control = getPatternControl();
79 String pattern = null;
80 if (control != null){
81 pattern = ((Text)control).getText();
82 }
83
84 model = CdmStore.getService(IAgentService.class).getUuidAndAbbrevTitleCache(limitOfInitialElements, pattern, null);
85 }
86
87 /** {@inheritDoc} */
88 @Override
89 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
90 if(TEAM.equals(parameter)){
91 return new NewTeamWizard(true);
92 }
93 else if(PERSON.equals(parameter)){
94 return new NewPersonWizard();
95 }
96 else{
97 throw new IllegalArgumentException("Could not determine the desired wizard.");
98 }
99 }
100 /**
101 *
102 * @return
103 */
104 @Override
105 protected ILabelProvider createListLabelProvider() {
106 return new FilteredCdmResourceAbbrevLabelProvider();
107 }
108
109 /**
110 * <p>select</p>
111 *
112 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
113 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
114 * @param entity a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
115 * @return a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
116 */
117 public static AgentBase select(Shell shell, ConversationHolder conversation, AgentBase entity, boolean teamMemberSelection) {
118 NomenclaturalAuthorSelectionDialog dialog = new NomenclaturalAuthorSelectionDialog(shell, conversation,
119 "Choose Agent", false, NomenclaturalAuthorSelectionDialog.class.getCanonicalName(), entity, teamMemberSelection);
120 return getSelectionFromDialog(dialog);
121 }
122
123 /**
124 * <p>getTitle</p>
125 *
126 * @param cdmObject a T object.
127 * @return a {@link java.lang.String} object.
128 */
129 @Override
130 protected String getTitle(AgentBase cdmObject) {
131 if(cdmObject == null){
132 return "";
133 }
134
135 if (cdmObject instanceof TeamOrPersonBase) {
136 return ((TeamOrPersonBase) cdmObject).getNomenclaturalTitle();
137 } else if (cdmObject instanceof AgentBase){
138 return ((TeamOrPersonBase) cdmObject).getTitleCache();
139 }
140
141 throw new IllegalArgumentException("Generic method only" +
142 " supports cdmObject of type IIdentifiableEntity." +
143 " Please implement specific method in subclass.");
144 }
145
146
147
148 public class FilteredCdmResourceAbbrevLabelProvider extends LabelProvider {
149 @Override
150 public String getText(Object element) {
151 if (element == null) {
152 return null;
153 }
154 UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
155 String titleCache = uuidAndTitleCache.getTitleCache();
156 String abbrevTitleCache = uuidAndTitleCache.getAbbrevTitleCache();
157 abbrevTitleCache += " - " + titleCache;
158 if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
159 abbrevTitleCache += " ["+uuidAndTitleCache.getId()+"]";
160 }
161 return abbrevTitleCache;
162 }
163 };
164
165 }