View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.file.action;
14  
15  import java.awt.event.ActionEvent;
16  import java.awt.event.KeyEvent;
17  import java.io.File;
18  import java.io.StringWriter;
19  
20  import javax.swing.Action;
21  import javax.swing.JOptionPane;
22  import javax.swing.KeyStroke;
23  import javax.swing.ListSelectionModel;
24  import javax.swing.ProgressMonitor;
25  
26  import org.abstracthorizon.aequo.file.FileCompareEntry;
27  import org.abstracthorizon.aequo.file.FilesModel;
28  
29  /**
30   * Delete files from selected side action.
31   *
32   * @author Daniel Sendula
33   */
34  public class DeleteAction extends FileBaseAction {
35  
36      /**
37       * Constructor
38       */
39      public DeleteAction() {
40          setConfirmRequired(true);
41          setName("Delete");
42          setMessage("Do you want to delete files/folders?");
43          setDescription("Deleting files and/or folders");
44          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
45          putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, ActionEvent.CTRL_MASK));
46      }
47  
48      /**
49       * Returns if selection is valid
50       * @return <code>true</code> if selection is valid<
51       */
52      public boolean isEnabled() {
53          return isSelectionValid();
54      }
55  
56      /**
57       * Deletes files from one column
58       * 
59       * @param e event 
60       * @param monitor monitor
61       */
62      public void perform(ActionEvent e, ProgressMonitor monitor) {
63          int failed = 0;
64          StringWriter failedDetails = null;
65  
66          FilesModel model = getFilesModel();
67          
68          int first = 0;
69          int last = -1;
70          FileCompareEntry[] entries = getPreselectedEntries();
71          if (entries.length > 0) {
72              first = entries[0].getIndex();
73              int i = entries.length - 1;
74              while ((i >= 0) && (last < 0)) {
75                  last = entries[i].getIndex();
76                  i--;
77              }
78          }
79  
80          monitor.setMaximum(entries.length);
81  
82          ListSelectionModel selModel = model.getSelectionModel();
83  
84          selModel.setValueIsAdjusting(true);
85          try {
86              int i = entries.length - 1;
87              while (i >= 0) {
88                  FileCompareEntry entry = entries[i];
89                  File[] data = entry.getData();
90                  File file = data[column];
91                  monitor.setNote(file.getAbsolutePath());
92                  if (file.delete()) {
93                      boolean remove = true;
94                      int j = 0;
95                      while (remove && (j < data.length)) {
96                          if (j != column) {
97                              remove = !data[j].exists();
98                          }
99                          j++;
100                     }
101                     if (remove) {
102                         model.remove(entry);
103                     }
104                 } else {
105                     if (!file.isDirectory()) {
106                         failed = failed + 1;
107                         if (failedDetails == null) {
108                             failedDetails = new StringWriter();
109                         }
110                         failedDetails.append(file.getAbsolutePath()).append('\n');
111                     }
112                 }
113                 entry.updateEntryStatus();
114                 monitor.setProgress(i);
115                 i--;
116             }
117             if (entries.length > 0) {
118                 model.notifyOfRemoved(first, last);
119             }
120         } finally {
121             selModel.setValueIsAdjusting(false);
122         }
123         if (failedDetails != null) {
124             JOptionPane.showInternalMessageDialog(null, failedDetails.toString(), "Problem deleting files", JOptionPane.WARNING_MESSAGE, null);
125         }
126     }
127 }