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 InsertColumnPanel 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: if (rowDW != null) {
117: //this.setEnabled(false);
118: //this.setFocusable(false);
119: //this.setBackground(Color.LIGHT_GRAY);
120: final Object obj = rowDW.getTColumn();
121: if (obj instanceof DBColumn) {
122: final DBColumn st = (DBColumn) obj; // SourceTable modified to
123: // DBColumn
124: if (!st.isInsertSelected()) {
125: this
126: .setToolTipText(NbBundle
127: .getMessage(
128: JDBCWizardTablePanel.class,
129: "TOOLTIP_source_table_disabled_unselected",
130: rowDW.getTColumn()));
131: }
132: if (!st.isNullable()) {
133: st.setInsertEditable(false);
134: }
135: }
136: this .myPanel.setBorder(MyBooleanRenderer.noFocusBorder);
137: this .myPanel.setBackground(Color.LIGHT_GRAY);
138: } else {
139: if (isSelected) {
140: this .setForeground(table.getSelectionForeground());
141: this .setBackground(table.getSelectionBackground());
142: this .myPanel.setForeground(table
143: .getSelectionForeground());
144: this .myPanel.setBackground(table
145: .getSelectionBackground());
146: } else {
147: this .setForeground(table.getForeground());
148: this .setBackground(table.getBackground());
149: this .myPanel.setForeground(table.getForeground());
150: this .myPanel.setBackground(table.getBackground());
151: }
152: if (hasFocus) { // NOI18N this scope block
153: this .myPanel
154: .setBorder(UIManager
155: .getBorder("Table.focusCellHighlightBorder"));
156: if (table.isCellEditable(row, column)) {
157: this .setForeground(UIManager
158: .getColor("Table.focusCellForeground"));
159: this .setBackground(UIManager
160: .getColor("Table.focusCellBackground"));
161: }
162: this .myPanel.setForeground(UIManager
163: .getColor("Table.focusCellForeground"));
164: this .myPanel.setBackground(UIManager
165: .getColor("Table.focusCellBackground"));
166: } else {
167: this .myPanel
168: .setBorder(MyBooleanRenderer.noFocusBorder);
169: }
170: this .setFocusable(true);
171: this .setSelected(true);
172: this .setToolTipText("");
173: }
174: this .setSelected((value != null && ((Boolean) value)
175: .booleanValue()));
176: return this .myPanel;
177: }
178:
179: /**
180: * Overrides <code>JComponent.setBackground</code> to assign the unselected-background
181: * color to the specified color.
182: *
183: * @param c set the background color to this value
184: */
185: public void setBackground(final Color c) {
186: super .setBackground(c);
187: }
188:
189: /**
190: * Overrides <code>JComponent.setForeground</code> to assign the unselected-foreground
191: * color to the specified color.
192: *
193: * @param c set the foreground color to this value
194: */
195: public void setForeground(final Color c) {
196: super .setForeground(c);
197: }
198: }
199:
200: class MyTColumnModel extends AbstractTableModel {
201: /**
202: *
203: */
204: private static final long serialVersionUID = 1L;
205:
206: private final String[] tcolumnNames = { "Select", "Column Name" };
207:
208: private List rowList;
209:
210: public MyTColumnModel(final List testList) {
211: this .rowList = new ArrayList();
212: for (int i = 0; i < testList.size(); i++) {
213: final RowDataWrapper rowData = new RowDataWrapper(
214: (DBColumn) testList.get(i));
215: this .rowList.add(rowData);
216: }
217: }
218:
219: /*
220: * JTable uses this method to determine the default renderer/ editor for each cell. If we
221: * didn't implement this method, then the last column would contain text ("true"/"false"),
222: * rather than a check box.
223: */
224: /**
225: *
226: */
227: public Class getColumnClass(final int c) {
228: return this .getValueAt(0, c).getClass();
229: }
230:
231: /**
232: *
233: */
234: public int getColumnCount() {
235: return this .tcolumnNames.length;
236: }
237:
238: /**
239: *
240: */
241: public String getColumnName(final int col) {
242: return this .tcolumnNames[col];
243: }
244:
245: /**
246: *
247: */
248: public int getRowCount() {
249: return this .rowList.size();
250: }
251:
252: /**
253: * @param row
254: * @return
255: */
256: public RowDataWrapper getRowDataWrapper(final int row) {
257: if (row < this .rowList.size()) {
258: return (RowDataWrapper) this .rowList.get(row);
259: }
260: return null;
261: }
262:
263: /**
264: * @return
265: */
266: public ArrayList getTColumns() {
267: final ArrayList columnList = new ArrayList();
268: for (int i = 0; i < this .rowList.size(); i++) {
269: final RowDataWrapper rowData = (RowDataWrapper) this .rowList
270: .get(i);
271: columnList.add(rowData.getTColumn());
272: }
273: return columnList;
274: }
275:
276: /**
277: *
278: */
279: public Object getValueAt(final int row, final int col) {
280: final RowDataWrapper rowData = (RowDataWrapper) this .rowList
281: .get(row);
282: switch (col) {
283: case 0:
284: return rowData.isSelected();
285: case 1:
286: return rowData.getTColumn();
287: }
288: return String.valueOf(col + "?");
289: }
290:
291: /*
292: * Don't need to implement this method unless your table's editable.
293: */
294: /**
295: *
296: */
297: public boolean isCellEditable(final int row, final int col) {
298: // Note that the data/cell address is constant,
299: // no matter where the cell appears onscreen.
300: final Object rowObj = this .rowList.get(row);
301: return rowObj != null ? ((RowDataWrapper) rowObj)
302: .isEditable().booleanValue()
303: && col == 0 : false;
304: }
305:
306: /**
307: * @param row
308: * @param col
309: * @param flag
310: */
311: public void setCellEditable(final int row, final int col,
312: final boolean flag) {
313: final Object rowObj = this .rowList.get(row);
314: if (rowObj != null) {
315: ((RowDataWrapper) rowObj)
316: .setEditable(flag ? Boolean.TRUE
317: : Boolean.FALSE);
318: }
319: }
320:
321: /*
322: * Don't need to implement this method unless your table's data can change.
323: */
324: /**
325: *
326: */
327: public void setValueAt(final Object value, final int row,
328: final int col) {
329: final RowDataWrapper rowData = (RowDataWrapper) this .rowList
330: .get(row);
331: switch (col) {
332: case 0:
333: rowData.setSelected((Boolean) value);
334: this .fireTableRowsUpdated(row, row);
335: break;
336: }
337: }
338: }
339:
340: static class MyTColumnModelCellRenderer extends
341: DefaultTableCellRenderer {
342: /**
343: *
344: */
345: private static final long serialVersionUID = 1L;
346: protected static Border noFocusBorder1 = BorderFactory
347: .createEmptyBorder(1, 1, 1, 1);
348:
349: /**
350: * Creates a default MyBooleanRenderer.
351: */
352: public MyTColumnModelCellRenderer() {
353: super ();
354: }
355:
356: /**
357: *
358: */
359: public Component getTableCellRendererComponent(
360: final JTable table, final Object value,
361: final boolean isSelected, final boolean hasFocus,
362: final int row, final int column) {
363: final JLabel renderer = (JLabel) super
364: .getTableCellRendererComponent(table, value,
365: isSelected, hasFocus, row, column);
366: final MyTColumnModel model = (MyTColumnModel) table
367: .getModel();
368: final RowDataWrapper rowDW = model.getRowDataWrapper(row);
369: if (rowDW != null && !rowDW.isEditable().booleanValue()) {
370: renderer.setEnabled(false);
371: renderer.setBackground(Color.lightGray);
372: final Object obj = rowDW.getTColumn();
373: final DBColumn st = (DBColumn) obj;
374: if (!st.isInsertSelected()) {
375: renderer.setToolTipText(NbBundle.getMessage(
376: JDBCWizardTablePanel.class,
377: "TOOLTIP_source_table_disabled_unselected",
378: rowDW.getTColumn()));
379: }
380: if (st.getName() != null) {
381: this .setText(st.getName());
382: }
383: renderer
384: .setBorder(MyTColumnModelCellRenderer.noFocusBorder1);
385: renderer.setFocusable(false);
386: } else {
387: if (isSelected) {
388: renderer.setForeground(table
389: .getSelectionForeground());
390: renderer.setBackground(table
391: .getSelectionBackground());
392: } else {
393: renderer.setForeground(table.getForeground());
394: renderer.setBackground(table.getBackground());
395: }
396: if (value instanceof DBColumn) {
397: final DBColumn dbModleTbl = (DBColumn) value;
398: if (dbModleTbl.getName() != null) {
399: this .setText(dbModleTbl.getName());
400: }
401: }
402: renderer.setToolTipText("");
403: renderer.setEnabled(true);
404: renderer.setFocusable(true);
405: }
406: return renderer;
407: }
408: }
409:
410: class RowDataWrapper {
411: private DBColumn tcolumn;
412:
413: /**
414: * @param mTColumn
415: */
416: public RowDataWrapper(final DBColumn mTColumn) {
417: this .tcolumn = mTColumn;
418: }
419:
420: /**
421: * @return
422: */
423: public Object getTColumn() {
424: return this .tcolumn;
425: }
426:
427: /**
428: * @return
429: */
430: public Boolean isEditable() {
431: return this .tcolumn.isInsertEditable() ? Boolean.TRUE
432: : Boolean.FALSE;
433: // return Boolean.TRUE;
434: }
435:
436: /**
437: * @return
438: */
439: public Boolean isSelected() {
440: return this .tcolumn.isInsertSelected() ? Boolean.TRUE
441: : Boolean.FALSE;
442: }
443:
444: /**
445: * @param isEditable
446: */
447: public void setEditable(final Boolean isEditable) {
448: this .tcolumn.setInsertEditable(isEditable.booleanValue());
449: }
450:
451: /**
452: * @param isSelected
453: */
454: public void setSelected(final Boolean isSelected) {
455: this .tcolumn.setInsertSelected(isSelected.booleanValue());
456: }
457: }
458:
459: private JPanel headerPnl;
460:
461: /* table to display meta data */
462: private MetaTColumnComponent metaDataTColumn;
463:
464: /* scrollpane for columns JTable */
465: private JScrollPane tableScroll;
466:
467: /**
468: * Creates a new instance of JDBCWizardTablePanel to render the selection of tables
469: * participating in an JDBC collaboration.
470: *
471: * @param testList List of tables
472: */
473: public InsertColumnPanel() {
474: final JPanel p = new JPanel();
475: p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
476: p.setOpaque(false);
477: this .headerPnl = new JPanel();
478: this .headerPnl.setLayout(new BorderLayout());
479: this .headerPnl.setOpaque(false);
480: this .headerPnl.add(p, BorderLayout.NORTH);
481: // addColumnTable(testList);
482: }
483:
484: /**
485: * Gets associated JTable.
486: *
487: * @return JTable
488: */
489: public JTable getColumnTable() {
490: return this .metaDataTColumn;
491: }
492:
493: /**
494: * Gets list of selected tables.
495: *
496: * @return List of selected tables
497: */
498: public List getColumnTables() {
499: final MyTColumnModel tcolumnModel = (MyTColumnModel) this .metaDataTColumn
500: .getModel();
501: return tcolumnModel.getTColumns();
502: }
503:
504: public List getSelectedColumnTables() {
505: final List allctabs = this .getColumnTables();
506: final List selctabs = new ArrayList();
507: for (int cnt = 0; cnt < allctabs.size(); cnt++) {
508: if (((DBColumn) allctabs.get(cnt)).isInsertSelected()) {
509: selctabs.add(allctabs.get(cnt));
510: }
511: }
512: return selctabs;
513: }
514:
515: /**
516: * Paints this component
517: *
518: * @param g graphics context
519: */
520: public void paint(final Graphics g) {
521: super .paint(g);
522: }
523:
524: /**
525: * Populates selected tables using items contained in the given List.
526: *
527: * @param tableNameList List of tables to use in repopulating set of selected tables
528: */
529: public void resetColumnTable(final List tcolumnNameList) {
530: final MyTColumnModel myMod = new MyTColumnModel(tcolumnNameList);
531: this .metaDataTColumn.setModel(myMod);
532: // set checkbox column size
533: final TableColumn column = this .metaDataTColumn
534: .getColumnModel().getColumn(0);
535: column.setResizable(true);
536: column.setMinWidth(40);
537: column.setPreferredWidth(80);
538: column.setMaxWidth(120);
539: }
540:
541: public void addColumnTable(final List testList) {
542: this .metaDataTColumn = new MetaTColumnComponent();
543: this .metaDataTColumn
544: .setFont(JDBCTableColumnDisplayable.FONT_TABLE_COLUMNS);
545: this .metaDataTColumn.getTableHeader().setFont(
546: JDBCTableColumnDisplayable.FONT_TABLE_HEADER);
547: final MyTColumnModel myModel = new MyTColumnModel(testList);
548: this .metaDataTColumn.setModel(myModel);
549: this .setLayout(new BorderLayout());
550: // add(headerPnl, BorderLayout.NORTH);
551: this .setPreferredSize(new Dimension(100, 100));
552: this .setMaximumSize(new Dimension(150, 150));
553: // set checkbox column size
554: final TableColumn column = this .metaDataTColumn
555: .getColumnModel().getColumn(0);
556: column.setResizable(true);
557: column.setMinWidth(40);
558: column.setPreferredWidth(80);
559: column.setMaxWidth(120);
560: this .tableScroll = new JScrollPane(this .metaDataTColumn);
561: final javax.swing.border.Border inside = BorderFactory
562: .createCompoundBorder(BorderFactory.createEmptyBorder(
563: 3, 3, 3, 3), BorderFactory
564: .createLineBorder(Color.GRAY));
565: this.tableScroll.setBorder(BorderFactory.createCompoundBorder(
566: BorderFactory.createEtchedBorder(), inside));
567: this.add(this.tableScroll, BorderLayout.CENTER);
568: }
569: }
|