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