View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: ImageFigure.java
13   * Created: 2009-07-27
14   * Author: kpietak
15   * $Id: ImageFigure.java 3244 2009-08-24 14:25:43Z tmilos $
16   */
17  
18  package pl.edu.agh.cast.figure;
19  
20  import org.eclipse.draw2d.Figure;
21  import org.eclipse.draw2d.Graphics;
22  import org.eclipse.draw2d.geometry.Dimension;
23  import org.eclipse.swt.graphics.Image;
24  import org.eclipse.swt.graphics.Rectangle;
25  
26  import pl.edu.agh.cast.CoreServiceLocator;
27  import pl.edu.agh.cast.resource.IVisualResource;
28  import pl.edu.agh.cast.resource.ImageSize;
29  import pl.edu.agh.cast.resource.ResourceRegistry;
30  
31  /**
32   * A {@link Figure} with an {@link Image}.
33   *
34   * The images are provided from {@link ResourceRegistry}.
35   *
36   * @author AGH CAST Team
37   */
38  public class ImageFigure extends Figure {
39  
40  	private Image image;
41  
42  	/**
43  	 * Use ImageFigure(String imageId) constructor to create this figure.
44  	 */
45  	@SuppressWarnings("unused")
46  	private ImageFigure() {
47  	}
48  
49  	/**
50  	 * Creates new figure with an image in resource by given id and with given size.
51  	 *
52  	 * @param resourceId
53  	 *            id of the image to be used
54  	 * @param size
55  	 *            image size
56  	 * @throws IllegalArgumentException
57  	 *             when resource does not exist or resource does not contain image with given size
58  	 */
59  	public ImageFigure(String resourceId, ImageSize size) {
60  		this(resourceId, size, null);
61  	}
62  
63  	/**
64  	 * Creates new figure with an image in resource by given id and with given size.
65  	 *
66  	 * @param resourceId
67  	 *            id of the image to be used
68  	 * @param size
69  	 *            image size
70  	 * @param defaultResourceId
71  	 *            id of the default image to be used (in case the specific one could not be located)
72  	 * @throws IllegalArgumentException
73  	 *             when resource nor default resource does not exist or resource does not contain image with given size
74  	 */
75  	@SuppressWarnings("nls")
76  	public ImageFigure(String resourceId, ImageSize size, String defaultResourceId) {
77  		IVisualResource resource = null;
78  		if (resourceId != null) {
79  			resource = CoreServiceLocator.getResourceRegistry().getResource(resourceId);
80  		}
81  
82  		if (resource == null) {
83  			if (defaultResourceId != null) {
84  				resource = CoreServiceLocator.getResourceRegistry().getResource(defaultResourceId);
85  				if (resource == null) {
86  					throw new IllegalArgumentException(String.format("Cannot find visual resource with default %s id",
87  					        defaultResourceId));
88  				}
89  			} else {
90  				throw new IllegalArgumentException(String.format("Cannot find visual resource with %s id", resourceId));
91  			}
92  		}
93  
94  		image = resource.getImage(size);
95  		if (image == null) {
96  			throw new IllegalArgumentException(String.format("Resource %s does not contain image with size %s",
97  			        resourceId, size.toString()));
98  		}
99  	}
100 
101 	/**
102 	 * {@inheritDoc}
103 	 *
104 	 * @see org.eclipse.draw2d.Figure#paintFigure(org.eclipse.draw2d.Graphics)
105 	 */
106 	@Override
107 	protected void paintFigure(Graphics gc) {
108 		gc.drawImage(image, getBounds().getTopLeft());
109 		// super.paintFigure(gc);
110 	}
111 
112 	/**
113 	 * {@inheritDoc}
114 	 *
115 	 * @see org.eclipse.draw2d.Figure#setBounds(org.eclipse.draw2d.geometry.Rectangle)
116 	 */
117 	@Override
118 	public void setBounds(org.eclipse.draw2d.geometry.Rectangle bounds) {
119 		super.setBounds(bounds);
120 	}
121 
122 	/**
123 	 * {@inheritDoc}
124 	 *
125 	 * @see org.eclipse.draw2d.Figure#getMinimumSize(int, int)
126 	 */
127 	@Override
128 	public Dimension getMinimumSize(int arg0, int arg1) {
129 		Rectangle bounds = image.getBounds();
130 		return new Dimension(bounds.width, bounds.height);
131 	}
132 
133 	/**
134 	 * {@inheritDoc}
135 	 *
136 	 * @see org.eclipse.draw2d.Figure#getPreferredSize(int, int)
137 	 */
138 	@Override
139 	public Dimension getPreferredSize(int x, int y) {
140 		return getMinimumSize(x, y);
141 	}
142 
143 }