KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > Language > Editor > SentencesTableModel


1 package SnowMailClient.Language.Editor;
2
3 import snow.sortabletable.*;
4 import SnowMailClient.Language.*;
5 import java.util.*;
6 import java.util.regex.*;
7 import java.awt.event.*;
8 import javax.swing.event.*;
9
10 /** Used to display sentences for editing.
11 */

12 public final class SentencesTableModel extends FineGrainTableModel implements ChangeListener
13 {
14   final static int COLUMN_SENTENCE = 0;
15   final static int COLUMN_TRANSLATION = 1;
16   final static int COLUMN_SOURCEFILE = 2;
17   final static int COLUMN_SOURCELINE = 3;
18   final private Vector<Sentence> sentences = new Vector<Sentence>();
19   private transient SentenceDictionary actualDic;
20   boolean editable = true;
21
22   public SentencesTableModel()
23   {
24   }
25
26   public void setDictionary(SentenceDictionary dic, boolean editable)
27   {
28     this.editable = editable;
29     if(actualDic!=null) actualDic.removeChangeListener(this);
30
31     actualDic = dic;
32     sentences.removeAllElements();
33     //
34
List<Sentence> v = dic.getAllSentences();
35     for(int i=0; i<v.size(); i++)
36     {
37       sentences.add(v.get(i));
38     }
39
40     this.fireTableDataChanged();
41
42     dic.addChangeListener(this);
43   }
44
45   // called when actualDic changed
46
public void stateChanged(ChangeEvent e)
47   {
48     fireTableDataChanged();
49   }
50
51   public String JavaDoc getColumnName(int col)
52   {
53      if(col==COLUMN_SENTENCE) return "English";
54      if(col==COLUMN_TRANSLATION) return "Translation";
55      if(col==COLUMN_SOURCEFILE) return "Found in source";
56      return "?";
57   }
58
59   public Sentence getSentenceAt(int pos)
60   {
61      return sentences.elementAt(pos);
62   }
63
64   public int getColumnCount() { return 3; }
65   public int getRowCount() { return sentences.size(); }
66
67
68   public boolean isCellEditable(int rowIndex, int columnIndex)
69   {
70      return false;
71   }
72
73   public Object JavaDoc getValueAt(int row, int col)
74   {
75      Sentence s = sentences.elementAt(row);
76
77      if(col==COLUMN_SENTENCE)
78      {
79         return s.getSentence();
80      }
81      else if(col==COLUMN_TRANSLATION)
82      {
83         return s.getTranslation();
84      }
85      else if(col==COLUMN_SOURCEFILE)
86      {
87         int line=s.getLinePosition();
88         String JavaDoc loc =s.getLocationClass();
89         if(loc.equals("")) return "";
90         return loc+" : "+(line==-1 ? "?" : ""+line);
91      }
92      else if(col==COLUMN_SOURCELINE)
93      {
94         return ""+ s.getLinePosition();
95      }
96      return "?";
97   }
98
99   public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex)
100   {
101    // not editable
102
}
103
104
105   public Class JavaDoc getColumnClass ( int column )
106   {
107      Object JavaDoc obj = getValueAt ( 0, column );
108      return obj.getClass ();
109   }
110
111   public int compareForColumnSort(int pos1, int pos2, int col)
112   {
113      Sentence s1 = sentences.get(pos1);
114      Sentence s2 = sentences.get(pos2);
115
116      if(col==COLUMN_SENTENCE)
117      {
118         return s1.getSentence().compareToIgnoreCase(s2.getSentence());
119      }
120      else if(col==COLUMN_TRANSLATION)
121      {
122         return s1.getTranslation().compareToIgnoreCase(s2.getTranslation());
123      }
124      else if(col==COLUMN_SOURCEFILE)
125      {
126         return s1.getLocationClass().compareToIgnoreCase(s2.getLocationClass());
127      }
128      else if(col==COLUMN_SOURCELINE)
129      {
130         int l1 = s1.getLinePosition();
131         int l2 = s2.getLinePosition();
132         if(l1==l2) return 0;
133         if(l1<l2) return -1;
134         return 1;
135      }
136      return 0;
137   }
138
139 /* public boolean hitForTextSearch(int row, String txt, Pattern p)
140   {
141      String upt = txt.toUpperCase();
142      Sentence s = (Sentence) sentences.elementAt(row);
143
144      if(s.getSentence().toUpperCase().indexOf(upt)!=-1) return true;
145      if(s.getTranslation().toUpperCase().indexOf(upt)!=-1) return true;
146
147      return false;
148   } */

149
150
151
152 } // SentencesTableModel
Popular Tags