summaryrefslogtreecommitdiff
path: root/demos/java/gsviewer/src/com/artifex/gsviewer/ViewerController.java
blob: 6353d5849718fa2489882e6ef5306b16dbb675db (plain)
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
package com.artifex.gsviewer;

import java.awt.Point;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import com.artifex.gsviewer.gui.ViewerGUIListener;
import com.artifex.gsviewer.gui.ViewerWindow;

public class ViewerController implements ViewerGUIListener {

	private ViewerWindow source;
	private Document currentDocument;

	public void open(final File file) {
		if (currentDocument != null)
			close();
		Document.loadDocumentAsync(file, (final Document doc, final Exception exception) -> {
			source.setLoadProgress(0);
			if (exception != null) {
				JOptionPane.showMessageDialog(source, exception.toString(),
						"Failed to load", JOptionPane.ERROR_MESSAGE);
			} else {
				System.out.println("Loaded document");
				this.currentDocument = doc;
				source.loadDocumentToViewer(doc);
			}
		}, (int progress) -> {
			source.setLoadProgress(progress);
		});
	}

	public void close() {
		if (currentDocument != null) {
			source.loadDocumentToViewer(null);
			currentDocument.unload();
			currentDocument = null;
		}
	}

	public Document getCurrentDocument() {
		return currentDocument;
	}

	@Override
	public void onViewerAdd(ViewerWindow source) {
		this.source = source;
	}

	@Override
	public void onPageChange(int oldPage, int newPage) {
		System.out.println("Page change: " + oldPage + " to " + newPage);
	}

	@Override
	public void onZoomChange(double oldZoom, double newZoom) {
		System.out.println("Zoom change: " + oldZoom + " to " + newZoom);
	}

	@Override
	public void onScrollChange(Point oldScroll, Point newScroll) {
		System.out.println("Scroll change: " + oldScroll + " to " + newScroll);
	}

	@Override
	public void onOpenFile() {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileFilter(GSFileFilter.INSTANCE);
		chooser.setCurrentDirectory(new File("").getAbsoluteFile());
		int status = chooser.showOpenDialog(source);
		if (status == JFileChooser.APPROVE_OPTION)
			open(chooser.getSelectedFile());
	}

	@Override
	public void onCloseFile() {
		close();
	}

	@Override
	public void onClosing() {
		source.dispose();
		System.exit(0);
	}

	@Override
	public void onSettingsOpen() {
		System.out.println("Settings open");
	}

}