View Javadoc

1   package org.eparapher.rcp.tools;
2   
3   import java.security.cert.X509Certificate;
4   
5   import org.eclipse.jface.action.Action;
6   import org.eclipse.jface.viewers.DoubleClickEvent;
7   import org.eclipse.jface.viewers.IDoubleClickListener;
8   import org.eclipse.jface.viewers.ISelection;
9   import org.eclipse.jface.viewers.IStructuredContentProvider;
10  import org.eclipse.jface.viewers.IStructuredSelection;
11  import org.eclipse.jface.viewers.ITableLabelProvider;
12  import org.eclipse.jface.viewers.LabelProvider;
13  import org.eclipse.jface.viewers.TableViewer;
14  import org.eclipse.jface.viewers.Viewer;
15  import org.eclipse.jface.viewers.ViewerSorter;
16  import org.eclipse.swt.SWT;
17  import org.eclipse.swt.graphics.Image;
18  import org.eclipse.swt.widgets.Composite;
19  import org.eclipse.swt.widgets.Table;
20  import org.eclipse.swt.widgets.TableColumn;
21  import org.eparapher.core.crypto.EPKeystoreManager;
22  import org.eparapher.core.crypto.KeystoreEntry;
23  import org.eparapher.core.crypto.cert.CertificateInfo;
24  import org.eparapher.core.crypto.keystore.IUserKeystore;
25  import org.eparapher.rcp.dialog.CertificateViewerDialog;
26  /**
27   * Table that list certificates
28   * @author arnault
29   *
30   */
31  public class CertificateView extends TableViewer {
32  
33  	protected Action      doubleClickAction;
34  	
35  	private KeystoreEntry[] ksentries;
36  	
37  	private String defaultAlias;
38  	private int defaultindex;
39  
40  	/** 
41  	 * Create a certificate view that list UserKeystore
42  	 */
43  	public CertificateView(Composite parent) {
44  		this( parent, SWT.SINGLE, null );
45  	}
46  
47  	/** 
48  	 * Create a certificate view with specific style and specific 
49  	 * @param parent  Composite that contain the tableView
50  	 * @param style   Choose SWT.SINGLE for single selection or SWT.MULTI for multiple selection
51  	 */
52  	public CertificateView(Composite parent, int style, KeystoreEntry[] content) {
53  		
54  		super( new Table( parent, SWT.BORDER | SWT.FULL_SELECTION | style ) );
55  		//getTable().setLayoutData(new GridData( GridData.FILL, GridData.FILL, true, true ));
56  		ksentries    = content;
57  		defaultindex = -1;
58  		
59  		//Build table
60  		//getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
61  		getTable().setHeaderVisible(true);
62  		getTable().setLinesVisible(false);
63  		//Build table columns
64  		String[] column = new String[] {" ","Alias","Subject","Validity","Key"};
65          for (int i = 0; i < column.length; i++) {
66              TableColumn c = new TableColumn(getTable(), SWT.LEFT);
67              c.setText(column[i]);
68              switch (i) {
69  			case 0:
70  	            c.setWidth(25);
71  				break;
72  			case 3:
73  	            c.setWidth(180);
74  				break;
75  			default:
76  				c.setWidth(120);
77  				break;
78  			}
79          }
80          
81  		this.setContentProvider(new ViewContentProvider());
82  		this.setLabelProvider(new ViewLabelProvider());
83  		this.setComparator(new NameSorter());
84  		this.setInput(parent);
85  		
86  		// Dble click mgmt
87  		doubleClickAction = new DoubleClickAction();
88  		addDoubleClickListener(new IDoubleClickListener() {
89  			public void doubleClick(DoubleClickEvent event) {
90  				doubleClickAction.run();
91  			}
92  		});
93  		//Set Default selection
94  		if (defaultindex>0)
95  			this.getTable().setSelection(defaultindex);
96  	}
97  
98  	public void setDefaultAlias(String defaultAlias) {
99  		this.defaultAlias = defaultAlias;
100 	}
101 
102 	public void setContent(KeystoreEntry[] newContent) {
103 		this.ksentries = newContent;
104 		this.setInput(ksentries);
105 		this.refresh();
106 	}
107 	public KeystoreEntry[] getKeystoreEntries() {
108 		return ksentries;
109 	}
110 	class ViewContentProvider implements IStructuredContentProvider {
111 		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
112 		}
113 		public void dispose() {
114 		}
115 		public Object[] getElements(Object parent) {
116 			if (ksentries == null) {
117 				ksentries = new KeystoreEntry[0];
118 				IUserKeystore userKeystore = EPKeystoreManager.getInstance().getUserkeystore();
119 				if ( userKeystore == null ) {
120 					eParapherTools.errorMessage("Failed to initialize your personal keystore.\r\nPlease change it in configuration.");
121 				} else if ( userKeystore.loadKeyStore() ) {
122 					ksentries = userKeystore.getKeystoreEntries();
123 				}
124 			}
125 			return ksentries;
126 		}
127 	}
128 
129 	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
130 		public String getColumnText(Object obj, int index) {
131 
132 			if (obj==null)
133 				return "";
134 			
135 			KeystoreEntry certchain = (KeystoreEntry) obj;
136 			
137 			X509Certificate userCert = null;
138 			if (certchain.getCertificateChain() == null) {
139 				if (index==4)
140 					return "Secret Key";
141 				if (index==1)
142 					return certchain.getKeystoreAlias();
143 				return "";
144 			}
145 			userCert = certchain.getCertificateChain()[0];
146 				
147 			switch (index) {
148 				case 0:	return "";
149 				case 1: return certchain.getKeystoreAlias();
150 				case 2: return CertificateInfo.getSubjectAsShortText(userCert);
151 				case 3: return CertificateInfo.getNotBeforeAsText( userCert ) + " to " + CertificateInfo.getNotAfterAsText( userCert );
152 				case 4: return CertificateInfo.getPublicKeyInfo( userCert.getPublicKey() );
153 				default: break;
154 			}
155 			return "";
156 		}
157 		public Image getColumnImage(Object obj, int index) {
158 			if (index == 0)
159 				return getImage(obj);
160 			return null;
161 		}
162 		
163 		public Image getImage(Object obj) {
164 			KeystoreEntry certchain = (KeystoreEntry) obj;
165 			if (certchain.getKeystoreAlias().equals(defaultAlias)) {
166 				for (int i = 0; i < ksentries.length; i++) {
167 					if (ksentries[i].equals(certchain))
168 						defaultindex = i;
169 				}
170 				return GUIIcons.CERTIFICATE_SEL_ICON_IMAGE;
171 			} else if ( certchain.getCertificateChain()==null || certchain.getCertificateChain().length==0 )
172 				return GUIIcons.PRIVATE_KEY_ICON_IMAGE;
173 			else
174 				return GUIIcons.CERTIFICATE_ICON_IMAGE;
175 		}
176 	}
177 	class NameSorter extends ViewerSorter {
178 	}
179 	class DoubleClickAction extends Action {
180 		public void run() {
181 			ISelection selection = getSelection();
182 			Object obj = ((IStructuredSelection)selection).getFirstElement();
183 			if (obj!=null)
184 				if (obj instanceof KeystoreEntry) {
185 					KeystoreEntry ke = (KeystoreEntry) obj; 
186 					CertificateViewerDialog cvg = new CertificateViewerDialog(ke.getCertificateChain());
187 					cvg.open();
188 				}
189 		}
190 	}
191 }