Coverage Report - org.jtheque.primary.view.impl.choice.AbstractDeleteChoiceAction
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDeleteChoiceAction
0 %
0/15
0 %
0/8
2
 
 1  
 package org.jtheque.primary.view.impl.choice;
 2  
 
 3  
 import org.jtheque.core.managers.Managers;
 4  
 import org.jtheque.core.managers.persistence.able.Entity;
 5  
 import org.jtheque.core.managers.undo.IUndoRedoManager;
 6  
 import org.jtheque.core.managers.view.able.IViewManager;
 7  
 import org.jtheque.core.utils.CoreUtils;
 8  
 
 9  
 import javax.swing.undo.UndoableEdit;
 10  
 import java.util.ArrayList;
 11  
 import java.util.Arrays;
 12  
 import java.util.Collection;
 13  
 
 14  
 /*
 15  
  * This file is part of JTheque.
 16  
  *            
 17  
  * JTheque is free software: you can redistribute it and/or modify
 18  
  * it under the terms of the GNU General Public License as published by
 19  
  * the Free Software Foundation, either version 3 of the License. 
 20  
  *
 21  
  * JTheque is distributed in the hope that it will be useful,
 22  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 23  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 24  
  * GNU General Public License for more details.
 25  
  *
 26  
  * You should have received a copy of the GNU General Public License
 27  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 28  
  */
 29  
 
 30  
 /**
 31  
  * An abstract delete action for the choice view.
 32  
  *
 33  
  * @author Baptiste Wicht
 34  
  */
 35  0
 public abstract class AbstractDeleteChoiceAction extends AbstractChoiceAction {
 36  0
         private final Collection<Deleter<? extends Entity>> deleters = new ArrayList<Deleter<? extends Entity>>(10);
 37  
 
 38  
         /**
 39  
          * Set the deleters to use to execute the action.
 40  
          *
 41  
          * @param deleters The deleters to use.
 42  
          */
 43  
         protected final void addDeleters(Deleter<? extends Entity>... deleters){
 44  0
                 this.deleters.addAll(Arrays.asList(deleters));
 45  0
         }
 46  
 
 47  
         @Override
 48  
         public final boolean canDoAction(String action){
 49  0
                 return "delete".equals(action);
 50  
         }
 51  
 
 52  
     @Override
 53  
     public void execute() {
 54  0
         final boolean yes = Managers.getManager(IViewManager.class).askUserForConfirmation(
 55  
                 CoreUtils.getMessage("choice.dialogs.delete") + ' ' + getSelectedItem().toString(),
 56  
                 CoreUtils.getMessage("choice.dialogs.delete.title"));
 57  
 
 58  0
         if (yes) {
 59  0
                         for (Deleter<? extends Entity> deleter : deleters){
 60  0
                                 if (deleter.canDelete(getContent())){
 61  0
                                         deleter.delete(getSelectedItem());
 62  
 
 63  0
                                         break;
 64  
                                 }
 65  
                         }
 66  
                 }
 67  0
     }
 68  
 
 69  
         /**
 70  
          * Add an edit to undo-redo manager if deleted.
 71  
          *
 72  
          * @param deleted The boolean tag of the delete operation.
 73  
          * @param edit The undoable edit.
 74  
          */
 75  
         protected static void addEditIfDeleted(boolean deleted, UndoableEdit edit){
 76  0
                 if (deleted){
 77  0
                         Managers.getManager(IUndoRedoManager.class).addEdit(edit);
 78  
                 }
 79  0
         }
 80  
 }