summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadSummaryPage.java
blob: 9bac2a17e60eaefa6b3fd9ab5860a7c4af3ff445 (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
package org.openslx.dozmod.gui.wizard.page;

import java.awt.KeyboardFocusManager;
import java.awt.Window;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.ImageSummaryRead;
import org.openslx.bwlp.thrift.iface.TransferState;
import org.openslx.dozmod.Branding;
import org.openslx.dozmod.filetransfer.TransferEvent;
import org.openslx.dozmod.filetransfer.TransferEventListener;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.MainWindow;
import org.openslx.dozmod.gui.wizard.LectureWizard;
import org.openslx.dozmod.gui.wizard.Wizard;
import org.openslx.dozmod.gui.wizard.layout.ImageUploadSummaryPageLayout;
import org.openslx.dozmod.state.UploadWizardState;
import org.openslx.dozmod.thrift.UploadInitiator.UploadInitState;
import org.openslx.dozmod.thrift.cache.ImageCache;
import org.openslx.util.QuickTimer;

@SuppressWarnings("serial")
public class ImageUploadSummaryPage extends ImageUploadSummaryPageLayout {

	private static final Logger LOGGER = Logger.getLogger(ImageUploadSummaryPage.class);

	private boolean pageIsVisible = false;
	private boolean uploadListenerAdded = false;
	private boolean allowCreateLecture = false;
	private final UploadWizardState state;

	private final TransferEventListener uploadListener = new TransferEventListener() {
		@Override
		public void update(TransferEvent event) {
			LOGGER.debug("update transfer event");
			if (!pageIsVisible)
				return;
			if (event.progress != null) {
				ctlUploadProgressBar.setStatus(event.progress);
			}
			if (event.state == TransferState.FINISHED) {
				updateInfoText(null);
				state.upload.getUploadTask().removeListener(uploadListener);
			}
		}
	};

	private final QuickTimer.Task updateHelpText = new QuickTimer.Task() {
		private UploadInitState lastInitState = null;

		@Override
		public void fire() {
			LOGGER.debug("update help text");
			if (!pageIsVisible) {
				this.cancel();
				return;
			}
			if (!uploadListenerAdded && state.upload.getUploadTask() != null) {
				uploadListenerAdded = true;
				state.upload.getUploadTask().addListener(uploadListener);
			}
			UploadInitState initState = state.upload.getState();
			if (initState != lastInitState) {
				lastInitState = initState;
				updateInfoText(initState);
			}
			if (lastInitState == UploadInitState.UPLOAD_STARTED || lastInitState == UploadInitState.ERROR) {
				this.cancel();
			}
		}
	};

	private void updateInfoText(final UploadInitState initState) {
		String text;
		if (state.upload.getUploadTask() != null && state.upload.getUploadTask().isComplete()) {
			text = "Die Virtuelle Maschine wurde erfolgreich auf den Server hochgeladen.";
		} else if (initState == null) {
			return;
		} else {
			switch (initState) {
			case REQUESTING:
				text = "Der Upload-Vorgang wird mit dem Server ausgehandelt...";
				break;
			case WAITING_FOR_SLOT:
				text = "Der Server ist zur Zeit überlastet, da zu viele Uploads gleichzeitig laufen."
						+ " Es wird gewartet, bis der Server wieder Kapazitäten frei hat."
						+ " Bitte schließen Sie dieses Fenster nicht.";
				break;
			case UPLOAD_STARTING:
				text = "Die Verbindung zur Übertragung der VM wird aufgebaut...";
				break;
			case UPLOAD_STARTED:
				text = "Der Upload Ihrer Virtuellen Maschine wurde gestartet."
						+ " Wenn Sie möchten, können Sie diesen Assistenten schließen,"
						+ " und die Anwendung weiterverwenden."
						+ " Die Übertragung läuft dabei im Hintergrund weiter.";
				break;
			case ERROR:
				text = "Die Initialisierung des Uploads auf den Server ist fehlgeschlagen."
						+ " Sie können versuchen, den Vorgang erneut zu starten."
						+ " Falls das Problem weiterhin besteht,"
						+ " kontaktieren Sie den " + Branding.getServiceName() + "-Support Ihrer Einrichtung.";
				if (state.upload.getErrorMessage() != null) {
					text += "<br><br>Weitere Informationen:<br>" + state.upload.getErrorMessage();
				}
				break;
			default:
				return;
			}
		}
		final String t = text;
		Gui.asyncExec(new Runnable() {
			@Override
			public void run() {
				lblHelpText.setText("<html><body style ='width:100%'>" + t + "</body></html>");
				if (initState == UploadInitState.UPLOAD_STARTED) {
					setPageComplete(true);
				}
				if (state.upload.getUploadTask() != null && state.upload.getUploadTask().isComplete()) {
					chkCreateLecture.setVisible(allowCreateLecture);
				}

			}
		});
	}

	public ImageUploadSummaryPage(Wizard wizard, UploadWizardState state, boolean allowCreateLecture) {
		super(wizard);
		this.state = state;
		this.allowCreateLecture = allowCreateLecture;
		setPageComplete(false);
	}

	@Override
	protected void onPageEnter() {
		if (pageIsVisible)
			return;
		pageIsVisible = true;
		updateInfoText(UploadInitState.REQUESTING);
		QuickTimer.scheduleAtFixedDelay(updateHelpText, 500, 3000);
	}

	@Override
	protected void onPageLeave() {
		if (!pageIsVisible)
			return;
		pageIsVisible = false;

		// optional lectureWizard
		state.upload.getUploadTask().removeListener(uploadListener);
		if (!wizard.isCancelled() && !state.upload.getUploadTask().isCanceled()) {
			MainWindow.addUpload(state);
		}
		if (!wizard.isCancelled() && chkCreateLecture.isSelected()) {
			wizard.setVisible(false);
			Window parent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
			ImageSummaryRead image = null;
			for (ImageSummaryRead tmpImage : ImageCache.get(true)) {
				if (tmpImage.imageBaseId.equals(state.uuid)) {
					image = tmpImage;
					break;
				}
			}
			if (image != null) {
				new LectureWizard(parent, image, state.upload.getToken()).setVisible(true);
			} else {
				LOGGER.error("Couldn't find the new image in the image cache.");
				//TODO could this happen? Show messagebox then?
			}
		}
	}

}