2 * Copyright (C) 2007 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.
10 package eu
.etaxonomy
.taxeditor
.store
;
12 import java
.util
.UUID
;
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
;
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
;
33 * <p>StoreUtil class.</p>
39 public class StoreUtil
extends AbstractUtility
{
42 * <p>checktaxonExists</p>
44 * @param fromString a {@link java.util.UUID} object.
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);
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"
61 public static CdmBase
getCdmEntity(Object object
){
62 // TODO temporary solution for ticket #4091????
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();
68 else if(object
instanceof FeatureNodeContainer
){
69 return ((FeatureNodeContainer
) object
).getFeatureNode();
71 else if(object
instanceof CdmBase
){
72 return (CdmBase
) object
;
74 throw new IllegalArgumentException("Object " + object
.toString() + " is neither a CdmBase nor a CDM \"container\"");
78 * <p>getOperationHistory</p>
80 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory} object.
82 public static IOperationHistory
getOperationHistory() {
83 return TaxeditorStorePlugin
.getDefault().getWorkbench().
84 getOperationSupport().getOperationHistory();
88 * <p>setStatusLineManager</p>
90 * @param manager a {@link org.eclipse.jface.action.IStatusLineManager} object.
92 public static void setStatusLineManager(IStatusLineManager manager
) {
93 statusLineManager
= manager
;
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();
102 scrolledForm
= (ScrolledForm
)parent
;
103 if(scrolledForm
!=null){
104 scrolledForm
.reflow(flushCashes
);
105 scrolledForm
.redraw();
111 * <p>getUndoContext</p>
113 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
115 public static IUndoContext
getUndoContext(){
116 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
122 * @return a {@link java.lang.String} object.
124 public static String
getPluginId(){
125 return TaxeditorStorePlugin
.PLUGIN_ID
;
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>
135 * the title string to clean
136 * @return the cleaned title string
138 public static String
cleanTitleString(String title
){
139 return title
.replace("&", "&&");
142 public static String
getPrefKey(Class
<?
extends AbstractFormSection
> sectionClass
, Object entity
) {
143 return sectionClass
.getCanonicalName()+";"+entity
.getClass().getCanonicalName();
147 * Checks the dirty flag and, if set, prompts the user to optionally save
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
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
);
159 editor
.save(new NullProgressMonitor());
171 public static boolean confirmDelete(){
172 return MessagingUtils
.confirmDialog("Confirm deletion", "Do you really want to delete the selected element(s)?");
176 * Compares the two given input strings considering the given search string.<br>
177 * Strings will be sorted according to <br>
179 * <li> result begins with search string
181 * <li> result contains search string
183 * <li> alphabetically
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
)){
195 return string1
.compareTo(string2
);
198 else if(string2
.startsWith(searchString
)){
201 //2. label that contains search string
202 if(string1
.contains(searchString
)){
203 if(!string2
.contains(searchString
)){
207 else if(string2
.contains(searchString
)){
210 return string1
.compareTo(string2
);