package com.danielvaughan.rssreader.client;
import com.danielvaughan.rssreader.client.services.FeedServiceAsync;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Popup;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.ComponentEvent;
public class LinkFeedPopup extends Popup {
private TextField<String> tfUrl;
public LinkFeedPopup(){
setShadow(true);
setAutoHide(false);
setBorders(true);
setSize(300, 55);
setLayout(new BorderLayout());
tfUrl = new TextField<String>();
tfUrl.setRegex("^http\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$");
tfUrl.setAllowBlank(false);
tfUrl.setAutoValidate(true);
tfUrl.getMessages().setBlankText("Please enter the URL of an existing feed");
tfUrl.getMessages().setRegexText("The link field must be a URL e.g.\n"+
"http://www.example.com/rss.html");
tfUrl.addKeyListener(new KeyListener() {
public void componentKeyDown(ComponentEvent event) {
if (event.getKeyCode() == KeyCodes.KEY_ENTER){
addFeed(tfUrl.getValue());
}
}
});
BorderLayoutData bld_tfUrl = new BorderLayoutData(LayoutRegion.CENTER);
bld_tfUrl.setMargins(new Margins(2, 20, 2, 2));
add(tfUrl, bld_tfUrl);
tfUrl.setFieldLabel("New TextField");
Button btnAdd = new Button("Add");
btnAdd.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
addFeed(tfUrl.getValue());
}
});
BorderLayoutData bld_btnAdd = new BorderLayoutData(LayoutRegion.EAST, 50);
bld_btnAdd.setMargins(new Margins(2, 2, 2, 2));
add(btnAdd, bld_btnAdd);
Text txtEnterAFeed = new Text("Enter a feed url");
BorderLayoutData bld_txtEnterAFeed = new BorderLayoutData(LayoutRegion.NORTH, 20);
bld_txtEnterAFeed.setMargins(new Margins(2, 2, 2, 2));
add(txtEnterAFeed, bld_txtEnterAFeed);
}
public void addFeed(final String url){
final FeedServiceAsync feedService = Registry.get(RSSReaderConstants.FEED_SERVICE);
feedService.addExistingFeed(url, new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
tfUrl.clear();
Info.display("RSS Reader", "Feed at " + url + " added successfully");
hide();
}
@Override
public void onFailure(Throwable caught) {
Info.display("RSS Reader", "Failed to add feed at: " + url +
"\n" + caught.getMessage());
}
});
}
}