alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (ClosureRenderer.java)

This example Groovy source code file (ClosureRenderer.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

awt, closure, closurerenderer, closurerenderer, component, component, jlist, jtable, jtable, jtree, jtree, list, listcellrenderer, listcellrenderer, object, object, table, tree, treecellrenderer, util

The Groovy ClosureRenderer.java source code

/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.swing.impl;

import groovy.lang.Closure;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Danno.Ferrin
 */
public class ClosureRenderer implements ListCellRenderer, TableCellRenderer, TreeCellRenderer {

    Closure update;
    List children = new ArrayList();

    JList list;
    JTable table;
    JTree tree;
    Object value;
    boolean selected;
    boolean focused;
    boolean leaf;
    boolean expanded;
    int row;
    int column;
    boolean tableHeader;
    private boolean defaultRenderer;

    public ClosureRenderer() {
        this(null);
    }

    public ClosureRenderer(Closure c) {
        setUpdate(c);
    }


    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        this.list = list;
        this.table = null;
        this.tree = null;
        this.value = value;
        this.row = index;
        this.column = -1;
        this.selected = isSelected;
        this.focused = cellHasFocus;
        this.leaf = false;
        this.expanded = false;

        return render();
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        this.list = null;
        this.table = table;
        this.tree = null;
        this.value = value;
        this.row = row;
        this.column = column;
        this.selected = isSelected;
        this.focused = hasFocus;
        this.leaf = false;
        this.expanded = false;

        return render();
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        this.list = null;
        this.table = null;
        this.tree = tree;
        this.value = value;
        this.row = row;
        this.column = -1;
        this.selected = selected;
        this.focused = hasFocus;
        this.leaf = leaf;
        this.expanded = expanded;

        return render();
    }

    private Component render() {
        if (children.isEmpty() || defaultRenderer) {
            defaultRenderer = true;
            children.clear();
            if (table != null) {
                TableCellRenderer tcr;
                if (tableHeader) {
                    tcr = table.getTableHeader().getDefaultRenderer();
                } else {
                    tcr = table.getDefaultRenderer(table.getColumnClass(column));
                }
                children.add(tcr.getTableCellRendererComponent(table, value, selected, focused, row, column));
            } else if (tree != null) {
                TreeCellRenderer tcr;
                tcr = new DefaultTreeCellRenderer();
                children.add(tcr.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, focused));
            } else if (list != null) {
                ListCellRenderer lcr = (ListCellRenderer) UIManager.get("List.cellRenderer");
                if (lcr == null) {
                    lcr = new DefaultListCellRenderer();
                }
                children.add(lcr.getListCellRendererComponent(list, value, row, selected, focused));
            }
        }
        Object o = update.call();
        if (o instanceof Component) {
            return (Component) o;
        } else {
            return (Component) children.get(0);
        }
    }

    public Closure getUpdate() {
        return update;
    }

    public void setUpdate(Closure update) {
        if (update != null) {
            update.setDelegate(this);
            update.setResolveStrategy(Closure.DELEGATE_FIRST);
        }
        this.update = update;
    }

    public void setTableHeader(boolean tableHeader) {
        this.tableHeader = tableHeader;
    }

    public boolean isTableHeader() {
        return tableHeader;
    }

    public List getChildren() {
        return children;
    }

    public JList getList() {
        return list;
    }

    public JTable getTable() {
        return table;
    }

    public Object getValue() {
        return value;
    }

    public boolean isSelected() {
        return selected;
    }

    public boolean isFocused() {
        return focused;
    }

    public int getRow() {
        return row;
    }

    public int getColumn() {
        return column;
    }

    public JTree getTree() {
        return tree;
    }

    public boolean isLeaf() {
        return leaf;
    }

    public boolean isExpanded() {
        return expanded;
    }

    public boolean isDefaultRenderer() {
        return defaultRenderer;
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ClosureRenderer.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.