summaryrefslogtreecommitdiff
path: root/src/bjc/imgchain/ImgChain.java
blob: a3a971226915f9cfe8a24678fa81c302865b6346 (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
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
package bjc.imgchain;

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import bjc.imgchain.pipeline.Pipeline;

/**
 * Main class for ImgChain
 * 
 * @author acm
 *
 */
public class ImgChain {
	/*
	 * Action to perform when loading an image.
	 */
	private final class LoadImageListener implements ActionListener {
		public LoadImageListener() {
			// TODO Auto-generated constructor stub
		}

		@Override
		public void actionPerformed(ActionEvent ev) {
			JFileChooser jfc = new JFileChooser();
			jfc.setMultiSelectionEnabled(true);

			int res = jfc.showOpenDialog(desktop);

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

			for (File fle : jfc.getSelectedFiles()) {
				ImgViewer view = new ImgViewer(ImgChain.this, fle);

				if (view.isInitialized()) {
					desktop.add(view);
					view.setVisible(true);
				}
			}
		}
	}

	/**
	 * The desktop everything is attached to.
	 */
	public JDesktopPane desktop;

	/*
	 * The storage for images.
	 */
	public final Map<String, Image> imageRepo;

	/*
	 * The storage for images.
	 */
	public final Map<String, Pipeline> pipelineRepo;

	/**
	 * The image chain instance.
	 */
	public static ImgChain chan;

	public JFrame frame;

	/**
	 * Main method
	 * 
	 * @param args
	 *                Unused CLI args
	 */
	public static void main(String[] args) {
		System.out.println("ImgChain Loading...");

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		chan = new ImgChain();

		chan.setupGUI();
	}

	/**
	 * Initialize image repo
	 */
	public ImgChain() {
		imageRepo = new HashMap<>();
		pipelineRepo = new HashMap<>();
	}

	/*
	 * Setup the GUI
	 */
	private void setupGUI() {
		frame = new JFrame("ImgChain v1");
		frame.setLayout(new GridLayout(1, 1));

		desktop = new JDesktopPane();

		JMenuBar menu = setupMenubar(frame);

		frame.setJMenuBar(menu);

		frame.add(desktop);

		frame.setSize(640, 480);

		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		frame.setVisible(true);
	}

	/*
	 * Setup the menubar.
	 */
	private JMenuBar setupMenubar(JFrame frame) {
		JMenuBar menu = new JMenuBar();

		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');

		JMenuItem aboutItem = new JMenuItem("About");
		aboutItem.setMnemonic('A');
		aboutItem.addActionListener((ev) -> {
			JOptionPane.showMessageDialog(frame, "ImgChain v2\nDeveloped by Benjamin Culkin",
					"About ImgChain", JOptionPane.INFORMATION_MESSAGE);
		});

		JMenuItem closeItem = new JMenuItem("Close");
		closeItem.setMnemonic('C');
		closeItem.addActionListener((ev) -> {
			frame.dispose();
		});

		fileMenu.add(aboutItem);
		fileMenu.addSeparator();
		fileMenu.add(closeItem);

		JMenu imageMenu = new JMenu("Images");
		imageMenu.setMnemonic('I');

		JMenuItem loadImage = new JMenuItem("Load Images...");
		loadImage.setMnemonic('L');
		loadImage.addActionListener(new LoadImageListener());

		imageMenu.add(loadImage);

		JMenu pipelineMenu = new JMenu("Pipelines");
		JMenuItem createPipe = new JMenuItem("Create Pipeline");
		createPipe.setMnemonic('C');
		createPipe.addActionListener((ev) -> {
			ImgPipeline pip = new ImgPipeline();

			desktop.add(pip);
			pip.setVisible(true);
		});

		pipelineMenu.add(createPipe);

		menu.add(fileMenu);
		menu.add(imageMenu);
		menu.add(pipelineMenu);

		return menu;
	}

	/**
	 * Add an image to the image repository.
	 * 
	 * @param name
	 *                The name of the image
	 * @param img
	 *                The image to add.
	 */
	public void addImage(String name, Image img) {
		if (imageRepo.containsKey(name)) {
			String msg = String.format("Are you sure you want to overwrite stored image '%s'?", name);

			if (JOptionPane.showInternalConfirmDialog(desktop, msg) != JOptionPane.OK_OPTION) {
				return;
			}
		}

		imageRepo.put(name, img);
	}

	/**
	 * Add an pipeline to the pipeline repository.
	 * 
	 * @param pipe
	 *                The pipeline to add.
	 */
	public void addPipe(Pipeline pipe) {
		String name = pipe.name();

		if (pipelineRepo.containsKey(name)) {
			String msg = String.format("Are you sure you want to overwrite stored pipeline '%s'?", name);

			if (JOptionPane.showInternalConfirmDialog(desktop, msg) != JOptionPane.OK_OPTION) {
				return;
			}
		}

		pipelineRepo.put(name, pipe);
	}
}