View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.deckbuilder;
20  
21  import java.awt.Dimension;
22  import java.awt.Graphics;
23  import java.awt.Graphics2D;
24  import java.awt.Image;
25  import java.awt.RenderingHints;
26  
27  import javax.swing.JComponent;
28  
29  import net.sf.magicproject.database.DatabaseCard;
30  import net.sf.magicproject.database.DatabaseFactory;
31  import net.sf.magicproject.management.MonitorListener;
32  import net.sf.magicproject.token.IdConst;
33  import net.sf.magicproject.tools.MCardCompare;
34  
35  /***
36   * @author <a href="mailto:goldeneyemdk@users.sourceforge.net">Sebastien Genete
37   *         </a>
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.83
40   */
41  public final class CardView extends JComponent implements MonitorListener {
42  	private CardView() {
43  		setPreferredSize(new Dimension(IdConst.STD_WIDTH, IdConst.STD_HEIGHT));
44  		setMaximumSize(getPreferredSize());
45  		setMinimumSize(getPreferredSize());
46  		setSize(getPreferredSize());
47  	}
48  
49  	public final void notifyChange() {
50  		repaint();
51  	}
52  
53  	/***
54  	 * Display the given card name. If this card name is not locally present, it
55  	 * will be downloaded from a web location depending on settings of current
56  	 * TBS. Theses settings are defined in the <code>tbs/xxx.xml</code>
57  	 * 
58  	 * @param card
59  	 *          the card name to display.
60  	 */
61  	public void setCard(MCardCompare card) {
62  		String errorMsg = null;
63  		try {
64  			databaseCard = DatabaseFactory.getDatabase(null, card
65  					.getModel(MdbLoader.lastMdbStream), null);
66  		} catch (Throwable e) {
67  			errorMsg = "<html>Error :" + e.getMessage();
68  		} finally {
69  			if (errorMsg != null) {
70  				databaseCard = null;
71  			}
72  		}
73  		repaint();
74  	}
75  
76  	private DatabaseCard databaseCard;
77  
78  	@Override
79  	public void paint(Graphics g) {
80  		final Image picture;
81  		if (databaseCard == null) {
82  			picture = DatabaseFactory.backImage;
83  		} else {
84  			picture = databaseCard.getImage(this);
85  		}
86  		Graphics2D g2d = (Graphics2D) g;
87  		g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
88  				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
89  		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
90  				RenderingHints.VALUE_ANTIALIAS_ON);
91  		g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
92  				RenderingHints.VALUE_RENDER_QUALITY);
93  		g.drawImage(picture, 0, 0, getSize().width, getSize().height, this);
94  
95  		// Draw the eventual progressbar
96  		if (databaseCard != null) {
97  			databaseCard.updatePaintNotification(this, g);
98  		}
99  	}
100 
101 	/***
102 	 * Unique instance of CardView class.
103 	 */
104 	public static CardView instance = new CardView();
105 
106 }