KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > gnupg > Views > KeysTableModel


1 package SnowMailClient.gnupg.Views;
2
3 import snow.utils.storage.*;
4 import snow.sortabletable.*;
5 import SnowMailClient.Language.Language;
6 import SnowMailClient.gnupg.model.*;
7 import SnowMailClient.gnupg.GnuPGLink;
8
9 import java.util.*;
10 import java.text.*;
11
12 public final class KeysTableModel extends FineGrainTableModel
13 {
14   private final Vector<GnuPGKeyID> keys = new Vector<GnuPGKeyID>();
15   private boolean advancedMode = true;
16
17   private static final String JavaDoc[] COLUMN_NAMES = new String JavaDoc[]{
18      Language.translate("Type"),
19      Language.translate("Trust"),
20      Language.translate("Name"),
21      Language.translate("Mail address"),
22      Language.translate("Capabilities"),
23      Language.translate("Created"),
24      Language.translate("Expires") };
25
26   private static final int[] PREFERED_COLUMN_WIDTHS = new int[]{6,7,16,20, 6, 7,7};
27
28   public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH);
29
30   public KeysTableModel()
31   {
32   }
33
34   public void setKeys(TreeSet<GnuPGKeyID> k )
35   {
36      this.fireTableModelWillChange();
37      keys.clear();
38      keys.addAll(k);
39      this.fireTableDataChanged();
40      this.fireTableModelHasChanged();
41   }
42
43   public void addKeys(TreeSet<GnuPGKeyID> k )
44   {
45      this.fireTableModelWillChange();
46      keys.addAll(k);
47      this.fireTableDataChanged();
48      this.fireTableModelHasChanged();
49   }
50
51   /** refresh the model. Reread all keys.
52       Use a trick because with GPG 1.4.0, the first time always fails after a key add or remove => do one dummy refresh
53
54   */

55   public void refreshModel(GnuPGLink link)
56   {
57      this.fireTableModelWillChange();
58
59      // reread the keys
60
try
61      {
62        // TRICK
63
link.setGPGPath(link.getPathToGPG());
64      }
65      catch(Exception JavaDoc ignored) {}
66
67      try
68      {
69        link.setGPGPath(link.getPathToGPG());
70        keys.clear();
71        keys.addAll( link.getAllPublicKeyIDs());
72        if(advancedMode)
73        {
74          keys.addAll( link.getAllSecretKeyIDs());
75        }
76      }
77      catch(Exception JavaDoc e) { throw new RuntimeException JavaDoc(e); }
78
79      this.fireTableDataChanged();
80      this.fireTableModelHasChanged();
81
82   }
83
84   public GnuPGKeyID getKeyAt(int pos)
85   {
86      return keys.elementAt(pos);
87   }
88
89
90   public int getRowCount()
91   {
92      return keys.size();
93   }
94
95   public int getColumnCount()
96   {
97      return COLUMN_NAMES.length;
98   }
99
100   public final Object JavaDoc getValueAt(int row, int column)
101   {
102      GnuPGKeyID k = keys.elementAt(row);
103      if(column==0)
104      {
105        if(k.isSecret()) return Language.translate("Secret");
106        return Language.translate("Public");
107      }
108      if(column==1)
109      {
110        if(k.isSecret()) return ""; // no meaning for secret keys !
111
if(k.hasExpired()) return Language.translate("Expired");
112        return k.getCalculatedTrust();
113      }
114      if(column==2) return k.getNames();
115      if(column==3) return k.getMails();
116      if(column==4) return k.getKeyCapabilities();
117      if(column==5)
118      {
119        long ed = k.getCreationDate();
120        if(ed==-1) return "";
121        return dateFormat.format(ed);
122      }
123      if(column==6)
124      {
125        long ed = k.getExpirationDate();
126        if(ed==-1) return "";
127        return dateFormat.format(ed);
128      }
129      return "?";
130   }
131
132   public String JavaDoc getColumnName(int col)
133   {
134      return COLUMN_NAMES[col];
135   }
136
137 /* Let the default implementation do their job correctly...
138
139   public boolean hitForTextSearch(int row, String txt)
140   {
141     return true;
142   }
143
144   public int compareForColumnSort(int pos1, int pos2, int col)
145   {
146
147   }*/

148   public void setAdvancedMode(boolean is, GnuPGLink link)
149   {
150     advancedMode = is;
151     this.refreshModel(link);
152   }
153
154   public int getPreferredColumnWidth(int column)
155   {
156     return PREFERED_COLUMN_WIDTHS[column];
157   }
158
159 }
Popular Tags