summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/MainMenuWindow.java
blob: 066ef54c4e23d5e945e99d480e132b473f8189c3 (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
package org.openslx.dozmod.gui.window;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import org.openslx.dozmod.App;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.MainWindow;
import org.openslx.dozmod.gui.window.layout.MainMenuWindowLayout;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

import com.google.gson.Gson;

@SuppressWarnings("serial")
public class MainMenuWindow extends MainMenuWindowLayout {

	public MainMenuWindow() {
		super();
		// function for vmButton
		vmButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MainWindow.showPage(ImageListWindow.class);
			}
		});

		// function for lecturesButton
		lecturesButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MainWindow.showPage(LectureListWindow.class);
			}
		});
	}

	@Override
	public boolean requestHide() {
		return true;
	}

	// Stuff start

	private boolean once = false;

	private class GMain {
		private GResponse responseData;
	}

	private class GResponse {
		private GResult[] results;
	}

	private class GResult {
		private String tbUrl;
	}

	private int loadImageToControl(int start, GResult[] results, final JButton control) {
		if (start >= results.length)
			start = results.length - 1;
		while (start < results.length) {
			try {
				URL oracle = new URL(results[start].tbUrl);
				final ImageIcon img = new ImageIcon(oracle);
				Gui.asyncExec(new Runnable() {
					@Override
					public void run() {
						control.setIcon(img);
						control.getParent().validate();
					}
				});
				break;
			} catch (Exception e) {
			}
			start++;
		}
		return start;
	}

	// Stuff end

	@Override
	public void requestShow() {
		// All stuff is stuff
		if (once)
			return;
		once = true;
		App.waitForInit();
		QuickTimer.scheduleOnce(new Task() {
			String[] terms = new String[] { "lehrpool", "dnbd3", "lehrstuhl", "dozmod", "lehrpool-suite",
					"java", "hund", "katze", "maus" };

			@Override
			public void fire() {
				String term = terms[(int) Math.floor(Math.random() * terms.length)] + "+"
						+ terms[(int) Math.floor(Math.random() * terms.length)];
				try {
					URL oracle = new URL(
							"http://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q=" + term);
					Reader reader = new InputStreamReader(oracle.openStream());
					Gson gson = new Gson();
					GMain data = gson.fromJson(reader, GMain.class);
					int used = loadImageToControl(0, data.responseData.results, lecturesButton);
					loadImageToControl(used + 1, data.responseData.results, vmButton);
				} catch (IOException e) {
					return;
				}
			}
		});
	}

}