001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.diagrameditor;
024:
025: import java.awt.Font;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.awt.Insets;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.event.KeyAdapter;
032: import java.awt.event.KeyEvent;
033:
034: import javax.swing.JButton;
035: import javax.swing.JFrame;
036: import javax.swing.JScrollPane;
037: import javax.swing.JTextPane;
038: import javax.swing.text.Element;
039: import javax.swing.text.SimpleAttributeSet;
040: import javax.swing.text.StyleConstants;
041: import javax.swing.text.StyledDocument;
042: import javax.swing.tree.DefaultMutableTreeNode;
043:
044: import com.jgraph.pad.graph.JGraphpadBusinessObject;
045: import com.jgraph.pad.graph.JGraphpadRichTextValue;
046:
047: /**
048: * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
049: * Builder, which is free for non-commercial use. If Jigloo is being used
050: * commercially (ie, by a corporation, company or business for any purpose
051: * whatever) then you should purchase a license for each developer using Jigloo.
052: * Please visit www.cloudgarden.com for details. Use of Jigloo implies
053: * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
054: * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
055: * ANY CORPORATE OR COMMERCIAL PURPOSE.
056: */
057: public class CellTextDialog extends javax.swing.JDialog {
058: private JButton okButton;
059:
060: private JButton cancelButton;
061:
062: private JTextPane textPane;
063:
064: private JScrollPane scrollPane;
065:
066: private DefaultMutableTreeNode node;
067:
068: /**
069: * Defines the underline font style. This font style is only available for
070: * rich text values.
071: */
072: protected static final int FONT_UNDERLINE = -1;
073:
074: public CellTextDialog(JFrame frame, DefaultMutableTreeNode node) {
075: super (frame);
076: initGUI();
077: this .node = node;
078: Object value = node.getUserObject();
079: if (value instanceof JGraphpadBusinessObject
080: && ((JGraphpadBusinessObject) value).isRichText())
081: try {
082: StyledDocument document = (StyledDocument) textPane
083: .getDocument();
084:
085: ((JGraphpadRichTextValue) ((JGraphpadBusinessObject) value)
086: .getValue()).insertInto(document);
087:
088: // Workaround for trailing newline
089: if (document.getLength() > 0) {
090: document.remove(document.getLength() - 1, 1);
091: }
092: } catch (Exception e) {
093: e.printStackTrace();
094: }
095: else {
096: textPane.setText(value.toString());
097: }
098: }
099:
100: private void initGUI() {
101: try {
102: GridBagLayout this Layout = new GridBagLayout();
103: this Layout.rowWeights = new double[] { 0.1, 0.0 };
104: this Layout.rowHeights = new int[] { 303, 7 };
105: this Layout.columnWeights = new double[] { 0.1, 0.0 };
106: this Layout.columnWidths = new int[] { 107, 7 };
107: getContentPane().setLayout(this Layout);
108: this .setName("Properties");
109: this .setModal(true);
110: this .setTitle("Shape text");
111: this .setFocusable(false);
112: {
113: scrollPane = new JScrollPane();
114: getContentPane().add(
115: scrollPane,
116: new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
117: GridBagConstraints.CENTER,
118: GridBagConstraints.BOTH, new Insets(0,
119: 0, 0, 0), 0, 0));
120: scrollPane.setPreferredSize(new java.awt.Dimension(360,
121: 159));
122: scrollPane.setAutoscrolls(true);
123: {
124: textPane = new JTextPane();
125: scrollPane.setViewportView(textPane);
126: textPane.setText("Description");
127: textPane.setPreferredSize(new java.awt.Dimension(
128: 218, 152));
129: textPane.addKeyListener(new KeyAdapter() {
130: public void keyTyped(KeyEvent evt) {
131: if (evt.isControlDown()) {
132: int keyChar = evt.getKeyChar();
133: switch (keyChar) {
134: case 2:
135: toggleFont(Font.BOLD);
136: break;
137: case 9:
138: toggleFont(Font.ITALIC);
139: break;
140: case 21:
141: toggleFont(FONT_UNDERLINE);
142: break;
143: }
144: }
145: }
146: });
147: }
148: }
149: {
150: okButton = new JButton();
151: getContentPane().add(
152: okButton,
153: new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
154: GridBagConstraints.EAST,
155: GridBagConstraints.NONE, new Insets(3,
156: 0, 3, 5), 0, 0));
157: okButton.setText("OK");
158: okButton.addActionListener(new ActionListener() {
159: public void actionPerformed(ActionEvent evt) {
160: okButtonActionPerformed(evt);
161: }
162: });
163: }
164: {
165: cancelButton = new JButton();
166: getContentPane().add(
167: cancelButton,
168: new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
169: GridBagConstraints.WEST,
170: GridBagConstraints.NONE, new Insets(3,
171: 0, 3, 3), 0, 0));
172: cancelButton.setText("Cancel");
173: cancelButton.addActionListener(new ActionListener() {
174: public void actionPerformed(ActionEvent evt) {
175: cancelButtonActionPerformed(evt);
176: }
177: });
178: }
179: this .setSize(237, 151);
180: } catch (Exception e) {
181: e.printStackTrace();
182: }
183: }
184:
185: private void okButtonActionPerformed(ActionEvent evt) {
186: if (node.getUserObject() instanceof JGraphpadBusinessObject) {
187: JGraphpadBusinessObject bo = (JGraphpadBusinessObject) node
188: .getUserObject();
189: bo.setValue(new JGraphpadRichTextValue(textPane
190: .getDocument()));
191: } else {
192: node.setUserObject(textPane.getText());
193: }
194: cancelButtonActionPerformed(evt);
195: }
196:
197: private void cancelButtonActionPerformed(ActionEvent evt) {
198: setVisible(false);
199: dispose();
200: }
201:
202: private void toggleFont(int style) {
203: StyledDocument doc = (StyledDocument) textPane.getDocument();
204: int start = textPane.getSelectionStart();
205: int len = textPane.getSelectionEnd() - start;
206: Element ce = doc.getCharacterElement(start);
207: SimpleAttributeSet sas = new SimpleAttributeSet();
208: switch (style) {
209: case Font.BOLD:
210: StyleConstants.setBold(sas, !StyleConstants.isBold(ce
211: .getAttributes()));
212: break;
213: case Font.ITALIC:
214: StyleConstants.setItalic(sas, !StyleConstants.isItalic(ce
215: .getAttributes()));
216: break;
217: case FONT_UNDERLINE:
218: StyleConstants.setUnderline(sas, !StyleConstants
219: .isUnderline(ce.getAttributes()));
220: break;
221: }
222: doc.setCharacterAttributes(start, len, sas, false);
223: }
224: }
|