OSDN Git Service

Merge commit 'cb03a1ed529387c1084e04e6f141eb9d35d095b8'
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / data / Land.java
1 /*
2  * land
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.data;
9
10 import java.awt.image.BufferedImage;
11 import java.io.IOException;
12 import java.net.MalformedURLException;
13 import java.net.URI;
14 import java.net.URL;
15 import java.util.Collections;
16 import java.util.LinkedList;
17 import java.util.List;
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
20 import jp.sfjp.jindolf.net.ServerAccess;
21 import jp.sourceforge.jindolf.corelib.LandDef;
22
23 /**
24  * いわゆる「国」。
25  */
26 public class Land {
27
28     private static final Logger LOGGER = Logger.getAnonymousLogger();
29
30
31     private final LandDef landDef;
32     private final ServerAccess serverAccess;
33
34     private final List<Village> villageList = new LinkedList<>();
35
36
37     /**
38      * コンストラクタ。
39      * @param landDef 国定義
40      * @throws java.lang.IllegalArgumentException 不正な国定義
41      */
42     public Land(LandDef landDef) throws IllegalArgumentException{
43         super();
44
45         this.landDef = landDef;
46
47         URL url;
48         try{
49             url = this.landDef.getCgiURI().toURL();
50         }catch(MalformedURLException e){
51             throw new IllegalArgumentException(e);
52         }
53         this.serverAccess = new ServerAccess(url, this.landDef.getEncoding());
54
55         return;
56     }
57
58
59     /**
60      * 国定義を得る。
61      * @return 国定義
62      */
63     public LandDef getLandDef(){
64         return this.landDef;
65     }
66
67     /**
68      * サーバ接続を返す。
69      * @return ServerAccessインスタンス
70      */
71     public ServerAccess getServerAccess(){
72         return this.serverAccess;
73     }
74
75     /**
76      * 指定されたインデックス位置の村を返す。
77      * @param index 0から始まるインデックス値
78      * @return 村
79      */
80     public Village getVillage(int index){
81         if(index < 0)                  return null;
82         if(index >= getVillageCount()) return null;
83
84         Village result = this.villageList.get(index);
85         return result;
86     }
87
88     /**
89      * 村の総数を返す。
90      * @return 村の総数
91      */
92     public int getVillageCount(){
93         int result = this.villageList.size();
94         return result;
95     }
96
97     /**
98      * 村のリストを返す。
99      * @return 村のリスト
100      */
101     // TODO インスタンス変数でいいはず。
102     public List<Village> getVillageList(){
103         return Collections.unmodifiableList(this.villageList);
104     }
105
106     /**
107      * 絶対または相対URLの指すパーマネントなイメージ画像をダウンロードする。
108      * ※ A,B,D 国の顔アイコンは絶対パスらしい…。
109      * @param imageURL 画像URL文字列
110      * @return 画像イメージ
111      */
112     public BufferedImage downloadImage(String imageURL){
113         ServerAccess server = getServerAccess();
114         BufferedImage image;
115         try{
116             image = server.downloadImage(imageURL);
117         }catch(IOException e){
118             LOGGER.log(Level.WARNING,
119                     "イメージ[" + imageURL + "]"
120                     + "のダウンロードに失敗しました",
121                     e );
122             return null;
123         }
124         return image;
125     }
126
127     /**
128      * 墓アイコンイメージを取得する。
129      * @return 墓アイコンイメージ
130      */
131     public BufferedImage getGraveIconImage(){
132         URI uri = getLandDef().getTombFaceIconURI();
133         BufferedImage result = downloadImage(uri.toASCIIString());
134         return result;
135     }
136
137     /**
138      * 墓アイコンイメージ(大)を取得する。
139      * @return 墓アイコンイメージ(大)
140      */
141     public BufferedImage getGraveBodyImage(){
142         URI uri = getLandDef().getTombBodyIconURI();
143         BufferedImage result = downloadImage(uri.toASCIIString());
144         return result;
145     }
146
147     /**
148      * 村リストを更新する。
149      * @param vset ソート済みの村一覧
150      */
151     public void updateVillageList(List<Village> vset){
152         // TODO 村リスト更新のイベントリスナがあると便利か?
153         this.villageList.clear();
154         this.villageList.addAll(vset);
155         return;
156     }
157
158     /**
159      * 国の文字列表現を返す。
160      * @return 文字列表現
161      */
162     @Override
163     public String toString(){
164         return getLandDef().getLandName();
165     }
166
167 }