java - My Code Generator Won't Work Properly -
so i'm trying create code generator generate setter , getter methods if input variable names like: "int nums" or "string words" etc.
my problem can take 1 input
if set more 1 variable give me this:
input
int nums string words
output
public int set nums string() { homecoming nums string; } public int nums string() { homecoming nums string; }
my approach create 2 text files templates static , nonstatic methods, illustration this:
public <<variable>> set <<name>>() { homecoming <<name>>; } public <<variable>> <<name>>() { homecoming <<name>>; }
i know setter isn't supposed homecoming anything, trying first step work before move on.
then store these in string , input user , utilize input replace <> , <> elements of string. here code note: using swing create gui
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*; public class codegenerator { //private variables , methods private boolean checked = false; private string statictemplate = null; private string nonstatictemplate = null; private arraylist<string> input = new arraylist<string>(); /** * replace parts of text documents variable type , name , repeat of variables in text area. * set result in output text area user re-create , paste */ public static void replace(arraylist<string> inputs, string methods, jtextarea output) { for(int = 0; < inputs.size(); i+=2) { methods = methods.replaceall("<<variable>>", inputs.get(i)); } for(int = 1; < inputs.size(); i+=2) { methods = methods.replaceall("<<name>>", inputs.get(i)); } output.settext(methods); } private jframe frame; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { seek { codegenerator window = new codegenerator(); window.frame.setvisible(true); } grab (exception e) { e.printstacktrace(); } } }); } /** * create application. */ public codegenerator() { initialize(); } /** * initialize contents of frame. */ private void initialize() { frame = new jframe(); frame.setresizable(false); frame.setbounds(100, 100, 526, 617); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(null); //this place generated code output jtextarea generatedcodearea = new jtextarea(); generatedcodearea.seteditable(true); generatedcodearea.setbounds(10, 140, 490, 367); frame.getcontentpane().add(generatedcodearea); //this text entered jtextarea inputtextarea = new jtextarea(); inputtextarea.setbounds(10, 11, 490, 123); frame.getcontentpane().add(inputtextarea); //this checkbox static , non static setter/getter jcheckbox nonstatic = new jcheckbox("non-static setter/getter"); nonstatic.setbounds(20, 514, 150, 23); frame.getcontentpane().add(nonstatic); nonstatic.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { //set checked variable utilize in action event generate button boolean selected = nonstatic.isselected(); if(selected == true) { checked = true; }else if(selected == false) { checked = false; } } }); //generate button jbutton btngenerate = new jbutton("generate"); btngenerate.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { generatedcodearea.settext(""); //store tempaltes strings edited later seek { statictemplate = new scanner(new file("staticgettersetter.txt")).usedelimiter("\\z").next(); nonstatictemplate = new scanner(new file("nonstaticgettersetter.txt")).usedelimiter("\\z").next(); } catch(ioexception q) { generatedcodearea.settext("something went wrong... " + q.getmessage()); } //store input string array string[] temp = inputtextarea.gettext().split(" "); //then add together arraylist for(string word : temp) { input.add(word); } //check see if checkbox checked or not if(checked == true) { //do non-static setter/getter --> works replace(input, statictemplate, generatedcodearea); }else if(checked == false) { //do static setter/getter -- works replace(input, nonstatictemplate, generatedcodearea); } } }); btngenerate.setbounds(215, 518, 179, 49); frame.getcontentpane().add(btngenerate); } private static void addpopup(component component, final jpopupmenu popup) { component.addmouselistener(new mouseadapter() { public void mousepressed(mouseevent e) { if (e.ispopuptrigger()) { showmenu(e); } } public void mousereleased(mouseevent e) { if (e.ispopuptrigger()) { showmenu(e); } } private void showmenu(mouseevent e) { popup.show(e.getcomponent(), e.getx(), e.gety()); } }); } }
how prepare issue?
the cause of problem is...
string[] temp = inputtextarea.gettext().split(" ");
if utilize input of
int nums string words
then end array of like...
[0] int [1] nums\nstring [2] words
...which isn't want...
you first need split input lines...
string[] lines = inputtextarea.gettext().split("\n");
then, each line, split words...
for (string line : lines) { string[] temp = line.split(" "); }
i either utilize output.append(methods)
or alter replace
method homecoming string
...
ps: know might "personal" project but, should avoid using null
layouts, pixel perfect layouts illusion within modern ui design. there many factors impact individual size of components, none of can control. swing designed work layout managers @ core, discarding these lead no end of issues , problems spend more , more time trying rectify.
see why frowned upon utilize null layout in swing? more details...
java arrays swing file-io jtextarea
No comments:
Post a Comment