summaryrefslogtreecommitdiff
path: root/src/bjc/imgchain/pipeline/stages/LoadStage.java
blob: d34b41916d2ff385a9c7fcf2d99d221985b8b892 (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
94
package bjc.imgchain.pipeline.stages;

import java.awt.BorderLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bjc.imgchain.pipeline.StageType;

/**
 * Stage which loads an image.
 * @author Ben Culkin
 *
 */
public class LoadStage extends AbstractPipelineStage {
	private String fileName;

	/**
	 * Create a new stage which loads an image.
	 */
	public LoadStage() {
		super(StageType.IMGSOURCE);
	}

	@Override
	public Image process(Image inp) {
		try {
			return ImageIO.read(new URL("file://" + fileName));
		} catch (IOException e) {
			String msg = String.format("Error: Could not load image %s", fileName);

			System.out.printf("%s\n", msg);

			e.printStackTrace();

			JOptionPane.showInternalMessageDialog(null, msg, "Error loading image",
					JOptionPane.ERROR_MESSAGE);
		}

		return inp;
	}

	@Override
	public String name() {
		return "Load Image";
	}

	@Override
	public String description() {
		return "Load an image from a file";
	}

	@Override
	public JComponent getEditor() {
		JPanel holder = new JPanel();
		holder.setLayout(new BorderLayout());

		JLabel fileLabel = new JLabel("File");

		JTextField fileField = new JTextField(80);
		fileField.addPropertyChangeListener("value", (ev) -> {
			fileName = fileField.getText();
		});

		JButton fileButton = new JButton("Select File");
		fileButton.addActionListener((ev) -> {
			JFileChooser jfc = new JFileChooser();
			jfc.setMultiSelectionEnabled(false);

			int res = jfc.showOpenDialog(holder);

			if (res != JFileChooser.APPROVE_OPTION) {
				return;
			}

			fileField.setText(jfc.getSelectedFile().getAbsolutePath());
		});

		holder.add(fileLabel, BorderLayout.LINE_START);
		holder.add(fileField, BorderLayout.CENTER);
		holder.add(fileButton, BorderLayout.LINE_END);

		return holder;
	}
}