8a8085aeead4311377787132473caf91272f5403
[taxeditor.git] / taxeditor-bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / BulkEditorLineDisplay.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.bulkeditor;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.apache.commons.lang.StringUtils;
18 import org.eclipse.jface.action.IStatusLineManager;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.widgets.Display;
22
23 import eu.etaxonomy.cdm.common.CdmUtils;
24 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25 import eu.etaxonomy.cdm.model.agent.AgentBase;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.common.IAnnotatableEntity;
28 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29 import eu.etaxonomy.cdm.model.common.Marker;
30 import eu.etaxonomy.cdm.model.common.MarkerType;
31 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
32 import eu.etaxonomy.cdm.model.reference.Reference;
33 import eu.etaxonomy.taxeditor.annotatedlineeditor.EntityListener;
34 import eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy;
35 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
36 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37
38 /**
39 * <p>BulkEditorLineDisplay class.</p>
40 *
41 * @author p.ciardelli
42 * @created 07.07.2009
43 * @version 1.0
44 */
45 public class BulkEditorLineDisplay implements ILineDisplayStrategy {
46
47 private BulkEditor editor;
48
49 /**
50 * <p>Constructor for BulkEditorLineDisplay.</p>
51 *
52 * @param editor a {@link org.eclipse.ui.IEditorPart} object.
53 */
54 public BulkEditorLineDisplay(BulkEditor editor) {
55 this.editor = editor;
56 }
57
58 /* (non-Javadoc)
59 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#getIcon(java.lang.Object)
60 */
61 /** {@inheritDoc} */
62 public Image getIcon(Object entity) {
63 return null;
64 }
65
66 /* (non-Javadoc)
67 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#getIndent(java.lang.Object)
68 */
69 /** {@inheritDoc} */
70 public int getIndent(Object entity) {
71 return 0;
72 }
73
74 /* (non-Javadoc)
75 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#getText(java.lang.Object)
76 */
77 /** {@inheritDoc} */
78 public String getText(Object entity) {
79 String text = null;
80
81 if(entity instanceof IdentifiableEntity){
82 IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
83 if(identifiableEntity instanceof TaxonNameBase){
84 // TODO it is not very nice to do this here. Please refactor
85 text = ((TaxonNameBase) identifiableEntity).getFullTitleCache();
86 }else{
87 text = identifiableEntity.getTitleCache();
88 }
89 }
90
91 return text == null ? "" : text;
92 }
93
94
95 /* (non-Javadoc)
96 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy#getSupplementalText(java.lang.Object)
97 */
98 /** {@inheritDoc} */
99 public String getSupplementalText(Object entity) {
100
101 String supplementalText = "";
102
103 AbstractBulkEditorInput input = editor.getEditorInput();
104 String typeText = input.getTypeText(entity);
105
106 supplementalText += CdmUtils.isEmpty(typeText) ? "" : String.format(" [%s]", typeText);
107
108 if (entity instanceof IAnnotatableEntity) {
109 IAnnotatableEntity annotatableEntity = (IAnnotatableEntity) HibernateProxyHelper.deproxy(entity);
110
111 Map<MarkerType, Boolean> editMarkerTypePreferences = PreferencesUtil.getEditMarkerTypePreferences();
112
113 String markerText = "";
114 List<String> markers = new ArrayList<String>();
115 for (Marker marker : annotatableEntity.getMarkers()) {
116 markers.add(String.format("%1s = %2s", marker.getMarkerType().getLabel(), marker.getFlag() ? "yes" : "no"));
117 }
118 if (! markers.isEmpty()) {
119 markerText = StringUtils.join(markers, ", ");
120 }
121
122 supplementalText += CdmUtils.isEmpty(markerText) ? "" : String.format(" [%s]", markerText);
123 }
124
125 return supplementalText;
126 }
127
128 /* (non-Javadoc)
129 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#setText(java.lang.String, java.lang.Object)
130 */
131 /** {@inheritDoc} */
132 public void setText(String text, Object entity) {
133 if (entity instanceof Reference) {
134 ((Reference) entity).setTitleCache(text);
135 }
136 }
137
138 /* (non-Javadoc)
139 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#getHoverText(java.lang.Object)
140 */
141 /** {@inheritDoc} */
142 public String getHoverText(Object entity) {
143 return null;
144 }
145
146 /* (non-Javadoc)
147 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#setStatusMessage(java.lang.String, java.lang.Object)
148 */
149 /** {@inheritDoc} */
150 public void setStatusMessage(final String text, Object entity) {
151
152 // Calling from the synchronized method ListEditorReconcilingStrategy#doReconcile
153 // without its own thread causes an invalid thread access exception
154 // see http://wiki.eclipse.org/FAQ_Why_do_I_get_an_invalid_thread_access_exception%3F
155 new Thread(new Runnable() {
156 public void run() {
157 Display.getDefault().asyncExec(new Runnable() {
158 public void run() {
159 IStatusLineManager statusLineManager = editor.getEditorSite().getActionBars().getStatusLineManager();
160 statusLineManager.setMessage(text);
161 }
162 });
163 }
164 }).start();
165 }
166
167 /* (non-Javadoc)
168 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy#getEmptyText(java.lang.Object)
169 */
170 /** {@inheritDoc} */
171 public String getEmptyCacheMessage(Object entity) {
172 if (entity instanceof Reference) {
173 return "No reference title cache";
174 }
175 if (entity instanceof TaxonNameBase) {
176 return "No name title cache";
177 }
178 if (entity instanceof AgentBase) {
179 return "No title cache";
180 }
181 return "";
182 }
183
184 /* (non-Javadoc)
185 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy#isEntityCacheEmpty(java.lang.Object)
186 */
187 /** {@inheritDoc} */
188 public boolean isEntityCacheEmpty(Object entity) {
189 if (entity instanceof Reference) {
190 String text = ((Reference) entity).getTitleCache();
191 return (text == null || text.equals(""));
192 }
193 if (entity instanceof TaxonNameBase) {
194 String text = ((TaxonNameBase) entity).getTitleCache();
195 return (text == null || text.equals(""));
196 }
197 if (entity instanceof AgentBase) {
198 String text = ((AgentBase) entity).getTitleCache();
199 return (text == null || text.equals(""));
200 }
201 return false;
202 }
203
204 /* (non-Javadoc)
205 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy#addDisplayListener(java.lang.Object)
206 */
207 /** {@inheritDoc} */
208 public void addDisplayListener(Object entity, EntityListener listener) {
209 /* Note: all non-field specific listeners require that corresponding property sheets
210 * contain the following:
211 *
212 *
213 * public void setPropertyValue(Object id, Object value) {
214 * ...
215 * reference.firePropertyChange(new PropertyChangeEvent(reference, "", null, null));
216 * }
217 *
218 */
219 if (entity instanceof CdmBase) {
220 ((CdmBase) entity).addPropertyChangeListener(listener);
221 }
222 }
223
224 /* (non-Javadoc)
225 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy#getFont(java.lang.Object)
226 */
227 /** {@inheritDoc} */
228 public Font getFont(Object entity) {
229 return null;
230 }
231 }