0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041: package org.netbeans.modules.etl.ui.view;
0042:
0043: import java.awt.BorderLayout;
0044: import java.awt.Component;
0045: import java.awt.Dialog;
0046: import java.awt.FlowLayout;
0047: import java.awt.event.ActionEvent;
0048: import java.awt.event.ActionListener;
0049: import java.sql.Types;
0050: import java.util.ArrayList;
0051: import java.util.Iterator;
0052: import java.util.List;
0053: import java.util.Vector;
0054:
0055: import javax.swing.BorderFactory;
0056: import javax.swing.DefaultCellEditor;
0057: import javax.swing.InputVerifier;
0058: import javax.swing.JButton;
0059: import javax.swing.JComboBox;
0060: import javax.swing.JComponent;
0061: import javax.swing.JPanel;
0062: import javax.swing.JScrollPane;
0063: import javax.swing.JTable;
0064: import javax.swing.JTextField;
0065: import javax.swing.table.AbstractTableModel;
0066: import javax.swing.table.DefaultTableCellRenderer;
0067: import javax.swing.table.TableColumn;
0068:
0069: import org.netbeans.modules.etl.ui.DataObjectProvider;
0070: import org.netbeans.modules.etl.ui.model.impl.ETLCollaborationModel;
0071: import org.netbeans.modules.sql.framework.model.DBColumn;
0072: import org.netbeans.modules.sql.framework.common.jdbc.SQLUtils;
0073: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
0074: import org.netbeans.modules.sql.framework.model.RuntimeOutput;
0075: import org.netbeans.modules.sql.framework.model.SQLConstants;
0076: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
0077: import org.netbeans.modules.sql.framework.model.SQLDBTable;
0078: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
0079: import org.netbeans.modules.sql.framework.model.SourceColumn;
0080: import org.netbeans.modules.sql.framework.model.TargetColumn;
0081: import org.netbeans.modules.sql.framework.model.TargetTable;
0082: import org.netbeans.modules.sql.framework.model.utils.SQLObjectUtil;
0083: import org.netbeans.modules.sql.framework.ui.view.graph.SQLBasicTableArea;
0084: import org.openide.DialogDescriptor;
0085: import org.openide.DialogDisplayer;
0086: import org.openide.NotifyDescriptor;
0087:
0088: import com.sun.sql.framework.exception.BaseException;
0089: import net.java.hulp.i18n.Logger;
0090: import org.netbeans.modules.etl.logger.Localizer;
0091: import org.netbeans.modules.etl.logger.LogUtil;
0092: import org.netbeans.modules.sql.framework.model.DatabaseModel;
0093:
0094: /**
0095: * This class is used to define columns for input and output table
0096: *
0097: * @author Ritesh Adval
0098: * @version $Revision$
0099: */
0100: public class TablePanel extends JPanel {
0101: private static transient final Logger mLogger = LogUtil
0102: .getLogger(TablePanel.class.getName());
0103: private static transient final Localizer mLoc = Localizer.get();
0104:
0105: class ActionAdapter implements ActionListener {
0106:
0107: /**
0108: * Invoked when an action occurs.
0109: */
0110: public void actionPerformed(ActionEvent e) {
0111: String actionCmd = e.getActionCommand();
0112: if (actionCmd.equals("Add")) {
0113: ((TableColumnModel) tbl.getModel()).addEmptyRow();
0114: } else if (actionCmd.equals("Remove")) {
0115: int[] rows = tbl.getSelectedRows();
0116: TableColumnModel tModel = (TableColumnModel) tbl
0117: .getModel();
0118:
0119: ArrayList columns = new ArrayList();
0120: for (int i = 0; i < rows.length; i++) {
0121: if (tModel.isCellEditable(rows[i], 0)) {
0122: columns.add(tModel.getColumns().get(rows[i]));
0123: } else {
0124: DialogDisplayer
0125: .getDefault()
0126: .notify(
0127: new NotifyDescriptor.Message(
0128: "Remove of Flat File Location Runtime argument is not allowed",
0129: NotifyDescriptor.WARNING_MESSAGE));
0130: }
0131: }
0132:
0133: // now delete columns
0134: for (int i = 0; i < columns.size(); i++) {
0135: ((TableColumnModel) tbl.getModel())
0136: .removeRow((ColumnWrapper) columns.get(i));
0137: }
0138: }
0139: }
0140: }
0141:
0142: class ColumnWrapper {
0143: private SQLDBColumn column;
0144: private String columnOldName;
0145: private boolean isNew = false;
0146:
0147: ColumnWrapper(SQLDBColumn column) {
0148: this .column = column;
0149: this .columnOldName = column.getName();
0150: }
0151:
0152: public SQLDBColumn getColumn() {
0153: return column;
0154: }
0155:
0156: public String getColumnOldName() {
0157: return this .columnOldName;
0158: }
0159:
0160: public String getDefaultValue() {
0161: return column.getDefaultValue();
0162: }
0163:
0164: public int getJdbcType() {
0165: return column.getJdbcType();
0166: }
0167:
0168: public String getJdbcTypeString() {
0169: return column.getJdbcTypeString();
0170: }
0171:
0172: public String getName() {
0173: return column.getName();
0174: }
0175:
0176: public int getPrecision() {
0177: return column.getPrecision();
0178: }
0179:
0180: public int getScale() {
0181: return column.getScale();
0182: }
0183:
0184: public boolean isNew() {
0185: return isNew;
0186: }
0187:
0188: public void setDefaultValue(String defaultVal) {
0189: column.setDefaultValue(defaultVal);
0190: }
0191:
0192: public void setJdbcType(int newType) {
0193: column.setJdbcType(newType);
0194: }
0195:
0196: public void setName(String name) {
0197: column.setName(name);
0198: column.setDisplayName(name);
0199: }
0200:
0201: public void setNew(boolean isNew) {
0202: this .isNew = isNew;
0203: }
0204:
0205: public void setPrecision(int precision) {
0206: column.setPrecision(precision);
0207: }
0208:
0209: public void setScale(int scale) {
0210: column.setScale(scale);
0211: }
0212: }
0213:
0214: class IntegerInputVerifier extends InputVerifier {
0215:
0216: /**
0217: * Checks whether the JComponent's input is valid. This method should have no side
0218: * effects. It returns a boolean indicating the status of the argument's input.
0219: *
0220: * @param input the JComponent to verify
0221: * @return <code>true</code> when valid, <code>false</code> when invalid
0222: * @see javax.swing.JComponent#setInputVerifier
0223: * @see javax.swing.JComponent#getInputVerifier
0224: */
0225: public boolean verify(JComponent input) {
0226: JTextField tf = (JTextField) input;
0227: String text = tf.getText();
0228: try {
0229: Integer.parseInt(text.trim());
0230: } catch (NumberFormatException e) {
0231: showMessage("Please Enter an Integer Value");
0232: return false;
0233: }
0234: return true;
0235: }
0236: }
0237:
0238: class TableColumnModel extends AbstractTableModel {
0239: // private AbstractDBTable table;
0240: // local list for table column
0241: private List columnList = new ArrayList();
0242:
0243: TableColumnModel(SQLDBTable table) {
0244: // this.table = table;
0245: Iterator it = table.getColumnList().iterator();
0246: while (it.hasNext()) {
0247: SQLDBColumn column = (SQLDBColumn) it.next();
0248: ColumnWrapper colWrapper = new ColumnWrapper(column);
0249: columnList.add(colWrapper);
0250: }
0251:
0252: // add default runtime outputs
0253: if (table.getObjectType() == SQLConstants.RUNTIME_OUTPUT) {
0254: addDefaultRuntimeOutput();
0255: } else if (table.getObjectType() == SQLConstants.RUNTIME_INPUT) {
0256: addFlatFileTableRuntimeArgument();
0257: }
0258:
0259: // if there are no columns then add one empty row
0260: if (columnList.size() == 0) {
0261: addEmptyRow();
0262: }
0263: }
0264:
0265: public void addEmptyRow() {
0266: boolean added = false;
0267:
0268: if (table.getObjectType() == SQLConstants.RUNTIME_INPUT) {
0269: SourceColumn column = SQLModelObjectFactory
0270: .getInstance().createSourceColumn(
0271: generateUniqueColumnName(),
0272: Types.VARCHAR, 0, 0, true);
0273: ColumnWrapper colWrapper = new ColumnWrapper(column);
0274: columnList.add(colWrapper);
0275: added = true;
0276: } else if (table.getObjectType() == SQLConstants.RUNTIME_OUTPUT) {
0277: TargetColumn column = SQLModelObjectFactory
0278: .getInstance().createTargetColumn(
0279: generateUniqueColumnName(),
0280: Types.VARCHAR, 0, 0, true);
0281: ColumnWrapper colWrapper = new ColumnWrapper(column);
0282: columnList.add(colWrapper);
0283: added = true;
0284: }
0285:
0286: if (added) {
0287: fireTableRowsInserted(columnList.size(), columnList
0288: .size());
0289: }
0290: }
0291:
0292: /**
0293: * Returns the number of columns in the model. A <code>JTable</code> uses this
0294: * method to determine how many columns it should create and display by default.
0295: *
0296: * @return the number of columns in the model
0297: * @see #getRowCount
0298: */
0299: public int getColumnCount() {
0300: return headers.length;
0301: }
0302:
0303: /**
0304: * @see javax.swing.table.AbstractTableModel#getColumnName
0305: */
0306: public String getColumnName(int col) {
0307: return headers[col];
0308: }
0309:
0310: public List getColumns() {
0311: return columnList;
0312: }
0313:
0314: public ColumnWrapper getColumnWrapper(String columnName) {
0315: Iterator it = columnList.iterator();
0316:
0317: while (it.hasNext()) {
0318: ColumnWrapper colWrapper = (ColumnWrapper) it.next();
0319: if (colWrapper.getColumnOldName().equals(columnName)) {
0320: return colWrapper;
0321: }
0322: }
0323: return null;
0324: }
0325:
0326: /**
0327: * Returns the number of rows in the model. A <code>JTable</code> uses this
0328: * method to determine how many rows it should display. This method should be
0329: * quick, as it is called frequently during rendering.
0330: *
0331: * @return the number of rows in the model
0332: * @see #getColumnCount
0333: */
0334: public int getRowCount() {
0335: return columnList.size();
0336: }
0337:
0338: /**
0339: * Returns the value for the cell at <code>columnIndex</code> and
0340: * <code>rowIndex</code>.
0341: *
0342: * @param rowIndex the row whose value is to be queried
0343: * @param columnIndex the column whose value is to be queried
0344: * @return the value Object at the specified cell
0345: */
0346: public Object getValueAt(int rowIndex, int columnIndex) {
0347: ColumnWrapper colWrapper = (ColumnWrapper) columnList
0348: .get(rowIndex);
0349: Object retValue = null;
0350:
0351: switch (columnIndex) {
0352: case 0:
0353: retValue = colWrapper.getName();
0354: break;
0355: case 1:
0356: retValue = colWrapper.getDefaultValue();
0357: break;
0358: case 2:
0359: retValue = colWrapper.getJdbcTypeString();
0360: break;
0361: case 3:
0362: retValue = new Integer(colWrapper.getPrecision());
0363: break;
0364: case 4:
0365: retValue = new Integer(colWrapper.getScale());
0366: break;
0367: }
0368:
0369: return retValue;
0370: }
0371:
0372: /**
0373: * Indicates whether given cell is editable.
0374: *
0375: * @param row row number of cell
0376: * @param col column number of cell
0377: * @return true if all cells are editable, false otherwise
0378: */
0379: public boolean isCellEditable(int row, int col) {
0380: ColumnWrapper colWrapper = (ColumnWrapper) columnList
0381: .get(row);
0382: SQLDBColumn column = colWrapper.getColumn();
0383:
0384: // we do not allow name to be edited
0385: if (col == 0) {
0386: return column.isEditable();
0387: }
0388: return true;
0389: }
0390:
0391: public void removeRow(ColumnWrapper colWrapper) {
0392: int idx = columnList.indexOf(colWrapper);
0393: if (idx != -1) {
0394: columnList.remove(idx);
0395: fireTableRowsDeleted(idx, idx);
0396: // removeRow(idx);
0397: }
0398: }
0399:
0400: public void removeRow(int row) {
0401: Iterator it = columnList.iterator();
0402: int cnt = 0;
0403:
0404: while (it.hasNext()) {
0405: if (cnt == row) {
0406: it.remove();
0407: fireTableRowsDeleted(row, row);
0408: }
0409: cnt++;
0410: }
0411:
0412: // if all rows are removed then add one empty one
0413: if (columnList.size() == 0) {
0414: addEmptyRow();
0415: }
0416: }
0417:
0418: /**
0419: * Sets the specified cell to the desired value
0420: *
0421: * @param value desired value of the cell
0422: * @param row row number
0423: * @param col column number
0424: */
0425: public void setValueAt(Object value, int row, int col) {
0426:
0427: ColumnWrapper colWrapper = (ColumnWrapper) columnList
0428: .get(row);
0429:
0430: int val = 0;
0431: boolean set = false;
0432:
0433: switch (col) {
0434: case 0:
0435: String name = (String) value;
0436:
0437: // if column name exist show message to user
0438: if (!name.equals(colWrapper.getName())
0439: && isArgNameExist(name)) {
0440: TablePanel.showMessage("Column " + name
0441: + " already exist");
0442: return;
0443: }
0444: // set the new name in column
0445: colWrapper.setName((String) value);
0446: set = true;
0447: break;
0448: case 1:
0449: colWrapper.setDefaultValue((String) value);
0450: set = true;
0451: break;
0452: case 2:
0453: colWrapper
0454: .setJdbcType(org.netbeans.modules.sql.framework.common.jdbc.SQLUtils
0455: .getStdJdbcType((String) value));
0456: set = true;
0457: break;
0458: case 3:
0459: try {
0460: val = Integer.parseInt((String) value);
0461: } catch (NumberFormatException e) {
0462: showMessage("Please Enter an Integer Value");
0463: return;
0464: }
0465:
0466: colWrapper.setPrecision(val);
0467: set = true;
0468: break;
0469: case 4:
0470: try {
0471: val = Integer.parseInt((String) value);
0472: } catch (NumberFormatException e) {
0473: showMessage("Please Enter an Integer Value");
0474: return;
0475: }
0476:
0477: colWrapper.setScale(val);
0478: set = true;
0479: }
0480:
0481: if (set) {
0482: fireTableCellUpdated(row, col);
0483: }
0484: }
0485:
0486: private void addDefaultRuntimeOutput() {
0487: if (collabModel != null) {
0488: List targetDBModel = collabModel
0489: .getTargetDatabaseModels();
0490: Iterator it = targetDBModel.iterator();
0491: while (it.hasNext()) {
0492: DatabaseModel tModel = (DatabaseModel) it.next();
0493: addTargetTableRuntimeArg(tModel);
0494: }
0495: }
0496:
0497: // add STATUS
0498: SQLDBColumn column = SQLModelObjectFactory.getInstance()
0499: .createTargetColumn("STATUS", Types.VARCHAR, 0, 0,
0500: true);
0501: column.setEditable(false);
0502: ColumnWrapper colWrapper = new ColumnWrapper(column);
0503:
0504: // if runtime output does not contain this then add
0505: if (!table.getColumns().containsKey(column.getName())) {
0506: columnList.add(colWrapper);
0507: fireTableRowsInserted(columnList.size(), columnList
0508: .size());
0509: }
0510:
0511: // add STARTTIME
0512: column = SQLModelObjectFactory.getInstance()
0513: .createTargetColumn("STARTTIME", Types.TIMESTAMP,
0514: 0, 0, true);
0515: column.setEditable(false);
0516: colWrapper = new ColumnWrapper(column);
0517:
0518: // if runtime output does not contain this then add
0519: if (!table.getColumns().containsKey(column.getName())) {
0520: columnList.add(colWrapper);
0521: fireTableRowsInserted(columnList.size(), columnList
0522: .size());
0523: }
0524:
0525: // add ENDTIME
0526: column = SQLModelObjectFactory.getInstance()
0527: .createTargetColumn("ENDTIME", Types.TIMESTAMP, 0,
0528: 0, true);
0529: column.setEditable(false);
0530: colWrapper = new ColumnWrapper(column);
0531:
0532: // if runtime output does not contain this then add
0533: if (!table.getColumns().containsKey(column.getName())) {
0534: columnList.add(colWrapper);
0535: fireTableRowsInserted(columnList.size(), columnList
0536: .size());
0537: }
0538: }
0539:
0540: private void addFlatFileTableRuntimeArgument() {
0541:
0542: List sTables = collabModel.getSQLDefinition()
0543: .getSourceTables();
0544: createFlatFileRuntimeArgument(sTables);
0545:
0546: List tTables = collabModel.getSQLDefinition()
0547: .getTargetTables();
0548: createFlatFileRuntimeArgument(tTables);
0549: }
0550:
0551: private void addTargetTableRuntimeArg(
0552: DatabaseModel targetDbModel) {
0553: Iterator it = targetDbModel.getTables().iterator();
0554: while (it.hasNext()) {
0555: TargetTable tt = (TargetTable) it.next();
0556: addTargetTableRuntimeArg(tt);
0557: }
0558: }
0559:
0560: // TODO: Unify creation of per-table count runtime argument for use in
0561: // RuntimeHandler as well.
0562: private void addTargetTableRuntimeArg(TargetTable targetTable) {
0563: // add Count_<Target_Table_Name> runtime output argument
0564: String argName = SQLObjectUtil
0565: .getTargetTableCountRuntimeOutput(targetTable);
0566:
0567: RuntimeOutput rOutput = ((RuntimeOutput) table);
0568: DBColumn column = rOutput.getColumn(argName);
0569: if (column == null) {
0570: TargetColumn tColumn = SQLModelObjectFactory
0571: .getInstance().createTargetColumn(argName,
0572: Types.INTEGER, 0, 0, true);
0573: tColumn.setEditable(false);
0574:
0575: ColumnWrapper colWrapper = new ColumnWrapper(tColumn);
0576: columnList.add(colWrapper);
0577: fireTableRowsInserted(columnList.size(), columnList
0578: .size());
0579: }
0580: }
0581:
0582: private void createFlatFileRuntimeArgument(List tables) {
0583: Iterator it = tables.iterator();
0584:
0585: while (it.hasNext()) {
0586: SQLDBTable table1 = (SQLDBTable) it.next();
0587: String argName = table1
0588: .getFlatFileLocationRuntimeInputName();
0589: if (argName != null && !isArgNameExist(argName)) {
0590: SQLDBColumn column = SQLObjectUtil
0591: .createRuntimeInputArg(table1, argName);
0592: ColumnWrapper colWrapper = new ColumnWrapper(column);
0593:
0594: // if runtime output does not contain this then add
0595: if (!table1.getColumns().containsKey(
0596: column.getName())) {
0597: columnList.add(colWrapper);
0598:
0599: fireTableRowsInserted(columnList.size(),
0600: columnList.size());
0601: }
0602: }
0603: }
0604: }
0605:
0606: private String generateUniqueColumnName() {
0607: int cnt = 0;
0608: String cName = "arg_" + cnt;
0609: while (isArgNameExist(cName)) {
0610: cName = "arg_" + cnt++;
0611: }
0612: return cName;
0613: }
0614:
0615: private boolean isArgNameExist(String name) {
0616: Iterator it = columnList.iterator();
0617: while (it.hasNext()) {
0618: ColumnWrapper colWrapper = (ColumnWrapper) it.next();
0619: SQLDBColumn column = colWrapper.getColumn();
0620: if (name.equals(column.getName())) {
0621: return true;
0622: }
0623: }
0624:
0625: return false;
0626: }
0627:
0628: }
0629:
0630: private static void showMessage(String msg) {
0631: DialogDisplayer.getDefault().notify(
0632: new NotifyDescriptor.Message(msg,
0633: NotifyDescriptor.WARNING_MESSAGE));
0634: }
0635:
0636: private boolean firstCreation = false;
0637:
0638: private String[] headers = new String[] { "Argument Name",
0639: "Default Value", "SQL Type", "Precision/Length", "Scale" };
0640: private SQLDBTable initialTable;
0641: private SQLDBTable table;
0642:
0643: private int tableType;
0644: private JTable tbl;
0645: private ETLCollaborationModel collabModel;
0646:
0647: /** Creates a new instance of TableColumnPanel */
0648: public TablePanel(int tableType) {
0649: this .tableType = tableType;
0650: String tableName = "";
0651:
0652: collabModel = DataObjectProvider.getProvider()
0653: .getActiveDataObject().getModel();
0654: if (collabModel == null) {
0655: return;
0656: }
0657: RuntimeDatabaseModel runtimeDbModel = collabModel
0658: .getRuntimeDbModel();
0659:
0660: if (tableType == SQLConstants.RUNTIME_INPUT) {
0661: if (runtimeDbModel != null
0662: && runtimeDbModel.getRuntimeInput() != null) {
0663: initialTable = runtimeDbModel.getRuntimeInput();
0664: table = SQLModelObjectFactory.getInstance()
0665: .createRuntimeInput(
0666: runtimeDbModel.getRuntimeInput());
0667:
0668: } else {
0669: table = SQLModelObjectFactory.getInstance()
0670: .createRuntimeInput();
0671: initialTable = table;
0672: firstCreation = true;
0673: }
0674: tableName += ":RuntimeInput";
0675: } else {
0676: if (runtimeDbModel != null
0677: && runtimeDbModel.getRuntimeOutput() != null) {
0678: initialTable = runtimeDbModel.getRuntimeOutput();
0679: table = SQLModelObjectFactory.getInstance()
0680: .createRuntimeOutput(
0681: runtimeDbModel.getRuntimeOutput());
0682:
0683: } else {
0684: table = SQLModelObjectFactory.getInstance()
0685: .createRuntimeOutput();
0686: initialTable = table;
0687: firstCreation = true;
0688: }
0689: tableName += ":RuntimeOutput";
0690: }
0691:
0692: table.setDisplayName(tableName);
0693: initGui();
0694: }
0695:
0696: /** Creates a new instance of TableColumnPanel */
0697: public TablePanel(int tableType, ETLCollaborationModel colmodel) {
0698: this .tableType = tableType;
0699: String tableName = "";
0700:
0701: collabModel = colmodel;
0702:
0703: if (collabModel == null) {
0704: return;
0705: }
0706: RuntimeDatabaseModel runtimeDbModel = collabModel
0707: .getRuntimeDbModel();
0708:
0709: if (tableType == SQLConstants.RUNTIME_INPUT) {
0710: if (runtimeDbModel != null
0711: && runtimeDbModel.getRuntimeInput() != null) {
0712: initialTable = runtimeDbModel.getRuntimeInput();
0713: table = SQLModelObjectFactory.getInstance()
0714: .createRuntimeInput(
0715: runtimeDbModel.getRuntimeInput());
0716:
0717: } else {
0718: table = SQLModelObjectFactory.getInstance()
0719: .createRuntimeInput();
0720: initialTable = table;
0721: firstCreation = true;
0722: }
0723: tableName += ":RuntimeInput";
0724: } else {
0725: if (runtimeDbModel != null
0726: && runtimeDbModel.getRuntimeOutput() != null) {
0727: initialTable = runtimeDbModel.getRuntimeOutput();
0728: table = SQLModelObjectFactory.getInstance()
0729: .createRuntimeOutput(
0730: runtimeDbModel.getRuntimeOutput());
0731:
0732: } else {
0733: table = SQLModelObjectFactory.getInstance()
0734: .createRuntimeOutput();
0735: initialTable = table;
0736: firstCreation = true;
0737: }
0738: tableName += ":RuntimeOutput";
0739: }
0740:
0741: table.setDisplayName(tableName);
0742: initGui();
0743: }
0744:
0745: public void showTablePanel() {
0746: String title = "";
0747: if (this .tableType == SQLConstants.RUNTIME_INPUT) {
0748: title = "Add Input Runtime Arguments";
0749: } else {
0750: title = "Add Output Runtime Arguments";
0751: }
0752:
0753: DialogDescriptor dd = new DialogDescriptor(this , title);
0754:
0755: Dialog dlg = DialogDisplayer.getDefault().createDialog(dd);
0756: dlg
0757: .getAccessibleContext()
0758: .setAccessibleDescription(
0759: "This is the dialog to add Input/Output Runtime arguments");
0760: dlg.setSize(450, 400);
0761: dlg.setVisible(true);
0762:
0763: if (NotifyDescriptor.OK_OPTION.equals(dd.getValue())) {
0764: commitTableEditingValue();
0765:
0766: if (collabModel != null && firstCreation) {
0767: try {
0768: addFirstTimeRuntimeArgs();
0769: collabModel.addObject(initialTable);
0770: } catch (com.sun.sql.framework.exception.BaseException ex) {
0771: showMessage("Can not create Runtime"
0772: + ex.getMessage());
0773: }
0774: // update the table already on canvas
0775: } else {
0776: try {
0777: if (initialTable.getObjectType() == SQLConstants.RUNTIME_INPUT) {
0778: checkForDanglingReference();
0779: } else {
0780: updateColumnValues();
0781: }
0782: } catch (com.sun.sql.framework.exception.BaseException ex) {
0783: showMessage("Can not update Runtime"
0784: + ex.getMessage());
0785: }
0786: }
0787: collabModel.setDirty(true);
0788: }
0789: }
0790:
0791: private void addFirstTimeRuntimeArgs() {
0792: TableColumnModel tModel = (TableColumnModel) tbl.getModel();
0793: List columns = tModel.getColumns();
0794: Iterator it = columns.iterator();
0795: int colId = 1;
0796:
0797: while (it.hasNext()) {
0798: ColumnWrapper colWrapper = (ColumnWrapper) it.next();
0799: SQLDBColumn column = colWrapper.getColumn();
0800: column.setOrdinalPosition(colId++);
0801: initialTable.addColumn(column);
0802: }
0803: }
0804:
0805: private void addNewRuntimeArgs() {
0806: ArrayList newColumnList = new ArrayList();
0807:
0808: TableColumnModel tModel = (TableColumnModel) tbl.getModel();
0809: List columns = tModel.getColumns();
0810: Iterator it = columns.iterator();
0811: int colId = 1;
0812:
0813: while (it.hasNext()) {
0814: ColumnWrapper colWrapper = (ColumnWrapper) it.next();
0815: SQLDBColumn column = colWrapper.getColumn();
0816: column.setOrdinalPosition(colId++);
0817: if (initialTable.getColumn(column.getName()) == null
0818: && initialTable.getColumn(colWrapper
0819: .getColumnOldName()) == null) {
0820: newColumnList.add(column);
0821: initialTable.addColumn(column);
0822: }
0823: }
0824: ETLCollaborationTopPanel etlView = null;
0825: try {
0826: etlView = DataObjectProvider.getProvider()
0827: .getActiveDataObject().getETLEditorTopPanel();
0828: } catch (Exception ex) {
0829: // ignore
0830: }
0831: if (etlView != null) {
0832: SQLBasicTableArea node = (SQLBasicTableArea) etlView
0833: .findGraphNode(initialTable);
0834:
0835: if (node != null) {
0836: node.addColumns(newColumnList);
0837: }
0838: }
0839:
0840: }
0841:
0842: // This method checks for dangling references of any runtime arguments which are
0843: // deleted
0844: private void checkForDanglingReference() throws BaseException {
0845: List colList = initialTable.getColumnList();
0846: Iterator it = colList.iterator();
0847: if (collabModel == null) {
0848: // TODO log this
0849: return;
0850: }
0851: ETLCollaborationTopPanel etlView = null;
0852: try {
0853: etlView = DataObjectProvider.getProvider()
0854: .getActiveDataObject().getETLEditorTopPanel();
0855: } catch (Exception ex) {
0856: // ignore
0857: }
0858: // delete columns from table gui
0859: SQLBasicTableArea tableArea = null;
0860: if (etlView != null) {
0861: tableArea = (SQLBasicTableArea) etlView
0862: .findGraphNode(initialTable);
0863: }
0864:
0865: // maintain a list of columns which were there in initialTable and
0866: // no nolonger is available in newTable meaning they are deleted and so we need
0867: // to check if any filter has reference to these deleted columns
0868: ArrayList deletedColumnList = new ArrayList();
0869:
0870: TableColumnModel tModel = (TableColumnModel) tbl.getModel();
0871:
0872: while (it.hasNext()) {
0873: SourceColumn column = (SourceColumn) it.next();
0874: // check if initialTable column also exist in newTable
0875: // if exist then delete this column from newTable and add the column from
0876: // initialTable
0877: ColumnWrapper newColumn = tModel.getColumnWrapper(column
0878: .getName());
0879: if (newColumn != null) {
0880: // delete column key (if column name has changed then key will be
0881: // dangling)and add new key
0882: initialTable.deleteColumn(column.getName());
0883:
0884: // column may be updated so set new value
0885: column.copyFrom(newColumn.getColumn());
0886:
0887: // add column again which is updated
0888: initialTable.addColumn(column);
0889:
0890: // update in gui
0891: if (tableArea != null) {
0892: tableArea.updateColumn(column);
0893: }
0894:
0895: } else {
0896: // newTable does not have column so we need to see if it is been
0897: // referenced elsewhere
0898: deletedColumnList.add(column);
0899: }
0900: }
0901:
0902: // now go throw the list of deleted columns and
0903: // check if it is referenced elsewhere
0904: it = deletedColumnList.iterator();
0905: while (it.hasNext()) {
0906: SourceColumn column = (SourceColumn) it.next();
0907: collabModel.removeDanglingColumnReference(column);
0908: initialTable.deleteColumn(column.getName());
0909: }
0910:
0911: if (tableArea != null) {
0912: tableArea.removeColumns(deletedColumnList);
0913: }
0914:
0915: // add new columns in table gui
0916: addNewRuntimeArgs();
0917:
0918: }
0919:
0920: // this is to commit the last edited value
0921: // if user press ok and has focus on a cell which he is
0922: // editing then this value needs to be commited
0923: // its a focus problem solution
0924: private void commitTableEditingValue() {
0925: Component editor = tbl.getEditorComponent();
0926:
0927: if (editor != null && editor instanceof JTextField) {
0928: JTextField textEditor = (JTextField) editor;
0929: String val = textEditor.getText();
0930:
0931: int row = tbl.getEditingRow();
0932: int col = tbl.getEditingColumn();
0933: if (row != -1 && col != -1) {
0934: tbl.getModel().setValueAt(val, row, col);
0935: }
0936: }
0937: }
0938:
0939: private void initGui() {
0940: // set layout
0941:
0942: this .setLayout(new BorderLayout());
0943: // create a panel to hold jtable at CENTER of this component
0944: JPanel tableColumnPanel = new JPanel();
0945: tableColumnPanel.setLayout(new BorderLayout());
0946: tableColumnPanel.setBorder(BorderFactory.createCompoundBorder(
0947: BorderFactory.createTitledBorder("Define Columns"),
0948: BorderFactory.createEmptyBorder(4, 4, 4, 4)));
0949:
0950: // create a panel to hold add remove button
0951: JPanel tableButtonPanel = new JPanel();
0952: FlowLayout fLayout = new FlowLayout();
0953: fLayout.setAlignment(FlowLayout.LEFT);
0954: tableButtonPanel.setLayout(fLayout);
0955:
0956: ActionAdapter aAdapter = new ActionAdapter();
0957:
0958: String nbBundle1 = mLoc.t("PRSR001: Add");
0959: JButton addColumnButton = new JButton(Localizer
0960: .parse(nbBundle1));
0961: addColumnButton.getAccessibleContext().setAccessibleName(
0962: Localizer.parse(nbBundle1));
0963: addColumnButton.setActionCommand("Add");
0964: addColumnButton.setMnemonic(Localizer.parse(nbBundle1)
0965: .charAt(0));
0966: addColumnButton.addActionListener(aAdapter);
0967: String nbBundle2 = mLoc.t("PRSR001: Remove");
0968: JButton removeColumnButton = new JButton(Localizer
0969: .parse(nbBundle2));
0970: removeColumnButton.getAccessibleContext().setAccessibleName(
0971: Localizer.parse(nbBundle2));
0972: removeColumnButton.setActionCommand("Remove");
0973: removeColumnButton.setMnemonic(Localizer.parse(nbBundle2)
0974: .charAt(0));
0975: removeColumnButton.addActionListener(aAdapter);
0976:
0977: tableButtonPanel.add(addColumnButton);
0978: tableButtonPanel.add(removeColumnButton);
0979:
0980: if (table.getObjectType() == SQLConstants.RUNTIME_INPUT) {
0981: // add add remove button panel to column panel
0982: tableColumnPanel.add(tableButtonPanel, BorderLayout.NORTH);
0983: }
0984:
0985: // create a table
0986: tbl = new JTable();
0987: JScrollPane sPane = new JScrollPane(tbl);
0988: tbl.setModel(new TableColumnModel(table));
0989: // set up table column editors and renderers
0990: setColumnRendererAndEditor();
0991:
0992: // set SQL Type column size
0993: TableColumn column = tbl.getColumnModel().getColumn(2);
0994: column.setMinWidth(40);
0995: column.setMaxWidth(100);
0996: column.setPreferredWidth(90);
0997:
0998: // set precision column size
0999: column = tbl.getColumnModel().getColumn(3);
1000: column.setMinWidth(40);
1001: column.setMaxWidth(100);
1002: column.setPreferredWidth(60);
1003:
1004: // set scale column size
1005: column = tbl.getColumnModel().getColumn(4);
1006: column.setMinWidth(40);
1007: column.setMaxWidth(100);
1008: column.setPreferredWidth(40);
1009:
1010: // add table to table panel
1011: tableColumnPanel.add(sPane, BorderLayout.CENTER);
1012:
1013: // add table panel to CENTER in this component
1014: this .add(tableColumnPanel, BorderLayout.CENTER);
1015: }
1016:
1017: private void setColumnRendererAndEditor() {
1018: JTextField editField = new JTextField();
1019: TableColumn tblCol;
1020: DefaultTableCellRenderer renderer;
1021:
1022: // for column name
1023: tblCol = tbl.getColumnModel().getColumn(0);
1024: tblCol.setCellEditor(new DefaultCellEditor(editField));
1025:
1026: renderer = new DefaultTableCellRenderer();
1027: renderer.setToolTipText("Click To Enter Column Name");
1028: tblCol.setCellRenderer(renderer);
1029:
1030: // for default column value
1031: tblCol = tbl.getColumnModel().getColumn(1);
1032: tblCol.setCellEditor(new DefaultCellEditor(editField));
1033:
1034: renderer = new DefaultTableCellRenderer();
1035: renderer.setToolTipText("Click To Enter Default Column Value");
1036: tblCol.setCellRenderer(renderer);
1037:
1038: // for sql type column
1039: tblCol = tbl.getColumnModel().getColumn(2);
1040: tblCol.setCellEditor(new DefaultCellEditor(new JComboBox(
1041: new Vector(SQLUtils.getSupportedLiteralTypes()))));
1042:
1043: renderer = new DefaultTableCellRenderer();
1044: renderer.setToolTipText("Click To Select Sql Type");
1045: tblCol.setCellRenderer(renderer);
1046:
1047: // for precision / length column
1048: tblCol = tbl.getColumnModel().getColumn(3);
1049: tblCol.setCellEditor(new DefaultCellEditor(editField));
1050:
1051: renderer = new DefaultTableCellRenderer();
1052: renderer.setToolTipText("Click To Enter Precision/Length");
1053: tblCol.setCellRenderer(renderer);
1054:
1055: // for scale column
1056: tblCol = tbl.getColumnModel().getColumn(4);
1057: tblCol.setCellEditor(new DefaultCellEditor(editField));
1058:
1059: renderer = new DefaultTableCellRenderer();
1060: renderer.setToolTipText("Click To Enter Scale");
1061: tblCol.setCellRenderer(renderer);
1062: }
1063:
1064: // update columns for runtimeout
1065: // runtime put attributes are editable except name,
1066: // also user can not add or remove them
1067: private void updateColumnValues() {
1068: List colList = initialTable.getColumnList();
1069: Iterator it = colList.iterator();
1070: TableColumnModel tModel = (TableColumnModel) tbl.getModel();
1071:
1072: while (it.hasNext()) {
1073: TargetColumn column = (TargetColumn) it.next();
1074:
1075: ColumnWrapper newColumn = tModel.getColumnWrapper(column
1076: .getName());
1077: if (newColumn != null) {
1078: // column may be updated so set new value
1079: column.copyFrom(newColumn.getColumn());
1080: }
1081: }
1082: }
1083: }
|