1 package eu
.etaxonomy
.taxeditor
.prototype2
.view
.nameviewersupport
;
3 import org
.eclipse
.core
.runtime
.Assert
;
4 import org
.eclipse
.jface
.text
.Position
;
5 import org
.eclipse
.swt
.SWT
;
6 import org
.eclipse
.swt
.custom
.StyledText
;
7 import org
.eclipse
.swt
.dnd
.DND
;
8 import org
.eclipse
.swt
.dnd
.DragSource
;
9 import org
.eclipse
.swt
.dnd
.DragSourceEvent
;
10 import org
.eclipse
.swt
.dnd
.DragSourceListener
;
11 import org
.eclipse
.swt
.dnd
.Transfer
;
12 import org
.eclipse
.swt
.events
.FocusAdapter
;
13 import org
.eclipse
.swt
.events
.FocusEvent
;
14 import org
.eclipse
.swt
.events
.ModifyEvent
;
15 import org
.eclipse
.swt
.events
.ModifyListener
;
16 import org
.eclipse
.swt
.events
.MouseEvent
;
17 import org
.eclipse
.swt
.events
.MouseListener
;
18 import org
.eclipse
.swt
.events
.SelectionEvent
;
19 import org
.eclipse
.swt
.events
.SelectionListener
;
20 import org
.eclipse
.swt
.layout
.GridData
;
21 import org
.eclipse
.swt
.layout
.GridLayout
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
23 import org
.eclipse
.swt
.widgets
.Control
;
24 import org
.eclipse
.swt
.widgets
.Display
;
25 import org
.eclipse
.swt
.widgets
.Label
;
26 import org
.eclipse
.swt
.widgets
.Menu
;
27 import org
.eclipse
.swt
.widgets
.MenuItem
;
29 import com
.swtdesigner
.SWTResourceManager
;
31 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.taxeditor
.prototype2
.controller
.ActionPopulatePropertySheet
;
36 import eu
.etaxonomy
.taxeditor
.prototype2
.controller
.TaxonTransfer
;
37 import eu
.etaxonomy
.taxeditor
.prototype2
.view
.NameEditorView
;
38 import eu
.etaxonomy
.taxeditor
.prototype2
.view
.NameViewer
;
40 public class NameComposite
extends Composite
{
42 public static final int ACCEPTED
= 0;
43 public static final int HOMOTYPIC_SYNONYM
= 1;
44 public static final int HETEROTYPIC_SYNONYM
= 2;
50 NameViewer nameViewer
;
51 boolean initialized
= false;
54 * Temporary procedure until ability to get accepted taxon
55 * from synonym is implemented
61 public NameComposite(Composite parent
, Synonym synonym
, Taxon taxon
) {
62 super(parent
, SWT
.NONE
);
65 this.synonym
= synonym
;
67 // Either initialize group composite,
68 // or assume its homotypic group
69 if (parent
instanceof GroupComposite
) {
71 if (((GroupComposite
) parent
).isAcceptedGroup())
72 this.compositeType
= HOMOTYPIC_SYNONYM
;
74 this.compositeType
= HETEROTYPIC_SYNONYM
;
76 if (((GroupComposite
) parent
).getHomotypicalGroup() == null)
77 ((GroupComposite
) parent
).setHomotypicalGroup(
78 synonym
.getName().getHomotypicalGroup());
80 synonym
.getName().setHomotypicalGroup(
81 ((GroupComposite
) parent
).getHomotypicalGroup());
84 createNameComposite();
87 public NameComposite(Composite parent
, Synonym synonym
) {
88 super(parent
, SWT
.NONE
);
90 this.synonym
= synonym
;
91 // this.taxon = (Taxon) synonym.getAcceptedTaxa().toArray()[0];
93 // Either initialize group composite,
94 // or assume its homotypic group
95 if (parent
instanceof GroupComposite
) {
97 if (((GroupComposite
) parent
).isAcceptedGroup())
98 this.compositeType
= HOMOTYPIC_SYNONYM
;
100 this.compositeType
= HETEROTYPIC_SYNONYM
;
102 if (((GroupComposite
) parent
).getHomotypicalGroup() == null)
103 ((GroupComposite
) parent
).setHomotypicalGroup(
104 synonym
.getName().getHomotypicalGroup());
106 synonym
.getName().setHomotypicalGroup(
107 ((GroupComposite
) parent
).getHomotypicalGroup());
110 createNameComposite();
113 public NameComposite(Composite parent
, Taxon taxon
) {
114 super(parent
, SWT
.NONE
);
117 this.compositeType
= ACCEPTED
;
119 // Initialize homotypic group component
120 if (parent
instanceof GroupComposite
) {
122 ((GroupComposite
) parent
).setHomotypicalGroup(
123 taxon
.getName().getHomotypicalGroup());
124 ((GroupComposite
) parent
).setAcceptedGroup(true);
127 createNameComposite();
133 public void createNameComposite() {
135 Assert
.isNotNull(taxon
);
137 // Graphic layout of composite
138 GridLayout gridLayout
= new GridLayout();
139 gridLayout
.verticalSpacing
= 0;
140 gridLayout
.marginWidth
= 5;
141 gridLayout
.marginHeight
= 5;
142 gridLayout
.horizontalSpacing
= 5;
143 gridLayout
.numColumns
= (compositeType
== ACCEPTED?
1: 2);
144 this.setLayout(gridLayout
);
145 this.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
146 this.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
147 if (compositeType
!= ACCEPTED
) {
148 // relationLabel.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/heterosyn_no_bg.gif"));
149 relationLabel
= new Label(this, SWT
.NONE
);
150 relationLabel
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.TOP
, false, false));
151 relationLabel
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
153 if (this.compositeType
== HOMOTYPIC_SYNONYM
)
154 relationLabel
.setImage(SWTResourceManager
.getImage(NameComposite
.class, "homosyn_no_bg.gif"));
155 // relationLabel.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/homosyn_no_bg.gif"));
157 relationLabel
.setImage(SWTResourceManager
.getImage(NameComposite
.class, "heterosyn_no_bg.gif"));
158 // relationLabel.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/heterosyn_no_bg.gif"));
160 nameViewer
= new NameViewer(this);
161 nameViewer
.getTextWidget().setLayoutData(
162 new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
163 nameViewer
.getTextWidget().setFont(
164 SWTResourceManager
.getFont("Georgia",
165 (compositeType
== ACCEPTED?
12 : 10),
168 // Add verify listener to check for line breaks
169 nameViewer
.getTextWidget().addVerifyListener(
170 new NameViewerVerifyListener(taxon
));
172 // Listen for changes to name
173 nameViewer
.getTextWidget().addModifyListener(new ModifyListener () {
174 public void modifyText(ModifyEvent e
) {
176 String text
= ((StyledText
) e
.widget
).getText();
177 setName(BotanicalName
.PARSED_NAME(text
));
182 // Listen for focus to send this name to the property sheet
183 nameViewer
.getTextWidget().addFocusListener(new FocusAdapter () {
184 public void focusGained(FocusEvent e
) {
186 if (nameEditorView
!= null)
187 nameEditorView
.setPropertySheetNodes(getName());
191 // Listen for user selecting text
192 nameViewer
.getTextWidget().addSelectionListener(new SelectionListener() {
194 public void widgetDefaultSelected(SelectionEvent e
) {
195 // TODO Auto-generated method stub
199 public void widgetSelected(SelectionEvent e
) {
200 // e.x = start, y = e.finish - strange but true
201 // nameViewer.getAnnotationModel().addAnnotation(
202 // new WarningAnnotation(0, "Could not parse name."),
203 // new Position(e.x, e.y - e.x));
209 // Put name into name viewer, show error if parsed incorrectly
210 nameViewer
.getTextWidget().setText(getName().getTitleCache() == null ?
"" : getName().getTitleCache());
211 nameViewer
.setShowError(getName().getHasProblem());
213 // Put cursor at end of name viewer
214 nameViewer
.getTextWidget().setCaretOffset(
215 nameViewer
.getTextWidget().getText().length());
217 // Set composite to initialized - any new input will now be parsed
220 // Add drag functionality to synonyms only
221 if (compositeType
== ACCEPTED
) return;
226 // **************** DRAG FUNCTIONALITY **************** //
227 Transfer
[] types
= new Transfer
[] {TaxonTransfer
.getInstance()};
228 int operations
= DND
.DROP_MOVE
| DND
.DROP_COPY
| DND
.DROP_LINK
;
230 // Make composite draggable
231 DragSource dragComposite
= new DragSource (this, operations
);
232 dragComposite
.setTransfer(types
);
233 dragComposite
.addDragListener(dragSourceListener
);
235 // Make relation label draggable
236 DragSource dragLabel
= new DragSource (relationLabel
, operations
);
237 dragLabel
.setTransfer(types
);
238 dragLabel
.addDragListener(dragSourceListener
);
242 * Shared drag listener which destroys the NameComposite after the drag
243 * gesture is completed.
245 DragSourceListener dragSourceListener
= new DragSourceListener () {
247 CompositeRectangle rectangle
;
249 public void dragStart(DragSourceEvent event
) {
251 rectangle
= new CompositeRectangle(NameComposite
.this,
252 Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
));
256 public void dragSetData (DragSourceEvent event
) {
257 TaxonTransfer
.getInstance().setTaxon(synonym
);
259 public void dragFinished(DragSourceEvent event
) {
260 if (event
.detail
== DND
.DROP_MOVE
) {
261 Composite parent
= NameComposite
.this.getParent();
262 Composite grandParent
= parent
.getParent();
264 // rectangle.dispose();
265 NameComposite
.this.dispose();
267 // If this was the last child in its homo. group,
268 // drop the group from the interface
269 if (parent
.getChildren().length
== 0)
272 // Only used if we're dropping it on its current homo. group
275 grandParent
.layout();
279 private NameEditorView nameEditorView
;
281 public void setName(TaxonNameBase name
) {
282 (compositeType
== ACCEPTED? taxon
: synonym
).setName(name
);
283 nameViewer
.setShowError(name
.getHasProblem());
286 public TaxonNameBase
getName() {
287 return (compositeType
== ACCEPTED? taxon
: synonym
).getName();
290 public void setNameEditorView(NameEditorView nameEditorView
) {
291 this.nameEditorView
= nameEditorView
;
294 private void createTempMenu() {
295 final Menu menu1
= new Menu(relationLabel
);
296 relationLabel
.setMenu(menu1
);
297 relationLabel
.addMouseListener(new MouseListener() {
299 public void mouseDoubleClick(MouseEvent e
) {
300 // TODO Auto-generated method stub
304 public void mouseDown(MouseEvent e
) {
305 menu1
.setVisible(true);
308 public void mouseUp(MouseEvent e
) {
309 // TODO Auto-generated method stub
315 final MenuItem deletehieraciumFuscoatrumMenuItem
= new MenuItem(menu1
, SWT
.NONE
);
316 deletehieraciumFuscoatrumMenuItem
.setText("Delete \"Hieracium wolffii subsp. trichotranssilvanicum\" from this taxon");
318 new MenuItem(menu1
, SWT
.SEPARATOR
);
320 final MenuItem moveMenuItem
= new MenuItem(menu1
, SWT
.NONE
);
321 moveMenuItem
.setText("Make \"Hieracium wolffii subsp. trichotranssilvanicum\" the accepted name of a new taxon");
323 final MenuItem makehieraciumFuscoatrumMenuItem
= new MenuItem(menu1
, SWT
.NONE
);
324 makehieraciumFuscoatrumMenuItem
.setText("Make \"Hieracium wolffii subsp. trichotranssilvanicum\" the accepted name of this taxon");
326 new MenuItem(menu1
, SWT
.SEPARATOR
);
328 final MenuItem turnhieraciumFuscoatrumMenuItem_3
= new MenuItem(menu1
, SWT
.NONE
);
329 turnhieraciumFuscoatrumMenuItem_3
.setText("Turn \"Hieracium wolffii subsp. trichotranssilvanicum\" into a basionym");
331 final MenuItem turnhieraciumFuscoatrumMenuItem_4
= new MenuItem(menu1
, SWT
.NONE
);
332 turnhieraciumFuscoatrumMenuItem_4
.setText("...");