added a readme file
[taxeditor.git] / taxeditor-bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / BulkEditorSortMenuProvider.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.bulkeditor;
12
13 import java.util.Comparator;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Menu;
18 import org.eclipse.swt.widgets.MenuItem;
19 import org.eclipse.ui.IEditorInput;
20
21 import eu.etaxonomy.cdm.model.common.TimePeriod;
22 import eu.etaxonomy.cdm.model.reference.Reference;
23 import eu.etaxonomy.taxeditor.annotatedlineeditor.IdentifiableEntityComparator;
24 import eu.etaxonomy.taxeditor.bulkeditor.input.ReferenceEditorInput;
25
26 /**
27 * <p>BulkEditorSortMenuProvider class.</p>
28 *
29 * @author p.ciardelli
30 * @created 19.08.2009
31 * @version 1.0
32 */
33 public class BulkEditorSortMenuProvider implements IBulkEditorSortMenuProvider {
34 private static final Logger logger = Logger
35 .getLogger(BulkEditorSortMenuProvider.class);
36
37 /* (non-Javadoc)
38 * @see eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortMenuProvider#fillMenu(org.eclipse.ui.IEditorInput, java.awt.Menu)
39 */
40 /** {@inheritDoc} */
41 public Menu fillMenu(IEditorInput input, Menu menu) {
42 addMenuItem(menu, "Title cache", new IdentifiableEntityComparator(), true);
43
44 if (input instanceof ReferenceEditorInput) {
45 addMenuItem(menu, "Year", new ReferenceYearComparator(), false);
46 }
47
48 if (input instanceof ReferenceEditorInput) {
49 addMenuItem(menu, "Reference Type", new ReferenceTypeComparator(), false);
50 }
51
52 return menu;
53 }
54
55 /**
56 * @param menu
57 * @param string
58 * @param identifiableEntityComparator
59 * @return
60 */
61 private MenuItem addMenuItem(Menu menu, String text, Comparator comparator, boolean selected) {
62 MenuItem menuItem = new MenuItem(menu, SWT.CHECK);
63 menuItem.setText(text);
64 menuItem.setData(comparator);
65 menuItem.setSelection(selected);
66 return menuItem;
67 }
68
69 class ReferenceYearComparator implements Comparator {
70
71 /**
72 * @param o1
73 * @return
74 */
75 private String getYearString(Object o) {
76 TimePeriod datePublished = o == null || !(o instanceof Reference) ?
77 null : ((Reference) o).getDatePublished();
78 return datePublished == null? null : datePublished.toString();
79 }
80
81 /* (non-Javadoc)
82 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
83 */
84 public int compare(Object o1, Object o2) {
85 String yearString1 = getYearString(o1);
86 String yearString2 = getYearString(o2);
87 if (yearString1 == null) {
88 if (yearString2 == null) {
89 return 0;
90 } else {
91 return -1;
92 }
93 }
94 if (yearString2 == null) {
95 return 1;
96 }
97 int returnVal = yearString1.compareToIgnoreCase(yearString2);
98 if (returnVal == 0) {
99 return new IdentifiableEntityComparator().compare(o1, 02);
100 } else {
101 return returnVal;
102 }
103 }
104 }
105
106 class ReferenceTypeComparator implements Comparator {
107
108 /* (non-Javadoc)
109 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
110 */
111 public int compare(Object o1, Object o2) {
112 String typeString1 = o1.getClass().toString();
113 String typeString2 = o2.getClass().toString();
114 if (typeString1 == null) {
115 if (typeString2 == null) {
116 return 0;
117 } else {
118 return -1;
119 }
120 }
121 if (typeString2 == null) {
122 return 1;
123 }
124 int returnVal = typeString1.compareToIgnoreCase(typeString2);
125 if (returnVal == 0) {
126 return new IdentifiableEntityComparator().compare(o1, 02);
127 } else {
128 return returnVal;
129 }
130 }
131 }
132 }