001: package tide.syntaxtree;
002:
003: import snow.utils.gui.NamedAction;
004: import java.awt.event.ActionEvent;
005: import snow.datatransfer.ClipboardUtils;
006: import javaparser.*;
007: import java.util.*;
008: import javax.swing.Action;
009:
010: public final class ActionsForNode {
011: private ActionsForNode() {
012: }
013:
014: /** These actions copies some text to clipboard...
015: */
016: public static List<Action> generateActionsForNode(ParserTreeNode tn) {
017: List<Action> actions = new ArrayList<Action>();
018:
019: if (tn instanceof FieldNode) {
020: final FieldNode fn = (FieldNode) tn;
021: actions.add(new NamedAction("Create getter and setter for "
022: + fn.name + " (CTRL+V to paste it)") {
023: public void actionPerformed(ActionEvent ae) {
024: generateGetSetCodeToClipboard(fn, true, true);
025: }
026: });
027: actions.add(new NamedAction("Create getter for " + fn.name
028: + " (CTRL+V to paste it)") {
029: public void actionPerformed(ActionEvent ae) {
030: generateGetSetCodeToClipboard(fn, true, false);
031: }
032: });
033: actions.add(new NamedAction("Create setter for " + fn.name
034: + " (CTRL+V to paste it)") {
035: public void actionPerformed(ActionEvent ae) {
036: generateGetSetCodeToClipboard(fn, false, true);
037: }
038: });
039:
040: actions.add(new NamedAction(
041: "Copy name (CTRL+V to paste it)") {
042: public void actionPerformed(ActionEvent ae) {
043: ClipboardUtils.copyToClipboard(fn.name);
044: }
045: });
046: }
047: if (tn instanceof MethodNode) {
048: MethodNode mn = (MethodNode) tn;
049: // ?
050: } else if (tn instanceof EnumNode) {
051: // not very useful in this syntax
052: final EnumNode fn = (EnumNode) tn;
053: actions.add(new NamedAction("Create enum switch for "
054: + fn.name + " (CTRL+V to paste it)") {
055: public void actionPerformed(ActionEvent ae) {
056: //generateCodeToClipboard(fn, true, true);
057: StringBuilder sb = new StringBuilder(
058: " switch (xxx) {\n");
059: for (int i = 0; i < fn.getChildCount(); i++) {
060: if (fn.getChildNodeAt(i) instanceof EnumConstantNode) {
061: sb
062: .append("\n case "
063: + fn.getChildNodeAt(i)
064: + ": break;"); // usually break or return...
065: }
066: }
067: sb.append("\n default: break; // ");
068: sb.append("\n }");
069: ClipboardUtils.copyToClipboard(sb.toString());
070: }
071: });
072: }
073:
074: return actions;
075: }
076:
077: /** Getter and/or setter.
078: */
079: private static void generateGetSetCodeToClipboard(FieldNode field,
080: boolean getter, boolean setter) {
081: String nameUp = field.name;
082: if (Character.isLowerCase(nameUp.charAt(0))) {
083: nameUp = Character.toUpperCase(nameUp.charAt(0))
084: + nameUp.substring(1);
085: }
086: StringBuilder sb = new StringBuilder();
087: if (getter) {
088: sb.append(" public ");
089: if (field.isStatic())
090: sb.append("static "); // static is already final
091: else
092: sb.append("final "); // added Oct2007
093: sb.append(field.type + " get" + nameUp + "() { return "
094: + field.name + "; }\r\n");
095: }
096:
097: if (setter) {
098: sb.append(" public ");
099: if (field.isStatic())
100: sb.append("static ");
101: else
102: sb.append("final "); // added Oct2007
103: sb.append("void set" + nameUp + "(" + field.type
104: + " a) { this." + field.name + " = a; }");
105: }
106:
107: ClipboardUtils.copyToClipboard(sb.toString().trim());
108: }
109: }
|