Example usage for org.eclipse.jface.viewers ColumnLabelProvider getFont

List of usage examples for org.eclipse.jface.viewers ColumnLabelProvider getFont

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers ColumnLabelProvider getFont.

Prototype

@Override
    public Font getFont(Object element) 

Source Link

Usage

From source file:org.entirej.applicationframework.rwt.renderers.blocks.EJRWTMultiRecordBlockRenderer.java

License:Apache License

public ColumnLabelProvider createScreenItem(EJRWTTableViewerColumnFactory factory,
        EJCoreMainScreenItemProperties itemProps) {
    if (itemProps.isSpacerItem()) {
        return null;
    }/*w w  w  .j a v  a  2 s .co m*/
    EJScreenItemController item = _block.getScreenItem(EJScreenType.MAIN, itemProps.getReferencedItemName());
    EJManagedItemRendererWrapper renderer = item.getManagedItemRenderer();
    if (renderer != null) {
        EJFrameworkExtensionProperties blockProperties = itemProps.getBlockRendererRequiredProperties();
        EJRWTAppItemRenderer itemRenderer = (EJRWTAppItemRenderer) renderer.getUnmanagedRenderer();
        ColumnLabelProvider labelProvider = itemRenderer.createColumnLabelProvider(itemProps, item);
        if (labelProvider != null) {
            String labelOrientation = blockProperties
                    .getStringProperty(EJRWTMultiRecordBlockDefinitionProperties.COLUMN_ALIGNMENT);

            int displayedWidth = blockProperties
                    .getIntProperty(EJRWTMultiRecordBlockDefinitionProperties.DISPLAY_WIDTH_PROPERTY, 0);

            // if no width define in block properties use item renderer pref
            // width
            if (displayedWidth == 0) {
                if (itemProps.getLabel() != null) {
                    // add offset
                    displayedWidth = itemProps.getLabel().length() + 2;
                } else {
                    displayedWidth = 5;
                }
            }

            String visualAttribute = blockProperties
                    .getStringProperty(EJRWTMultiRecordBlockDefinitionProperties.VISUAL_ATTRIBUTE_PROPERTY);

            if (visualAttribute != null) {
                EJCoreVisualAttributeProperties va = EJCoreProperties.getInstance()
                        .getVisualAttributesContainer().getVisualAttributeProperties(visualAttribute);
                if (va != null) {
                    itemRenderer.setInitialVisualAttribute(va);
                }
            }

            TableViewerColumn viewerColumn = factory.createColumn(itemProps.getLabel(), displayedWidth,
                    labelProvider, getComponentStyle(labelOrientation));
            TableColumn column = viewerColumn.getColumn();
            column.setData("KEY", itemProps.getReferencedItemName());
            column.setToolTipText(itemProps.getHint());

            column.setMoveable(blockProperties
                    .getBooleanProperty(EJRWTMultiRecordBlockDefinitionProperties.ALLOW_COLUMN_REORDER, true));
            column.setResizable(blockProperties
                    .getBooleanProperty(EJRWTMultiRecordBlockDefinitionProperties.ALLOW_COLUMN_RESIZE, true));
            if (blockProperties.getBooleanProperty(EJRWTMultiRecordBlockDefinitionProperties.ALLOW_ROW_SORTING,
                    true)) {
                EJRWTAbstractTableSorter columnSorter = itemRenderer.getColumnSorter(itemProps, item);
                if (columnSorter != null) {
                    new EJRWTTableSortSelectionListener(_tableViewer, column, columnSorter, SWT.UP, false);
                }
            }
            // ensure that the width property of the table column is in
            // Characters
            Font font = labelProvider.getFont(new Object());
            if (font == null) {
                font = _tableViewer.getTable().getFont();
            }

            if (font != null) {
                float avgCharWidth = EJRWTImageRetriever.getGraphicsProvider().getAvgCharWidth(font);
                if (avgCharWidth > 0) {
                    // add + 1 padding
                    column.setWidth((int) ((column.getWidth() + 1) * avgCharWidth));
                }
            }
            return labelProvider;
        }
    }
    return null;
}

From source file:org.entirej.applicationframework.rwt.renderers.blocks.EJRWTTreeTableRecordBlockRenderer.java

License:Apache License

@Override
public void buildGuiComponent(EJRWTEntireJGridPane blockCanvas) {
    EJFrameworkExtensionProperties appProp = EJCoreProperties.getInstance().getApplicationDefinedProperties();
    if (appProp != null) {
        EJFrameworkExtensionProperties propertyGroup = appProp
                .getPropertyGroup(EJRWTSingleRecordBlockDefinitionProperties.ACTION_GROUP);
        if (propertyGroup != null) {

            addActionKeyinfo(/* w ww  .jav  a 2  s  .  c  o  m*/
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_QUERY_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_QUERY_KEY);
            addActionKeyinfo(
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_INSERT_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_INSERT_KEY);
            addActionKeyinfo(
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_UPDATE_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_UPDATE_KEY);
            addActionKeyinfo(
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_DELETE_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_DELETE_KEY);
            addActionKeyinfo(
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_REFRESH_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_REFRESH_KEY);

        }
    }
    EJBlockProperties blockProperties = _block.getProperties();
    EJMainScreenProperties mainScreenProperties = blockProperties.getMainScreenProperties();

    GridData gridData = new GridData(GridData.FILL_BOTH);
    gridData.widthHint = mainScreenProperties.getWidth();
    gridData.heightHint = mainScreenProperties.getHeight();

    gridData.horizontalSpan = mainScreenProperties.getHorizontalSpan();
    gridData.verticalSpan = mainScreenProperties.getVerticalSpan();
    gridData.grabExcessHorizontalSpace = mainScreenProperties.canExpandHorizontally();
    gridData.grabExcessVerticalSpace = mainScreenProperties.canExpandVertically();

    if (gridData.grabExcessHorizontalSpace) {
        gridData.minimumWidth = mainScreenProperties.getWidth();
    }
    if (gridData.grabExcessVerticalSpace) {
        gridData.minimumHeight = mainScreenProperties.getHeight();
    }
    _rendererProp = blockProperties.getBlockRendererProperties();
    blockCanvas.setLayoutData(gridData);

    EJFrameworkExtensionProperties sectionProperties = null;
    if (_rendererProp != null) {
        sectionProperties = _rendererProp
                .getPropertyGroup(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR);
    }
    if (sectionProperties != null
            && sectionProperties.getStringProperty(
                    EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE) != null
            && !EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE_GROUP
                    .equals(sectionProperties.getStringProperty(
                            EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE))) {
        int style = ExpandableComposite.TITLE_BAR;

        String mode = sectionProperties
                .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE);
        if (EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE_TWISTIE.equals(mode)) {
            style = style | ExpandableComposite.TWISTIE;
        } else if (EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE_TREE_NODE
                .equals(mode)) {
            style = style | ExpandableComposite.TREE_NODE;
        }
        if (sectionProperties.getBooleanProperty(
                EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_EXPANDED, true)) {
            style = style | ExpandableComposite.EXPANDED;
        }
        Section section = toolkit.createSection(blockCanvas, style);
        section.setLayoutData(gridData);
        String title = sectionProperties
                .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_TITLE);
        if (title != null) {
            section.setText(title);
        }
        EJRWTImageRetriever.getGraphicsProvider().rendererSection(section);
        if (mainScreenProperties.getDisplayFrame()) {
            Group group = new Group(section, SWT.NONE);
            group.setData(EJ_RWT.CUSTOM_VARIANT, EJ_RWT.CSS_CV_ITEM_GROUP);
            group.setLayout(new FillLayout());
            group.setLayoutData(gridData);
            hookKeyListener(group);
            String frameTitle = mainScreenProperties.getFrameTitle();
            if (frameTitle != null && frameTitle.length() > 0) {
                group.setText(frameTitle);
            }
            _mainPane = new EJRWTEntireJGridPane(group, 1);
            _mainPane.setData(EJ_RWT.CUSTOM_VARIANT, EJ_RWT.CSS_CV_ITEM_GROUP);
            section.setClient(group);

        } else {
            _mainPane = new EJRWTEntireJGridPane(section, 1);
            _mainPane.setLayoutData(gridData);
            _mainPane.cleanLayoutHorizontal();
            _mainPane.setData(EJ_RWT.CUSTOM_VARIANT, EJ_RWT.CSS_CV_ITEM_GROUP);
            section.setClient(_mainPane);
        }

        final EJFrameworkExtensionPropertyList propertyList = sectionProperties
                .getPropertyList(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTIONS);

        if (propertyList != null && propertyList.getAllListEntries().size() > 0) {
            ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
            final ToolBar toolbar = toolBarManager.createControl(section);
            final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
            toolbar.setCursor(handCursor);
            // Cursor needs to be explicitly disposed
            toolbar.addDisposeListener(new DisposeListener() {
                @Override
                public void widgetDisposed(DisposeEvent e) {
                    if (handCursor != null && handCursor.isDisposed() == false) {
                        handCursor.dispose();
                    }
                }
            });
            List<EJFrameworkExtensionPropertyListEntry> allListEntries = propertyList.getAllListEntries();
            for (EJFrameworkExtensionPropertyListEntry entry : allListEntries) {
                final String actionID = entry
                        .getProperty(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTION_ID);
                String actionImage = entry.getProperty(
                        EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTION_IMAGE);
                String actionName = entry.getProperty(
                        EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTION_NAME);
                String actionTooltip = entry.getProperty(
                        EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTION_TOOLTIP);

                if (actionID != null) {
                    Action action = new Action(actionID, IAction.AS_PUSH_BUTTON) {
                        @Override
                        public void runWithEvent(Event event) {
                            _block.executeActionCommand(actionID, EJScreenType.MAIN);
                        }
                    };
                    if (actionName != null) {
                        action.setText(actionName);
                    }
                    if (actionTooltip != null) {
                        action.setDescription(actionTooltip);
                    }
                    if (actionImage != null && actionImage.length() > 0) {
                        action.setImageDescriptor(
                                ImageDescriptor.createFromImage(EJRWTImageRetriever.get(actionImage)));
                    }
                    toolBarManager.add(action);
                }
            }

            toolBarManager.update(true);
            section.setTextClient(toolbar);
        }
    } else {
        if (mainScreenProperties.getDisplayFrame()) {
            Group group = new Group(blockCanvas, SWT.NONE);
            group.setData(EJ_RWT.CUSTOM_VARIANT, EJ_RWT.CSS_CV_ITEM_GROUP);
            group.setLayout(new FillLayout());
            group.setLayoutData(gridData);
            hookKeyListener(group);
            String frameTitle = mainScreenProperties.getFrameTitle();
            if (frameTitle != null && frameTitle.length() > 0) {
                group.setText(frameTitle);
            }
            _mainPane = new EJRWTEntireJGridPane(group, 1);
            _mainPane.setData(EJ_RWT.CUSTOM_VARIANT, EJ_RWT.CSS_CV_ITEM_GROUP);
        } else {
            _mainPane = new EJRWTEntireJGridPane(blockCanvas, 1);
            _mainPane.setLayoutData(gridData);
            _mainPane.cleanLayout();
            _mainPane.setData(EJ_RWT.CUSTOM_VARIANT, EJ_RWT.CSS_CV_ITEM_GROUP);
        }
    }

    hookKeyListener(_mainPane);
    int style = SWT.VIRTUAL | SWT.FULL_SELECTION;

    if (!_rendererProp.getBooleanProperty(EJRWTTreeTableBlockDefinitionProperties.HIDE_TREE_BORDER, false)) {
        style = style | SWT.BORDER;
    }

    Collection<EJItemGroupProperties> allItemGroupProperties = _block.getProperties()
            .getScreenItemGroupContainer(EJScreenType.MAIN).getAllItemGroupProperties();
    final Tree table;
    final EJRWTAbstractFilteredTree filterTree;
    if (_rendererProp.getBooleanProperty(EJRWTTreeTableBlockDefinitionProperties.FILTER, true)) {
        if (allItemGroupProperties.size() > 0) {
            EJItemGroupProperties displayProperties = allItemGroupProperties.iterator().next();
            if (displayProperties.dispayGroupFrame()) {
                Group group = new Group(_mainPane, SWT.NONE);
                group.setLayout(new FillLayout());
                if (displayProperties.getFrameTitle() != null
                        && displayProperties.getFrameTitle().length() > 0) {
                    group.setText(displayProperties.getFrameTitle());
                }
                group.setLayoutData(
                        new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
                filterTree = new EJRWTAbstractFilteredTree(group, style) {
                    @Override
                    public void filter(String filter) {
                        if (_filteredContentProvider != null
                                && (filter == null && _filteredContentProvider.getFilter() != null
                                        || !filter.equals(_filteredContentProvider.getFilter()))) {
                            _filteredContentProvider.setFilter(filter);
                            refresh(filter);
                        }
                    }
                };
            } else {
                filterTree = new EJRWTAbstractFilteredTree(_mainPane, style) {
                    @Override
                    public void filter(String filter) {
                        if (_filteredContentProvider != null
                                && (filter == null && _filteredContentProvider.getFilter() != null
                                        || !filter.equals(_filteredContentProvider.getFilter()))) {
                            _filteredContentProvider.setFilter(filter);
                            refresh(filter);
                        }
                    }
                };
                filterTree.setLayoutData(
                        new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
            }
        } else {
            filterTree = new EJRWTAbstractFilteredTree(_mainPane, style) {
                @Override
                public void filter(String filter) {
                    if (_filteredContentProvider != null
                            && (filter == null && _filteredContentProvider.getFilter() != null
                                    || !filter.equals(_filteredContentProvider.getFilter()))) {
                        _filteredContentProvider.setFilter(filter);
                        refresh(filter);
                    }
                }
            };

            filterTree.setLayoutData(
                    new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
        }
        table = (_tableViewer = filterTree.getViewer()).getTree();
    } else {
        filterTree = null;
        if (allItemGroupProperties.size() > 0) {
            EJItemGroupProperties displayProperties = allItemGroupProperties.iterator().next();
            if (displayProperties.dispayGroupFrame()) {
                Group group = new Group(_mainPane, SWT.NONE);
                group.setLayout(new FillLayout());
                if (displayProperties.getFrameTitle() != null
                        && displayProperties.getFrameTitle().length() > 0) {
                    group.setText(displayProperties.getFrameTitle());
                }
                group.setLayoutData(
                        new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
                table = new Tree(group, style);
            } else {
                table = new Tree(_mainPane, style);

                table.setLayoutData(
                        new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
            }
        } else {
            table = new Tree(_mainPane, style);

            table.setLayoutData(
                    new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
        }
    }

    table.setLinesVisible(_rendererProp
            .getBooleanProperty(EJRWTTreeTableBlockDefinitionProperties.SHOW_VERTICAL_LINES, true));
    table.setHeaderVisible(_rendererProp
            .getBooleanProperty(EJRWTTreeTableBlockDefinitionProperties.SHOW_HEADING_PROPERTY, true));
    Control[] children = table.getChildren();
    for (Control control : children) {
        hookKeyListener(control);
    }
    hookKeyListener(table);
    // final boolean hideSelection = (style & SWT.HIDE_SELECTION) != 0;
    _tableViewer = new TreeViewer(table);

    ColumnViewerToolTipSupport.enableFor(_tableViewer);

    EJRWTTreeTableViewerColumnFactory factory = new EJRWTTreeTableViewerColumnFactory(_tableViewer);
    final List<ColumnLabelProvider> nodeTextProviders = new ArrayList<ColumnLabelProvider>();
    for (EJItemGroupProperties groupProperties : allItemGroupProperties) {
        Collection<EJScreenItemProperties> itemProperties = groupProperties.getAllItemProperties();
        for (EJScreenItemProperties screenItemProperties : itemProperties) {
            EJCoreMainScreenItemProperties mainScreenItemProperties = (EJCoreMainScreenItemProperties) screenItemProperties;
            ColumnLabelProvider screenItem = createScreenItem(factory, mainScreenItemProperties);
            if (screenItem != null) {
                nodeTextProviders.add(screenItem);
            }
        }
    }

    table.addFocusListener(new FocusListener() {
        @Override
        public void focusLost(FocusEvent arg0) {
            setHasFocus(false);
        }

        @Override
        public void focusGained(FocusEvent arg0) {
            setHasFocus(true);
        }
    });
    _mainPane.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent arg0) {
            if (!table.isFocusControl()) {
                setHasFocus(true);
            }
        }
    });
    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent arg0) {
            if (!table.isFocusControl()) {
                setHasFocus(true);
            }
        }

    });
    final String pid = _rendererProp.getStringProperty(EJRWTTreeTableBlockDefinitionProperties.PARENT_ITEM);
    final String rid = _rendererProp.getStringProperty(EJRWTTreeTableBlockDefinitionProperties.RELATION_ITEM);
    final String imageid = _rendererProp
            .getStringProperty(EJRWTTreeTableBlockDefinitionProperties.NODE_IMAGE_ITEM);

    if (imageid != null && _tableViewer.getTree().getColumnCount() > 0) {
        final ColumnLabelProvider baselabelProvider = (ColumnLabelProvider) _tableViewer.getLabelProvider(0);

        ColumnLabelProvider imgPatchedProvider = new ColumnLabelProvider() {
            final transient Map<Object, Image> imageMap = new HashMap<Object, Image>();

            @Override
            public void dispose() {
                for (Image img : imageMap.values()) {
                    img.dispose();
                }
                imageMap.clear();
                super.dispose();
            }

            @Override
            public Font getFont(Object element) {
                return baselabelProvider.getFont(element);
            }

            @Override
            public Color getBackground(Object element) {
                return baselabelProvider.getBackground(element);
            }

            @Override
            public Color getForeground(Object element) {
                return baselabelProvider.getForeground(element);
            }

            @Override
            public String getText(Object element) {
                return baselabelProvider.getText(element);
            }

            @Override
            public Color getToolTipBackgroundColor(Object object) {
                return baselabelProvider.getToolTipBackgroundColor(object);
            }

            @Override
            public int getToolTipDisplayDelayTime(Object object) {
                return baselabelProvider.getToolTipDisplayDelayTime(object);
            }

            @Override
            public Font getToolTipFont(Object object) {
                return baselabelProvider.getToolTipFont(object);
            }

            @Override
            public Color getToolTipForegroundColor(Object object) {
                return baselabelProvider.getToolTipForegroundColor(object);
            }

            @Override
            public Image getToolTipImage(Object object) {
                return baselabelProvider.getToolTipImage(object);
            }

            @Override
            public Point getToolTipShift(Object object) {
                return baselabelProvider.getToolTipShift(object);
            }

            @Override
            public int getToolTipStyle(Object object) {
                return baselabelProvider.getToolTipStyle(object);
            }

            @Override
            public String getToolTipText(Object element) {
                return baselabelProvider.getToolTipText(element);
            }

            @Override
            public int getToolTipTimeDisplayed(Object object) {
                return baselabelProvider.getToolTipTimeDisplayed(object);
            }

            @Override
            public Image getImage(Object element) {
                if (imageid != null && element instanceof EJDataRecord) {
                    EJDataRecord record = (EJDataRecord) element;
                    Object iV = record.getValue(imageid);
                    if (iV == null) {
                        return null;
                    }
                    Image image = imageMap.get(iV);
                    if (image != null && !image.isDisposed()) {
                        return image;
                    }

                    if (iV instanceof URL) {
                        image = ImageDescriptor.createFromURL((URL) iV).createImage();
                    } else if (iV instanceof byte[]) {
                        image = new Image(Display.getDefault(), new ByteArrayInputStream((byte[]) iV));
                    }
                    if (image != null) {
                        imageMap.put(iV, image);
                    }
                    return image;
                }
                return super.getImage(element);
            }

        };
        ((TreeViewerColumn) _tableViewer.getTree().getColumn(0).getData("VIEWER"))
                .setLabelProvider(imgPatchedProvider);

    }
    _tableViewer.setAutoExpandLevel(
            _rendererProp.getIntProperty(EJRWTTreeTableBlockDefinitionProperties.NODE_EXPAND_LEVEL, 1));

    _tableViewer.setContentProvider(_filteredContentProvider = new FilteredContentProvider() {
        private List<EJDataRecord> root = new ArrayList<EJDataRecord>();
        private Map<Object, Object> indexMap = new HashMap<Object, Object>();
        private Map<Object, List<EJDataRecord>> cmap = new HashMap<Object, List<EJDataRecord>>();

        private List<EJDataRecord> froot = new ArrayList<EJDataRecord>();
        private Map<Object, List<EJDataRecord>> fcmap = new HashMap<Object, List<EJDataRecord>>();

        boolean matchItem(EJDataRecord rec) {
            if (filter != null && filter.trim().length() > 0) {
                for (ColumnLabelProvider filterTextProvider : nodeTextProviders) {
                    String text = filterTextProvider.getText(rec);
                    if (text != null && text.toLowerCase().contains(filter.toLowerCase())) {
                        return true;
                    }
                }
            }
            return false;
        }

        @Override
        public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
            _treeBaseRecords.clear();
            if (arg2 != null && arg2.equals(filter) && filter.trim().length() > 0) {
                froot.clear();
                fcmap.clear();
                // filter
                for (Entry<Object, List<EJDataRecord>> entry : cmap.entrySet()) {

                    List<EJDataRecord> values = entry.getValue();
                    List<EJDataRecord> fvalues = new ArrayList<EJDataRecord>(values.size());
                    fcmap.put(entry.getKey(), fvalues);
                    for (EJDataRecord record : values) {
                        if (matchItem(record)) {
                            fvalues.add(record);
                        }
                    }
                }
                // filter root
                for (EJDataRecord record : root) {
                    if (matchItem(record)) {
                        froot.add(record);
                    } else if (hasChildren(record)) {
                        froot.add(record);
                    }
                }
                for (EJDataRecord record : froot) {
                    _treeBaseRecords.add(record);
                    addSubRecords(record.getValue(pid), fcmap);
                }
            } else {
                filter = null;
                if (filterTree != null) {
                    filterTree.clearText();
                }
                root.clear();
                indexMap.clear();
                froot.clear();
                cmap.clear();
                fcmap.clear();
                Collection<EJDataRecord> records = _block.getRecords();
                for (EJDataRecord record : records) {
                    Object rV = record.getValue(rid);
                    Object pV = record.getValue(pid);
                    if (rV == null) {
                        root.add(record);
                        if (pid != null) {
                            indexMap.put(pV, record);
                        }

                        continue;
                    }
                    List<EJDataRecord> list = cmap.get(rV);
                    if (list == null) {
                        list = new ArrayList<EJDataRecord>();
                        cmap.put(rV, list);
                    }
                    list.add(record);
                }

                //child node with no parent need to consider as roots
                MAIN: for (Object key : new HashSet<Object>(cmap.keySet())) {
                    if (indexMap.containsKey(key)) {
                        continue;
                    }

                    for (EJDataRecord rec : records) {
                        if (key.equals(rec.getValue(pid))) {
                            continue MAIN;
                        }
                    }
                    List<EJDataRecord> list = cmap.get(key);
                    cmap.remove(key);
                    for (EJDataRecord record : list) {
                        Object pV = record.getValue(pid);
                        root.add(record);
                        if (pid != null) {
                            indexMap.put(pV, record);
                        }
                    }
                }

                for (EJDataRecord record : root) {
                    _treeBaseRecords.add(record);
                    addSubRecords(record.getValue(pid), cmap);
                }

            }

        }

        private void addSubRecords(Object key, Map<Object, List<EJDataRecord>> cmap) {
            if (key != null) {
                List<EJDataRecord> list = cmap.get(key);
                if (list != null) {
                    for (EJDataRecord record : list) {
                        _treeBaseRecords.add(record);
                        addSubRecords(record.getValue(pid), cmap);
                    }
                }
            }
        }

        @Override
        public void dispose() {
            root.clear();
            indexMap.clear();
            cmap.clear();
            froot.clear();
            fcmap.clear();
        }

        @Override
        public Object[] getElements(Object arg0) {
            if (filter != null && filter.trim().length() > 0) {
                return froot.toArray();
            }
            return root.toArray();
        }

        @Override
        public Object[] getChildren(Object arg0) {
            Map<Object, List<EJDataRecord>> map = filter != null && filter.trim().length() > 0 ? fcmap : cmap;
            if (arg0 instanceof EJDataRecord) {
                EJDataRecord record = (EJDataRecord) arg0;
                Object pV = record.getValue(pid);
                if (pV != null) {
                    List<EJDataRecord> list = map.get(pV);
                    if (list != null) {
                        return list.toArray();
                    }
                }
            }
            return new Object[0];
        }

        @Override
        public Object getParent(Object arg0) {
            if (arg0 instanceof EJDataRecord) {
                EJDataRecord record = (EJDataRecord) arg0;
                Object rV = record.getValue(rid);
                if (rV != null) {
                    return indexMap.get(rV);
                }
            }
            return null;
        }

        @Override
        public boolean hasChildren(Object arg0) {
            if (arg0 instanceof EJDataRecord) {
                Map<Object, List<EJDataRecord>> map = filter != null && filter.trim().length() > 0 ? fcmap
                        : cmap;
                EJDataRecord record = (EJDataRecord) arg0;
                Object pV = record.getValue(pid);
                if (pV != null) {
                    List<EJDataRecord> list = map.get(pV);
                    return list != null && list.size() > 0;

                }
            }
            return false;
        }
    });
    _tableViewer.setInput(new Object());
    selectRow(0);

    // add double click action
    final String doubleClickActionCommand = _rendererProp
            .getStringProperty(EJRWTTreeTableBlockDefinitionProperties.DOUBLE_CLICK_ACTION_COMMAND);
    if (doubleClickActionCommand != null) {
        _tableViewer.addDoubleClickListener(new IDoubleClickListener() {

            @Override
            public void doubleClick(DoubleClickEvent arg0) {
                _block.executeActionCommand(doubleClickActionCommand, EJScreenType.MAIN);
            }
        });
    }
    _tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent arg0) {
            EJDataRecord focusedRecord = getFocusedRecord();
            if (focusedRecord != null) {
                _block.newRecordInstance(focusedRecord);
            }
        }
    });
}

From source file:org.entirej.applicationframework.rwt.renderers.blocks.EJRWTTreeTableRecordBlockRenderer.java

License:Apache License

public ColumnLabelProvider createScreenItem(EJRWTTreeTableViewerColumnFactory factory,
        EJCoreMainScreenItemProperties itemProps) {
    if (itemProps.isSpacerItem()) {

        return null;
    }//from w ww.jav  a  2s .  c o  m
    EJScreenItemController item = _block.getScreenItem(EJScreenType.MAIN, itemProps.getReferencedItemName());
    EJManagedItemRendererWrapper renderer = item.getManagedItemRenderer();
    if (renderer != null) {
        EJFrameworkExtensionProperties blockProperties = itemProps.getBlockRendererRequiredProperties();
        EJRWTAppItemRenderer itemRenderer = (EJRWTAppItemRenderer) renderer.getUnmanagedRenderer();
        ColumnLabelProvider labelProvider = itemRenderer.createColumnLabelProvider(itemProps, item);
        if (labelProvider != null) {

            String labelOrientation = blockProperties
                    .getStringProperty(EJRWTTreeTableBlockDefinitionProperties.COLUMN_ALIGNMENT);

            int displayedWidth = blockProperties
                    .getIntProperty(EJRWTTreeTableBlockDefinitionProperties.DISPLAY_WIDTH_PROPERTY, 0);

            // if no width define in block properties use item renderer pref
            // width
            if (displayedWidth == 0) {
                if (itemProps.getLabel() != null) {
                    displayedWidth = itemProps.getLabel().length() + 2;// add
                                                                       // offset
                } else {
                    displayedWidth = 5;
                }
            }

            String visualAttribute = blockProperties
                    .getStringProperty(EJRWTTreeTableBlockDefinitionProperties.VISUAL_ATTRIBUTE_PROPERTY);

            if (visualAttribute != null) {
                EJCoreVisualAttributeProperties va = EJCoreProperties.getInstance()
                        .getVisualAttributesContainer().getVisualAttributeProperties(visualAttribute);
                if (va != null) {
                    itemRenderer.setInitialVisualAttribute(va);
                }
            }

            TreeViewerColumn viewerColumn = factory.createColumn(itemProps.getLabel(), displayedWidth,
                    labelProvider, getComponentStyle(labelOrientation));
            TreeColumn column = viewerColumn.getColumn();
            column.setData("KEY", itemProps.getReferencedItemName());
            column.setData("VIEWER", viewerColumn);
            column.setToolTipText(itemProps.getHint());

            column.setMoveable(blockProperties
                    .getBooleanProperty(EJRWTTreeTableBlockDefinitionProperties.ALLOW_COLUMN_REORDER, true));
            column.setResizable(blockProperties
                    .getBooleanProperty(EJRWTTreeTableBlockDefinitionProperties.ALLOW_COLUMN_RESIZE, true));
            if (blockProperties.getBooleanProperty(EJRWTTreeTableBlockDefinitionProperties.ALLOW_ROW_SORTING,
                    true)) {
                EJRWTAbstractTableSorter columnSorter = itemRenderer.getColumnSorter(itemProps, item);
                if (columnSorter != null) {
                    new EJRWTTreeTableSortSelectionListener(_tableViewer, column, columnSorter, SWT.UP, false);
                }
            }
            // ensure that the width property of the table column is in
            // Characters
            Font font = labelProvider.getFont(new Object());
            if (font == null) {
                font = _tableViewer.getTree().getFont();
            }
            if (font != null) {
                float avgCharWidth = EJRWTImageRetriever.getGraphicsProvider().getAvgCharWidth(font);
                if (avgCharWidth > 0) {
                    column.setWidth((int) ((column.getWidth() + 1) * avgCharWidth));// add
                                                                                    // +1
                                                                                    // padding
                }
            }

            return labelProvider;
        }
    }
    return null;
}

From source file:org.entirej.applicationframework.rwt.renderers.html.EJRWTHtmlTableBlockRenderer.java

License:Apache License

@Override
public void buildGuiComponent(EJRWTEntireJGridPane blockCanvas) {
    if (_browser != null && !_browser.isDisposed()) {
        _browser.dispose();/*from www  . j  a va  2 s .c  o m*/
    }

    EJBlockProperties blockProperties = _block.getProperties();
    EJMainScreenProperties mainScreenProperties = blockProperties.getMainScreenProperties();

    EJFrameworkExtensionProperties blockRendererProperties = blockProperties.getBlockRendererProperties();
    boolean addHeader = true;
    if (blockRendererProperties != null) {
        addHeader = blockRendererProperties
                .getBooleanProperty(EJRWTMultiRecordBlockDefinitionProperties.SHOW_HEADING_PROPERTY, true);
        EJCoreFrameworkExtensionPropertyList propertyList = blockRendererProperties.getPropertyList(ACTIONS);

        if (propertyList != null) {
            List<EJFrameworkExtensionPropertyListEntry> allListEntries = propertyList.getAllListEntries();
            for (EJFrameworkExtensionPropertyListEntry entry : allListEntries) {
                String actionID = entry.getProperty(ACTION_ID);
                String actionkey = entry.getProperty(ACTION_KEY);
                if (actionID != null && actionkey != null && actionID.trim().length() > 0
                        && actionkey.trim().length() > 0) {
                    addActionKeyinfo(actionkey, actionID);
                }
            }
        }

    }

    GridData gridData = new GridData(GridData.FILL_BOTH);
    gridData.widthHint = mainScreenProperties.getWidth();
    gridData.heightHint = mainScreenProperties.getHeight();

    gridData.horizontalSpan = mainScreenProperties.getHorizontalSpan();
    gridData.verticalSpan = mainScreenProperties.getVerticalSpan();
    gridData.grabExcessHorizontalSpace = mainScreenProperties.canExpandHorizontally();
    gridData.grabExcessVerticalSpace = mainScreenProperties.canExpandVertically();

    // if (gridData.grabExcessHorizontalSpace)
    // gridData.minimumHeight = mainScreenProperties.getHeight();
    // if (gridData.grabExcessVerticalSpace)
    // gridData.minimumWidth = mainScreenProperties.getHeight();
    blockCanvas.setLayoutData(gridData);
    scrollComposite = new ScrolledComposite(blockCanvas, SWT.V_SCROLL | SWT.H_SCROLL);
    scrollComposite.setData(EJ_RWT.CUSTOM_VARIANT, EJ_RWT.CSS_CV_ITEM_GROUP);
    scrollComposite.setExpandHorizontal(true);
    scrollComposite.setExpandVertical(true);
    scrollComposite.setMinSize(mainScreenProperties.getWidth(), mainScreenProperties.getHeight());
    boolean filtered = blockRendererProperties.getBooleanProperty(EJRWTTreeBlockDefinitionProperties.FILTER,
            false);
    EJRWTAbstractFilteredHtml filterHtml = null;
    if (mainScreenProperties.getDisplayFrame()) {
        String frameTitle = mainScreenProperties.getFrameTitle();
        if (frameTitle != null && frameTitle.length() > 0) {
            Group group = new Group(scrollComposite, SWT.NONE);
            group.setData(EJ_RWT.CUSTOM_VARIANT, EJ_RWT.CSS_CV_ITEM_GROUP);
            hookKeyListener(group);
            scrollComposite.setContent(group);
            group.setLayout(new FillLayout());
            scrollComposite.setLayoutData(gridData);

            if (frameTitle != null && frameTitle.length() > 0) {
                group.setText(frameTitle);
            }

            if (filtered) {
                filterHtml = new EJRWTAbstractFilteredHtml(group, SWT.NONE) {

                    @Override
                    public void filter(String filter) {
                        _filteredContentProvider.setFilter(filter);
                        createHTML();
                    }

                    @Override
                    protected EJRWTHtmlView doCreateTableViewer(Composite parent, int style) {

                        return new EJRWTHtmlView(parent, SWT.NONE) {
                            private static final long serialVersionUID = 1L;

                            @Override
                            public void action(String method, JsonObject parameters) {
                                if ("eaction".equals(method)) {
                                    final Object arg1 = parameters.get("0").asString();
                                    Object arg2 = parameters.get("1").asString();
                                    if (arg1 instanceof String) {
                                        if (arg2 instanceof String) {
                                            currentRec = getRecordAt(Integer.valueOf((String) arg2));
                                            if (currentRec != null)
                                                _block.newRecordInstance(currentRec);
                                        }
                                        Display.getDefault().asyncExec(new Runnable() {

                                            @Override
                                            public void run() {
                                                _block.executeActionCommand((String) arg1, EJScreenType.MAIN);
                                            }
                                        });

                                    }
                                } else if ("esort".equals(method)) {
                                    handleSort(parameters);
                                }

                            }
                        };
                    }
                };
                _browser = filterHtml.getViewer();
            } else {
                _browser = new EJRWTHtmlView(group, SWT.NONE) {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void action(String method, JsonObject parameters) {
                        if ("eaction".equals(method)) {
                            final Object arg1 = parameters.get("0").asString();
                            Object arg2 = parameters.get("1").asString();
                            if (arg1 instanceof String) {
                                if (arg2 instanceof String) {
                                    currentRec = getRecordAt(Integer.valueOf((String) arg2));
                                    if (currentRec != null)
                                        _block.newRecordInstance(currentRec);
                                }
                                Display.getDefault().asyncExec(new Runnable() {

                                    @Override
                                    public void run() {
                                        _block.executeActionCommand((String) arg1, EJScreenType.MAIN);
                                    }
                                });

                            }
                        } else if ("esort".equals(method)) {
                            handleSort(parameters);
                        }

                    }
                };
            }

        } else {
            if (filtered) {
                filterHtml = new EJRWTAbstractFilteredHtml(scrollComposite, SWT.NONE) {

                    @Override
                    public void filter(String filter) {
                        _filteredContentProvider.setFilter(filter);
                        createHTML();
                    }

                    @Override
                    protected EJRWTHtmlView doCreateTableViewer(Composite parent, int style) {

                        return new EJRWTHtmlView(parent, SWT.NONE) {
                            private static final long serialVersionUID = 1L;

                            @Override
                            public void action(String method, JsonObject parameters) {
                                if ("eaction".equals(method)) {
                                    final Object arg1 = parameters.get("0").asString();
                                    Object arg2 = parameters.get("1").asString();
                                    if (arg1 instanceof String) {
                                        if (arg2 instanceof String) {
                                            currentRec = getRecordAt(Integer.valueOf((String) arg2));
                                            if (currentRec != null)
                                                _block.newRecordInstance(currentRec);
                                        }
                                        Display.getDefault().asyncExec(new Runnable() {

                                            @Override
                                            public void run() {
                                                _block.executeActionCommand((String) arg1, EJScreenType.MAIN);
                                            }
                                        });

                                    }
                                } else if ("esort".equals(method)) {
                                    handleSort(parameters);
                                }

                            }
                        };
                    }
                };
                _browser = filterHtml.getViewer();
                scrollComposite.setContent(filterHtml);
                scrollComposite.setLayoutData(gridData);
            } else {

                filterHtml = null;
                _browser = new EJRWTHtmlView(scrollComposite, SWT.BORDER) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void action(String method, JsonObject parameters) {
                        if ("eaction".equals(method)) {
                            final Object arg1 = parameters.get("0").asString();
                            Object arg2 = parameters.get("1").asString();
                            if (arg1 instanceof String) {
                                if (arg2 instanceof String) {
                                    currentRec = getRecordAt(Integer.valueOf((String) arg2));
                                    if (currentRec != null)
                                        _block.newRecordInstance(currentRec);
                                }
                                Display.getDefault().asyncExec(new Runnable() {

                                    @Override
                                    public void run() {
                                        _block.executeActionCommand((String) arg1, EJScreenType.MAIN);
                                    }
                                });

                            }
                        } else if ("esort".equals(method)) {
                            handleSort(parameters);
                        }

                    }
                };
                scrollComposite.setContent(_browser);
                scrollComposite.setLayoutData(gridData);

            }
        }

    } else {
        if (filtered) {
            filterHtml = new EJRWTAbstractFilteredHtml(scrollComposite, SWT.NONE) {

                @Override
                public void filter(String filter) {
                    _filteredContentProvider.setFilter(filter);
                    createHTML();
                }

                @Override
                protected EJRWTHtmlView doCreateTableViewer(Composite parent, int style) {

                    return new EJRWTHtmlView(parent, SWT.NONE) {
                        private static final long serialVersionUID = 1L;

                        @Override
                        public void action(String method, JsonObject parameters) {
                            if ("eaction".equals(method)) {
                                final Object arg1 = parameters.get("0").asString();
                                Object arg2 = parameters.get("1").asString();
                                if (arg1 instanceof String) {
                                    if (arg2 instanceof String) {
                                        currentRec = getRecordAt(Integer.valueOf((String) arg2));
                                        if (currentRec != null)
                                            _block.newRecordInstance(currentRec);
                                    }
                                    Display.getDefault().asyncExec(new Runnable() {

                                        @Override
                                        public void run() {
                                            _block.executeActionCommand((String) arg1, EJScreenType.MAIN);
                                        }
                                    });

                                }
                            } else if ("esort".equals(method)) {
                                handleSort(parameters);
                            }

                        }
                    };
                }
            };
            _browser = filterHtml.getViewer();
            scrollComposite.setContent(filterHtml);
            scrollComposite.setLayoutData(gridData);
        } else {
            filterHtml = null;
            _browser = new EJRWTHtmlView(scrollComposite, SWT.NONE) {
                private static final long serialVersionUID = 1L;

                @Override
                public void action(String method, JsonObject parameters) {

                    if ("eaction".equals(method)) {
                        final Object arg1 = parameters.get("0").asString();
                        Object arg2 = parameters.get("1").asString();
                        if (arg1 instanceof String) {
                            if (arg2 instanceof String) {
                                currentRec = getRecordAt(Integer.valueOf((String) arg2));
                                if (currentRec != null)
                                    _block.newRecordInstance(currentRec);
                            }
                            Display.getDefault().asyncExec(new Runnable() {

                                @Override
                                public void run() {
                                    _block.executeActionCommand((String) arg1, EJScreenType.MAIN);
                                }
                            });

                        }
                    } else if ("esort".equals(method)) {
                        handleSort(parameters);
                    }

                }
            };
            scrollComposite.setContent(_browser);
            scrollComposite.setLayoutData(gridData);
            hookKeyListener(scrollComposite);
        }
    }

    _browser.addFocusListener(new FocusListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void focusLost(FocusEvent arg0) {
            setHasFocus(false);

        }

        @Override
        public void focusGained(FocusEvent arg0) {
            setHasFocus(true);

        }
    });
    _browser.addMouseListener(new MouseAdapter() {

        private static final long serialVersionUID = 1L;

        @Override
        public void mouseDown(MouseEvent arg0) {
            setHasFocus(true);

        }

    });

    if (_items.isEmpty()) {
        Collection<EJItemGroupProperties> allItemGroupProperties = _block.getProperties()
                .getScreenItemGroupContainer(EJScreenType.MAIN).getAllItemGroupProperties();

        int cellSpacing = blockProperties.getBlockRendererProperties().getIntProperty(CELL_SPACING_PROPERTY, 0);
        int cellPadding = blockProperties.getBlockRendererProperties().getIntProperty(CELL_PADDING_PROPERTY, 0);
        String paddingStyle = null;
        if (cellPadding > 0) {
            String str = String.valueOf(cellPadding);
            paddingStyle = String.format("padding: %spx %spx %spx %spx; ", str, str, str, str);
        }

        StringBuilder header = new StringBuilder();
        for (EJItemGroupProperties groupProperties : allItemGroupProperties) {
            Collection<EJScreenItemProperties> itemProperties = groupProperties.getAllItemProperties();
            for (EJScreenItemProperties screenItemProperties : itemProperties) {
                EJCoreMainScreenItemProperties itemProps = (EJCoreMainScreenItemProperties) screenItemProperties;

                EJScreenItemController item = _block.getScreenItem(EJScreenType.MAIN,
                        itemProps.getReferencedItemName());
                EJManagedItemRendererWrapper renderer = item.getManagedItemRenderer();
                if (renderer != null) {
                    EJRWTAppItemRenderer itemRenderer = (EJRWTAppItemRenderer) renderer.getUnmanagedRenderer();

                    ColumnLabelProvider labelProvider = itemRenderer.createColumnLabelProvider(itemProps, item);
                    _items.add(itemProps);
                    _itemLabelProviders.put(itemProps.getReferencedItemName(), labelProvider);

                    if (addHeader) {

                        String styleClass = "default_all";
                        EJFrameworkExtensionProperties rendererProperties = item.getReferencedItemProperties()
                                .getItemRendererProperties();
                        header.append("<th ");

                        String alignment = null;

                        String alignmentProperty = rendererProperties.getStringProperty(PROPERTY_ALIGNMENT);
                        if (alignmentProperty == null) {
                            alignmentProperty = rendererProperties.getStringProperty("ALLIGNMENT");
                        }
                        alignment = getComponentAlignment(alignmentProperty);

                        EJFrameworkExtensionProperties extentionProperties = itemProps
                                .getBlockRendererRequiredProperties();

                        SortInfo sortInfo = null;
                        if (extentionProperties.getBooleanProperty(ALLOW_ROW_SORTING, true)) {
                            EJRWTAbstractTableSorter columnSorter = itemRenderer.getColumnSorter(itemProps,
                                    item);
                            if (columnSorter != null) {
                                _itemSortProviders.put(itemProps.getReferencedItemName(), columnSorter);
                                sortInfo = new SortInfo();
                                sortInfo.columnName = itemProps.getReferencedItemName();
                                _sortContext.put(sortInfo.id, sortInfo);
                            }
                        }

                        String functionDef = null;
                        if (sortInfo != null) {
                            functionDef = String.format("em='esort' earg='%s' ", sortInfo.id);
                        }

                        String valueVA = blockProperties.getBlockRendererProperties()
                                .getStringProperty(HEADER_VA);
                        if (valueVA != null && valueVA.length() > 0) {
                            styleClass = valueVA;
                            valueVA = rendererProperties.getStringProperty(HEADER_VA);
                            if (valueVA != null && valueVA.length() > 0)
                                styleClass = valueVA;
                        }
                        header.append(String.format(" class=\"%s\" ", styleClass));
                        if (alignment != null) {
                            header.append(String.format(" align=\'%s\'", alignment));
                        }
                        if (paddingStyle != null) {
                            header.append(String.format(" style=\'%s\'", paddingStyle));
                        }

                        float width = extentionProperties.getIntProperty(DISPLAY_WIDTH_PROPERTY, 0);
                        if (width > 0) {
                            Font font = labelProvider.getFont(new Object());

                            if (font == null)
                                font = _browser.getFont();
                            if (font != null) {
                                float avgCharWidth = RWTUtils.getAvgCharWidth(font);
                                if (avgCharWidth > 0) {
                                    if (width != 1) {
                                        // add +1 padding
                                        width = ((int) (((width + 1) * avgCharWidth)));
                                    }
                                }
                            }

                            header.append(String.format(" width=%s ", width));
                        }
                        header.append("> ");

                        if (itemProps.getLabel() != null) {
                            if (functionDef != null) {
                                header.append(String.format("<ejl><u %s class=\"%s %s\"  ",
                                        "style=\"line-height: 100%\"",
                                        ("default_all".equals(styleClass) ? "default_link_fg" : "default_link"),
                                        styleClass));
                                header.append(functionDef).append(">");
                            }
                            header.append(itemProps.getLabel());
                            if (sortInfo != null)
                                header.append(String.format("<esh %s/>", sortInfo.id));
                        }
                        header.append("</th>");
                    }
                }
            }
        }

        if (addHeader) {
            _headerTag = header.toString();
        }
    }
    hookKeyListener(_browser);

    final EJRWTAbstractFilteredHtml _filterHtml = filterHtml;
    _filteredContentProvider = new FilteredContentProvider() {

        boolean matchItem(EJDataRecord rec) {
            if (filter != null && filter.trim().length() > 0) {
                for (ColumnLabelProvider filterTextProvider : _itemLabelProviders.values()) {

                    String text = filterTextProvider.getText(rec);
                    if (text != null && text.toLowerCase().contains(filter.toLowerCase())) {
                        return true;
                    }
                }
                //if no match try to match Numeric value  
                try {
                    double parseDouble = Double.parseDouble(filter);
                    for (String item : _itemLabelProviders.keySet()) {
                        Object value = rec.getValue(item);
                        if (value instanceof Number) {
                            if (((Number) value).doubleValue() == parseDouble) {
                                return true;
                            }
                        }
                    }
                } catch (NumberFormatException e) {
                    //ignore
                }
            }
            return false;
        }

        @Override
        public void setFilter(String filter) {
            if (getFilter() != null && getFilter().equals(filter))
                return;
            super.setFilter(filter);
            _tableBaseRecords.clear();

            if (filter == null || filter.trim().length() == 0) {
                if (_filterHtml != null) {
                    _filterHtml.clearText();
                }
                _tableBaseRecords.addAll(_block.getBlock().getRecords());
            } else {
                for (EJDataRecord record : _block.getBlock().getRecords()) {
                    if (matchItem(record)) {
                        _tableBaseRecords.add(record);
                    }
                }
            }

        }
    };

    createHTML();

}

From source file:org.entirej.applicationframework.rwt.renderers.html.EJRWTHtmlTableBlockRenderer.java

License:Apache License

private void createHTML() {

    if (_browser == null || _browser.isDisposed()) {
        return;// w w w.ja v a2 s. co m
    }

    StringBuilder builder = new StringBuilder();
    {
        builder.append("<div id=\"table\" style=\"float:left;width:100%;height:100%; overflow:auto\">");
        {
            EJCoreBlockProperties blockProperties = _block.getProperties();
            int cellSpacing = blockProperties.getBlockRendererProperties().getIntProperty(CELL_SPACING_PROPERTY,
                    0);
            int cellPadding = blockProperties.getBlockRendererProperties().getIntProperty(CELL_PADDING_PROPERTY,
                    0);
            String paddingStyle = null;
            if (cellPadding > 0) {
                String str = String.valueOf(cellPadding);
                paddingStyle = String.format("padding: %spx %spx %spx %spx; ", str, str, str, str);
            }
            builder.append("<table border=0 cellspacing=").append(cellSpacing).append(" width=\"100%\" >");
            {
                builder.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
                builder.append(createVACSSUrl());
                builder.append("\">");
                int charHeight = EJRWTImageRetriever.getGraphicsProvider()
                        .getCharHeight(Display.getDefault().getSystemFont());
                String trDef = String.format("<tr style=\"height: %spx\">", String.valueOf(charHeight));

                if (_headerTag != null) {
                    String sortHeader = _headerTag;
                    if (activeSortColumn != null) {
                        StringBuilder header = new StringBuilder();
                        if (activeSortColumn.direction != SortInfo.DIRECTION.NONE) {
                            header.append("&nbsp <img name=\"open\" ");
                            header.append("src=\"");
                            header.append(createImageUrl(activeSortColumn.direction == SortInfo.DIRECTION.ASC
                                    ? "resource/widget/rap/column/sort-indicator-up.png"
                                    : "resource/widget/rap/column/sort-indicator-down.png"));
                            header.append("\"");
                            header.append(" >");
                        }

                        sortHeader = _headerTag.replace(String.format("<esh %s/>", activeSortColumn.id),
                                header.toString());
                    }
                    builder.append(sortHeader);
                }

                Collection<EJDataRecord> records = _tableBaseRecords;

                if (records.size() > 0) {

                    records = sortedRecords(records);
                    int lastRowSpan = 0;

                    String oddVA = "default_all";
                    String valueVA = blockProperties.getBlockRendererProperties().getStringProperty(ROW_ODD_VA);
                    if (valueVA != null && valueVA.length() > 0) {
                        oddVA = valueVA;
                    }
                    String evenVA = "default_all";
                    valueVA = blockProperties.getBlockRendererProperties().getStringProperty(ROW_EVEN_VA);
                    if (valueVA != null && valueVA.length() > 0) {
                        evenVA = valueVA;
                    }
                    int rowid = 0;
                    for (EJDataRecord record : records) {
                        rowid++;
                        if (lastRowSpan > 1) {
                            for (int i = 1; i < lastRowSpan; i++) {
                                builder.append(trDef).append("</tr>");

                            }
                            lastRowSpan = 0;
                        }
                        builder.append(trDef);
                        for (EJCoreMainScreenItemProperties item : _items) {
                            String styleClass = (rowid % 2) != 0 ? oddVA : evenVA;

                            String actionDef = null;
                            String alignment = null;
                            float width = -1;

                            ColumnLabelProvider columnLabelProvider = _itemLabelProviders
                                    .get(item.getReferencedItemName());

                            EJScreenItemController screenItem = _block.getScreenItem(EJScreenType.MAIN,
                                    item.getReferencedItemName());
                            EJCoreVisualAttributeProperties iva = screenItem.getManagedItemRenderer()
                                    .getVisualAttributeProperties();
                            if (iva != null) {
                                styleClass = iva.getName();
                            }

                            EJFrameworkExtensionProperties rendererProperties = item
                                    .getReferencedItemProperties().getItemRendererProperties();

                            EJCoreVisualAttributeProperties diva = record.getItem(item.getReferencedItemName())
                                    .getVisualAttribute();
                            if (diva != null) {
                                styleClass = diva.getName();
                            }
                            builder.append(String.format("<td class=\"%s\" ", styleClass));
                            if (paddingStyle != null) {
                                builder.append(String.format(" style=\'%s\'", paddingStyle));
                            }

                            EJFrameworkExtensionProperties extentionProperties = item
                                    .getBlockRendererRequiredProperties();
                            if (width == -1) {
                                width = extentionProperties.getIntProperty(DISPLAY_WIDTH_PROPERTY, 0);
                            }

                            String action = extentionProperties.getStringProperty(CELL_ACTION_COMMAND);
                            if (action == null || action.length() == 0) {
                                action = item.getActionCommand();
                            }
                            if (action != null && action.length() > 0) {
                                actionDef = String.format("em='eaction' earg='%s , %s' ", action,
                                        String.valueOf(getDisplayedRecordNumber(record)));

                            }

                            if (width > 0) {
                                Font font = columnLabelProvider.getFont(new Object());

                                if (font == null)
                                    font = _browser.getFont();
                                if (font != null) {
                                    float avgCharWidth = RWTUtils.getAvgCharWidth(font);
                                    if (avgCharWidth > 0) {
                                        if (width != 1) {
                                            // add +1 padding
                                            width = ((int) (((width + 1) * avgCharWidth)));
                                        }
                                    }
                                }

                                builder.append(String.format(" width=%s ", width));
                            }
                            if (alignment == null) {
                                String alignmentProperty = rendererProperties
                                        .getStringProperty(PROPERTY_ALIGNMENT);
                                if (alignmentProperty == null) {
                                    alignmentProperty = rendererProperties.getStringProperty("ALLIGNMENT");
                                }
                                alignment = getComponentAlignment(alignmentProperty);

                            }
                            if (alignment != null) {
                                builder.append(String.format(" align=\'%s\'", alignment));
                            }
                            final String caseProperty = getComponentCase(
                                    rendererProperties.getStringProperty(PROPERTY_CASE));

                            builder.append(String.format(" font style=\'%s\'", caseProperty));

                            builder.append(">");

                            String text = columnLabelProvider.getText(record);

                            if (actionDef != null && text != null && text.length() > 0) {
                                builder.append(String.format("<ejl><u %s class=\"%s %s\"  ",
                                        "style=\"line-height: 100%\"",
                                        ("default_all".equals(styleClass) ? "default_link_fg" : "default_link"),
                                        styleClass));
                                builder.append(actionDef).append(">");
                            }

                            Image image = columnLabelProvider.getImage(record);
                            if (image != null) {
                                if (actionDef == null) {
                                    builder.append("<img src=\"");

                                    builder.append(ImageFactory.getImagePath(image));

                                    builder.append("\"");
                                    builder.append(String.format(" class=\"default %s\"  >", styleClass));
                                } else

                                {
                                    builder.append("<ejl><img src=\"");
                                    builder.append(ImageFactory.getImagePath(image));
                                    builder.append("\"");
                                    builder.append(
                                            String.format("style=\"cursor: hand;\" class=\"%s \" %s  > </ejl>",
                                                    styleClass, actionDef));
                                }
                            }
                            // builder.append(String.format("<p class=\"default %s\">",
                            // styleClass));

                            builder.append(text);
                            builder.append("</td>");
                        }
                        builder.append("</tr>");
                    }

                } else {
                    builder.append(trDef);
                    for (EJCoreMainScreenItemProperties item : _items) {
                        String padding = paddingStyle;
                        float width = -1;

                        ColumnLabelProvider columnLabelProvider = _itemLabelProviders
                                .get(item.getReferencedItemName());

                        builder.append(String.format("<td class=\"%s\" ", "default_all"));
                        if (padding != null) {
                            builder.append(String.format(" style=\'%s\'", padding));
                        }

                        EJFrameworkExtensionProperties extentionProperties = item
                                .getBlockRendererRequiredProperties();
                        if (width == -1) {
                            width = extentionProperties.getIntProperty(DISPLAY_WIDTH_PROPERTY, 0);
                        }

                        if (width > 0) {
                            Font font = columnLabelProvider.getFont(new Object());

                            if (font == null)
                                font = _browser.getFont();
                            if (font != null) {
                                float avgCharWidth = RWTUtils.getAvgCharWidth(font);
                                if (avgCharWidth > 0) {
                                    if (width != 1) {
                                        // add +1 padding
                                        width = ((int) (((width + 1) * avgCharWidth)));
                                    }
                                }
                            }
                            builder.append(String.format(" width=%s ", width));
                        }

                        builder.append(">");
                        builder.append("</td>");

                    }
                    builder.append("</tr>");
                }
            }
            builder.append("</table>");
        }
        builder.append("</<div>");
    }
    String html = builder.toString();
    if (_browser.getText() == null || (!html.equals(_browser.getText()))) {
        _browser.setText(html);
        LOGGER.debug(html);
    }

}

From source file:org.entirej.applicationframework.rwt.renderers.lov.EJRWTStandardLovRenderer.java

License:Apache License

public ColumnLabelProvider createScreenItem(EJRWTTableViewerColumnFactory factory,
        EJCoreMainScreenItemProperties itemProps) {
    if (itemProps.isSpacerItem()) {

        return null;
    }//  ww  w. j av  a 2  s .  c  o m
    EJScreenItemController item = _block.getScreenItem(EJScreenType.MAIN, itemProps.getReferencedItemName());
    EJManagedItemRendererWrapper renderer = item.getManagedItemRenderer();
    if (renderer != null) {
        EJFrameworkExtensionProperties blockProperties = itemProps.getLovRendererRequiredProperties();

        EJRWTAppItemRenderer itemRenderer = (EJRWTAppItemRenderer) renderer.getUnmanagedRenderer();
        ColumnLabelProvider labelProvider = itemRenderer.createColumnLabelProvider(itemProps, item);
        if (labelProvider != null) {

            int _widthHint = blockProperties
                    .getIntProperty(EJRWTMultiRecordBlockDefinitionProperties.DISPLAY_WIDTH_PROPERTY, 0);
            // if no width define in block properties use item renderer pref
            // width
            if (_widthHint == 0) {
                if (itemProps.getLabel() != null) {
                    _widthHint = itemProps.getLabel().length() + 2;// offset
                } else {
                    _widthHint = 5;
                }
            }

            String alignmentProperty = blockProperties
                    .getStringProperty(EJRWTMultiRecordBlockDefinitionProperties.COLUMN_ALIGNMENT);

            TableViewerColumn viewerColumn = factory.createColumn(itemProps.getLabel(), _widthHint,
                    labelProvider, getComponentStyle(alignmentProperty));
            TableColumn column = viewerColumn.getColumn();
            column.setToolTipText(itemProps.getHint());

            column.setMoveable(blockProperties
                    .getBooleanProperty(EJRWTMultiRecordBlockDefinitionProperties.ALLOW_COLUMN_REORDER, true));
            column.setResizable(blockProperties
                    .getBooleanProperty(EJRWTMultiRecordBlockDefinitionProperties.ALLOW_COLUMN_RESIZE, true));
            if (blockProperties.getBooleanProperty(EJRWTMultiRecordBlockDefinitionProperties.ALLOW_ROW_SORTING,
                    true)) {
                EJRWTAbstractTableSorter columnSorter = itemRenderer.getColumnSorter(itemProps, item);
                if (columnSorter != null) {
                    new EJRWTTableSortSelectionListener(_tableViewer, column, columnSorter, SWT.UP, false);
                }
            }

            // ensure that the width property of the table column is in
            // Characters
            Font font = labelProvider.getFont(new Object());
            if (font == null) {
                font = _tableViewer.getTable().getFont();
            }
            if (font != null) {
                float avgCharWidth = EJRWTImageRetriever.getGraphicsProvider().getAvgCharWidth(font);
                if (avgCharWidth > 0) {
                    column.setWidth((int) ((column.getWidth() + 1) * avgCharWidth));// add
                                                                                    // +1
                                                                                    // padding
                }
            }
            return labelProvider;

        }
    }
    return null;
}

From source file:org.entirej.applicationframework.rwt.renderers.mobile.blocks.EJRWTListRecordBlockRenderer.java

License:Apache License

public void buildGuiComponent(EJRWTEntireJGridPane blockCanvas) {
    EJFrameworkExtensionProperties appProp = EJCoreProperties.getInstance().getApplicationDefinedProperties();
    if (appProp != null) {
        EJFrameworkExtensionProperties propertyGroup = appProp
                .getPropertyGroup(EJRWTSingleRecordBlockDefinitionProperties.ACTION_GROUP);
        if (propertyGroup != null) {

            addActionKeyinfo(/* w w  w.  jav  a 2s.c om*/
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_QUERY_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_QUERY_KEY);
            addActionKeyinfo(
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_INSERT_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_INSERT_KEY);
            addActionKeyinfo(
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_UPDATE_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_UPDATE_KEY);
            addActionKeyinfo(
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_DELETE_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_DELETE_KEY);
            addActionKeyinfo(
                    propertyGroup
                            .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ACTION_REFRESH_KEY),
                    EJRWTSingleRecordBlockDefinitionProperties.ACTION_REFRESH_KEY);

        }
    }
    // blockCanvas.cleanLayoutVertical();
    EJBlockProperties blockProperties = _block.getProperties();
    EJMainScreenProperties mainScreenProperties = blockProperties.getMainScreenProperties();

    GridData gridData = new GridData(GridData.FILL_BOTH);
    gridData.widthHint = mainScreenProperties.getWidth();
    gridData.heightHint = mainScreenProperties.getHeight();

    gridData.horizontalSpan = mainScreenProperties.getHorizontalSpan();
    gridData.verticalSpan = mainScreenProperties.getVerticalSpan();
    gridData.grabExcessHorizontalSpace = mainScreenProperties.canExpandHorizontally();
    gridData.grabExcessVerticalSpace = mainScreenProperties.canExpandVertically();

    if (gridData.grabExcessHorizontalSpace)
        gridData.minimumWidth = mainScreenProperties.getWidth();
    if (gridData.grabExcessVerticalSpace)
        gridData.minimumHeight = mainScreenProperties.getHeight();
    EJFrameworkExtensionProperties rendererProp = blockProperties.getBlockRendererProperties();
    blockCanvas.setLayoutData(gridData);

    EJFrameworkExtensionProperties sectionProperties = null;
    if (rendererProp != null) {
        sectionProperties = rendererProp
                .getPropertyGroup(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR);
    }
    if (sectionProperties != null
            && sectionProperties.getStringProperty(
                    EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE) != null
            && !EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE_GROUP
                    .equals(sectionProperties.getStringProperty(
                            EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE))) {
        int style = ExpandableComposite.TITLE_BAR;

        String mode = sectionProperties
                .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE);
        if (EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE_TWISTIE.equals(mode)) {
            style = style | ExpandableComposite.TWISTIE;
        } else if (EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_MODE_TREE_NODE
                .equals(mode)) {
            style = style | ExpandableComposite.TREE_NODE;
        }
        if (sectionProperties.getBooleanProperty(
                EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_EXPANDED, true)) {
            style = style | ExpandableComposite.EXPANDED;
        }
        Section section = toolkit.createSection(blockCanvas, style);
        section.setLayoutData(gridData);
        String title = sectionProperties
                .getStringProperty(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_TITLE);
        if (title != null)
            section.setText(title);
        EJRWTImageRetriever.getGraphicsProvider().rendererSection(section);
        if (mainScreenProperties.getDisplayFrame()) {

            Group group = new Group(section, SWT.NONE);
            group.setLayout(new FillLayout());
            group.setLayoutData(gridData);
            hookKeyListener(group);
            String frameTitle = mainScreenProperties.getFrameTitle();
            if (frameTitle != null && frameTitle.length() > 0) {
                group.setText(frameTitle);
            }
            _mainPane = new EJRWTEntireJGridPane(group, 1);
            section.setClient(group);

        } else {
            _mainPane = new EJRWTEntireJGridPane(section, 1);
            _mainPane.setLayoutData(gridData);
            _mainPane.cleanLayoutHorizontal();
            section.setClient(_mainPane);
        }

        final EJFrameworkExtensionPropertyList propertyList = sectionProperties
                .getPropertyList(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTIONS);

        if (propertyList != null && propertyList.getAllListEntries().size() > 0) {
            ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
            final ToolBar toolbar = toolBarManager.createControl(section);
            final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
            toolbar.setCursor(handCursor);
            // Cursor needs to be explicitly disposed
            toolbar.addDisposeListener(new DisposeListener() {
                public void widgetDisposed(DisposeEvent e) {
                    if ((handCursor != null) && (handCursor.isDisposed() == false)) {
                        handCursor.dispose();
                    }
                }
            });
            List<EJFrameworkExtensionPropertyListEntry> allListEntries = propertyList.getAllListEntries();
            for (EJFrameworkExtensionPropertyListEntry entry : allListEntries) {
                final String actionID = entry
                        .getProperty(EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTION_ID);
                String actionImage = entry.getProperty(
                        EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTION_IMAGE);
                String actionName = entry.getProperty(
                        EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTION_NAME);
                String actionTooltip = entry.getProperty(
                        EJRWTSingleRecordBlockDefinitionProperties.ITEM_GROUP_TITLE_BAR_ACTION_TOOLTIP);

                if (actionID != null) {
                    Action action = new Action(actionID, IAction.AS_PUSH_BUTTON) {

                        @Override
                        public void runWithEvent(Event event) {

                            _block.executeActionCommand(actionID, EJScreenType.MAIN);
                        }

                    };
                    if (actionName != null)
                        action.setText(actionName);
                    if (actionTooltip != null)
                        action.setDescription(actionTooltip);
                    if (actionImage != null && actionImage.length() > 0) {

                        action.setImageDescriptor(
                                ImageDescriptor.createFromImage(EJRWTImageRetriever.get(actionImage)));
                    }
                    toolBarManager.add(action);
                }

            }

            toolBarManager.update(true);

            section.setTextClient(toolbar);
        }
    } else {
        if (mainScreenProperties.getDisplayFrame()) {

            Group group = new Group(blockCanvas, SWT.NONE);
            group.setLayout(new FillLayout());
            group.setLayoutData(gridData);
            hookKeyListener(group);
            String frameTitle = mainScreenProperties.getFrameTitle();
            if (frameTitle != null && frameTitle.length() > 0) {
                group.setText(frameTitle);
            }
            _mainPane = new EJRWTEntireJGridPane(group, 1);

        } else {
            _mainPane = new EJRWTEntireJGridPane(blockCanvas, 1);
            _mainPane.setLayoutData(gridData);
            _mainPane.cleanLayoutHorizontal();
        }
    }

    hookKeyListener(_mainPane);
    int style = SWT.VIRTUAL | SWT.V_SCROLL;

    if (!rendererProp.getBooleanProperty(EJRWTListRecordBlockDefinitionProperties.HIDE_TABLE_BORDER, false))
        style = style | SWT.BORDER;

    if (rendererProp.getBooleanProperty(EJRWTListRecordBlockDefinitionProperties.ROW_SELECTION_PROPERTY, true))
        style = style | SWT.FULL_SELECTION;
    else
        style = style | SWT.HIDE_SELECTION;
    Collection<EJItemGroupProperties> allItemGroupProperties = _block.getProperties()
            .getScreenItemGroupContainer(EJScreenType.MAIN).getAllItemGroupProperties();
    final Table table;
    final boolean hideSelection = (style & SWT.HIDE_SELECTION) != 0;
    final EJRWTAbstractFilteredTable filterTree;
    if (rendererProp.getBooleanProperty(EJRWTTreeBlockDefinitionProperties.FILTER, false)) {
        if (allItemGroupProperties.size() > 0) {
            EJItemGroupProperties displayProperties = allItemGroupProperties.iterator().next();
            if (displayProperties.dispayGroupFrame()) {
                Group group = new Group(_mainPane, SWT.NONE);
                group.setLayout(new FillLayout());
                if (displayProperties.getFrameTitle() != null && displayProperties.getFrameTitle().length() > 0)
                    group.setText(displayProperties.getFrameTitle());
                group.setLayoutData(
                        new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));

                filterTree = new EJRWTAbstractFilteredTable(group, style) {
                    @Override
                    public void filter(String filter) {
                        if (filteredContentProvider != null
                                && ((filter == null && filteredContentProvider.getFilter() != null)
                                        || !filter.equals(filteredContentProvider.getFilter()))) {
                            filteredContentProvider.setFilter(filter);
                            getViewer().setInput(filter);
                            notifyStatus();
                        }
                    }

                    @Override
                    protected TableViewer doCreateTableViewer(Composite parent, int style) {
                        return _tableViewer = new TableViewer(parent) {

                            @Override
                            public void setSelection(ISelection selection) {
                                if (hideSelection)
                                    selection = new StructuredSelection();
                                super.setSelection(selection);
                            }

                            @Override
                            public void setSelection(ISelection selection, boolean reveal) {
                                if (hideSelection)
                                    selection = new StructuredSelection();
                                super.setSelection(selection, reveal);
                            }
                        };
                    }
                };
            } else {

                filterTree = new EJRWTAbstractFilteredTable(_mainPane, style) {
                    @Override
                    public void filter(String filter) {
                        if (filteredContentProvider != null
                                && ((filter == null && filteredContentProvider.getFilter() != null)
                                        || !filter.equals(filteredContentProvider.getFilter()))) {
                            filteredContentProvider.setFilter(filter);
                            getViewer().setInput(filter);
                            notifyStatus();
                        }
                    }

                    @Override
                    protected TableViewer doCreateTableViewer(Composite parent, int style) {
                        return _tableViewer = new TableViewer(parent) {

                            @Override
                            public void setSelection(ISelection selection) {
                                if (hideSelection)
                                    selection = new StructuredSelection();
                                super.setSelection(selection);
                            }

                            @Override
                            public void setSelection(ISelection selection, boolean reveal) {
                                if (hideSelection)
                                    selection = new StructuredSelection();
                                super.setSelection(selection, reveal);
                            }
                        };
                    }

                };
                filterTree.setLayoutData(
                        new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
            }
        } else {
            filterTree = new EJRWTAbstractFilteredTable(_mainPane, style) {
                @Override
                public void filter(String filter) {
                    if (filteredContentProvider != null
                            && ((filter == null && filteredContentProvider.getFilter() != null)
                                    || !filter.equals(filteredContentProvider.getFilter()))) {
                        filteredContentProvider.setFilter(filter);
                        getViewer().setInput(filter);
                        notifyStatus();
                    }
                }

                @Override
                protected TableViewer doCreateTableViewer(Composite parent, int style) {
                    return _tableViewer = new TableViewer(parent) {

                        @Override
                        public void setSelection(ISelection selection) {
                            if (hideSelection)
                                selection = new StructuredSelection();
                            super.setSelection(selection);
                        }

                        @Override
                        public void setSelection(ISelection selection, boolean reveal) {
                            if (hideSelection)
                                selection = new StructuredSelection();
                            super.setSelection(selection, reveal);
                        }
                    };
                }
            };

            filterTree.setLayoutData(
                    new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
        }
        table = (_tableViewer = filterTree.getViewer()).getTable();
    } else {
        filterTree = null;
        if (allItemGroupProperties.size() > 0) {
            EJItemGroupProperties displayProperties = allItemGroupProperties.iterator().next();
            if (displayProperties.dispayGroupFrame()) {
                Group group = new Group(_mainPane, SWT.NONE);
                group.setLayout(new FillLayout());
                if (displayProperties.getFrameTitle() != null && displayProperties.getFrameTitle().length() > 0)
                    group.setText(displayProperties.getFrameTitle());
                group.setLayoutData(
                        new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
                table = new Table(group, style);
            } else {
                table = new Table(_mainPane, style);

                table.setLayoutData(
                        new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
            }
        } else {
            table = new Table(_mainPane, style);

            table.setLayoutData(
                    new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
        }

        _tableViewer = new TableViewer(table) {

            @Override
            public void setSelection(ISelection selection) {
                if (hideSelection)
                    selection = new StructuredSelection();
                super.setSelection(selection);
            }

            @Override
            public void setSelection(ISelection selection, boolean reveal) {
                if (hideSelection)
                    selection = new StructuredSelection();
                super.setSelection(selection, reveal);
            }
        };
    }

    table.setLinesVisible(false);
    table.setHeaderVisible(false);
    Control[] children = table.getChildren();
    for (Control control : children) {
        hookKeyListener(control);
    }
    hookKeyListener(table);

    EJRWTTableViewerColumnFactory factory = new EJRWTTableViewerColumnFactory(_tableViewer);
    ColumnViewerToolTipSupport.enableFor(_tableViewer);

    final List<ColumnLabelProvider> nodeTextProviders = new ArrayList<ColumnLabelProvider>();
    final Map<ColumnLabelProvider, EJScreenItemProperties> nodeTextProvidersMap = new HashMap<ColumnLabelProvider, EJScreenItemProperties>();

    for (EJItemGroupProperties groupProperties : allItemGroupProperties) {
        Collection<EJScreenItemProperties> itemProperties = groupProperties.getAllItemProperties();

        for (EJScreenItemProperties screenItemProperties : itemProperties) {
            EJCoreMainScreenItemProperties mainScreenItemProperties = (EJCoreMainScreenItemProperties) screenItemProperties;
            ColumnLabelProvider screenItem = createScreenItem(factory, mainScreenItemProperties);
            if (screenItem != null) {
                nodeTextProviders.add(screenItem);
                nodeTextProvidersMap.put(screenItem, screenItemProperties);
            }
        }
    }

    if (!nodeTextProviders.isEmpty()) {
        table.setData(EJ_RWT.MARKUP_ENABLED, Boolean.TRUE);

        int height = rendererProp.getIntProperty(EJRWTListRecordBlockDefinitionProperties.ROW_HEIGHT, -1);

        if (height == -1 && nodeTextProviders.size() > 1) {
            table.setData(EJ_RWT.CUSTOM_ITEM_HEIGHT, Integer.valueOf(nodeTextProviders.size() * 20));
        } else if (height > 0) {
            table.setData(EJ_RWT.CUSTOM_ITEM_HEIGHT, Integer.valueOf(height));
        }

        // add double click action
        final String doubleClickActionCommand = rendererProp
                .getStringProperty(EJRWTListRecordBlockDefinitionProperties.CLICK_ACTION_COMMAND);
        final boolean hasAction = doubleClickActionCommand != null && doubleClickActionCommand.length() != 0;
        if (hasAction) {
            _tableViewer.addDoubleClickListener(new IDoubleClickListener() {

                public void doubleClick(DoubleClickEvent arg0) {
                    _block.executeActionCommand(doubleClickActionCommand, EJScreenType.MAIN);
                }
            });
        }

        final TableViewerColumn dataColumn = factory.createColumn("HTML", 500, new ColumnLabelProvider() {

            public String toHex(int r, int g, int b) {
                return "#" + toBrowserHexValue(r) + toBrowserHexValue(g) + toBrowserHexValue(b);
            }

            private String toBrowserHexValue(int number) {
                StringBuilder builder = new StringBuilder(Integer.toHexString(number & 0xff));
                while (builder.length() < 2) {
                    builder.append("0");
                }
                return builder.toString().toUpperCase();
            }

            @Override
            public String getText(Object element) {

                StringBuilder builder = new StringBuilder();
                boolean addBr = false;
                for (ColumnLabelProvider labelProvider : nodeTextProviders) {
                    if (addBr) {
                        builder.append("<br/>");
                    }

                    boolean added = false;

                    EJScreenItemProperties item = nodeTextProvidersMap.get(labelProvider);

                    if (item != null && item.getLabel() != null && item.getLabel().trim().length() > 0) {
                        builder.append(item.getLabel());
                        builder.append(" : ");
                        added = true;
                    }

                    Image image = labelProvider.getImage(element);
                    if (image != null) {
                        String imagePath = ImageFactory.getImagePath(image);
                        if (imagePath != null) {
                            builder.append("<img src=\"");
                            builder.append(imagePath);
                            Rectangle bounds = image.getBounds();
                            builder.append("\" style=\"vertical-align:middle;\" ");
                            builder.append(" width=\"");
                            builder.append(bounds.width);
                            builder.append("\" ");
                            builder.append(" height=\"");
                            builder.append(bounds.height);
                            builder.append("\"/>");
                            added = true;
                        }
                    }

                    String text = labelProvider.getText(element);
                    if (text != null && text.length() != 0) {

                        Font vaFont = labelProvider.getFont(element);
                        builder.append("<span style=\"");
                        if (vaFont != null && vaFont.getFontData().length > 0) {
                            FontData fontData = vaFont.getFontData()[0];

                            if ((fontData.getStyle() & SWT.BOLD) != 0) {
                                builder.append("font-weight:bold;");
                            }
                            if ((fontData.getStyle() & SWT.ITALIC) != 0) {
                                builder.append("font-style:italic;");
                            }

                            builder.append("font-size:");
                            builder.append(fontData.getHeight());
                            builder.append("px;");

                            builder.append("font-family:");
                            builder.append(fontData.getName().replace('"', ' '));
                            builder.append(";");

                        }
                        Color background = labelProvider.getBackground(element);
                        if (background != null) {
                            builder.append("background-color:");
                            builder.append(
                                    toHex(background.getRed(), background.getGreen(), background.getBlue()));
                            builder.append(";");
                        }
                        Color foreground = labelProvider.getForeground(element);
                        if (foreground != null) {
                            builder.append("color:");
                            builder.append(
                                    toHex(foreground.getRed(), foreground.getGreen(), foreground.getBlue()));
                            builder.append(";");
                        }

                        builder.append("\">");
                        builder.append(text);
                        builder.append("</span>");
                        added = true;
                    }
                    if (added) {
                        addBr = true;
                    }
                }
                return builder.toString();
            }
        });

        if (hasAction) {
            final Image arrow = EJRWTImageRetriever.get("icons/left-arrow.png");
            final TableViewerColumn actionColumn = factory.createColumn("HTML-ACTION", 40,
                    new ColumnLabelProvider() {

                        @Override
                        public String getText(Object element) {
                            return "";
                        }

                        @Override
                        public Image getImage(Object element) {

                            return arrow;
                        }

                    });
            actionColumn.getColumn().setAlignment(SWT.RIGHT);
            actionColumn.setEditingSupport(new EditingSupport(_tableViewer) {

                protected void setValue(Object arg0, Object arg1) {
                    // ignore

                }

                protected Object getValue(Object arg0) {
                    // ignore
                    return null;
                }

                protected CellEditor getCellEditor(Object arg0) {

                    return new CellEditor() {

                        @Override
                        protected void doSetValue(Object arg0) {
                            // ignore

                        }

                        @Override
                        protected void doSetFocus() {
                            // ignore

                        }

                        @Override
                        protected Object doGetValue() {
                            // ignore
                            return null;
                        }

                        @Override
                        protected Control createControl(Composite parent) {
                            Label action = new Label(parent, SWT.NONE);
                            action.setImage(arrow);

                            return action;
                        }
                    };
                }

                protected boolean canEdit(Object arg0) {
                    _block.executeActionCommand(doubleClickActionCommand, EJScreenType.MAIN);
                    return false;
                }
            });
        }

        table.addControlListener(new ControlAdapter() {

            @Override
            public void controlResized(ControlEvent e) {
                int offset = hasAction ? 50 : 10;
                if (table.getSize().x > offset)
                    dataColumn.getColumn().setWidth(table.getSize().x - offset);
            }
        });
    }

    table.addFocusListener(new FocusListener() {

        public void focusLost(FocusEvent arg0) {
            setHasFocus(false);

        }

        public void focusGained(FocusEvent arg0) {
            setHasFocus(true);

        }
    });
    // setHasFocus(true);
    _mainPane.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDown(MouseEvent arg0) {
            if (!table.isFocusControl())
                setHasFocus(true);

        }

    });
    table.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDown(MouseEvent arg0) {
            if (!table.isFocusControl())
                setHasFocus(true);

        }

    });
    final EJBlockController blockController = _block;

    _tableViewer.setContentProvider(filteredContentProvider = new FilteredContentProvider() {

        boolean matchItem(EJDataRecord rec) {
            if (filter != null && filter.trim().length() > 0) {
                for (ColumnLabelProvider filterTextProvider : nodeTextProviders) {
                    String text = filterTextProvider.getText(rec);
                    if ((text != null) && text.toLowerCase().contains(filter.toLowerCase())) {
                        return true;
                    }
                }
            }

            return false;
        }

        public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
            tableBaseRecords.clear();

            if (arg2 != null && arg2.equals(filter) && filter.trim().length() > 0) {
                // filter

                for (EJDataRecord record : _block.getBlock().getRecords()) {
                    if (matchItem(record))
                        tableBaseRecords.add(record);
                }
            } else {
                filter = null;
                if (filterTree != null)
                    filterTree.clearText();
                tableBaseRecords.addAll(_block.getBlock().getRecords());
            }
        }

        public void dispose() {
        }

        public Object[] getElements(Object arg0) {
            return tableBaseRecords.toArray();
        }
    });
    _tableViewer.setInput(new Object());
    selectRow(0);

    _tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent arg0) {

            EJDataRecord focusedRecord = getFocusedRecord();
            if (focusedRecord != null)
                _block.newRecordInstance(focusedRecord);
            notifyStatus();
        }
    });

}

From source file:org.entirej.custom.renderers.WorkWeekBlockRenderer.java

License:Apache License

@Override
public void buildGuiComponent(EJRWTEntireJGridPane blockCanvas) {
    if (_browser != null && !_browser.isDisposed()) {
        _browser.dispose();/*ww w .j  a v a2 s.c  o m*/
    }

    EJBlockProperties blockProperties = _block.getProperties();
    EJMainScreenProperties mainScreenProperties = blockProperties.getMainScreenProperties();

    EJFrameworkExtensionProperties blockRendererProperties = blockProperties.getBlockRendererProperties();
    boolean addHeader = true;
    if (blockRendererProperties != null) {
        addHeader = blockRendererProperties
                .getBooleanProperty(EJRWTMultiRecordBlockDefinitionProperties.SHOW_HEADING_PROPERTY, true);
        EJCoreFrameworkExtensionPropertyList propertyList = blockRendererProperties.getPropertyList(ACTIONS);

        if (propertyList != null) {
            List<EJFrameworkExtensionPropertyListEntry> allListEntries = propertyList.getAllListEntries();
            for (EJFrameworkExtensionPropertyListEntry entry : allListEntries) {
                String actionID = entry.getProperty(ACTION_ID);
                String actionkey = entry.getProperty(ACTION_KEY);
                if (actionID != null && actionkey != null && actionID.trim().length() > 0
                        && actionkey.trim().length() > 0) {
                    addActionKeyinfo(actionkey, actionID);
                }
            }
        }

    }

    final GridData gridData = new GridData(GridData.FILL_BOTH);
    gridData.widthHint = mainScreenProperties.getWidth();
    gridData.heightHint = mainScreenProperties.getHeight();

    gridData.horizontalSpan = mainScreenProperties.getHorizontalSpan();
    gridData.verticalSpan = mainScreenProperties.getVerticalSpan();
    gridData.grabExcessHorizontalSpace = mainScreenProperties.canExpandHorizontally();
    gridData.grabExcessVerticalSpace = mainScreenProperties.canExpandVertically();

    // if (gridData.grabExcessHorizontalSpace)
    // gridData.minimumHeight = mainScreenProperties.getHeight();
    // if (gridData.grabExcessVerticalSpace)
    // gridData.minimumWidth = mainScreenProperties.getHeight();
    blockCanvas.setLayoutData(gridData);

    header = new Composite(blockCanvas, SWT.NONE);

    header.setLayoutData(gridData);
    GridLayout headerLayout = new GridLayout(22, false);
    header.setLayout(headerLayout);

    initActiveWeek();
    createHeaderUI();

    scrollComposite = new ScrolledComposite(header, SWT.V_SCROLL | SWT.H_SCROLL);
    GridData data = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
    data.horizontalSpan = headerLayout.numColumns;
    scrollComposite.setLayoutData(data);
    scrollComposite.setExpandHorizontal(true);
    scrollComposite.setExpandVertical(true);
    scrollComposite.setMinSize(mainScreenProperties.getWidth(), mainScreenProperties.getHeight());
    if (mainScreenProperties.getDisplayFrame()) {
        String frameTitle = mainScreenProperties.getFrameTitle();
        if (frameTitle != null && frameTitle.length() > 0) {
            Group group = new Group(scrollComposite, SWT.NONE);
            hookKeyListener(group);
            scrollComposite.setContent(group);
            group.setLayout(new FillLayout());

            if (frameTitle != null && frameTitle.length() > 0) {
                group.setText(frameTitle);
            }
            _browser = new EJRWTHtmlView(group, SWT.NONE, true) {
                private static final long serialVersionUID = 1L;

                @Override
                public void action(String method, JsonObject parameters) {
                    if ("eaction".equals(method)) {
                        final Object arg1 = parameters.get("0").asString();
                        Object arg2 = parameters.get("1").asString();
                        if (arg1 instanceof String) {
                            if (arg2 instanceof String) {
                                currentRec = getRecordAt(Integer.valueOf((String) arg2));
                                if (currentRec != null)
                                    _block.newRecordInstance(currentRec);
                            }
                            Display.getDefault().asyncExec(new Runnable() {

                                @Override
                                public void run() {
                                    _block.executeActionCommand((String) arg1, EJScreenType.MAIN);
                                }
                            });

                        }
                    }

                }
            };
        } else {
            _browser = new EJRWTHtmlView(scrollComposite, SWT.BORDER, true) {

                private static final long serialVersionUID = 1L;

                @Override
                public void action(String method, JsonObject parameters) {
                    if ("eaction".equals(method)) {
                        final Object arg1 = parameters.get("0").asString();
                        Object arg2 = parameters.get("1").asString();
                        if (arg1 instanceof String) {
                            if (arg2 instanceof String) {
                                currentRec = getRecordAt(Integer.valueOf((String) arg2));
                                if (currentRec != null)
                                    _block.newRecordInstance(currentRec);
                            }
                            Display.getDefault().asyncExec(new Runnable() {

                                @Override
                                public void run() {
                                    _block.executeActionCommand((String) arg1, EJScreenType.MAIN);
                                }
                            });

                        }
                    }

                }
            };
            scrollComposite.setContent(_browser);
        }

    } else {
        _browser = new EJRWTHtmlView(scrollComposite, SWT.NONE, true) {
            private static final long serialVersionUID = 1L;

            @Override
            public void action(String method, JsonObject parameters) {

                if ("eaction".equals(method)) {
                    final Object arg1 = parameters.get("0").asString();
                    Object arg2 = parameters.get("1").asString();
                    if (arg1 instanceof String) {
                        if (arg2 instanceof String) {
                            currentRec = getRecordAt(Integer.valueOf((String) arg2));
                            if (currentRec != null)
                                _block.newRecordInstance(currentRec);
                        }
                        Display.getDefault().asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                _block.executeActionCommand((String) arg1, EJScreenType.MAIN);
                            }
                        });

                    }
                }

            }
        };
        scrollComposite.setContent(_browser);
        hookKeyListener(scrollComposite);
    }

    _browser.addFocusListener(new FocusListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void focusLost(FocusEvent arg0) {
            setHasFocus(false);

        }

        @Override
        public void focusGained(FocusEvent arg0) {
            setHasFocus(true);

        }
    });
    _browser.addMouseListener(new MouseAdapter() {

        private static final long serialVersionUID = 1L;

        @Override
        public void mouseDown(MouseEvent arg0) {
            setHasFocus(true);

        }

    });

    if (_items.isEmpty()) {
        Collection<EJItemGroupProperties> allItemGroupProperties = _block.getProperties()
                .getScreenItemGroupContainer(EJScreenType.MAIN).getAllItemGroupProperties();

        int cellSpacing = blockProperties.getBlockRendererProperties().getIntProperty(CELL_SPACING_PROPERTY, 0);
        int cellPadding = blockProperties.getBlockRendererProperties().getIntProperty(CELL_PADDING_PROPERTY, 0);
        String paddingStyle = "";
        String cellStyleTOP = "";
        String cellStyleLEFT = "";
        if (cellPadding > 0) {
            String str = String.valueOf(cellPadding);
            paddingStyle = String.format("padding: %spx %spx %spx %spx; ", str, str, str, str);
        }
        if (cellSpacing > 0) {
            cellStyleTOP = String.format("border-top: %dpx  solid  #9e9e9e; ", cellSpacing);
            cellStyleLEFT = String.format(" border-left: %dpx  solid  #9e9e9e;", cellSpacing);
        }

        StringBuilder header = new StringBuilder();
        {
            if (addHeader) {

                String styleClass = "default_all";

                header.append("<th ");

                String alignment = "center";

                String valueVA = blockProperties.getBlockRendererProperties().getStringProperty(HEADER_VA);
                if (valueVA != null && valueVA.length() > 0) {
                    styleClass = valueVA;

                }
                header.append(String.format(" class=\"%s\" ", styleClass));
                if (alignment != null) {
                    header.append(String.format(" align=\'%s\'", alignment));
                }
                if (paddingStyle != null) {
                    header.append(String.format(" style=\'%s\'", paddingStyle + cellStyleTOP + cellStyleLEFT));
                }
                header.append("> ");

                header.append("Day");
                header.append("</th>");
            }
        }
        for (EJItemGroupProperties groupProperties : allItemGroupProperties) {
            Collection<EJScreenItemProperties> itemProperties = groupProperties.getAllItemProperties();
            for (EJScreenItemProperties screenItemProperties : itemProperties) {
                EJCoreMainScreenItemProperties itemProps = (EJCoreMainScreenItemProperties) screenItemProperties;

                EJScreenItemController item = _block.getScreenItem(EJScreenType.MAIN,
                        itemProps.getReferencedItemName());
                EJManagedItemRendererWrapper renderer = item.getManagedItemRenderer();
                if (renderer != null) {
                    EJRWTAppItemRenderer itemRenderer = (EJRWTAppItemRenderer) renderer.getUnmanagedRenderer();

                    ColumnLabelProvider labelProvider = itemRenderer.createColumnLabelProvider(itemProps, item);
                    _items.add(itemProps);
                    _itemLabelProviders.put(itemProps.getReferencedItemName(), labelProvider);

                    if (addHeader) {

                        String styleClass = "default_all";
                        EJFrameworkExtensionProperties rendererProperties = item.getReferencedItemProperties()
                                .getItemRendererProperties();
                        header.append("<th ");

                        String alignment = null;

                        String alignmentProperty = rendererProperties.getStringProperty(PROPERTY_ALIGNMENT);
                        if (alignmentProperty == null) {
                            alignmentProperty = rendererProperties.getStringProperty("ALLIGNMENT");
                        }
                        alignment = getComponentAlignment(alignmentProperty);

                        int width = -1;
                        if (width == -1) {
                            width = rendererProperties.getIntProperty(DISPLAY_WIDTH_PROPERTY, 0);
                        }

                        if (width > 0) {
                            Font font = labelProvider.getFont(new Object());

                            if (font == null)
                                font = _browser.getFont();
                            if (font != null) {
                                float avgCharWidth = RWTUtils.getAvgCharWidth(font);
                                if (avgCharWidth > 0) {
                                    if (width != 1) {
                                        // add +1 padding
                                        width = ((int) (((width + 1) * avgCharWidth)));
                                    }
                                }
                            }
                            header.append(String.format(" width=%s ", width));

                        }

                        String functionDef = null;

                        String valueVA = blockProperties.getBlockRendererProperties()
                                .getStringProperty(HEADER_VA);
                        if (valueVA != null && valueVA.length() > 0) {
                            styleClass = valueVA;
                            valueVA = rendererProperties.getStringProperty(HEADER_VA);
                            if (valueVA != null && valueVA.length() > 0)
                                styleClass = valueVA;
                        }
                        header.append(String.format(" class=\"%s\" ", styleClass));
                        if (alignment != null) {
                            header.append(String.format(" align=\'%s\'", alignment));
                        }
                        if (paddingStyle != null) {
                            header.append(String.format(" style=\'%s\'",
                                    paddingStyle + cellStyleTOP + cellStyleLEFT));
                        }
                        header.append("> ");
                        if (functionDef != null) {
                            header.append(String.format("<ejl><u %s class=\"%s %s\"  ",
                                    "style=\"line-height: 130%\"",
                                    ("default_all".equals(styleClass) ? "default_link_fg" : "default_link"),
                                    styleClass));
                            header.append(functionDef).append(">");
                        }
                        if (itemProps.getLabel() != null)
                            header.append(itemProps.getLabel());
                        header.append("</th>");
                    }
                }
            }
        }

        if (addHeader) {
            _headerTag = header.toString();
        }
    }
    hookKeyListener(_browser);
    createHTML();

}

From source file:org.entirej.custom.renderers.WorkWeekBlockRenderer.java

License:Apache License

private void createHTML() {

    if (_browser == null || _browser.isDisposed()) {
        return;// ww w  .  ja  v a2 s. com
    }

    StringBuilder builder = new StringBuilder();
    {

        builder.append("<div id=\"table\" style=\"float:left;width:100%;height:100%; overflow:auto\">");
        {
            EJCoreBlockProperties blockProperties = _block.getProperties();
            int cellSpacing = blockProperties.getBlockRendererProperties().getIntProperty(CELL_SPACING_PROPERTY,
                    0);
            int cellPadding = blockProperties.getBlockRendererProperties().getIntProperty(CELL_PADDING_PROPERTY,
                    0);
            String paddingStyle = "";
            String cellStyleTOP = "";
            String cellStyleLEFT = "";
            if (cellPadding > 0) {
                String str = String.valueOf(cellPadding);
                paddingStyle = String.format("padding: %spx %spx %spx %spx; ", str, str, str, str);
            }
            if (cellSpacing > 0) {
                cellStyleTOP = String.format("border-top: %dpx  solid  #9e9e9e; ", cellSpacing);
                cellStyleLEFT = String.format(" border-left: %dpx  solid  #9e9e9e;", cellSpacing);
            }
            builder.append("<table cellspacing=").append(0).append(
                    " width=\"100%\" style='border-bottom: 1px  solid  #9e9e9e;border-right: 1px  solid  #9e9e9e;'>");
            {
                builder.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
                builder.append(createVACSSUrl());
                builder.append("\">");
                int charHeight = EJRWTImageRetriever.getGraphicsProvider()
                        .getCharHeight(Display.getDefault().getSystemFont());
                String trDef = String.format("<tr style=\"height: %spx\">", String.valueOf(charHeight));

                if (_headerTag != null) {
                    String sortHeader = _headerTag;

                    builder.append(sortHeader);
                }

                String oddVA = "default_all";
                String valueVA = blockProperties.getBlockRendererProperties().getStringProperty(ROW_ODD_VA);
                if (valueVA != null && valueVA.length() > 0) {
                    oddVA = valueVA;
                }
                String evenVA = "default_all";
                valueVA = blockProperties.getBlockRendererProperties().getStringProperty(ROW_EVEN_VA);
                if (valueVA != null && valueVA.length() > 0) {
                    evenVA = valueVA;
                }

                int[] WEEK_DAYS = new int[] { Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY,
                        Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY, Calendar.SUNDAY };

                Map<Integer, List<EJDataRecord>> grouped = getGrouped(_block.getRecords());
                int rowid = 0;
                for (int DAY : WEEK_DAYS) {
                    rowid++;
                    final String styleClass = (rowid % 2) != 0 ? oddVA : evenVA;
                    List<EJDataRecord> list = grouped.get(DAY);
                    if (list == null || list.size() == 0) {
                        boolean addDAYTD = true;
                        int emptyLines = 2;
                        for (int i = 0; i < emptyLines; i++) {

                            builder.append(trDef);

                            {
                                if (addDAYTD) {
                                    addDAYTD = false;

                                    addDateTD(builder, paddingStyle + cellStyleTOP + cellStyleLEFT, DAY,
                                            emptyLines, styleClass);
                                }

                                addEmptyLine(builder, paddingStyle, styleClass, i == 0 ? cellStyleTOP : "",
                                        cellStyleLEFT);

                            }
                        }

                        continue;
                    }

                    Collections.sort(list, new Comparator<EJDataRecord>() {

                        @Override
                        public int compare(EJDataRecord o1, EJDataRecord o2) {
                            TimeEntry e1 = (TimeEntry) o1.getServicePojo();
                            TimeEntry e2 = (TimeEntry) o2.getServicePojo();
                            return (e2.getStartTime().compareTo(e1.getStartTime()));
                        }
                    });

                    boolean addDAYTD = true;
                    int size = list.size() + 1;// add total line
                    for (EJDataRecord record : list) {

                        builder.append(trDef);

                        {
                            if (addDAYTD) {
                                addDAYTD = false;

                                addDateTD(builder, paddingStyle + cellStyleTOP + cellStyleLEFT, DAY, size,
                                        styleClass);
                            }

                        }

                        for (EJCoreMainScreenItemProperties item : _items) {

                            String actionDef = null;
                            String alignment = null;
                            float width = -1;

                            ColumnLabelProvider columnLabelProvider = _itemLabelProviders
                                    .get(item.getReferencedItemName());

                            EJScreenItemController screenItem = _block.getScreenItem(EJScreenType.MAIN,
                                    item.getReferencedItemName());
                            String computeStyleClass = styleClass;
                            EJCoreVisualAttributeProperties iva = screenItem.getManagedItemRenderer()
                                    .getVisualAttributeProperties();
                            if (iva != null) {
                                computeStyleClass = iva.getName();
                            }

                            EJFrameworkExtensionProperties rendererProperties = item
                                    .getReferencedItemProperties().getItemRendererProperties();

                            EJCoreVisualAttributeProperties diva = record.getItem(item.getReferencedItemName())
                                    .getVisualAttribute();
                            if (diva != null) {
                                computeStyleClass = diva.getName();
                            }
                            builder.append(String.format("<td class=\"%s\" ", computeStyleClass));
                            if (paddingStyle != null) {
                                builder.append(String.format(" style=\'%s\'",
                                        paddingStyle + cellStyleTOP + cellStyleLEFT));
                            }

                            EJFrameworkExtensionProperties extentionProperties = item
                                    .getBlockRendererRequiredProperties();
                            if (width == -1) {
                                width = extentionProperties.getIntProperty(DISPLAY_WIDTH_PROPERTY, 0);
                            }

                            String action = extentionProperties.getStringProperty(CELL_ACTION_COMMAND);

                            if (action != null && action.length() > 0) {
                                actionDef = String.format("em='eaction' earg='%s , %s' ", action,
                                        String.valueOf(getDisplayedRecordNumber(record)));

                            }

                            if (width > 0) {
                                Font font = columnLabelProvider.getFont(new Object());

                                if (font == null)
                                    font = _browser.getFont();
                                if (font != null) {
                                    float avgCharWidth = RWTUtils.getAvgCharWidth(font);
                                    if (avgCharWidth > 0) {
                                        if (width != 1) {
                                            // add +1 padding
                                            width = ((int) (((width + 1) * avgCharWidth)));
                                        }
                                    }
                                }

                                builder.append(String.format(" width=%s ", width));
                            }
                            if (alignment == null) {
                                String alignmentProperty = rendererProperties
                                        .getStringProperty(PROPERTY_ALIGNMENT);
                                if (alignmentProperty == null) {
                                    alignmentProperty = rendererProperties.getStringProperty("ALLIGNMENT");
                                }
                                alignment = getComponentAlignment(alignmentProperty);

                            }
                            if (alignment != null) {
                                builder.append(String.format(" align=\'%s\'", alignment));
                            }
                            final String caseProperty = getComponentCase(
                                    rendererProperties.getStringProperty(PROPERTY_CASE));

                            builder.append(String.format(" font style=\'%s\'", caseProperty));

                            builder.append(">");

                            String text = columnLabelProvider.getText(record);

                            if (actionDef != null && text != null && text.length() > 0) {
                                builder.append(String.format("<ejl><u %s class=\"%s %s\"  ",
                                        "style=\"line-height: 100%\"",
                                        ("default_all".equals(computeStyleClass) ? "default_link_fg"
                                                : "default_link"),
                                        computeStyleClass));
                                builder.append(actionDef).append(">");
                            }

                            Image image = columnLabelProvider.getImage(record);
                            if (image != null) {
                                if (actionDef == null) {
                                    builder.append("<img src=\"");

                                    builder.append(ImageFactory.getImagePath(image));

                                    builder.append("\"");
                                    builder.append(
                                            String.format(" class=\"default %s\"  >", computeStyleClass));
                                } else

                                {
                                    builder.append("<ejl><img src=\"");
                                    builder.append(ImageFactory.getImagePath(image));
                                    builder.append("\"");
                                    builder.append(
                                            String.format("style=\"cursor: hand;\" class=\"%s \" %s  > </ejl>",
                                                    computeStyleClass, actionDef));
                                }
                            }
                            // builder.append(String.format("<p class=\"default %s\">",
                            // styleClass));

                            builder.append(text);
                            builder.append("</td>");
                        }

                        builder.append("</tr>");
                    }
                    if (size > 1)
                        addTotalLine(builder, paddingStyle, styleClass, cellStyleTOP, cellStyleLEFT,
                                getTotalHours(list));

                }

            }
            builder.append("</table>");
        }
        builder.append("</<div>");
    }
    String html = builder.toString();
    if (_browser.getText() == null || (!html.equals(_browser.getText()))) {
        _browser.setText(html);
        LOGGER.debug(html);
    }

}

From source file:org.entirej.custom.renderers.WorkWeekBlockRenderer.java

License:Apache License

public void addTotalLine(StringBuilder builder, final String paddingStyle, String styleClass,
        String cellStyleTOP, String cellStyleLEFT, String total) {
    boolean addCSLeft = true;
    int totalColIndex = -1;

    for (EJCoreMainScreenItemProperties item : _items) {
        if (item.getReferencedItemName().equals("hoursWorked")) {
            totalColIndex = _items.indexOf(item);
            break;
        }/*  w  w w  .j  a v a  2  s .c om*/
    }
    for (EJCoreMainScreenItemProperties item : _items) {
        String padding = paddingStyle + cellStyleTOP;
        if (addCSLeft) {
            addCSLeft = false;
            padding += cellStyleLEFT;
        }
        boolean addTotalLabel = totalColIndex > 0 && _items.indexOf(item) == (totalColIndex - 1);
        String alignment = addTotalLabel ? "right" : null;
        float width = -1;

        ColumnLabelProvider columnLabelProvider = _itemLabelProviders.get(item.getReferencedItemName());

        EJScreenItemController screenItem = _block.getScreenItem(EJScreenType.MAIN,
                item.getReferencedItemName());
        EJCoreVisualAttributeProperties iva = screenItem.getManagedItemRenderer()
                .getVisualAttributeProperties();
        if (iva != null) {
            styleClass = iva.getName();
        }

        EJFrameworkExtensionProperties rendererProperties = item.getReferencedItemProperties()
                .getItemRendererProperties();

        builder.append(String.format("<td class=\"%s\" ", styleClass));
        if (padding != null) {
            builder.append(String.format(" style=\'%s\'", padding));
        }

        EJFrameworkExtensionProperties extentionProperties = item.getBlockRendererRequiredProperties();
        if (width == -1) {
            width = extentionProperties.getIntProperty(DISPLAY_WIDTH_PROPERTY, 0);
        }

        if (width > 0) {
            Font font = columnLabelProvider.getFont(new Object());

            if (font == null)
                font = _browser.getFont();
            if (font != null) {
                float avgCharWidth = RWTUtils.getAvgCharWidth(font);
                if (avgCharWidth > 0) {
                    if (width != 1) {
                        // add +1 padding
                        width = ((int) (((width + 1) * avgCharWidth)));
                    }
                }
            }
            builder.append(String.format(" width=%s ", width));
        }
        if (alignment == null) {
            String alignmentProperty = rendererProperties.getStringProperty(PROPERTY_ALIGNMENT);
            if (alignmentProperty == null) {
                alignmentProperty = rendererProperties.getStringProperty("ALLIGNMENT");
            }
            alignment = getComponentAlignment(alignmentProperty);

        }
        if (alignment != null) {
            builder.append(String.format(" align=\'%s\'", alignment));
        }
        final String caseProperty = getComponentCase(rendererProperties.getStringProperty(PROPERTY_CASE));

        builder.append(String.format(" font style=\'%s\'", caseProperty));

        builder.append(">");

        if (addTotalLabel) {
            builder.append("Total:");
        }
        if (_items.indexOf(item) == (totalColIndex)) {
            builder.append(total);
        }

        builder.append("</td>");

    }
}