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.graph;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045:
046: import javax.swing.event.TableModelEvent;
047: import javax.swing.event.TableModelListener;
048: import javax.swing.table.TableModel;
049:
050: import org.netbeans.modules.sql.framework.model.DBColumn;
051: import org.netbeans.modules.sql.framework.model.SQLConnectableObject;
052: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
053: import org.netbeans.modules.sql.framework.ui.graph.IGraphNode;
054: import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
055: import org.netbeans.modules.sql.framework.ui.graph.IGraphView;
056: import org.netbeans.modules.sql.framework.ui.graph.impl.BasicCellArea;
057: import org.netbeans.modules.sql.framework.ui.graph.impl.ColumnArea;
058: import org.netbeans.modules.sql.framework.ui.graph.impl.PortArea;
059: import org.netbeans.modules.sql.framework.ui.graph.impl.TableArea;
060: import org.netbeans.modules.sql.framework.ui.graph.impl.TableConstants;
061: import org.netbeans.modules.sql.framework.ui.utils.UIUtil;
062:
063: import com.nwoods.jgo.JGoLink;
064: import com.nwoods.jgo.JGoListPosition;
065: import com.nwoods.jgo.JGoPort;
066: import com.nwoods.jgo.JGoText;
067: import com.sun.sql.framework.exception.BaseException;
068: import net.java.hulp.i18n.Logger;
069: import org.netbeans.modules.etl.logger.Localizer;
070: import org.netbeans.modules.etl.logger.LogUtil;
071: import org.netbeans.modules.sql.framework.model.DBTable;
072:
073: /**
074: *
075: * @author Ritesh Adval
076: * @version $Revision$
077: */
078: public class SQLTableArea extends TableArea implements
079: TableModelListener {
080:
081: /* log4j logger category */
082: private static final String LOG_CATEGORY = SQLTableArea.class
083: .getName();
084: private static transient final Logger mLogger = LogUtil
085: .getLogger(SQLTableArea.class.getName());
086: private static transient final Localizer mLoc = Localizer.get();
087: private TableModel dataModel;
088:
089: /**
090: * Creates a new instance of SQLTableArea
091: *
092: * @param tType type of the table
093: * @param table table
094: */
095: public SQLTableArea(int tType, DBTable table) {
096: super (tType);
097: MetaTableModel model = new MetaTableModel(table, tType);
098: setModel(model);
099: initialize(tType);
100:
101: if (columnAreas != null) {
102: Iterator it = columnAreas.iterator();
103: while (it.hasNext()) {
104: ColumnArea column = (ColumnArea) it.next();
105: switch (tType) {
106: case TableConstants.INPUT_TABLE:
107: column.setTextAlignment(JGoText.ALIGN_RIGHT);
108: break;
109:
110: case TableConstants.OUTPUT_TABLE:
111: column.setTextAlignment(JGoText.ALIGN_LEFT);
112: break;
113:
114: case TableConstants.NO_PORT_TABLE:
115: column.setTextAlignment(JGoText.ALIGN_LEFT);
116: break;
117:
118: case TableConstants.INPUT_OUTPUT_TABLE:
119: default:
120: column.setTextAlignment(JGoText.ALIGN_CENTER);
121: break;
122: }
123: }
124: }
125: }
126:
127: /**
128: * Creates a new instance of SQLTableArea
129: *
130: * @param myType type of the table
131: * @param myDataModel associated table model
132: */
133: public SQLTableArea(int myType, TableModel myDataModel) {
134: super (myType);
135: setModel(myDataModel);
136: }
137:
138: public void setTableObject(DBTable table) {
139: removeAll();
140: initGui();
141: MetaTableModel model = new MetaTableModel(table, this
142: .getTableType());
143: setModel(model);
144: initialize(this .getTableType());
145: }
146:
147: /**
148: * set the table model
149: *
150: * @param dataModel table model
151: */
152: public void setModel(TableModel dataModel) {
153: removeAll();
154:
155: if (dataModel == null) {
156: throw new IllegalArgumentException(
157: "Cannot set a null TableModel");
158: }
159: if (this .dataModel != dataModel) {
160: TableModel old = this .dataModel;
161: if (old != null) {
162: old.removeTableModelListener(this );
163: }
164: this .dataModel = dataModel;
165: dataModel.addTableModelListener(this );
166: }
167: initializeTable(dataModel);
168: }
169:
170: /**
171: * get the table model
172: *
173: * @return table model
174: */
175: public TableModel getModel() {
176: return dataModel;
177: }
178:
179: private void initializeTable(TableModel dataMod) {
180: int columnCount = dataMod.getColumnCount();
181: int rowCount = dataMod.getRowCount();
182:
183: for (int i = 0; i < columnCount; i++) {
184: String columnName = dataMod.getColumnName(i);
185: addColumn(columnName);
186:
187: for (int j = 0; j < rowCount; j++) {
188: SQLDBColumn rowVal = (SQLDBColumn) dataMod.getValueAt(
189: j, i);
190: addItem(j, i, rowVal.getName(), UIUtil
191: .getColumnToolTip(rowVal));
192: }
193: }
194: }
195:
196: /**
197: * This fine grain notification tells listeners the exact range of cells, rows, or
198: * columns that changed.
199: *
200: * @param e table model event
201: */
202: public void tableChanged(TableModelEvent e) {
203: int firstRow = e.getFirstRow();
204: int lastRow = e.getLastRow();
205:
206: switch (e.getType()) {
207: case TableModelEvent.INSERT:
208: insertRow(firstRow, lastRow);
209: break;
210: case TableModelEvent.DELETE:
211: deleteRow(firstRow, lastRow);
212: break;
213: case TableModelEvent.UPDATE:
214: updateRow(firstRow, lastRow);
215: break;
216: }
217: }
218:
219: private void insertRow(int firstRow, int lastRow) {
220: int columnCount = dataModel.getColumnCount();
221:
222: for (int i = 0; i < columnCount; i++) {
223: for (int j = firstRow; j <= lastRow; j++) {
224: SQLDBColumn rowVal = (SQLDBColumn) dataModel
225: .getValueAt(j, i);
226: addItem(j, i, rowVal.getName(), UIUtil
227: .getColumnToolTip(rowVal));
228: }
229: }
230:
231: }
232:
233: private void deleteRow(int firstRow, int lastRow) {
234: int columnCount = dataModel.getColumnCount();
235: deleteLinks(firstRow, lastRow);
236: for (int i = 0; i < columnCount; i++) {
237: for (int j = firstRow; j <= lastRow; j++) {
238:
239: this .removeItem(j, i);
240:
241: }
242: }
243: }
244:
245: @SuppressWarnings("unchecked")
246: private void deleteLinks(int firstRow, int lastRow) {
247: //before removing rows we need to find if port
248: //we are removing has any links and if so we should remove them
249: ArrayList links = new ArrayList();
250:
251: for (int k = firstRow; k <= lastRow; k++) {
252: IGraphPort port = this .getLeftGraphPort(k);
253: if (port != null) {
254: this .addLinks(port, links);
255: }
256:
257: port = this .getRightGraphPort(k);
258: if (port != null) {
259: this .addLinks(port, links);
260: }
261: }
262: //now delete the links
263: SQLBasicTableArea tableArea = (SQLBasicTableArea) this
264: .getParent();
265: if (tableArea != null) {
266: IGraphView view = tableArea.getGraphView();
267: if (view != null) {
268: view.deleteLinks(links);
269: }
270: }
271: }
272:
273: private void updateRow(int firstRow, int lastRow) {
274: int columnCount = dataModel.getColumnCount();
275:
276: for (int i = 0; i < columnCount; i++) {
277: ColumnArea cArea = this .getColumnArea(i);
278: if (cArea == null) {
279: continue;
280: }
281:
282: for (int j = firstRow; j <= lastRow; j++) {
283: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
284: BasicCellArea cell = cArea.getCellAt(j);
285: if (cell != null) {
286: cell.setText(rowVal.getName());
287: cell.setToolTipText(UIUtil
288: .getColumnToolTip((SQLDBColumn) rowVal));
289: }
290: }
291: }
292: }
293:
294: public boolean isColumnMapped(SQLDBColumn column) {
295: int columnCount = dataModel.getColumnCount();
296: int rowCount = dataModel.getRowCount();
297:
298: for (int i = 0; i < columnCount; i++) {
299: for (int j = 0; j < rowCount; j++) {
300: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
301: if (rowVal.equals(column)) {
302: PortArea p = this .rightPortArea.getPortAreaAt(j);
303: IGraphPort rightPort = p.getGraphPort();
304: if (rightPort != null) {
305: JGoPort port = (JGoPort) rightPort;
306: if (port.getNumLinks() > 0) {
307: return true;
308: }
309: }
310: }
311: }
312: }
313:
314: return false;
315: }
316:
317: public void makeColumnVisible(SQLDBColumn column) {
318: int columnCount = dataModel.getColumnCount();
319: int rowCount = dataModel.getRowCount();
320:
321: for (int i = 0; i < columnCount; i++) {
322: for (int j = 0; j < rowCount; j++) {
323: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
324: if (rowVal.equals(column)) {
325: //add cell
326: this .addItem(j, i, column.getName(), column
327: .toString());
328: }
329: }
330: }
331: }
332:
333: public void makeColumnInVisible(SQLDBColumn column)
334: throws BaseException {
335: try {
336: //first try to remove column refs
337: removeColumnReference(column);
338: } catch (BaseException ex) {
339: mLogger
340: .errorNoloc(
341: mLoc
342: .t(
343: "PRSR178: Error making column invisible, unable to remove column references{0}",
344: column.getName()), ex);
345: throw ex;
346:
347: }
348:
349: int columnCount = dataModel.getColumnCount();
350: int rowCount = dataModel.getRowCount();
351:
352: for (int i = 0; i < columnCount; i++) {
353: for (int j = 0; j < rowCount; j++) {
354: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
355: if (rowVal.equals(column)) {
356: ColumnArea cArea = this .getColumnArea(i);
357: //remove cell
358: cArea.removeItem(j);
359:
360: //remove port
361: this .rightPortArea.removePort(j);
362: }
363: }
364: }
365: }
366:
367: public void removeColumnReference(SQLDBColumn column)
368: throws BaseException {
369: int columnCount = dataModel.getColumnCount();
370: int rowCount = dataModel.getRowCount();
371:
372: for (int i = 0; i < columnCount; i++) {
373: for (int j = 0; j < rowCount; j++) {
374: DBColumn rowVal = (DBColumn) dataModel.getValueAt(j, i);
375: if (rowVal.equals(column)) {
376: //remove column reference
377: PortArea p = this .rightPortArea.getPortAreaAt(j);
378: IGraphPort rightPort = p.getGraphPort();
379: if (rightPort != null) {
380: JGoPort port = (JGoPort) rightPort;
381: removeColumnFromExpressionObject(port, column);
382: return;
383: }
384: }
385: }
386: }
387: }
388:
389: private void removeColumnFromExpressionObject(JGoPort port,
390: SQLDBColumn column) throws BaseException {
391: JGoListPosition pos = port.getFirstLinkPos();
392: while (pos != null) {
393: JGoLink link = port.getLinkAtPos(pos);
394: pos = port.getNextLinkPos(pos);
395: JGoPort toPort = link.getToPort();
396: IGraphPort toGraphPort = (IGraphPort) toPort;
397:
398: IGraphNode node = toGraphPort.getDataNode();
399: String argName = node.getFieldName(toGraphPort);
400:
401: Object dataObj = node.getDataObject();
402: if (dataObj != null
403: && dataObj instanceof SQLConnectableObject) {
404: SQLConnectableObject expObj = (SQLConnectableObject) dataObj;
405: expObj.removeInputByArgName(argName, column);
406: }
407: }
408: }
409: }
|