Skip to main content
summaryrefslogtreecommitdiffstats
blob: dd606113f3445039639ae91cf610aa62007a3240 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*******************************************************************************
 * Copyright (c) 2010 Poznan Supercomputing and Networking Center
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Jan Konczak (PSNC) - initial implementation
 ******************************************************************************/

package org.eclipse.ptp.rm.smoa.ui.actions;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.ptp.remote.core.PTPRemoteCorePlugin;
import org.eclipse.ptp.rm.smoa.core.rservices.SMOAFileStore;
import org.eclipse.ptp.rm.smoa.core.rservices.SMOARemoteServices;
import org.eclipse.ptp.rm.smoa.core.util.NotifyShell;
import org.eclipse.ptp.rm.smoa.ui.SMOAUIPlugin;
import org.eclipse.ptp.ui.PTPUIPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class SMOACustomSyncAction extends AbstractHandler {

	private class FileFormatException extends IOException {
		private static final long serialVersionUID = -1360379446136195743L;

		public FileFormatException(String s) {
			super(s);
		}
	}

	/** Policy if and when the files should be overwritten */
	enum OverwritePolicy {
		Always, IfNewer, Never
	}

	private static final QualifiedName SMOA_SYNC_OVERWRITE = new QualifiedName(
			SMOAUIPlugin.PLUGIN_ID, "smoa.sync.rm.files.overwrite"); //$NON-NLS-1$

	private static final String SMOA_SYNC_FILELIST_NAME = ".smoa_sync_file_list"; //$NON-NLS-1$

	SMOARemoteServices rservices = (SMOARemoteServices) PTPRemoteCorePlugin
			.getDefault().getRemoteServices("org.eclipse.ptp.remote.SMOARemoteServices"); //$NON-NLS-1$
	// GUI components
	private Tree remoteTree;
	private Tree localTree;

	private Combo policy;
	// Data
	/** Remote file store (rendered in remoteTree) */
	private IFileStore sfs;
	/** Local file store (rendered in localTree) */
	private IFileStore lfs;

	private OverwritePolicy overwrite;

	private IProject project;

	int counter;

	private TreeItem addToTree(Tree toRegenerate, TreeItem treeItem,
			IFileStore newStore) {
		TreeItem parent = null;
		if (treeItem.getParentItem() != null) {
			parent = addToTree(toRegenerate, treeItem.getParentItem(), null);
		}

		TreeItem newItem;
		if (parent == null) {
			newItem = findChild(toRegenerate, treeItem.getText());
			if (newItem == null) {
				newItem = new TreeItem(toRegenerate, SWT.NONE);
				newItem.setText(treeItem.getText());
			}
		} else {
			newItem = findChild(parent, treeItem.getText());
			if (newItem == null) {
				newItem = new TreeItem(parent, SWT.NONE);
				newItem.setText(treeItem.getText());
			}
		}

		if (newStore != null) {
			newItem.setData(newStore);
		}

		return newItem;
	}

	/**
	 * Copies a single tree item to store, and updates toRegenerate tree
	 */
	private void copyTreeItem(TreeItem treeItem, IFileStore store,
			Tree toRegenerate) throws CoreException {
		final IFileStore newStore = store
				.getFileStore(new Path(getRelPath(treeItem)));
		final IFileInfo info = ((IFileStore) treeItem.getData()).fetchInfo();
		if (info.isDirectory()) {
			newStore.mkdir(0, null);
			newStore.putInfo(info, 0, null);
		} else {
			switch (overwrite) {
			case Always:
				break;
			case Never:
				if (newStore.fetchInfo().exists()) {
					return;
				}
				break;
			case IfNewer:
				final IFileInfo newInfo = newStore.fetchInfo();
				if (newInfo.exists()
						&& newInfo.getLastModified() > info.getLastModified()) {
					return;
				}
				break;
			default:
				throw new CoreException(new Status(IStatus.ERROR,
						PTPUIPlugin.PLUGIN_ID, Messages.SMOACustomSyncAction_IncorretOverwritePolicy));
			}
			((IFileStore) treeItem.getData()).copy(newStore, EFS.OVERWRITE,
					null);
		}

		addToTree(toRegenerate, treeItem, newStore);
	}

	@Override
	public void dispose() {
		// saveTrees(project);
		super.dispose();
	}

	/**
	 * Called when user triggers this action from a menu (Described in
	 * plugin.properties)
	 */
	public Object execute(ExecutionEvent execEvent) throws ExecutionException {
		try {

			project = SelectConnetionAndDestDir.getSelectedProject(execEvent);

			final SelectConnetionAndDestDir dialog = new SelectConnetionAndDestDir(
					project);

			if (!dialog.open()) {
				return null;
			}

			sfs = dialog.getRemoteFileStore();
			if (sfs == null) {
				throw new ExecutionException(Messages.SMOACustomSyncAction_ErrorAccessingRemoteFS);
			}

			final Shell topShell = Display.getCurrent().getActiveShell();
			final Shell shell = new Shell(topShell, new Shell().getStyle()
					| SWT.APPLICATION_MODAL);
			shell.setText(Messages.SMOACustomSyncAction_WindowTitle);
			final GridLayout gridLayout = new GridLayout(2, true);
			shell.setLayout(gridLayout);

			new Label(shell, SWT.NONE).setText(Messages.SMOACustomSyncAction_Local);
			new Label(shell, SWT.NONE).setText(Messages.SMOACustomSyncAction_Remote);

			localTree = new Tree(shell, SWT.CHECK | SWT.VIRTUAL | SWT.MULTI);
			localTree.setLayoutData(new GridData(GridData.FILL_BOTH));

			final Listener treeListener = new Listener() {
				public void handleEvent(Event event) {
					assert event.item instanceof TreeItem;
					final TreeItem me = (TreeItem) event.item;
					if (me.getChecked()) {

						TreeItem parentItem = me.getParentItem();
						while (parentItem != null) {
							parentItem.setChecked(true);
							parentItem = parentItem.getParentItem();
						}
						for (final TreeItem item : me.getItems()) {
							item.setChecked(true);
							final Event e = new Event();
							e.item = item;
							this.handleEvent(e);
						}
					} else {
						for (final TreeItem item : me.getItems()) {
							item.setChecked(false);
							final Event e = new Event();
							e.item = item;
							this.handleEvent(e);
						}
					}
				}
			};

			remoteTree = new Tree(shell, SWT.CHECK | SWT.VIRTUAL | SWT.MULTI);
			remoteTree.setLayoutData(new GridData(GridData.FILL_BOTH));

			final Button copyLocalToRemote = new Button(shell, SWT.PUSH
					| SWT.BORDER);
			copyLocalToRemote.setLayoutData(new GridData(
					GridData.HORIZONTAL_ALIGN_END));
			copyLocalToRemote.setText(Messages.SMOACustomSyncAction_FromLocal);

			copyLocalToRemote.addSelectionListener(new SelectionListener() {

				public void widgetDefaultSelected(SelectionEvent arg0) {
				}

				public void widgetSelected(SelectionEvent arg0) {
					toRemote();
					// saveTrees(project);
				}
			});

			final Button copyRemoteToLoacal = new Button(shell, SWT.PUSH
					| SWT.BORDER);
			copyRemoteToLoacal.setLayoutData(new GridData(
					GridData.HORIZONTAL_ALIGN_BEGINNING));
			copyRemoteToLoacal.setText(Messages.SMOACustomSyncAction_FromRemote);

			copyRemoteToLoacal.addSelectionListener(new SelectionListener() {

				public void widgetDefaultSelected(SelectionEvent arg0) {
				}

				public void widgetSelected(SelectionEvent arg0) {
					fromRemote();
					// saveTrees(project);
				}
			});

			final Composite options = new Composite(shell, SWT.NONE);
			GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
			gridData.horizontalSpan = 2;
			options.setLayoutData(gridData);
			options.setLayout(new GridLayout(2, false));
			new Label(options, SWT.NONE).setText(Messages.SMOACustomSyncAction_FileOverwritePolicy);

			policy = new Combo(options, SWT.BORDER | SWT.READ_ONLY);
			for (final OverwritePolicy p : OverwritePolicy.values()) {
				policy.add(p.name());
			}

			final String policyVal = project
					.getPersistentProperty(SMOA_SYNC_OVERWRITE);
			if (policyVal != null) {
				policy.select(policy.indexOf(policyVal));
			} else {
				policy.select(policy.indexOf(OverwritePolicy.Always.name()));
			}

			assert OverwritePolicy.valueOf(policy.getText()) != null;
			overwrite = OverwritePolicy.valueOf(policy.getText());

			policy.addSelectionListener(new SelectionListener() {
				public void widgetDefaultSelected(SelectionEvent arg0) {
				}

				public void widgetSelected(SelectionEvent arg0) {
					try {
						assert OverwritePolicy.valueOf(policy.getText()) != null;
						overwrite = OverwritePolicy.valueOf(policy.getText());
						project.setPersistentProperty(SMOA_SYNC_OVERWRITE,
								policy.getText());
					} catch (final CoreException e) {
						final MessageBox mb = new MessageBox(localTree.getShell(),
								SWT.ICON_ERROR | SWT.OK);
						mb.setText(Messages.SMOACustomSyncAction_ErrorOverwritePolicyTitle);
						mb.setMessage(e.getLocalizedMessage());
						mb.open();
					}
				}
			});

			final Shell progress = new Shell(topShell);
			final GridLayout layout = new GridLayout();
			progress.setLayout(layout);
			final Label msg = new Label(progress, SWT.NONE);
			msg.setText(Messages.SMOACustomSyncAction_PleaswWait);
			gridData = new GridData(GridData.FILL_BOTH);
			gridData.minimumWidth = 300;
			msg.setLayoutData(gridData);
			progress.pack();
			progress.open();

			lfs = rootFromProject(localTree, project, msg);
			rootFromFileStore(remoteTree, sfs, msg);

			if (!progress.isDisposed()) {
				progress.close();
			}

			restoreTrees(project);

			localTree.addListener(SWT.Selection, treeListener);
			remoteTree.addListener(SWT.Selection, treeListener);

			shell.open();

			shell.addListener(SWT.Close, new Listener() {
				public void handleEvent(Event event) {
					saveTrees(project);
				}
			});

			return null;
		} catch (final CoreException e) {
			NotifyShell.open(Messages.SMOACustomSyncAction_ExceptionBySynchro, e.getLocalizedMessage());
			throw new ExecutionException(e.getLocalizedMessage(), e);
		}
	}

	private TreeItem findChild(Tree parent, String text) {
		for (final TreeItem it : parent.getItems()) {
			if (it.getText().equals(text)) {
				return it;
			}
		}
		return null;
	}

	private TreeItem findChild(TreeItem parent, String text) {
		for (final TreeItem it : parent.getItems()) {
			if (it.getText().equals(text)) {
				return it;
			}
		}
		return null;
	}

	/** Calls synchronise in local ← remote direction */
	protected void fromRemote() {
		synchronise(getCheckedItems(remoteTree), lfs, localTree);
	}

	/**
	 * Returns array with relative paths to the checked items
	 */
	private TreeItem[] getCheckedItems(Tree tree) {
		final Vector<TreeItem> v = new Vector<TreeItem>();
		for (final TreeItem it : tree.getItems()) {
			v.addAll(getCheckedItems(it));
		}
		return v.toArray(new TreeItem[v.size()]);
	}

	/**
	 * Returns all checked items under this item
	 */
	private Vector<TreeItem> getCheckedItems(TreeItem treeItem) {
		final Vector<TreeItem> v = new Vector<TreeItem>();
		for (final TreeItem it : treeItem.getItems()) {
			if (it.getChecked()) {
				v.add(it);
			}
			v.addAll(getCheckedItems(it));
		}
		return v;
	}

	/** Recursively builds up relative path representing the tree item */
	String getRelPath(TreeItem item) {
		if (item.getParentItem() != null) {
			return getRelPath(item.getParentItem()) + "/" + item.getText(); //$NON-NLS-1$
		}
		return "."; //$NON-NLS-1$
	}

	/**
	 * Recursively and eagerly fills the tree with store and it's children
	 */
	private void populate(TreeItem tree, IFileStore store, Label msg)
			throws CoreException {
		counter++;
		final TreeItem treeItem = new TreeItem(tree, SWT.NONE);
		treeItem.setText(store.getName());
		treeItem.setData(store);

		if (msg != null && !msg.isDisposed()) {
			msg.setText(Messages.SMOACustomSyncAction_RegeneratingTree_FoundFiles + counter);
			msg.redraw();
			msg.getShell().redraw();
			msg.getDisplay().update();
		}

		if (store instanceof SMOAFileStore || store.fetchInfo().isDirectory()) {
			for (final IFileStore fs : store.childStores(0, null)) {
				populate(treeItem, fs, msg);
			}
		}
	}

	/**
	 * Reads the state of trees from project and applies to the trees
	 */
	private void restoreTrees(IProject project) {
		try {
			final BufferedReader in = new BufferedReader(new FileReader(project
					.getLocation().toString() + "/" + SMOA_SYNC_FILELIST_NAME)); //$NON-NLS-1$

			if (!"[Local]".equals(in.readLine())) { //$NON-NLS-1$
				throw new FileFormatException(
						"First line does not contain " + "[Local]"); //$NON-NLS-1$ //$NON-NLS-2$
			}

			int c;
			while ((c = in.read()) == '\t') {
				final String line = in.readLine();
				if (line == null) {
					return;
				}
				selectFromPath(localTree, line);
			}

			if (c == -1) {
				return;
			}

			if (c != '[') {
				throw new FileFormatException("Illegal line start (" + c + ")"); //$NON-NLS-1$ //$NON-NLS-2$
			}

			final String rem = in.readLine();
			if (!"Remote]".equals(rem)) { //$NON-NLS-1$
				throw new FileFormatException("Invalid line [" + rem); //$NON-NLS-1$
			}

			while ((c = in.read()) == '\t') {
				final String line = in.readLine();
				if (line == null) {
					return;
				}
				selectFromPath(remoteTree, line);
			}

			if (c == -1) {
				return;
			}

			throw new FileFormatException("Illegal line start (" + c + ")"); //$NON-NLS-1$ //$NON-NLS-2$

		} catch (final FileNotFoundException e) {
			// first run
		} catch (final IOException e) {
			final MessageBox mb = new MessageBox(localTree.getShell(), SWT.ICON_ERROR
					| SWT.OK);
			mb.setText(Messages.SMOACustomSyncAction_ExceptionByReadingSettings);
			mb.setMessage(e.getLocalizedMessage());
			mb.open();
		}
	}

	/**
	 * Starts filling the tree basing on the given store. Updates the message
	 * label with progress.
	 */
	private void rootFromFileStore(Tree tree, IFileStore store, Label msg)
			throws CoreException {

		counter = 0;
		final TreeItem treeItem = new TreeItem(tree, SWT.NONE);
		treeItem.setText(store.getName());
		treeItem.setData(store);
		for (final IFileStore fs : store.childStores(0, null)) {
			populate(treeItem, fs, msg);
		}

	}

	/**
	 * Starts filling the tree basing on the project location. Updates the
	 * message label with progress.
	 */
	private IFileStore rootFromProject(Tree tree, IProject project, Label msg)
			throws CoreException {
		final IFileStore store = EFS.getLocalFileSystem().getStore(
				project.getLocation());

		rootFromFileStore(tree, store, msg);
		return store;
	}

	/**
	 * Records the state of trees in Project
	 */
	protected void saveTrees(IProject project) {

		try {
			final DataOutputStream out = new DataOutputStream(new FileOutputStream(
					project.getLocation().toString() + "/" //$NON-NLS-1$
							+ SMOA_SYNC_FILELIST_NAME));

			out.writeBytes("[Local]\n"); //$NON-NLS-1$

			for (final TreeItem item : getCheckedItems(localTree)) {
				out.write('\t');
				out.writeBytes(getRelPath(item));
				out.write('\n');
			}

			out.writeBytes("[Remote]\n"); //$NON-NLS-1$
			for (final TreeItem item : getCheckedItems(remoteTree)) {
				out.write('\t');
				out.writeBytes(getRelPath(item));
				out.write('\n');
			}

		} catch (final IOException e) {
			final MessageBox mb = new MessageBox(localTree.getShell(), SWT.ICON_ERROR
					| SWT.OK);
			mb.setText(Messages.SMOACustomSyncAction_ExceptionByWritingSettings);
			mb.setMessage(e.getLocalizedMessage());
			mb.open();
		}
	}

	/**
	 * Looks for a TreeItem corresponding to given path in given tree and
	 * selects it.
	 */
	private void selectFromPath(Tree tree, String file) {
		TreeItem root = tree.getItems()[0];
		TreeItem next = root;
		root.setChecked(true);
		final String[] path = new Path(file).segments();
		for (int i = 0; i < path.length; ++i) {
			for (final TreeItem item : root.getItems()) {
				if (item.getText().equals(path[i])) {
					next = item;
				}
			}
			if (next == root) {
				return;
			}
			root = next;
		}
		next.setChecked(true);
	}

	/**
	 * Copies files (given as array of {@link TreeItem}s) to another tree, given
	 * as the tree and it's starting store
	 * 
	 * @param items
	 *            - what to copy
	 * @param store
	 *            - where to copy
	 * @param toRegenerate
	 *            - which tree to update
	 */
	protected void synchronise(TreeItem[] items, IFileStore store,
			Tree toRegenerate) {

		final Shell topShell = Display.getCurrent().getActiveShell();
		final Shell shell = new Shell(topShell, new Shell().getStyle()
				| SWT.APPLICATION_MODAL);
		shell.setText(Messages.SMOACustomSyncAction_ProgressWindowTitle);

		shell.setLayout(new GridLayout(2, false));

		new Label(shell, SWT.NONE).setText(Messages.SMOACustomSyncAction_FilesTotal);
		new Label(shell, SWT.NONE).setText(Integer.toString(items.length));
		new Label(shell, SWT.NONE).setText(Messages.SMOACustomSyncAction_FilesCopied);
		final Label done = new Label(shell, SWT.NONE);
		done.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		new Label(shell, SWT.NONE).setText(Messages.SMOACustomSyncAction_CurrentFile);
		final Label current = new Label(shell, SWT.NONE);
		final GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.minimumWidth = 500;
		current.setLayoutData(gd);

		int doneVal = -1;

		shell.pack(true);
		shell.open();

		try {
			for (final TreeItem treeItem : items) {
				if (!shell.isDisposed()) {
					done.setText(Integer.toString(++doneVal));
					current.setText(getRelPath(treeItem));

					shell.pack(true);
					done.redraw();
					current.redraw();
					shell.redraw();
					shell.getDisplay().update();
				}
				copyTreeItem(treeItem, store, toRegenerate);
			}

			if (!shell.isDisposed()) {
				done.setText(Integer.toString(++doneVal));
				current.setText(Messages.SMOACustomSyncAction_RegeneratingTree);

				shell.pack(true);
				done.redraw();
				current.redraw();
				shell.redraw();
				shell.getDisplay().update();
			}

		} catch (final CoreException e) {
			final MessageBox mb = new MessageBox(Display.getCurrent()
					.getActiveShell(), SWT.ICON_ERROR | SWT.OK);
			mb.setText(Messages.SMOACustomSyncAction_ErrorDialogTitle);
			mb.setMessage(e.getMessage());
			mb.open();
		}

		if (!shell.isDisposed()) {
			shell.close();
		}

	}

	/** Calls synchronise in local → remote direction */
	protected void toRemote() {
		synchronise(getCheckedItems(localTree), sfs, remoteTree);
	}
}

Back to the top