Skip to content

Commit

Permalink
Projects can now be opened from Connect-view
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Dec 8, 2015
1 parent 6489d8b commit b5e945a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
38 changes: 31 additions & 7 deletions src/main/java/com/speedment/internal/ui/UISession.java
Expand Up @@ -24,6 +24,7 @@
import com.speedment.internal.ui.resource.SpeedmentIcon;
import com.speedment.internal.logging.Logger;
import com.speedment.internal.logging.LoggerManager;
import static com.speedment.internal.ui.UISession.ReuseStage.CREATE_A_NEW_STAGE;
import com.speedment.internal.ui.controller.SceneController;
import static com.speedment.internal.ui.util.OutputUtil.error;
import static com.speedment.internal.ui.util.OutputUtil.info;
Expand All @@ -46,11 +47,11 @@
import java.util.function.Predicate;
import javafx.scene.control.Label;
import javafx.stage.FileChooser;
import static com.speedment.internal.util.TextUtil.alignRight;
import java.io.IOException;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.nio.file.Files;
import java.nio.file.Path;
import static com.speedment.internal.util.TextUtil.alignRight;
import static java.util.Objects.requireNonNull;

/**
Expand All @@ -61,6 +62,11 @@ public final class UISession {

public final static File DEFAULT_GROOVY_LOCATION = new File("src/main/groovy/speedment.groovy");

public enum ReuseStage {
USE_EXISTING_STAGE,
CREATE_A_NEW_STAGE
}

private final static Logger LOGGER = LoggerManager.getLogger(UISession.class);
private final static String DIALOG_PANE_ICON_SIZE = "48px";

Expand Down Expand Up @@ -136,8 +142,12 @@ public <T extends Event, E extends EventHandler<T>> E newProject() {
});
}

@SuppressWarnings("unchecked")
public <T extends Event, E extends EventHandler<T>> E openProject() {
return openProject(CREATE_A_NEW_STAGE);
}

@SuppressWarnings("unchecked")
public <T extends Event, E extends EventHandler<T>> E openProject(ReuseStage reuse) {
return on(event -> {
final FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open .groovy File");
Expand All @@ -155,13 +165,27 @@ public <T extends Event, E extends EventHandler<T>> E openProject() {
final Project p = Project.newProject(speedment);
GroovyParser.fromGroovy(p, file.toPath());

final Stage newStage = new Stage();
final Speedment newSpeedment = speedment.newInstance();
final UISession session = new UISession(newSpeedment, application, newStage, p);
switch (reuse) {
case CREATE_A_NEW_STAGE :
final Stage newStage = new Stage();
final Speedment newSpeedment = speedment.newInstance();
final UISession session = new UISession(newSpeedment, application, newStage, p);
SceneController.createAndShow(session);
break;

case USE_EXISTING_STAGE :
project.loadSettingsFrom(p);
SceneController.createAndShow(this);
break;

default :
throw new IllegalStateException(
"Unknown enum constant '" + reuse + "'."
);
}

SceneController.createAndShow(session);
currentlyOpenFile = file;
} catch (Exception e) {
} catch (IOException | IllegalStateException e) {
LOGGER.error(e);
log(error(e.getMessage()));
showError("Could not load project", e.getMessage(), e);
Expand Down
Expand Up @@ -48,6 +48,8 @@
* @author Emil Forslund
*/
public final class ProjectProperty extends AbstractParentProperty<Project, Child<Project>> implements Project, ChildHelper<Project, ProjectManager> {

private final static Path DEFAULT_CONFIG_PATH = Paths.get("src/main/groovy/speedment.groovy");

private final ObservableSet<Dbms> dbmsChildren;
private final ObservableSet<PluginData> pluginDataChildren;
Expand All @@ -72,13 +74,25 @@ public ProjectProperty(Speedment speedment, Project prototype) {
pluginDataChildren = copyChildrenFrom(prototype, PluginData.class, PluginDataProperty::new);
packageName = new SimpleStringProperty(prototype.getPackageName());
packageLocation = new SimpleStringProperty(prototype.getPackageLocation());
configPath = prototype.getConfigPath().orElse(Paths.get("src/main/groovy/speedment.groovy"));
configPath = prototype.getConfigPath().orElse(DEFAULT_CONFIG_PATH);
}

private void setDefaults() {
setPackageLocation("src/main/java");
setPackageName("com.company.speedment.test");
setConfigPath(Paths.get("src/main/groovy/speedment.groovy"));
setConfigPath(DEFAULT_CONFIG_PATH);
}

public void loadSettingsFrom(Project prototype) {
packageName.setValue(prototype.getPackageName());
packageLocation.setValue(prototype.getPackageLocation());
configPath = prototype.getConfigPath().orElse(DEFAULT_CONFIG_PATH);

dbmsChildren.clear();
dbmsChildren.addAll(copyChildrenFrom(prototype, Dbms.class, DbmsProperty::new));

pluginDataChildren.clear();
pluginDataChildren.addAll(copyChildrenFrom(prototype, PluginData.class, PluginDataProperty::new));
}

@Override
Expand Down
Expand Up @@ -48,6 +48,8 @@
import static com.speedment.internal.ui.controller.ToolbarController.ICON_SIZE;
import static javafx.beans.binding.Bindings.createBooleanBinding;
import static java.util.Objects.requireNonNull;
import static com.speedment.internal.ui.UISession.ReuseStage.USE_EXISTING_STAGE;
import static java.util.Objects.requireNonNull;

/**
*
Expand Down Expand Up @@ -159,6 +161,8 @@ public Number fromString(String string) {
fieldHost.setText(Settings.inst().get("last_known_host", DEFAULT_HOST));
fieldUser.setText(Settings.inst().get("last_known_user", DEFAULT_USER));
fieldName.setText(Settings.inst().get("last_known_name", DEFAULT_NAME));

buttonOpen.setOnAction(session.openProject(USE_EXISTING_STAGE));

buttonConnect.setOnAction(ev -> {
session.getProject().setName(fieldSchema.getText());
Expand Down

0 comments on commit b5e945a

Please sign in to comment.