Java tutorial
/* * Copyright 2009 Tsutomu YANO. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * under the License. */ package jp.gihyo.wicket.page.simple; import jp.gihyo.wicket.page.*; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.List; import jp.gihyo.wicket.AppSession; //import org.apache.wicket.PageParameters; //import org.apache.wicket.RedirectToUrlException; import org.apache.wicket.markup.html.WebPage; //import org.apache.wicket.markup.html.WebResource; import org.apache.wicket.request.resource.ByteArrayResource; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextArea; import org.apache.wicket.markup.html.image.Image; import org.apache.wicket.markup.html.link.ExternalLink; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.html.list.ListItem; import org.apache.wicket.markup.html.list.ListView; import org.apache.wicket.markup.html.panel.FeedbackPanel; import org.apache.wicket.model.AbstractReadOnlyModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.LoadableDetachableModel; import org.apache.wicket.model.PropertyModel; import org.apache.wicket.request.flow.RedirectToUrlException; import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.ResourceStreamNotFoundException; import org.apache.wicket.util.resource.UrlResourceStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import twitter4j.Paging; import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterException; /** * * @author Tsutomu YANO */ public class MyTimeline extends WebPage { private static final long serialVersionUID = -1289967315883152057L; private static final Logger LOGGER = LoggerFactory.getLogger(MyTimeline.class); private static final int ITEMS_PER_PAGE = 20; private FeedbackPanel feedback; public MyTimeline(PageParameters parameters) { super(parameters); if (!AppSession.get().isLoggedIn()) { throw new RedirectToUrlException(getRequestCycle().urlFor(Login.class, null).toString()); } constructPage(); } private void constructPage() { final TweetForm form = new TweetForm("tweetForm"); add(form); feedback = new FeedbackPanel("feedback"); feedback.setOutputMarkupId(true); add(feedback); /* * ^CCpIModel?B * ??[hf?[^NGXg?EX|X?ETCNLbV?A * LoadableDetachableModelNXgp?B */ IModel<List<Status>> statusModel = new LoadableDetachableModel<List<Status>>() { @Override protected List<Status> load() { try { Twitter twitter = AppSession.get().getTwitterSession(); return twitter.getFriendsTimeline(new Paging(1, ITEMS_PER_PAGE)); } catch (TwitterException ex) { MyTimeline.this.error(getString("canNotRetrieveFriendTimeline")); return Collections.emptyList(); } } }; /* * ^CCXg */ ListView<Status> timeline = new ListView<Status>("statusView", statusModel) { @Override protected void populateItem(final ListItem<Status> item) { final Status status = item.getModelObject(); String userUrl = "http://twitter.com/" + status.getUser().getScreenName(); ExternalLink imageLink = new ExternalLink("imageLink", userUrl); byte[] byteArray = {};/* f?[^?? */ UrlResourceStream stream = new UrlResourceStream(status.getUser().getProfileImageURL()); try { stream.getInputStream().read(byteArray); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ResourceStreamNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Image userImage = new Image("userImage", new ByteArrayResource("image/jpeg", byteArray)); imageLink.add(userImage); item.add(imageLink); ExternalLink screenNameLink = new ExternalLink("screenName", userUrl, status.getUser().getScreenName()); item.add(screenNameLink); Label content = new Label("tweetContent", status.getText()); item.add(content); ExternalLink tweetLink = new ExternalLink("tweetLink", userUrl + "/status/" + status.getId(), null); item.add(tweetLink); Label time = new Label("tweetTime", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(status.getCreatedAt())); tweetLink.add(time); Label clientName = new Label("clientName", status.getSource()); item.add(clientName.setEscapeModelStrings(false)); /* * CNx?BIModel?f?A?fav/unfav * \?B */ final Label favName = new Label("favName", new AbstractReadOnlyModel<String>() { @Override public String getObject() { return status.isFavorited() ? "unfav" : "fav"; } }); /* * CN?BNbN?C?E??s?B */ Link<Void> favLink = new Link<Void>("favLink") { @Override public void onClick() { try { Twitter twitterSession = AppSession.get().getTwitterSession(); if (status.isFavorited()) { twitterSession.destroyFavorite(status.getId()); info(getString("favorateRemoved")); } else { twitterSession.createFavorite(status.getId()); info(getString("favorateRegistered")); } } catch (TwitterException ex) { String message = getString("catNotCreateFavorite") + ": " + ex.getStatusCode(); error(message); LOGGER.error(message, ex); } } }; item.add(favLink); favLink.add(favName); /* * vCpN?B * ?u@screenName?v?B */ item.add(new Link<Void>("replyLink") { @Override public void onClick() { String targetScreenName = status.getUser().getScreenName(); form.insertText("@" + targetScreenName + " "); } }); } }; //ListView\e?s????BreuseItemsv?peB??A //y?[W\?Ay?[WTu~bg?AXgeIuWFNg\??B //twitterey?[We?X??AXg????AXge? //Xe?[^Xu?A??dv?B timeline.setReuseItems(true); add(timeline); } static class TweetForm extends Form<MyTimeline> { private static final long serialVersionUID = -6873087998089286302L; private String tweet; private TextArea<String> updateText; public TweetForm(String id) { super(id); constructForm(); } private void constructForm() { updateText = new TextArea<String>("tweetText", new PropertyModel<String>(this, "tweet")); updateText.setOutputMarkupId(true); add(updateText); /* * ?e{^ */ add(new Button("post") { @Override public void onSubmit() { if (tweet != null && tweet.length() > 0) { try { Twitter twitterSession = AppSession.get().getTwitterSession(); twitterSession.updateStatus(tweet); setResponsePage(MyTimeline.class); } catch (TwitterException ex) { String message = getString("updateFailed"); LOGGER.error(message, ex); error(message); } } } }); } public void insertText(String text) { updateText.setModelObject(text); } } }