Project

General

Profile

« Previous | Next » 

Revision 4da7c81b

Added by Katja Luther about 3 years ago

ref #8198: show media delete config wizard only for galleries with media

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/media/handler/DeleteMediaHandler.java
3 3
 */
4 4
package eu.etaxonomy.taxeditor.editor.view.media.handler;
5 5

  
6
import java.util.ArrayList;
6 7
import java.util.Iterator;
8
import java.util.List;
7 9
import java.util.Set;
8 10

  
9 11
import javax.inject.Named;
......
68 70
                    break;
69 71
                }
70 72
            }
71
            // TODO use undo context specific to editor
72
            MediaDeletionConfigurator config = new MediaDeletionConfigurator();
73

  
74
            config.setDeleteFromDescription(true);
75
            config.setOnlyRemoveFromGallery(false);
76

  
77
            if (description instanceof SpecimenDescription){
78
                config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
79
            } else if (description instanceof TaxonDescription){
80
                config.setDeleteFrom(((TaxonDescription)description).getTaxon());
81
            }else if (description instanceof TaxonNameDescription){
82
                config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
83
            }
84
            DeleteConfiguratorDialog dialog;
85
            dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM,  null,  Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
86
            int result_dialog= dialog.open();
87
            if (result_dialog != IStatus.OK){
88
                return;
89
            }
90
            if(object instanceof Media){
91
                AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, (Media) object, config, mediaView);
92
                if (mediaView.getSelectionProvidingPart().getObject() instanceof TaxonNameEditorE4){
93
                    if (!config.isOnlyRemoveFromGallery()){
94
                        ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).getEditorInput().addOperation(operation);
95
                    }
96
                    Media media = (Media)object;
97
                    if (description.isImageGallery()){
98
                        Set<DescriptionElementBase> elements = description.getElements();
99
                        if (elements.size() == 1){
100
                              DescriptionElementBase element = elements.iterator().next();
101
                              element.removeMedia(media);
102
                              if (element.getMedia().isEmpty()){
103
                                  description.removeElement(element);
104
                              }
105
                        }
106
                    }
107 73

  
108
                  ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).setDirty();
109
                  ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).redraw();
110
                }else{
111
                    AbstractUtility.executeOperation(operation, sync);
74
            if(object instanceof Media){
75
                // TODO use undo context specific to editor
76
                MediaDeletionConfigurator config = fillDeleteConfigurator(shell, description, Messages.DeleteMediaHandler_CONFIRM_MESSAGE_MEDIA);
77
                if (config == null){
78
                    return;
112 79
                }
80
                List<Media> mediaList = new ArrayList<>();
81
                mediaList.add((Media)object);
82
                createMediaDeleteOperation(menuItem.getLocalizedLabel(), sync, mediaView, mediaList, description, config);
113 83
            }
114 84
            if(object instanceof DescriptionBase && ((DescriptionBase) object).isImageGallery()){
85
                if (!((DescriptionBase)object).getElements().isEmpty()){
86
                    MediaDeletionConfigurator config = fillDeleteConfigurator(shell, description, Messages.DeleteMediaHandler_CONFIRM_MESSAGE_DESCRIPTION);
87
                    if (config == null){
88
                        return;
89
                    }
90
                    Iterator<DescriptionElementBase> elements = ((DescriptionBase)object).getElements().iterator();
91
                    DescriptionElementBase element = elements.next();
92
                    createMediaDeleteOperation(menuItem.getLocalizedLabel(), sync, mediaView, element.getMedia(), description, config);
93
                }
94

  
115 95
                if(object instanceof TaxonDescription){
96

  
116 97
                    DeleteTaxonDescriptionOperation deleteTaxonDescriptionOperation = new DeleteTaxonDescriptionOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), (TaxonDescription)object, mediaView, null);
117 98
                    if (mediaView.getSelectionProvidingPart().getObject() instanceof TaxonNameEditorE4){
99
                        ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).getTaxon().removeDescription((TaxonDescription)description);
118 100
                        ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).getEditorInput().addOperation(deleteTaxonDescriptionOperation);
119 101
                        ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).setDirty();
120 102
                        ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).redraw();
......
137 119
        }
138 120
    }
139 121

  
122
    public void createMediaDeleteOperation(String menuLabel, UISynchronize sync, MediaViewPartE4 mediaView,
123
            List<Media> media, DescriptionBase description, MediaDeletionConfigurator config) {
124
        AbstractPostOperation<?> operation = new DeleteMediaOperation(menuLabel, EditorUtil.getUndoContext(), description, media, config, mediaView);
125
        if (mediaView.getSelectionProvidingPart().getObject() instanceof TaxonNameEditorE4){
126
            if (!config.isOnlyRemoveFromGallery()){
127
                ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).getEditorInput().addOperation(operation);
128
            }
129

  
130
            if (description.isImageGallery()){
131
                Set<DescriptionElementBase> elements = description.getElements();
132
                if (elements.size() == 1){
133
                      DescriptionElementBase element = elements.iterator().next();
134
                      for (Media mediaElement:media){
135
                          element.removeMedia(mediaElement);
136
                      }
137
                      if (element.getMedia().isEmpty()){
138
                          description.removeElement(element);
139
                      }
140
                }
141
            }
142

  
143
          ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).setDirty();
144
          ((TaxonNameEditorE4)mediaView.getSelectionProvidingPart().getObject()).redraw();
145
        }else{
146
            AbstractUtility.executeOperation(operation, sync);
147
        }
148
    }
149

  
150
    public MediaDeletionConfigurator fillDeleteConfigurator(Shell shell, DescriptionBase description, String message) {
151
        MediaDeletionConfigurator config = new MediaDeletionConfigurator();
152

  
153
        config.setDeleteFromDescription(true);
154
        config.setOnlyRemoveFromGallery(false);
155

  
156
        if (description instanceof SpecimenDescription){
157
            config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
158
        } else if (description instanceof TaxonDescription){
159
            config.setDeleteFrom(((TaxonDescription)description).getTaxon());
160
        }else if (description instanceof TaxonNameDescription){
161
            config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
162
        }
163
        DeleteConfiguratorDialog dialog;
164
        dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM,  null,  message, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
165
        int result_dialog= dialog.open();
166
        if (result_dialog != IStatus.OK){
167
            return null;
168
        }
169
        return config;
170
    }
171

  
140 172
    @CanExecute
141 173
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
142 174
            MHandledMenuItem menuItem){
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/media/operation/DeleteMediaOperation.java
9 9
package eu.etaxonomy.taxeditor.editor.view.media.operation;
10 10

  
11 11
import java.util.ArrayList;
12
import java.util.HashSet;
12 13
import java.util.List;
14
import java.util.Set;
15
import java.util.UUID;
13 16

  
14 17
import org.eclipse.core.commands.ExecutionException;
15 18
import org.eclipse.core.commands.operations.IUndoContext;
......
37 40
 */
38 41
public class DeleteMediaOperation extends AbstractPostTaxonOperation {
39 42

  
40
	private final Media media;
43
	private final List<Media> media;
41 44

  
42 45
	private final DescriptionBase<?> description;
43 46

  
......
53 56
	 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
54 57
	 */
55 58
	public DeleteMediaOperation(String label, IUndoContext undoContext,
56
	        DescriptionBase<?> description, Media media, MediaDeletionConfigurator config, IPostOperationEnabled postOperationEnabled) {
59
	        DescriptionBase<?> description, List<Media> media, MediaDeletionConfigurator config, IPostOperationEnabled postOperationEnabled) {
57 60
		super(label, undoContext, postOperationEnabled);
58 61
		this.config = config;
59 62
		this.media = media;
60 63
		this.description = description;
61 64
	}
62 65

  
66
	public DeleteMediaOperation(String label, IUndoContext undoContext,
67
            DescriptionBase<?> description, Media media, MediaDeletionConfigurator config, IPostOperationEnabled postOperationEnabled) {
68
        super(label, undoContext, postOperationEnabled);
69
        this.config = config;
70
        this.media = new ArrayList<>();
71
        this.media.add(media);
72
        this.description = description;
73
    }
74

  
75

  
63 76
	@Override
64 77
	public IStatus execute(IProgressMonitor monitor, IAdaptable info)
65 78
			throws ExecutionException {
......
68 81
	    if (monitor != null) {
69 82
            monitor.worked(20);
70 83
        }
71
		if (!config.isOnlyRemoveFromGallery() && media.getId() != 0){
72
			DeleteResult result = CdmStore.getService(IMediaService.class).delete(media.getUuid(), config);
84
		if (!config.isOnlyRemoveFromGallery() ){
85
		    Set<UUID> toDelete= new HashSet<>();
86
		    for (Media mediaElement: media){
87
		        if (mediaElement.getId() != 0){
88
		            toDelete.add(mediaElement.getUuid());
89
		        }
90
		    }
91

  
92
			DeleteResult result = CdmStore.getService(IMediaService.class).delete(toDelete, config);
73 93
			String errorMessage = "The media ";
74 94

  
75 95
			if (!result.isOk()){
......
100 120
	@Override
101 121
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
102 122
			throws ExecutionException {
103

  
104
		ImagesUtility.addMediaToGallery(description, media);
123
	    for (Media mediaElement: media){
124
	        ImagesUtility.addMediaToGallery(description, mediaElement);
125
	    }
105 126

  
106 127
		return postExecute(null);
107 128
	}

Also available in: Unified diff