You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
5.1 KiB

package geisplugin.popup.actions;
import geisplugin.Tools;
import java.sql.SQLException;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;
/**
* DE:
*
* EN:
*
*
* Program changes
* **************************************************************************************
* Date * Ticket * KonzFirm * Responsible * programmer *
* Change *
* **************************************************************************************
*
* @author huellmante
* @version 1.0
*
*/
public class GenerateReflectionGetSet extends AbstractHandler {
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
try {
if (Tools.isNotUpToDate()) {
return null;
}
} finally {
try {
if (Tools.con != null && !Tools.con.isClosed()) {
try {
Tools.con.rollback();
Tools.con.close();
} catch (final SQLException e) {
}
Tools.con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
Shell shell = HandlerUtil.getActiveShell(event);
for (Object firstElement : ((IStructuredSelection) HandlerUtil.getActiveMenuSelection(event)).toList()) {
try {
if (firstElement instanceof ICompilationUnit) {
createOutput(shell, firstElement);
} else {
MessageDialog.openInformation(shell, "Info", "Please select Java file");
}
} catch (Exception e) {
MessageDialog.openError(shell, "Fehler", "Fehler " + e.getMessage());
}
}
return null;
}
private void createOutput(final Shell shell, final Object firstElement) {
ICompilationUnit cu = (ICompilationUnit) firstElement;
String file = cu.getResource().getRawLocation().toString();
try {
String fileAsString = Tools.read(file);
int where = fileAsString.lastIndexOf('}');
String afterEnd = "";
if (where < fileAsString.length()) {
afterEnd = fileAsString.substring(where + 1);
}
fileAsString = fileAsString.substring(0, where) + "\n\tpublic void set(String fieldname, Object value) {\t\t\n\t\t\ttry {\n\t\t\t\tif (value != null) {\n\t\t\t\t\t\n\t\t\t\t\tif (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.lang.String\") ) {\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, value);\t\n\t\t\t\t\t\t\n\t\t\t\t\t} else if (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.lang.Long\") ) {\n\t\t\t\t\t\t\n\t\t\t\t\t\tif (value == null)\n\t\t\t\t\t\t\tvalue = 0;\n\t\t\t\t\t\t\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, Long.parseLong(value.toString()));\t\n\t\t\t\t\t} else if (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.lang.Integer\") ) {\n\t\t\t\t\t\tif (value.toString().trim().length() == 0)\n\t\t\t\t\t\t\tvalue = 0;\n\t\t\t\t\t\t\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, Integer.parseInt(value.toString()));\t\n\t\t\t\t\t} else if (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.lang.Double\") ) {\n\t\t\t\t\t\tif (value.toString().trim().length() == 0)\n\t\t\t\t\t\t\tvalue = 0.0;\n\t\t\t\t\t\t\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, Double.parseDouble(value.toString()));\t\n\t\t\t\t\t} else if (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.util.Date\") ) {\n\t\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, value);\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t}\n\t\t\t} catch (Exception e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t}\n\t\n\tpublic Object get(String fieldname) {\n\t\tObject result = null;\n\t\t\n\t\ttry {\n\t\t\t\n\t\t\tif (fieldname == null || fieldname.trim().length() == 0)\n\t\t\t\treturn \"\";\n\t\t\t\n\t\t\tresult = CCTools.getValueOf(this, fieldname.toLowerCase());\t\n\t\t\t\n\t\t\tif (result == null)\n\t\t\t\tresult = \"\";\n\t\t\t\n\t\t\t\n\t\t} catch (Exception e) {\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn result;\n\t}\n\t\n\tpublic String getType(String fieldname) {\n\t\tString result = null;\n\t\ttry {\n\t\t\t\n\t\t\tif (fieldname == null || fieldname.trim().length() == 0)\n\t\t\t\treturn \"\";\n\t\t\t\n\t\t\tresult = this.getClass().getField(fieldname.trim().toLowerCase()).getType().getName();\t\n\t\t} catch (Exception e) {\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn result;\n\t}" + "\n\n}" + afterEnd;
fileAsString = fileAsString.replace("public", "public").replaceFirst("private static final long serialVersionUID", "public static final long serialVersionUID");
Tools.writeNoPathCreation(file, fileAsString);
Tools.getCurrentSelectedProject().refreshLocal(IResource.DEPTH_INFINITE, null);
} catch (final Exception e) {
MessageDialog.openError(shell, "Error", "Error!\n " + e.getMessage());
}
}
}