001: package net.xoetrope.builder.editor.helper;
002:
003: import java.util.ArrayList;
004:
005: import java.awt.Component;
006: import java.awt.Container;
007: import java.awt.event.KeyEvent;
008: import javax.swing.JOptionPane;
009:
010: import net.xoetrope.builder.editor.XPageResource;
011: import net.xoetrope.builder.editor.XSourceEditor;
012: import net.xoetrope.xui.XPage;
013: import net.xoetrope.builder.editor.XEditorProject;
014: import net.xoetrope.builder.editor.components.swing.XComponentProxy;
015:
016: /**
017: * Insert a refernce to a component as a member variable, initialize the reference
018: * in thge pageCreated method and add any required imports.
019: *
020: * <p> Copyright (c) Xoetrope Ltd., 2002-2003</p>
021: * <p> $Revision: 1.5 $</p>
022: * <p> License: see License.txt</p>
023: */
024: public class MemberReferenceCodeHelper extends CodeHelper {
025: private static MemberReferenceCodeHelper instance = new MemberReferenceCodeHelper();
026:
027: public MemberReferenceCodeHelper() {
028: }
029:
030: public static void register(ArrayList resources) {
031: if ((resources != null) && (!resources.contains(instance)))
032: resources.add(instance);
033: }
034:
035: /**
036: * Insert the new member reference, imports and initialization code
037: */
038: public void doInsert(XSourceEditor editor,
039: XEditorProject currentProject, XPageResource pageRes) {
040: Component comp = getComponent(editor, pageRes.getPage());
041: if (comp == null)
042: return;
043:
044: String compName = comp.getName();
045: String className;
046: if (comp instanceof XComponentProxy)
047: className = ((XComponentProxy) comp).getProxiedComponent()
048: .getClass().getName();
049: else
050: className = comp.getClass().getName();
051:
052: if (!currentProject.isSwingClient())
053: className = className.replaceFirst("swing", "awt");
054: String compType = className.substring(className
055: .lastIndexOf('.') + 1);
056:
057: String importStr = "import " + className + ";";
058: String refStr = compType + " " + compName + ";";
059: String initStr = compName + " = (" + compType
060: + ")findComponent( \"" + compName + "\" );";
061:
062: try {
063: String classText = editor.getText();
064: if (classText.indexOf(importStr) < 0) {
065: int currentPos = editor.getCaretPosition();
066: int firstLine = editor.getLineStartOffset(2);
067: editor.getDocument().insertString(firstLine,
068: importStr + "\n", null);
069: }
070:
071: // Find the class def
072: if (classText.indexOf(refStr) < 0) {
073: classText = editor.getText();
074: int classPos = classText.indexOf("public class ");
075: int refPos = classText.indexOf("{", classPos);
076: refPos++;
077: if (classText.charAt(refPos) == '\n')
078: refPos++;
079: editor.getDocument().insertString(refPos,
080: "\t" + refStr + "\n", null);
081: }
082:
083: // Find the page created event handler
084: if (classText.indexOf(initStr) < 0) {
085: classText = editor.getText();
086: int classPos = classText
087: .indexOf("public void pageCreated()");
088: if (classPos < 0) {
089: String pageCreatedStr = "\n\t/**\n\t"
090: + " * A method called once the page has been created but not yet initialized.\n\t"
091: + " */\n\t"
092: + "public void pageCreated()\n\t" + "{\n\t"
093: + "}\n\t";
094:
095: editor.getDocument().insertString(
096: classText.lastIndexOf('}') - 1,
097: "\t" + pageCreatedStr + "\n", null);
098: classText = editor.getText();
099: classPos = classText
100: .indexOf("public void pageCreated()");
101: }
102: int refPos = classText.indexOf("{", classPos);
103: refPos++;
104: if (classText.charAt(refPos) == '\n')
105: refPos++;
106: editor.getDocument().insertString(refPos,
107: "\t\t" + initStr + "\n", null);
108: }
109: } catch (Exception ex) {
110: }
111: }
112:
113: /**
114: * Get the caption displayed on the popup menu
115: * @return the caption
116: */
117: public String getCaption() {
118: return "component reference";
119: }
120:
121: /**
122: * Get the mnemonic key for this item
123: * @return the mnemonic
124: */
125: public Integer getMnemonic() {
126: return new Integer(KeyEvent.VK_C);
127: }
128:
129: /**
130: * Get the component to reference
131: * @param editor the source editor window/the parent for the popup
132: * @param page the page whose components are to be listed
133: * @return the selected component or null
134: */
135: protected Component getComponent(XSourceEditor editor, XPage page) {
136: try {
137: ArrayList componentNames = new ArrayList();
138: addComponentNames((Container) page, componentNames);
139:
140: Object[] componentList = componentNames.toArray();
141: String value = (String) JOptionPane.showInputDialog(editor,
142: "Which component?", "Add reference",
143: JOptionPane.QUESTION_MESSAGE, null, componentList,
144: null);
145: return page.findComponent(value);
146: } catch (Exception ex) {
147: return null;
148: }
149: }
150:
151: /**
152: * Recursively add the named components
153: * @param cont the current container
154: * @param store the store in which to place the names
155: */
156: private void addComponentNames(Container cont, ArrayList store) {
157: Component comps[] = cont.getComponents();
158: for (int i = 0; i < comps.length; i++) {
159: String compName = comps[i].getName();
160: if (compName != null)
161: store.add(compName);
162: if (comps[i] instanceof Container)
163: addComponentNames((Container) comps[i], store);
164: }
165: }
166: }
|