Project

General

Profile

Download (8.51 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
        scrolledForm = (ScrolledForm)parent;
87
        if(scrolledForm!=null){
88
            if (!scrolledForm.isDisposed()){
89
                scrolledForm.reflow(flushCashes);
90
                scrolledForm.redraw();
91
            }
92

    
93
        }
94
    }
95

    
96
	public static IUndoContext getUndoContext(){
97
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
98
	}
99

    
100
	public static String getPluginId(){
101
		return TaxeditorStorePlugin.PLUGIN_ID;
102
	}
103

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

    
118
    public static String getPrefKey(Class<? extends AbstractFormSection> sectionClass, String entity) {
119
        return sectionClass.getCanonicalName()+";"+entity;
120
    }
121

    
122
    /**
123
     * Checks the dirty flag and, if set, prompts the user to optionally save
124
     * the editor
125
     *
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
129
     */
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);
134
            if (proceed) {
135
                editor.save(new NullProgressMonitor());
136
                return false;
137
            }
138
            else{
139
                return true;
140
            }
141
        }
142
        else{
143
            return false;
144
        }
145
    }
146

    
147
    public static boolean confirmDelete(){
148
        return MessagingUtils.confirmDialog("Confirm deletion", "Do you really want to delete the selected element(s)?");
149
    }
150

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

    
189
    public static int getSectionStyle(Class<? extends AbstractFormSection> clazz, String input){
190
        return StoreUtil.getSectionStyle(clazz, input, false);
191
    }
192

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

    
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();
216
        }
217
        return path;
218
    }
219

    
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);
224
        }
225
        text.setText(CdmUtils.Nz(string));
226
        for (Listener listener : listeners) {
227
            text.addListener(SWT.Modify, listener);
228
        }
229
    }
230

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