package net.sf.fast.ibatis.dialog.tab; import java.io.File; import net.sf.fast.ibatis.model.DataStorage; import net.sf.fast.ibatis.model.FastIbatisConfig; import net.sf.fast.ibatis.util.Constants; import net.sf.fast.ibatis.util.UIHelper; import net.sf.fast.ibatis.util.Utils; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.json.simple.JSONObject; import org.json.simple.JSONValue; public class MybatisTab extends Composite { /** * this is used for svn st command to list all the modified files generally, * it should wrapper in shell */ private Text mapperPatternField; private Text serviceInterfacePatternField; private Text serviceImplPatternField; private Text mapperFile; private Text serviceInterfaceFile; private Text serviceImplFile; private Text commentField; private Text retTypeField; private Text methodNameField; private Text paramTextField; private String methodName; private FastIbatisConfig config; private final Composite shell = this; public MybatisTab(Composite parent, int style) { super(parent, style); } public MybatisTab(Composite parent, int style, String methodName, FastIbatisConfig config) { super(parent, style); this.config = config; this.methodName = methodName; GridLayout gdLayout = new GridLayout(6, false); gdLayout.horizontalSpacing = 10; gdLayout.verticalSpacing = 10; this.setLayout(gdLayout); this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); init(); } private void init() { JSONObject obj = null; try { String jsonStr = DataStorage.get(Constants.PROJECT_NAME, config.getXmlFileName()); if (jsonStr != null && jsonStr.length() > 0) { obj = (JSONObject)JSONValue.parse(jsonStr); } } catch(Exception e) { } final Label label1 = new Label(this, SWT.NULL); label1.setText("Mapper:"); mapperPatternField = new Text(this, SWT.BORDER); mapperPatternField.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false)); UIHelper.updateText(this.getShell(), mapperPatternField, Utils.getJsonValue(obj, "mapperPattern", "(.*)Mapper"), false, true); final Label label2 = new Label(this, SWT.NULL); label2.setText("Srv:"); serviceInterfacePatternField = new Text(this, SWT.BORDER); serviceInterfacePatternField.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false)); UIHelper.updateText(this.getShell(), serviceInterfacePatternField, Utils.getJsonValue(obj, "srvPattern", ""), false, true); final Label label3 = new Label(this, SWT.NULL); label3.setText("Srv Impl:"); serviceImplPatternField = new Text(this, SWT.BORDER); serviceImplPatternField.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false)); UIHelper.updateText(this.getShell(), serviceImplPatternField, Utils.getJsonValue(obj, "srvImplPattern", ""), false, true); final Label label4 = new Label(this, SWT.NULL); label4.setText("Mapper File"); mapperFile = new Text(this, SWT.BORDER); mapperFile.setText(obj == null ? "" : (String)obj.get("mapperFile")); GridData data4 = new GridData(SWT.FILL, SWT.NONE, true, false); data4.horizontalSpan = 5; mapperFile.setLayoutData(data4); final Label label5 = new Label(this, SWT.NULL); label5.setText("Srv File"); serviceInterfaceFile = new Text(this, SWT.BORDER); serviceInterfaceFile.setText(Utils.getJsonValue(obj, "srvFile", "")); GridData data5 = new GridData(SWT.FILL, SWT.NONE, true, false); data5.horizontalSpan = 5; serviceInterfaceFile.setLayoutData(data5); final Label label6 = new Label(this, SWT.NULL); label6.setText("SrvImpl File"); serviceImplFile = new Text(this, SWT.BORDER); serviceImplFile.setText(Utils.getJsonValue(obj, "srvImplFile", "")); GridData data6 = new GridData(SWT.FILL, SWT.NONE, true, false); data6.horizontalSpan = 5; serviceImplFile.setLayoutData(data6); final Label label7 = new Label(this, SWT.NULL); label7.setText("Return:"); this.retTypeField = new Text(this, SWT.BORDER); GridData gd7 = new GridData(SWT.FILL, SWT.NONE, true, false); gd7.horizontalSpan = 2; retTypeField.setLayoutData(gd7); UIHelper.updateText(this.getShell(), retTypeField, "", false, true); final Label label8 = new Label(this, SWT.NULL); label8.setText("Method:"); this.methodNameField = new Text(this, SWT.BORDER); GridData gd8 = new GridData(SWT.FILL, SWT.NONE, true, false); gd8.horizontalSpan = 2; methodNameField.setLayoutData(gd8); UIHelper.updateText(this.getShell(), methodNameField, methodName, false, true); final Label label9 = new Label(this, SWT.NULL); label9.setText("Params:"); GridData gd9 = new GridData(SWT.FILL, SWT.NONE, true, false); gd9.horizontalSpan = 5; this.paramTextField = new Text(this, SWT.BORDER); paramTextField.setLayoutData(gd9); UIHelper.updateText(this.getShell(), paramTextField, "", false, true); final Label commentLabel = new Label(this, SWT.BORDER); commentLabel.setText("Comment:"); commentField = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL); UIHelper.createTextArea(this.getShell(), commentField, "", 40, 20, 5); createButton(this.getShell()); } private final boolean handleContent(Shell shell) { JSONObject o = new JSONObject(); String str1 = mapperPatternField.getText(); if (str1 == null || str1.trim().length() == 0) { MessageDialog.openWarning(shell, "Warning", "you must fill the mapper pattern"); return false; } config.setMapperPattern(str1); o.put("mapperPattern", mapperPatternField.getText()); str1 = serviceInterfacePatternField.getText(); if (str1 != null && str1.trim().length() > 0) { o.put("srvPattern", serviceInterfacePatternField.getText()); } config.setSrvPattern(str1); str1 = serviceImplPatternField.getText(); if (str1 != null && str1.trim().length() > 0) { o.put("srvImplPattern", serviceImplPatternField.getText()); } config.setSrvImplPattern(str1); String mapperFileName = Utils.getMapperFileName(config.getXmlFileName(), mapperPatternField.getText(), config); config.setMapperName(Utils.lowFirstChar(mapperFileName)); StringBuilder sb = new StringBuilder(); Utils.findFile(mapperFileName + ".java", new File(config.getProjectPath()), sb); String relativePath1 = sb.toString().replace(config.getProjectPath() + "/", ""); mapperFile.setText(relativePath1); String mapperFilePath = sb.toString(); if (mapperFilePath == null || mapperFilePath.trim().length() == 0) { MessageDialog.openWarning(shell, "Warning", "you must fill the mapper file path"); return false; } config.setMapperFilePath(mapperFilePath); File f = new File(mapperFilePath); if (!f.exists()) { MessageDialog.openWarning(shell, "Warning", "mapper file is not exist"); return false; } o.put("mapperFile", mapperFile.getText()); String modelName = config.getModelName(); String srvPattern = serviceInterfacePatternField.getText(); StringBuilder sb1 = new StringBuilder(); if (srvPattern != null && srvPattern.length() > 0) { String srvFileName = String.format(srvPattern, modelName); Utils.findFile(srvFileName + ".java", new File(config.getProjectPath()), sb1); String relativePath2 = sb1.toString().replace(config.getProjectPath() + "/", ""); serviceInterfaceFile.setText(relativePath2); } str1 = sb1.toString(); if (str1 != null && str1.trim().length() > 0) { File f1 = new File(str1); if (!f1.exists()) { MessageDialog.openWarning(shell, "Warning", "srv file is not exist"); return false; } o.put("srvFile", serviceInterfaceFile.getText()); } config.setSrvFilePath(str1); StringBuilder sb2 = new StringBuilder(); String srvImplPattern = serviceImplPatternField.getText(); if (srvImplPattern != null && srvImplPattern.length() > 0) { String srvImplFileName = String.format(srvImplPattern, modelName); Utils.findFile(srvImplFileName + ".java", new File(config.getProjectPath()), sb2); String relativePath2 = sb2.toString().replace(config.getProjectPath() + "/", ""); serviceImplFile.setText(relativePath2); } str1 = sb2.toString(); if (str1 != null && str1.trim().length() > 0) { File f1 = new File(str1); if (!f1.exists()) { MessageDialog.openWarning(shell, "Warning", "srv imple file is not exist"); return false; } o.put("srvImplFile", serviceImplFile.getText()); } config.setSrvImplFilePath(str1); DataStorage.put(Constants.PROJECT_NAME, config.getXmlFileName(), o.toJSONString()); return true; } private void createButton(final Shell shell) { GridData data = new GridData(SWT.RIGHT, SWT.NONE, true, false); data.horizontalSpan = 3; data.widthHint = 90; final Button buttonSave = new Button(this, SWT.PUSH); buttonSave.setText("Save"); buttonSave.setLayoutData(data); buttonSave.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { if (handleContent(shell)) { MessageDialog.openInformation(shell, "Status", "save successfully"); } } }); GridData data1 = new GridData(SWT.LEFT, SWT.NONE, true, false); data1.widthHint = 90; final Button btnGenerate = new Button(this, SWT.PUSH); btnGenerate.setText("Generate"); btnGenerate.setLayoutData(data1); btnGenerate.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { boolean flag = handleContent(shell); if (flag) { String str1 = retTypeField.getText(); if (str1 == null || str1.trim().length() == 0) { MessageDialog.openWarning(shell, "Warning", "you must fill the return type"); return; } config.setReturnType(str1);; str1 = methodNameField.getText(); if (str1 == null || str1.trim().length() == 0) { MessageDialog.openWarning(shell, "Warning", "you must fill the method name"); return; } config.setMethodName(str1); str1 = paramTextField.getText(); if (str1 == null || str1.trim().length() == 0) { MessageDialog.openWarning(shell, "Warning", "you must fill the params"); return; } config.setParamType(str1); if (commentField.getText() != null) { config.setMethodComment(commentField.getText()); } config.setNeedGeneration(true); shell.dispose(); } } }); } public static void main(String args[]) { StringBuilder sb = new StringBuilder(); Utils.findFile("MybatisTab.java", new File("/Users/dan/my_projects/IDE_Plugins/FastIbatis"), sb); System.out.println(sb); } }