1 56 package org.objectstyle.cayenne.modeler.dialog.validator; 57 58 import java.awt.BorderLayout ; 59 import java.awt.Color ; 60 import java.awt.Component ; 61 import java.awt.FlowLayout ; 62 import java.awt.event.ActionEvent ; 63 import java.awt.event.ActionListener ; 64 import java.awt.event.MouseAdapter ; 65 import java.awt.event.MouseEvent ; 66 import java.util.Collections ; 67 import java.util.List ; 68 69 import javax.swing.JButton ; 70 import javax.swing.JDialog ; 71 import javax.swing.JOptionPane ; 72 import javax.swing.JPanel ; 73 import javax.swing.JScrollPane ; 74 import javax.swing.JTable ; 75 import javax.swing.event.ListSelectionEvent ; 76 import javax.swing.event.ListSelectionListener ; 77 import javax.swing.table.AbstractTableModel ; 78 import javax.swing.table.DefaultTableCellRenderer ; 79 80 import org.objectstyle.cayenne.modeler.Application; 81 import org.objectstyle.cayenne.modeler.CayenneModelerFrame; 82 import org.objectstyle.cayenne.modeler.action.ValidateAction; 83 import org.objectstyle.cayenne.modeler.util.CayenneDialog; 84 import org.objectstyle.cayenne.project.validator.ValidationInfo; 85 import org.objectstyle.cayenne.project.validator.Validator; 86 87 import com.jgoodies.forms.builder.PanelBuilder; 88 import com.jgoodies.forms.layout.CellConstraints; 89 import com.jgoodies.forms.layout.FormLayout; 90 91 97 public class ValidatorDialog extends CayenneDialog { 98 99 protected static ValidatorDialog instance; 100 101 public static final Color WARNING_COLOR = new Color (245, 194, 194); 102 public static final Color ERROR_COLOR = new Color (237, 121, 121); 103 104 protected JTable problemsTable; 105 protected JButton closeButton; 106 protected JButton refreshButton; 107 protected List validationObjects; 108 109 public static synchronized void showDialog( 110 CayenneModelerFrame editor, 111 Validator validator) { 112 113 if (instance == null) { 114 instance = new ValidatorDialog(editor); 115 instance.centerWindow(); 116 } 117 118 instance.refreshFromModel(validator); 119 instance.setVisible(true); 120 } 121 122 public static synchronized void showValidationSuccess( 123 CayenneModelerFrame editor, 124 Validator val) { 125 126 if (instance != null) { 127 instance.dispose(); 128 instance = null; 129 } 130 131 JOptionPane 132 .showMessageDialog(Application.getFrame(), "Cayenne project is valid."); 133 } 134 135 protected ValidatorDialog(CayenneModelerFrame editor) { 136 super(editor, "Validation Problems", false); 137 138 this.validationObjects = Collections.EMPTY_LIST; 139 140 initView(); 141 initController(); 142 } 143 144 private void initView() { 145 146 refreshButton = new JButton ("Refresh"); 147 closeButton = new JButton ("Close"); 148 149 problemsTable = new JTable (); 150 problemsTable.setRowHeight(25); 151 problemsTable.setRowMargin(3); 152 problemsTable.setCellSelectionEnabled(false); 153 problemsTable.setRowSelectionAllowed(true); 154 problemsTable.setDefaultRenderer(ValidationInfo.class, new ValidationRenderer()); 155 156 CellConstraints cc = new CellConstraints(); 158 PanelBuilder builder = new PanelBuilder(new FormLayout( 159 "fill:200dlu:grow", 160 "pref, 3dlu, top:40dlu:grow")); 161 162 builder.setDefaultDialogBorder(); 163 164 builder 165 .addLabel( 166 "Click on any row below to go to the object that has a validation problem:", 167 cc.xy(1, 1)); 168 builder.add(new JScrollPane (problemsTable), cc.xy(1, 3)); 169 170 JPanel buttons = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 171 buttons.add(refreshButton); 172 buttons.add(closeButton); 173 174 getContentPane().setLayout(new BorderLayout ()); 175 getContentPane().add(builder.getPanel(), BorderLayout.CENTER); 176 getContentPane().add(buttons, BorderLayout.SOUTH); 177 178 setSize(450, 350); 180 } 181 182 private void initController() { 183 184 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 185 problemsTable.getSelectionModel().addListSelectionListener( 186 new ListSelectionListener () { 187 188 public void valueChanged(ListSelectionEvent e) { 189 showFailedObject(); 190 } 191 }); 192 193 closeButton.addActionListener(new ActionListener () { 194 195 public void actionPerformed(ActionEvent e) { 196 setVisible(false); 197 dispose(); 198 }; 199 }); 200 201 refreshButton.addActionListener(new ActionListener () { 202 203 public void actionPerformed(ActionEvent e) { 204 Application 205 .getFrame() 206 .getAction(ValidateAction.getActionName()) 207 .actionPerformed(e); 208 }; 209 }); 210 211 this.problemsTable.addMouseListener(new MouseAdapter () { 212 213 public void mouseClicked(MouseEvent e) { 214 int row = problemsTable.rowAtPoint(e.getPoint()); 215 216 if (row >= 0 && problemsTable.getSelectedRow() == row) { 218 showFailedObject(); 219 } 220 } 221 }); 222 } 223 224 protected void refreshFromModel(Validator validator) { 225 validationObjects = validator.validationResults(); 226 problemsTable.setModel(new ValidatorTableModel()); 227 } 228 229 private void showFailedObject() { 230 if (problemsTable.getSelectedRow() >= 0) { 231 ValidationInfo obj = (ValidationInfo) problemsTable.getModel().getValueAt( 232 problemsTable.getSelectedRow(), 233 0); 234 ValidationDisplayHandler.getErrorMsg(obj).displayField( 235 getMediator(), 236 super.getParentEditor()); 237 } 238 } 239 240 class ValidatorTableModel extends AbstractTableModel { 241 242 public int getRowCount() { 243 return validationObjects.size(); 244 } 245 246 public int getColumnCount() { 247 return 1; 248 } 249 250 public Object getValueAt(int row, int col) { 251 return validationObjects.get(row); 252 } 253 254 public boolean isCellEditable(int row, int col) { 255 return false; 256 } 257 258 public String getColumnName(int column) { 259 return " "; 260 } 261 262 public Class getColumnClass(int columnIndex) { 263 return ValidationInfo.class; 264 } 265 } 266 267 class ValidationRenderer extends DefaultTableCellRenderer { 269 270 public Component getTableCellRendererComponent( 271 JTable table, 272 Object value, 273 boolean isSelected, 274 boolean hasFocus, 275 int row, 276 int column) { 277 278 boolean error = false; 279 if (value != null) { 280 ValidationInfo info = (ValidationInfo) value; 281 error = info.getSeverity() == ValidationInfo.ERROR; 282 value = (error) ? "Error: " + info.getMessage() : "Warning: " 283 + info.getMessage(); 284 } 285 286 setBackground(error ? ERROR_COLOR : WARNING_COLOR); 287 return super.getTableCellRendererComponent( 288 table, 289 value, 290 isSelected, 291 hasFocus, 292 row, 293 column); 294 } 295 } 296 } 297 | Popular Tags |