View Javadoc

1   /*
2    *  File: ClassImageRenderer.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.ui.table.renderer;
12  
13  import java.util.HashMap;
14  import java.util.List;
15  import java.util.Map;
16  
17  import org.eclipse.jface.resource.ImageDescriptor;
18  import org.eclipse.jface.resource.ImageRegistry;
19  import org.eclipse.swt.SWT;
20  import org.eclipse.swt.graphics.Color;
21  import org.eclipse.swt.graphics.GC;
22  import org.eclipse.swt.graphics.Image;
23  import org.eclipse.swt.graphics.Rectangle;
24  import org.eclipse.swt.printing.Printer;
25  import org.eclipse.swt.widgets.Display;
26  
27  import de.jaret.util.ui.ResourceImageDescriptor;
28  import de.jaret.util.ui.table.JaretTable;
29  import de.jaret.util.ui.table.model.IColumn;
30  import de.jaret.util.ui.table.model.IRow;
31  
32  /***
33   * CellRenderer rendering images corresponding to the class of the value.
34   * 
35   * @author Peter Kliem
36   * @version $Id: ClassImageRenderer.java 391 2007-04-30 23:55:39Z olk $
37   */
38  public class ClassImageRenderer extends CellRendererBase implements ICellRenderer {
39      protected Map<Class, String> _keyMap = new HashMap<Class, String>();
40      private ImageRegistry _imageRegistry;
41  
42      public ClassImageRenderer(Printer printer) {
43          super(printer);
44      }
45  
46      public ClassImageRenderer() {
47          super(null);
48      }
49  
50      /***
51       * Add a mapping between a class and an image descriptor.
52       * 
53       * @param clazz the class
54       * @param key string key (has to be non null an unique for this renderer) to identfy the object
55       * @param imageDescriptor image descriptor for the image
56       */
57      public void addClassImageDescriptorMapping(Class<?> clazz, String key, ImageDescriptor imageDescriptor) {
58          getImageRegistry().put(key, imageDescriptor);
59          _keyMap.put(clazz, key);
60      }
61  
62      /***
63       * Add a mapping between a class and an image ressource.
64       * 
65       * @param clazz class
66       * @param key string key (has to be non null an unique for this renderer) to identfy the object
67       * @param ressourceName ressource path
68       */
69      public void addClassRessourceNameMapping(Class<?> clazz, String key, String ressourceName) {
70          ImageDescriptor imgDesc = new ResourceImageDescriptor(ressourceName, this.getClass());
71          addClassImageDescriptorMapping(clazz, key, imgDesc);
72      }
73  
74      /***
75       * Retrieve the key for a class, checking all super classes and interfaces.
76       * 
77       * @param clazz class to check
78       * @return key or null
79       */
80      protected String getKeyForClass(Class<?> clazz) {
81          String result = _keyMap.get(clazz);
82          if (result != null) {
83              return result;
84          }
85  
86          Class<?>[] interfaces = clazz.getInterfaces();
87          for (int i = 0; i < interfaces.length; i++) {
88              result = _keyMap.get(interfaces[i]);
89              if (result != null) {
90                  return result;
91              }
92          }
93  
94          Class<?> sc = clazz.getSuperclass();
95          if (sc != null) {
96              result = getKeyForClass(sc);
97          }
98          return result;
99      }
100 
101     /***
102      * {@inheritDoc}
103      */
104     public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
105             IColumn column, boolean drawFocus, boolean selected, boolean printing) {
106         drawBackground(gc, drawingArea, cellStyle, selected, printing);
107         Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
108         Rectangle rect = applyInsets(drect);
109         Object value = column.getValue(row);
110         String key = getKeyForClass(value.getClass());
111 
112         if (key != null) {
113             Image img = null;
114             img = getImageRegistry().get(key);
115             int x = rect.x + (rect.width - scaleX(img.getBounds().width)) / 2;
116             int y = rect.y + (rect.height - scaleY(img.getBounds().height)) / 2;
117             gc.drawImage(img, 0, 0, img.getBounds().width, img.getBounds().height, x, y, scaleX(img.getBounds().width),
118                     scaleY(img.getBounds().height));
119         } else {
120             // indicate error with red fill
121             Color bg = gc.getBackground();
122             gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA));
123             gc.fillRectangle(rect);
124             gc.setBackground(bg);
125         }
126         if (drawFocus) {
127             drawFocus(gc, drect);
128         }
129         drawSelection(gc, drawingArea, cellStyle, selected, printing);
130 
131     }
132 
133     /***
134      * {@inheritDoc}
135      * 
136      * @TODO
137      */
138     public int getPreferredWidth(List<IRow> rows, IColumn column) {
139         return -1;// return getImageRegistry().get(CHECKED).getBounds().width;
140     }
141 
142     /***
143      * {@inheritDoc} TODO
144      */
145     public int getPreferredHeight(GC gc, ICellStyle cellStyle, int width, IRow row, IColumn column) {
146         return -1;// getImageRegistry().get(CHECKED).getBounds().height;
147     }
148 
149     /***
150      * Retrieve the image registry instance.
151      * 
152      * @return ImageRegistry
153      */
154     private synchronized ImageRegistry getImageRegistry() {
155         if (_imageRegistry == null) {
156             _imageRegistry = new ImageRegistry();
157         }
158         return _imageRegistry;
159     }
160 
161     /***
162      * {@inheritDoc} Disposes the image registry and clears the key map to help garbage collecting.
163      */
164     public void dispose() {
165         if (_imageRegistry != null) {
166             _imageRegistry.dispose();
167         }
168         _keyMap.clear();
169     }
170 
171     /***
172      * {@inheritDoc}
173      */
174     public ICellRenderer createPrintRenderer(Printer printer) {
175         ClassImageRenderer renderer = new ClassImageRenderer(printer);
176         for (Class<?> clazz : _keyMap.keySet()) {
177             String key = _keyMap.get(clazz);
178             ImageDescriptor imageDesc = getImageRegistry().getDescriptor(key);
179             renderer.addClassImageDescriptorMapping(clazz, key, imageDesc);
180         }
181         return renderer;
182     }
183 
184 }