001/*
002 * Copyright (c) 2004-2007 by Michael Connor. All Rights Reserved.
003 *
004 * Redistribution and use in source and binary forms, with or without
005 * modification, are permitted provided that the following conditions are met:
006 *
007 *  o Redistributions of source code must retain the above copyright notice,
008 *    this list of conditions and the following disclaimer.
009 *
010 *  o Redistributions in binary form must reproduce the above copyright notice,
011 *    this list of conditions and the following disclaimer in the documentation
012 *    and/or other materials provided with the distribution.
013 *
014 *  o Neither the name of FormLayoutBuilder or Michael Connor nor the names of
015 *    its contributors may be used to endorse or promote products derived
016 *    from this software without specific prior written permission.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.mlc.swing.layout;
031
032import java.util.ArrayList;
033import java.util.List;
034import java.util.Map;
035
036import javax.swing.JScrollPane;
037import javax.swing.JTable;
038import javax.swing.table.AbstractTableModel;
039
040/**
041 * This is the ComponentBuilder used to build JTables. When a JTable is added,
042 * it will be given some default data just so the user can get an idea of what
043 * it might look like.
044 *
045 * @author Michael Connor
046@version $Id$
047@since Ptolemy II 8.0
048 */
049public class JTableComponentBuilder implements ComponentBuilder {
050    List<BeanProperty> properties = new ArrayList<BeanProperty>();
051
052    public JTableComponentBuilder() {
053    }
054
055    @Override
056    public String getDeclaration(String name,
057            java.util.Map<String, Object> beanProperties) {
058        return "javax.swing.JTable " + name
059                + "Control = new javax.swing.JTable();\njavax.swing.JScrollPane "
060                + name + " = new javax.swing.JScrollPane(" + name
061                + "Control);\n";
062    }
063
064    @Override
065    @SuppressWarnings("serial")
066    public java.awt.Component getInstance(
067            java.util.Map<String, Object> beanProperties) throws Exception {
068        JTable table = new JTable();
069        table.setModel(new AbstractTableModel() {
070            @Override
071            public int getRowCount() {
072                return 10;
073            }
074
075            @Override
076            public int getColumnCount() {
077                return 10;
078            }
079
080            @Override
081            public Object getValueAt(int row, int col) {
082                return "" + row + ":" + col;
083            }
084        });
085
086        JScrollPane scrollPane = new JScrollPane(table);
087        return scrollPane;
088    }
089
090    @Override
091    public java.util.List<BeanProperty> getProperties() {
092        return properties;
093    }
094
095    @Override
096    public boolean isComponentALayoutContainer() {
097        return false;
098    }
099
100    @Override
101    public String toString() {
102        return "javax.swing.JTable";
103    }
104
105    @Override
106    public ComponentDef getComponentDef(String name,
107            Map<String, Object> beanProperties) {
108        String imp = "import javax.swing.JTable;\n"
109                + "import javax.swing.JScrollPane;";
110        String decl = "JTable ${name}Control = new JTable();\n"
111                + "JScrollPane ${name} = new JScrollPane(${name}Control);";
112        String add = "${container}.add(${name}, \"${name}\");";
113        ComponentDef cd = new ComponentDef(name, imp, decl, add);
114        return cd;
115    }
116
117}