\r
import org.apache.log4j.Logger;\r
import org.eclipse.core.runtime.Assert;\r
+import org.eclipse.jface.dialogs.Dialog;\r
import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.window.DefaultToolTip;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.custom.StyledText;\r
import org.eclipse.swt.dnd.DND;\r
import org.eclipse.swt.dnd.DragSourceEvent;\r
import org.eclipse.swt.dnd.DragSourceListener;\r
import org.eclipse.swt.dnd.Transfer;\r
+import org.eclipse.swt.events.ControlAdapter;\r
+import org.eclipse.swt.events.ControlEvent;\r
+import org.eclipse.swt.events.ControlListener;\r
import org.eclipse.swt.events.FocusAdapter;\r
import org.eclipse.swt.events.FocusEvent;\r
import org.eclipse.swt.events.FocusListener;\r
import org.eclipse.swt.graphics.Color;\r
import org.eclipse.swt.graphics.Font;\r
+import org.eclipse.swt.graphics.GC;\r
import org.eclipse.swt.graphics.Image;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.swt.widgets.Control;\r
\r
protected NameViewer textViewer;\r
protected IManagedForm managedForm;\r
- private Label nonEditableInfo;\r
+ private Label nonEditableInfoLabel;\r
+ private DefaultToolTip nonEditableInfoHover;\r
\r
protected Taxon taxon;\r
\r
*/\r
public GroupedComposite(Composite parent, IManagedForm managedForm) {\r
super(parent, SWT.NONE);\r
- \r
- createContent();\r
- \r
+ \r
this.managedForm = managedForm;\r
\r
Object formData = managedForm.getForm().getBody().getData();\r
Assert.isTrue(formData instanceof Taxon, \r
"Managed form must have a Taxon in its data field.");\r
taxon = (Taxon) formData;\r
- }\r
\r
- protected void createLineWrapSupport() {\r
- if (textViewer instanceof NameViewer) {\r
- new LineWrapSupport(textViewer, managedForm);\r
- } else {\r
- logger.warn("Can't create line wrap support because textViewer has not been initialized.");\r
- }\r
+ createControl();\r
}\r
\r
- protected void createContent() {\r
+ protected void createControl() {\r
setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));\r
TableWrapLayout layout = new TableWrapLayout();\r
layout.leftMargin = 0;\r
getColor(ITaxEditorConstants.GROUP_GRAY_BKG_COLOR);\r
setBackground(groupBackgroundColor);\r
}\r
- \r
+\r
+ protected void createLineWrapSupport() {\r
+ if (textViewer instanceof NameViewer) {\r
+ new LineWrapSupport(textViewer, managedForm);\r
+ } else {\r
+ logger.warn("Can't create line wrap support because textViewer has not been initialized.");\r
+ }\r
+ }\r
+ \r
public Taxon getTaxon() {\r
return taxon;\r
}\r
.getFont(ITaxEditorConstants.DEFAULT_PROMPT_FONT);\r
setFocusListener(new FocusListener() {\r
\r
- public void focusGained(FocusEvent e) {\r
- logger.warn(document.get()); \r
+ public void focusGained(FocusEvent e) { \r
if (document.get().equals(prompt)) {\r
textControl.setFont(getViewerFont());\r
document.set("");\r
WidgetTransfer.getInstance().setWidget(GroupedComposite.this);\r
}\r
};\r
+\r
+ private String nonEditableText;\r
+ ControlListener nonEditableResizeListener = new ControlAdapter() {\r
+ \r
+ int width = 0;\r
+ \r
+ public void controlResized(ControlEvent e) {\r
+ if (nonEditableInfoLabel.getBounds().width == width) {\r
+ return;\r
+ }\r
+ width = nonEditableInfoLabel.getBounds().width;\r
+ if (nonEditableInfoLabel.getBounds().width > 0) {\r
+ nonEditableInfoLabel.setText(\r
+ Dialog.shortenText(nonEditableText.toUpperCase(), \r
+ nonEditableInfoLabel));\r
+ }\r
+ }\r
+ };\r
+\r
+ private String nonEditableHoverText;\r
+\r
+ private LabelEllipsisListener nonEditableLabelEllipsisListener;\r
\r
/**\r
* nonEditableInfo is a label displayed underneath a GroupedComposite's \r
* @param info\r
*/\r
public void setNonEditableInfo(String info) {\r
- if (nonEditableInfo == null) {\r
- nonEditableInfo = new Label(this, SWT.WRAP);\r
- nonEditableInfo.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP));\r
+ // TODO non editable info should only be drawn once, when everything else is drawn\r
+ info = info.toUpperCase();\r
+ if (nonEditableInfoLabel == null) {\r
+ nonEditableText = info;\r
+ nonEditableHoverText = info;\r
+ \r
+ nonEditableInfoLabel = new Label(this, SWT.NONE);\r
+ nonEditableInfoLabel.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP));\r
+ \r
+ nonEditableLabelEllipsisListener = new LabelEllipsisListener(nonEditableInfoLabel) {\r
+ @Override\r
+ public String getLabelText() {\r
+ return nonEditableText.toUpperCase();\r
+ }\r
+ }; \r
+ nonEditableInfoLabel.addControlListener(nonEditableLabelEllipsisListener);\r
+ \r
+ nonEditableInfoHover = new DefaultToolTip(nonEditableInfoLabel);\r
+ nonEditableInfoHover.setRespectDisplayBounds(true);\r
+ \r
} else {\r
- info = nonEditableInfo.getText() + ", " + info;\r
+ nonEditableText += ", " + info;\r
+ nonEditableHoverText += "\n" + info;\r
+ \r
}\r
- nonEditableInfo.setText(info.toUpperCase());\r
+ nonEditableInfoHover.setText(nonEditableHoverText);\r
}\r
\r
/**\r
textWidget.removeFocusListener(borderDecorator);\r
}\r
}\r
+ \r
+ if (nonEditableInfoLabel != null && nonEditableLabelEllipsisListener != null) {\r
+ nonEditableInfoLabel.removeControlListener(nonEditableLabelEllipsisListener);\r
+ }\r
\r
super.dispose();\r
}\r
+ \r
+ public static final String ELLIPSIS = "...";\r
+ \r
+ /**\r
+ * @param textValue\r
+ * @param control\r
+ * @return\r
+ * @see org.eclipse.jface.dialogs.Dialog#shortenText(String, Control)\r
+ */\r
+ public static String shortenText(String textValue, Control control) {\r
+ if (textValue == null) {\r
+ return null;\r
+ }\r
+ GC gc = new GC(control);\r
+ int maxWidth = control.getBounds().width;\r
+ int maxExtent = gc.textExtent(textValue).x;\r
+ if (maxExtent < maxWidth) {\r
+ gc.dispose();\r
+ return textValue;\r
+ }\r
+ int length = textValue.length();\r
+ int charsToClip = Math.round(0.95f*length * (1 - ((float)maxWidth/maxExtent)));\r
+ \r
+ int end = length - charsToClip;\r
+ while (end > 0) {\r
+ String s1 = textValue.substring(0, end);\r
+ String s = s1 + ELLIPSIS;\r
+ int l = gc.textExtent(s).x;\r
+ if (l < maxWidth) {\r
+ gc.dispose();\r
+ return s;\r
+ }\r
+ end--;\r
+ }\r
+ gc.dispose();\r
+ return textValue;\r
+ \r
+ }\r
+ \r
+ abstract class LabelEllipsisListener extends ControlAdapter {\r
+ \r
+ private Label label;\r
+ int width = 0;\r
+\r
+ LabelEllipsisListener(Label label) {\r
+ this.label = label;\r
+ }\r
+ \r
+ abstract public String getLabelText();\r
+ \r
+ public void controlResized(ControlEvent e) {\r
+ if (label.getBounds().width == width) {\r
+ return;\r
+ }\r
+ width = label.getBounds().width;\r
+ if (label.getBounds().width > 0) {\r
+ label.setText(\r
+// Dialog.shortenText(getLabelText(), label));\r
+ shortenText(getLabelText(), label));\r
+ }\r
+ }\r
+ }\r
}
\ No newline at end of file
\r
import java.beans.PropertyChangeEvent;\r
import java.beans.PropertyChangeListener;\r
+import java.util.Set;\r
\r
import org.apache.log4j.Logger;\r
import org.eclipse.core.commands.operations.IUndoContext;\r
import org.eclipse.ui.views.properties.IPropertySource;\r
\r
import eu.etaxonomy.cdm.model.name.BotanicalName;\r
+import eu.etaxonomy.cdm.model.name.NameRelationship;\r
import eu.etaxonomy.cdm.model.name.NonViralName;\r
import eu.etaxonomy.cdm.model.name.TaxonNameBase;\r
import eu.etaxonomy.cdm.model.name.ZoologicalName;\r
createNameListener(taxonBase.getName());\r
}\r
\r
-// protected String getEmptyTextPrompt() {\r
-// return EMPTY_NAME_PROMPT;\r
-// }\r
\r
+ /**\r
+ * \r
+ */\r
+ private void showNameRelations() {\r
+ TaxonNameBase name = getName();\r
+ if (name == null) {\r
+ return;\r
+ }\r
+ Set<NameRelationship> nameRelations = name.getNameRelations();\r
+ if (nameRelations.size() == 0) {\r
+ return;\r
+ }\r
+ for (NameRelationship nameRelation : nameRelations) {\r
+ String typeLabel = null;\r
+ TaxonNameBase relatedName = null; \r
+ if (name.equals(nameRelation.getFromName())) {\r
+ typeLabel = nameRelation.getType().getInverseLabel();\r
+ relatedName = nameRelation.getToName();\r
+ } else {\r
+ typeLabel = nameRelation.getType().getLabel();\r
+ relatedName = nameRelation.getFromName();\r
+ }\r
+ setNonEditableInfo(typeLabel + " " + relatedName.getTitleCache());\r
+ }\r
+ }\r
+\r
/**\r
* Listens for changes to this name's <code>fullTitleCache</code>.\r
* \r
}\r
\r
protected void initTextViewer(TaxonBase taxonBase) {\r
+ \r
+ showNameRelations();\r
+ \r
String text = CdmUtil.getDisplayNameWithRef(taxonBase);\r
if (text.length() == 0) {\r
initEmptyText();\r