desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java
branchdesktop
changeset 260 8ebffb5aa10e
child 263 ac802aa234fc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java	Mon Sep 13 16:27:07 2010 +0200
     1.3 @@ -0,0 +1,54 @@
     1.4 +/*
     1.5 + * To change this template, choose Tools | Templates
     1.6 + * and open the template in the editor.
     1.7 + */
     1.8 +
     1.9 +package cz.xelfi.quoridor.desktop.sample;
    1.10 +
    1.11 +import com.sun.jersey.api.client.Client;
    1.12 +import com.sun.jersey.api.client.GenericType;
    1.13 +import com.sun.jersey.api.client.WebResource;
    1.14 +import cz.xelfi.quoridor.webidor.Game;
    1.15 +import cz.xelfi.quoridor.webidor.GameId;
    1.16 +import java.util.List;
    1.17 +import javax.ws.rs.core.MediaType;
    1.18 +import org.openide.util.Lookup;
    1.19 +
    1.20 +/** Class that connects to the server and answers queries.
    1.21 + *
    1.22 + * @author Jaroslav Tulach
    1.23 + */
    1.24 +abstract class Quoridor {
    1.25 +
    1.26 +    public static Quoridor getDefault() {
    1.27 +        Quoridor q = Lookup.getDefault().lookup(Quoridor.class);
    1.28 +        return q == null ? new Impl() : q;
    1.29 +    }
    1.30 +
    1.31 +    public abstract List<GameId> listGames();
    1.32 +    public abstract Game getGame(String id);
    1.33 +
    1.34 +    private static class Impl extends Quoridor {
    1.35 +        private final WebResource wr;
    1.36 +
    1.37 +        public Impl() {
    1.38 +            Client c = new Client();
    1.39 +            wr = c.resource("http://quoridor.xelfi.cz/api/");
    1.40 +        }
    1.41 +
    1.42 +        @Override
    1.43 +        public List<GameId> listGames() {
    1.44 +            GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
    1.45 +            List<GameId> ids = wr.path("games").accept(MediaType.TEXT_XML).get(gType);
    1.46 +            return ids;
    1.47 +        }
    1.48 +
    1.49 +
    1.50 +        @Override
    1.51 +        public Game getGame(String id) {
    1.52 +            Game g = wr.path("games").path(id).accept(MediaType.TEXT_XML).get(Game.class);
    1.53 +            return g;
    1.54 +        }
    1.55 +
    1.56 +    }
    1.57 +}