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 next difference section in the file
29   *
30   * @author Daniel Sendula
31   */
32  public class NextDifferenceAction extends TextBaseAction {
33  
34      /** List */
35      protected TextComparePanel panel;
36      
37      /**
38       * Constructor
39       */
40      public NextDifferenceAction() {
41          setName("Next difference");
42          setMessage("Next difference");
43          setDescription("Next difference");
44          putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 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 next 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              int size = model.getSize();
67              while ((i < size) && (model.get(i).getStatus(0) != CompareEntry.EQUAL)) {
68                  i++;
69              }
70              if (i == size) {
71                  return;
72              }
73  
74              while ((i < size) && (model.get(i).getStatus(0) == CompareEntry.EQUAL)) {
75                  i++;
76              }
77              if (i == size) {
78                  return;
79              }
80              
81              JList list = ((JList)panel.getLeftList());
82  
83              list.getSelectionModel().setSelectionInterval(i, i);
84              if (i > size - 10) {
85                  i = size;
86              } else {
87                  i = i + 10;
88              }
89              list.ensureIndexIsVisible(i);
90          }
91      }
92      
93      
94  }