Skip to content

Commit

Permalink
Add event hooks for plugins to react to GUI events
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Nov 11, 2015
1 parent 2711544 commit 5b72e94
Show file tree
Hide file tree
Showing 29 changed files with 475 additions and 154 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/speedment/Speedment.java
Expand Up @@ -21,6 +21,7 @@
import com.speedment.component.ConnectionPoolComponent;
import com.speedment.component.DbmsHandlerComponent;
import com.speedment.component.EntityManager;
import com.speedment.component.EventComponent;
import com.speedment.component.JavaTypeMapperComponent;
import com.speedment.component.LoggerFactoryComponent;
import com.speedment.component.ManagerComponent;
Expand Down Expand Up @@ -65,6 +66,7 @@ public interface Speedment {
* <li>{@link com.speedment.component.StreamSupplierComponent StreamSupplierComponent}</li>
* <li>{@link com.speedment.component.TypeMapperComponent TypeMapperComponent}</li>
* <li>{@link com.speedment.component.PluginComponent PluginComponent}</li>
* <li>{@link com.speedment.component.EventComponent EventComponent}</li>
* </ul>
*
* @param <R> The intended return type
Expand Down Expand Up @@ -162,4 +164,8 @@ default TypeMapperComponent getTypeMapperComponent() {
default PluginComponent getPluginComponent() {
return get(PluginComponent.class);
}

default EventComponent getEventComponent() {
return get(EventComponent.class);
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/speedment/component/ComponentBuilder.java
Expand Up @@ -21,9 +21,9 @@

/**
*
* @author Emil Forslund
* @param <C>
* @since 2.3
* @author Emil Forslund
* @param <C> the component type to build
* @since 2.3
*/
@Api(version="2.3")
public interface ComponentBuilder<C extends Component> {
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/speedment/component/EventComponent.java
@@ -0,0 +1,65 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.component;

import com.speedment.annotation.Api;
import com.speedment.event.DefaultEvent;
import com.speedment.event.Event;
import java.util.function.Consumer;

/**
* An event bus that is used to let Speedment plugins know when different stages of
* execution has been reached.
*
* @author Emil Forslund
* @since 2.3
* @see Event
* @see DefaultEvent
*/
@Api(version="2.3")
public interface EventComponent extends Component {

@Override
public default Class<? extends Component> getComponentClass() {
return EventComponent.class;
}

/**
* Notifies all listeners for this type of event.
*
* @param event the event that happened
*/
void notify(Event event);

/**
* Listens to a particular type of event. The specified action
* will be called when the appropriate {@link #notify(com.speedment.event.Event) notify()}
* method is called.
*
* @param <E> the event implementation
* @param event the event that happened
* @param action the action to call
*/
<E extends Event> void on(E event, Consumer<E> action);

/**
* Listens to all kind of events.
*
* @param action the action to perform on the event
*/
void onAny(Consumer<Event> action);
}
16 changes: 9 additions & 7 deletions src/main/java/com/speedment/config/ForeignKey.java
Expand Up @@ -16,14 +16,15 @@
*/
package com.speedment.config;

import com.speedment.Speedment;
import com.speedment.annotation.Api;
import com.speedment.config.aspects.Parent;
import com.speedment.config.aspects.Child;
import com.speedment.config.aspects.Enableable;
import com.speedment.internal.core.config.ForeignKeyImpl;
import groovy.lang.Closure;
import static java.util.Objects.requireNonNull;
import java.util.function.Supplier;
import java.util.function.Function;

/**
*
Expand All @@ -37,7 +38,7 @@ public interface ForeignKey extends Node, Enableable, Child<Table>, Parent<Forei
*/
enum Holder {
HOLDER;
private Supplier<ForeignKey> provider = ForeignKeyImpl::new;
private Function<Speedment, ForeignKey> provider = ForeignKeyImpl::new;
}

/**
Expand All @@ -46,19 +47,20 @@ enum Holder {
*
* @param provider the new constructor
*/
static void setSupplier(Supplier<ForeignKey> provider) {
static void setSupplier(Function<Speedment, ForeignKey> provider) {
Holder.HOLDER.provider = requireNonNull(provider);
}

/**
* Creates a new instance implementing this interface by using the class
* supplied by the default factory. To change implementation, please use the
* {@link #setSupplier(java.util.function.Supplier) setSupplier} method.
* {@link #setSupplier(java.util.function.Function) setSupplier} method.
*
* @return the new instance
* @param speedment the speedment instance
* @return the new instance
*/
static ForeignKey newForeignKey() {
return Holder.HOLDER.provider.get();
static ForeignKey newForeignKey(Speedment speedment) {
return Holder.HOLDER.provider.apply(speedment);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/speedment/config/Index.java
Expand Up @@ -16,6 +16,7 @@
*/
package com.speedment.config;

import com.speedment.Speedment;
import com.speedment.annotation.Api;
import com.speedment.annotation.External;
import com.speedment.config.aspects.Parent;
Expand All @@ -24,7 +25,7 @@
import com.speedment.internal.core.config.IndexImpl;
import groovy.lang.Closure;
import static java.util.Objects.requireNonNull;
import java.util.function.Supplier;
import java.util.function.Function;

/**
*
Expand All @@ -38,7 +39,7 @@ public interface Index extends Node, Enableable, Child<Table>, Parent<IndexColum
*/
enum Holder {
HOLDER;
private Supplier<Index> provider = IndexImpl::new;
private Function<Speedment, Index> provider = IndexImpl::new;
}

/**
Expand All @@ -47,19 +48,20 @@ enum Holder {
*
* @param provider the new constructor
*/
static void setSupplier(Supplier<Index> provider) {
static void setSupplier(Function<Speedment, Index> provider) {
Holder.HOLDER.provider = requireNonNull(provider);
}

/**
* Creates a new instance implementing this interface by using the class
* supplied by the default factory. To change implementation, please use the
* {@link #setSupplier(java.util.function.Supplier) setSupplier} method.
* {@link #setSupplier(java.util.function.Function) setSupplier} method.
*
* @return the new instance
* @param speedment the speedment instance
* @return the new instance
*/
static Index newIndex() {
return Holder.HOLDER.provider.get();
static Index newIndex(Speedment speedment) {
return Holder.HOLDER.provider.apply(speedment);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/speedment/config/Node.java
Expand Up @@ -159,4 +159,15 @@ default <T extends Parent<?>> String getRelativeName(final Class<T> from) {
* @return the main interface class
*/
Class<?> getInterfaceMainClass();

/**
* Returns a path to the icon to use for this node in the GUI. If empty, the
* gui will attempt to load one of the predefined icons. Make sure that any
* returned path exists in the context the application is running in!
*
* @return the path to the icon to load or empty to use a predefined one
*/
default Optional<String> getIconPath() {
return Optional.empty();
}
}
29 changes: 3 additions & 26 deletions src/main/java/com/speedment/config/PluginData.java
Expand Up @@ -19,7 +19,6 @@
import com.speedment.HasSpeedment;
import com.speedment.Speedment;
import com.speedment.annotation.Api;
import com.speedment.annotation.External;
import com.speedment.component.PluginComponent;
import com.speedment.config.aspects.Child;
import com.speedment.config.aspects.Enableable;
Expand Down Expand Up @@ -86,41 +85,19 @@ default Class<Project> getParentInterfaceMainClass() {
return Project.class;
}

/**
* Sets the name of the {@link Plugin} that should be used to manage
* this node.
* <p>
* This property is editable in the GUI through reflection.
*
* @param pluginName the name as returned by {@link Plugin#getName()}
*/
@External(type = String.class)
void setPluginName(String pluginName);

/**
* Returns the name of the {@link Plugin} that should be used to manage
* this node.
* <p>
* This property is editable in the GUI through reflection.
*
* @return the name as returned by {@link Plugin#getName()}
*/
@External(type = String.class)
String getPluginName();

/**
* Contacts the {@link PluginComponent} to find a {@link Plugin} that
* matches the name specified by {@link #getPluginName()}.
* matches the name specified by {@link #getName()}.
*
* @return the plugin
* @throws SpeedmentException if the plugin could not be found
*/
default Plugin findPlugin() throws SpeedmentException {
return getSpeedment()
.getPluginComponent()
.get(getPluginName())
.get(getName())
.orElseThrow(() -> new SpeedmentException(
"Could not find plugin '" + getPluginName() + "'."
"Could not find plugin '" + getName() + "'."
));
}

Expand Down
12 changes: 2 additions & 10 deletions src/main/java/com/speedment/config/Project.java
Expand Up @@ -91,11 +91,7 @@ default Class<ProjectManager> getParentInterfaceMainClass() {
* @param speedment the {@link Speedment} instance
* @return the newly added child
*/
default Dbms addNewDbms(Speedment speedment) {
final Dbms e = Dbms.newDbms(speedment);
add(e);
return e;
}
Dbms addNewDbms(Speedment speedment);

/**
* Creates and adds a new {@link PluginData} as a child to this node in the
Expand All @@ -104,11 +100,7 @@ default Dbms addNewDbms(Speedment speedment) {
* @param speedment the {@link Speedment} instance
* @return the newly added child
*/
default PluginData addNewPluginData(Speedment speedment) {
final PluginData e = PluginData.newPluginData(speedment);
add(e);
return e;
}
PluginData addNewPluginData(Speedment speedment);

/**
* Returns the name of the generated package where this project will be
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/speedment/config/Schema.java
Expand Up @@ -16,6 +16,7 @@
*/
package com.speedment.config;

import com.speedment.Speedment;
import com.speedment.annotation.Api;
import com.speedment.annotation.External;
import com.speedment.config.aspects.Parent;
Expand All @@ -28,7 +29,7 @@
import groovy.lang.Closure;
import static java.util.Objects.requireNonNull;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.function.Function;

/**
*
Expand All @@ -45,7 +46,7 @@ public interface Schema extends Node, Enableable, Child<Dbms>, Parent<Table>,
*/
enum Holder {
HOLDER;
private Supplier<Schema> provider = SchemaImpl::new;
private Function<Speedment, Schema> provider = SchemaImpl::new;
}

/**
Expand All @@ -54,19 +55,20 @@ enum Holder {
*
* @param provider the new constructor
*/
static void setSupplier(Supplier<Schema> provider) {
static void setSupplier(Function<Speedment, Schema> provider) {
Holder.HOLDER.provider = requireNonNull(provider);
}

/**
* Creates a new instance implementing this interface by using the class
* supplied by the default factory. To change implementation, please use the
* {@link #setSupplier(java.util.function.Supplier) setSupplier} method.
* {@link #setSupplier(java.util.function.Function) setSupplier} method.
*
* @param speedment the speedment instance
* @return the new instance
*/
static Schema newSchema() {
return Holder.HOLDER.provider.get();
static Schema newSchema(Speedment speedment) {
return Holder.HOLDER.provider.apply(speedment);
}

/**
Expand Down

0 comments on commit 5b72e94

Please sign in to comment.