001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.ui.view.join;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import javax.swing.event.TableModelEvent;
046: import javax.swing.event.TableModelListener;
047: import javax.swing.table.TableModel;
048: import org.netbeans.modules.sql.framework.model.DBColumn;
049: import org.netbeans.modules.sql.framework.model.SQLConnectableObject;
050: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
051: import org.netbeans.modules.sql.framework.ui.graph.IGraphNode;
052: import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
053: import org.netbeans.modules.sql.framework.ui.graph.IGraphView;
054: import org.netbeans.modules.sql.framework.ui.graph.impl.BasicCellArea;
055: import org.netbeans.modules.sql.framework.ui.graph.impl.ColumnArea;
056: import org.netbeans.modules.sql.framework.ui.graph.impl.PortArea;
057: import org.netbeans.modules.sql.framework.ui.graph.impl.TableArea;
058: import org.netbeans.modules.sql.framework.ui.graph.impl.TableConstants;
059: import org.netbeans.modules.sql.framework.ui.utils.UIUtil;
060: import org.netbeans.modules.sql.framework.ui.view.graph.MetaTableModel;
061: import org.netbeans.modules.sql.framework.ui.view.graph.SQLBasicTableArea;
062: import com.nwoods.jgo.JGoLink;
063: import com.nwoods.jgo.JGoListPosition;
064: import com.nwoods.jgo.JGoPort;
065: import com.nwoods.jgo.JGoText;
066: import com.sun.sql.framework.exception.BaseException;
067: import net.java.hulp.i18n.Logger;
068: import org.netbeans.modules.etl.logger.Localizer;
069: import org.netbeans.modules.etl.logger.LogUtil;
070: import org.netbeans.modules.sql.framework.model.DBTable;
071:
072: /**
073: *
074: * @author Ritesh Adval
075: * @version $Revision$
076: */
077: public class SQLTableArea extends TableArea implements
078: TableModelListener {
079:
080: /* log4j logger category */
081: private static final String LOG_CATEGORY = SQLTableArea.class
082: .getName();
083: private TableModel dataModel;
084: private static transient final Logger mLogger = LogUtil
085: .getLogger(SQLTableArea.class.getName());
086: private static transient final Localizer mLoc = Localizer.get();
087:
088: /**
089: * Creates a new instance of SQLTableArea
090: *
091: * @param tType type of the table
092: * @param table table
093: */
094: public SQLTableArea(int tType, DBTable table) {
095: super (tType);
096: MetaTableModel model = new MetaTableModel(table, tType);
097: setModel(model);
098: initialize(tType);
099:
100: if (columnAreas != null) {
101: Iterator it = columnAreas.iterator();
102: while (it.hasNext()) {
103: ColumnArea column = (ColumnArea) it.next();
104: switch (tType) {
105: case TableConstants.INPUT_TABLE:
106: column.setTextAlignment(JGoText.ALIGN_RIGHT);
107: break;
108: case TableConstants.OUTPUT_TABLE:
109: column.setTextAlignment(JGoText.ALIGN_LEFT);
110: break;
111: case TableConstants.NO_PORT_TABLE:
112: column.setTextAlignment(JGoText.ALIGN_LEFT);
113: break;
114: case TableConstants.INPUT_OUTPUT_TABLE:
115: default:
116: column.setTextAlignment(JGoText.ALIGN_CENTER);
117: break;
118: }
119: }
120: }
121: }
122:
123: /**
124: * Creates a new instance of SQLTableArea
125: *
126: * @param myType type of the table
127: * @param myDataModel associated table model
128: */
129: public SQLTableArea(int myType, TableModel myDataModel) {
130: super (myType);
131: setModel(myDataModel);
132: }
133:
134: public void setTableObject(DBTable table) {
135: removeAll();
136: initGui();
137: MetaTableModel model = new MetaTableModel(table, this
138: .getTableType());
139: setModel(model);
140: initialize(this .getTableType());
141: }
142:
143: /**
144: * set the table model
145: *
146: * @param dataModel table model
147: */
148: public void setModel(TableModel dataModel) {
149: removeAll();
150:
151: if (dataModel == null) {
152: throw new IllegalArgumentException(
153: "Cannot set a null TableModel");
154: }
155: if (this .dataModel != dataModel) {
156: TableModel old = this .dataModel;
157: if (old != null) {
158: old.removeTableModelListener(this );
159: }
160: this .dataModel = dataModel;
161: dataModel.addTableModelListener(this );
162: }
163: initializeTable(dataModel);
164: }
165:
166: /**
167: * get the table model
168: *
169: * @return table model
170: */
171: public TableModel getModel() {
172: return dataModel;
173: }
174:
175: private void initializeTable(TableModel dataMod) {
176: int columnCount = dataMod.getColumnCount();
177: int rowCount = dataMod.getRowCount();
178:
179: for (int i = 0; i < columnCount; i++) {
180: String columnName = dataMod.getColumnName(i);
181: addColumn(columnName);
182:
183: for (int j = 0; j < rowCount; j++) {
184: SQLDBColumn rowVal = (SQLDBColumn) dataMod.getValueAt(
185: j, i);
186: addItem(j, i, rowVal.getName(), UIUtil
187: .getColumnToolTip(rowVal));
188: }
189: }
190: }
191:
192: /**
193: * This fine grain notification tells listeners the exact range of cells, rows, or
194: * columns that changed.
195: *
196: * @param e table model event
197: */
198: public void tableChanged(TableModelEvent e) {
199: int firstRow = e.getFirstRow();
200: int lastRow = e.getLastRow();
201:
202: switch (e.getType()) {
203: case TableModelEvent.INSERT:
204: insertRow(firstRow, lastRow);
205: break;
206: case TableModelEvent.DELETE:
207: deleteRow(firstRow, lastRow);
208: break;
209: case TableModelEvent.UPDATE:
210: updateRow(firstRow, lastRow);
211: break;
212: }
213: }
214:
215: private void insertRow(int firstRow, int lastRow) {
216: int columnCount = dataModel.getColumnCount();
217:
218: for (int i = 0; i < columnCount; i++) {
219: for (int j = firstRow; j <= lastRow; j++) {
220: SQLDBColumn rowVal = (SQLDBColumn) dataModel
221: .getValueAt(j, i);
222: addItem(j, i, rowVal.getName(), UIUtil
223: .getColumnToolTip(rowVal));
224: }
225: }
226: }
227:
228: private void deleteRow(int firstRow, int lastRow) {
229: int columnCount = dataModel.getColumnCount();
230: deleteLinks(firstRow, lastRow);
231: for (int i = 0; i < columnCount; i++) {
232: for (int j = firstRow; j <= lastRow; j++) {
233: this .removeItem(j, i);
234: }
235: }
236: }
237:
238: private void deleteLinks(int firstRow, int lastRow) {
239: //before removing rows we need to find if port
240: //we are removing has any links and if so we should remove them
241: ArrayList<JGoLink> links = new ArrayList<JGoLink>();
242:
243: for (int k = firstRow; k <= lastRow; k++) {
244: IGraphPort port = this .getLeftGraphPort(k);
245: if (port != null) {
246: this .addLinks(port, links);
247: }
248:
249: port = this .getRightGraphPort(k);
250: if (port != null) {
251: this .addLinks(port, links);
252: }
253: }
254: //now delete the links
255: SQLBasicTableArea tableArea = (SQLBasicTableArea) this
256: .getParent();
257: if (tableArea != null) {
258: IGraphView view = tableArea.getGraphView();
259: if (view != null) {
260: view.deleteLinks(links);
261: }
262: }
263: }
264:
265: private void updateRow(int firstRow, int lastRow) {
266: int columnCount = dataModel.getColumnCount();
267:
268: for (int i = 0; i < columnCount; i++) {
269: ColumnArea cArea = this .getColumnArea(i);
270: if (cArea == null) {
271: continue;
272: }
273:
274: for (int j = firstRow; j <= lastRow; j++) {
275: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
276: BasicCellArea cell = cArea.getCellAt(j);
277: if (cell != null) {
278: cell.setText(rowVal.getName());
279: }
280: }
281: }
282: }
283:
284: public boolean isColumnMapped(SQLDBColumn column) {
285: int columnCount = dataModel.getColumnCount();
286: int rowCount = dataModel.getRowCount();
287:
288: for (int i = 0; i < columnCount; i++) {
289: for (int j = 0; j < rowCount; j++) {
290: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
291: if (rowVal.equals(column)) {
292: PortArea p = this .rightPortArea.getPortAreaAt(j);
293: IGraphPort rightPort = p.getGraphPort();
294: if (rightPort != null) {
295: JGoPort port = (JGoPort) rightPort;
296: if (port.getNumLinks() > 0) {
297: return true;
298: }
299: }
300: }
301: }
302: }
303:
304: return false;
305: }
306:
307: public void makeColumnVisible(SQLDBColumn column) {
308: int columnCount = dataModel.getColumnCount();
309: int rowCount = dataModel.getRowCount();
310:
311: for (int i = 0; i < columnCount; i++) {
312: for (int j = 0; j < rowCount; j++) {
313: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
314: if (rowVal.equals(column)) {
315: //add cell
316: this .addItem(j, i, column.getName(), column
317: .toString());
318: }
319: }
320: }
321: }
322:
323: public void makeColumnInVisible(SQLDBColumn column)
324: throws BaseException {
325: try {
326: //first try to remove column refs
327: removeColumnReference(column);
328: } catch (BaseException ex) {
329: mLogger
330: .errorNoloc(
331: mLoc
332: .t(
333: "PRSR193: Error making column invisible, unable to remove column references.{0}",
334: column.getName()), ex);
335: throw ex;
336: }
337:
338: int columnCount = dataModel.getColumnCount();
339: int rowCount = dataModel.getRowCount();
340:
341: for (int i = 0; i < columnCount; i++) {
342: for (int j = 0; j < rowCount; j++) {
343: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
344: if (rowVal.equals(column)) {
345: ColumnArea cArea = this .getColumnArea(i);
346: //remove cell
347: cArea.removeItem(j);
348:
349: //remove port
350: this .rightPortArea.removePort(j);
351: }
352: }
353: }
354: }
355:
356: public void removeColumnReference(SQLDBColumn column)
357: throws BaseException {
358: int columnCount = dataModel.getColumnCount();
359: int rowCount = dataModel.getRowCount();
360:
361: for (int i = 0; i < columnCount; i++) {
362: for (int j = 0; j < rowCount; j++) {
363: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
364: if (rowVal.equals(column)) {
365: //remove column reference
366: PortArea p = this .rightPortArea.getPortAreaAt(j);
367: IGraphPort rightPort = p.getGraphPort();
368: if (rightPort != null) {
369: JGoPort port = (JGoPort) rightPort;
370: removeColumnFromExpressionObject(port, column);
371: return;
372: }
373: }
374: }
375: }
376: }
377:
378: private void removeColumnFromExpressionObject(JGoPort port,
379: SQLDBColumn column) throws BaseException {
380: JGoListPosition pos = port.getFirstLinkPos();
381: while (pos != null) {
382: JGoLink link = port.getLinkAtPos(pos);
383: pos = port.getNextLinkPos(pos);
384: JGoPort toPort = link.getToPort();
385: IGraphPort toGraphPort = (IGraphPort) toPort;
386:
387: IGraphNode node = toGraphPort.getDataNode();
388: String argName = node.getFieldName(toGraphPort);
389:
390: Object dataObj = node.getDataObject();
391: if (dataObj != null
392: && dataObj instanceof SQLConnectableObject) {
393: SQLConnectableObject expObj = (SQLConnectableObject) dataObj;
394: expObj.removeInputByArgName(argName, column);
395: }
396: }
397: }
398: }
|