package net.sf.fast.ibatis.dialog;
import net.sf.fast.ibatis.build.HandleType;
import net.sf.fast.ibatis.dialog.tab.MybatisTab;
import net.sf.fast.ibatis.dialog.tab.ResultTypeTab;
import net.sf.fast.ibatis.model.FastIbatisConfig;
import net.sf.fast.ibatis.util.Constants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
public class MybatisDialog extends Dialog implements IbatisDialog {
/**
* Default constructor
*
* @param parent
* the workspace shell enviroment.
*/
public MybatisDialog(Shell parent) {
super(parent);
}
public MybatisDialog(Shell parent, int style) {
super(parent, style);
}
private void centerDialog(Shell shell) {
int width = shell.getMonitor().getClientArea().width;
int height = shell.getMonitor().getClientArea().height;
int x = shell.getSize().x;
int y = shell.getSize().y;
if (x > width) {
shell.getSize().x = width;
}
if (y > height) {
shell.getSize().y = height;
}
shell.setLocation((width - x) / 2, (height - y) / 2);
}
public void open(String methodName, HandleType handleType, FastIbatisConfig fc) {
Shell parent = getParent();
final Shell shell = new Shell(parent, SWT.TITLE | SWT.BORDER
| SWT.CLOSE | SWT.RESIZE | SWT.APPLICATION_MODAL);
shell.setSize(Constants.WIDTH, Constants.HEIGHT);
shell.setText("Mybatis Fast Code Generation");
shell.setLayout(new FillLayout());
Display display = parent.getDisplay();
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER | SWT.CLOSE
| SWT.NONE);
Rectangle clientArea = shell.getClientArea();
tabFolder.setLocation(clientArea.x, clientArea.y);
final MybatisTab mybatisTab = new MybatisTab(tabFolder, SWT.BORDER, methodName, fc);
createTab("Code Generation", tabFolder, mybatisTab);
final ResultTypeTab resultTypeTab = new ResultTypeTab(tabFolder,
SWT.BORDER, fc);
createTab("Result Type", tabFolder, resultTypeTab);
tabFolder.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
}
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
});
tabFolder.pack();
centerDialog(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
private void createTab(String tabName, TabFolder tabFolder,
Composite composite) {
TabItem item = new TabItem(tabFolder, SWT.BORDER);
item.setText(tabName);
item.setToolTipText(tabName);
item.setControl(composite);
}
public static void main(String args[]) {
Display display = new Display();
final Shell shell = new Shell(display);
MybatisDialog m = new MybatisDialog(shell);
//m.test(display, shell);
FastIbatisConfig fc = new FastIbatisConfig();
fc.setXmlFileName("test");
m.open("", null, fc);
}
}