KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > validator > ValidatorDialog


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler.dialog.validator;
57
58 import java.awt.BorderLayout JavaDoc;
59 import java.awt.Color JavaDoc;
60 import java.awt.Component JavaDoc;
61 import java.awt.FlowLayout JavaDoc;
62 import java.awt.event.ActionEvent JavaDoc;
63 import java.awt.event.ActionListener JavaDoc;
64 import java.awt.event.MouseAdapter JavaDoc;
65 import java.awt.event.MouseEvent JavaDoc;
66 import java.util.Collections JavaDoc;
67 import java.util.List JavaDoc;
68
69 import javax.swing.JButton JavaDoc;
70 import javax.swing.JDialog JavaDoc;
71 import javax.swing.JOptionPane JavaDoc;
72 import javax.swing.JPanel JavaDoc;
73 import javax.swing.JScrollPane JavaDoc;
74 import javax.swing.JTable JavaDoc;
75 import javax.swing.event.ListSelectionEvent JavaDoc;
76 import javax.swing.event.ListSelectionListener JavaDoc;
77 import javax.swing.table.AbstractTableModel JavaDoc;
78 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
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 /**
92  * Dialog for displaying validation errors.
93  *
94  * @author Michael Misha Shengaout
95  * @author Andrei Adamchik
96  */

97 public class ValidatorDialog extends CayenneDialog {
98
99     protected static ValidatorDialog instance;
100
101     public static final Color JavaDoc WARNING_COLOR = new Color JavaDoc(245, 194, 194);
102     public static final Color JavaDoc ERROR_COLOR = new Color JavaDoc(237, 121, 121);
103
104     protected JTable JavaDoc problemsTable;
105     protected JButton JavaDoc closeButton;
106     protected JButton JavaDoc refreshButton;
107     protected List JavaDoc 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 JavaDoc("Refresh");
147         closeButton = new JButton JavaDoc("Close");
148
149         problemsTable = new JTable JavaDoc();
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         // assemble
157
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 JavaDoc(problemsTable), cc.xy(1, 3));
169
170         JPanel JavaDoc buttons = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
171         buttons.add(refreshButton);
172         buttons.add(closeButton);
173
174         getContentPane().setLayout(new BorderLayout JavaDoc());
175         getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
176         getContentPane().add(buttons, BorderLayout.SOUTH);
177
178         // TODO: use preferences
179
setSize(450, 350);
180     }
181
182     private void initController() {
183
184         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
185         problemsTable.getSelectionModel().addListSelectionListener(
186                 new ListSelectionListener JavaDoc() {
187
188                     public void valueChanged(ListSelectionEvent JavaDoc e) {
189                         showFailedObject();
190                     }
191                 });
192
193         closeButton.addActionListener(new ActionListener JavaDoc() {
194
195             public void actionPerformed(ActionEvent JavaDoc e) {
196                 setVisible(false);
197                 dispose();
198             };
199         });
200
201         refreshButton.addActionListener(new ActionListener JavaDoc() {
202
203             public void actionPerformed(ActionEvent JavaDoc e) {
204                 Application
205                         .getFrame()
206                         .getAction(ValidateAction.getActionName())
207                         .actionPerformed(e);
208             };
209         });
210
211         this.problemsTable.addMouseListener(new MouseAdapter JavaDoc() {
212
213             public void mouseClicked(MouseEvent JavaDoc e) {
214                 int row = problemsTable.rowAtPoint(e.getPoint());
215
216                 // if this happens to be a selected row, re-run object selection
217
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 JavaDoc {
241
242         public int getRowCount() {
243             return validationObjects.size();
244         }
245
246         public int getColumnCount() {
247             return 1;
248         }
249
250         public Object JavaDoc 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 JavaDoc getColumnName(int column) {
259             return " ";
260         }
261
262         public Class JavaDoc getColumnClass(int columnIndex) {
263             return ValidationInfo.class;
264         }
265     }
266
267     // a renderer for the error message
268
class ValidationRenderer extends DefaultTableCellRenderer JavaDoc {
269
270         public Component JavaDoc getTableCellRendererComponent(
271                 JTable JavaDoc table,
272                 Object JavaDoc 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