Skip to content

Commit

Permalink
Add notifications to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Apr 2, 2016
1 parent 4265037 commit a8c8a3b
Show file tree
Hide file tree
Showing 26 changed files with 1,110 additions and 94 deletions.
15 changes: 13 additions & 2 deletions src/main/java/com/speedment/component/EventComponent.java
Expand Up @@ -19,6 +19,7 @@
import com.speedment.annotation.Api;
import com.speedment.event.DefaultEvent;
import com.speedment.event.Event;
import com.speedment.event.UIEvent;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -48,7 +49,7 @@ public default Class<? extends Component> getComponentClass() {

/**
* Listens to a particular type of event. The specified action
* will be called when the appropriate {@link #notify(com.speedment.event.Event) notify()}
* will be called when the appropriate {@link #notify(Event) notify()}
* method is called.
*
* @param <E> the event implementation
Expand All @@ -59,14 +60,24 @@ public default Class<? extends Component> getComponentClass() {

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

/**
* Listens to a particular type of event. The specified action
* will be called when the appropriate {@link #notify(Event) notify()}
* method is called.
*
* @param event the event type to listen for
* @param action the action to call
*/
void on(UIEvent event, Consumer<UIEvent> action);

/**
* Listens to all kind of events.
*
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/speedment/event/UIEvent.java
@@ -0,0 +1,39 @@
/**
*
* Copyright (c) 2006-2016, 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.event;

import com.speedment.annotation.Api;

/**
* Event that is created when the User Interface is starting. This happens when
* the {@link UISession} instance is accessible but no window have been created
* yet.
*
* @author Emil Forslund
* @since 2.3
*/
@Api(version = "2.3")
public enum UIEvent implements Event {

STARTED,
OPEN_MAIL_PROMPT_WINDOW,
OPEN_CONNECT_WINDOW,
OPEN_MAIN_WINDOW,
OPEN_COMPONENTS_WINDOW,
OPEN_ABOUT_WINDOW;

}
33 changes: 0 additions & 33 deletions src/main/java/com/speedment/event/UiStarting.java

This file was deleted.

Expand Up @@ -20,6 +20,7 @@
import com.speedment.component.EventComponent;
import com.speedment.event.DefaultEvent;
import com.speedment.event.Event;
import com.speedment.event.UIEvent;
import com.speedment.license.Software;
import java.util.Collections;
import java.util.EnumMap;
Expand All @@ -35,38 +36,49 @@
*/
public final class EventComponentImpl extends InternalOpenSourceComponent implements EventComponent {

private final Map<DefaultEvent, Set<Consumer<Event>>> defaultEventListeners;
private final Map<DefaultEvent, Set<Consumer<DefaultEvent>>> defaultEventListeners;
private final Map<UIEvent, Set<Consumer<UIEvent>>> uiEventListeners;
private final Map<Class<? extends Event>, Set<Consumer<Event>>> otherEventListeners;
private final Set<Consumer<Event>> anyEventListeners;

public EventComponentImpl(Speedment speedment) {
super(speedment);

final EnumMap<DefaultEvent, Set<Consumer<Event>>> listeners =
final EnumMap<DefaultEvent, Set<Consumer<DefaultEvent>>> defaultListeners =
new EnumMap<>(DefaultEvent.class);

for (final DefaultEvent ev : DefaultEvent.values()) {
listeners.put(ev, Collections.newSetFromMap(new ConcurrentHashMap<>()));
defaultListeners.put(ev, Collections.newSetFromMap(new ConcurrentHashMap<>()));
}

defaultEventListeners = Collections.unmodifiableMap(listeners);
defaultEventListeners = Collections.unmodifiableMap(defaultListeners);

final EnumMap<UIEvent, Set<Consumer<UIEvent>>> uiListeners =
new EnumMap<>(UIEvent.class);

for (final UIEvent ev : UIEvent.values()) {
uiListeners.put(ev, Collections.newSetFromMap(new ConcurrentHashMap<>()));
}

uiEventListeners = Collections.unmodifiableMap(uiListeners);

otherEventListeners = new ConcurrentHashMap<>();
anyEventListeners = Collections.newSetFromMap(new ConcurrentHashMap<>());
}

@Override
public void notify(Event event) {
final Set<Consumer<Event>> listeners;


if (event instanceof DefaultEvent) {
@SuppressWarnings("unchecked")
final DefaultEvent ev = (DefaultEvent) event;
listeners = defaultListeners(ev);
defaultListeners(ev).forEach(listener -> listener.accept(ev));
} else if (event instanceof UIEvent) {
final UIEvent ev = (UIEvent) event;
uiListeners(ev).forEach(listener -> listener.accept(ev));
} else {
listeners = listeners(event.getClass());
listeners(event.getClass()).forEach(listener -> listener.accept(event));
}

listeners.forEach(listener -> listener.accept(event));
anyEventListeners.forEach(listener -> listener.accept(event));
}

Expand All @@ -77,9 +89,13 @@ public <E extends Event> void on(Class<E> event, Consumer<E> action) {
}

@Override
@SuppressWarnings("unchecked")
public void on(DefaultEvent event, Consumer<DefaultEvent> action) {
defaultListeners(event).add((Consumer<Event>) (Object) action);
defaultListeners(event).add(action);
}

@Override
public void on(UIEvent event, Consumer<UIEvent> action) {
uiListeners(event).add(action);
}

@Override
Expand All @@ -99,7 +115,11 @@ private <E extends Event> Set<Consumer<Event>> listeners(Class<E> event) {
return set;
}

private Set<Consumer<Event>> defaultListeners(DefaultEvent event) {
private Set<Consumer<DefaultEvent>> defaultListeners(DefaultEvent event) {
return defaultEventListeners.get(event);
}

private Set<Consumer<UIEvent>> uiListeners(UIEvent event) {
return uiEventListeners.get(event);
}
}
12 changes: 8 additions & 4 deletions src/main/java/com/speedment/internal/ui/MainApp.java
Expand Up @@ -17,9 +17,8 @@
package com.speedment.internal.ui;

import com.speedment.Speedment;
import com.speedment.event.UiStarting;
import com.speedment.event.UIEvent;
import com.speedment.internal.core.runtime.DefaultSpeedmentApplicationLifecycle;
import com.speedment.internal.ui.resource.SpeedmentFont;
import com.speedment.internal.logging.Logger;
import com.speedment.internal.logging.LoggerManager;
import com.speedment.internal.ui.controller.ConnectController;
Expand All @@ -31,9 +30,9 @@
import javafx.application.Application;
import javafx.stage.Stage;

import static com.speedment.internal.ui.UISession.ReuseStage.USE_EXISTING_STAGE;
import static java.util.Objects.requireNonNull;
import static javafx.application.Application.launch;
import static com.speedment.internal.ui.UISession.ReuseStage.USE_EXISTING_STAGE;

/**
*
Expand All @@ -57,6 +56,11 @@ public void start(Stage stage) throws Exception {
SPEEDMENT = new DefaultSpeedmentApplicationLifecycle().build();
}

SPEEDMENT.getEventComponent().on(UIEvent.OPEN_MAIN_WINDOW, ev -> {
SPEEDMENT.getUserInterfaceComponent().getUISession()
.showNotification("Hello, World!");
});

final Parameters parameters = getParameters();
final List<String> params = parameters.getRaw();
if (params.isEmpty()) {
Expand Down Expand Up @@ -85,7 +89,7 @@ public static void main(String[] args) {
private UISession createSession(Stage stage, String configLocation) {
final UISession session = new UISession(SPEEDMENT, this, stage, configLocation);
Statistics.onGuiStarted();
SPEEDMENT.getEventComponent().notify(new UiStarting());
SPEEDMENT.getEventComponent().notify(UIEvent.STARTED);
return session;
}
}
55 changes: 51 additions & 4 deletions src/main/java/com/speedment/internal/ui/UISession.java
Expand Up @@ -33,6 +33,7 @@
import com.speedment.internal.ui.config.DbmsProperty; // Exposes internal
import com.speedment.ui.config.DocumentProperty;
import com.speedment.internal.ui.controller.ConnectController;
import com.speedment.internal.ui.controller.NotificationController;
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 Down Expand Up @@ -86,6 +87,7 @@
import static java.util.Objects.requireNonNull;
import java.util.concurrent.ExecutionException;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;

/**
*
Expand Down Expand Up @@ -291,6 +293,13 @@ public <T extends Event, E extends EventHandler<T>> E generate() {
+ "| Files generated " + alignRight("" + instance.getFilesCreated(), 30) + " |\n"
+ "+-------------------------------------------------+"
));

showNotification(
"Generation completed! " + instance.getFilesCreated() +
" files created.",
FontAwesomeIcon.STAR,
NotificationController.GREEN
);
} catch (final Exception ex) {
if (!stopwatch.isStopped()) {
stopwatch.stop();
Expand All @@ -303,9 +312,10 @@ public <T extends Event, E extends EventHandler<T>> E generate() {
+ "| Exception Type " + alignRight(ex.getClass().getSimpleName(), 30) + " |\n"
+ "+-------------------------------------------------+"
));

LOGGER.error(ex, "Error! Failed to generate code.");


final String msg = "Error! Failed to generate code. A " + ex.getClass().getSimpleName() + " was thrown.";

LOGGER.error(ex, msg);
showError("Failed to generate code", ex.getMessage(), ex);
}
});
Expand All @@ -326,6 +336,8 @@ public <T extends Event, E extends EventHandler<T>> E toggleOutput() {
public <T extends Event, E extends EventHandler<T>> E togglePreview() {
return toggle("preview", hiddenPreview, StoredNode.InsertAt.END);
}



private final static class StoredNode {

Expand Down Expand Up @@ -584,6 +596,30 @@ private void showProgressDialog(String title, ProgressMeasure progress, Completa
dialog.showAndWait();
}
}

public void showNotification(String message) {
NotificationController.showNotification(this, message);
}

public void showNotification(String message, FontAwesomeIcon icon) {
NotificationController.showNotification(this, message, icon);
}

public void showNotification(String message, Runnable action) {
NotificationController.showNotification(this, message, action);
}

public void showNotification(String message, Color color) {
NotificationController.showNotification(this, message, color);
}

public void showNotification(String message, FontAwesomeIcon icon, Color color) {
NotificationController.showNotification(this, message, icon, color, () -> {});
}

public void showNotification(String message, FontAwesomeIcon icon, Color color, Runnable action) {
NotificationController.showNotification(this, message, icon, color, action);
}

public <DOC extends DocumentProperty> boolean loadFromDatabase(DbmsProperty dbms, String schemaName) {
try {
Expand Down Expand Up @@ -612,7 +648,17 @@ public <DOC extends DocumentProperty> boolean loadFromDatabase(DbmsProperty dbms

showProgressDialog("Loading Database Metadata", progress, future);

return future.get();
final boolean status = future.get();

if (status) {
showNotification(
"Database metadata has been loaded.",
FontAwesomeIcon.DATABASE,
NotificationController.GREEN
);
}

return status;

} catch (final InterruptedException | ExecutionException ex) {
showError("Error Executing Connection Task",
Expand Down Expand Up @@ -727,6 +773,7 @@ private void saveConfigFile(File file) {
final String absolute = parent.toFile().getAbsolutePath();
Settings.inst().set("project_location", absolute);
log(success("Saved project file to '" + absolute + "'."));
showNotification("Configuration saved.", FontAwesomeIcon.SAVE, NotificationController.GREEN);
currentlyOpenFile = file;

} catch (IOException ex) {
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.speedment.SpeedmentVersion;
import com.speedment.component.Component;
import com.speedment.component.UserInterfaceComponent.Brand;
import com.speedment.event.UIEvent;
import com.speedment.internal.ui.util.Loader;
import com.speedment.internal.ui.UISession;
import com.speedment.internal.util.Trees;
Expand Down Expand Up @@ -115,5 +116,7 @@ public static void createAndShow(UISession session) {
dialog.initOwner(session.getStage());
dialog.setScene(scene);
dialog.show();

session.getSpeedment().getEventComponent().notify(UIEvent.OPEN_ABOUT_WINDOW);
}
}

0 comments on commit a8c8a3b

Please sign in to comment.