queryeditpane.java

来自「自动生成JAVA-Struts网站的程序」· Java 代码 · 共 429 行

JAVA
429
字号
package com.sutternow.swingkar.gui;import javax.swing.*;import javax.swing.text.MaskFormatter;import javax.swing.event.CellEditorListener;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableCellEditor;import javax.swing.table.AbstractTableModel;import javax.swing.border.Border;import java.awt.event.*;import java.awt.*;import java.util.*;import java.util.List;import java.text.ParseException;import org.dom4j.Element;import org.dom4j.DocumentHelper;import net.sf.easylayouts.*;import com.gargoylesoftware.base.gui.TableLayout;import com.sutternow.swingkar.ConfigManager;/** * Created by IntelliJ IDEA. * User: Matthew Payne * Date: Jan 18, 2003 * Time: 1:01:50 AM * To change this template use Options | File Templates. */public class QueryEditPane implements SimpleForm,  ActionListener, MouseListener {    public String Title = "Query";    public QueryEditPane(ConfigManager _cm) {        cm = _cm;        props = new LinkedHashMap();        queryPanel = this.createPanel();    }    public String getTitle() {        return Title;    }    public JPanel getEditForm() {        return queryPanel;    }    private JPanel createPanel() {        final TableLayout layout = new TableLayout();        final JPanel panel = new JPanel();        final JPanel topPanel = new JPanel(new TableLayout());        final JPanel bottomPanel = new JPanel();        /* rowLayout used as parent panel */        RowLayout rowLayout = new RowLayout(panel, 1, 25);        topPanel.add(new JLabel("Name:"), "1,1");        colName = new JTextField(10);        topPanel.add(colName, "1,2");        topPanel.add(new JLabel("Result:"), "2,1");        String types[] = {"single", "list"};        resultType = new JComboBox(types);        topPanel.add(resultType, "2,2");        topPanel.add(new JLabel("Details:"), "3,1");        qtm = new QueryTableModel(props.entrySet());        fieldsTable = new JTable(qtm);        JComboBox conditBox = new JComboBox();        conditBox.addItem("");        conditBox.addItem("=?");        conditBox.addItem(">?");        conditBox.addItem("<?");        conditBox.addItem("=<?");        conditBox.addItem("=>?");        fieldsTable.getColumn("Condition").setCellEditor(new DefaultCellEditor(conditBox));        MaskFormatter formatter = null;        try {            formatter = new MaskFormatter("#");        } catch (ParseException e) {            e.printStackTrace();  //To change body of catch statement use Options | File Templates.        }        fieldsTable.getColumn("Order").setCellEditor(new DefaultCellEditor(new JFormattedTextField(formatter)));        JScrollPane jsp = new JScrollPane(fieldsTable);        jsp.setMinimumSize(new Dimension(130, 80));        jsp.setMaximumSize(new Dimension(390, 260));        jsp.setPreferredSize(new Dimension(360, 240));        fieldsTable.getColumn("Order").setPreferredWidth(6);        fieldsTable.getColumn("Condition").setPreferredWidth(18);       /* cmdSave = new JButton("Save");        cmdCancel = new JButton("Cancel");        cmdSave.addActionListener(this);        cmdCancel.addActionListener(this);        bottomPanel.add(cmdSave);        bottomPanel.add(cmdCancel);*/        layout.setColumnExpandable(0, true);        layout.setColumnExpandable(1, true);        rowLayout.add(topPanel, 0);        rowLayout.add(jsp, 1);        rowLayout.add(bottomPanel, 2);        return panel;    }    public void setValues(Element beanCfg) {        this.clearForm();        String relPath = beanCfg.getUniquePath();        colProps = beanCfg;        colName.setText(colProps.valueOf(relPath + "/name"));        resultType.setSelectedItem(colProps.elementText("result"));        List choices = beanCfg.selectNodes(relPath + "/../*[name()='primary-key' or name()='attribute']/column");        System.out.println("field count is=" + choices.size());        for (Iterator iter = choices.listIterator(); iter.hasNext();) {            QueryBean query = new QueryBean();            String display = "";            Element element = (Element) iter.next();            display = element.elementText("name");            query.setFieldName(element.elementText("name"));            String cond = beanCfg.valueOf(relPath + "/condition[field-name='" + display + "']/field-condition");            if (cond != null) {                query.setCondition(cond);            }            /*                  <condition>                       <field-name>role</field-name>                       <field-condition>=?</field-condition>                  </condition>            */            props.put(display, query);        }        /*         <sorting>             <field-name>firstName</field-name>         </sorting>     */        choices = beanCfg.selectNodes(relPath + "/sorting");        for (int i = 0; i < choices.size(); i++) {            Element elm = (Element) choices.get(i);            elm.elementText("field-name");            QueryBean query = (QueryBean) props.get(elm.elementText("field-name"));            query.setOrder(Integer.toString(i + 1));        }        qtm.requestRefresh();    }    public void actionPerformed(ActionEvent ae) {        JButton src = (JButton) ae.getSource();    }    private void clearForm() {        /*            Set values to defaults        */        colName.setText("");        resultType.setSelectedIndex(1);        props.clear();    }    public void doSave() {        colProps.element("name").setText(colName.getText());        colProps.elements("result").clear();        colProps.addElement("result").setText((String) resultType.getSelectedItem());        colProps.elements("condition").clear();        // clear then save current conditions        Iterator itr = props.entrySet().iterator();        while (itr.hasNext()) {            Map.Entry entry = (Map.Entry) itr.next();            QueryBean qb = (QueryBean) entry.getValue();            if (qb.getCondition() != null && qb.getCondition() != "") {                Element cond = colProps.addElement("condition");                cond.addElement("field-name").setText(qb.getFieldName());                cond.addElement("field-condition").setText(qb.getCondition());            }        }        colProps.elements("sorting").clear();        ArrayList orderedList = new ArrayList(props.values());        Collections.sort(orderedList, new QuerySort());        itr = orderedList.iterator();        while (itr.hasNext()) {            QueryBean qb = (QueryBean) itr.next();            if (qb.getOrder() != null && qb.getOrder().trim() != "") {                System.out.println(qb.getFieldName() + qb.getOrder());                Element cond = colProps.addElement("sorting");                cond.addElement("field-name").setText(qb.getFieldName());            }        }        cm.saveChanges();        System.out.println(this.Title + " Saved");        //cm.saveBuildConfig(this.makeResponse());    }    private void handleMouseEvent(MouseEvent evt) {    }    public void mousePressed(MouseEvent evt) {        handleMouseEvent(evt);    }    public void mouseReleased(MouseEvent evt) {        handleMouseEvent(evt);    }    public void mouseClicked(MouseEvent e) {    }    public void mouseEntered(MouseEvent e) {    }    public void mouseExited(MouseEvent e) {    }    private JPanel queryPanel;    private JTextField colName;    private JComboBox resultType;    private QueryTableModel qtm;    private JTable fieldsTable;    private Map props;   // private JButton cmdSave;  //  private JButton cmdCancel;    private ConfigManager cm;    private Element colProps;}class QueryBean {    String fieldName;    String condition;    String order;    String aggregateCondition;    public String getFieldName() {        return fieldName;    }    public void setFieldName(String fieldName) {        this.fieldName = fieldName;    }    public String getCondition() {        return condition;    }    public void setCondition(String condition) {        this.condition = condition;    }    public String getOrder() {        return order;    }    public void setOrder(String order) {        this.order = order;    }    public String getAggregateCondition() {        return aggregateCondition;    }    public void setAggregateCondition(String aggregateCondition) {        this.aggregateCondition = aggregateCondition;    }}class QuerySort implements Comparator {    public int compare(Object o1, Object o2) {        QueryBean r1 = (QueryBean) o1;        QueryBean r2 = (QueryBean) o2;        if (r2.order == null && r1.order == null) return 0;        if (r1.order == null) return -1;        if (r2.order == null) return 1;        return r2.order.compareTo(r1.order);    }}class QueryTableModel extends AbstractTableModel {    /**     * Constructs an AppList table model.     * @param _appSet  the collection of extentions and associations     */    public QueryTableModel(Set _appSet) {        appSet = _appSet;    }    public int getRowCount() {        return appSet.size();    }    public boolean isCellEditable(int rowIndex, int columnIndex) {        return true;    }    public void requestRefresh() {        /* Used to refresh the table */        super.fireTableDataChanged();    }    public int getColumnCount() {        return 4;    }    public void setValueAt(Object aValue, int r, int c) {        Iterator iter = appSet.iterator();        int iCurrentRow = 0;        while (iter.hasNext()) {            Map.Entry entry = (Map.Entry) iter.next();            if (iCurrentRow == r) {                QueryBean query = (QueryBean) entry.getValue();                switch (c) {                    case 0:                        break; // column 1 not editable                    case 1:                        query.setCondition((String) aValue);                        break;                    case 2:                        query.setAggregateCondition((String) aValue);                        break;                    case 3:                        query.setOrder((String) aValue);                }            }            iCurrentRow++;        }    }    public Object getValueAt(int r, int c) {        Iterator iter = appSet.iterator();        int iCurrentRow = 0;        while (iter.hasNext()) {            Map.Entry entry = (Map.Entry) iter.next();            if (iCurrentRow == r) {                QueryBean query = (QueryBean) entry.getValue();                switch (c) {                    case 0:                        return entry.getKey();                    case 1:                        return query.getCondition();                    case 2:                        return query.getAggregateCondition();                    case 3:                        return query.getOrder();                }            }            iCurrentRow++;        }        return "no value dude";    }    public String getColumnName(int c) {        switch (c) {            case 0:                return "Field Name";            case 1:                return "Condition";            case 2:                return "Aggregate Condition";            case 3:                return "Order";        }        return "no value dude";    }    private Set appSet;} //}}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?