001: package net.sourceforge.squirrel_sql.plugins.dataimport.gui;
002:
003: /*
004: * Copyright (C) 2007 Thorsten Mürell
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: import java.util.Vector;
022:
023: import javax.swing.table.AbstractTableModel;
024:
025: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
026: import net.sourceforge.squirrel_sql.fw.util.StringManager;
027: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
028:
029: /**
030: * This is a table model for the column mapping table at the bottom
031: * of the file import dialog.
032: *
033: * @author Thorsten Mürell
034: */
035: public class ColumnMappingTableModel extends AbstractTableModel {
036: private static final long serialVersionUID = -7080889957246771971L;
037:
038: private static final StringManager stringMgr = StringManagerFactory
039: .getStringManager(ColumnMappingTableModel.class);
040:
041: private TableColumnInfo[] columns = null;
042: private Vector<String> mapping = new Vector<String>();
043: private Vector<String> defaults = new Vector<String>();
044:
045: /**
046: * The default constructor.
047: *
048: * @param columns These are the columns of the destination table
049: */
050: public ColumnMappingTableModel(TableColumnInfo[] columns) {
051: this .columns = columns;
052: for (int i = 0; i < columns.length; i++) {
053: mapping.add(SpecialColumnMapping.SKIP.getVisibleString());
054: defaults.add("");
055: }
056: }
057:
058: /*
059: * (non-Javadoc)
060: * @see javax.swing.table.TableModel#getColumnCount()
061: */
062: public int getColumnCount() {
063: return 3;
064: }
065:
066: /* (non-Javadoc)
067: * @see javax.swing.table.TableModel#getRowCount()
068: */
069: public int getRowCount() {
070: return columns.length;
071: }
072:
073: /* (non-Javadoc)
074: * @see javax.swing.table.TableModel#getValueAt(int, int)
075: */
076: public Object getValueAt(int rowIndex, int columnIndex) {
077: if (columnIndex == 0) {
078: return columns[rowIndex].getColumnName();
079: } else if (columnIndex == 1) {
080: return mapping.get(rowIndex);
081: } else if (columnIndex == 2) {
082: return defaults.get(rowIndex);
083: }
084: return null;
085: }
086:
087: /* (non-Javadoc)
088: * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
089: */
090: @Override
091: public boolean isCellEditable(int rowIndex, int columnIndex) {
092: if (columnIndex == 1 || columnIndex == 2) {
093: return true;
094: }
095: return false;
096: }
097:
098: /* (non-Javadoc)
099: * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
100: */
101: @Override
102: public void setValueAt(Object value, int row, int col) {
103: if (col == 1) {
104: mapping.set(row, value.toString());
105: } else if (col == 2) {
106: defaults.set(row, value.toString());
107: }
108: fireTableCellUpdated(row, col);
109: }
110:
111: /* (non-Javadoc)
112: * @see javax.swing.table.AbstractTableModel#getColumnName(int)
113: */
114: @Override
115: public String getColumnName(int column) {
116: if (column == 0) {
117: // i18n[ImportFileDialog.tableColumn=Table column]
118: return stringMgr.getString("ImportFileDialog.tableColumn");
119: } else if (column == 1) {
120: // i18n[ImportFileDialog.importFileColumn=Import file column]
121: return stringMgr
122: .getString("ImportFileDialog.importFileColumn");
123: } else if (column == 2) {
124: // i18n[ImportFileDialog.fixedValue=Fixed value]
125: return stringMgr.getString("ImportFileDialog.fixedValue");
126: }
127: return null;
128: }
129:
130: /**
131: * Returns the index of the column with the given name.
132: *
133: * @param columnName The column name
134: * @return The row of the given column
135: */
136: public int findTableColumn(String columnName) {
137: int i = 0;
138: for (i = 0; i < columns.length; i++) {
139: if (columnName.equals(columns[i].getColumnName()))
140: return i;
141: }
142: return -1;
143: }
144:
145: /**
146: * Resets the column mappings to "Skip"
147: */
148: public void resetMappings() {
149: mapping.clear();
150: defaults.clear();
151: for (int i = 0; i < columns.length; i++) {
152: mapping.add(SpecialColumnMapping.SKIP.getVisibleString());
153: defaults.add("");
154: }
155: }
156: }
|