2 * Copyright (C) 2013 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.specimen
;
11 import java
.awt
.Color
;
12 import java
.awt
.Dimension
;
13 import java
.awt
.event
.ItemEvent
;
14 import java
.awt
.event
.ItemListener
;
15 import java
.io
.Serializable
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Collections
;
18 import java
.util
.HashMap
;
19 import java
.util
.HashSet
;
20 import java
.util
.List
;
23 import java
.util
.UUID
;
25 import javax
.swing
.BoxLayout
;
26 import javax
.swing
.ButtonGroup
;
27 import javax
.swing
.JLabel
;
28 import javax
.swing
.JOptionPane
;
29 import javax
.swing
.JPanel
;
30 import javax
.swing
.JRadioButton
;
31 import javax
.swing
.JScrollPane
;
32 import javax
.swing
.JTextArea
;
34 import org
.apache
.commons
.lang
.StringUtils
;
35 import org
.apache
.logging
.log4j
.LogManager
;
36 import org
.apache
.logging
.log4j
.Logger
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
39 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
40 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
41 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
42 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceBase
;
43 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceType
;
44 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
45 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
55 public class SpecimenUserInteraction
implements ItemListener
, Serializable
{
57 private static final long serialVersionUID
= 6384263183459028523L;
58 @SuppressWarnings("unused")
59 private static final Logger log
= LogManager
.getLogger();
61 public Classification
askForClassification(Map
<String
, Classification
> classMap
){
62 List
<String
> possibilities
= new ArrayList
<String
>(classMap
.keySet());
63 Collections
.sort(possibilities
);
65 if (classMap
.keySet().size()>0) {
66 classMap
.put("Nothing matches, create a new classification",null);
67 possibilities
.add(0, "Nothing matches, create a new classification");
72 JTextArea textArea
= new JTextArea("Which existing classification should be used ?");
73 JScrollPane scrollPane
= new JScrollPane(textArea
);
74 textArea
.setLineWrap(true);
75 textArea
.setWrapStyleWord(true);
76 scrollPane
.setPreferredSize( new Dimension( 500, 50 ) );
80 s
= (String
)JOptionPane
.showInputDialog(
83 "Please select a classification in the list",
84 JOptionPane
.PLAIN_MESSAGE
,
86 possibilities
.toArray(),
87 "Nothing matches, create a new classification");
89 return classMap
.get(s
);
93 * @return the name for the new Classification
95 public String
createNewClassification() {
96 JTextArea textArea
= new JTextArea("How should the classification be named ?");
97 JScrollPane scrollPane
= new JScrollPane(textArea
);
98 textArea
.setLineWrap(true);
99 textArea
.setWrapStyleWord(true);
100 scrollPane
.setPreferredSize( new Dimension( 500, 50 ) );
104 s
=(String
)JOptionPane
.showInputDialog(
107 "Get full classification name",
108 JOptionPane
.PLAIN_MESSAGE
,
120 public Reference
askForReference(Map
<String
, Reference
> refMap
) {
121 List
<String
> possibilities
= new ArrayList
<>(refMap
.keySet());
122 Collections
.sort(possibilities
);
123 if (refMap
.keySet().size()>0) {
124 refMap
.put("Nothing matches, create a new reference",null);
125 possibilities
.add(0, "Nothing matches, create a new reference");
130 JTextArea textArea
= new JTextArea("Which existing reference should be used?");
131 JScrollPane scrollPane
= new JScrollPane(textArea
);
132 textArea
.setLineWrap(true);
133 textArea
.setWrapStyleWord(true);
134 scrollPane
.setPreferredSize( new Dimension( 700, 50 ) );
138 s
= (String
)JOptionPane
.showInputDialog(
141 "Please select a reference in the list",
142 JOptionPane
.PLAIN_MESSAGE
,
144 possibilities
.toArray(),
147 return refMap
.get(s
);
152 * @param iReferenceService
156 public List
<OriginalSourceBase
> askForSource(Map
<String
, OriginalSourceBase
> refMap
, String currentElement
, String blabla
,
157 IReferenceService iReferenceService
, List
<String
> docSources
) {
159 // System.out.println(refMap);
160 List
<String
> possibilities
= new ArrayList
<> (refMap
.keySet());
162 Set
<String
> all
= new HashSet
<>();
163 all
.addAll(possibilities
);
165 List
<String
> allList
= new ArrayList
<String
>();
167 Collections
.sort(allList
);
168 allList
.add(0, "Create a new source");
170 JLabel label
= new JLabel(blabla
);
172 sources
=new ArrayList
<String
>();
174 JPanel checkPanel
= null;
175 ButtonGroup group
= null;
176 JScrollPane scrollPane
= null;
178 JRadioButton jcb
= null;
180 Object
[] options
= {"Add and close", "Add and continue - I want to add more sources","Close without adding anything"};
182 // System.out.println(docSources);
185 group
= new ButtonGroup();
186 checkPanel
= new JPanel();
187 checkPanel
.setLayout(new BoxLayout(checkPanel
, BoxLayout
.Y_AXIS
));
188 // allList.removeAll(sources);
189 scrollPane
= new JScrollPane(checkPanel
);
190 scrollPane
.setPreferredSize( new Dimension( 700, 300 ) );
192 checkPanel
.add(label
);
194 for (String ch
:allList
){
195 if (StringUtils
.isBlank(ch
)) {
198 // System.out.println("HOP ="+ch+"=");
199 if(docSources
.contains(ch
)) {
200 jcb
= new JRadioButton("<html>"+ch
.replace("---", "<br/>")+"</html>");
201 jcb
.setForeground(Color
.blue
);
203 jcb
= new JRadioButton("<html>"+ch
.replace("---", "<br/>")+"</html>");
204 jcb
.setForeground(Color
.black
);
206 jcb
.addItemListener(this);
211 n
= JOptionPane
.showOptionDialog(null,
213 "Choose a source for "+currentElement
+"(in blue the source from the document)",
214 JOptionPane
.YES_NO_CANCEL_OPTION
,
215 JOptionPane
.QUESTION_MESSAGE
,
219 if(n
<3 && !currentSource
.isEmpty() && !currentSource
.equalsIgnoreCase("Create a new source")) {
220 sources
.add(currentSource
);
222 // System.out.println("current source: "+currentSource);
223 if(currentSource
.equalsIgnoreCase("Create a new source")){
224 String a
= createNewSource();
225 if (a
!=null && !a
.isEmpty()) {
231 List
<OriginalSourceBase
> dess
= new ArrayList
<>();
232 for (String src
:sources
){
233 if (refMap
.get(src
) !=null) {
234 dess
.add(refMap
.get(src
));
238 String titlecache
="";
240 if (src
.indexOf("---")>-1){
241 titlecache
= src
.split("---")[0].trim();
242 micro
=src
.split("---")[1].trim();
245 titlecache
= src
.split("---")[0].trim();
248 List
<Reference
> references
= iReferenceService
.list(Reference
.class, null, null, null, null);
249 for (Reference reference
:references
){
250 if (reference
.getTitleCache().equalsIgnoreCase(titlecache
)) {
255 re
= ReferenceFactory
.newGeneric();
256 re
.setTitleCache(titlecache
);
257 iReferenceService
.saveOrUpdate(re
);
260 dess
.add(IdentifiableSource
.NewInstance(OriginalSourceType
.Import
,null, null, re
,micro
));
266 public String
createNewReference() {
267 JTextArea textArea
= new JTextArea("How should the reference be named ?");
268 JScrollPane scrollPane
= new JScrollPane(textArea
);
269 textArea
.setLineWrap(true);
270 textArea
.setWrapStyleWord(true);
271 scrollPane
.setPreferredSize( new Dimension( 500, 50 ) );
275 s
= (String
)JOptionPane
.showInputDialog(
278 "Get full reference name",
279 JOptionPane
.PLAIN_MESSAGE
,
282 "ABCD Import from XML");
287 public String
createNewSource() {
288 JTextArea textArea
= new JTextArea("How should the source be named? If there is a citation detail, prefix it with 3 minus signs ('---').");
289 JScrollPane scrollPane
= new JScrollPane(textArea
);
290 textArea
.setLineWrap(true);
291 textArea
.setWrapStyleWord(true);
292 scrollPane
.setPreferredSize( new Dimension( 500, 50 ) );
296 s
= (String
)JOptionPane
.showInputDialog(
299 "Get full source name",
300 JOptionPane
.PLAIN_MESSAGE
,
303 "ABCD Import from XML");
308 public TaxonDescription
askForDescriptionGroup(Set
<TaxonDescription
> descriptions
) {
309 JTextArea textArea
= new JTextArea("One or several description group(s) does already exist for this taxon.");
310 JScrollPane scrollPane
= new JScrollPane(textArea
);
311 textArea
.setLineWrap(true);
312 textArea
.setWrapStyleWord(true);
313 scrollPane
.setPreferredSize( new Dimension( 700, 50 ) );
315 Map
<String
,TaxonDescription
> descrMap
= new HashMap
<String
, TaxonDescription
>();
318 for (TaxonDescription description
: descriptions
){
319 // System.out.println("descr. titlecache "+description.getTitleCache());
320 Set
<IdentifiableSource
> sources
= description
.getTaxon().getSources();
321 sources
.addAll(description
.getSources());
322 List
<String
> src
=new ArrayList
<String
>();
323 for (IdentifiableSource s
:sources
) {
324 src
.add(s
.getCitation().getTitleCache());
326 List
<String
> srcb
= new ArrayList
<String
>(new HashSet
<>(src
));
328 if(descrMap
.containsKey(descCnt
+": "+description
.getTitleCache()+"("+StringUtils
.join(srcb
,";")+")")) {
331 descrMap
.put(descCnt
+": "+description
.getTitleCache()+"("+StringUtils
.join(srcb
,";")+")",description
);
334 if(descrMap
.containsKey(description
.getTitleCache())) {
337 descrMap
.put(descCnt
+": "+description
.getTitleCache(),description
);
338 // for (IdentifiableSource source:sources){
339 // if(ref.equals(source.getCitation())) {
340 // taxonDescription = description;
345 List
<String
> possibilities
= new ArrayList
<>(descrMap
.keySet());
346 if (possibilities
.size()==0) {
349 Collections
.sort(possibilities
);
351 descrMap
.put("No, add a brand new description group", null);
352 possibilities
.add(0, "No, add a brand new description group");
356 s
= (String
)JOptionPane
.showInputDialog(
359 "What should be done? Should an existing group be reused ? ",
360 JOptionPane
.PLAIN_MESSAGE
,
362 possibilities
.toArray(),
363 "No, add a brand new description group");
366 if (descrMap
.get(s
) !=null) {
367 return descrMap
.get(s
);
374 * Look if the same name already exists in the ALL classifications and ask the user to select one or none.
375 * @param scientificName
377 * @return null if not found, or the selected Taxon
379 @SuppressWarnings("rawtypes")
380 public Taxon
askWhereToFixData(String scientificName
, List
<TaxonBase
> taxonList
, Classification classification
) {
381 Map
<String
,TaxonNode
> classMap
= new HashMap
<>();
382 boolean sameClassification
=false;
384 for (TaxonBase taxonBase
: taxonList
){
385 if(taxonBase
.isInstanceOf(Taxon
.class)){
386 Taxon taxon
= HibernateProxyHelper
.deproxy(taxonBase
, Taxon
.class);
387 for (TaxonNode node
: taxon
.getTaxonNodes()){
388 classMap
.put("Reuse the one from the classification \""+node
.getClassification().getTitleCache()+"\"", node
);
389 if (node
.getClassification().getUuid().equals(classification
.getUuid())) {
390 sameClassification
=true;
396 if (classMap
.keySet().size()==1 && sameClassification
) {
400 JTextArea textArea
= new JTextArea("The same taxon ("+scientificName
+") already exists in an other classification.");
401 JScrollPane scrollPane
= new JScrollPane(textArea
);
402 textArea
.setLineWrap(true);
403 textArea
.setWrapStyleWord(true);
404 scrollPane
.setPreferredSize( new Dimension( 700, 50 ) );
406 List
<String
> possibilities
= new ArrayList
<String
> (classMap
.keySet());
407 if (possibilities
.size()==0) {
410 Collections
.sort(possibilities
);
411 if(!sameClassification
){
412 classMap
.put("Add a brand new Taxon to the current classification, no recycling please", null);
413 possibilities
.add(0, "Add a brand new Taxon to the current classification, no recycling please");
417 s
= (String
)JOptionPane
.showInputDialog(
420 "What should be done? ",
421 JOptionPane
.PLAIN_MESSAGE
,
423 possibilities
.toArray(),
424 "Add a brand new Taxon to the current classification, no recycling please");
427 if (classMap
.get(s
) !=null) {
428 return classMap
.get(s
).getTaxon();
435 * Look if the same TaxonBase already exists in the SAME classification
436 * @param taxonBaseList
437 * @return null if not found, or the corresponding Taxon
439 @SuppressWarnings("rawtypes")
440 public Taxon
lookForTaxaIntoCurrentClassification(List
<TaxonBase
> taxonBaseList
, Classification classification
) {
441 Taxon taxonFound
=null;
442 for (TaxonBase taxonBase
:taxonBaseList
){
443 if(taxonBase
.isInstanceOf(Taxon
.class)){
444 Taxon taxon
= HibernateProxyHelper
.deproxy(taxonBase
, Taxon
.class);
445 for (TaxonNode node
: taxon
.getTaxonNodes()){
446 UUID classUuid
= node
.getClassification().getUuid();
447 if (classification
.getUuid().equals(classUuid
)){
456 List
<String
> sources
= new ArrayList
<>();
457 String currentSource
= "";
460 public void itemStateChanged(ItemEvent e
) {
461 JRadioButton cb
= (JRadioButton
) e
.getItem();
462 int state
= e
.getStateChange();
463 if (state
== ItemEvent
.SELECTED
) {
464 currentSource
=cb
.getText().replace("</html>", "").replace("<html>","")
465 .replace("<br/>","---").replace("<font color=\"blue\">","").replace("</font>","");