r1609 - branches/jaxx-2.X/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer
Author: tchemit Date: 2009-10-28 08:38:25 +0100 (Wed, 28 Oct 2009) New Revision: 1609 Added: branches/jaxx-2.X/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer/MultiDecoratorTableCelleRenderer.java Log: add multi table decorator Added: branches/jaxx-2.X/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer/MultiDecoratorTableCelleRenderer.java =================================================================== --- branches/jaxx-2.X/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer/MultiDecoratorTableCelleRenderer.java (rev 0) +++ branches/jaxx-2.X/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer/MultiDecoratorTableCelleRenderer.java 2009-10-28 07:38:25 UTC (rev 1609) @@ -0,0 +1,85 @@ +/* + * *##% + * JAXX Runtime + * Copyright (C) 2008 - 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ +package jaxx.runtime.swing.renderer; + +import java.awt.Component; +import java.util.ArrayList; +import java.util.List; +import javax.swing.JTable; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.TableCellRenderer; +import jaxx.runtime.decorator.Decorator; +import jaxx.runtime.decorator.JXPathDecorator; + +/** + * A {@link TableCellRenderer} which compute text with the matching decorator + * from {@link #decorators} based on the type of value and leave the hand to + * the {@link #delegate} to perform the visual renderer. + * + * @author chemit + * @since 2.0.0 + */ +public class MultiDecoratorTableCelleRenderer implements TableCellRenderer { + + /** + * Delegate cell renderer + */ + protected TableCellRenderer delegate; + /** + * accepted types + */ + protected List<Class<?>> types; + /** + * decorators for accepted types + */ + protected Decorator<?>[] decorators; + + public MultiDecoratorTableCelleRenderer(JXPathDecorator<?>... decorator) { + this(new DefaultTableCellRenderer(), decorator); + } + + public MultiDecoratorTableCelleRenderer(TableCellRenderer delegate, JXPathDecorator<?>... decorator) { + this.delegate = delegate; + + this.types = new ArrayList<Class<?>>(); + List<Decorator<?>> tmp = new ArrayList<Decorator<?>>(); + for (JXPathDecorator<?> d : decorator) { + if (types.contains(d.getInternalClass())) { + throw new IllegalArgumentException("can not have twice a decorator of type " + d.getInternalClass()); + } + types.add(d.getInternalClass()); + tmp.add(d); + } + decorators = tmp.toArray(new Decorator<?>[tmp.size()]); + } + + @Override + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasfocus, int row, int column) { + if (value != null) { + int i = types.indexOf(value.getClass()); + if (i != -1) { + Decorator<?> d = decorators[i]; + value = d.toString(value); + } + } + return delegate.getTableCellRendererComponent(table, value, isSelected, hasfocus, row, column); + } +} Property changes on: branches/jaxx-2.X/jaxx-runtime/src/main/java/jaxx/runtime/swing/renderer/MultiDecoratorTableCelleRenderer.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL
participants (1)
-
tchemit@users.nuiton.org