View Javadoc

1   /*
2    *  File: ObjectImageRenderer.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 object instances (i.e. enums) to images.
34   * 
35   * @author Peter Kliem
36   * @version $Id: ObjectImageRenderer.java 179 2007-01-07 17:37:50Z olk $
37   */
38  public class ObjectImageRenderer extends CellRendererBase implements ICellRenderer {
39      protected Map<Object, String> _keyMap = new HashMap<Object, String>();
40      private ImageRegistry _imageRegistry;
41  
42      public ObjectImageRenderer(Printer printer) {
43          super(printer);
44      }
45  
46      public ObjectImageRenderer() {
47          super(null);
48      }
49  
50      /***
51       * Add a mapping between an object instance and an image descriptor.
52       * 
53       * @param o object instance
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 addObjectImageDescriptorMapping(Object o, String key, ImageDescriptor imageDescriptor) {
58          getImageRegistry().put(key, imageDescriptor);
59          _keyMap.put(o, key);
60      }
61  
62      /***
63       * Add a mapping between object instance and an image ressource.
64       * 
65       * @param o object instance
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 addObjectRessourceNameMapping(Object o, String key, String ressourceName) {
70          ImageDescriptor imgDesc = new ResourceImageDescriptor(ressourceName, this.getClass());
71          addObjectImageDescriptorMapping(o, key, imgDesc);
72      }
73  
74      /***
75       * {@inheritDoc}
76       */
77      public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
78              IColumn column, boolean drawFocus, boolean selected, boolean printing) {
79          drawBackground(gc, drawingArea, cellStyle, selected, printing);
80          Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
81          Rectangle rect = applyInsets(drect);
82          Object value = column.getValue(row);
83          String key = _keyMap.get(value);
84  
85          if (key != null) {
86              Image img = null;
87              img = getImageRegistry().get(key);
88              int x = rect.x + (rect.width - scaleX(img.getBounds().width)) / 2;
89              int y = rect.y + (rect.height - scaleY(img.getBounds().height)) / 2;
90              gc.drawImage(img, 0, 0, img.getBounds().width, img.getBounds().height, x, y, scaleX(img.getBounds().width),
91                      scaleY(img.getBounds().height));
92          } else {
93              // indicate error with red fill
94              Color bg = gc.getBackground();
95              gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA));
96              gc.fillRectangle(rect);
97              gc.setBackground(bg);
98          }
99          if (drawFocus) {
100             drawFocus(gc, drect);
101         }
102         drawSelection(gc, drawingArea, cellStyle, selected, printing);
103 
104     }
105 
106     /***
107      * {@inheritDoc}
108      * 
109      * @TODO
110      */
111     public int getPreferredWidth(List<IRow> rows, IColumn column) {
112         return -1;// return getImageRegistry().get(CHECKED).getBounds().width;
113     }
114 
115     /***
116      * {@inheritDoc} TODO
117      */
118     public int getPreferredHeight(GC gc, ICellStyle cellStyle, int width, IRow row, IColumn column) {
119         return -1;// getImageRegistry().get(CHECKED).getBounds().height;
120     }
121 
122     /***
123      * Retrieve the image registry instance.
124      * 
125      * @return ImageRegistry
126      */
127     private synchronized ImageRegistry getImageRegistry() {
128         if (_imageRegistry == null) {
129             _imageRegistry = new ImageRegistry();
130         }
131         return _imageRegistry;
132     }
133 
134     /***
135      * {@inheritDoc} Disposes the image registry and clears the map with object instances to help garbage collecting.
136      */
137     public void dispose() {
138         if (_imageRegistry != null) {
139             _imageRegistry.dispose();
140         }
141         _keyMap.clear();
142     }
143 
144     /***
145      * {@inheritDoc}
146      */
147     public ICellRenderer createPrintRenderer(Printer printer) {
148         ObjectImageRenderer renderer = new ObjectImageRenderer(printer);
149         for (Object o : _keyMap.keySet()) {
150             String key = _keyMap.get(o);
151             ImageDescriptor imageDesc = getImageRegistry().getDescriptor(key);
152             renderer.addObjectImageDescriptorMapping(o, key, imageDesc);
153         }
154         return renderer;
155     }
156 
157 }