001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.jdbcwizard.wizards;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Dimension;
026: import java.awt.Graphics;
027: import java.util.ArrayList;
028: import java.util.List;
029:
030: import javax.swing.BorderFactory;
031: import javax.swing.BoxLayout;
032: import javax.swing.JCheckBox;
033: import javax.swing.JLabel;
034: import javax.swing.JPanel;
035: import javax.swing.JTable;
036: import javax.swing.JScrollPane;
037: import javax.swing.SwingConstants;
038: import javax.swing.UIManager;
039: import javax.swing.border.Border;
040: import javax.swing.table.AbstractTableModel;
041: import javax.swing.table.DefaultTableCellRenderer;
042: import javax.swing.table.TableCellRenderer;
043: import javax.swing.table.TableColumn;
044: import javax.swing.table.JTableHeader;
045: import org.openide.util.NbBundle;
046:
047: import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBColumn;
048: import org.netbeans.modules.jdbcwizard.builder.dbmodel.impl.DBColumnImpl;
049:
050: /**
051: * @author Administrator
052: */
053: public class PolledColumnPanel extends JPanel implements
054: JDBCTableColumnDisplayable {
055:
056: /**
057: *
058: */
059: private static final long serialVersionUID = 1L;
060:
061: class MetaTColumnComponent extends JTable {
062: /**
063: *
064: */
065: private static final long serialVersionUID = 1L;
066:
067: public MetaTColumnComponent() {
068: // Need to revisit whether should use abstract model here??
069: this .setDefaultRenderer(DBColumnImpl.class,
070: new MyTColumnModelCellRenderer());
071: this .setDefaultRenderer(Boolean.class,
072: new MyBooleanRenderer());
073: final JTableHeader header = this .getTableHeader();
074: header.setReorderingAllowed(false);
075: header.setResizingAllowed(true);
076: }
077: }
078:
079: static class MyBooleanRenderer extends JCheckBox implements
080: TableCellRenderer {
081: /**
082: *
083: */
084: private static final long serialVersionUID = 1L;
085:
086: protected static Border noFocusBorder = BorderFactory
087: .createEmptyBorder(1, 1, 1, 1);
088:
089: private JPanel myPanel;
090:
091: /**
092: * Creates a default MyBooleanRenderer.
093: */
094: public MyBooleanRenderer() {
095: super ();
096: this .setHorizontalAlignment(SwingConstants.CENTER);
097: this .setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
098: this .myPanel = new JPanel();
099: this .myPanel.setLayout(new BorderLayout());
100: this .myPanel.add(this , BorderLayout.CENTER);
101: this .setEnabled(true);
102: this .myPanel.setOpaque(true);
103: this .myPanel.setBorder(MyBooleanRenderer.noFocusBorder);
104: }
105:
106: /**
107: *
108: */
109: public Component getTableCellRendererComponent(
110: final JTable table, final Object value,
111: final boolean isSelected, final boolean hasFocus,
112: final int row, final int column) {
113: final RowDataWrapper rowDW = ((MyTColumnModel) table
114: .getModel()).getRowDataWrapper(row);
115: if (rowDW != null && !rowDW.isEditable().booleanValue()) {
116: this .setEnabled(false);
117: this .setFocusable(false);
118: this .setBackground(Color.LIGHT_GRAY);
119: final Object obj = rowDW.getTColumn();
120: if (obj instanceof DBColumn) {
121: final DBColumn st = (DBColumn) obj; // SourceTable modified to
122: // DBColumn
123: if (!st.isPollSelected()) {
124: this
125: .setToolTipText(NbBundle
126: .getMessage(
127: JDBCWizardTablePanel.class,
128: "TOOLTIP_source_table_disabled_unselected",
129: rowDW.getTColumn()));
130: }
131: }
132: this .myPanel.setBorder(MyBooleanRenderer.noFocusBorder);
133: this .myPanel.setBackground(Color.LIGHT_GRAY);
134: } else {
135: if (isSelected) {
136: this .setForeground(table.getSelectionForeground());
137: this .setBackground(table.getSelectionBackground());
138: this .myPanel.setForeground(table
139: .getSelectionForeground());
140: this .myPanel.setBackground(table
141: .getSelectionBackground());
142: } else {
143: this .setForeground(table.getForeground());
144: this .setBackground(table.getBackground());
145: this .myPanel.setForeground(table.getForeground());
146: this .myPanel.setBackground(table.getBackground());
147: }
148: if (hasFocus) { // NOI18N this scope block
149: this .myPanel
150: .setBorder(UIManager
151: .getBorder("Table.focusCellHighlightBorder"));
152: if (table.isCellEditable(row, column)) {
153: this .setForeground(UIManager
154: .getColor("Table.focusCellForeground"));
155: this .setBackground(UIManager
156: .getColor("Table.focusCellBackground"));
157: }
158: this .myPanel.setForeground(UIManager
159: .getColor("Table.focusCellForeground"));
160: this .myPanel.setBackground(UIManager
161: .getColor("Table.focusCellBackground"));
162: } else {
163: this .myPanel
164: .setBorder(MyBooleanRenderer.noFocusBorder);
165: }
166: this .setFocusable(true);
167: this .setSelected(true);
168: this .setToolTipText("");
169: }
170: this .setSelected((value != null && ((Boolean) value)
171: .booleanValue()));
172: return this .myPanel;
173: }
174:
175: /**
176: * Overrides <code>JComponent.setBackground</code> to assign the unselected-background
177: * color to the specified color.
178: *
179: * @param c set the background color to this value
180: */
181: public void setBackground(final Color c) {
182: super .setBackground(c);
183: }
184:
185: /**
186: * Overrides <code>JComponent.setForeground</code> to assign the unselected-foreground
187: * color to the specified color.
188: *
189: * @param c set the foreground color to this value
190: */
191: public void setForeground(final Color c) {
192: super .setForeground(c);
193: }
194: }
195:
196: class MyTColumnModel extends AbstractTableModel {
197: /**
198: *
199: */
200: private static final long serialVersionUID = 1L;
201:
202: private final String[] tcolumnNames = { "Select", "Column Name" };
203:
204: private List rowList;
205:
206: public MyTColumnModel(final List testList) {
207: this .rowList = new ArrayList();
208: for (int i = 0; i < testList.size(); i++) {
209: final RowDataWrapper rowData = new RowDataWrapper(
210: (DBColumn) testList.get(i));
211: this .rowList.add(rowData);
212: }
213: }
214:
215: /*
216: * JTable uses this method to determine the default renderer/ editor for each cell. If we
217: * didn't implement this method, then the last column would contain text ("true"/"false"),
218: * rather than a check box.
219: */
220: /**
221: *
222: */
223: public Class getColumnClass(final int c) {
224: return this .getValueAt(0, c).getClass();
225: }
226:
227: /**
228: *
229: */
230: public int getColumnCount() {
231: return this .tcolumnNames.length;
232: }
233:
234: /**
235: *
236: */
237: public String getColumnName(final int col) {
238: return this .tcolumnNames[col];
239: }
240:
241: /**
242: *
243: */
244: public int getRowCount() {
245: return this .rowList.size();
246: }
247:
248: /**
249: * @param row
250: * @return
251: */
252: public RowDataWrapper getRowDataWrapper(final int row) {
253: if (row < this .rowList.size()) {
254: return (RowDataWrapper) this .rowList.get(row);
255: }
256: return null;
257: }
258:
259: /**
260: * @return
261: */
262: public ArrayList getTColumns() {
263: final ArrayList columnList = new ArrayList();
264: for (int i = 0; i < this .rowList.size(); i++) {
265: final RowDataWrapper rowData = (RowDataWrapper) this .rowList
266: .get(i);
267: columnList.add(rowData.getTColumn());
268: }
269: return columnList;
270: }
271:
272: /**
273: *
274: */
275: public Object getValueAt(final int row, final int col) {
276: final RowDataWrapper rowData = (RowDataWrapper) this .rowList
277: .get(row);
278: switch (col) {
279: case 0:
280: return rowData.isSelected();
281: case 1:
282: return rowData.getTColumn();
283: }
284: return String.valueOf(col + "?");
285: }
286:
287: /*
288: * Don't need to implement this method unless your table's editable.
289: */
290: /**
291: *
292: */
293: public boolean isCellEditable(final int row, final int col) {
294: // Note that the data/cell address is constant,
295: // no matter where the cell appears onscreen.
296: final Object rowObj = this .rowList.get(row);
297: return rowObj != null ? ((RowDataWrapper) rowObj)
298: .isEditable().booleanValue()
299: && col == 0 : false;
300: }
301:
302: /**
303: * @param row
304: * @param col
305: * @param flag
306: */
307: public void setCellEditable(final int row, final int col,
308: final boolean flag) {
309: final Object rowObj = this .rowList.get(row);
310: if (rowObj != null) {
311: ((RowDataWrapper) rowObj)
312: .setEditable(flag ? Boolean.TRUE
313: : Boolean.FALSE);
314: }
315: }
316:
317: /*
318: * Don't need to implement this method unless your table's data can change.
319: */
320: /**
321: *
322: */
323: public void setValueAt(final Object value, final int row,
324: final int col) {
325: final RowDataWrapper rowData = (RowDataWrapper) this .rowList
326: .get(row);
327: switch (col) {
328: case 0:
329: rowData.setSelected((Boolean) value);
330: this .fireTableRowsUpdated(row, row);
331: break;
332: }
333: }
334: }
335:
336: static class MyTColumnModelCellRenderer extends
337: DefaultTableCellRenderer {
338: /**
339: *
340: */
341: private static final long serialVersionUID = 1L;
342: protected static Border noFocusBorder1 = BorderFactory
343: .createEmptyBorder(1, 1, 1, 1);
344:
345: /**
346: * Creates a default MyBooleanRenderer.
347: */
348: public MyTColumnModelCellRenderer() {
349: super ();
350: }
351:
352: /**
353: *
354: */
355: public Component getTableCellRendererComponent(
356: final JTable table, final Object value,
357: final boolean isSelected, final boolean hasFocus,
358: final int row, final int column) {
359: final JLabel renderer = (JLabel) super
360: .getTableCellRendererComponent(table, value,
361: isSelected, hasFocus, row, column);
362: final MyTColumnModel model = (MyTColumnModel) table
363: .getModel();
364: final RowDataWrapper rowDW = model.getRowDataWrapper(row);
365: if (rowDW != null && !rowDW.isEditable().booleanValue()) {
366: renderer.setEnabled(false);
367: renderer.setBackground(Color.lightGray);
368: final Object obj = rowDW.getTColumn();
369: final DBColumn st = (DBColumn) obj;
370: if (!st.isPollSelected()) {
371: renderer.setToolTipText(NbBundle.getMessage(
372: JDBCWizardTablePanel.class,
373: "TOOLTIP_source_table_disabled_unselected",
374: rowDW.getTColumn()));
375: }
376: renderer
377: .setBorder(MyTColumnModelCellRenderer.noFocusBorder1);
378: renderer.setFocusable(false);
379: } else {
380: if (isSelected) {
381: renderer.setForeground(table
382: .getSelectionForeground());
383: renderer.setBackground(table
384: .getSelectionBackground());
385: } else {
386: renderer.setForeground(table.getForeground());
387: renderer.setBackground(table.getBackground());
388: }
389: if (value instanceof DBColumn) {
390: final DBColumn dbModleTbl = (DBColumn) value;
391: if (dbModleTbl.getName() != null) {
392: this .setText(dbModleTbl.getName());
393: }
394: }
395: renderer.setToolTipText("");
396: renderer.setEnabled(true);
397: renderer.setFocusable(true);
398: }
399: return renderer;
400: }
401: }
402:
403: class RowDataWrapper {
404: private DBColumn tcolumn;
405:
406: /**
407: * @param mTColumn
408: */
409: public RowDataWrapper(final DBColumn mTColumn) {
410: this .tcolumn = mTColumn;
411: }
412:
413: /**
414: * @return
415: */
416: public Object getTColumn() {
417: return this .tcolumn;
418: }
419:
420: /**
421: * @return
422: */
423: public Boolean isEditable() {
424: return this .tcolumn.isEditable() ? Boolean.TRUE
425: : Boolean.FALSE;
426: // return Boolean.TRUE;
427: }
428:
429: /**
430: * @return
431: */
432: public Boolean isSelected() {
433: return this .tcolumn.isPollSelected() ? Boolean.TRUE
434: : Boolean.FALSE;
435: }
436:
437: /**
438: * @param isEditable
439: */
440: public void setEditable(final Boolean isEditable) {
441: this .tcolumn.setEditable(isEditable.booleanValue());
442: }
443:
444: /**
445: * @param isSelected
446: */
447: public void setSelected(final Boolean isSelected) {
448: this .tcolumn.setPollSelected(isSelected.booleanValue());
449: }
450: }
451:
452: private JPanel headerPnl;
453:
454: /* table to display meta data */
455: private MetaTColumnComponent metaDataTColumn;
456:
457: /* scrollpane for columns JTable */
458: private JScrollPane tableScroll;
459:
460: /** Creates a default instance of JDBCWizardTablePanel */
461:
462: /**
463: * Creates a new instance of JDBCWizardTablePanel to render the selection of tables
464: * participating in an JDBC collaboration.
465: *
466: * @param testList List of tables
467: */
468: public PolledColumnPanel() {
469: final JPanel p = new JPanel();
470: p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
471: p.setOpaque(false);
472: this .headerPnl = new JPanel();
473: this .headerPnl.setLayout(new BorderLayout());
474: this .headerPnl.setOpaque(false);
475: this .headerPnl.add(p, BorderLayout.NORTH);
476: // addColumnTable(testList);
477: }
478:
479: /**
480: * Gets associated JTable.
481: *
482: * @return JTable
483: */
484: public JTable getColumnTable() {
485: return this .metaDataTColumn;
486: }
487:
488: /**
489: * Gets list of selected tables.
490: *
491: * @return List of selected tables
492: */
493: public List getColumnTables() {
494: final MyTColumnModel tcolumnModel = (MyTColumnModel) this .metaDataTColumn
495: .getModel();
496: return tcolumnModel.getTColumns();
497: }
498:
499: public List getSelectedColumnTables() {
500: final List allctabs = this .getColumnTables();
501: final List selctabs = new ArrayList();
502: for (int cnt = 0; cnt < allctabs.size(); cnt++) {
503: if (((DBColumn) allctabs.get(cnt)).isPollSelected()) {
504: selctabs.add(allctabs.get(cnt));
505: }
506: }
507: return selctabs;
508: }
509:
510: /**
511: * Paints this component
512: *
513: * @param g graphics context
514: */
515: public void paint(final Graphics g) {
516: super .paint(g);
517: }
518:
519: /**
520: * Populates selected tables using items contained in the given List.
521: *
522: * @param tableNameList List of tables to use in repopulating set of selected tables
523: */
524: public void resetColumnTable(final List tcolumnNameList) {
525: final MyTColumnModel myMod = new MyTColumnModel(tcolumnNameList);
526: this .metaDataTColumn.setModel(myMod);
527: // set checkbox column size
528: final TableColumn column = this .metaDataTColumn
529: .getColumnModel().getColumn(0);
530: column.setResizable(true);
531: column.setMinWidth(40);
532: column.setPreferredWidth(80);
533: column.setMaxWidth(120);
534: }
535:
536: public void addColumnTable(final List testList) {
537: this .metaDataTColumn = new MetaTColumnComponent();
538: this .metaDataTColumn
539: .setFont(JDBCTableColumnDisplayable.FONT_TABLE_COLUMNS);
540: this .metaDataTColumn.getTableHeader().setFont(
541: JDBCTableColumnDisplayable.FONT_TABLE_HEADER);
542: final MyTColumnModel myModel = new MyTColumnModel(testList);
543: this .metaDataTColumn.setModel(myModel);
544: this .setLayout(new BorderLayout());
545: // add(headerPnl, BorderLayout.NORTH);
546: this .setPreferredSize(new Dimension(100, 100));
547: this .setMaximumSize(new Dimension(150, 150));
548: // set checkbox column size
549: final TableColumn column = this .metaDataTColumn
550: .getColumnModel().getColumn(0);
551: column.setResizable(true);
552: column.setMinWidth(40);
553: column.setPreferredWidth(80);
554: column.setMaxWidth(120);
555: this .tableScroll = new JScrollPane(this .metaDataTColumn);
556: final javax.swing.border.Border inside = BorderFactory
557: .createCompoundBorder(BorderFactory.createEmptyBorder(
558: 3, 3, 3, 3), BorderFactory
559: .createLineBorder(Color.GRAY));
560: this.tableScroll.setBorder(BorderFactory.createCompoundBorder(
561: BorderFactory.createEtchedBorder(), inside));
562: this.add(this.tableScroll, BorderLayout.CENTER);
563: }
564: }
|