Merge branch 'hotfix/3.8.1'
[taxeditor.git] / eu.etaxonomy.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.hibernate.HibernateProxyHelper;
24 import eu.etaxonomy.cdm.model.agent.AgentBase;
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.common.IAnnotatableEntity;
27 import eu.etaxonomy.cdm.model.common.ICdmBase;
28 import eu.etaxonomy.cdm.model.common.Marker;
29 import eu.etaxonomy.cdm.model.common.MarkerType;
30 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31 import eu.etaxonomy.cdm.model.reference.Reference;
32 import eu.etaxonomy.taxeditor.annotatedlineeditor.EntityListener;
33 import eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy;
34 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
35 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
36
37 /**
38 * <p>BulkEditorLineDisplay class.</p>
39 *
40 * @author p.ciardelli
41 * @created 07.07.2009
42 * @version 1.0
43 */
44 public class BulkEditorLineDisplay implements ILineDisplayStrategy {
45
46 private BulkEditor editor;
47
48 /**
49 * <p>Constructor for BulkEditorLineDisplay.</p>
50 *
51 * @param editor a {@link org.eclipse.ui.IEditorPart} object.
52 */
53 public BulkEditorLineDisplay(BulkEditor editor) {
54 this.editor = editor;
55 }
56
57 /* (non-Javadoc)
58 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#getIcon(java.lang.Object)
59 */
60 /** {@inheritDoc} */
61 @Override
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 @Override
71 public int getIndent(Object entity) {
72 return 0;
73 }
74
75 /* (non-Javadoc)
76 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#getText(java.lang.Object)
77 */
78 /** {@inheritDoc} */
79 @Override
80 public String getText(Object entity) {
81 return editor.getEditorInput().getText((ICdmBase) entity);
82 }
83
84
85 /* (non-Javadoc)
86 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy#getSupplementalText(java.lang.Object)
87 */
88 /** {@inheritDoc} */
89 @Override
90 public String getSupplementalText(Object entity) {
91
92 String supplementalText = "";
93
94 AbstractBulkEditorInput input = editor.getEditorInput();
95 String typeText = input.getTypeText(entity);
96
97 supplementalText += StringUtils.isBlank(typeText) ? "" : String.format(" [%s]", typeText);
98
99 if (entity instanceof IAnnotatableEntity) {
100 IAnnotatableEntity annotatableEntity = (IAnnotatableEntity) HibernateProxyHelper.deproxy(entity);
101
102 Map<MarkerType, Boolean> editMarkerTypePreferences = PreferencesUtil.getEditMarkerTypePreferences();
103
104 String markerText = "";
105 List<String> markers = new ArrayList<String>();
106 for (Marker marker : annotatableEntity.getMarkers()) {
107 String markerLabel = marker.getMarkerType() == null? " unknown marker " : marker.getMarkerType().getLabel();
108 markers.add(String.format("%1s = %2s", markerLabel, marker.getFlag() ? "yes" : "no"));
109 }
110 if (! markers.isEmpty()) {
111 markerText = StringUtils.join(markers, ", ");
112 }
113
114 supplementalText += StringUtils.isBlank(markerText) ? "" : String.format(" [%s]", markerText);
115 }
116
117 return supplementalText;
118 }
119
120 /* (non-Javadoc)
121 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#setText(java.lang.String, java.lang.Object)
122 */
123 /** {@inheritDoc} */
124 @Override
125 public void setText(String text, Object entity) {
126 if (entity instanceof Reference) {
127 ((Reference) entity).setTitleCache(text);
128 }
129 }
130
131 /* (non-Javadoc)
132 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#getHoverText(java.lang.Object)
133 */
134 /** {@inheritDoc} */
135 @Override
136 public String getHoverText(Object entity) {
137 return null;
138 }
139
140 /* (non-Javadoc)
141 * @see eu.etaxonomy.taxeditor.bulkeditor.ILineDisplayStrategy#setStatusMessage(java.lang.String, java.lang.Object)
142 */
143 /** {@inheritDoc} */
144 @Override
145 public void setStatusMessage(final String text, Object entity) {
146
147 // Calling from the synchronized method ListEditorReconcilingStrategy#doReconcile
148 // without its own thread causes an invalid thread access exception
149 // see http://wiki.eclipse.org/FAQ_Why_do_I_get_an_invalid_thread_access_exception%3F
150 new Thread(new Runnable() {
151 @Override
152 public void run() {
153 Display.getDefault().asyncExec(new Runnable() {
154 @Override
155 public void run() {
156 IStatusLineManager statusLineManager = editor.getEditorSite().getActionBars().getStatusLineManager();
157 statusLineManager.setMessage(text);
158 }
159 });
160 }
161 }).start();
162 }
163
164 /* (non-Javadoc)
165 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy#getEmptyText(java.lang.Object)
166 */
167 /** {@inheritDoc} */
168 @Override
169 public String getEmptyCacheMessage(Object entity) {
170 if (entity instanceof Reference) {
171 return "No reference title cache";
172 }
173 if (entity instanceof TaxonNameBase) {
174 return "No name title cache";
175 }
176 if (entity instanceof AgentBase) {
177 return "No title cache";
178 }
179 return "";
180 }
181
182 /* (non-Javadoc)
183 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy#isEntityCacheEmpty(java.lang.Object)
184 */
185 /** {@inheritDoc} */
186 @Override
187 public boolean isEntityCacheEmpty(Object entity) {
188 if (entity instanceof Reference) {
189 String text = ((Reference) entity).getTitleCache();
190 return (text == null || text.equals(""));
191 }
192 if (entity instanceof TaxonNameBase) {
193 String text = ((TaxonNameBase) entity).getTitleCache();
194 return (text == null || text.equals(""));
195 }
196 if (entity instanceof AgentBase) {
197 String text = ((AgentBase) entity).getTitleCache();
198 return (text == null || text.equals(""));
199 }
200 return false;
201 }
202
203 /* (non-Javadoc)
204 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy#addDisplayListener(java.lang.Object)
205 */
206 /** {@inheritDoc} */
207 @Override
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 @Override
229 public Font getFont(Object entity) {
230 return null;
231 }
232 }