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 javax.swing.ListSelectionModel;
16  
17  import org.abstracthorizon.aequo.action.BaseAction;
18  import org.abstracthorizon.aequo.text.TextModel;
19  
20  /**
21   * Base action for operations on text model
22   * 
23   * @author Daniel Sendula
24   */
25  public abstract class TextBaseAction extends BaseAction {
26      
27      /** Model */
28      protected TextModel textModel;
29      
30      /**
31       * Constructor
32       */
33      public TextBaseAction() {
34      }
35  
36      /**
37       * Sets text model
38       * @param model text model
39       */
40      public void setTextModel(TextModel model) {
41          this.textModel = model;
42      }
43      
44      /**
45       * Returns text model
46       * @return text model
47       */
48      public TextModel getTextModel() {
49          return textModel;
50      }
51      
52      
53      /**
54       * Returns if exactly one row is selected
55       * @return <code>true</code> if exactly one row is selected
56       */
57      public boolean isOneSelected() {
58          ListSelectionModel selModel = getTextModel().getSelectionModel();
59          int min = selModel.getMinSelectionIndex();
60          int max = selModel.getMaxSelectionIndex();
61          return ((min >= 0) && (min == max));
62      }
63      
64      /**
65       * Returns if exactly one row is selected
66       * @return <code>true</code> if exactly one row is selected
67       */
68      public boolean isContinuousSelected() {
69          ListSelectionModel selModel = getTextModel().getSelectionModel();
70          int min = selModel.getMinSelectionIndex();
71          int max = selModel.getMaxSelectionIndex();
72          if ((min >= 0) && (min <= max)) {
73              boolean notInterrupted = true;
74              for (int i = min; (i < max) && notInterrupted; i++) {
75                  notInterrupted = selModel.isSelectedIndex(i);
76              }
77              return notInterrupted;
78          } else {
79              return false;
80          }
81      }
82  
83      /**
84       * Returns if selection is valid
85       * @return <code>true</code> if selection is valid
86       */
87      public boolean isSelectionValid() {
88          ListSelectionModel selModel = getTextModel().getSelectionModel();
89          int min = selModel.getMinSelectionIndex();
90          int max = selModel.getMaxSelectionIndex();
91          return ((min >= 0) && (min <= max));
92      }
93  
94  }