001: /*
002: * PopUpWindow.java
003: *
004: * Created on 30 January 2007, 15:55
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package com.xoetrope.svg;
011:
012: import java.awt.BasicStroke;
013: import java.awt.BorderLayout;
014: import java.awt.Color;
015: import java.awt.Font;
016: import java.awt.Graphics;
017: import java.awt.Graphics2D;
018: import java.awt.Rectangle;
019: import java.awt.RenderingHints;
020: import java.awt.Stroke;
021: import java.awt.geom.Arc2D;
022: import java.awt.geom.GeneralPath;
023: import java.awt.geom.Line2D;
024: import java.awt.geom.RoundRectangle2D;
025: import javax.swing.BorderFactory;
026: import javax.swing.JComponent;
027: import javax.swing.JDialog;
028: import javax.swing.JFrame;
029: import javax.swing.JPanel;
030: import javax.swing.JTextArea;
031: import javax.swing.JTextField;
032: import net.xoetrope.swing.XTextArea;
033: import net.xoetrope.xui.XAttributedComponent;
034: import net.xoetrope.xui.XProject;
035: import net.xoetrope.xui.XProjectManager;
036: import net.xoetrope.xui.XTextHolder;
037: import net.xoetrope.xui.helper.XuiUtilities;
038:
039: /**
040: *
041: *
042: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
043: * the GNU Public License (GPL), please see license.txt for more details. If
044: * you make commercial use of this software you must purchase a commercial
045: * license from Xoetrope.</p>
046: * <p> $Revision: 1.2 $</p>
047: */
048: public class XPopUpWindow extends JComponent implements
049: XAttributedComponent, XTextHolder {
050: protected int fontSize;
051: protected double x, y, width, height, scale1, scale2, scale3,
052: scale4, scale5, scale6, scale7, scale8;
053: protected String text, captionStyle;
054: protected Stroke stroke;
055: protected XTextArea textArea;
056: protected boolean first;
057:
058: protected XProject currentProject = XProjectManager
059: .getCurrentProject();
060:
061: /** Creates a new instance of PopUpWindow
062: * @param x <CODE>int</CODE> specifying the x-coordinate of the pop-up window.
063: * @param y <CODE>int</CODE> specifying the y-coordinate of the pop-up window.
064: * @param text <CODE>String</CODE> the text to be displayed in the pop-up window.
065: */
066: public XPopUpWindow() {
067: first = true;
068:
069: textArea = new XTextArea();
070: stroke = new BasicStroke(2.0F);
071: width = 180.0;
072: height = 170.0;
073: fontSize = 12;
074:
075: scale1 = 0.33333333333333333333333333333333;
076: scale2 = 0.52941176470588235294117647058824;
077: scale3 = 0.47058823529411764705882352941176;
078: scale4 = 0.63529411764705882352941176470588;
079: scale5 = 0.88888888888888888888888888888889;
080: scale6 = 0.41176470588235294117647058823529;
081: scale7 = 0.27777777777777777777777777777778;
082: scale8 = 0.94117647058823529411764705882353;
083:
084: setSize(1, 1);
085: setOpaque(true);
086: }
087:
088: /**
089: * Set the size of the pop-up window.
090: * @param x1 <CODE>int</CODE> specifying the x coordinate of the component.
091: * @param y1 <CODE>int</CODE> specifying the y coordinate of the component.
092: * @param w1 <CODE>int</CODE> specifying the width of the component.
093: * @param h1 <CODE>int<CODE> specifying the height of the component.
094: */
095: public void setBounds(int x1, int y1, int w1, int h1) {
096: super .setBounds(x1, y1, w1, h1);
097:
098: x = w1 * 0.34;
099: y = h1 + 1;
100: width = w1 - 3.0;
101: height = h1;
102: }
103:
104: /**
105: * Sets the content of the pop-up window.
106: * @param text <CODE>String</CODE> specifying the content.
107: */
108: public void setText(String text) {
109: this .text = text;
110: textArea.setText(text);
111: }
112:
113: /**
114: * Returns the content currently displayed in the pop-up window.
115: * @return <CODE>String</CODE> containing the currently displayed text.
116: */
117: public String getText() {
118: return text;
119: }
120:
121: /**
122: * Set one or more attributes of the component. Currently this handles the attributes
123: * <OL>
124: * <LI>content, value=The textual information displayed by the component or</LI>
125: * <LI>text, value=The textual information displayed by the component</LI>
126: * <LI>style, value=The style for the text </LI>
127: * </OL>
128: * @param attribName the attribute name
129: * @param attribValue the attribute value
130: * @return 0 for success, non zero otherwise
131: */
132: public int setAttribute(String attribName, Object attribValue) {
133: String attribNameLwr = attribName.toLowerCase();
134: String attribValueStr = (String) attribValue;
135: if (attribNameLwr.equals("content")
136: || attribNameLwr.equals("text"))
137: setText(attribValueStr);
138: else if (attribNameLwr.equals("style"))
139: setStyle(attribValueStr);
140:
141: return 0;
142: }
143:
144: /**
145: * Sets the size of the font displayed in the pop-up window.
146: * @param fontSize <CODE>String</CODE> specifying the size of the font.
147: */
148: public void setFontSize(String fontSize) {
149: this .fontSize = Integer.parseInt(fontSize);
150: textArea.setFont(new Font("Arial", Font.PLAIN, Integer
151: .parseInt(fontSize)));
152: }
153:
154: /**
155: * Sets the size of the font displayed in the pop-up window.
156: * @param fontSize <CODE>int</CODE> specifying the size of the font.
157: */
158: public void setFontSize(int fontSize) {
159: this .fontSize = fontSize;
160: textArea.setFont(new Font("Arial", Font.PLAIN, fontSize));
161: }
162:
163: /**
164: * Returns the font size of the currently displayed text.
165: * @return <CODE>int</CODE> specifying the font size of the currently displayed text.
166: */
167: public int getFontSize() {
168: return fontSize;
169: }
170:
171: /**
172: * Set the text style
173: * @param newStyle the new text style
174: */
175: public void setStyle(String newStyle) {
176: captionStyle = newStyle;
177: XuiUtilities.applyStyle(currentProject, textArea, newStyle);
178: }
179:
180: /**
181: * Paints the pop-up window within its parent component.
182: * @param g the delegate <CODE>Graphics</CODE> object.
183: */
184: public void paintComponent(Graphics g) {
185: Graphics2D g2 = (Graphics2D) g.create();
186: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
187: RenderingHints.VALUE_ANTIALIAS_ON);
188: g2.setStroke(stroke);
189: g2.setColor(Color.gray);
190:
191: GeneralPath path = new GeneralPath();
192:
193: RoundRectangle2D.Double roundRect = new RoundRectangle2D.Double(
194: 2, 2, width, (height * scale2), 20.0, 20.0);
195: g2.draw(roundRect);
196:
197: Line2D.Double line = new Line2D.Double((width * scale1),
198: height, (width * scale4), (height * scale2));
199: path.append(line, true);
200:
201: Line2D.Double line2 = new Line2D.Double((width * scale3),
202: (height * scale2), (width * scale1), height);
203: path.append(line2, true);
204:
205: g2.draw(path);
206:
207: g2.setColor(Color.decode("#FFF6D5"));
208: g2.fill(roundRect);
209: g2.fill(path);
210:
211: g2.setColor(Color.black);
212:
213: if (first) {
214: textArea.setFont(new Font("Arial", Font.PLAIN, fontSize));
215: textArea.setLineWrap(true);
216: textArea.setWrapStyleWord(true);
217: textArea.setEditable(false);
218: textArea.setBackground(Color.decode("#FFF6D5"));
219: add(textArea);
220: first = false;
221: }
222:
223: textArea.setBounds((int) (x - (width * scale7)),
224: (int) (y - (height * scale8)), (int) (width * scale5),
225: (int) (height * scale6));
226:
227: g2.dispose();
228: }
229: }
|