1
2
3
4
5
6
7
8
9
10
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
36
37
38
39 public class CopyToAction extends FileBaseAction {
40
41
42
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
55
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
68
69
70 public boolean isEnabled() {
71 return (column >= 0) && isSelectionValid();
72 }
73
74
75
76
77
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
155
156
157
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 }