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   * Move to other side action.
31   *
32   * @author Daniel Sendula
33   */
34  public class MoveToAction extends FileBaseAction {
35  
36      public MoveToAction() {
37          setConfirmRequired(true);
38          setDirectionalAction(true);
39          setName("{0} Move to {1}");
40          setMessage("Do you want to move files/folders to other side?");
41          setDescription("Moving files and/or folders");
42          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_M);
43      }
44  
45      /**
46       * Sets the column and updates the accelerator keys
47       * @param column column
48       */
49      public void setColumn(int column) {
50          super.setColumn(column);
51          if (column == 0) {
52              putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
53          } else {
54              putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
55          }
56      }
57  
58      /**
59       * Returns if selection is valid
60       * @return <code>true</code> if selection is valid<
61       */
62      public boolean isEnabled() {
63          return (column >= 0) && isSelectionValid();
64      }
65      
66      /**
67       * Moves files from one side to another
68       * 
69       * @param e event 
70       * @param monitor monitor
71       */
72      public void perform(ActionEvent e, ProgressMonitor monitor) {
73          int failed = 0;
74          StringWriter failedDetails = new StringWriter();
75  
76          FilesModel model = getFilesModel();
77          
78          int first = 0;
79          int last = -1;
80          FileCompareEntry[] entries = getPreselectedEntries();
81          if (entries.length > 0) {
82              first = entries[0].getIndex();
83              int i = entries.length - 1;
84              while ((i >= 0) && (last < 0)) {
85                  last = entries[i].getIndex();
86                  i--;
87              }
88          }
89          monitor.setMaximum(entries.length);
90  
91          ListSelectionModel selModel = model.getSelectionModel();
92  
93          selModel.setValueIsAdjusting(true);
94          try {
95              int i = 0;
96              while (i < entries.length) {
97                  FileCompareEntry entry = entries[i];
98                  File[] data = entry.getData();
99                  File fromFile = data[column];
100                 File toFile = data[destColumn];
101                 monitor.setNote(fromFile.getAbsolutePath() + " to " + toFile.getAbsolutePath());
102                 if (fromFile.exists()) {
103                     if (fromFile.isDirectory()) {
104                         boolean err = false;
105                         if (toFile.exists() && !toFile.isDirectory() && !toFile.delete()) {
106                             failed = failed + 1;
107                             failedDetails.append("Failed to delete ").append(toFile.getAbsolutePath());
108                             err = true;
109                         }
110                         
111                         if (!err && !toFile.exists()) {
112                             if (!toFile.mkdirs()) {
113                                 failed = failed + 1;
114                                 failedDetails.append("Failed to created folder ").append(toFile.getAbsolutePath());
115                             } else {
116                                 toFile.setLastModified(fromFile.lastModified());
117                             }
118                         } else if (!err) {
119                             toFile.setLastModified(fromFile.lastModified());
120                         }
121                     } else {
122                         if (!fromFile.renameTo(toFile)) {
123                             failed = failed + 1;
124                             failedDetails.append("Failed creating directory ").append(toFile.getAbsolutePath());
125                         }
126                     }
127                     entry.updateEntryStatus();
128                 }
129                 monitor.setProgress(i);
130                 i++;
131             }
132             if (entries.length > 0) {
133                 model.notifyOfChange(first, last);
134             }
135         } finally {
136             selModel.setValueIsAdjusting(false);
137         }
138         if (failed > 0) {
139             JOptionPane.showInternalMessageDialog(null, failedDetails.toString(), "Problem deleting files", JOptionPane.WARNING_MESSAGE, null);
140         }
141     }
142 }