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.JList;
20  import javax.swing.KeyStroke;
21  import javax.swing.ListSelectionModel;
22  
23  import org.abstracthorizon.aequo.CompareEntry;
24  import org.abstracthorizon.aequo.text.TextComparePanel;
25  import org.abstracthorizon.aequo.text.TextModel;
26  
27  /**
28   * Scrolls to previous difference section in the file
29   *
30   * @author Daniel Sendula
31   */
32  public class PreviousDifferenceAction extends TextBaseAction {
33  
34      /** List */
35      protected TextComparePanel panel;
36      
37      /**
38       * Constructor
39       */
40      public PreviousDifferenceAction() {
41          setName("Previous difference");
42          setMessage("Previous difference");
43          setDescription("Previous difference");
44          putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_UP, ActionEvent.CTRL_MASK));
45      }
46      
47      /**
48       * Sets the link to text compare panel
49       * @param panel text compare panel
50       */
51      public void setTextComparePanel(TextComparePanel panel) {
52          this.panel = panel;
53      }
54      
55      /**
56       * Scrolls to previous difference section in the file
57       * 
58       * @param e event 
59       */
60      public void perform(ActionEvent e) {
61  
62          TextModel model = getTextModel();        
63          ListSelectionModel selModel = model.getSelectionModel();
64          int i = selModel.getLeadSelectionIndex();
65          if (i > 0) {
66              while ((i >= 0) && (model.get(i).getStatus(0) != CompareEntry.EQUAL)) {
67                  i--;
68              }
69              if (i < 0) {
70                  return;
71              }
72  
73              while ((i >= 0) && (model.get(i).getStatus(0) == CompareEntry.EQUAL)) {
74                  i--;
75              }
76              if (i < 0) {
77                  return;
78              }
79              
80              while ((i >= 0) && (model.get(i).getStatus(0) != CompareEntry.EQUAL)) {
81                  i--;
82              }
83              i++;
84  
85              JList list = ((JList)panel.getLeftList());
86  
87              list.getSelectionModel().setSelectionInterval(i, i);
88              if (i < 10) {
89                  i = 0;
90              } else {
91                  i = i - 10;
92              }
93              list.ensureIndexIsVisible(i);
94          }
95      }
96      
97      
98  }