Revision 3be6ef3e
Added by Niels Hoffmann over 13 years ago
taxeditor-store/src/main/java/eu/etaxonomy/taxeditor/operations/MoveMediaInListOperation.java | ||
---|---|---|
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.operations; |
|
11 |
|
|
12 |
import java.util.Collections; |
|
13 |
import java.util.List; |
|
14 |
import java.util.Set; |
|
15 |
|
|
16 |
import org.eclipse.core.commands.ExecutionException; |
|
17 |
import org.eclipse.core.commands.operations.IUndoContext; |
|
18 |
import org.eclipse.core.runtime.IAdaptable; |
|
19 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
20 |
import org.eclipse.core.runtime.IStatus; |
|
21 |
|
|
22 |
import eu.etaxonomy.cdm.model.description.DescriptionBase; |
|
23 |
import eu.etaxonomy.cdm.model.description.DescriptionElementBase; |
|
24 |
import eu.etaxonomy.cdm.model.media.Media; |
|
25 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
|
26 |
import eu.etaxonomy.taxeditor.store.StoreUtil; |
|
27 |
|
|
28 |
/** |
|
29 |
* @author p.ciardelli |
|
30 |
* @created 05.02.2009 |
|
31 |
* @version 1.0 |
|
32 |
*/ |
|
33 |
public class MoveMediaInListOperation extends AbstractPostOperation { |
|
34 |
|
|
35 |
public static final int UP = 1; |
|
36 |
public static final int DOWN = -1; |
|
37 |
|
|
38 |
private DescriptionBase description; |
|
39 |
private Media media; |
|
40 |
private int direction; |
|
41 |
|
|
42 |
/** |
|
43 |
* @param name |
|
44 |
* @param undoContext |
|
45 |
* @param taxon |
|
46 |
* @param element |
|
47 |
* @param image |
|
48 |
* @param i |
|
49 |
* @param editor |
|
50 |
*/ |
|
51 |
public MoveMediaInListOperation(String label, IUndoContext undoContext, |
|
52 |
Taxon taxon, DescriptionBase description, Media media, |
|
53 |
int direction, IPostOperationEnabled postOperationEnabled) { |
|
54 |
super(label, undoContext, taxon, postOperationEnabled); |
|
55 |
|
|
56 |
this.description = description; |
|
57 |
this.media = media; |
|
58 |
this.direction = direction; |
|
59 |
} |
|
60 |
|
|
61 |
/* (non-Javadoc) |
|
62 |
* @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable) |
|
63 |
*/ |
|
64 |
@Override |
|
65 |
public IStatus execute(IProgressMonitor monitor, IAdaptable info) |
|
66 |
throws ExecutionException { |
|
67 |
|
|
68 |
monitor.worked(20); |
|
69 |
|
|
70 |
moveMedia(description, media, direction); |
|
71 |
monitor.worked(40); |
|
72 |
|
|
73 |
return postExecute(media); |
|
74 |
} |
|
75 |
|
|
76 |
/* (non-Javadoc) |
|
77 |
* @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable) |
|
78 |
*/ |
|
79 |
@Override |
|
80 |
public IStatus redo(IProgressMonitor monitor, IAdaptable info) |
|
81 |
throws ExecutionException { |
|
82 |
return execute(monitor, info); |
|
83 |
} |
|
84 |
|
|
85 |
/* (non-Javadoc) |
|
86 |
* @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable) |
|
87 |
*/ |
|
88 |
@Override |
|
89 |
public IStatus undo(IProgressMonitor monitor, IAdaptable info) |
|
90 |
throws ExecutionException { |
|
91 |
moveMedia(description, media, direction * -1); |
|
92 |
return postExecute(media); |
|
93 |
} |
|
94 |
|
|
95 |
private void moveMedia(DescriptionBase description, Media media, int direction){ |
|
96 |
Set<DescriptionElementBase> elements = description.getElements(); |
|
97 |
|
|
98 |
if(elements.size() != 1){ |
|
99 |
StoreUtil.error(this.getClass(), "More than one description element in this image gallery", null); |
|
100 |
} |
|
101 |
|
|
102 |
DescriptionElementBase element = elements.iterator().next(); |
|
103 |
|
|
104 |
List<Media> medias = element.getMedia(); |
|
105 |
|
|
106 |
int index = medias.indexOf(media); |
|
107 |
int newIndex = index + direction; |
|
108 |
|
|
109 |
if(index < 0){ |
|
110 |
return; |
|
111 |
} |
|
112 |
|
|
113 |
if (newIndex >= 0 && newIndex < medias.size()) { |
|
114 |
try{ |
|
115 |
Collections.swap(medias, newIndex, index); |
|
116 |
}catch(ArrayIndexOutOfBoundsException e){ |
|
117 |
StoreUtil.error(this.getClass(), e); |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
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.operations; |
|
11 |
|
|
12 |
import java.util.Collections; |
|
13 |
import java.util.List; |
|
14 |
import java.util.Set; |
|
15 |
|
|
16 |
import org.eclipse.core.commands.ExecutionException; |
|
17 |
import org.eclipse.core.commands.operations.IUndoContext; |
|
18 |
import org.eclipse.core.runtime.IAdaptable; |
|
19 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
20 |
import org.eclipse.core.runtime.IStatus; |
|
21 |
|
|
22 |
import eu.etaxonomy.cdm.model.description.DescriptionBase; |
|
23 |
import eu.etaxonomy.cdm.model.description.DescriptionElementBase; |
|
24 |
import eu.etaxonomy.cdm.model.media.Media; |
|
25 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
|
26 |
import eu.etaxonomy.taxeditor.store.StoreUtil; |
|
27 |
|
|
28 |
/** |
|
29 |
* <p>MoveMediaInListOperation class.</p> |
|
30 |
* |
|
31 |
* @author p.ciardelli |
|
32 |
* @created 05.02.2009 |
|
33 |
* @version 1.0 |
|
34 |
*/ |
|
35 |
public class MoveMediaInListOperation extends AbstractPostOperation { |
|
36 |
|
|
37 |
/** Constant <code>UP=1</code> */ |
|
38 |
public static final int UP = 1; |
|
39 |
/** Constant <code>DOWN=-1</code> */ |
|
40 |
public static final int DOWN = -1; |
|
41 |
|
|
42 |
private DescriptionBase description; |
|
43 |
private Media media; |
|
44 |
private int direction; |
|
45 |
|
|
46 |
/** |
|
47 |
* <p>Constructor for MoveMediaInListOperation.</p> |
|
48 |
* |
|
49 |
* @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object. |
|
50 |
* @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object. |
|
51 |
* @param label a {@link java.lang.String} object. |
|
52 |
* @param description a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object. |
|
53 |
* @param media a {@link eu.etaxonomy.cdm.model.media.Media} object. |
|
54 |
* @param direction a int. |
|
55 |
* @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operations.IPostOperationEnabled} object. |
|
56 |
*/ |
|
57 |
public MoveMediaInListOperation(String label, IUndoContext undoContext, |
|
58 |
Taxon taxon, DescriptionBase description, Media media, |
|
59 |
int direction, IPostOperationEnabled postOperationEnabled) { |
|
60 |
super(label, undoContext, taxon, postOperationEnabled); |
|
61 |
|
|
62 |
this.description = description; |
|
63 |
this.media = media; |
|
64 |
this.direction = direction; |
|
65 |
} |
|
66 |
|
|
67 |
/* (non-Javadoc) |
|
68 |
* @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable) |
|
69 |
*/ |
|
70 |
/** {@inheritDoc} */ |
|
71 |
@Override |
|
72 |
public IStatus execute(IProgressMonitor monitor, IAdaptable info) |
|
73 |
throws ExecutionException { |
|
74 |
|
|
75 |
monitor.worked(20); |
|
76 |
|
|
77 |
moveMedia(description, media, direction); |
|
78 |
monitor.worked(40); |
|
79 |
|
|
80 |
return postExecute(media); |
|
81 |
} |
|
82 |
|
|
83 |
/* (non-Javadoc) |
|
84 |
* @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable) |
|
85 |
*/ |
|
86 |
/** {@inheritDoc} */ |
|
87 |
@Override |
|
88 |
public IStatus redo(IProgressMonitor monitor, IAdaptable info) |
|
89 |
throws ExecutionException { |
|
90 |
return execute(monitor, info); |
|
91 |
} |
|
92 |
|
|
93 |
/* (non-Javadoc) |
|
94 |
* @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable) |
|
95 |
*/ |
|
96 |
/** {@inheritDoc} */ |
|
97 |
@Override |
|
98 |
public IStatus undo(IProgressMonitor monitor, IAdaptable info) |
|
99 |
throws ExecutionException { |
|
100 |
moveMedia(description, media, direction * -1); |
|
101 |
return postExecute(media); |
|
102 |
} |
|
103 |
|
|
104 |
private void moveMedia(DescriptionBase description, Media media, int direction){ |
|
105 |
Set<DescriptionElementBase> elements = description.getElements(); |
|
106 |
|
|
107 |
if(elements.size() != 1){ |
|
108 |
StoreUtil.error(this.getClass(), "More than one description element in this image gallery", null); |
|
109 |
} |
|
110 |
|
|
111 |
DescriptionElementBase element = elements.iterator().next(); |
|
112 |
|
|
113 |
List<Media> medias = element.getMedia(); |
|
114 |
|
|
115 |
int index = medias.indexOf(media); |
|
116 |
int newIndex = index + direction; |
|
117 |
|
|
118 |
if(index < 0){ |
|
119 |
return; |
|
120 |
} |
|
121 |
|
|
122 |
if (newIndex >= 0 && newIndex < medias.size()) { |
|
123 |
try{ |
|
124 |
Collections.swap(medias, newIndex, index); |
|
125 |
}catch(ArrayIndexOutOfBoundsException e){ |
|
126 |
StoreUtil.error(this.getClass(), e); |
|
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
} |
Also available in: Unified diff
performed javacscript:fix and worked on documentation