Lifestyle, hobbies, work, rage…
Flex Builder 3: Autogenerating setter/getter
Use the following script with Eclipse Monkey; activate with ALT+9 after selecting variables:
/*
* Menu: Actionscript > Generate Properties
* Key: M3+9
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
*/
function main() {
var editor = editors.activeEditor
var source = editor.source
if (editor.selectionRange) {
var range = editor.selectionRange
var offset = range.startingOffset
var text = source.substring(offset, range.endingOffset)
var result = text.match(/([\w_]+\s*:\s*[\w_]+)/g);
o = ""
for (var i = 0, n = result.length; i < n; ++i)
o += getvar(result[i])
o += "\n";
for (var i = 0, n = result.length; i < n; ++i)
o += props(result[i])
o += "\n"
// debug(o)
editor.applyEdit(offset, range.endingOffset - offset, o)
}
}
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/, '');
};
function props(v) {
var parts = v.split(":");
if (2 != parts.length) return "";
parts[0] = parts[0].trim();
parts[1] = parts[1].trim();
var s = "\n\t\tpublic function get " + parts[0].replace("_", "") + "() : " + parts[1] + " {";
if (parts[0].indexOf("_") < 0) s += "\n\t\t\treturn _" + parts[0] + ";";
else s += "\n\t\t\treturn " + parts[0] + ";";
s += "\n\t\t}\n\n";
s += "\t\t[Bindable]\n";
s += "\t\tpublic function set " + parts[0].replace("_", "") + "(value : " + parts[1] + ") : void {";
if (parts[0].indexOf("_") < 0) s += "\n\t\t\t_" + parts[0] + " = value;";
else s += "\n\t\t\t" + parts[0] + " = value;";
s += "\n\t\t}\n";
return s;
}
function getvar(s) {
if (1 > s.length) return "";
if (s.indexOf("_") < 0) return "\n\t\tprotected var _" + s + ";";
return "\n\t\tprotected var " + s + ";";
}
function debug(s) {
Packages.org.eclipse.jface.dialogs.MessageDialog.openInformation(window.getShell(), "Monkey Debugging", s);
}
| Print article | This entry was posted by Boris on May 26, 2009 at 12:06 pm, and is filed under Flex, Internet, Personal. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |