Skip to content

Commit

Permalink
0004943: Added custom Label class
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-miller-jumpmind committed Jul 28, 2021
1 parent 723727a commit cd26ef4
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 57 deletions.
Expand Up @@ -149,10 +149,9 @@ public static void notify(String caption, String message, Throwable ex, Notifica
HorizontalLayout layout = new HorizontalLayout();
Notification notification = new Notification(layout);

Span span = new Span();
span.getElement().setProperty("innerHTML", caption + "\n" + contactWithLineFeed(FormatUtils.wordWrap(message, 150)));
layout.add(span);
layout.setVerticalComponentAlignment(Alignment.CENTER, span);
Label label = new Label(caption + "\n" + contactWithLineFeed(FormatUtils.wordWrap(message, 150)));
layout.add(label);
layout.setVerticalComponentAlignment(Alignment.CENTER, label);

Icon closeIcon = new Icon(VaadinIcon.CLOSE_CIRCLE_O);
closeIcon.setSize("8ex");
Expand Down
Expand Up @@ -46,9 +46,7 @@ public ConfirmDialog(String caption, String text, final IConfirmListener confirm
setCloseOnOutsideClick(false);

if (caption != null) {
Span header = new Span();
header.getElement().setProperty("innerHTML", caption + "<hr>");
add(header);
add(new Label(caption + "<hr>"));
}

VerticalLayout layout = new VerticalLayout();
Expand Down
@@ -0,0 +1,125 @@
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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 org.jumpmind.vaadin.ui.common;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;

public class Label extends Span {

private static final long serialVersionUID = 1L;

private String text;

private Component leftIcon;

private Component rightIcon;

public Label() {
super();
}

public Label(String text) {
setText(text);
}

public Label(VaadinIcon icon, String text) {
setText(text);
setLeftIcon(icon);
}

public Label(String text, VaadinIcon icon) {
setText(text);
setRightIcon(icon);
}

public Label(Icon icon, String text) {
setText(text);
setLeftIcon(icon);
}

public Label(String text, Icon icon) {
setText(text);
setRightIcon(icon);
}

public Label(Component component) {
setText(component.getElement().getOuterHTML());
}

public void setLeftIcon(VaadinIcon icon) {
leftIcon = new Icon(icon);
configureIcon(leftIcon);
updateLabel();
}

public void setRightIcon(VaadinIcon icon) {
rightIcon = new Icon(icon);
configureIcon(rightIcon);
updateLabel();
}

public void setLeftIcon(Component icon) {
leftIcon = icon;
configureIcon(leftIcon);
updateLabel();
}

public void setRightIcon(Component icon) {
rightIcon = icon;
configureIcon(rightIcon);
updateLabel();
}

@Override
public String getText() {
return getElement().getProperty("innerHTML");
}

@Override
public void setText(String text) {
this.text = text;
updateLabel();
}

@Override
public void removeAll() {
super.removeAll();
text = null;
leftIcon = null;
rightIcon = null;
}

private void updateLabel() {
getElement().setProperty("innerHTML", (leftIcon != null ? leftIcon.getElement().getOuterHTML() + " " : "")
+ (text != null ? text : "") + (rightIcon != null ? " " + rightIcon.getElement().getOuterHTML() : ""));
}

private void configureIcon(Component icon) {
icon.getElement().getStyle().set("margin-top", "-4px");
if (icon != null && icon instanceof Icon) {
((Icon) icon).setSize("1em");
}
}

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

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.NotificationVariant;
import com.vaadin.flow.component.UI;
Expand Down Expand Up @@ -60,10 +59,9 @@ public NotifyDialog(String caption, String text, final Throwable ex, Notificatio

final String message = text;

final Span textSpan = new Span();
textSpan.getElement().setProperty("innerHTML", message);
messageArea.add(textSpan);
messageArea.expand(textSpan);
final Label textLabel = new Label(message);
messageArea.add(textLabel);
messageArea.expand(textLabel);

content.add(messageArea);
content.expand(messageArea);
Expand All @@ -75,13 +73,13 @@ public NotifyDialog(String caption, String text, final Throwable ex, Notificatio
if (detailsMode) {
String msg = "<pre>" + ExceptionUtils.getStackTrace(ex).trim() + "</pre>";
msg = msg.replace("\t", " ");
textSpan.getElement().setProperty("innerHTML", msg);
textLabel.setText(msg);
detailsButton.setText("Message");
messageArea.getStyle().set("margin", "0 0 0 16px");
setHeight("600px");
setWidth("1000px");
} else {
textSpan.getElement().setProperty("innerHTML", message);
textLabel.setText(message);
detailsButton.setText("Details");
messageArea.setMargin(true);
setWidth("400px");
Expand Down
Expand Up @@ -50,9 +50,7 @@ public PromptDialog(String caption, String text, String defaultValue,
setCloseOnOutsideClick(false);

if (caption != null) {
Span header = new Span();
header.getElement().setProperty("innerHTML", caption + "<hr>");
add(header);
add(new Label(caption + "<hr>"));
}

VerticalLayout layout = new VerticalLayout();
Expand Down
Expand Up @@ -53,9 +53,7 @@ public ResizableDialog(String caption) {
setResizable(true);

if (caption != null) {
Span header = new Span();
header.getElement().setProperty("innerHTML", caption + "<hr>");
super.add(header);
super.add(new Label(caption + "<hr>"));
}

content = new VerticalLayout();
Expand Down
Expand Up @@ -34,11 +34,10 @@
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.IDdlReader;
import org.jumpmind.vaadin.ui.common.CommonUiUtils;
import org.jumpmind.vaadin.ui.common.Label;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
Expand Down Expand Up @@ -87,15 +86,11 @@ public DbTree(IDbProvider databaseProvider, ISettingsProvider settingsProvider)
expandedNodes = new LinkedHashSet<DbTreeNode>();

addComponentHierarchyColumn(node -> {
HorizontalLayout layout = new HorizontalLayout();
Label label = new Label(node.getName());
if (node.getIcon() != null) {
Icon icon = new Icon(node.getIcon());
icon.setSize("16px");
layout.add(icon);
layout.setVerticalComponentAlignment(Alignment.CENTER, icon);
label.setLeftIcon(node.getIcon());
}
layout.add(new Span(node.getName()));
return layout;
return label;
});

addExpandListener(event -> {
Expand Down
Expand Up @@ -39,6 +39,7 @@

import org.apache.commons.lang3.StringUtils;
import org.jumpmind.vaadin.ui.common.CommonUiUtils;
import org.jumpmind.vaadin.ui.common.Label;
import org.jumpmind.vaadin.ui.common.TabSheet;
import org.jumpmind.vaadin.ui.common.TabSheet.EnhancedTab;
import org.slf4j.Logger;
Expand Down Expand Up @@ -389,11 +390,10 @@ protected boolean execute(final boolean runAsScript, String sqlText, final int t
final HorizontalLayout executingLayout = new HorizontalLayout();
executingLayout.setMargin(true);
executingLayout.setSizeFull();
final Span span = new Span();
span.getElement().setProperty("innerHTML", "Executing:\n\n" + StringUtils.abbreviate(sqlText, 250));
span.setEnabled(false);
executingLayout.add(span);
executingLayout.setVerticalComponentAlignment(Alignment.START, span);
final Label label = new Label("Executing:\n\n" + StringUtils.abbreviate(sqlText, 250));
label.setEnabled(false);
executingLayout.add(label);
executingLayout.setVerticalComponentAlignment(Alignment.START, label);

final String sql = sqlText;
final EnhancedTab executingTab;
Expand Down Expand Up @@ -491,7 +491,7 @@ public void finished(final VaadinIcon icon, final List<Component> results, final
final Button cancel = new Button("Cancel");
cancel.addClickListener(event -> {
log.info("Canceling sql: " + sql);
span.setText("Canceling" + span.getText().substring(9));
label.setText("Canceling" + label.getText().substring(9));
executingLayout.remove(cancel);
canceled = true;
new Thread(new Runnable() {
Expand Down
Expand Up @@ -46,6 +46,7 @@
import org.jumpmind.vaadin.ui.common.CommonUiUtils;
import org.jumpmind.vaadin.ui.common.ConfirmDialog;
import org.jumpmind.vaadin.ui.common.ConfirmDialog.IConfirmListener;
import org.jumpmind.vaadin.ui.common.Label;
import org.jumpmind.vaadin.ui.common.TabSheet.EnhancedTab;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -58,13 +59,11 @@
import com.vaadin.flow.component.grid.GridMultiSelectionModel;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.grid.contextmenu.GridContextMenu;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.Icon;
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;
import com.vaadin.flow.component.splitlayout.SplitLayout;
Expand Down Expand Up @@ -589,11 +588,8 @@ protected DbTree buildDbTree() {

}

protected HorizontalLayout createItem(String text, VaadinIcon icon) {
HorizontalLayout layout = new HorizontalLayout();
layout.add(new Icon(icon));
layout.add(new Span(text));
return layout;
protected Label createItem(String text, VaadinIcon icon) {
return new Label(icon, text);
}

protected QueryPanel getQueryPanelForDb(IDb db) {
Expand Down
Expand Up @@ -46,11 +46,11 @@
import org.jumpmind.db.sql.SqlScriptReader;
import org.jumpmind.properties.TypedProperties;
import org.jumpmind.vaadin.ui.common.CommonUiUtils;
import org.jumpmind.vaadin.ui.common.Label;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.Scroller;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
Expand Down Expand Up @@ -407,12 +407,11 @@ protected Scroller wrapTextInComponent(String text, String style) {
VerticalLayout content = new VerticalLayout();
content.setMargin(true);
panel.setContent(content);
Span span = new Span();
span.getElement().setProperty("innerHTML", "<pre>" + text.toString() + "</pre>");
Label label = new Label("<pre>" + text.toString() + "</pre>");
if (StringUtils.isNotBlank(style)) {
span.setClassName(style);
label.setClassName(style);
}
content.add(span);
content.add(label);
return panel;
}

Expand Down
Expand Up @@ -59,6 +59,7 @@
import org.jumpmind.vaadin.ui.common.CsvExport;
import org.jumpmind.vaadin.ui.common.GridDataProvider;
import org.jumpmind.vaadin.ui.common.IDataProvider;
import org.jumpmind.vaadin.ui.common.Label;
import org.jumpmind.vaadin.ui.common.NotifyDialog;
import org.jumpmind.vaadin.ui.sqlexplorer.SqlRunner.ISqlRunnerListener;
import org.slf4j.Logger;
Expand Down Expand Up @@ -145,7 +146,7 @@ public class TabularResultLayout extends VerticalLayout {

ColumnVisibilityToggler columnVisibilityToggler;

Span resultSpan;
Label resultLabel;

public TabularResultLayout(IDb db, String sql, ResultSet rs, ISqlRunnerListener listener, Settings settings, boolean showSql)
throws SQLException {
Expand Down Expand Up @@ -329,10 +330,9 @@ protected void createTabularResultLayout() {
long count = (grid.getDataProvider().fetch(new Query<>()).count());
int maxResultsSize = settings.getProperties().getInt(SQL_EXPLORER_MAX_RESULTS);
if (count >= maxResultsSize) {
resultSpan.getElement().setProperty("innerHTML",
"Limited to <span style='color: red'>" + maxResultsSize + "</span> rows;");
resultLabel.setText("Limited to <span style='color: red'>" + maxResultsSize + "</span> rows;");
} else {
resultSpan.getElement().setProperty("innerHTML", count + " rows returned;");
resultLabel.setText(count + " rows returned;");
}
} catch (SQLException ex) {
log.error(ex.getMessage(), ex);
Expand All @@ -348,8 +348,8 @@ private void createMenuBar() {

HorizontalLayout leftBar = new HorizontalLayout();
leftBar.setSpacing(true);
resultSpan = new Span("");
leftBar.add(resultSpan);
resultLabel = new Label("");
leftBar.add(resultLabel);

final Span sqlSpan = new Span("");
sqlSpan.setWidth("800px");
Expand Down

0 comments on commit cd26ef4

Please sign in to comment.