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 scrolledForm
= (ScrolledForm
)parent
;
87 if(scrolledForm
!=null){
88 if (!scrolledForm
.isDisposed()){
89 scrolledForm
.reflow(flushCashes
);
90 scrolledForm
.redraw();
96 public static IUndoContext
getUndoContext(){
97 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
100 public static String
getPluginId(){
101 return TaxeditorStorePlugin
.PLUGIN_ID
;
105 * Cleans title string for output in section titles<br>
106 * E.g. escapes '&' with "&&" to avoid mnemonic handling (see
107 * Label.setText() documentation)<br>
111 * the title string to clean
112 * @return the cleaned title string
114 public static String
cleanTitleString(String title
){
115 return title
.replace("&", "&&");
118 public static String
getPrefKey(Class
<?
extends AbstractFormSection
> sectionClass
, String entity
) {
119 return sectionClass
.getCanonicalName()+";"+entity
;
123 * Checks the dirty flag and, if set, prompts the user to optionally save
126 * @return <code>false</code> if the editor is not dirty anymore, either
127 * because it wasn't beforehand or because it has been saved.
128 * <code>true</code> otherwise
130 public static boolean promptCheckIsDirty(IE4SavablePart editor
){
131 if (editor
.isDirty()){
132 boolean proceed
= MessageDialog
.openQuestion(null,
133 Messages
.DefinedTermEditorE4_SAVE_TITLE
, Messages
.DefinedTermEditorE4_SAVE_MESSAGE
);
135 editor
.save(new NullProgressMonitor());
147 public static boolean confirmDelete(){
148 return MessagingUtils
.confirmDialog("Confirm deletion", "Do you really want to delete the selected element(s)?");
152 * Compares the two given input strings considering the given search string.<br>
153 * Strings will be sorted according to <br>
155 * <li> result begins with search string
157 * <li> result contains search string
159 * <li> alphabetically
162 public static int compareBySearchString(String searchString
, String string1
, String string2
) {
163 string1
= string1
.toLowerCase();
164 string2
= string2
.toLowerCase();
165 //1. search string at the beginning
166 if(string1
.startsWith(searchString
)){
167 if(!string2
.startsWith(searchString
)){
171 return string1
.compareTo(string2
);
174 else if(string2
.startsWith(searchString
)){
177 //2. label that contains search string
178 if(string1
.contains(searchString
)){
179 if(!string2
.contains(searchString
)){
183 else if(string2
.contains(searchString
)){
186 return string1
.compareTo(string2
);
189 public static int getSectionStyle(Class
<?
extends AbstractFormSection
> clazz
, String input
){
190 return StoreUtil
.getSectionStyle(clazz
, input
, false);
193 public static int getSectionStyle(Class
<?
extends AbstractFormSection
> clazz
, String input
, boolean initiallyExpanded
){
194 int style
= ExpandableComposite
.TWISTIE
;
195 String prefKey
= getPrefKey(clazz
, input
);
196 if(PreferencesUtil
.contains(prefKey
)){
197 String string
= PreferencesUtil
.getStringValue(prefKey
, true);
199 style
= string
.equals(CdmSectionPart
.EXPANDED
)?style
|= ExpandableComposite
.EXPANDED
:style
;
201 style
= initiallyExpanded?style
|= ExpandableComposite
.EXPANDED
:style
;
205 style
= initiallyExpanded?style
|= ExpandableComposite
.EXPANDED
:style
;
210 public static String
getPath(TermNode
<?
> node
){
211 String path
= node
.getTerm().getLabel();
212 TermNode
<?
> parent
= node
.getParent();
213 while(parent
!= null && parent
.getTerm()!=null){
214 path
= parent
.getTerm().getLabel() + "/" + path
;
215 parent
= parent
.getParent();
220 public static void setTextWithoutModifyListeners(Text text
, String string
){
221 Listener
[] listeners
= text
.getListeners(SWT
.Modify
);
222 for (Listener listener
: listeners
) {
223 text
.removeListener(SWT
.Modify
, listener
);
225 text
.setText(CdmUtils
.Nz(string
));
226 for (Listener listener
: listeners
) {
227 text
.addListener(SWT
.Modify
, listener
);
231 public static Exception
mergeUpdateResultExceptions(UpdateResult result
) {
232 Exception t
= new Exception();
233 if (result
.getExceptions().size() >1){
234 for (Exception e
:result
.getExceptions()){
238 t
= result
.getExceptions().iterator().next();