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