Commit 4c19757a authored by Matija Obreza's avatar Matija Obreza
Browse files

Workspace dialog

parent 69e5faf4
......@@ -10,23 +10,23 @@
*******************************************************************************/
package org.eclipse.wb.swt;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
/**
* Utility class for managing OS resources associated with SWT controls such as colors, fonts, images, etc.
......
......@@ -225,12 +225,28 @@ public class AppWindow {
* @param args
*/
public static void main(String[] args) {
Shell workspaceShell = new Shell();
WorkspaceDialog workspaceDialog = new WorkspaceDialog(workspaceShell);
Object workspacePath = workspaceDialog.open();
workspaceShell.dispose();
if (workspacePath == null) {
_log.error("No workspace selected. Exiting now!");
return;
}
_log.info("Using workspace: " + workspacePath);
System.setProperty("genesys.magic.workspace", (String) workspacePath);
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AppWindow appWindow = (AppWindow) ctx.getBean("appWindow");
appWindow.execute();
_log.info("Closing Spring context");
ctx.close();
_log.info("All tidied up.");
// Workaround for hanging derby.rawStoreDeamon
System.exit(0);
}
private void execute() {
......@@ -572,7 +588,7 @@ public class AppWindow {
}
protected CTabItem createTab(CTabFolder tabFolder, DataSourceSheet currentSheet) {
CTabItem tbtmOther = new CTabItem(tabFolder, SWT.NONE);
tbtmOther.setShowClose(true);
tbtmOther.setText(currentSheet.getSheetName());
......@@ -587,7 +603,7 @@ public class AppWindow {
sheetDisplay.dispose();
}
});
return tbtmOther;
}
......
......@@ -10,12 +10,17 @@ import org.genesys2.anno.model.Settings;
import org.genesys2.anno.parser.CsvDataSourceParser;
import org.genesys2.anno.parser.XlsxDataSourceParser;
import org.genesys2.client.oauth.GenesysClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* Main Spring configuration
......@@ -26,6 +31,9 @@ import org.springframework.context.annotation.Scope;
@ComponentScan(basePackages = { "org.genesys2.anno.gui" })
public class SpringConfig {
@Value("${genesys.magic.workspace}")
private String workspacePath;
@Bean
public DataSourceLoader dataSourceLoader() {
DataSourceLoaderImpl dataSourceLoader = new DataSourceLoaderImpl();
......@@ -67,13 +75,24 @@ public class SpringConfig {
public PushDialog pushDialog(Shell shell, int style) {
return new PushDialog(shell, style);
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public SettingsDialog settingsDialog(Shell shell, int style) {
return new SettingsDialog(shell, style);
}
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
// Need to ignore "genesys.properties" if not found
propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true);
propertyPlaceholderConfigurer.setLocations(new Resource[] { new ClassPathResource("genesys.properties"), new ClassPathResource("local.properties") });
return propertyPlaceholderConfigurer;
}
// Not a bean
public static GenesysClient createGenesysClient(OAuthSettings oauthSettings) {
GenesysClient genesysClient = new GenesysClient();
......
package org.genesys2.anno.gui;
import java.io.File;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import swing2swt.layout.BorderLayout;
public class WorkspaceDialog extends Dialog {
protected Object result;
protected Shell shlWorkspaceLauncher;
private Text txtWorkspacePath;
private Label lblWorkspaceError;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
Display display = Display.getDefault();
Shell sh = new Shell(display);
WorkspaceDialog wd = new WorkspaceDialog(sh, SWT.NONE);
Object res = wd.open();
System.err.println("Selected: " + res);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*
* @param parent
* @param style
*/
public WorkspaceDialog(Shell parent, int style) {
super(parent, style);
setText("Workspace Launcher");
}
public WorkspaceDialog(Shell parent) {
super(parent, SWT.NONE);
}
/**
* Open the dialog.
*
* @return the result
*/
public Object open() {
createContents();
shlWorkspaceLauncher.open();
shlWorkspaceLauncher.layout();
Display display = getParent().getDisplay();
while (!shlWorkspaceLauncher.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return result;
}
/**
* Create contents of the dialog.
*/
private void createContents() {
shlWorkspaceLauncher = new Shell(getParent(), SWT.CLOSE | SWT.TITLE);
shlWorkspaceLauncher.setSize(600, 168);
shlWorkspaceLauncher.setText("Workspace Launcher");
shlWorkspaceLauncher.setLayout(new BorderLayout(0, 0));
Composite composite = new Composite(shlWorkspaceLauncher, SWT.NONE);
composite.setLayoutData(BorderLayout.CENTER);
composite.setLayout(new GridLayout(3, false));
Label lblWorkspace = new Label(composite, SWT.NONE);
lblWorkspace.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblWorkspace.setText("Workspace:");
txtWorkspacePath = new Text(composite, SWT.BORDER);
txtWorkspacePath.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
doCheckPath();
}
});
txtWorkspacePath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Button btnBrowse = new Button(composite, SWT.NONE);
btnBrowse.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
doBrowseWorkspace();
}
});
btnBrowse.setText("Browse...");
new Label(composite, SWT.NONE);
lblWorkspaceError = new Label(composite, SWT.NONE);
lblWorkspaceError.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
lblWorkspaceError.setText("Cannot use selected directory as workspace!");
lblWorkspaceError.setVisible(false);
new Label(composite, SWT.NONE);
Composite composite_1 = new Composite(shlWorkspaceLauncher, SWT.NONE);
composite_1.setLayoutData(BorderLayout.SOUTH);
RowLayout rl_composite_1 = new RowLayout(SWT.HORIZONTAL);
rl_composite_1.fill = true;
rl_composite_1.wrap = false;
rl_composite_1.pack = false;
composite_1.setLayout(rl_composite_1);
Button btnCancel = new Button(composite_1, SWT.NONE);
btnCancel.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
result = null;
shlWorkspaceLauncher.dispose();
}
});
btnCancel.setText("Cancel");
Button btnOk = new Button(composite_1, SWT.NONE);
btnOk.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
doWorkspaceSelected();
}
});
btnOk.setText("OK");
Composite composite_2 = new Composite(shlWorkspaceLauncher, SWT.NONE);
composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
composite_2.setLayoutData(BorderLayout.NORTH);
FillLayout fl_composite_2 = new FillLayout(SWT.HORIZONTAL);
fl_composite_2.marginWidth = 5;
fl_composite_2.marginHeight = 10;
composite_2.setLayout(fl_composite_2);
Label lblSelectWorkspaceDirectory = new Label(composite_2, SWT.WRAP | SWT.CENTER);
lblSelectWorkspaceDirectory.setText("Select workspace directory where your configuration and databases are stored.");
}
protected void doWorkspaceSelected() {
doCheckPath();
File file = new File(txtWorkspacePath.getText());
if (checkSelectedPath(file.getAbsolutePath())) {
ensureWorkspace(file.getAbsolutePath());
result = file.getAbsolutePath();
shlWorkspaceLauncher.dispose();
} else {
result = null;
}
}
private void ensureWorkspace(String path) {
File absolutePath=new File(path);
absolutePath.mkdirs();
File localProperties = new File(absolutePath, "local.properties");
if (localProperties.exists()) {
} else {
try {
localProperties.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void doBrowseWorkspace() {
DirectoryDialog dialog = new DirectoryDialog(shlWorkspaceLauncher, SWT.OPEN);
dialog.setMessage("This is where your configuration and databases will be stored.");
dialog.setText("Select Workspace directory to use");
dialog.setFilterPath(txtWorkspacePath.getText());
String path = dialog.open();
if (path != null) {
txtWorkspacePath.setText(path);
txtWorkspacePath.setSelection(path.length());
doCheckPath();
}
}
private void doCheckPath() {
result = null;
if (checkSelectedPath(txtWorkspacePath.getText())) {
lblWorkspaceError.setVisible(false);
} else {
lblWorkspaceError.setVisible(true);
}
}
private boolean checkSelectedPath(String directoryPath) {
File path = new File(directoryPath);
File localProperties = new File(path, "local.properties");
if (path.exists()) {
System.err.println("Path exists");
// Check if workspace or blank
if (localProperties.exists()) {
System.err.println("Found local.properties");
return true;
} else if (path.list().length == 0) {
System.err.println("Path has no files");
return true;
} else {
System.err.println("Other");
return false;
}
} else {
System.err.println("Path doesnt exist");
return true;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment