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.beans.PropertyChangeListener;
045: import java.beans.PropertyChangeSupport;
046: import java.beans.PropertyEditorSupport;
047: import java.util.List;
048:
049: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
050: import org.netbeans.modules.sql.framework.model.RuntimeInput;
051: import org.netbeans.modules.sql.framework.model.SQLCondition;
052: import org.netbeans.modules.sql.framework.model.SQLConstants;
053: import org.netbeans.modules.sql.framework.model.SQLDBTable;
054: import org.netbeans.modules.sql.framework.model.SQLDefinition;
055: import org.netbeans.modules.sql.framework.model.SQLObject;
056: import org.netbeans.modules.sql.framework.model.SQLPredicate;
057: import org.netbeans.modules.sql.framework.model.SourceTable;
058: import org.netbeans.modules.sql.framework.model.TargetTable;
059: import org.netbeans.modules.sql.framework.model.utils.ConditionUtil;
060: import org.netbeans.modules.sql.framework.model.utils.SQLObjectUtil;
061: import org.netbeans.modules.sql.framework.ui.editor.property.IProperty;
062: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertyEditor;
063: import org.netbeans.modules.sql.framework.ui.view.conditionbuilder.ConditionBuilderUtil;
064: import org.netbeans.modules.sql.framework.ui.view.conditionbuilder.ConditionBuilderView;
065: import org.openide.DialogDisplayer;
066: import org.openide.NotifyDescriptor;
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.SQLJoinOperator;
071:
072: /**
073: * @author Ritesh Adval
074: * @author Jonathan Giron
075: * @version $Revision$
076: */
077: public class ConditionPropertyEditor extends PropertyEditorSupport
078: implements IPropertyEditor {
079:
080: private static transient final Logger mLogger = LogUtil
081: .getLogger(ConditionPropertyEditor.class.getName());
082: private static transient final Localizer mLoc = Localizer.get();
083:
084: public static class Validation extends ConditionPropertyEditor {
085:
086: public Validation(IGraphViewContainer editor, SQLDBTable table) {
087: super (editor, table);
088: }
089:
090: protected Component getCustomEditorForSource() {
091: return ConditionBuilderUtil
092: .getValidationConditionBuilderView(
093: (SourceTable) table, editor);
094: }
095:
096: protected Component getCustomEditorForTarget() {
097: throw new UnsupportedOperationException(
098: "Validation Condition is not supported for target table");
099: }
100: }
101:
102: public static class OuterFilter extends ConditionPropertyEditor {
103:
104: public OuterFilter(IGraphViewContainer editor, SQLDBTable table) {
105: super (editor, table);
106: }
107:
108: protected Component getCustomEditorForSource() {
109: throw new UnsupportedOperationException(
110: "Validation Condition is not supported for target table");
111: }
112:
113: protected Component getCustomEditorForTarget() {
114: return ConditionBuilderUtil.getFilterConditionBuilderView(
115: (TargetTable) table, editor);
116: }
117: }
118:
119: public static class JoinConditionEditor extends
120: ConditionPropertyEditor {
121:
122: private SQLJoinOperator joinOp;
123:
124: public JoinConditionEditor(IGraphViewContainer editor,
125: SQLJoinOperator joinOp) {
126: super (editor, null);
127: this .joinOp = joinOp;
128: }
129:
130: public Component getCustomEditor() {
131: return ConditionBuilderUtil.getConditionBuilderView(joinOp,
132: editor);
133: }
134:
135: protected Component getCustomEditorForSource() {
136: throw new UnsupportedOperationException("not supported ");
137: }
138:
139: protected Component getCustomEditorForTarget() {
140: throw new UnsupportedOperationException("not supported ");
141: }
142: }
143:
144: /* log4j logger category */
145: private static final String LOG_CATEGORY = ConditionPropertyEditor.class
146: .getName();
147: protected ConditionBuilderView cView;
148: protected IGraphViewContainer editor;
149: protected SQLDBTable table;
150: private SQLCondition conditionContainer;
151: private PropertyChangeSupport iPropertyChange = new PropertyChangeSupport(
152: this );
153: private IProperty property;
154:
155: /** Creates a new instance of ConditionPropertyEditor */
156: public ConditionPropertyEditor(IGraphViewContainer editor,
157: SQLDBTable table) {
158: this .editor = editor;
159: this .table = table;
160: }
161:
162: /**
163: * Register a listener for the PropertyChange event. The class will fire a
164: * PropertyChange value whenever the value is updated.
165: *
166: * @param listener An object to be invoked when a PropertyChange event is fired.
167: */
168: public synchronized void addPropertyChangeListener(
169: PropertyChangeListener listener) {
170: iPropertyChange.addPropertyChangeListener(listener);
171: }
172:
173: public String getAsText() {
174: String sql = "";
175: String text = conditionContainer != null ? conditionContainer
176: .getConditionText() : null;
177:
178: return text != null ? text : sql;
179: }
180:
181: public Component getCustomEditor() {
182: if (table.getObjectType() == SQLConstants.TARGET_TABLE) {
183: return getCustomEditorForTarget();
184: }
185: return getCustomEditorForSource();
186: }
187:
188: public String getJavaInitializationString() {
189: return super .getJavaInitializationString();
190: }
191:
192: public IProperty getProperty() {
193: return property;
194: }
195:
196: public Object getValue() {
197: return this .conditionContainer;
198: }
199:
200: /**
201: * Remove a listener for the PropertyChange event.
202: *
203: * @param listener The PropertyChange listener to be removed.
204: */
205: public synchronized void removePropertyChangeListener(
206: PropertyChangeListener listener) {
207: iPropertyChange.removePropertyChangeListener(listener);
208: }
209:
210: /**
211: * Sets the property value by parsing a given String. May raise
212: * java.lang.IllegalArgumentException if either the String is badly formatted or if
213: * this kind of property can't be expressed as text.
214: *
215: * @param text The string to be parsed.
216: */
217: public void setAsText(String text) {
218: String oldText = this .conditionContainer.getConditionText();
219: if (this .conditionContainer != null && text != null
220: && !text.equals(oldText)) {
221: try {
222: this .conditionContainer = (SQLCondition) conditionContainer
223: .cloneSQLObject();
224: } catch (CloneNotSupportedException ex) {
225: mLogger.errorNoloc(mLoc.t(
226: "PRSR196: error cloning the condition {0}",
227: LOG_CATEGORY), ex);
228: return;
229: }
230:
231: this .conditionContainer.setConditionText(text);
232: SQLDefinition def = SQLObjectUtil
233: .getAncestralSQLDefinition((SQLObject) conditionContainer
234: .getParent());
235: try {
236: SQLObject obj = ConditionUtil.parseCondition(text, def);
237: conditionContainer.removeAllObjects();
238: ConditionUtil
239: .populateCondition(conditionContainer, obj);
240: // if we do not get a predicate then the condition is invalid
241: // and if text is not empty string
242: if (!(obj instanceof SQLPredicate)
243: && !text.trim().equals("")) {
244: warnForInvalidCondition();
245: }
246: } catch (Exception ex) {
247: mLogger
248: .errorNoloc(
249: mLoc
250: .t(
251: "PRSR197: Error finding root predicate from text condition{0}",
252: text), ex);
253: warnForInvalidCondition();
254: }
255:
256: // if user modified text then change the gui mode
257: this .conditionContainer
258: .setGuiMode(SQLCondition.GUIMODE_SQLCODE);
259: try {
260: if (this .property != null) {
261: this .property.setValue(this .conditionContainer);
262: }
263: } catch (Exception ex) {
264: mLogger
265: .errorNoloc(
266: mLoc
267: .t(
268: "PRSR198: Error occurred in setting the property value for condition{0}from joinview table.",
269: text), ex);
270: }
271: }
272: }
273:
274: public void setProperty(IProperty property) {
275: this .property = property;
276: }
277:
278: public void setValue(Object value) {
279: this .conditionContainer = (SQLCondition) value;
280: }
281:
282: public boolean supportsCustomEditor() {
283: return true;
284: }
285:
286: protected void addRuntimeInput(List tables) {
287: // add the runtime arguments also.
288: RuntimeInput rInput = getRuntimeInput();
289: if (rInput != null) {
290: tables.add(rInput);
291: }
292: }
293:
294: protected Component getCustomEditorForSource() {
295: return ConditionBuilderUtil.getConditionBuilderView(
296: (SourceTable) table, editor);
297: }
298:
299: protected Component getCustomEditorForTarget() {
300: return ConditionBuilderUtil.getJoinConditionBuilderView(
301: (TargetTable) table, editor);
302: }
303:
304: protected RuntimeInput getRuntimeInput() {
305: SQLDefinition sqlDefinition = SQLObjectUtil
306: .getAncestralSQLDefinition(table);
307: if (sqlDefinition != null) {
308: RuntimeDatabaseModel runModel = sqlDefinition
309: .getRuntimeDbModel();
310: if (runModel != null) {
311: RuntimeInput rInput = runModel.getRuntimeInput();
312: return rInput;
313: }
314: }
315:
316: return null;
317: }
318:
319: private void warnForInvalidCondition() {
320: String nbBundle1 = mLoc
321: .t("PRSR001: The condition is not valid.Make sure you correct it.");
322: DialogDisplayer.getDefault().notify(
323: new NotifyDescriptor.Message(
324: Localizer.parse(nbBundle1),
325: NotifyDescriptor.WARNING_MESSAGE));
326: }
327: }
|