001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    /**
021     * 
022     */
023    package org.mactor.ui.gui.project.editors;
024    
025    import java.awt.event.ActionEvent;
026    import java.awt.event.ActionListener;
027    import java.util.HashMap;
028    import java.util.Iterator;
029    import java.util.LinkedList;
030    import java.util.List;
031    import java.util.Map;
032    
033    import javax.swing.JButton;
034    import javax.swing.JLabel;
035    import javax.swing.JTextField;
036    
037    public class ParamsPanel extends SimpleFormPanel {
038            List<RowPanel> rows = new LinkedList<RowPanel>();
039            public ParamsPanel() {
040                    refreshRows();
041            }
042            public void removeRow(RowPanel row) {
043                    remove(row);
044                    rows.remove(row);
045                    revalidate();
046                    repaint();
047            }
048            public void addNewRow() {
049                    rows.add(new ParamsPanel.RowPanel());
050                    refreshRows();
051            }
052            public void refreshRows() {
053                    removeAll();
054                    Iterator it = rows.iterator();
055                    while (it.hasNext()) {
056                            add((RowPanel) it.next());
057                    }
058                    revalidate();
059                    repaint();
060            }
061            public Map<String, String> getParams() {
062                    HashMap<String, String> m = new HashMap<String, String>();
063                    for (RowPanel row : rows) {
064                            String n = trim(row.nameField.getText());
065                            if (n != null)
066                                    m.put(n, trim(row.valueField.getText()));
067                    }
068                    return m;
069            }
070            private String trim(String s) {
071                    if (s == null)
072                            return null;
073                    s = s.trim();
074                    if (s.length() == 0)
075                            return null;
076                    return s;
077            }
078            class RowPanel extends SimpleRowPanel {
079                    JTextField nameField = new JTextField(10);
080                    JTextField valueField = new JTextField(10);
081                    JButton removeButton = new JButton("X");
082                    public RowPanel() {
083                            add(nameField);
084                            add(new JLabel(":"));
085                            add(valueField);
086                            add(removeButton);
087                            removeButton.addActionListener(new ActionListener() {
088                                    public void actionPerformed(ActionEvent e) {
089                                            removeRow(RowPanel.this);
090                                    }
091                            });
092                    }
093            }
094    }