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;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.beans.PropertyEditorSupport;
046: import java.util.ArrayList;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.StringTokenizer;
050:
051: import org.netbeans.modules.sql.framework.model.SQLGroupBy;
052: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
053: import org.netbeans.modules.sql.framework.model.SourceTable;
054: import org.netbeans.modules.sql.framework.model.TargetTable;
055: import org.netbeans.modules.sql.framework.ui.editor.property.IProperty;
056: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertyEditor;
057: import org.openide.DialogDisplayer;
058: import org.openide.NotifyDescriptor;
059: import net.java.hulp.i18n.Logger;
060: import org.netbeans.modules.etl.logger.Localizer;
061: import org.netbeans.modules.etl.logger.LogUtil;
062:
063: /**
064: * @author Ritesh Adval
065: * @version $Revision$
066: */
067: public class GroupByPropertyEditor extends PropertyEditorSupport
068: implements IPropertyEditor {
069:
070: private static final String LOG_CATEGORY = GroupByPropertyEditor.class
071: .getName();
072: private IProperty property;
073: private static transient final Logger mLogger = LogUtil
074: .getLogger(GroupByPropertyEditor.class.getName());
075: private static transient final Localizer mLoc = Localizer.get();
076: private TargetTable targetTable;
077: IGraphViewContainer editor;
078:
079: public GroupByPropertyEditor(IGraphViewContainer editor,
080: TargetTable tTable) {
081: this .targetTable = tTable;
082: this .editor = editor;
083: }
084:
085: public String getAsText() {
086: StringBuilder text = new StringBuilder(50);
087: SQLGroupBy groupBy = (SQLGroupBy) this .getValue();
088: if (groupBy != null) {
089: List columns = groupBy.getColumns();
090: Iterator it = columns.iterator();
091: while (it.hasNext()) {
092: Object colObj = it.next();
093: text.append(colObj.toString());
094: if (it.hasNext()) {
095: text.append(",");
096: }
097: }
098: }
099: return text.toString();
100: }
101:
102: public Component getCustomEditor() {
103: List groupByColumns = new ArrayList();
104: if (this .getValue() != null) {
105: groupByColumns = ((SQLGroupBy) this .getValue())
106: .getColumns();
107: }
108: GroupByView view = new GroupByView(editor, targetTable,
109: groupByColumns);
110: view.setPreferredSize(new Dimension(350, 390));
111: return view;
112: }
113:
114: public IProperty getProperty() {
115: return property;
116: }
117:
118: /**
119: * Sets the property value by parsing a given String. May raise
120: * java.lang.IllegalArgumentException if either the String is badly formatted or if
121: * this kind of property can't be expressed as text.
122: *
123: * @param text The string to be parsed.
124: */
125: public void setAsText(String text) {
126: if (!text.equals(this .getAsText())) {
127: List allColumns = new ArrayList();
128: try {
129: List srcTables = targetTable.getSourceTableList();
130: for (Iterator iter = srcTables.iterator(); iter
131: .hasNext();) {
132: allColumns.addAll(((SourceTable) iter.next())
133: .getColumnList());
134: }
135: } catch (Exception e) {
136: // ignore
137: }
138: allColumns.addAll(this .targetTable.getColumnList());
139:
140: ArrayList columns = new ArrayList();
141: StringTokenizer tok = new StringTokenizer(text, ",");
142: while (tok.hasMoreTokens()) {
143: String token = tok.nextToken();
144: Object column = getExistingColumn(allColumns, token);
145: if (column != null) {
146: columns.add(column);
147: } else {
148: warnForInValidColumn(token);
149: return;
150: }
151: }
152:
153: this .setValue(SQLModelObjectFactory.getInstance()
154: .createGroupBy(columns, targetTable));
155: try {
156: if (this .property != null) {
157: this .property.setValue(this .getValue());
158: }
159: } catch (Exception ex) {
160: mLogger
161: .errorNoloc(
162: mLoc
163: .t(
164: "PRSR199: Error occured in setting the property value for Group By{0}from joinview table.",
165: text), ex);
166: }
167: }
168: }
169:
170: public void setProperty(IProperty property) {
171: this .property = property;
172: }
173:
174: public boolean supportsCustomEditor() {
175: return true;
176: }
177:
178: private Object getExistingColumn(List allColumns, String columnName) {
179: Iterator it = allColumns.iterator();
180: while (it.hasNext()) {
181: Object column = it.next();
182: if (columnName.equalsIgnoreCase(column.toString())) {
183: return column;
184: }
185: }
186: return null;
187: }
188:
189: private void warnForInValidColumn(String columnName) {
190: DialogDisplayer
191: .getDefault()
192: .notify(
193: new NotifyDescriptor.Message(
194: "The column "
195: + columnName
196: + " is invalid, please specify a valid column name.",
197: NotifyDescriptor.WARNING_MESSAGE));
198: }
199: }
|