ref #8045: further implementation for default/explicit value selection preference...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / StoreUtil.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
10 package eu.etaxonomy.taxeditor.store;
11
12 import java.util.UUID;
13
14 import org.eclipse.core.commands.operations.IOperationHistory;
15 import org.eclipse.core.commands.operations.IUndoContext;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17 import org.eclipse.jface.action.IStatusLineManager;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.ui.forms.widgets.ScrolledForm;
21
22 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
23 import eu.etaxonomy.cdm.model.common.CdmBase;
24 import eu.etaxonomy.taxeditor.l10n.Messages;
25 import eu.etaxonomy.taxeditor.model.AbstractUtility;
26 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
27 import eu.etaxonomy.taxeditor.model.MessagingUtils;
28 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
29 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
30 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
31
32 /**
33 * <p>StoreUtil class.</p>
34 *
35 * @author n.hoffmann
36 * @created 11.05.2009
37 * @version 1.0
38 */
39 public class StoreUtil extends AbstractUtility {
40
41 /**
42 * <p>checktaxonExists</p>
43 *
44 * @param fromString a {@link java.util.UUID} object.
45 */
46 public static void checktaxonExists(UUID fromString) {
47 // if (CdmStore.getTaxonService().getTaxonByUuid(UUID.fromString(uuid)) == null) {
48 // logger.warn("Couldn't find taxon with UUID " + uuid);
49 // return null;
50 // }
51 }
52
53 /**
54 * If the object given is already a {@link CdmBase} then it is returned.<br>
55 * If it is a kind of "container" for CDM objects then it is asked for its "responsible" CdmBase entity.<br>
56 * Otherwise an exception is thrown.
57 * @param object the object to test for CdmBase
58 * @return a CdmBase object
59 * @throws IllegalArgumentException if the tested object is neither a CdmBase nor a CDM "container"
60 */
61 public static CdmBase getCdmEntity(Object object){
62 // TODO temporary solution for ticket #4091????
63 if (object == null){
64 return null; //not sure if an object should ever be null at this point, but this needs to be handled in calling methods
65 }else if(object instanceof DerivedUnitFacade){
66 return ((DerivedUnitFacade)object).baseUnit();
67 }
68 else if(object instanceof FeatureNodeContainer){
69 return ((FeatureNodeContainer) object).getFeatureNode();
70 }
71 else if(object instanceof CdmBase){
72 return (CdmBase) object;
73 }
74 throw new IllegalArgumentException("Object " + object.toString() + " is neither a CdmBase nor a CDM \"container\"");
75 }
76
77 /**
78 * <p>getOperationHistory</p>
79 *
80 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory} object.
81 */
82 public static IOperationHistory getOperationHistory() {
83 return TaxeditorStorePlugin.getDefault().getWorkbench().
84 getOperationSupport().getOperationHistory();
85 }
86
87 /**
88 * <p>setStatusLineManager</p>
89 *
90 * @param manager a {@link org.eclipse.jface.action.IStatusLineManager} object.
91 */
92 public static void setStatusLineManager(IStatusLineManager manager) {
93 statusLineManager = manager;
94 }
95
96 public static void reflowParentScrolledForm(Composite composite, boolean flushCashes){
97 ScrolledForm scrolledForm = null;
98 Composite parent = composite;
99 while(parent!=null && !(parent instanceof ScrolledForm)){
100 parent = parent.getParent();
101 }
102 scrolledForm = (ScrolledForm)parent;
103 if(scrolledForm!=null){
104 scrolledForm.reflow(flushCashes);
105 scrolledForm.redraw();
106 }
107 }
108
109
110 /**
111 * <p>getUndoContext</p>
112 *
113 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
114 */
115 public static IUndoContext getUndoContext(){
116 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
117 }
118
119 /**
120 * <p>getPluginId</p>
121 *
122 * @return a {@link java.lang.String} object.
123 */
124 public static String getPluginId(){
125 return TaxeditorStorePlugin.PLUGIN_ID;
126 }
127
128 /**
129 * Cleans title string for output in section titles<br>
130 * E.g. escapes '&' with "&&" to avoid mnemonic handling (see
131 * Label.setText() documentation)<br>
132 * see also #4302
133 *
134 * @param title
135 * the title string to clean
136 * @return the cleaned title string
137 */
138 public static String cleanTitleString(String title){
139 return title.replace("&", "&&");
140 }
141
142 public static String getPrefKey(Class<? extends AbstractFormSection> sectionClass, Object entity) {
143 return sectionClass.getCanonicalName()+";"+entity.getClass().getCanonicalName();
144 }
145
146 /**
147 * Checks the dirty flag and, if set, prompts the user to optionally save
148 * the editor
149 *
150 * @return <code>false</code> if the editor is not dirty anymore, either
151 * because it wasn't beforehand or because it has been saved.
152 * <code>true</code> otherwise
153 */
154 public static boolean promptCheckIsDirty(IE4SavablePart editor){
155 if (editor.isDirty()){
156 boolean proceed = MessageDialog.openQuestion(null,
157 Messages.DefinedTermEditorE4_SAVE_TITLE, Messages.DefinedTermEditorE4_SAVE_MESSAGE);
158 if (proceed) {
159 editor.save(new NullProgressMonitor());
160 return false;
161 }
162 else{
163 return true;
164 }
165 }
166 else{
167 return false;
168 }
169 }
170
171 public static boolean confirmDelete(){
172 return MessagingUtils.confirmDialog("Confirm deletion", "Do you really want to delete the selected element(s)?");
173 }
174
175 /**
176 * Compares the two given input strings considering the given search string.<br>
177 * Strings will be sorted according to <br>
178 * <ol>
179 * <li> result begins with search string
180 * <li> string length
181 * <li> result contains search string
182 * <li> string length
183 * <li> alphabetically
184 * </ol>
185 */
186 public static int compareBySearchString(String searchString, String string1, String string2) {
187 string1 = string1.toLowerCase();
188 string2 = string2.toLowerCase();
189 //1. search string at the beginning
190 if(string1.startsWith(searchString)){
191 if(!string2.startsWith(searchString)){
192 return -1;
193 }
194 else{
195 return string1.compareTo(string2);
196 }
197 }
198 else if(string2.startsWith(searchString)){
199 return 1;
200 }
201 //2. label that contains search string
202 if(string1.contains(searchString)){
203 if(!string2.contains(searchString)){
204 return -1;
205 }
206 }
207 else if(string2.contains(searchString)){
208 return 1;
209 }
210 return string1.compareTo(string2);
211 }
212 }