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
.ExpandableComposite
;
21 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
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
;
37 * <p>StoreUtil class.</p>
43 public class StoreUtil
extends AbstractUtility
{
46 * <p>checktaxonExists</p>
48 * @param fromString a {@link java.util.UUID} object.
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);
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"
65 public static CdmBase
getCdmEntity(Object object
){
66 // TODO temporary solution for ticket #4091????
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();
72 else if(object
instanceof FeatureNodeContainer
){
73 return ((FeatureNodeContainer
) object
).getFeatureNode();
75 else if(object
instanceof CdmBase
){
76 return (CdmBase
) object
;
78 throw new IllegalArgumentException("Object " + object
.toString() + " is neither a CdmBase nor a CDM \"container\"");
82 * <p>getOperationHistory</p>
84 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory} object.
86 public static IOperationHistory
getOperationHistory() {
87 return TaxeditorStorePlugin
.getDefault().getWorkbench().
88 getOperationSupport().getOperationHistory();
92 * <p>setStatusLineManager</p>
94 * @param manager a {@link org.eclipse.jface.action.IStatusLineManager} object.
96 public static void setStatusLineManager(IStatusLineManager manager
) {
97 statusLineManager
= manager
;
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();
106 scrolledForm
= (ScrolledForm
)parent
;
107 if(scrolledForm
!=null){
108 scrolledForm
.reflow(flushCashes
);
109 scrolledForm
.redraw();
115 * <p>getUndoContext</p>
117 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
119 public static IUndoContext
getUndoContext(){
120 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
126 * @return a {@link java.lang.String} object.
128 public static String
getPluginId(){
129 return TaxeditorStorePlugin
.PLUGIN_ID
;
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>
139 * the title string to clean
140 * @return the cleaned title string
142 public static String
cleanTitleString(String title
){
143 return title
.replace("&", "&&");
146 public static String
getPrefKey(Class
<?
extends AbstractFormSection
> sectionClass
, String entity
) {
147 return sectionClass
.getCanonicalName()+";"+entity
;
151 * Checks the dirty flag and, if set, prompts the user to optionally save
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
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
);
163 editor
.save(new NullProgressMonitor());
175 public static boolean confirmDelete(){
176 return MessagingUtils
.confirmDialog("Confirm deletion", "Do you really want to delete the selected element(s)?");
180 * Compares the two given input strings considering the given search string.<br>
181 * Strings will be sorted according to <br>
183 * <li> result begins with search string
185 * <li> result contains search string
187 * <li> alphabetically
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
)){
199 return string1
.compareTo(string2
);
202 else if(string2
.startsWith(searchString
)){
205 //2. label that contains search string
206 if(string1
.contains(searchString
)){
207 if(!string2
.contains(searchString
)){
211 else if(string2
.contains(searchString
)){
214 return string1
.compareTo(string2
);
217 public static int getSectionStyle(Class
<?
extends AbstractFormSection
> clazz
, String input
){
218 return StoreUtil
.getSectionStyle(clazz
, input
, false);
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
;
228 style
= initiallyExpanded?style
|= ExpandableComposite
.EXPANDED
:style
;
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();