KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > fromdb > TableUISupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.AbstractListModel JavaDoc;
31 import javax.swing.DefaultListCellRenderer JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JList JavaDoc;
35 import javax.swing.JTable JavaDoc;
36 import javax.swing.UIManager JavaDoc;
37 import javax.swing.event.ChangeEvent JavaDoc;
38 import javax.swing.event.ChangeListener JavaDoc;
39 import javax.swing.table.AbstractTableModel JavaDoc;
40 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
41 import javax.swing.table.TableColumn JavaDoc;
42 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.Table.DisabledReason;
43 import org.openide.util.NbBundle;
44
45 /**
46  *
47  * @author Andrei Badea
48  */

49 public class TableUISupport {
50
51     private TableUISupport() {
52     }
53
54     public static void connectAvailable(JList JavaDoc availableTablesList, TableClosure tableClosure) {
55         availableTablesList.setModel(new AvailableTablesModel(tableClosure));
56
57         if (!(availableTablesList.getCellRenderer() instanceof AvailableTableRenderer)) {
58             availableTablesList.setCellRenderer(new AvailableTableRenderer());
59         }
60     }
61
62     public static void connectSelected(JList JavaDoc selectedTablesList, TableClosure tableClosure) {
63         selectedTablesList.setModel(new SelectedTablesModel(tableClosure));
64
65         if (!(selectedTablesList.getCellRenderer() instanceof SelectedTableRenderer)) {
66             selectedTablesList.setCellRenderer(new SelectedTableRenderer());
67         }
68     }
69
70     public static Set JavaDoc<Table> getSelectedTables(JList JavaDoc list) {
71         Set JavaDoc<Table> result = new HashSet JavaDoc<Table>();
72
73         Object JavaDoc[] selectedValues = list.getSelectedValues();
74         for (int i = 0; i < selectedValues.length; i++) {
75             result.add((Table)selectedValues[i]);
76         }
77
78         return result;
79     }
80
81     public static void connectClassNames(JTable JavaDoc table, SelectedTables selectedTables) {
82         table.setModel(new TableClassNamesModel(selectedTables));
83         setRenderer(table.getColumnModel().getColumn(0));
84         setRenderer(table.getColumnModel().getColumn(1));
85     }
86
87     private static void setRenderer(TableColumn JavaDoc column) {
88         if (!(column.getCellRenderer() instanceof TableClassNameRenderer)) {
89             column.setCellRenderer(new TableClassNameRenderer());
90         }
91     }
92
93     private static final class AvailableTablesModel extends AbstractListModel JavaDoc implements ChangeListener JavaDoc {
94
95         private final TableClosure tableClosure;
96
97         private List JavaDoc<Table> displayTables;
98
99         public AvailableTablesModel(TableClosure tableClosure) {
100             this.tableClosure = tableClosure;
101             tableClosure.addChangeListener(this);
102             refresh();
103         }
104
105         public Object JavaDoc getElementAt(int index) {
106             return displayTables.get(index);
107         }
108
109         public int getSize() {
110             return displayTables != null ? displayTables.size() : 0;
111         }
112
113         public void stateChanged(ChangeEvent JavaDoc event) {
114             refresh();
115         }
116
117         private void refresh() {
118             int oldSize = getSize();
119             displayTables = new ArrayList JavaDoc<Table>(tableClosure.getAvailableTables());
120             Collections.sort(displayTables);
121             fireContentsChanged(this, 0, Math.max(oldSize, getSize()));
122         }
123     }
124
125     private static final class SelectedTablesModel extends AbstractListModel JavaDoc implements ChangeListener JavaDoc {
126
127         private final TableClosure tableClosure;
128
129         private List JavaDoc<Table> displayTables;
130
131         public SelectedTablesModel(TableClosure tableClosure) {
132             this.tableClosure = tableClosure;
133             tableClosure.addChangeListener(this);
134             refresh();
135         }
136
137         public Object JavaDoc getElementAt(int index) {
138             return displayTables.get(index);
139         }
140
141         public int getSize() {
142             return displayTables != null ? displayTables.size() : 0;
143         }
144
145         public void stateChanged(ChangeEvent JavaDoc event) {
146             refresh();
147         }
148
149         private void refresh() {
150             int oldSize = getSize();
151             displayTables = new ArrayList JavaDoc<Table>(tableClosure.getSelectedTables());
152             Collections.sort(displayTables);
153             fireContentsChanged(this, 0, Math.max(oldSize, getSize()));
154         }
155
156         public TableClosure getTableClosure() {
157             return tableClosure;
158         }
159     }
160
161     private static final class AvailableTableRenderer extends DefaultListCellRenderer JavaDoc {
162
163         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
164             DisabledReason disabledReason = null;
165             Object JavaDoc displayName = null;
166
167             if (value instanceof Table) {
168                 Table tableItem = (Table)value;
169                 disabledReason = tableItem.getDisabledReason();
170                 if (disabledReason!= null) {
171                     displayName = NbBundle.getMessage(TableUISupport.class, "LBL_TableNameWithDisabledReason", tableItem.getName(), disabledReason.getDisplayName());
172                 } else {
173                     displayName = tableItem.getName();
174                 }
175             }
176
177             JLabel JavaDoc component = (JLabel JavaDoc)super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus);
178             component.setEnabled(disabledReason == null);
179             component.setToolTipText(disabledReason != null ? disabledReason.getDescription() : null);
180
181             return component;
182         }
183
184     }
185
186     private static final class SelectedTableRenderer extends DefaultListCellRenderer JavaDoc {
187
188         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
189             Table table = null;
190             Object JavaDoc displayName = null;
191             boolean referenced = false;
192             TableClosure tableClosure = null;
193
194             if (value instanceof Table) {
195                 table = (Table)value;
196                 displayName = table.getName();
197
198                 if (list.getModel() instanceof SelectedTablesModel) {
199                     SelectedTablesModel model = (SelectedTablesModel)list.getModel();
200                     tableClosure = model.getTableClosure();
201                     referenced = tableClosure.getReferencedTables().contains(table);
202                 }
203             } else {
204                 displayName = value;
205             }
206
207             JLabel JavaDoc component = (JLabel JavaDoc)super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus);
208             component.setEnabled(!referenced);
209             component.setToolTipText(referenced ? getTableTooltip(table, tableClosure) : null); // NOI18N
210

211             return component;
212         }
213
214         private static String JavaDoc getTableTooltip(Table table, TableClosure tableClosure) {
215             List JavaDoc<Table> tables = new ArrayList JavaDoc<Table>();
216             Set JavaDoc<Table> relatedTables;
217             String JavaDoc bundleKey;
218
219             if (table.isJoin()) {
220                 relatedTables = table.getReferencedTables();
221                 bundleKey = "LBL_RelatedTableJoin"; // NOI18N
222
} else {
223                 relatedTables = table.getReferencedByTables();
224                 bundleKey = "LBL_RelatedTableRefBy"; // NOI18N
225
}
226             for (Iterator JavaDoc<Table> i = relatedTables.iterator(); i.hasNext();) {
227                 Table refTable = i.next();
228                 if (tableClosure.getSelectedTables().contains(refTable)) {
229                     tables.add(refTable);
230                 }
231             }
232             return NbBundle.getMessage(TableUISupport.class, bundleKey, createTableList(tables));
233         }
234
235         private static String JavaDoc createTableList(List JavaDoc<Table> tables) {
236             assert tables.size() > 0;
237
238             if (tables.size() == 1) {
239                 return tables.iterator().next().getName();
240             }
241
242             Collections.sort(tables);
243
244             String JavaDoc separator = NbBundle.getMessage(TableUISupport.class, "LBL_TableListSep");
245             Iterator JavaDoc<Table> i = tables.iterator();
246             StringBuilder JavaDoc builder = new StringBuilder JavaDoc(i.next().getName());
247             String JavaDoc lastTable = i.next().getName();
248             while (i.hasNext()) {
249                 builder.append(separator);
250                 builder.append(lastTable);
251                 lastTable = i.next().getName();
252             }
253             return NbBundle.getMessage(TableUISupport.class, "LBL_TableList", builder.toString(), lastTable);
254         }
255     }
256
257     private static final class TableClassNamesModel extends AbstractTableModel JavaDoc {
258
259         private SelectedTables selectedTables;
260         private final List JavaDoc<Table> tables;
261
262         public TableClassNamesModel(SelectedTables selectedTables) {
263             this.selectedTables = selectedTables;
264             this.tables = selectedTables.getTables();
265         }
266
267         public Table getTableAt(int rowIndex) {
268             return tables.get(rowIndex);
269         }
270
271         public boolean isValidClass(Table table) {
272             return !selectedTables.hasProblem(table);
273         }
274
275         public String JavaDoc getProblemDisplayName(Table table) {
276             return selectedTables.getProblemDisplayNameForTable(table);
277         }
278
279         public int getRowCount() {
280             return tables.size();
281         }
282
283         public int getColumnCount() {
284             return 2;
285         }
286
287         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
288             switch (columnIndex) {
289                 case 0:
290                     return tables.get(rowIndex).getName();
291
292                 case 1:
293                     Table table = tables.get(rowIndex);
294                     return selectedTables.getClassName(table);
295
296                 default:
297                     assert false;
298             }
299
300             return null;
301         }
302
303         public void setValueAt(Object JavaDoc value, int rowIndex, int columnIndex) {
304             if (columnIndex != 1) {
305                 return;
306             }
307
308             Table table = tables.get(rowIndex);
309             selectedTables.setClassName(table, (String JavaDoc)value);
310         }
311
312         public boolean isCellEditable(int rowIndex, int columnIndex) {
313             Table table = tables.get(rowIndex);
314             return !table.isJoin() && columnIndex == 1;
315         }
316
317         public String JavaDoc getColumnName(int column) {
318             switch (column) {
319                 case 0:
320                     return NbBundle.getMessage(TableUISupport.class, "LBL_DatabaseTable");
321
322                 case 1:
323                     return NbBundle.getMessage(TableUISupport.class, "LBL_ClassName");
324
325                 default:
326                     assert false;
327             }
328
329             return null;
330         }
331     }
332
333     private static final class TableClassNameRenderer extends DefaultTableCellRenderer JavaDoc {
334         private static Color JavaDoc errorForeground;
335         private static Color JavaDoc nonErrorForeground;
336
337         static {
338             errorForeground = UIManager.getColor("nb.errorForeground"); //NOI18N
339
if (errorForeground == null) {
340                 errorForeground = Color.RED;
341             }
342             nonErrorForeground = UIManager.getColor("Label.foreground"); // NOI18N
343
}
344
345         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc jTable, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
346             boolean joinTable = false;
347             boolean validClass = true;
348             String JavaDoc problemDisplayName = null;
349
350             if (jTable.getModel() instanceof TableClassNamesModel) {
351                 TableClassNamesModel model = (TableClassNamesModel)jTable.getModel();
352                 Table table = model.getTableAt(row);
353                 joinTable = table.isJoin();
354                 if (column == 1) {
355                     validClass = model.isValidClass(table);
356                     if (!validClass) {
357                         problemDisplayName = model.getProblemDisplayName(table);
358                     }
359                 }
360             }
361
362             Object JavaDoc realValue = null;
363             if (joinTable && column == 1) {
364                 realValue = NbBundle.getMessage(TableUISupport.class, "LBL_JoinTable");
365             } else {
366                 realValue = value;
367             }
368             JComponent JavaDoc component = (JComponent JavaDoc)super.getTableCellRendererComponent(jTable, realValue, isSelected, hasFocus, row, column);
369             component.setEnabled(!joinTable);
370             component.setToolTipText(joinTable ? NbBundle.getMessage(TableUISupport.class, "LBL_JoinTableDescription") : problemDisplayName);
371             component.setForeground((validClass) ? nonErrorForeground : errorForeground);
372            
373             return component;
374         }
375     }
376 }
377
Popular Tags