/*
* Spadger - an open source discussion forum system.
*
* Copyright (C) 2009 The Spadger Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
*
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.ajiaojr.spadger.client.desktop.view;
import java.util.Date;
import javax.inject.Inject;
import javax.inject.Singleton;
import net.ajiaojr.spadger.client.HotKeysManager;
import net.ajiaojr.spadger.client.WindowTitleManager;
import net.ajiaojr.spadger.client.desktop.dialog.AboutDialog;
import net.ajiaojr.spadger.client.place.DashboardPlace;
import net.ajiaojr.spadger.client.place.SearchPlace;
import net.ajiaojr.spadger.client.place.SettingsPlace;
import net.ajiaojr.spadger.client.view.MainUiView;
import net.ajiaojr.spadger.shared.entity.SpadgerConfig;
import net.ajiaojr.spadger.shared.entity.SpadgerUser;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.http.client.URL;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.Widget;
/**
* The desktop version of the user interface.
*
* @author The Spadger Team
*/
@Singleton
public class MainUiViewImpl extends ResizeComposite implements MainUiView {
/**
* UiBinder interface.
*
* @author The Spadger Team
*/
@UiTemplate("MainUiView.ui.xml")
interface MainUiViewUiBinder extends
UiBinder<DockLayoutPanel, MainUiViewImpl> {
}
private static MainUiViewUiBinder uiBinder = GWT
.create(MainUiViewUiBinder.class);
/**
* The menu item for about.
*/
@UiField
MenuItem aboutMenuItem;
/**
* The container for the main components.
*/
@UiField
AcceptsOneWidget contentArea;
/**
* The span element that displays the current year.
*/
@UiField
SpanElement currentYear;
/**
* Menu item for login.
*/
@UiField
MenuItem loginMenuItem;
/**
* Menu item for logout.
*/
@UiField
MenuItem logoutMenuItem;
private PlaceController placeController;
/**
* Suggest box for search keywords.
*/
@UiField(provided = true)
SuggestBox searchKeyword;
/**
* Menu item for settings.
*/
@UiField
MenuItem settingsMenuItem;
/**
* Spadger info read from the host page.
*/
SpadgerConfig spadgerInfo = getSpadgerConfig();
/**
* User info read from the host page.
*/
SpadgerUser userInfo = getUser();
/**
* Creates a new instance.
*/
public MainUiViewImpl() {
// TODO: do proper suggestion
searchKeyword = new SuggestBox();
initWidget(uiBinder.createAndBindUi(this));
currentYear.setInnerText(Integer.toString(new Date().getYear() + 1900));
aboutMenuItem.setCommand(new Command() {
@Override
public void execute() {
GWT.runAsync(new RunAsyncCallback() {
@Override
public void onFailure(Throwable reason) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess() {
AboutDialog aboutDialog = new AboutDialog();
aboutDialog.center();
aboutDialog.show();
}
});
}
});
// Check to see if we have an user logged in, if so, let's build logout
// menus and stuff. Otherwise, we'll show login link.
if (userInfo != null) {
settingsMenuItem.setCommand(new Command() {
@Override
public void execute() {
placeController.goTo(new SettingsPlace());
}
});
settingsMenuItem.setVisible(true);
logoutMenuItem.setCommand(new Command() {
@Override
public void execute() {
StringBuilder forwardUrl = new StringBuilder(Window.Location
.getPath());
String queryString = Window.Location.getQueryString();
if (queryString != null) {
forwardUrl.append(queryString);
}
String hash = Window.Location.getHash();
if (hash != null) {
forwardUrl.append(hash);
}
Window.Location.assign("/logout.php?f="
+ URL.encodeQueryString(forwardUrl.toString()));
}
});
logoutMenuItem.setVisible(true);
} else {
loginMenuItem.setCommand(new Command() {
@Override
public void execute() {
StringBuilder forwardUrl = new StringBuilder(Window.Location
.getPath());
String queryString = Window.Location.getQueryString();
if (queryString != null) {
forwardUrl.append(queryString);
}
String hash = Window.Location.getHash();
if (hash != null) {
forwardUrl.append(hash);
}
Window.Location.assign("/login.php?f="
+ URL.encodeQueryString(forwardUrl.toString()));
}
});
loginMenuItem.setVisible(true);
}
searchKeyword.getTextBox().addFocusHandler(new FocusHandler() {
@Override
public void onFocus(FocusEvent event) {
HotKeysManager.deactivateHotKeys();
}
});
searchKeyword.getTextBox().addBlurHandler(new BlurHandler() {
@Override
public void onBlur(BlurEvent event) {
HotKeysManager.activateHotKeys();
}
});
HotKeysManager.registerHotKey('?', new Command() {
@Override
public void execute() {
// TODO decide how we do the hot keys display popup.
}
});
WindowTitleManager.setSystemName(spadgerInfo.getName());
}
@Override
public Widget asWidget() {
return this;
}
/*
* (non-Javadoc)
*
* @see net.ajiaojr.spadger.client.view.MainUiView#getContentArea()
*/
@Override
public AcceptsOneWidget getContentArea() {
return contentArea;
}
private final native SpadgerConfig getSpadgerConfig() /*-{
return $wnd.Spadger.system;
}-*/;
private final native SpadgerUser getUser() /*-{
return $wnd.Spadger.user;
}-*/;
/**
* Handles click event on the logo image.
*
* @param event
* the click event.
*/
@UiHandler("logoImage")
public void handleLogoClick(ClickEvent event) {
placeController.goTo(new DashboardPlace());
}
/**
* Handles the clicking event on the search button.
*
* @param event
* the click event.
*/
// @UiHandler("searchButton")
// public void handleSearchButtonOnClick(ClickEvent event) {
// search(searchKeyword.getValue());
// }
/**
* Handles enter key press on the search keyword text box.
*
* @param event
* the key press event.
*/
@UiHandler("searchKeyword")
public void handleSearchKeywordOnEnter(KeyPressEvent event) {
if (KeyCodes.KEY_ENTER == event.getNativeEvent().getKeyCode()) {
search(searchKeyword.getValue());
}
}
/**
* Handles a search.
*
* @param keywords
* search keywords.
*/
private void search(String keywords) {
placeController.goTo(new SearchPlace(searchKeyword.getValue()));
}
@Inject
public void setPlaceController(PlaceController placeController) {
this.placeController = placeController;
}
/*
* (non-Javadoc)
*
* @see
* net.ajiaojr.spadger.client.view.MainUiView#setSearchKeywords(java.lang.
* String)
*/
@Override
public void setSearchKeywords(String keyword) {
searchKeyword.setText(keyword);
}
}