Skip to content

Commit

Permalink
Fix #108 by making sure Dbms::getTypeName is always set
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Jan 20, 2016
1 parent b48474e commit 2e4cfbf
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/speedment/config/db/Schema.java
Expand Up @@ -52,12 +52,12 @@ public interface Schema extends

/**
* Returns {@code true} if this schema is the default one, else
* {@code false}.
* {@code false}. Default value is {@code true}.
*
* @return {@code true} if default, else {@code false}
*/
default boolean isDefaultSchema() {
return getAsBoolean(DEFAULT_SCHEMA).orElse(false);
return getAsBoolean(DEFAULT_SCHEMA).orElse(true);
}

default Stream<? extends Table> tables() {
Expand Down
Expand Up @@ -263,13 +263,11 @@ public final void removeListener(InvalidationListener listener) {

@Override
public final StringProperty stringPropertyOf(String key, Supplier<String> ifEmpty) {
System.out.println("Requesting string property of key: '" + key + "'.");
return (StringProperty) properties.computeIfAbsent(key, k -> prepare(k, new SimpleStringProperty(getAsString(k).orElseGet(ifEmpty))));
}

@Override
public final IntegerProperty integerPropertyOf(String key, IntSupplier ifEmpty) {
System.out.println("Requesting int property of key: '" + key + "'.");
return (IntegerProperty) properties.computeIfAbsent(key, k -> prepare(k, new SimpleIntegerProperty(getAsInt(k).orElseGet(ifEmpty))));
}

Expand Down Expand Up @@ -391,15 +389,13 @@ public final void invalidate() {
}

private <T, P extends Property<T>> P prepare(String key, P property) {
System.out.println("Preparing key: '" + key + "'");

// property.addListener((ObservableValue<? extends T> observable, T oldValue, T newValue) -> {
// monitor.runWithoutGeneratingEvents(() ->
// config.put(key, newValue)
// );
//
// invalidate();
// });
property.addListener((ObservableValue<? extends T> observable, T oldValue, T newValue) -> {
monitor.runWithoutGeneratingEvents(() ->
config.put(key, newValue)
);

invalidate();
});

return property;
}
Expand Down Expand Up @@ -447,11 +443,9 @@ public void runWithoutGeneratingEvents(Runnable runnable) {
public <T> T runWithoutGeneratingEvents(Supplier<T> runnable) {
final T result;
synchronized (silence) {
System.out.println("Entering monitor");
silence.set(true);
result = runnable.get();
silence.set(false);
System.out.println("Leaving monitor");
}

return result;
Expand Down
Expand Up @@ -28,6 +28,7 @@
import com.speedment.internal.ui.property.DefaultStringPropertyItem;
import com.speedment.internal.ui.property.IntegerPropertyItem;
import static com.speedment.internal.util.document.DocumentUtil.toStringHelper;
import static com.speedment.util.NullUtil.requireKeys;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
Expand All @@ -37,7 +38,6 @@
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.controlsfx.control.PropertySheet;

Expand All @@ -49,8 +49,7 @@ public final class DbmsProperty extends AbstractChildDocumentProperty<Project>
implements Dbms, HasEnabledProperty, HasNameProperty {

public DbmsProperty(Project parent, Map<String, Object> data) {
super(parent, data);
FXCollections.observableArrayList();
super(parent, requireKeys(data, Dbms.TYPE_NAME));
}

@Override
Expand Down
Expand Up @@ -27,6 +27,7 @@
import com.speedment.internal.ui.config.trait.HasOrdinalPositionProperty;
import com.speedment.internal.ui.property.StringPropertyItem;
import static com.speedment.internal.util.document.DocumentUtil.toStringHelper;
import static com.speedment.util.NullUtil.requireKeys;
import java.util.Map;
import java.util.stream.Stream;
import static javafx.beans.binding.Bindings.createObjectBinding;
Expand All @@ -42,7 +43,7 @@ public final class ForeignKeyColumnProperty extends AbstractChildDocumentPropert
implements ForeignKeyColumn, HasNameProperty, HasOrdinalPositionProperty, HasColumnProperty {

public ForeignKeyColumnProperty(ForeignKey parent, Map<String, Object> data) {
super(parent, data);
super(parent, requireKeys(data, ForeignKeyColumn.FOREIGN_COLUMN_NAME, ForeignKeyColumn.FOREIGN_TABLE_NAME));
}

@Override
Expand Down
Expand Up @@ -17,13 +17,15 @@
package com.speedment.internal.ui.config;

import com.speedment.Speedment;
import com.speedment.config.db.Dbms;
import com.speedment.config.db.Project;
import static com.speedment.config.db.Project.CONFIG_PATH;
import static com.speedment.config.db.Project.DEFAULT_PROJECT_NAME;
import static com.speedment.config.db.Project.PACKAGE_LOCATION;
import static com.speedment.config.db.Project.PACKAGE_NAME;
import static com.speedment.config.db.trait.HasName.NAME;
import com.speedment.exception.SpeedmentException;
import com.speedment.internal.core.config.dbms.StandardDbmsType;
import com.speedment.internal.ui.config.trait.HasEnabledProperty;
import com.speedment.internal.ui.config.trait.HasNameProperty;
import com.speedment.internal.ui.property.DefaultStringPropertyItem;
Expand Down Expand Up @@ -132,7 +134,10 @@ public Stream<DbmsProperty> dbmses() {

@Override
public DbmsProperty addNewDbms() {
final DbmsProperty created = new DbmsProperty(this, new ConcurrentHashMap<>());
final Map<String, Object> defaultMap = new ConcurrentHashMap<>();
defaultMap.put(Dbms.TYPE_NAME, StandardDbmsType.defaultType().getName());

final DbmsProperty created = new DbmsProperty(this, defaultMap);
dbmsesProperty().add(created);
return created;
}
Expand Down Expand Up @@ -168,6 +173,5 @@ public String getName() throws SpeedmentException {
@Override
public String toString() {
return toStringHelper(this);
}

}
}
Expand Up @@ -18,6 +18,7 @@

import com.speedment.config.db.parameters.DbmsType;
import com.speedment.exception.SpeedmentException;
import com.speedment.internal.core.config.dbms.StandardDbmsType;
import com.speedment.internal.ui.config.DbmsProperty;
import com.speedment.internal.ui.util.Loader;
import com.speedment.internal.ui.UISession;
Expand Down Expand Up @@ -89,6 +90,8 @@ public void initialize(URL location, ResourceBundle resources) {
.map(DbmsType::getName)
.collect(Collectors.toCollection(FXCollections::observableArrayList))
);

fieldType.getSelectionModel().select(StandardDbmsType.defaultType().getName());

fieldType.getSelectionModel().selectedItemProperty().addListener((observable, old, next) -> {
if (!observable.getValue().isEmpty()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/speedment/internal/ui/util/Loader.java
Expand Up @@ -54,8 +54,8 @@ public static <T extends Initializable> Parent create(UISession session, String
final Parent loaded = loader.load();
consumer.accept(control);
return loaded;
} catch (Exception ex) { // TODO IOException
throw new SpeedmentException(ex);
} catch (final IOException ex) {
throw new SpeedmentException("Failed to load FXML file '" + filename + "'.", ex);
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/speedment/util/NullUtil.java
Expand Up @@ -19,6 +19,7 @@
import com.speedment.annotation.Api;
import static com.speedment.util.StaticClassUtil.instanceNotAllowed;
import java.util.Collection;
import java.util.Map;
import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -219,6 +220,41 @@ public static void requireNonNulls(Object o0, Object o1, Object o2, Object o3, O
throwNpeFor(8, 7);
}
}

public static <K, V> Map<K, V> requireKeys(Map<K, V> map, K... requiredKeys) {
requireNonNulls(map, requiredKeys);

for (final K key : requiredKeys) {
if (key == null || !map.containsKey(key)) {
throw new NullPointerException("Key " + key + " in the map is not defined.");
}
}

return map;
}

public static <K, V> Map<K, V> requireKeys(Map<K, V> map, K requiredKey) {
requireNonNull(map);

if (requiredKey == null || !map.containsKey(requiredKey)) {
throw new NullPointerException("Key " + requiredKey + " in the map is not defined.");
}

return map;
}

public static <K, V> Map<K, V> requireKeys(Map<K, V> map, K requiredKeyA, K requiredKeyB) {
requireKeys(map, requiredKeyA);
requireKeys(map, requiredKeyB);
return map;
}

public static <K, V> Map<K, V> requireKeys(Map<K, V> map, K requiredKeyA, K requiredKeyB, K requiredKeyC) {
requireKeys(map, requiredKeyA);
requireKeys(map, requiredKeyB);
requireKeys(map, requiredKeyC);
return map;
}

private static void throwNpeFor(int count, int i) {
if (count == 1) {
Expand Down

0 comments on commit 2e4cfbf

Please sign in to comment.