001: /* *************************************************************************
002: *
003: * Copyright (c) 2005, Sun Microsystems,
004: * All Rights Reserved.
005:
006: If you wish your version of this file to be governed by only the CDDL
007: or only the GPL Version 2, indicate your decision by adding
008: "[Contributor] elects to include this software in this distribution
009: under the [CDDL or GPL Version 2] license." If you do not indicate a
010: single choice of license, a recipient has the option to distribute
011: your version of this file under either the CDDL, the GPL Version 2 or
012: to extend the choice of license to its licensees as provided above.
013: However, if you add GPL Version 2 code and therefore, elected the GPL
014: Version 2 license, then the option applies only if the new code is
015: made subject to such option by the copyright holder.
016:
017: If you wish your version of this file to be governed by only the CDDL
018: or only the GPL Version 2, indicate your decision by adding
019: "[Contributor] elects to include this software in this distribution
020: under the [CDDL or GPL Version 2] license." If you do not indicate a
021: single choice of license, a recipient has the option to distribute
022: your version of this file under either the CDDL, the GPL Version 2 or
023: to extend the choice of license to its licensees as provided above.
024: However, if you add GPL Version 2 code and therefore, elected the GPL
025: Version 2 license, then the option applies only if the new code is
026: made subject to such option by the copyright holder.
027: *
028: * This program, and all the routines referenced herein,
029: * are the proprietary properties and trade secrets of
030: * Sun Microsystems.
031: *
032: * Except as provided for by license agreement, this
033: * program shall not be duplicated, used, or disclosed
034: * without written consent signed by an officer of
035: * Sun Microsystems.
036: *
037: ***************************************************************************/
038: package org.netbeans.modules.sql.framework.ui.view.graph;
039:
040: import java.awt.Dialog;
041: import java.awt.event.ActionEvent;
042: import java.util.Iterator;
043: import java.util.List;
044:
045: import javax.swing.ImageIcon;
046: import javax.swing.JMenuItem;
047: import javax.swing.JPopupMenu;
048: import net.java.hulp.i18n.Logger;
049: import com.sun.sql.framework.jdbc.SQLUtils;
050: import org.netbeans.modules.etl.logger.Localizer;
051: import org.netbeans.modules.etl.logger.LogUtil;
052: import org.netbeans.modules.sql.framework.model.GUIInfo;
053: import org.netbeans.modules.sql.framework.model.SQLCanvasObject;
054: import org.netbeans.modules.sql.framework.model.SQLOperatorArg;
055: import org.netbeans.modules.sql.framework.model.impl.SQLCustomOperatorImpl;
056: import org.netbeans.modules.sql.framework.ui.graph.IOperatorField;
057: import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfo;
058: import org.netbeans.modules.sql.framework.ui.graph.impl.CustomOperatorNode;
059: import org.netbeans.modules.sql.framework.ui.graph.impl.OperatorGraphFieldNode;
060: import org.netbeans.modules.sql.framework.ui.model.CollabSQLUIModel;
061: import org.netbeans.modules.sql.framework.ui.model.impl.ConditionBuilderSQLUIModelImpl;
062: import org.netbeans.modules.sql.framework.ui.view.graph.SQLOperatorGraphNode.OperatorActionListener;
063: import org.openide.DialogDescriptor;
064: import org.openide.DialogDisplayer;
065: import org.openide.NotifyDescriptor;
066:
067: /**
068: * Graphical representation of a custom operator
069: *
070: * @author Srinivasan Rengarajan
071: */
072: public class CustomSQLOperatorGraphNode extends SQLOperatorGraphNode {
073:
074: private static transient final Logger mLogger = LogUtil
075: .getLogger(CustomSQLOperatorGraphNode.class.getName());
076: private static transient final Localizer mLoc = Localizer.get();
077: private JMenuItem editItem;
078: private IOperatorXmlInfo info;
079: private static final String LOG_CATEGORY = CustomSQLOperatorGraphNode.class
080: .getName();
081:
082: /** Creates a new instance of OperatorGraphNode */
083: public CustomSQLOperatorGraphNode(IOperatorXmlInfo info) {
084:
085: super (info, false);
086: this .info = info;
087: }
088:
089: public CustomSQLOperatorGraphNode(IOperatorXmlInfo info,
090: boolean show) {
091:
092: super (info, show);
093: this .info = info;
094: }
095:
096: public CustomSQLOperatorGraphNode(IOperatorXmlInfo info,
097: boolean show, String nameOverride) {
098:
099: super (info, show, nameOverride);
100: this .info = info;
101: }
102:
103: /**
104: * populates the menu and adds the edit option
105: */
106: protected void initializePopUpMenu() {
107:
108: CustomOperatorActionListener aListener = new CustomOperatorActionListener();
109: // remove menu
110: if (popUpMenu == null) {
111: popUpMenu = new JPopupMenu();
112: }
113:
114: editItem = new JMenuItem("Edit", new ImageIcon(editUrl));
115: editItem.addActionListener(aListener);
116: popUpMenu.add(editItem);
117: popUpMenu.addSeparator();
118: super .initializePopUpMenu();
119:
120: }
121:
122: private class CustomOperatorActionListener extends
123: OperatorActionListener {
124:
125: public void actionPerformed(ActionEvent e) {
126: super .actionPerformed(e);
127: Object source = e.getSource();
128: if (source == editItem) {
129: editItem_actionPerformed(e);
130: }
131: }
132: }
133:
134: /**
135: * method invoked to edit a custom operator definition
136: *
137: */
138: private void editCustomOperator() {
139: // step1 : get the original Data object
140: SQLCanvasObject sqlObj = (SQLCanvasObject) this .getDataObject();
141: Object graphModel = getGraphView().getGraphModel();
142: // step2 : clone it
143: SQLCanvasObject copyObj = null;
144: try {
145: // step3 : build the gui
146: SQLCustomOperatorImpl custOp = (SQLCustomOperatorImpl) sqlObj;
147: SQLOperatorArg retType = null;
148:
149: Iterator iter = custOp.getOperatorXmlInfo()
150: .getOutputFields().iterator();
151: if (iter.hasNext()) {
152: IOperatorField operField = (IOperatorField) iter.next();
153: String jdbcType = (String) operField
154: .getAttributeValue("retTypeStr");
155: retType = new SQLOperatorArg(operField.getName(),
156: SQLUtils.getStdJdbcType(jdbcType));
157: }
158: List inputArgs = custOp.getOperatorDefinition()
159: .getArgList();
160:
161: CustomOperatorPane customOptPane = new CustomOperatorPane(
162: inputArgs, retType);
163:
164: customOptPane.setFunctionName(custOp
165: .getCustomOperatorName());
166: // String title = NbBundle.getMessage(BasicSQLGraphController.class,
167: // "TITLE_user_function");
168: DialogDescriptor dlgDesc = new DialogDescriptor(
169: customOptPane, "title", true,
170: NotifyDescriptor.OK_CANCEL_OPTION,
171: NotifyDescriptor.OK_OPTION,
172: DialogDescriptor.DEFAULT_ALIGN, null, null);
173: Dialog customOptDialog = DialogDisplayer.getDefault()
174: .createDialog(dlgDesc);
175: customOptDialog
176: .getAccessibleContext()
177: .setAccessibleDescription(
178: "This dialog provides information about Custom Operators");
179: customOptDialog.setVisible(true);
180: if (NotifyDescriptor.CANCEL_OPTION == dlgDesc.getValue()) {
181: return;
182: }
183:
184: // step 4: apply changes to the custOp which is a clone of the data
185: // object
186: inputArgs = customOptPane.getArgList();
187: retType = customOptPane.getReturnType();
188: custOp.setCustomOperatorName(customOptPane
189: .getFunctionName());
190: custOp.getOperatorDefinition().setArgList(inputArgs);
191: CustomOperatorNode customOptNode = new CustomOperatorNode(
192: info, inputArgs, retType);
193: custOp.setOperatorXmlInfo(customOptNode);
194: Iterator fieldIter = fieldList.iterator();
195: while (fieldIter.hasNext()) {
196: OperatorGraphFieldNode fieldNode = (OperatorGraphFieldNode) fieldIter
197: .next();
198: this .removeObject(fieldNode);
199:
200: }
201: this .fieldList.clear();
202: super .initialize(customOptNode);
203: GUIInfo guiInfo = custOp.getGUIInfo();
204: int x = guiInfo.getX();
205: int y = guiInfo.getY();
206: int width = guiInfo.getWidth();
207: int height = guiInfo.getHeight();
208: this .getBoundingRect().setLocation(x, y);
209: this .getBoundingRect().setSize(width, height);
210: this .titleArea.setTitle(customOptPane.getFunctionName());
211: this .titleArea.setTopLeft(x, y);
212: this .layoutChildren();
213: if (graphModel instanceof CollabSQLUIModel) {
214: ((CollabSQLUIModel) graphModel).restoreUIState();
215: } else if (graphModel instanceof ConditionBuilderSQLUIModelImpl) {
216: ((ConditionBuilderSQLUIModelImpl) graphModel)
217: .restoreUIState();
218: }
219: } catch (Exception e) {
220: mLogger.errorNoloc(mLoc.t("PRSR167: editCustomOperator{0}",
221: e.getMessage()), e);
222: }
223: }
224:
225: /**
226: * Invoked when an action occurs.
227: */
228: private void editItem_actionPerformed(ActionEvent e) {
229: this.editCustomOperator();
230: }
231: }
|