1 package coras.client.ui;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Frame;
6 import java.awt.GridBagConstraints;
7 import java.awt.GridBagLayout;
8 import java.awt.event.ItemEvent;
9 import java.util.Collections;
10 import java.util.List;
11
12 import javax.swing.Icon;
13 import javax.swing.ImageIcon;
14 import javax.swing.JButton;
15 import javax.swing.JCheckBox;
16 import javax.swing.JDialog;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JTable;
20 import javax.swing.table.AbstractTableModel;
21
22 import no.sintef.xml.XmlHelper;
23
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26
27 public class ConsistencyDialog extends JDialog {
28
29 private static final ClassLoader cl = NewElementDialog.class.getClassLoader();
30 private static final Icon WARNING_ICON = new ImageIcon(cl.getResource("images/icons/warning.gif"));
31 private static final Icon ERROR_ICON = new ImageIcon(cl.getResource("images/icons/error.gif"));
32
33 private JPanel jContentPane = null;
34 private JScrollPane consistencyResultScrollPane = null;
35 private JTable consistencyResultTable = null;
36 private JPanel buttonPanel = null;
37 private JCheckBox showWarningsCheckBox = null;
38 private JButton closeButton = null;
39 private AbstractTableModel tableModel = null;
40
41 private boolean showWarnings = false;
42 private Element consistencyElem = null;
43 private List elems = Collections.EMPTY_LIST;
44
45 /***
46 * This is the default constructor
47 */
48 public ConsistencyDialog() {
49 this(null);
50 }
51
52 public ConsistencyDialog(Component owner) {
53 super(owner instanceof Frame ? (Frame)owner : null);
54 initialize();
55 setLocationRelativeTo(owner);
56 }
57
58 public void setConsistencyResult(Document consistencyDoc) {
59 consistencyElem = XmlHelper.getRootElement(consistencyDoc);
60 showWarnings = XmlHelper.getElements(consistencyElem, null, "error").size() == 0;
61 getShowWarningsCheckBox().setSelected(showWarnings);
62 updateElems();
63 }
64
65 private void updateElems() {
66 elems =
67 XmlHelper.getElements(consistencyElem,
68 null,
69 showWarnings ? null : "error");
70 getTableModel().fireTableDataChanged();
71 }
72
73 /***
74 * This method initializes this
75 *
76 * @return void
77 */
78 private void initialize() {
79 this.setSize(500, 300);
80 this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
81 this.setContentPane(getJContentPane());
82 }
83
84 /***
85 * This method initializes jContentPane
86 *
87 * @return javax.swing.JPanel
88 */
89 private JPanel getJContentPane() {
90 if (jContentPane == null) {
91 GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
92 gridBagConstraints2.gridx = 0;
93 gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
94 gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
95 gridBagConstraints2.gridy = 1;
96 GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
97 gridBagConstraints1.gridx = 0;
98 gridBagConstraints1.gridy = 2;
99 GridBagConstraints gridBagConstraints = new GridBagConstraints();
100 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
101 gridBagConstraints.gridy = 0;
102 gridBagConstraints.weightx = 1.0;
103 gridBagConstraints.weighty = 1.0;
104 gridBagConstraints.gridx = 0;
105 jContentPane = new JPanel();
106 jContentPane.setLayout(new GridBagLayout());
107 jContentPane.add(getConsistencyResultScrollPane(), gridBagConstraints);
108 jContentPane.add(getShowWarningsCheckBox(), gridBagConstraints2);
109 jContentPane.add(getButtonPanel(), gridBagConstraints1);
110 }
111 return jContentPane;
112 }
113
114 /***
115 * This method initializes consistencyResultScrollPane
116 *
117 * @return javax.swing.JScrollPane
118 */
119 private JScrollPane getConsistencyResultScrollPane() {
120 if (consistencyResultScrollPane == null) {
121 consistencyResultScrollPane = new JScrollPane();
122 consistencyResultScrollPane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
123 consistencyResultScrollPane.setViewportView(getConsistencyResultTable());
124 consistencyResultScrollPane.getViewport().setBackground(Color.WHITE);
125 }
126 return consistencyResultScrollPane;
127 }
128
129 /***
130 * This method initializes consistencyResultTable
131 *
132 * @return javax.swing.JTable
133 */
134 private JTable getConsistencyResultTable() {
135 if (consistencyResultTable == null) {
136 consistencyResultTable = new JTable();
137 consistencyResultTable.setModel(getTableModel());
138 consistencyResultTable.getColumnModel().getColumn(0).setMaxWidth(20);
139 }
140 return consistencyResultTable;
141 }
142
143 /***
144 * This method initializes buttonPanel
145 *
146 * @return javax.swing.JPanel
147 */
148 private JPanel getButtonPanel() {
149 if (buttonPanel == null) {
150 buttonPanel = new JPanel();
151 buttonPanel.add(getCloseButton(), null);
152 }
153 return buttonPanel;
154 }
155
156 /***
157 * This method initializes showWarningsCheckBox
158 *
159 * @return javax.swing.JCheckBox
160 */
161 private JCheckBox getShowWarningsCheckBox() {
162 if (showWarningsCheckBox == null) {
163 showWarningsCheckBox = new JCheckBox();
164 showWarningsCheckBox.setSelected(false);
165 showWarningsCheckBox.setText("Show warnings");
166 showWarningsCheckBox.setMnemonic(java.awt.event.KeyEvent.VK_W);
167 showWarningsCheckBox.addItemListener(new java.awt.event.ItemListener() {
168 public void itemStateChanged(java.awt.event.ItemEvent e) {
169 showWarnings = e.getStateChange() == ItemEvent.SELECTED;
170 updateElems();
171 }
172 });
173 }
174 return showWarningsCheckBox;
175 }
176
177 /***
178 * This method initializes okButton
179 *
180 * @return javax.swing.JButton
181 */
182 private JButton getCloseButton() {
183 if (closeButton == null) {
184 closeButton = new JButton();
185 closeButton.setText("Close");
186 closeButton.setMnemonic(java.awt.event.KeyEvent.VK_C);
187 closeButton.addActionListener(new java.awt.event.ActionListener() {
188 public void actionPerformed(java.awt.event.ActionEvent e) {
189 dispose();
190 }
191 });
192 }
193 return closeButton;
194 }
195
196 /***
197 * This method initializes tableModel
198 *
199 * @return javax.swing.table.DefaultTableModel
200 */
201 private AbstractTableModel getTableModel() {
202 if (tableModel == null) {
203 tableModel = new ConsistencyTableModel();
204 }
205 return tableModel;
206 }
207
208 class ConsistencyTableModel extends AbstractTableModel {
209 private String[] columnNames = {
210 " ", "Description"
211 };
212
213 public String getColumnName(int column) {
214 return columnNames[column];
215 }
216
217 public Class getColumnClass(int columnIndex) {
218 if (columnIndex == 0) {
219 return Icon.class;
220 } else {
221 return String.class;
222 }
223 }
224
225 public int getRowCount() {
226 return elems.size();
227 }
228
229 public int getColumnCount() {
230 return 2;
231 }
232
233 public Object getValueAt(int rowIndex, int columnIndex) {
234 Element elem = (Element) elems.get(rowIndex);
235
236 switch (columnIndex) {
237 case 0 :
238 if (elem.getLocalName().equals("error")) {
239 return ERROR_ICON;
240 } else {
241 return WARNING_ICON;
242 }
243 case 1 :
244 return XmlHelper.getText(elem);
245 }
246 return null;
247 }
248 }
249
250 }