001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.export;
020:
021: import java.awt.Component;
022: import java.awt.Toolkit;
023: import java.awt.datatransfer.Clipboard;
024: import java.awt.datatransfer.StringSelection;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027:
028: import com.jeta.forms.gui.beans.JETABean;
029: import com.jeta.forms.gui.form.FormComponent;
030: import com.jeta.forms.gui.form.GridComponent;
031: import com.jeta.forms.gui.form.GridView;
032: import com.jeta.forms.gui.form.StandardComponent;
033:
034: /**
035: * Exports the component names to the clipboard for the given form.
036: *
037: *
038: * @author Jeff Tassin
039: */
040: public class ComponentNamesExporter {
041: /**
042: * Flag that indicates if linked, nested forms should be included in the
043: * export
044: */
045: private boolean m_include_linked = false;
046:
047: /**
048: * Flag that indicates if embedded, nested forms should be included in the
049: * export
050: */
051: private boolean m_include_embedded = true;
052:
053: /**
054: * Flag that indicates if labels should be included in the export
055: */
056: private boolean m_include_labels = false;
057:
058: /**
059: * The line decorator separated into tokens.
060: */
061: private LinkedList m_tokens = new LinkedList();
062:
063: /**
064: * Valid tokens for the line decorator
065: */
066: public static final String IDENTIFIER = "$identifier";
067: public static final String COMPONENT_NAME = "$name";
068: public static final String TYPE = "$type";
069:
070: private String[] m_token_defs = { IDENTIFIER, COMPONENT_NAME, TYPE };
071:
072: /**
073: * ctor
074: */
075: public ComponentNamesExporter(ExportNamesView view) {
076: m_include_linked = view.isIncludeLinkedForms();
077: m_include_embedded = view.isIncludeEmbeddedForms();
078: m_include_labels = view.isIncludeLabels();
079:
080: String decorator = view.getDecorator();
081: assert (decorator != null);
082:
083: boolean bfound = true;
084: while (bfound) {
085: bfound = false;
086: for (int index = 0; index < m_token_defs.length; index++) {
087: String token_def = m_token_defs[index];
088: int pos = decorator.indexOf(token_def);
089: if (pos >= 0) {
090: String token = decorator.substring(0, pos);
091: if (token.length() > 0)
092: m_tokens.add(token);
093:
094: m_tokens.add(token_def);
095: decorator = decorator.substring(pos
096: + token_def.length(), decorator.length());
097: bfound = true;
098: }
099: }
100: }
101: m_tokens.add(decorator);
102: }
103:
104: /**
105: * Decorates the component name using the supplied formatting rules
106: */
107: private String decorateName(String compName, Component comp) {
108: if (compName == null || compName.length() == 0)
109: return null;
110:
111: if (comp instanceof javax.swing.JLabel && !m_include_labels)
112: return null;
113:
114: StringBuffer sbuff = new StringBuffer();
115: Iterator iter = m_tokens.iterator();
116: while (iter.hasNext()) {
117: String token = (String) iter.next();
118: if (IDENTIFIER.equals(token)) {
119: sbuff.append(toJavaLiteral(compName));
120: } else if (COMPONENT_NAME.equals(token)) {
121: sbuff.append(compName);
122: } else if (TYPE.equals(token)) {
123: if (comp instanceof GridView) {
124: sbuff.append(javax.swing.JPanel.class.getName());
125: } else {
126: sbuff.append(comp.getClass().getName());
127: }
128: } else {
129: sbuff.append(token);
130: }
131: }
132: return sbuff.toString();
133: }
134:
135: /**
136: * Exports the component names to the clipboard for the given form.
137: */
138: private String export(FormComponent form) {
139: StringBuffer sbuff = new StringBuffer();
140:
141: String formname = form.getChildView().getName();
142: if (formname != null && formname.length() > 0) {
143: String result = decorateName(formname, form.getChildView());
144: if (result != null) {
145: sbuff.append(result);
146: sbuff.append('\n');
147: }
148: }
149:
150: Iterator iter = form.gridIterator();
151:
152: LinkedList forms = new LinkedList();
153: while (iter.hasNext()) {
154: GridComponent gc = (GridComponent) iter.next();
155: if (gc instanceof FormComponent) {
156: FormComponent childform = (FormComponent) gc;
157: if (childform.isLinked() && !m_include_linked)
158: continue;
159:
160: if (childform.isEmbedded() && !m_include_embedded)
161: continue;
162:
163: forms.add(childform);
164: } else if (gc instanceof StandardComponent) {
165: StandardComponent stdcomp = (StandardComponent) gc;
166: JETABean bean = stdcomp.getBean();
167: if (bean != null) {
168: Component comp = bean.getDelegate();
169: if (comp != null) {
170:
171: String compname = comp.getName();
172: if (compname != null && compname.length() > 0) {
173: String result = decorateName(compname, comp);
174: if (result != null) {
175: sbuff.append(result);
176: sbuff.append('\n');
177: }
178: }
179: }
180: }
181: } else {
182: assert (false);
183: }
184: }
185:
186: /**
187: * Now, export each child form
188: */
189: iter = forms.iterator();
190: while (iter.hasNext()) {
191: FormComponent fc = (FormComponent) iter.next();
192: String compname = fc.getChildView().getName();
193: if (compname != null && compname.length() > 0) {
194: String result = decorateName(compname, fc
195: .getChildView());
196: if (result != null) {
197: sbuff.append(result);
198: sbuff.append('\n');
199: }
200: }
201: sbuff.append(export(fc));
202: }
203:
204: return sbuff.toString();
205: }
206:
207: /**
208: * Exports the component names to the clipboard
209: */
210: public void exportToClipboard(FormComponent fc) {
211: String result = export(fc);
212: try {
213: Toolkit kit = Toolkit.getDefaultToolkit();
214: Clipboard clipboard = kit.getSystemClipboard();
215: StringSelection transferable = new StringSelection(result);
216: clipboard.setContents(transferable, null);
217: } catch (Exception e) {
218: e.printStackTrace();
219: }
220: }
221:
222: /**
223: * Converts the given name to a valid Java Literal. All non-characters are
224: * converted to a _ (underscore).
225: */
226: private String toJavaLiteral(String compNameValue) {
227: String compName = compNameValue.toUpperCase();
228: StringBuffer sbuff = new StringBuffer();
229: for (int index = 0; index < compName.length(); index++) {
230: char c = compName.charAt(index);
231: if (index == 0) {
232: if (Character.isJavaIdentifierStart(c)) {
233: sbuff.append(c);
234: } else {
235: sbuff.append('_');
236: sbuff.append(c);
237: }
238: } else {
239: if (Character.isJavaIdentifierPart(c)) {
240: sbuff.append(c);
241: } else {
242: sbuff.append('_');
243: }
244: }
245: }
246: return sbuff.toString();
247: }
248:
249: }
|