1
/*
2
 * Copyright (C) 2011 Openismus GmbH
3
 *
4
 * This file is part of GWT-Glom.
5
 *
6
 * GWT-Glom is free software: you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by the
8
 * Free Software Foundation, either version 3 of the License, or (at your
9
 * option) any later version.
10
 *
11
 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14
 * for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
package org.glom.web.client.activity;
21
22
import org.glom.web.client.ClientFactory;
23
import org.glom.web.client.OnlineGlomServiceAsync;
24
import org.glom.web.client.ui.DocumentSelectionView;
25
import org.glom.web.client.ui.View;
26
import org.glom.web.shared.Documents;
27
28
import com.google.gwt.activity.shared.AbstractActivity;
29
import com.google.gwt.event.shared.EventBus;
30
import com.google.gwt.place.shared.Place;
31
import com.google.gwt.user.client.Window;
32
import com.google.gwt.user.client.rpc.AsyncCallback;
33
import com.google.gwt.user.client.ui.AcceptsOneWidget;
34
35
public class DocumentSelectionActivity extends AbstractActivity implements View.Presenter {
36
37
	// TODO inject with GIN
38
	private final ClientFactory clientFactory;
39
	private final DocumentSelectionView documentSelectionView;
40
41
	public DocumentSelectionActivity(final ClientFactory clientFactory) {
42
		this.clientFactory = clientFactory;
43
		this.documentSelectionView = clientFactory.getDocumentSelectionView();
44
	}
45
46
	@Override
47
	public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
48
		Window.setTitle("Online Glom");
49
50
		documentSelectionView.setPresenter(this);
51
52
		final AsyncCallback<Documents> callback = new AsyncCallback<Documents>() {
53
			@Override
54
			public void onFailure(final Throwable caught) {
55
				// Try to get an error message. Most likely this won't work but it's worth a try.
56
				getAndSetErrorMessage();
57
			}
58
59
			@Override
60
			public void onSuccess(final Documents documents) {
61
				documentSelectionView.clearHyperLinks();
62
				if (documents.getCount() > 0) {
63
					for (int i = 0; i < documents.getCount(); i++) {
64
						documentSelectionView
65
								.addDocumentLink(documents.getDocumentID(i), documents.getTitle(i));
66
						// TODO: Get default locale.
67
					}
68
				} else {
69
					getAndSetErrorMessage();
70
				}
71
			}
72
		};
73
		OnlineGlomServiceAsync.Util.getInstance().getDocuments(callback);
74
75
		panel.setWidget(documentSelectionView.asWidget());
76
	}
77
78
	@Override
79
	public void goTo(final Place place) {
80
		clientFactory.getPlaceController().goTo(place);
81
	}
82
83
	private void getAndSetErrorMessage() {
84
85
		final AsyncCallback<String> callback = new AsyncCallback<String>() {
86
			@Override
87
			public void onFailure(final Throwable caught) {
88
				documentSelectionView
89
						.setErrorMessage("Unable to communicate with the servlet. Check the error log for more information.");
90
			}
91
92
			@Override
93
			public void onSuccess(final String errorMessage) {
94
				documentSelectionView.setErrorMessage("Configuration Error: " + errorMessage);
95
			}
96
		};
97
		OnlineGlomServiceAsync.Util.getInstance().getConfigurationErrorMessage(callback);
98
99
	}
100
}