Example usage for org.apache.commons.lang3 StringUtils abbreviate

List of usage examples for org.apache.commons.lang3 StringUtils abbreviate

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils abbreviate.

Prototype

public static String abbreviate(final String str, final int maxWidth) 

Source Link

Document

Abbreviates a String using ellipses.

Usage

From source file:org.onexus.website.api.pages.browser.BrowserPage.java

@Override
protected void onBeforeRender() {

    VisibleTabs visibleTabs = new VisibleTabs();

    // Tabs can change when we fix/unfix entities
    addOrReplace(new ListView<TabGroup>("tabs", visibleTabs) {

        @Override//from  w ww .  jav a 2 s  .com
        protected void populateItem(ListItem<TabGroup> item) {

            TabGroup tabGroup = item.getModelObject();

            if (tabGroup.hasSubMenu()) {

                WebMarkupContainer link = new WebMarkupContainer("link");
                link.add(new AttributeModifier("class", "dropdown-toggle"));
                link.add(new AttributeModifier("data-toggle", "dropdown"));
                link.add(new AttributeModifier("href", "#"));

                if (tabGroup.containsTab(getStatus().getCurrentTabId())) {
                    item.add(new AttributeModifier("class", "dropdown active"));

                    TabConfig currentTab = getConfig().getTab(getStatus().getCurrentTabId());
                    String tabTitle = StringUtils.abbreviate(currentTab.getTitle(), 14);
                    String currentLabel = "<div style=\"position:relative;\"><div style=\"white-space: nowrap; position:absolute; left:13px; top: 13px; font-size: 9px;\"><em>"
                            + tabTitle + "</em></div></div>";
                    link.add(new Label("label",
                            currentLabel + "<b class='caret'></b>&nbsp;" + tabGroup.getGroupLabel())
                                    .setEscapeModelStrings(false));

                } else {
                    item.add(new AttributeModifier("class", "dropdown"));
                    link.add(new Label("label", "<b class='caret'></b>&nbsp;" + tabGroup.getGroupLabel())
                            .setEscapeModelStrings(false));
                }
                item.add(link);

                WebMarkupContainer submenu = new WebMarkupContainer("submenu");
                submenu.add(new ListView<TabConfig>("item", tabGroup.getTabConfigs()) {

                    @Override
                    protected void populateItem(ListItem<TabConfig> item) {

                        TabConfig tab = item.getModelObject();

                        BrowserPageLink<String> link = new BrowserPageLink<String>("link",
                                Model.of(tab.getId())) {

                            @Override
                            public void onClick(AjaxRequestTarget target) {
                                getStatus().setCurrentTabId(getModelObject());
                                sendEvent(EventTabSelected.EVENT);
                            }

                        };

                        if (!Strings.isEmpty(tab.getHelp())) {
                            item.add(new AttributeModifier("rel", "tooltip"));
                            item.add(new AttributeModifier("data-placement", "right"));
                            item.add(new AttributeModifier("title", tab.getHelp()));
                        }

                        link.add(new Label("label", tab.getTitle()));
                        item.add(link);

                        if (isCurrentTab(tab.getId())) {
                            item.add(new AttributeModifier("class", new Model<String>("active")));
                        }

                    }
                });
                item.add(submenu);

            } else {

                TabConfig tab = tabGroup.getTabConfigs().get(0);
                BrowserPageLink<String> link = new BrowserPageLink<String>("link", Model.of(tab.getId())) {

                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        getStatus().setCurrentTabId(getModelObject());
                        sendEvent(EventTabSelected.EVENT);
                    }

                };

                if (!Strings.isEmpty(tab.getHelp())) {
                    link.add(new AttributeModifier("rel", "tooltip"));
                    link.add(new AttributeModifier("title", tab.getHelp()));
                }

                link.add(new Label("label", tab.getTitle()));
                item.add(link);

                if (isCurrentTab(tab.getId())) {
                    item.add(new AttributeModifier("class", new Model<String>("active")));
                }

                item.add(new WebMarkupContainer("submenu").setVisible(false));

            }

        }

    });

    // Check if the current selected tab is visible.
    String currentTabId = getStatus().getCurrentTabId();

    // Set a current tab if there is no one.
    if (currentTabId == null) {

        List<TabConfig> tabs = getConfig().getTabs();
        if (tabs != null && !tabs.isEmpty()) {
            currentTabId = tabs.get(0).getId();
            getStatus().setCurrentTabId(currentTabId);

            // Init currentPage
            //TODO This is not a good solution
            PageParameters pageParameters = getPage().getPageParameters();
            pageParameters.set(Website.PARAMETER_CURRENT_PAGE, getConfig().getId());
            pageParameters.set("ptab", currentTabId);
            setResponsePage(Website.class, pageParameters);
        }

    }

    List<TabGroup> tabs = visibleTabs.getObject();
    boolean hiddenTab = true;
    for (TabGroup tab : tabs) {
        if (tab.containsTab(currentTabId)) {
            hiddenTab = false;
        }
    }

    if (hiddenTab && !tabs.isEmpty()) {
        TabConfig firstTab = tabs.get(0).getTabConfigs().get(0);
        getStatus().setCurrentTabId(firstTab.getId());
    }

    List<String> views = new ArrayList<String>();
    if (getCurrentTab().getViews() != null) {
        for (ViewConfig view : getCurrentTab().getViews()) {
            views.add(view.getTitle());
        }
    }

    if (getStatus().getCurrentView() == null && views.size() > 0) {
        getStatus().setCurrentView(views.get(0));
    }

    // Check that the current view is visible
    List<ViewConfig> filteredViews = new ArrayList<ViewConfig>();
    VisiblePredicate predicate = new VisiblePredicate(getStatus().getORI().getParent(),
            getStatus().getEntitySelections());
    CollectionUtils.select(getCurrentTab().getViews(), predicate, filteredViews);

    boolean visibleView = false;
    String currentView = getStatus().getCurrentView();
    for (ViewConfig view : filteredViews) {
        if (view.getTitle().equals(currentView)) {
            visibleView = true;
            break;
        }
    }

    if (!visibleView) {
        getStatus().setCurrentView(filteredViews.get(0).getTitle());
    }

    ViewConfig viewConfig = null;
    if (getCurrentTab().getViews() != null) {
        for (ViewConfig view : getCurrentTab().getViews()) {
            if (view.getTitle().equals(getStatus().getCurrentView())) {
                viewConfig = view;
                break;
            }
        }
    }

    if (viewConfig == null) {
        addOrReplace(new EmptyPanel("content"));
    } else {
        if (viewConfig.getLeft() != null && viewConfig.getTop() != null) {
            addOrReplace(new TopleftLayout("content", viewConfig, getModel()));
        } else if (viewConfig.getLeft() != null) {
            addOrReplace(new LeftMainLayout("content", viewConfig, getModel()));
        } else if (viewConfig.getTop() != null || viewConfig.getTopRight() != null) {
            addOrReplace(new TopmainLayout("content", viewConfig, getModel()));
        } else {
            addOrReplace(new SingleLayout("content", viewConfig, getModel()));
        }
    }

    boolean visible = !isEmbed();
    get("selection").setVisible(visible);
    get("tabs").setVisible(visible);
    //TODO viewSelector.setVisible(visible && views.size() > 1);

    super.onBeforeRender();
}

From source file:org.onexus.website.api.pages.search.boxes.FieldsPanel.java

public FieldsPanel(String id, String labelField, IEntity entity) {
    super(id);/*from   ww  w  .  j  av a2  s. c o  m*/

    Collection collection = entity.getCollection();

    // Fields values
    RepeatingView fields = new RepeatingView("fields");
    for (Field field : collection.getFields()) {

        if (labelField != null && labelField.equals(field.getId())) {
            continue;
        }

        Object value = entity.get(field.getId());
        if (value != null && !StringUtils.isEmpty(value.toString())) {

            WebMarkupContainer fc = new WebMarkupContainer(fields.newChildId());
            fc.setRenderBodyOnly(true);
            fc.add(new Label("label", field.getLabel()).add(new AttributeModifier("title", field.getTitle())));
            fc.add(new Label("value", StringUtils.abbreviate(value.toString(), 50)));
            fields.add(fc);
        }
    }

    add(fields);
}

From source file:org.onexus.website.api.widgets.tableviewer.decorators.box.BoxEntityPanel.java

public BoxEntityPanel(String id, Field columnField, IEntity entity, List<String> fieldIds,
        Map<String, String> decorators) {
    super(id);//from w  w w .j av  a  2 s  . co m

    Collection collection = entity.getCollection();

    List<Field> fields = new ArrayList<Field>();
    if (fieldIds == null || fieldIds.isEmpty()) {
        fields.addAll(collection.getFields());
    } else {
        for (String fieldId : fieldIds) {
            fields.add(collection.getField(fieldId));
        }
    }

    RepeatingView fieldsView = new RepeatingView("fields");
    boolean allValuesAreNull = true;
    for (Field field : fields) {

        Object value = entity.get(field.getId());
        if (value != null && !StringUtils.isEmpty(value.toString())) {

            allValuesAreNull = false;

            WebMarkupContainer fc = new WebMarkupContainer(fieldsView.newChildId());
            fc.setRenderBodyOnly(true);
            fc.add(new Label("label", field.getLabel()).add(new AttributeModifier("title", field.getTitle())));
            fc.add(new Label("value", StringUtils.abbreviate(value.toString(), 50)));

            String externalLink = field.getProperty("EXTERNAL_LINK");
            if (!StringUtils.isEmpty(externalLink)) {
                fc.add(new ExternalLink("link", replaceEntityValues(externalLink, entity)));
            } else {
                fc.add(new WebMarkupContainer("link").setVisible(false));
            }

            fieldsView.add(fc);
        }
    }

    // Hide the box if it is empty
    if (allValuesAreNull) {
        setVisible(false);
    }

    add(fieldsView);

    WebMarkupContainer decoratorsContainer = new WebMarkupContainer("decoratorsContainer");
    add(decoratorsContainer);
    RepeatingView decoratorsView = new RepeatingView("decorators");

    for (Map.Entry<String, String> decorator : decorators.entrySet()) {
        WebMarkupContainer item = new WebMarkupContainer(decoratorsView.newChildId());

        IDecorator decoratorImpl = getDecoratorManager().getDecorator(decorator.getKey(),
                entity.getCollection(), columnField);
        decoratorImpl.populateCell(item, "decorator", new EntityModel(entity));
        item.add(new Label("label", decorator.getValue()));
        decoratorsView.add(item);
    }

    if (decorators.isEmpty()) {
        decoratorsContainer.setVisible(false);
    }

    decoratorsContainer.add(decoratorsView);

}

From source file:org.onexus.website.api.widgets.tableviewer.decorators.link.LinkDecorator.java

@Override
public void populateCell(WebMarkupContainer cellContainer, String componentId, IModel<IEntity> entityModel) {

    IEntity entity = entityModel.getObject();
    Object description = getValue(entity);

    if (description != null) {

        String currentColumnValue = String.valueOf(entity.get(getField().getId()));

        List<String> columnValues = new ArrayList<String>();
        if (parameters.containsKey(LinkDecoratorParameters.SEPARATOR)) {
            for (String value : currentColumnValue
                    .split(parameters.get(LinkDecoratorParameters.SEPARATOR).trim())) {
                columnValues.add(value.trim());
            }//from ww  w  .  j  ava 2 s. c  om
        } else {
            columnValues.add(getFormatValue(entity));
        }

        StringBuilder content = new StringBuilder();
        Iterator<String> columnValueIt = columnValues.iterator();

        while (columnValueIt.hasNext()) {
            String columnValue = columnValueIt.next();

            String href = parameters.get(LinkDecoratorParameters.URL);

            href = replaceParameters(getField(), columnValue, entity, href, false);
            href = fixLinkUrl(href);

            content.append("<a href=\"").append(href).append("\"");
            if (parameters.containsKey(LinkDecoratorParameters.TARGET)) {
                content.append(" target=\"").append(parameters.get(LinkDecoratorParameters.TARGET))
                        .append("\"");
            }

            if (parameters.containsKey(LinkDecoratorParameters.LENGTH)) {
                columnValue = StringUtils.abbreviate(columnValue,
                        Integer.valueOf(parameters.get(LinkDecoratorParameters.LENGTH)));
            }

            if (parameters.containsKey(LinkDecoratorParameters.ICON)) {

                String iconTitle = parameters.get(LinkDecoratorParameters.ICON_TITLE);
                if (iconTitle == null) {
                    iconTitle = "Click to see more details about this value";
                }
                content.append(" rel=\"tooltip\" title=\"").append(iconTitle).append("\"><i class=\"");
                content.append(parameters.get(LinkDecoratorParameters.ICON)).append("\"></i></a>&nbsp;");
                content.append(columnValue);
            } else {
                content.append(">").append(columnValue).append("</a>");
            }

            if (columnValueIt.hasNext()) {
                content.append(", ");
            }
        }

        cellContainer.add(new Label(componentId, content.toString()).setEscapeModelStrings(false));

    } else {
        cellContainer.add(new EmptyPanel(componentId));
    }

    cellContainer.add(new AttributeModifier("title",
            new Model<String>(description == null ? "No data" : description.toString())));
}

From source file:org.openaccessbutton.openaccessbutton.blog.Post.java

public Post(String title, String description, Date date, String author, String content, String link) {
    this.title = title;
    this.description = description;
    this.date = date;
    this.author = author;
    this.content = content;
    this.link = link;
    // Shorten blogpost titles if they're too long
    this.shortTitle = StringUtils.abbreviate(title, SHORT_TITLE_LENGTH);
}

From source file:org.openestate.io.idx.IdxFormat.java

public static String printString(String value, int maxLength) {
    value = StringUtils.trimToNull(value);
    if (maxLength < 1)
        return value;
    else if (maxLength < 4)
        return StringUtils.left(value, maxLength);
    else//from  w ww  .ja va2  s.c o m
        return StringUtils.abbreviate(value, maxLength);
}

From source file:org.openestate.io.immobiliare_it.ImmobiliareItUtils.java

private static String printText(String value, int maxLength) {
    value = StringUtils.trimToEmpty(value);
    int length = value.length();
    if (length <= 0)
        return StringUtils.EMPTY;
    else if (length <= maxLength)
        return value;
    else if (maxLength > 3)
        return StringUtils.abbreviate(value, maxLength);
    else//from   w  ww. j  a v  a  2 s .com
        return value.substring(0, maxLength);
}

From source file:org.openestate.io.kyero.KyeroUtils.java

public static String printIdType(String value) {
    value = StringUtils.trimToNull(value);
    if (value == null || !isValidIdType(value))
        throw new IllegalArgumentException("Can't print id value!");
    else/*from w w w  . j  av  a 2 s.c o  m*/
        return StringUtils.abbreviate(value, 50);
}

From source file:org.openestate.io.kyero.KyeroUtils.java

public static String printLocationType(String value) {
    value = StringUtils.trimToNull(value);
    if (value == null)
        throw new IllegalArgumentException("Can't print location value!");
    else// w  ww.  jav a 2  s . co  m
        return StringUtils.abbreviate(value, 50);
}

From source file:org.openestate.io.kyero.KyeroUtils.java

public static String printRefType(String value) {
    value = StringUtils.trimToNull(value);
    if (value == null || !isValidRefType(value))
        throw new IllegalArgumentException("Can't print ref value!");
    else//w  ww .  java  2 s . c  o  m
        return StringUtils.abbreviate(value, 15);
}