1 package org.eparapher.rcp.properties;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.security.NoSuchAlgorithmException;
6
7 import org.apache.log4j.Logger;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.core.runtime.IStatus;
10 import org.eclipse.core.runtime.Status;
11 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
12 import org.eclipse.core.runtime.jobs.Job;
13 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
14 import org.eclipse.jface.preference.PreferencePage;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.dnd.Clipboard;
17 import org.eclipse.swt.dnd.RTFTransfer;
18 import org.eclipse.swt.dnd.TextTransfer;
19 import org.eclipse.swt.dnd.Transfer;
20 import org.eclipse.swt.events.MenuAdapter;
21 import org.eclipse.swt.events.MenuEvent;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Event;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Listener;
33 import org.eclipse.swt.widgets.Menu;
34 import org.eclipse.swt.widgets.MenuItem;
35 import org.eclipse.swt.widgets.Table;
36 import org.eclipse.swt.widgets.TableColumn;
37 import org.eclipse.swt.widgets.TableItem;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.dialogs.PropertyPage;
40 import org.eparapher.core.crypto.EPDigestManager;
41 import org.eparapher.core.tools.FileUtil;
42 import org.eparapher.rcp.tools.GUIIcons;
43 import org.eparapher.rcp.views.documents.SecuredDocumentsView;
44
45 public class FileHashPropertyPage extends PropertyPage {
46
47 private static final String ID = "org.eparapher.rcp.properties.filePropertyPage";
48
49 private static Logger log = Logger.getLogger(FileHashPropertyPage.class);
50
51 private static final String RECALC_TITLE = "Process digests";
52
53 private Button processHashes = null;
54 private Label progress = null;
55 private Table hashTable = null;
56
57
58
59
60 public FileHashPropertyPage() {
61 super();
62 }
63
64 private void addFirstSection(Composite parent) {
65 Composite composite = createDefaultComposite(parent);
66
67 processHashes = new Button(composite, SWT.PUSH);
68 processHashes.setText(RECALC_TITLE);
69 processHashes.addSelectionListener(new SelectionAdapter() {
70 public void widgetSelected(SelectionEvent e) {
71 processDigests();
72 }
73 });
74 progress = new Label(composite, SWT.NONE);
75 progress.setText("");
76 }
77
78 private void addSeparator(Composite parent) {
79 Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
80 GridData gridData = new GridData();
81 gridData.horizontalAlignment = GridData.FILL;
82 gridData.grabExcessHorizontalSpace = true;
83 separator.setLayoutData(gridData);
84 }
85
86 private void addSecondSection(Composite parent) {
87 Composite composite = createDefaultComposite(parent);
88
89 hashTable = new Table(composite, SWT.BORDER | SWT.SINGLE );
90 hashTable.setLayoutData(new GridData(GridData.FILL_BOTH));
91 hashTable.setLinesVisible(true);
92 hashTable.setHeaderVisible(true);
93
94 TableColumn algColumn = new TableColumn(hashTable, SWT.NONE);
95 algColumn.setWidth(80);
96 algColumn.setText("Algorithm");
97 TableColumn hashValueColumn = new TableColumn(hashTable, SWT.NONE);
98 hashValueColumn.setWidth(700);
99 hashValueColumn.setText("Value");
100
101
102 TableItem ti;
103 for (int i = 0; i < EPDigestManager.DIGESTFORMAT.length; i++) {
104 String alg = EPDigestManager.DIGESTFORMAT[i];
105 ti = new TableItem(hashTable,SWT.NONE);
106 ti.setText(0, alg);
107
108 }
109
110 final Menu menu = new Menu(hashTable);
111 hashTable.setMenu(menu);
112 menu.addMenuListener(new MenuAdapter() {
113 public void menuShown(MenuEvent e) {
114
115 MenuItem[] items = menu.getItems();
116 for (int i = 0; i < items.length; i++) {
117 items[i].dispose();
118 }
119
120 int index = hashTable.getSelectionIndex();
121 if (index == -1) return;
122 MenuItem copyHashItem = new MenuItem(menu, SWT.PUSH);
123 copyHashItem.setText("Copy");
124 copyHashItem.setImage(GUIIcons.EDIT_COPY_ICON_IMAGE);
125 copyHashItem.setAccelerator(SWT.CTRL + 'c');
126 copyHashItem.addListener( SWT.Selection ,new Listener() {
127 public void handleEvent(Event event) {
128 System.out.println("Copy pressed");
129 if (hashTable.getSelection()!=null) {
130 Clipboard clipboard = new Clipboard(PlatformUI.getWorkbench().getDisplay());
131 String plainText = hashTable.getSelection()[0].getText(1);
132 String rtfText = "{\\rtf1\\b "+plainText+"}";
133 clipboard.setContents(new String[]{plainText, rtfText}, new Transfer[]{TextTransfer.getInstance(), RTFTransfer.getInstance()});
134 clipboard.dispose();
135 }
136 }
137 });
138 }
139 });
140 }
141
142
143
144
145 protected Control createContents(Composite parent) {
146 Composite composite = new Composite(parent, SWT.NONE);
147 GridLayout layout = new GridLayout(1,false);
148 composite.setLayout(layout);
149
150 GridData data = new GridData(GridData.FILL);
151 data.grabExcessHorizontalSpace = true;
152 composite.setLayoutData(data);
153
154 addFirstSection(composite);
155 addSeparator(composite);
156 addSecondSection(composite);
157
158
159
160 return composite;
161 }
162
163 private Composite createDefaultComposite(Composite parent) {
164 Composite composite = new Composite(parent, SWT.NULL);
165 GridLayout layout = new GridLayout();
166 layout.numColumns = 2;
167 composite.setLayout(layout);
168
169 GridData data = new GridData();
170 data.verticalAlignment = GridData.FILL;
171 data.horizontalAlignment = GridData.FILL;
172 composite.setLayoutData(data);
173
174 return composite;
175 }
176
177 protected void performDefaults() {
178
179
180 }
181
182 public boolean performOk() {
183 return true;
184 }
185
186 private void processDigests() {
187 FileDigestJob job = new FileDigestJob();
188 job.setUser(true);
189 job.schedule();
190 }
191
192 class FileDigestJob extends Job {
193
194 private String filePath;
195 private String[] algs;
196 private String[] values;
197
198 protected FileDigestJob() {
199
200 super("Digests for " + ((SecuredDocumentsView.TreeObject) getElement()).getFilePath() );
201
202
203 filePath = ((SecuredDocumentsView.TreeObject) getElement()).getFilePath();
204
205
206 TableItem[] items = hashTable.getItems();
207 algs = new String[items.length];
208 for (int i = 0; i < items.length; i++) {
209 algs[i] = items[i].getText(0);
210 }
211
212
213 this.addJobChangeListener(new JobChangeAdapter() {
214 public void done(IJobChangeEvent event) {
215 if (event.getResult().isOK()) {
216 log.info("File digests job completed successfully");
217
218 } else
219 log.warn("Calculating file digest failed");
220 }
221 });
222 }
223
224 @SuppressWarnings("unchecked")
225 @Override
226 public IStatus run(IProgressMonitor monitor) {
227 monitor.beginTask("Processing file digests", algs.length+1);
228
229 monitor.subTask("Loading file to memory");
230 byte[] fcontent = null;
231 File file = new File(filePath);
232 try {
233 fcontent = FileUtil.readFile(file.getAbsolutePath());
234 } catch (IOException e) {
235 log.error("Error while loading file : " + filePath, e);
236 return Status.CANCEL_STATUS;
237 }
238 monitor.worked(1);
239
240 values = new String[algs.length];
241 for (int i = 0; i < algs.length; i++) {
242 monitor.subTask("Calculating " + algs[i] );
243 try {
244 values[i] = EPDigestManager.getInstance().hashInHex(algs[i],fcontent);
245 } catch (NoSuchAlgorithmException e) {
246 log.warn("Missing Digest algorithm" + algs[i]);
247 algs[i] = "algorithm unavailable in JCE";
248 }
249 refreshTable();
250 monitor.worked(1);
251 }
252 monitor.done();
253 return Status.OK_STATUS;
254 }
255 private void refreshTable() {
256 Display.getDefault().asyncExec(new Runnable() {
257 public void run() {
258 for(int i = 0; i < hashTable.getItems().length; i++)
259 hashTable.getItems()[i].setText(1, values[i]);
260 }
261 });
262 }
263 }
264 }