Skip to content

Commit

Permalink
0004943: Cleaned up notify() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-miller-jumpmind committed Sep 22, 2021
1 parent 237553b commit 1d06e31
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.Consumer;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.time.FastDateFormat;
Expand All @@ -64,7 +65,6 @@
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.Notification.Position;
import com.vaadin.flow.component.notification.NotificationVariant;
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.page.Page;
Expand Down Expand Up @@ -139,22 +139,18 @@ public static AceEditor createAceEditor() {
}

public static void notify(String message) {
notify("", message, NotificationVariant.LUMO_SUCCESS);
notify("", message);
}

public static void notify(String caption, String message) {
notify(caption, message, NotificationVariant.LUMO_SUCCESS);
}

public static void notify(String message, NotificationVariant type) {
notify("", message, type);

public static void notify(String message, Consumer<Boolean> shortcutToggler) {
notify("", message, shortcutToggler);
}

public static void notify(String caption, String message, NotificationVariant type) {
notify(caption, message, null, type);
public static void notify(String caption, String message) {
notify(caption, message, null);
}

public static void notify(String caption, String message, Throwable ex, NotificationVariant type) {
public static void notify(String caption, String message, Consumer<Boolean> shortcutToggler) {
Page page = UI.getCurrent().getPage();
if (page != null) {
HorizontalLayout layout = new HorizontalLayout();
Expand All @@ -178,6 +174,10 @@ public static void notify(String caption, String message, Throwable ex, Notifica
layout.add(closeIcon);
layout.setVerticalComponentAlignment(Alignment.START, closeIcon);

if (shortcutToggler != null) {
notification.addOpenedChangeListener(event -> shortcutToggler.accept(event.isOpened()));
}

notification.setPosition(Position.MIDDLE);
notification.setDuration(-1);
notification.open();
Expand All @@ -191,13 +191,21 @@ private static String contactWithLineFeed(String[] lines) {
}
return line.toString();
}

public static void notify(String message, Throwable ex) {
notify("An error occurred", message, ex, NotificationVariant.LUMO_ERROR);

public static void notifyError() {
notifyError((Consumer<Boolean>) null);
}

public static void notifyError(Consumer<Boolean> shortcutToggler) {
notify("An unexpected error occurred", "See the log file for additional details", shortcutToggler);
}

public static void notify(Throwable ex) {
notify("An unexpected error occurred", "See the log file for additional details", ex, NotificationVariant.LUMO_ERROR);
public static void notifyError(String message) {
notifyError(message, null);
}

public static void notifyError(String message, Consumer<Boolean> shortcutToggler) {
notify("An error occurred", message, shortcutToggler);
}

public static Object getObject(ResultSet rs, int i) throws SQLException {
Expand Down
Expand Up @@ -405,7 +405,7 @@ protected void exportToEditor() {
} catch (IOException e) {
String msg = "Failed to export to the sql editor";
log.error(msg, e);
CommonUiUtils.notify(msg, e);
CommonUiUtils.notifyError(msg, opened -> enableEscapeShortcut(!opened));
}
}

Expand Down Expand Up @@ -444,7 +444,7 @@ private StreamResource createResource() {
} catch (IOException e) {
String msg = "Failed to export to a file";
log.error(msg, e);
CommonUiUtils.notify(msg, e);
CommonUiUtils.notifyError(msg, opened -> enableEscapeShortcut(!opened));
}
return null;
});
Expand Down
Expand Up @@ -244,7 +244,7 @@ public OutputStream receiveUpload(String filename, String mimeType) {
return new BufferedOutputStream(new FileOutputStream(file));
} catch (Exception e) {
log.warn(e.getMessage(), e);
CommonUiUtils.notify("Failed to import " + filename, e);
CommonUiUtils.notifyError("Failed to import " + filename, opened -> enableEscapeShortcut(!opened));
}
return null;
}
Expand Down
Expand Up @@ -39,8 +39,6 @@
import org.slf4j.LoggerFactory;

import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.treegrid.TreeGrid;
import com.vaadin.flow.data.provider.hierarchy.TreeData;
import com.vaadin.flow.data.provider.hierarchy.TreeDataProvider;
Expand Down Expand Up @@ -272,7 +270,7 @@ protected void loadChildren(DbTreeNode treeNode) {
}
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
CommonUiUtils.notify(ex);
CommonUiUtils.notifyError();
}
}

Expand Down
Expand Up @@ -41,7 +41,6 @@
import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.notification.NotificationVariant;
import com.vaadin.flow.component.textfield.TextField;

public class SettingsDialog extends ResizableDialog {
Expand Down Expand Up @@ -201,11 +200,11 @@ protected boolean save() {
return true;
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
CommonUiUtils.notify(ex);
CommonUiUtils.notifyError(opened -> enableEscapeShortcut(!opened));
return false;
}
}
CommonUiUtils.notify("Save Failed", "Ensure that all fields are valid", NotificationVariant.LUMO_CONTRAST);
CommonUiUtils.notify("Save Failed", "Ensure that all fields are valid", opened -> enableEscapeShortcut(!opened));
return false;
}
}
Expand Up @@ -65,7 +65,6 @@
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.menubar.MenuBar;
import com.vaadin.flow.component.menubar.MenuBarVariant;
import com.vaadin.flow.component.notification.NotificationVariant;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.Scroller;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
Expand Down Expand Up @@ -419,7 +418,7 @@ public boolean onOk() {
db.getPlatform().dropTables(false, table);
} catch (Exception e) {
String msg = "Failed to drop " + table.getFullyQualifiedTableName() + ". ";
CommonUiUtils.notify(msg + "See log file for more details", NotificationVariant.LUMO_CONTRAST);
CommonUiUtils.notify(msg + "See log file for more details");
log.warn(msg, e);
}
}
Expand Down
Expand Up @@ -338,7 +338,7 @@ protected void createTabularResultLayout() {
}
} catch (SQLException ex) {
log.error(ex.getMessage(), ex);
CommonUiUtils.notify(ex);
CommonUiUtils.notifyError();
}

}
Expand Down

0 comments on commit 1d06e31

Please sign in to comment.