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