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