package org.techo.dgna.client;
import org.techo.dgna.utils.LoginInfo;
import com.google.api.gwt.oauth2.client.Auth;
import com.google.api.gwt.oauth2.client.AuthRequest;
import com.google.api.gwt.oauth2.client.Callback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ToggleButton;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class TechoDGnA implements EntryPoint {
/**
* The message displayed to the user when the server cannot be reached or
* returns an error.
*/
private static final String SERVER_ERROR = "An error occurred while "
+ "attempting to contact the server. Please check your network "
+ "connection and try again.";
private static final Auth AUTH = Auth.get();
private static final String GOOGLE_AUTH_URL =
"https://accounts.google.com/o/oauth2/auth";
private static final String GOOGLE_CLIENT_ID =
"165856365230.apps.googleusercontent.com";
private static final String PLUS_ME_SCOPE =
"https://www.googleapis.com/auth/userinfo.profile";
private final RootPanel rootPanel = RootPanel.get("main-panel");
private final TabLayoutPanel tabLayoutPanel = new TabLayoutPanel(2, Unit.EM);
/*** Controls auth **/
private final AbsolutePanel loginPanel = new AbsolutePanel();
private final Anchor signInLink = new Anchor("");
private final Image loginImage = new Image();
private final TextBox nameField = new TextBox();
/**
* Create a remote service proxy to talk to the server-side Greeting service.
*/
private final GreetingServiceAsync greetingService = GWT
.create(GreetingService.class);
/**
* This is the entry point method.
*/
public void onModuleLoad() {
// Add the nameField and sendButton to the RootPanel
// Use RootPanel.get() to get the entire body element
//rootPanel = RootPanel.get("main-panel");
rootPanel.setSize("1000px", "800px");
// tabLayoutPanel = new TabLayoutPanel(1.5, Unit.EM);
tabLayoutPanel.setVisible(false);
tabLayoutPanel.setAnimationDuration(3);
tabLayoutPanel.setAnimationVertical(true);
tabLayoutPanel.setSize("1000px", "600px");
AbsolutePanel absolutePanel_1 = new AbsolutePanel();
tabLayoutPanel.add(absolutePanel_1, "Detección", false);
final ToggleButton btnComunidades = new ToggleButton("Comunidades");
btnComunidades.setDown(true);
absolutePanel_1.add(btnComunidades, 10, 10);
final ToggleButton btnFamilias = new ToggleButton("Familias");
absolutePanel_1.add(btnFamilias, 111, 10);
TextBox textBox = new TextBox();
absolutePanel_1.add(textBox, 699, 10);
textBox.setSize("212px", "15px");
Button btnBuscar = new Button("Buscar");
absolutePanel_1.add(btnBuscar, 931, 10);
final AbsolutePanel comunidadesPanel = new AbsolutePanel();
absolutePanel_1.add(comunidadesPanel, 10, 50);
comunidadesPanel.setSize("978px", "520px");
DecoratedTabPanel decoratedTabPanel = new DecoratedTabPanel();
comunidadesPanel.add(decoratedTabPanel, 10, 10);
decoratedTabPanel.setSize("958px", "500px");
AbsolutePanel absolutePanel_9 = new AbsolutePanel();
decoratedTabPanel.add(absolutePanel_9, "Listado de comunidades", false);
decoratedTabPanel.selectTab(0);
absolutePanel_9.setSize("946px", "458px");
AbsolutePanel absolutePanel_10 = new AbsolutePanel();
decoratedTabPanel.add(absolutePanel_10, "Panoramas", false);
absolutePanel_10.setSize("947px", "458px");
AbsolutePanel absolutePanel_11 = new AbsolutePanel();
decoratedTabPanel.add(absolutePanel_11, "Insertar ficha de comunidad", false);
absolutePanel_11.setSize("948px", "460px");
AbsolutePanel absolutePanel_12 = new AbsolutePanel();
decoratedTabPanel.add(absolutePanel_12, "Mapa", false);
absolutePanel_12.setSize("946px", "460px");
AbsolutePanel absolutePanel_13 = new AbsolutePanel();
decoratedTabPanel.add(absolutePanel_13, "Fotos", false);
absolutePanel_13.setSize("947px", "462px");
AbsolutePanel absolutePanel_14 = new AbsolutePanel();
decoratedTabPanel.add(absolutePanel_14, "Bitacora", false);
absolutePanel_14.setSize("948px", "462px");
AbsolutePanel absolutePanel_2 = new AbsolutePanel();
tabLayoutPanel.add(absolutePanel_2, "Eventos", false);
AbsolutePanel absolutePanel_3 = new AbsolutePanel();
tabLayoutPanel.add(absolutePanel_3, "Investigación", false);
AbsolutePanel absolutePanel_4 = new AbsolutePanel();
tabLayoutPanel.add(absolutePanel_4, "New Widget", false);
AbsolutePanel absolutePanel_5 = new AbsolutePanel();
tabLayoutPanel.add(absolutePanel_5, "Estadísticas", false);
AbsolutePanel absolutePanel_6 = new AbsolutePanel();
tabLayoutPanel.add(absolutePanel_6, "Vinculaciones", false);
AbsolutePanel absolutePanel_7 = new AbsolutePanel();
tabLayoutPanel.add(absolutePanel_7, "Documentos", false);
AbsolutePanel absolutePanel = new AbsolutePanel();
tabLayoutPanel.add(absolutePanel, "Inicio", false);
tabLayoutPanel.selectTab(0);
rootPanel.add(tabLayoutPanel, 0, 35);
signInLink.getElement().setClassName("login-area");
signInLink.setText("Iniciar Sesión");
signInLink.setTitle("Click para finalizar sesión");
loginImage.getElement().setClassName("login-area");
loginPanel.add(signInLink);
RootPanel.get("loginPanelContainer").add(loginPanel);
final StringBuilder userEmail = new StringBuilder();
greetingService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {
@Override
public void onFailure(final Throwable caught) {
GWT.log("login -> onFailure");
}
@Override
public void onSuccess(final LoginInfo result) {
if (result.getName() != null && !result.getName().isEmpty()) {
addGoogleAuthHelper();
loadLogout(result);
} else {
loadLogin(result);
}
userEmail.append(result.getEmailAddress());
}
});
// Click handler for showing the Comunidades panel
btnComunidades.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if ( ! comunidadesPanel.isVisible() ){
comunidadesPanel.setVisible(true);
btnFamilias.setDown(false);
btnComunidades.setDown(true);
}
else {
comunidadesPanel.setVisible(false);
btnFamilias.setDown(true);
btnComunidades.setDown(false);
}
}
});
}
private void loadLogin(final LoginInfo loginInfo) {
AUTH.clearAllTokens();
signInLink.setHref(loginInfo.getLoginUrl());
// signInLink.setText("");
// signInLink.setTitle("Sign in");
}
private void loadLogout(final LoginInfo loginInfo) {
signInLink.setHref(loginInfo.getLogoutUrl());
signInLink.setText(loginInfo.getName());
signInLink.setTitle("Sign out");
}
private void addGoogleAuthHelper() {
final AuthRequest req =
new AuthRequest(GOOGLE_AUTH_URL, GOOGLE_CLIENT_ID).withScopes(PLUS_ME_SCOPE);
AUTH.login(req, new Callback<String, Throwable>() {
@Override
public void onSuccess(final String token) {
if (!token.isEmpty()) {
greetingService.loginDetails(token, new AsyncCallback<LoginInfo>() {
@Override
public void onFailure(final Throwable caught) {
GWT.log("loginDetails -> onFailure");
}
@Override
public void onSuccess(final LoginInfo loginInfo) {
signInLink.setText(loginInfo.getName());
nameField.setText(loginInfo.getName());
signInLink.setStyleName("login-area");
loginImage.setUrl(loginInfo.getPictureUrl());
loginImage.setVisible(false);
loginPanel.add(loginImage);
loginImage.addLoadHandler(new LoadHandler() {
@Override
public void onLoad(final LoadEvent event) {
final int newWidth = 24;
final com.google.gwt.dom.client.Element element =
event.getRelativeElement();
if (element.equals(loginImage.getElement())) {
final int originalHeight = loginImage.getOffsetHeight();
final int originalWidth = loginImage.getOffsetWidth();
if (originalHeight > originalWidth) {
loginImage.setHeight(newWidth + "px");
} else {
loginImage.setWidth(newWidth + "px");
}
loginImage.setVisible(true);
/** Show root panel */
tabLayoutPanel.setVisible(true);
}
}
});
}
});
}
}
@Override
public void onFailure(final Throwable caught) {
GWT.log("Error -> loginDetails\n" + caught.getMessage());
}
});
}
}