/
codebase/src/net/sourceforge/pain/admin/console/command/
codebase/src/net/sourceforge/pain/data/role/
codebase/src/net/sourceforge/pain/network/console/telnet/
codebase/src/net/sourceforge/pain/network/guitool/
codebase/src/net/sourceforge/pain/plugin/
codebase/src/net/sourceforge/pain/util/
db/src/net/sourceforge/pain/util/
gui/
gui/lib/
gui/src/net/sourceforge/pain/tools/guitool/dbbrowse/
gui/src/net/sourceforge/pain/tools/guitool/dialog/
gui/src/net/sourceforge/pain/tools/guitool/menu/
gui/src/net/sourceforge/pain/tools/guitool/resources/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/explorer/
mudlibs/tinylib/
mudlibs/tinylib/area/
mudlibs/tinylib/etc/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/prototype/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/deploy/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/fn/util/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/impl/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/command/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/reset/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/shutdown/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/social/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/util/
tests/
tests/src/
tests/src/net/sourceforge/pain/db/data/
package net.sourceforge.pain.tools.guitool.dbbrowse;

import net.sourceforge.pain.network.guitool.*;
import net.sourceforge.pain.tools.guitool.*;
import net.sourceforge.pain.tools.guitool.dialog.*;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

import com.intellij.uiDesigner.core.*;

public class GTRawDbBrowser {
    private JButton findButton;
    private JTextField oidEdit;
    private JLabel oidLabel;
    private JButton refreshButton;
    private JButton nextButton;
    private JButton prevButton;
    private JPanel mainPanel;
    private JTable contentTable;
    private JLabel classNameLabel;

    private TableModel tableModel;
    private static GTRawDbBrowser single;
    private DatabaseSchema schema = null;
    Vector fields = new Vector();
    String activeQuery;

    Vector history = new Vector();
    int histPos = 0;

    public GTRawDbBrowser() {
        init();
    }

    private void ensureSchemaLoaded() throws IOException, InterruptedException {
        if (schema == null) {
            GuiTool.log("Loading database schema.");
            schema = loadSchema();
            GuiTool.log("Database schema loaded!");
        }
    }

    private DatabaseSchema loadSchema() throws IOException, InterruptedException {
        GTNetPacket p = GlobalPerformer.getConnection().sendBlocking(new GTNetPacket("DbLoadSchemaEvent", ""));
        return new DatabaseSchema(p);
    }

    private void init() {
        tableModel = new GTDbBrowserTableModel();
        contentTable.setModel(tableModel);
        contentTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        final TableColumn valueColumn = contentTable.getColumn("Field Value");
        GTRawDbBrowserValueCellHandler re = new GTRawDbBrowserValueCellHandler(this);
        valueColumn.setCellEditor(re);
        valueColumn.setCellRenderer(re);
        findButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                processQuery();
            }
        });
        refreshButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                refreshActiveObject();
            }
        });
        refreshButton.setEnabled(false);
        prevButton.setEnabled(false);
        nextButton.setEnabled(false);

        oidEdit.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    e.consume();
                    processQuery();
                }
            }
        });

        mainPanel.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                oidEdit.selectAll();
                oidEdit.requestFocus();
            }
        });

        prevButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                goPrev();
            }
        });

        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                goNext();
            }
        });
    }


    public synchronized static Component instance() {
        if (single == null) {
            single = new GTRawDbBrowser();
        }
        return single.mainPanel;
    }


    private class GTDbBrowserTableModel extends AbstractTableModel {
        public int getColumnCount() {
            return 3;
        }

        public int getRowCount() {
            return fields.size();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            FieldInfo info = (FieldInfo) fields.get(rowIndex);
            if (columnIndex == 0) {
                return info.name;
            } else if (columnIndex == 1) {
                return info.getTypeName();
            } else {
                return info;
            }
        }

        public String getColumnName(int column) {
            return column == 0 ? "Field Name" : (column == 1) ? "Field Type" : "Field Value";
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 2;
        }

    }

    void executeQuery(String query) {
        GuiTool.log("Changing active object..");
        if (_updateActiveData(query)) {
            GuiTool.log("Done.");
            if (activeQuery != null && (history.isEmpty() || !history.get(histPos).equals(activeQuery))) {
                for (int i = history.size(); --i > histPos;) {
                    history.remove(i);
                }
                history.addElement(activeQuery);
                histPos = history.size() - 1;
            }
        } else {
            GuiTool.log("Failed.");
        }
        updateButtons();
    }

    void processQuery() {
        String query = oidEdit.getText();
        if (query == null || query.length() == 0) {
            return;
        }
        executeQuery(query);

    }

    private void refreshActiveObject() {
        GuiTool.log("Refreshing obj info..");
        _updateActiveData(activeQuery);
        GuiTool.log("Refresh done.");
    }

    private void updateButtons() {
        refreshButton.setEnabled(activeQuery != null);
        nextButton.setEnabled(histPos < history.size() - 1);
        prevButton.setEnabled(histPos > 0);

    }

    private boolean _updateActiveData(String query) {
        try {
            ensureSchemaLoaded();
            query = query.trim();
            if (!"root".equals(query) && !(query.startsWith("[") && query.endsWith("]"))) {
                int fieldNameStart = query.indexOf('.');
                int eqIndex = query.indexOf('=');
                if (fieldNameStart <= 0 || fieldNameStart == query.length() || eqIndex  < fieldNameStart || eqIndex == query.length()-1) {
                    JOptionPane.showMessageDialog(mainPanel, "Illegal ClassName.fieldName='..' construct!", "Error", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                String className = query.substring(0, fieldNameStart);
                String fieldName = query.substring(fieldNameStart + 1, eqIndex);
                DatabaseSchema.ClassInfo clazz = schema.findClassName(className);
                if (clazz == null) {
                    JOptionPane.showMessageDialog(mainPanel, "Class not found:" + className, "Error", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
                int type = clazz.getFieldType(fieldName);
                if (type == -1) {
                    JOptionPane.showMessageDialog(mainPanel, "No such field:" + fieldName, "Error", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            }

            GTNetPacket p = GlobalPerformer.getConnection().sendBlocking(new GTNetPacket("DbBrowseEvent", new String[]{"retrieve", query}));
            final Object values[] = (Object[]) p.data;
            if (values.length == 1) {
                JOptionPane.showMessageDialog(mainPanel, values[0], "Error", JOptionPane.ERROR_MESSAGE);
            } else {
                oidLabel.setText((String) values[0]);
                classNameLabel.setText((String) values[1]);
                fields.clear();
                for (int i = 2; i < values.length; i += 3) {
                    fields.add(new FieldInfo((String) values[i], ((Integer) values[i + 1]).intValue(), values[i + 2]));
                }
                activeQuery = (String) values[0];

                contentTable.setVisible(false);
                TableCellEditor cellEditor = contentTable.getCellEditor();
                if (cellEditor != null) {
                    cellEditor.cancelCellEditing();
                }
                oidEdit.requestFocus();
                contentTable.setVisible(true);
                return true;
            }
        } catch (Exception e) {
            GuiTool.showFailInfo(e);
            GuiTool.log("Error." + e.getMessage());
        }
        return false;
    }

    private void goNext() {
        if (histPos < history.size() - 1) {
            histPos++;
            String oid = (String) history.get(histPos);
            _updateActiveData(oid);
            updateButtons();
        }
    }

    private void goPrev() {
        if (histPos > 0) {
            histPos--;
            String oid = (String) history.get(histPos);
            _updateActiveData(oid);
            updateButtons();
        }
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     */
    private void $$$setupUI$$$() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        mainPanel.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
        panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), null));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 7, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
        prevButton = new JButton();
        prevButton.setText("Prev");
        prevButton.setToolTipText("Goto prev object");
        panel2.add(prevButton, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        nextButton = new JButton();
        nextButton.setText("Next");
        nextButton.setToolTipText("Goto next object");
        panel2.add(nextButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        refreshButton = new JButton();
        refreshButton.setText("Refresh");
        refreshButton.setToolTipText("Refresh fields values");
        panel2.add(refreshButton, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        final Spacer spacer1 = new Spacer();
        panel2.add(spacer1, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null));
        oidEdit = new JTextField();
        oidEdit.setToolTipText("Enter object oid or 'root' ");
        oidEdit.setFocusCycleRoot(false);
        oidEdit.setText("root");
        panel2.add(oidEdit, new GridConstraints(0, 5, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null));
        findButton = new JButton();
        findButton.setText("Find");
        findButton.setToolTipText("Find object with specified oid");
        panel2.add(findButton, new GridConstraints(0, 6, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        final JLabel label1 = new JLabel();
        label1.setText("Query:");
        panel2.add(label1, new GridConstraints(0, 4, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(1, 7, new Insets(0, 0, 0, 0), -1, -1));
        mainPanel.add(panel3, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null));
        panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), null));
        final Spacer spacer2 = new Spacer();
        panel3.add(spacer2, new GridConstraints(0, 6, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null));
        classNameLabel = new JLabel();
        classNameLabel.setText("None");
        panel3.add(classNameLabel, new GridConstraints(0, 5, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        final JLabel label2 = new JLabel();
        label2.setText("Class:");
        panel3.add(label2, new GridConstraints(0, 4, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        final Spacer spacer3 = new Spacer();
        panel3.add(spacer3, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_FIXED, 1, null, null, null));
        oidLabel = new JLabel();
        oidLabel.setText("None");
        panel3.add(oidLabel, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        final Spacer spacer4 = new Spacer();
        panel3.add(spacer4, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_FIXED, 1, null, null, null));
        final JLabel label3 = new JLabel();
        label3.setText("Query:");
        panel3.add(label3, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        final JPanel panel4 = new JPanel();
        panel4.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        mainPanel.add(panel4, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null));
        final JScrollPane scrollPane1 = new JScrollPane();
        panel4.add(scrollPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null));
        contentTable = new JTable();
        scrollPane1.setViewportView(contentTable);
    }

}