Project

General

Profile

Download (8.8 KB) Statistics
| Branch: | Tag: | Revision:
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 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;
23

    
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;
38

    
39
/**
40
 * <p>StoreUtil class.</p>
41
 *
42
 * @author n.hoffmann
43
 * @created 11.05.2009
44
 */
45
public class StoreUtil extends AbstractUtility {
46

    
47
	/**
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"
54
	 */
55
	public static CdmBase getCdmEntity(Object object){
56
        // TODO temporary solution for ticket #4091????
57
        if (object == null){
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();
61
        }
62
        else if(object instanceof FeatureNodeContainer){
63
            return ((FeatureNodeContainer) object).getFeatureNode();
64
        }
65
        else if(object instanceof CdmBase){
66
            return (CdmBase) object;
67
        }
68
        throw new IllegalArgumentException("Object " + object.toString() + " is neither a CdmBase nor a CDM \"container\"");
69
	}
70

    
71
	public static IOperationHistory getOperationHistory() {
72
		return TaxeditorStorePlugin.getDefault().getWorkbench().
73
					getOperationSupport().getOperationHistory();
74
	}
75

    
76
	public static void setStatusLineManager(IStatusLineManager manager) {
77
		statusLineManager = manager;
78
	}
79

    
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();
85
        }
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();
90
        }
91

    
92
        if(scrolledForm!=null){
93
            if (!scrolledForm.isDisposed()){
94
                scrolledForm.reflow(flushCashes);
95
                scrolledForm.redraw();
96
            }
97
        }
98
    }
99

    
100
	public static IUndoContext getUndoContext(){
101
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
102
	}
103

    
104
	public static String getPluginId(){
105
		return TaxeditorStorePlugin.PLUGIN_ID;
106
	}
107

    
108
    /**
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>
112
     * see also #4302
113
     *
114
     * @param title
115
     *            the title string to clean
116
     * @return the cleaned title string
117
     */
118
	public static String cleanTitleString(String title){
119
	    return title.replace("&", "&&");
120
	}
121

    
122
    public static String getPrefKey(Class<? extends AbstractFormSection> sectionClass, String entity) {
123
        return sectionClass.getCanonicalName()+";"+entity;
124
    }
125

    
126
    /**
127
     * Checks the dirty flag and, if set, prompts the user to optionally save
128
     * the editor
129
     *
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
133
     */
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);
138
            if (proceed) {
139
                editor.save(new NullProgressMonitor());
140
                return false;
141
            }
142
            else{
143
                return true;
144
            }
145
        }
146
        else{
147
            return false;
148
        }
149
    }
150

    
151
    public static boolean confirmDelete(){
152
        return MessagingUtils.confirmDialog("Confirm deletion", "Do you really want to delete the selected element(s)?");
153
    }
154

    
155
    /**
156
     * Compares the two given input strings considering the given search string.<br>
157
     * Strings will be sorted according to <br>
158
     * <ol>
159
     * <li> result begins with search string
160
     * <li> string length
161
     * <li> result contains search string
162
     * <li> string length
163
     * <li> alphabetically
164
     * </ol>
165
     */
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)){
172
                return -1;
173
            }
174
            else{
175
                return string1.compareTo(string2);
176
            }
177
        }
178
        else if(string2.startsWith(searchString)){
179
            return 1;
180
        }
181
        //2. label that contains search string
182
        if(string1.contains(searchString)){
183
            if(!string2.contains(searchString)){
184
                return -1;
185
            }
186
        }
187
        else if(string2.contains(searchString)){
188
            return 1;
189
        }
190
        return string1.compareTo(string2);
191
    }
192

    
193
    public static int getSectionStyle(Class<? extends AbstractFormSection> clazz, String input){
194
        return StoreUtil.getSectionStyle(clazz, input, false);
195
    }
196

    
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);
202
            if (string != null){
203
                style = string.equals(CdmSectionPart.EXPANDED)?style |= ExpandableComposite.EXPANDED:style;
204
            }else{
205
                style = initiallyExpanded?style |= ExpandableComposite.EXPANDED:style;
206
            }
207
        }
208
        else{
209
            style = initiallyExpanded?style |= ExpandableComposite.EXPANDED:style;
210
        }
211
        return style;
212
    }
213

    
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();
220
        }
221
        return path;
222
    }
223

    
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);
228
        }
229
        text.setText(CdmUtils.Nz(string));
230
        for (Listener listener : listeners) {
231
            text.addListener(SWT.Modify, listener);
232
        }
233
    }
234

    
235
    public static Exception mergeUpdateResultExceptions(UpdateResult result) {
236
        Exception t = new Exception();
237
        if (result.getExceptions().size() >1){
238
        	for (Exception e:result.getExceptions()){
239
        		t.addSuppressed(e);
240
        	}
241
        }else {
242
        	t = result.getExceptions().iterator().next();
243
        }
244
        return t;
245
    }
246
}
(9-9/13)