PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/etl.editor/src/org/netbeans/modules/sql/framework/ui/view/graph/SQLLiteralGraphNode.java

https://bitbucket.org/rsaqc/netbeans-soa
Java | 391 lines | 229 code | 68 blank | 94 comment | 31 complexity | 3db1ba8fe37a73a369f56ef22030b1b3 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.sql.framework.ui.view.graph;
  45. import java.awt.Color;
  46. import java.awt.Rectangle;
  47. import java.beans.PropertyChangeEvent;
  48. import java.beans.PropertyChangeListener;
  49. import java.net.URL;
  50. import java.sql.Types;
  51. import javax.swing.Icon;
  52. import javax.swing.ImageIcon;
  53. import javax.swing.SwingUtilities;
  54. import org.netbeans.modules.sql.framework.common.jdbc.SQLUtils;
  55. import org.netbeans.modules.sql.framework.model.SQLLiteral;
  56. import org.netbeans.modules.sql.framework.model.VisibleSQLLiteral;
  57. import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
  58. import org.netbeans.modules.sql.framework.ui.graph.IOperatorField;
  59. import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfo;
  60. import org.netbeans.modules.sql.framework.ui.graph.impl.BasicCellArea;
  61. import org.netbeans.modules.sql.framework.ui.graph.impl.CanvasArea;
  62. import org.netbeans.modules.sql.framework.ui.graph.impl.OperatorGraphFieldNode;
  63. import com.nwoods.jgo.JGoPen;
  64. import com.nwoods.jgo.JGoRectangle;
  65. import com.nwoods.jgo.JGoText;
  66. import net.java.hulp.i18n.Logger;
  67. import org.netbeans.modules.etl.logger.Localizer;
  68. /**
  69. * Graphical representation of literal element.
  70. *
  71. * @author Ritesh Adval
  72. * @author Jonathan Giron
  73. * @version $Revision$
  74. */
  75. public class SQLLiteralGraphNode extends SQLOperatorGraphNode implements PropertyChangeListener {
  76. /* contains literal value + link node */
  77. private OperatorGraphFieldNode valueNode;
  78. private static final URL URL_NUMBER_ICON = SQLLiteralGraphNode.class.getResource("/org/netbeans/modules/sql/framework/ui/resources/images/Math.png");
  79. private static final URL URL_DATE_ICON = SQLLiteralGraphNode.class.getResource("/org/netbeans/modules/sql/framework/ui/resources/images/NOW2.png");
  80. private static final URL URL_TEXT_ICON = SQLLiteralGraphNode.class.getResource("/org/netbeans/modules/sql/framework/ui/resources/images/literal.png");
  81. private static transient final Logger mLogger = Logger.getLogger(SQLLiteralGraphNode.class.getName());
  82. private static transient final Localizer mLoc = Localizer.get();
  83. private static Icon numberIcon;
  84. private static Icon dateIcon;
  85. private static Icon textIcon;
  86. /**
  87. * Creates a new instance of SQLLiteralGraphNode, using the given visual configuration
  88. * information.
  89. *
  90. * @param info GUI configuration information
  91. */
  92. public SQLLiteralGraphNode(IOperatorXmlInfo info) {
  93. super(info);
  94. }
  95. /**
  96. * Initializes the UI look and feel based on the given visual configuration
  97. * information.
  98. *
  99. * @param info GUI configuration information
  100. */
  101. protected void initialize(IOperatorXmlInfo info) {
  102. //add one output graph field
  103. for (int i = 0; i < info.getOutputCount(); i++) {
  104. IOperatorField field = (IOperatorField) info.getOutputFields().get(i);
  105. valueNode = new OperatorGraphFieldNode(BasicCellArea.RIGHT_PORT_AREA, field);
  106. valueNode.addPropertyChangeListener(this);
  107. valueNode.setTextAlignment(JGoText.ALIGN_CENTER);
  108. valueNode.setLinePen(JGoPen.makeStockPen(Color.WHITE));
  109. valueNode.setBrush(BRUSH_OUTPUT_REGULAR);
  110. valueNode.setTextColor(TEXT_COLOR_LITERAL);
  111. this.addObjectAtTail(valueNode);
  112. fieldList.add(valueNode);
  113. }
  114. this.setResizable(true);
  115. }
  116. /**
  117. * get maximum height
  118. *
  119. * @return max height
  120. */
  121. public int getMaximumHeight() {
  122. return super.getMaximumHeight();
  123. }
  124. /**
  125. * @see org.netbeans.modules.sql.framework.ui.graph.ICanvasInterface#getMaximumWidth()
  126. */
  127. public int getMaximumWidth() {
  128. // Establish max width of 'literal' label, other components in parent, versus type
  129. // label.
  130. return Math.max(super.getMaximumWidth(), valueNode.getMaximumWidth());
  131. }
  132. /**
  133. * Gets output graph port, given its field name.
  134. *
  135. * @param fieldName field name
  136. * @return graph port
  137. */
  138. public IGraphPort getOutputGraphPort(String fieldName) {
  139. return valueNode.getRightGraphPort();
  140. }
  141. /**
  142. * Sets data object for which this UI element provides a view.
  143. *
  144. * @param obj data object
  145. */
  146. public void setDataObject(Object obj) {
  147. this.dataObject = obj;
  148. valueNode.setText(((VisibleSQLLiteral) obj).getValue());
  149. SQLLiteral lit = (SQLLiteral) obj;
  150. if (lit != null) {
  151. try {
  152. int jdbcType = lit.getJdbcType();
  153. switch (jdbcType) {
  154. case Types.NUMERIC:
  155. case Types.FLOAT:
  156. case Types.DOUBLE:
  157. case Types.INTEGER:
  158. titleArea.setTitleImage(getNumberIcon());
  159. break;
  160. case Types.TIME:
  161. case Types.TIMESTAMP:
  162. titleArea.setTitleImage(getDateIcon());
  163. break;
  164. case Types.VARCHAR:
  165. case Types.CHAR:
  166. default:
  167. titleArea.setTitleImage(getTextIcon());
  168. break;
  169. }
  170. titleArea.setTitle(SQLUtils.getStdSqlType(jdbcType));
  171. } catch (IllegalArgumentException ignore) {
  172. String nbBundle1 = mLoc.t("BUND447: SQL type:");
  173. final String typeLabel = nbBundle1.substring(15);
  174. titleArea.setTitle(typeLabel);
  175. }
  176. }
  177. this.layoutChildren();
  178. }
  179. private static Icon getNumberIcon() {
  180. if (numberIcon == null) {
  181. numberIcon = new ImageIcon(URL_NUMBER_ICON);
  182. }
  183. return numberIcon;
  184. }
  185. private static Icon getDateIcon() {
  186. if (dateIcon == null) {
  187. dateIcon = new ImageIcon(URL_DATE_ICON);
  188. }
  189. return dateIcon;
  190. }
  191. private static Icon getTextIcon() {
  192. if (textIcon == null) {
  193. textIcon = new ImageIcon(URL_TEXT_ICON);
  194. }
  195. return textIcon;
  196. }
  197. /**
  198. * layout the children
  199. */
  200. public void layoutChildren() {
  201. Rectangle boundingRect = new Rectangle(this.getBoundingRect());
  202. int minLayoutWidth = getMinimumWidth();
  203. if (boundingRect.width < minLayoutWidth || !isExpandedState()) {
  204. boundingRect.width = minLayoutWidth;
  205. this.setBoundingRect(boundingRect);
  206. }
  207. columnRect.setBoundingRect(boundingRect);
  208. int rectleft = getLeft();
  209. int recttop = getTop();
  210. int rectwidth = getWidth();
  211. int rectheight = getHeight();
  212. int left = rectleft + insets.left;
  213. int top = recttop + insets.top;
  214. int width = rectwidth - insets.left - insets.right;
  215. int height = rectheight - insets.top - insets.bottom;
  216. titleArea.setBoundingRect(left, top, width, titleArea.getMinimumHeight());
  217. int aggrHeight = top + titleArea.getMinimumHeight();
  218. for (int i = 0; i < fieldList.size(); i++) {
  219. OperatorGraphFieldNode fieldNode = (OperatorGraphFieldNode) fieldList.get(i);
  220. if (aggrHeight < top + height) {
  221. fieldNode.setVisible(true);
  222. fieldNode.setBoundingRect(left, aggrHeight, width, fieldNode.getHeight() + verticalGap);
  223. } else {
  224. fieldNode.setVisible(false);
  225. fieldNode.setBoundingRect(left, top, width, fieldNode.getHeight() + verticalGap);
  226. }
  227. aggrHeight += fieldNode.getHeight() + verticalGap;
  228. }
  229. }
  230. /**
  231. * Handles change to a bound property as indicated by the given event.
  232. *
  233. * @param evt A PropertyChangeEvent object describing the event source and the
  234. * property that has changed.
  235. */
  236. public void propertyChange(PropertyChangeEvent evt) {
  237. if (evt.getPropertyName().equals(BasicCellArea.TEXT)) {
  238. VisibleSQLLiteral lit = (VisibleSQLLiteral) this.getDataObject();
  239. String newValue = (String) evt.getNewValue();
  240. if (lit != null) {
  241. final String errorMsg = LiteralDialog.evaluateIfLiteralValid(newValue, lit.getJdbcType());
  242. if (errorMsg == null) {
  243. lit.setValue((String) evt.getNewValue());
  244. } else {
  245. valueNode.setText((String) evt.getOldValue());
  246. SwingUtilities.invokeLater(new Runnable() {
  247. public void run() {
  248. LiteralDialog.showMessage(errorMsg);
  249. }
  250. });
  251. }
  252. setSize(getMaximumWidth(), getMaximumHeight());
  253. } else {
  254. valueNode.setText((String) evt.getOldValue());
  255. }
  256. }
  257. }
  258. class BasicTypeArea extends CanvasArea {
  259. private BasicCellArea typeArea1;
  260. private BasicCellArea typeValueArea;
  261. private JGoRectangle rect;
  262. BasicTypeArea(String type, String typeValue) {
  263. super();
  264. this.setSelectable(false);
  265. this.setResizable(false);
  266. rect = new JGoRectangle();
  267. rect.setPen(SQLOperatorGraphNode.PEN_DEFAULT);
  268. rect.setBrush(SQLOperatorGraphNode.BRUSH_INPUT_REGULAR);
  269. rect.setSelectable(false);
  270. rect.setResizable(false);
  271. addObjectAtHead(rect);
  272. typeArea1 = new BasicCellArea(type);
  273. typeArea1.setTextAlignment(JGoText.ALIGN_CENTER);
  274. typeArea1.setLinePen(SQLOperatorGraphNode.PEN_DEFAULT);
  275. typeArea1.setBrush(SQLOperatorGraphNode.BRUSH_INPUT_REGULAR);
  276. typeArea1.setTextColor(TEXT_COLOR_INPUT);
  277. this.addObjectAtTail(typeArea1);
  278. typeValueArea = new BasicCellArea(typeValue);
  279. typeValueArea.setTextAlignment(JGoText.ALIGN_CENTER);
  280. typeValueArea.setLinePen(SQLOperatorGraphNode.PEN_DEFAULT);
  281. typeValueArea.setBrush(SQLOperatorGraphNode.BRUSH_INPUT_REGULAR);
  282. typeValueArea.setTextColor(TEXT_COLOR_INPUT);
  283. this.addObjectAtTail(typeValueArea);
  284. this.setSize(getMaximumWidth(), getMaximumHeight());
  285. }
  286. public int getMaximumHeight() {
  287. int h = this.insets.top + this.insets.bottom;
  288. if (typeArea1 != null) {
  289. h += typeArea1.getMaximumHeight();
  290. }
  291. if (typeValueArea != null) {
  292. h += typeValueArea.getHeight();
  293. }
  294. return h;
  295. }
  296. public int getMaximumWidth() {
  297. int w = this.insets.left + this.insets.right;
  298. int width = 0;
  299. if (typeArea1 != null) {
  300. width = typeArea1.getMaximumWidth();
  301. }
  302. if (typeValueArea != null && typeValueArea.getWidth() > width) {
  303. width = typeValueArea.getWidth();
  304. }
  305. w += width;
  306. return w;
  307. }
  308. public void layoutChildren() {
  309. rect.setBoundingRect(this.getBoundingRect());
  310. int rectleft = getLeft();
  311. int recttop = getTop();
  312. int rectwidth = getWidth();
  313. int left = rectleft + insets.left;
  314. int top = recttop + insets.top;
  315. int width = rectwidth - insets.left - insets.right;
  316. typeArea1.setBoundingRect(left, top, width, typeArea1.getHeight());
  317. typeValueArea.setBoundingRect(left, top + typeArea1.getHeight(), width, typeValueArea.getHeight());
  318. }
  319. public void setValueType(String vType) {
  320. typeValueArea.setText(vType);
  321. }
  322. }
  323. }