View Javadoc

1   package org.eparapher.rcp.views.documents;
2   
3   import java.util.ArrayList;
4   
5   import org.apache.log4j.Logger;
6   import org.eclipse.core.runtime.IAdaptable;
7   import org.eclipse.jface.action.Action;
8   import org.eclipse.jface.action.IMenuListener;
9   import org.eclipse.jface.action.IMenuManager;
10  import org.eclipse.jface.action.IToolBarManager;
11  import org.eclipse.jface.action.MenuManager;
12  import org.eclipse.jface.action.Separator;
13  import org.eclipse.jface.viewers.DoubleClickEvent;
14  import org.eclipse.jface.viewers.IDoubleClickListener;
15  import org.eclipse.jface.viewers.ISelectionProvider;
16  import org.eclipse.jface.viewers.IStructuredContentProvider;
17  import org.eclipse.jface.viewers.ITreeContentProvider;
18  import org.eclipse.jface.viewers.LabelProvider;
19  import org.eclipse.jface.viewers.TreeViewer;
20  import org.eclipse.jface.viewers.Viewer;
21  import org.eclipse.jface.viewers.ViewerSorter;
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.graphics.Image;
24  import org.eclipse.swt.widgets.Composite;
25  import org.eclipse.swt.widgets.Menu;
26  import org.eclipse.ui.IActionBars;
27  import org.eclipse.ui.IWorkbenchActionConstants;
28  import org.eclipse.ui.dialogs.PropertyDialogAction;
29  import org.eclipse.ui.part.DrillDownAdapter;
30  import org.eclipse.ui.part.ViewPart;
31  import org.eparapher.core.tools.JVMSettings;
32  import org.eparapher.rcp.EPReferences;
33  import org.eparapher.rcp.actions.CMSSignDocumentAction;
34  import org.eparapher.rcp.actions.DecryptAction;
35  import org.eparapher.rcp.actions.EncryptAction;
36  import org.eparapher.rcp.actions.OpenEditorAction;
37  import org.eparapher.rcp.actions.ToPDFAndSignDocumentAction;
38  import org.eparapher.rcp.actions.XMLSignDocumentAction;
39  import org.eparapher.rcp.tools.GUIIcons;
40  import org.eparapher.rcp.tools.RCPSettings;
41  
42  /**
43   * This sample class demonstrates how to plug-in a new
44   * workbench view. The view shows data obtained from the
45   * model. The sample creates a dummy model on the fly,
46   * but a real implementation would connect to the model
47   * available either in this or another plug-in (e.g. the workspace).
48   * The view is connected to the model using a content provider.
49   * <p>
50   * The view uses a label provider to define how model
51   * objects should be presented in the view. Each
52   * view can present the same model objects using
53   * different labels and icons, if needed. Alternatively,
54   * a single label provider can be shared between views
55   * in order to ensure that objects of the same type are
56   * presented in the same way everywhere.
57   * <p>
58   */
59  
60  public class SecuredDocumentsView extends ViewPart {
61  	
62  	public static final String ID = "org.eparapher.rcp.views.documents.SecuredDocumentsView";
63  	private static Logger log = Logger.getLogger(SecuredDocumentsView.class);
64  	
65  	private TreeViewer viewer;
66  	private DrillDownAdapter drillDownAdapter;
67  	
68  	private Action cmsSignAction;
69  	private Action cmsSignActionwithwizard;
70  
71  	private Action xmlSignAction;
72  	private Action xmlSignActionwithwizard;
73  	
74  	private Action convertAndSignPDFAction;
75  	private Action convertAndSignPDFActionwithwizard;
76  	
77  	//private Action verifsignatureAction;
78  	
79  	private Action refreshAction;
80  	
81  	private Action encryptAction;
82  	private Action encryptwithwizardAction;
83  	private Action decryptAction;
84  	private Action decryptwithwizardAction;
85  
86  	private Action doubleClickAction;
87  	
88  	private Action propertyAction;
89  
90  	/*
91  	 * The content provider class is responsible for
92  	 * providing objects to the view. It can wrap
93  	 * existing objects in adapters or simply return
94  	 * objects as-is. These objects may be sensitive
95  	 * to the current input of the view, or ignore
96  	 * it and always show the same content 
97  	 * (like Task List, for example).
98  	 */
99  	 
100 	public class TreeObject implements IAdaptable {
101 		
102 		protected String   name;
103 		private TreeParent parent;
104 		private boolean    isReadableDir;
105 		
106 		public TreeObject(String name) {
107 			this.name = name;
108 			this.isReadableDir = false;
109 		}
110 
111 		public TreeObject(String name,boolean misReadableDir) {
112 			this.name = name;
113 			this.isReadableDir = false;
114 		}
115 		public String getName() {
116 			return name;
117 		}
118 		public void setParent(TreeParent parent) {
119 			this.parent = parent;
120 		}
121 		public TreeParent getParent() {
122 			return parent;
123 		}
124 		public String getFilePath() {
125 			String fp = getName();
126 			TreeParent tp = getParent();
127 			//Stop when got root parent
128 			while (!tp.getParent().getName().equals("")) {
129 				fp = tp.getName() + java.io.File.separator + fp;
130 				tp = tp.getParent();
131 			}
132 			//Correct a bug on Windows
133 			if (fp.endsWith(java.io.File.separator))
134 				fp = tp.getName() + fp;
135 			else 
136 				fp = tp.getName() + java.io.File.separator + fp;
137 			return fp;
138 		}
139 		public String toString() {
140 			return getName();
141 		}
142 		public Object getAdapter(Class key) {
143 			return null;
144 		}
145 	}
146 	
147 	public class TreeParent extends TreeObject {
148 		private ArrayList<TreeObject> children;
149 		private boolean isRoot;
150 		private int     rootnb;
151 		public TreeParent(String name) {
152 			super(name);
153 			children = new ArrayList<TreeObject>();
154 			isRoot = false;
155 		}
156 		public void addChild(TreeObject child) {
157 			children.add(child);
158 			child.setParent(this);
159 		}
160 		public void removeChild(TreeObject child) {
161 			children.remove(child);
162 			child.setParent(null);
163 		}
164 		public TreeObject [] getChildren() {
165 			return (TreeObject [])children.toArray(new TreeObject[children.size()]);
166 		}
167 		public boolean hasChildren() {
168 			return children.size()>0;
169 		}
170 		public boolean isRoot() {
171 			return isRoot;
172 		}
173 		public void setRoot(boolean misroot, String mpath) {
174 			isRoot = misroot;
175 			if (isRoot)
176 				this.name = mpath;
177 		}
178 		/*public String getFilePath() {
179 			if (isRoot())
180 				return EPConfig.getSecureDocumentPath();
181 			else
182 				return super.getFilePath();
183 		}*/
184 	}
185 
186 	class ViewLabelProvider extends LabelProvider {
187 
188 		public String getText(Object obj) {
189 			return obj.toString();
190 		}
191 		public Image getImage(Object obj) {
192 			//String imageKey = ISharedImages.IMG_OBJ_FILE;
193 			if (obj instanceof TreeParent) {
194 				TreeParent tp = (TreeParent) obj;
195 				if (tp.isRoot()) {
196 					return GUIIcons.HOME_LOCAL_FILE_ICON;
197 				}
198 				else 
199 					return GUIIcons.FILE_ICON_FOLDER; //imageKey = ISharedImages.IMG_OBJ_FOLDER;
200 			}
201 			if (obj instanceof TreeObject) {
202 				TreeObject to = (TreeObject) obj;
203 				return GUIIcons.getIconFromFileExt(to);
204 			}
205 			return GUIIcons.FILE_ICON_DEFAULT;
206 			//return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
207 		}
208 	}
209 	class NameSorter extends ViewerSorter {
210 	}
211 	class DocViewContentProvider implements IStructuredContentProvider, ITreeContentProvider {
212 		
213 		private TreeParent   invisibleRoot;
214 		private TreeParent[] localDocsRoot;
215 		private String[]     localDocsPath;
216 		
217 		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
218 		}
219 		public void dispose() {
220 		}
221 		public Object[] getElements(Object parent) {
222 			if (parent.equals(getViewSite())) {
223 				if ( (invisibleRoot==null) || (isRootPathsChanged()) )
224 					initialize();
225 				else refresh();
226 				return getChildren(invisibleRoot);
227 			}
228 			return getChildren(parent);
229 		}
230 		
231 		private boolean isRootPathsChanged() {
232 			boolean returnvalue=false;
233 			String[] docpathsconfig = RCPSettings.getSecureDocumentPath();
234 			if (localDocsPath.length!=docpathsconfig.length)
235 				return true;
236 			for (int i = 0; i < localDocsPath.length; i++) {
237 				if (!localDocsPath[i].equals(docpathsconfig[i]))
238 					returnvalue=true;
239 			}
240 			
241 			return returnvalue;
242 		}
243 		private void refresh() {
244 			for (int i = 0; i < localDocsPath.length; i++) {
245 				log.debug("Refreshing directory " + localDocsRoot[i]);
246 				recursiveupdate(localDocsRoot[i], new java.io.File(localDocsPath[i]));
247 			}
248 			log.debug("Refreshing directories finished");
249 		}
250 		public Object getParent(Object child) {
251 			if (child instanceof TreeObject) {
252 				return ((TreeObject)child).getParent();
253 			}
254 			return null;
255 		}
256 		public Object [] getChildren(Object parent) {
257 			if (parent instanceof TreeParent) {
258 				return ((TreeParent)parent).getChildren();
259 			}
260 			return new Object[0];
261 		}
262 		public boolean hasChildren(Object parent) {
263 			if (parent instanceof TreeParent)
264 				return ((TreeParent)parent).hasChildren();
265 			return false;
266 		}
267 		/*
268 		 * We set up a dummy model to initialize tree heararchy.
269 		 * In a real code, you will connect to a real model and
270 		 * expose its hierarchy.
271 		 */
272 		private void initialize() {
273 
274 			localDocsPath = RCPSettings.getSecureDocumentPath();
275 			localDocsRoot = new TreeParent[localDocsPath.length];
276 			invisibleRoot = new TreeParent("");
277 			
278 			for (int i = 0; i < localDocsPath.length; i++) {
279 				java.io.File rootpath = new java.io.File(localDocsPath[i]);
280 				localDocsRoot[i] = recursiveinit(rootpath);
281 				localDocsRoot[i].setRoot(true,localDocsPath[i]);
282 				invisibleRoot.addChild(localDocsRoot[i]);
283 			}
284 		}
285 		private TreeParent recursiveinit(java.io.File pathToDo) {
286 
287 			java.io.File[] files = pathToDo.listFiles();
288 			TreeParent tp = new TreeParent(pathToDo.getName());
289 			try {
290 			for (int i = 0; i < files.length; i++) {
291 				java.io.File file = files[i];
292 				if (!file.isHidden()&& file.canRead()) {
293 					if (file.isFile()) {
294 						TreeObject to = new TreeObject(file.getName());
295 						tp.addChild(to);
296 					}
297 					if (file.isDirectory()) {
298 						TreeParent subtp = recursiveinit(file);
299 						tp.addChild(subtp);
300 					}
301 				}
302 			}
303 			} catch (Exception e) {
304 				log.debug("Error while creating doc tree for " + pathToDo.getAbsolutePath(),e);
305 			}
306 			return tp;
307 		}
308 
309 		private void recursiveupdate(TreeParent tp, java.io.File pathToCheck) {
310 			
311 			java.io.File[] files = pathToCheck.listFiles();
312 			TreeObject[]   tos    = tp.getChildren();
313 			
314 			//Add check : List files in the directory and check if exists in the tree
315 			for (int i = 0; i < files.length; i++) {
316 				java.io.File file = files[i];
317 				int  corresponding_index = -1;
318 				//search for corresponding object
319 				for (int j = 0; j < tos.length; j++)
320 					if (file.getName().equals(tos[j].getName()))
321 						corresponding_index = j;
322 				//Not found : new file on FS that is not in the treeview, so add it ...
323 				if (corresponding_index == -1) {
324 					if (!file.isHidden() && file.canRead()) {
325 						if (file.isFile()) {
326 							// New File
327 							TreeObject to = new TreeObject(file.getName());
328 							tp.addChild(to);
329 						}
330 						if (file.isDirectory()) {
331 							//New Directory
332 							TreeParent subtp = recursiveinit(file);
333 							tp.addChild(subtp);
334 						}
335 					}
336 				}
337 			}
338 			
339 			//Delete check : List tree and check if the file or directory exists
340 			for (int i = 0; i < tos.length; i++) {
341 				TreeObject to  = tos[i];
342 				java.io.File file = new java.io.File(to.getFilePath());
343 				if ( !file.exists() )
344 					tp.removeChild(tos[i]);
345 				else {
346 					if ( file.isDirectory() && (to instanceof TreeParent)) {
347 						TreeParent tp_subdir = (TreeParent) to;
348 						recursiveupdate(tp_subdir,file);
349 					}
350 					//Directory change to file or vice versa
351 					if ( file.isDirectory() && !(to instanceof TreeParent)) {
352 						
353 					}
354 				}
355 			}
356 			
357 		}
358 	}
359 	
360 	/**
361 	 * The constructor.
362 	 */
363 	public SecuredDocumentsView() {
364 	}
365 
366 	/**
367 	 * This is a callback that will allow us
368 	 * to create the viewer and initialize it.
369 	 */
370 	public void createPartControl(Composite parent) {
371 		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
372 		drillDownAdapter = new DrillDownAdapter(viewer);
373 		viewer.setContentProvider(new DocViewContentProvider());
374 		viewer.setLabelProvider(new ViewLabelProvider());
375 		viewer.setSorter(new NameSorter());
376 		viewer.setInput(getViewSite());
377 		makeActions();
378 		hookContextMenu();
379 		hookDoubleClickAction();
380 		contributeToActionBars();
381 		EPReferences.getInstance().setDocviews(this);
382 		//Send the change selection notification to the selection service
383 		getSite().setSelectionProvider(viewer);
384 	}
385 
386 	private void hookContextMenu() {
387 		MenuManager menuMgr = new MenuManager("#PopupMenu");
388 		menuMgr.setRemoveAllWhenShown(true);
389 		menuMgr.addMenuListener(new IMenuListener() {
390 			public void menuAboutToShow(IMenuManager manager) {
391 				SecuredDocumentsView.this.fillContextMenu(manager);
392 			}
393 		});
394 		Menu menu = menuMgr.createContextMenu(viewer.getControl());
395 		viewer.getControl().setMenu(menu);
396 		getSite().registerContextMenu(menuMgr, viewer);
397 	}
398 
399 	private void contributeToActionBars() {
400 		IActionBars bars = getViewSite().getActionBars();
401 		fillLocalPullDown(bars.getMenuManager());
402 		fillLocalToolBar(bars.getToolBarManager());
403 	}
404 	
405 	private void fillLocalToolBar(IToolBarManager manager) {
406 		manager.add(refreshAction);
407 		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
408 		drillDownAdapter.addNavigationActions(manager);
409 	}
410 
411 	private void fillLocalPullDown(IMenuManager manager) {
412 		manager.add(convertAndSignPDFActionwithwizard);
413 		manager.add(cmsSignActionwithwizard);
414 		if (JVMSettings.isJava16Min())
415 			manager.add(xmlSignActionwithwizard);
416 		manager.add(new Separator());
417 		//manager.add(verifsignatureAction);
418 		//drillDownAdapter.addNavigationActions(manager);
419 	}
420 
421 	private void fillContextMenu(IMenuManager manager) {
422 		
423 		MenuManager signMenu = new MenuManager("Signature");
424 		signMenu.add(convertAndSignPDFAction);
425 		signMenu.add(convertAndSignPDFActionwithwizard);
426 		signMenu.add(new Separator());
427 		signMenu.add(cmsSignAction);
428 		signMenu.add(cmsSignActionwithwizard);
429 
430 		if (JVMSettings.isJava16Min()) {
431 			signMenu.add(new Separator());
432 			signMenu.add(xmlSignAction);
433 			signMenu.add(xmlSignActionwithwizard);
434 		}
435 		manager.add(signMenu);
436 
437 		MenuManager encryptMenu = new MenuManager("Encryption");
438 		encryptMenu.add(encryptAction);
439 		encryptMenu.add(encryptwithwizardAction);
440 		encryptMenu.add(decryptAction);
441 		manager.add(encryptMenu);
442 
443 		//manager.add(new Separator());
444 		//manager.add(verifsignatureAction);
445 		manager.add(new Separator());
446 		manager.add(refreshAction);
447 		//drillDownAdapter.addNavigationActions(manager);
448 		// Other plug-ins can contribute there actions here
449 		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
450 		
451 		manager.add(new Separator());
452         manager.add(propertyAction);
453 
454 	}
455 
456 	private void makeActions() {
457 		cmsSignAction             = new CMSSignDocumentAction(viewer, false);
458 		cmsSignActionwithwizard   = new CMSSignDocumentAction(viewer, true);
459 		
460 		convertAndSignPDFAction   = new ToPDFAndSignDocumentAction(viewer, false);
461 		convertAndSignPDFActionwithwizard = new ToPDFAndSignDocumentAction(viewer, true);
462 
463 		if (JVMSettings.isJava16Min()) {
464 			xmlSignAction             = new XMLSignDocumentAction(viewer, false);
465 			xmlSignActionwithwizard   = new XMLSignDocumentAction(viewer, true);
466 			//verifsignatureAction = new SignatureVerificationAction();
467 		}
468 		
469 		refreshAction =  new Action() {
470 			public void run() {viewer.refresh();}
471 		};
472 		refreshAction.setText("Re&fresh");
473 		refreshAction.setToolTipText("Refresh the Tree for added/removed files");
474 		refreshAction.setImageDescriptor(GUIIcons.REFRESH_ICON);
475 		refreshAction.setAccelerator(SWT.F5);
476 
477 		encryptAction           = new EncryptAction(viewer, false);
478 		encryptwithwizardAction = new EncryptAction(viewer, true);
479 		decryptAction           = new DecryptAction(viewer);
480 		encryptAction.setEnabled(false);
481 		encryptwithwizardAction.setEnabled(false);
482 		decryptAction.setEnabled(false);
483 
484 		propertyAction = new PropertyDialogAction(getSite(), viewer);
485 		propertyAction.setImageDescriptor(GUIIcons.INFO_ICON);
486 		
487 		doubleClickAction = new OpenEditorAction(viewer);
488 	}
489 
490 	private void hookDoubleClickAction() {
491 		viewer.addDoubleClickListener(new IDoubleClickListener() {
492 			public void doubleClick(DoubleClickEvent event) {
493 				doubleClickAction.run();
494 			}
495 		});
496 	}
497 
498 	/**
499 	 * Passing the focus request to the viewer's control.
500 	 */
501 	public void setFocus() {
502 		viewer.getControl().setFocus();
503 	}
504 	/*
505 	 *   IActionBars bars = getViewSite().getActionBars();
506      *   IStatusLineManager statusLine = bars.getStatusLineManager();
507      *  IProgressMonitor pm = statusLine.getProgressMonitor();
508      */
509 	public void refreshView() {
510 		//refresh the view
511 		if (viewer!=null)
512 			viewer.refresh();
513 	}
514 }