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.util.ArrayList;
17  import java.util.List;
18  
19  import javax.swing.ListSelectionModel;
20  
21  import org.abstracthorizon.aequo.text.TextCompareEntry;
22  import org.abstracthorizon.aequo.text.TextModel;
23  
24  /**
25   * Rescans selection
26   *
27   * @author Daniel Sendula
28   */
29  public class SplitAction extends TextBaseAction {
30  
31      /**
32       * Constructor
33       */
34      public SplitAction() {
35          setDirectionalAction(true);
36          setName("Split");
37          setMessage("Split");
38          setDescription("Split");
39  //        putValue(Action.MNEMONIC_KEY, KeyEvent.VK_L);
40  //        putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0));
41      }
42  
43      /**
44       * Returns if selection is valid
45       * @return <code>true</code> if selection is valid<
46       */
47      public boolean isEnabled() {
48          return (column >= 0) && isContinuousSelected();
49      }
50  
51      /**
52       * Invokes action
53       * @param e action event
54       */
55      public void perform(ActionEvent e) {
56          TextModel model = getTextModel();
57  
58          ListSelectionModel selModel = model.getSelectionModel();
59          int min = selModel.getMinSelectionIndex();
60          int max = selModel.getMaxSelectionIndex();
61          selModel.setValueIsAdjusting(true);
62          try {
63              int max2 = max - min + 1 + max;
64              
65              int startLineNumber = model.get(min).getLineNumber(destColumn);
66              int endLineNumber = model.get(max).getLineNumber(column);
67              
68              List<TextCompareEntry> newEntries = new ArrayList<TextCompareEntry>();
69              for (int i = max; i >= min; i--) {
70                  TextCompareEntry entry = model.get(i);
71                  String[] data = entry.getData();
72                  String[] newData = new String[2];
73                  newData[destColumn] = data[destColumn];
74                  newData[column] = null;
75                  data[destColumn] = null;
76                  
77                  int[] lineNumbers = entry.getLineNumbers();
78                  int[] newLineNumbers = new int[2];
79                  newLineNumbers[destColumn] = lineNumbers[destColumn];
80                  newLineNumbers[column] = endLineNumber;
81                  lineNumbers[destColumn] = startLineNumber;
82                  
83                  if (newData[destColumn] == null) {
84                      max2 = max2 - 1;
85                  } else {
86                      TextCompareEntry newEntry = new TextCompareEntry(newData, newLineNumbers);
87                      newEntry.updateEntryStatus();
88                      newEntries.add(newEntry);
89                  }
90                  entry.updateEntryStatus();
91                  if (data[column] == null) {
92                      model.removeImpl(i);
93                      max = max - 1;
94                      max2 = max2 - 1;
95                  }
96              }
97              int i = max + 1;
98              for (TextCompareEntry entry : newEntries) {
99                  model.addImpl(i, entry);
100             }
101             model.notifyUpdate(min, max2);
102             selModel.addSelectionInterval(max, max2);
103         } finally {
104             selModel.setValueIsAdjusting(false);
105         }
106     }
107 }