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.text.action;
14  
15  import java.awt.event.ActionEvent;
16  import java.awt.event.KeyEvent;
17  
18  import javax.swing.Action;
19  import javax.swing.KeyStroke;
20  import javax.swing.ListSelectionModel;
21  
22  import org.abstracthorizon.aequo.CompareEntry;
23  import org.abstracthorizon.aequo.text.TextModel;
24  
25  /**
26   * Copy to other side action.
27   *
28   * @author Daniel Sendula
29   */
30  public class CopyToAction extends TextBaseAction {
31  
32      public CopyToAction() {
33          setDirectionalAction(true);
34          setName("{0} Copy To {1}");
35          setMessage("Copy To");
36          setDescription("Copy To");
37          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);
38      }
39  
40      /**
41       * Returns if selection is valid
42       * @return <code>true</code> if selection is valid<
43       */
44      public boolean isEnabled() {
45          return (column >= 0) && isSelectionValid();
46      }
47  
48      /**
49       * Sets the column and updates the accelerator keys
50       * @param column column
51       */
52      public void setColumn(int column) {
53          super.setColumn(column);
54          if (column == 0) {
55              putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK));
56          } else {
57              putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ActionEvent.CTRL_MASK));
58          }
59      }
60      
61      /**
62       * Copies lines
63       * @param e event
64       */
65      public void perform(ActionEvent e) {
66          TextModel model = getTextModel();
67          ListSelectionModel selModel = model.getSelectionModel();
68          int min = selModel.getMinSelectionIndex();
69          int max = selModel.getMaxSelectionIndex();
70          selModel.setValueIsAdjusting(true);
71          try {
72              for (int i = max; i >= min; i--) {
73                  CompareEntry<String> entry = model.get(i);
74                  String source = entry.getData(column);
75                  String dest = entry.getData(destColumn);
76                  if ((source != dest) && ((source == null) || !source.equals(dest))) {
77                      entry.setData(destColumn, source);
78                      // TODO UNDO 
79                      model.setFileDirty(destColumn);
80                  }
81              }
82              model.notifyUpdate(min, max);
83          } finally {
84              selModel.setValueIsAdjusting(false);
85          }
86      }
87  }
88