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 org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
13 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
14 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
15 import org
.eclipse
.jface
.action
.IStatusLineManager
;
16 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.widgets
.Composite
;
19 import org
.eclipse
.swt
.widgets
.Listener
;
20 import org
.eclipse
.swt
.widgets
.Text
;
21 import org
.eclipse
.ui
.forms
.widgets
.ExpandableComposite
;
22 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
24 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
25 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
;
26 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.term
.TermNode
;
29 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
30 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
31 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
32 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
33 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
34 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
36 import eu
.etaxonomy
.taxeditor
.view
.detail
.CdmSectionPart
;
37 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
40 * <p>StoreUtil class.</p>
45 public class StoreUtil
extends AbstractUtility
{
48 * If the object given is already a {@link CdmBase} then it is returned.<br>
49 * If it is a kind of "container" for CDM objects then it is asked for its "responsible" CdmBase entity.<br>
50 * Otherwise an exception is thrown.
51 * @param object the object to test for CdmBase
52 * @return a CdmBase object
53 * @throws IllegalArgumentException if the tested object is neither a CdmBase nor a CDM "container"
55 public static CdmBase
getCdmEntity(Object object
){
56 // TODO temporary solution for ticket #4091????
58 return null; //not sure if an object should ever be null at this point, but this needs to be handled in calling methods
59 }else if(object
instanceof DerivedUnitFacade
){
60 return ((DerivedUnitFacade
)object
).baseUnit();
62 else if(object
instanceof FeatureNodeContainer
){
63 return ((FeatureNodeContainer
) object
).getFeatureNode();
65 else if(object
instanceof CdmBase
){
66 return (CdmBase
) object
;
68 throw new IllegalArgumentException("Object " + object
.toString() + " is neither a CdmBase nor a CDM \"container\"");
71 public static IOperationHistory
getOperationHistory() {
72 return TaxeditorStorePlugin
.getDefault().getWorkbench().
73 getOperationSupport().getOperationHistory();
76 public static void setStatusLineManager(IStatusLineManager manager
) {
77 statusLineManager
= manager
;
80 public static void reflowParentScrolledForm(Composite composite
, boolean flushCashes
){
81 ScrolledForm scrolledForm
= null;
82 Composite parent
= composite
;
83 while(parent
!=null && !(parent
instanceof ScrolledForm
) && !parent
.isDisposed()){
84 parent
= parent
.getParent();
86 if (parent
instanceof ScrolledForm
){
87 scrolledForm
= (ScrolledForm
)parent
;
88 }else if ( parent
!= null && !parent
.isDisposed() && parent
.getParent() != null && !parent
.getParent().isDisposed() &&parent
.getParent() instanceof ScrolledForm
){
89 scrolledForm
= (ScrolledForm
)parent
.getParent();
92 if(scrolledForm
!=null){
93 if (!scrolledForm
.isDisposed()){
94 scrolledForm
.reflow(flushCashes
);
95 scrolledForm
.redraw();
100 public static IUndoContext
getUndoContext(){
101 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
104 public static String
getPluginId(){
105 return TaxeditorStorePlugin
.PLUGIN_ID
;
109 * Cleans title string for output in section titles<br>
110 * E.g. escapes '&' with "&&" to avoid mnemonic handling (see
111 * Label.setText() documentation)<br>
115 * the title string to clean
116 * @return the cleaned title string
118 public static String
cleanTitleString(String title
){
119 return title
.replace("&", "&&");
122 public static String
getPrefKey(Class
<?
extends AbstractFormSection
> sectionClass
, String entity
) {
123 return sectionClass
.getCanonicalName()+";"+entity
;
127 * Checks the dirty flag and, if set, prompts the user to optionally save
130 * @return <code>false</code> if the editor is not dirty anymore, either
131 * because it wasn't beforehand or because it has been saved.
132 * <code>true</code> otherwise
134 public static boolean promptCheckIsDirty(IE4SavablePart editor
){
135 if (editor
.isDirty()){
136 boolean proceed
= MessageDialog
.openQuestion(null,
137 Messages
.DefinedTermEditorE4_SAVE_TITLE
, Messages
.DefinedTermEditorE4_SAVE_MESSAGE
);
139 editor
.save(new NullProgressMonitor());
151 public static boolean confirmDelete(){
152 return MessagingUtils
.confirmDialog("Confirm deletion", "Do you really want to delete the selected element(s)?");
156 * Compares the two given input strings considering the given search string.<br>
157 * Strings will be sorted according to <br>
159 * <li> result begins with search string
161 * <li> result contains search string
163 * <li> alphabetically
166 public static int compareBySearchString(String searchString
, String string1
, String string2
) {
167 string1
= string1
.toLowerCase();
168 string2
= string2
.toLowerCase();
169 //1. search string at the beginning
170 if(string1
.startsWith(searchString
)){
171 if(!string2
.startsWith(searchString
)){
175 return string1
.compareTo(string2
);
178 else if(string2
.startsWith(searchString
)){
181 //2. label that contains search string
182 if(string1
.contains(searchString
)){
183 if(!string2
.contains(searchString
)){
187 else if(string2
.contains(searchString
)){
190 return string1
.compareTo(string2
);
193 public static int getSectionStyle(Class
<?
extends AbstractFormSection
> clazz
, String input
){
194 return StoreUtil
.getSectionStyle(clazz
, input
, false);
197 public static int getSectionStyle(Class
<?
extends AbstractFormSection
> clazz
, String input
, boolean initiallyExpanded
){
198 int style
= ExpandableComposite
.TWISTIE
;
199 String prefKey
= getPrefKey(clazz
, input
);
200 if(PreferencesUtil
.contains(prefKey
)){
201 String string
= PreferencesUtil
.getStringValue(prefKey
, true);
203 style
= string
.equals(CdmSectionPart
.EXPANDED
)?style
|= ExpandableComposite
.EXPANDED
:style
;
205 style
= initiallyExpanded?style
|= ExpandableComposite
.EXPANDED
:style
;
209 style
= initiallyExpanded?style
|= ExpandableComposite
.EXPANDED
:style
;
214 public static String
getPath(TermNode
<?
> node
){
215 String path
= node
.getTerm().getLabel();
216 TermNode
<?
> parent
= node
.getParent();
217 while(parent
!= null && parent
.getTerm()!=null){
218 path
= parent
.getTerm().getLabel() + "/" + path
;
219 parent
= parent
.getParent();
224 public static void setTextWithoutModifyListeners(Text text
, String string
){
225 Listener
[] listeners
= text
.getListeners(SWT
.Modify
);
226 for (Listener listener
: listeners
) {
227 text
.removeListener(SWT
.Modify
, listener
);
229 text
.setText(CdmUtils
.Nz(string
));
230 for (Listener listener
: listeners
) {
231 text
.addListener(SWT
.Modify
, listener
);
235 public static Exception
mergeUpdateResultExceptions(UpdateResult result
) {
236 Exception t
= new Exception();
237 if (result
.getExceptions().size() >1){
238 for (Exception e
:result
.getExceptions()){
242 t
= result
.getExceptions().iterator().next();