Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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 org.apache.cayenne.modeler.dialog.validator;
 
 import java.awt.Color;
 import java.util.List;
 
 
 import  org.apache.cayenne.project.validator.ValidationInfo;
 import  org.apache.cayenne.project.validator.Validator;
 
 import  com.jgoodies.forms.builder.PanelBuilder;
 import  com.jgoodies.forms.layout.CellConstraints;
 import  com.jgoodies.forms.layout.FormLayout;

Dialog for displaying validation errors.
 
 public class ValidatorDialog extends CayenneDialog {
 
     protected static ValidatorDialog instance;
 
     public static final Color WARNING_COLOR = new Color(245, 194, 194);
     public static final Color ERROR_COLOR = new Color(237, 121, 121);
 
     protected JTable problemsTable;
     protected JButton closeButton;
     protected JButton refreshButton;
     protected List validationObjects;
 
     public static synchronized void showDialog(
             CayenneModelerFrame editor,
             Validator validator) {
 
         if ( == null) {
              = new ValidatorDialog(editor);
             .centerWindow();
         }
 
         .refreshFromModel(validator);
         .setVisible(true);
     }
 
     public static synchronized void showValidationSuccess(
             CayenneModelerFrame editor,
             Validator val) {
 
         if ( != null) {
             .dispose();
              = null;
         }
 
         JOptionPane
                 .showMessageDialog(Application.getFrame(), "Cayenne project is valid.");
     }
 
     protected ValidatorDialog(CayenneModelerFrame editor) {
         super(editor"Validation Problems"false);
 
        this. = .;
        initView();
        initController();
    }
    private void initView() {
         = new JButton("Refresh");
         = new JButton("Close");
         = new JTable();
        .setRowHeight(25);
        .setRowMargin(3);
        .setDefaultRenderer(ValidationInfo.classnew ValidationRenderer());
        // assemble
        CellConstraints cc = new CellConstraints();
        PanelBuilder builder = new PanelBuilder(new FormLayout(
                "fill:200dlu:grow",
                "pref, 3dlu, fill:40dlu:grow"));
        builder.setDefaultDialogBorder();
        builder
                .addLabel(
                        "Click on any row below to go to the object that has a validation problem:",
                        cc.xy(1, 1));
        builder.add(new JScrollPane(), cc.xy(1, 3));
        JPanel buttons = new JPanel(new FlowLayout(.));
        buttons.add();
        buttons.add();
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(builder.getPanel(), .);
        getContentPane().add(buttons.);
        // TODO: use preferences
        setSize(450, 350);
    }
    private void initController() {
                new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent e) {
                        showFailedObject();
                    }
                });
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                dispose();
            }
        });
            public void actionPerformed(ActionEvent e) {
                Application
                        .getInstance()
                        .getAction(ValidateAction.getActionName())
                        .actionPerformed(e);
            }
        });
        this..addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int row = .rowAtPoint(e.getPoint());
                // if this happens to be a selected row, re-run object selection
                if (row >= 0 && .getSelectedRow() == row) {
                    showFailedObject();
                }
            }
        });
    }
    protected void refreshFromModel(Validator validator) {
         = validator.validationResults();
    }
    private void showFailedObject() {
        if (.getSelectedRow() >= 0) {
            ValidationInfo obj = (ValidationInfo) .getModel().getValueAt(
                    .getSelectedRow(),
                    0);
            ValidationDisplayHandler.getErrorMsg(obj).displayField(
                    getMediator(),
                    super.getParentEditor());
        }
    }
    class ValidatorTableModel extends AbstractTableModel {
        public int getRowCount() {
            return .size();
        }
        public int getColumnCount() {
            return 1;
        }
        public Object getValueAt(int rowint col) {
            return .get(row);
        }
        public boolean isCellEditable(int rowint col) {
            return false;
        }
        public String getColumnName(int column) {
            return " ";
        }
        public Class getColumnClass(int columnIndex) {
            return ValidationInfo.class;
        }
    }
    // a renderer for the error message
                JTable table,
                Object value,
                boolean isSelected,
                boolean hasFocus,
                int row,
                int column) {
            boolean error = false;
            if (value != null) {
                ValidationInfo info = (ValidationInfo) value;
                error = info.getSeverity() == ValidationInfo.ERROR;
                value = (error) ? "Error: " + info.getMessage() : "Warning: "
                        + info.getMessage();
            }
            setBackground(error ?  : );
            return super.getTableCellRendererComponent(
                    table,
                    value,
                    isSelected,
                    hasFocus,
                    row,
                    column);
        }
    }
New to GrepCode? Check out our FAQ X