Property sheet changes cause editor to refresh per #527
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / propertysheet / EditorPropertySheetEntry.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 package eu.etaxonomy.taxeditor.propertysheet;
10
11 import org.apache.log4j.Logger;
12 import org.eclipse.ui.views.properties.PropertySheetEntry;
13
14 import eu.etaxonomy.cdm.model.taxon.Taxon;
15 import eu.etaxonomy.taxeditor.controller.EditorController;
16
17 /**
18 * Redraws editor / freetext area when a value in the property sheet changes.
19 * <p>
20 * Also includes a lovely little hack to enable custom sorting:
21 * <p>
22 * GetDisplayName is called by two methods from the PropertySheetViewer, which
23 * <ul>
24 * <li>we are not allowed to set, and</li>
25 * <li>uses a comparator which sorts alphabetically.</li>
26 * </ul>
27 * So we set display names in the PropertySourceDescriptors as "01:Uninomial",
28 * "02:Specific Epithet"; if we ascertain from the stack trace that a "compare"
29 * method is asking for the display name, we return the uncut version for
30 * sorting; when any other method requests a display name, we chop off the ":"
31 * and everything before it.
32 * </p>
33 * @author p.ciardelli
34 * @created 16.05.2008
35 * @version 1.0
36 */
37 public class EditorPropertySheetEntry extends PropertySheetEntry {
38 private static final Logger logger = Logger
39 .getLogger(EditorPropertySheetEntry.class);
40
41 private Taxon taxon;
42
43 protected PropertySheetEntry createChildEntry() {
44 return new EditorPropertySheetEntry(taxon);
45 }
46
47 public EditorPropertySheetEntry(Taxon taxon) {
48 super();
49
50 this.taxon = taxon;
51 }
52
53 /* (non-Javadoc)
54 * @see org.eclipse.ui.views.properties.PropertySheetEntry#getDisplayName()
55 */
56 public String getDisplayName() {
57
58 String displayName = super.getDisplayName();
59
60 Thread currentThread = Thread.currentThread();
61
62 // currentThread.getStackTrace()[0] = call to getStackTrace
63 // currentThread.getStackTrace()[1] = getDisplayName
64 // currentThread.getStackTrace()[2] = who is calling getDisplayName
65 StackTraceElement stackRequestingName = currentThread.getStackTrace()[2];
66
67 if (stackRequestingName.toString().lastIndexOf("compare") != -1) {
68 return displayName;
69 }
70
71 return truncateDisplayName(displayName);
72 }
73
74 /**
75 * Display names take the form "01:xxxx" to allow sorting. This method
76 * truncates everything before the colon, and is marked <i>public</i>
77 * to allow consistent truncation of property sheet id's.
78 *
79 * @param displayName
80 * @return
81 */
82 public static String truncateDisplayName(String displayName) {
83 int colon = displayName.lastIndexOf(':');
84 if (colon != -1) {
85 displayName = displayName.substring(colon + 1);
86 }
87 return displayName;
88 }
89
90 protected void valueChanged(PropertySheetEntry child) {
91
92 super.valueChanged(child);
93
94 // If this is the top level of the property sheet, redraw the freetext area
95 if (getParent() == null) {
96 EditorController.redraw(taxon);
97 }
98 }
99
100 }