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   * Sets date to same as on other side
31   *
32   * @author Daniel Sendula
33   */
34  public class TouchAction extends FileBaseAction {
35  
36      /**
37       * Constructor
38       */
39      public TouchAction() {
40          setConfirmRequired(true);
41          setDirectionalAction(true);
42          setName("Touch");
43          setMessage("Do you want to copy date from other side?");
44          setDescription("Copying date");
45          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_T);
46          putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
47      }
48  
49      /**
50       * Returns if selection is valid
51       * @return <code>true</code> if selection is valid<
52       */
53      public boolean isEnabled() {
54          return (column >= 0) && isSelectionValid();
55      }
56      
57      /**
58       * Copies date from other side
59       * @param e event
60       * @param monitor monitor
61       */
62      public void perform(ActionEvent e, ProgressMonitor monitor) {
63          int failed = 0;
64          StringWriter failedDetails = new StringWriter();
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 = 0;
87              while (i < entries.length) {
88                  FileCompareEntry entry = entries[i];
89                  File[] data = entry.getData();
90                  File fromFile = data[column];
91                  File toFile = data[destColumn];
92                  monitor.setNote(fromFile.getAbsolutePath() + " to " + toFile.getAbsolutePath());
93                  if (fromFile.exists() && toFile.exists()) {
94                      toFile.setLastModified(fromFile.lastModified());
95                      entry.updateEntryStatus();
96                  }
97                  monitor.setProgress(i);
98                  i++;
99              }
100             if (entries.length > 0) {
101                 model.notifyOfChange(first, last);
102             }
103         } finally {
104             selModel.setValueIsAdjusting(false);
105         }
106         if (failed > 0) {
107             JOptionPane.showMessageDialog(null, failedDetails.toString(), "Problem deleting files", JOptionPane.WARNING_MESSAGE, null);
108         }
109     }
110 }