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.text.TextCompareEntry;
23  import org.abstracthorizon.aequo.text.TextModel;
24  
25  /**
26   * Delete from one side action.
27   *
28   * @author Daniel Sendula
29   */
30  public class DeleteAction extends TextBaseAction {
31  
32      /**
33       * Constructor
34       */
35      public DeleteAction() {
36          setName("Delete");
37          setMessage("Delete");
38          setDescription("Delete");
39          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
40          putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, ActionEvent.CTRL_MASK));
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) && isSelectionValid();
49      }
50  
51      /**
52       * Invokes action
53       * @param e action event
54       */
55      public void perform(ActionEvent e) {
56          TextModel model = getTextModel();
57          ListSelectionModel selModel = model.getSelectionModel();
58          int min = selModel.getMinSelectionIndex();
59          int max = selModel.getMaxSelectionIndex();
60          selModel.setValueIsAdjusting(true);
61          try {
62              for (int i = max; i >= min; i--) {
63                  TextCompareEntry entry = model.get(i);
64                  String d = entry.getData(column);
65                  if (d != null) {
66                      int j = 0;
67                      Object[] data = entry.getData();
68                      while ((j < data.length) && ((j == column) || (j != column) && (data[j] == null))) {
69                          j++;
70                      }
71                      // TODO UNDO
72                      if (j == data.length) {
73                          model.removeImpl(i);
74                      } else {
75                          entry.setData(column, null);
76                      }
77                      model.setFileDirty(column);
78                  }
79              }
80              model.updateLineNumbers(min, -1);
81              model.notifyUpdate(min, max);
82          } finally {
83              selModel.setValueIsAdjusting(false);
84          }
85      }
86  }