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.FileInputStream;
19  import java.io.FileOutputStream;
20  import java.io.IOException;
21  import java.io.StringWriter;
22  import java.nio.MappedByteBuffer;
23  import java.nio.channels.FileChannel;
24  
25  import javax.swing.Action;
26  import javax.swing.JOptionPane;
27  import javax.swing.KeyStroke;
28  import javax.swing.ListSelectionModel;
29  import javax.swing.ProgressMonitor;
30  
31  import org.abstracthorizon.aequo.file.FileCompareEntry;
32  import org.abstracthorizon.aequo.file.FilesModel;
33  
34  /**
35   * Copy to other side action.
36   *
37   * @author Daniel Sendula
38   */
39  public class CopyToAction extends FileBaseAction {
40  
41      /**
42       * Constructor
43       */
44      public CopyToAction() {
45          setConfirmRequired(true);
46          setDirectionalAction(true);
47          setName("{0} Copy to {1}");
48          setMessage("Do you want to copy files/folders to other side?");
49          setDescription("Copying files and/or folders");
50          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);
51      }
52  
53      /**
54       * Sets the column and updates the accelerator keys
55       * @param column column
56       */
57      public void setColumn(int column) {
58          super.setColumn(column);
59          if (column == 0) {
60              putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK));
61          } else {
62              putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ActionEvent.CTRL_MASK));
63          }
64      }
65  
66      /**
67       * Returns if selection is valid
68       * @return <code>true</code> if selection is valid<
69       */
70      public boolean isEnabled() {
71          return (column >= 0) && isSelectionValid();
72      }
73      
74      /**
75       * Copies files
76       * @param e event
77       * @param monitor monitor
78       */
79      public void perform(ActionEvent e, ProgressMonitor monitor) {
80          int failed = 0;
81          StringWriter failedDetails = new StringWriter();
82  
83          FilesModel model = getFilesModel();
84          
85          int first = 0;
86          int last = -1;
87          FileCompareEntry[] entries = getPreselectedEntries();
88          if (entries.length > 0) {
89              first = entries[0].getIndex();
90              int i = entries.length - 1;
91              while ((i >= 0) && (last < 0)) {
92                  last = entries[i].getIndex();
93                  i--;
94              }
95          }
96  
97          monitor.setMaximum(entries.length);
98  
99          ListSelectionModel selModel = model.getSelectionModel();
100 
101         selModel.setValueIsAdjusting(true);
102         try {
103             int i = 0;
104             while (i < entries.length) {
105                 FileCompareEntry entry = entries[i];
106                 File[] data = entry.getData();
107                 File fromFile = data[column];
108                 File toFile = data[destColumn];
109                 monitor.setNote(fromFile.getAbsolutePath() + " to " + toFile.getAbsolutePath());
110                 if (fromFile.exists()) {
111                     if (fromFile.isDirectory()) {
112                         boolean err = false;
113                         if (toFile.exists() && !toFile.isDirectory() && !toFile.delete()) {
114                             failed = failed + 1;
115                             failedDetails.append("Failed to delete ").append(toFile.getAbsolutePath());
116                             err = true;
117                         }
118                         
119                         if (!err && !toFile.exists()) {
120                             if (!toFile.mkdirs()) {
121                                 failed = failed + 1;
122                                 failedDetails.append("Failed to created folder ").append(toFile.getAbsolutePath());
123                             } else {
124                                 toFile.setLastModified(fromFile.lastModified());
125                             }
126                         } else if (!err) {
127                             toFile.setLastModified(fromFile.lastModified());
128                         }
129                     } else {
130                         if (!copyFile(fromFile, toFile)) {
131                             failed = failed + 1;
132                             failedDetails.append("Failed creating directory ").append(toFile.getAbsolutePath());
133                         } else {
134                             toFile.setLastModified(fromFile.lastModified());
135                         }
136                     }
137                     entry.updateEntryStatus();
138                 }
139                 monitor.setProgress(i);
140                 i++;
141             }
142             if (entries.length > 0) {
143                 model.notifyOfChange(first, last);
144             }
145         } finally {
146             selModel.setValueIsAdjusting(false);
147         }
148         if (failed > 0) {
149             JOptionPane.showMessageDialog(null, failedDetails.toString(), "Problem deleting files", JOptionPane.WARNING_MESSAGE, null);
150         }
151     }
152 
153     /**
154      * Copies files and directories
155      * @param fromFile source file/dir
156      * @param toFile destination file/dir
157      * @return
158      */
159     public static boolean copyFile(File fromFile, File toFile) {
160         try {
161             FileInputStream fromInputStream = new FileInputStream(fromFile);
162             try {
163                 FileOutputStream toOutputStream = new FileOutputStream(toFile);
164                 try {
165                     FileChannel inChannel = fromInputStream.getChannel();
166                     try {
167                         FileChannel outChannel = toOutputStream.getChannel();
168                         try {
169                             MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
170             
171                             outChannel.write(buffer);
172                         } finally {
173                             outChannel.close();
174                         }
175                     } finally {
176                         inChannel.close();
177                     }
178                 } finally {
179                     toOutputStream.close();
180                 }
181             } finally {
182                 fromInputStream.close();
183             }
184             return true;
185         } catch (IOException exc) {
186             return false;
187         }
188     }
189 }