Example usage for javax.swing.table TableColumn getIdentifier

List of usage examples for javax.swing.table TableColumn getIdentifier

Introduction

In this page you can find the example usage for javax.swing.table TableColumn getIdentifier.

Prototype

public Object getIdentifier() 

Source Link

Document

Returns the identifier object for this column.

Usage

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

protected TableCellRenderer getColumnRenderer(int column) {
    TableColumn tableColumn = impl.getColumnModel().getColumn(column);
    if (tableColumn.getIdentifier() instanceof Table.Column) {
        Table.Column columnConf = (Table.Column) tableColumn.getIdentifier();
        if (columnConf.getId() instanceof MetaPropertyPath && !(isEditable() && columnConf.isEditable())
                && !getTableModel().isGeneratedColumn(columnConf)) {
            MetaPropertyPath propertyPath = (MetaPropertyPath) columnConf.getId();

            final CellProvider cellViewProvider = getCustomCellView(propertyPath);
            if (cellViewProvider != null) {
                return new CellProviderRenderer(cellViewProvider);
            } else if (multiLineCells && String.class == columnConf.getType()) {
                return new MultiLineTableCellRenderer();
            }/*w w  w . j  a v  a2 s.  c o m*/
        }
    }
    return null;
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

protected void setVisibleColumns(List<?> columnsOrder) {
    for (TableColumn tableColumn : getAllColumns()) {
        Column columnIdentifier = (Column) tableColumn.getIdentifier();
        if (!columnsOrder.contains(columnIdentifier.getId())) {
            impl.removeColumn(tableColumn);
        }/*from   ww w .ja va2s .  co  m*/
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

/**
 * {@inheritDoc}//from  w w w  .  ja v  a2  s. c om
 */
@Override
public void repaint() {
    TableCellEditor cellEditor = impl.getCellEditor();
    if (cellEditor instanceof DesktopTableCellEditor) {
        ((DesktopTableCellEditor) cellEditor).clearCache();
    }

    List<TableColumn> implColumns = impl.getColumns();
    for (Column column : getColumns()) {
        TableColumn tableColumn = null;
        for (TableColumn implColumn : implColumns) {
            if (column.equals((implColumn.getIdentifier()))) {
                tableColumn = implColumn;
                break;
            }
        }
        // column may be hidden
        if (tableColumn != null) {
            TableCellEditor columnCellEditor = tableColumn.getCellEditor();
            if (columnCellEditor instanceof DesktopTableCellEditor) {
                ((DesktopTableCellEditor) columnCellEditor).clearCache();
            }
        }
    }
    packRows();
    repaintImplIfNeeded();
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

protected void setColumnIdentifiers() {
    int i = 0;// www.jav  a2  s. c o  m
    for (TableColumn tableColumn : getAllColumns()) {
        Column column = columnsOrder.get(i++);
        if (!(tableColumn.getIdentifier() instanceof Column)) {
            tableColumn.setIdentifier(column);
        }
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

protected void adjustColumnHeaders() {
    List<TableColumn> notInited = new LinkedList<>();
    int summaryWidth = 0;
    int componentWidth = impl.getParent().getWidth();

    // take into account only visible columns
    Enumeration<TableColumn> columnEnumeration = impl.getColumnModel().getColumns();
    while (columnEnumeration.hasMoreElements()) {
        TableColumn tableColumn = columnEnumeration.nextElement();
        Column column = (Column) tableColumn.getIdentifier();

        Integer width = column.getWidth();
        if (width != null) {
            tableColumn.setPreferredWidth(width);
            tableColumn.setWidth(width);
            summaryWidth += width;/*from w ww .  ja v  a2  s  . c o  m*/
        } else {
            notInited.add(tableColumn);
        }
    }

    if (notInited.size() != impl.getColumnCount()) {
        impl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        if (!notInited.isEmpty() && (componentWidth > summaryWidth)) {
            int defaultWidth = (componentWidth - summaryWidth) / notInited.size();
            for (TableColumn column : notInited) {
                column.setPreferredWidth(Math.max(defaultWidth, column.getWidth()));
            }
        }
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

@Override
public void removeColumn(Column column) {
    if (column == null) {
        return;/* www .  jav  a  2 s . c om*/
    }

    String name;
    if (column.getId() instanceof MetaPropertyPath) {
        MetaPropertyPath metaPropertyPath = (MetaPropertyPath) column.getId();
        name = metaPropertyPath.getMetaProperty().getName();

        editableColumns.remove(metaPropertyPath);
    } else {
        name = column.getId().toString();
    }

    TableColumn tableColumn = null;

    Iterator<TableColumn> columnIterator = getAllColumns().iterator();
    while (columnIterator.hasNext() && (tableColumn == null)) {
        TableColumn xColumn = columnIterator.next();
        Object identifier = xColumn.getIdentifier();
        if (identifier instanceof String && identifier.equals(name)) {
            tableColumn = xColumn;
        } else if (column.equals(identifier)) {
            tableColumn = xColumn;
        }
    }

    if (tableColumn != null) {
        // store old cell editors / renderers
        Map<Object, TableCellEditor> cellEditors = new HashMap<>();
        Map<Object, TableCellRenderer> cellRenderers = new HashMap<>();

        for (int i = 0; i < tableModel.getColumnCount(); i++) {
            Column tableModelColumn = tableModel.getColumn(i);

            if (tableModel.isGeneratedColumn(tableModelColumn)) {
                TableColumn oldColumn = getColumn(tableModelColumn);

                cellEditors.put(tableModelColumn.getId(), oldColumn.getCellEditor());
                cellRenderers.put(tableModelColumn.getId(), oldColumn.getCellRenderer());
            }
        }

        impl.getColumnModel().removeColumn(tableColumn);
        impl.removeColumn(tableColumn);

        columns.remove(column.getId());
        columnsOrder.remove(column);

        if (tableModel != null) {
            tableModel.removeColumn(column);
        }

        // reassign column identifiers
        setColumnIdentifiers();

        // reattach old generated columns
        for (int i = 0; i < tableModel.getColumnCount(); i++) {
            Column tableModelColumn = tableModel.getColumn(i);

            if (tableModel.isGeneratedColumn(tableModelColumn)) {
                TableColumn oldColumn = getColumn(tableModelColumn);
                if (cellEditors.containsKey(tableModelColumn.getId())) {
                    oldColumn.setCellEditor(cellEditors.get(tableModelColumn.getId()));
                }
                if (cellRenderers.containsKey(tableModelColumn.getId())) {
                    oldColumn.setCellRenderer(cellRenderers.get(tableModelColumn.getId()));
                }
            }
        }

        packRows();
        repaintImplIfNeeded();
    }

    column.setOwner(null);
}

From source file:org.nuclos.client.ui.collect.SubForm.java

private int getDefaultColumnWidth(TableColumn tc, int iColumn) {
    final Integer preferredCellWidth = getColumnWidth("" + tc.getIdentifier());
    final int width = (preferredCellWidth != null) ? preferredCellWidth
            : Math.max(TableUtils.getPreferredColumnWidth(subformtbl, iColumn, 50, TableUtils.TABLE_INSETS),
                    subformtbl.getSubFormModel().getMinimumColumnWidth(iColumn));
    return width;
}

From source file:pl.otros.logview.gui.LogViewPanel.java

public LogViewPanel(final LogDataTableModel dataTableModel, TableColumns[] visibleColumns,
        final OtrosApplication otrosApplication) {
    super();//from  w ww  .  jav a2  s . c o m
    this.dataTableModel = dataTableModel;
    this.otrosApplication = otrosApplication;
    this.statusObserver = otrosApplication.getStatusObserver();
    configuration = otrosApplication.getConfiguration();

    AllPluginables allPluginable = AllPluginables.getInstance();
    markersContainer = allPluginable.getMarkersContainser();
    markersContainer.addListener(new MarkersMenuReloader());
    logFiltersContainer = allPluginable.getLogFiltersContainer();
    messageColorizersContainer = allPluginable.getMessageColorizers();
    messageFormattersContainer = allPluginable.getMessageFormatters();
    selectedMessageColorizersContainer = new PluginableElementsContainer<MessageColorizer>();
    selectedMessageFormattersContainer = new PluginableElementsContainer<MessageFormatter>();
    for (MessageColorizer messageColorizer : messageColorizersContainer.getElements()) {
        selectedMessageColorizersContainer.addElement(messageColorizer);
    }
    for (MessageFormatter messageFormatter : messageFormattersContainer.getElements()) {
        selectedMessageFormattersContainer.addElement(messageFormatter);
    }
    messageColorizersContainer.addListener(
            new SynchronizePluginableContainerListener<MessageColorizer>(selectedMessageColorizersContainer));
    messageFormattersContainer.addListener(
            new SynchronizePluginableContainerListener<MessageFormatter>(selectedMessageFormattersContainer));

    menuLabelFont = new JLabel().getFont().deriveFont(Font.BOLD);
    filtersPanel = new JPanel();
    logsTablePanel = new JPanel();
    logsMarkersPanel = new JPanel();
    leftPanel = new JPanel(new MigLayout());
    logDetailTextArea = new JTextPane();
    logDetailTextArea.setEditable(false);
    MouseAdapter locationInfo = new LocationClickMouseAdapter(otrosApplication, logDetailTextArea);
    logDetailTextArea.addMouseMotionListener(locationInfo);
    logDetailTextArea.addMouseListener(locationInfo);
    logDetailTextArea.setBorder(BorderFactory.createTitledBorder("Details"));
    logDetailWithRulerScrollPane = RulerBarHelper.wrapTextComponent(logDetailTextArea);
    table = new JTableWith2RowHighliting(dataTableModel);

    // Initialize default column visible before creating context menu
    table.setColumnControlVisible(true);
    final ColumnControlButton columnControlButton = new ColumnControlButton(table) {

        @Override
        public void togglePopup() {
            populatePopup();
            super.togglePopup();
        }

        @Override
        protected List<Action> getAdditionalActions() {
            final List<Action> additionalActions = super.getAdditionalActions();
            final AbstractAction saveLayout = new AbstractAction("Save current to new column layout",
                    Icons.DISK) {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    String newLayoutName = JOptionPane.showInputDialog(table, "New Layout name");
                    if (newLayoutName == null) {
                        return;
                    }
                    newLayoutName = newLayoutName.trim();
                    LOGGER.info(String.format("Saving New column layout '%s'", newLayoutName));
                    ArrayList<String> visibleColNames = new ArrayList<String>();
                    for (TableColumn tc : table.getColumns()) {
                        Object o = tc.getIdentifier();
                        if (!(o instanceof TableColumns)) {
                            LOGGER.severe("TableColumn identifier of unexpected type: "
                                    + tc.getIdentifier().getClass().getName());
                            LOGGER.warning("Throw up a pop-up");
                            return;
                        }
                        TableColumns tcs = (TableColumns) o;
                        visibleColNames.add(tcs.getName());
                    }
                    ColumnLayout columnLayout = new ColumnLayout(newLayoutName, visibleColNames);
                    final List<ColumnLayout> columnLayouts = LogTableFormatConfigView
                            .loadColumnLayouts(configuration);
                    columnLayouts.add(columnLayout);
                    LogTableFormatConfigView.saveColumnLayouts(columnLayouts, configuration);
                    populatePopup();
                }
            };
            additionalActions.add(saveLayout);

            final List<ColumnLayout> columnLayoutNames = LogTableFormatConfigView
                    .loadColumnLayouts(configuration);
            for (final ColumnLayout columnLayout : columnLayoutNames) {
                final String name = columnLayout.getName();
                final AbstractAction applyColumnLayout = new ApplyColumnLayoutAction(name, Icons.EDIT_COLUMNS,
                        columnLayout, table);
                additionalActions.add(applyColumnLayout);
            }
            return additionalActions;
        }
    };
    table.setColumnControl(columnControlButton);

    List<TableColumn> columns = table.getColumns(true);
    for (int i = 0; i < columns.size(); i++) {
        columns.get(i).setIdentifier(TableColumns.getColumnById(i));
    }
    for (TableColumn tableColumn : columns) {
        table.getColumnExt(tableColumn.getIdentifier()).setVisible(false);
    }
    for (TableColumns tableColumns : visibleColumns) {
        table.getColumnExt(tableColumns).setVisible(true);
    }

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    updateColumnsSize();
    table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
    final Renderers renderers = Renderers.getInstance(otrosApplication);
    table.setDefaultRenderer(String.class, new TableMarkDecoratorRenderer(renderers.getStringRenderer()));
    table.setDefaultRenderer(Object.class,
            new TableMarkDecoratorRenderer(table.getDefaultRenderer(Object.class)));
    table.setDefaultRenderer(Integer.class,
            new TableMarkDecoratorRenderer(table.getDefaultRenderer(Object.class)));
    table.setDefaultRenderer(Level.class, new TableMarkDecoratorRenderer(renderers.getLevelRenderer()));
    table.setDefaultRenderer(Date.class, new TableMarkDecoratorRenderer(renderers.getDateRenderer()));
    final TimeDeltaRenderer timeDeltaRenderer = new TimeDeltaRenderer();
    table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            final int[] selectedRows = table.getSelectedRows();
            if (selectedRows.length > 0) {
                final int selectedRow = selectedRows[selectedRows.length - 1];
                final Date selectedDate = dataTableModel.getLogData(table.convertRowIndexToModel(selectedRow))
                        .getDate();
                timeDeltaRenderer.setSelectedTimestamp(selectedDate);
                table.repaint();
            }
        }
    });
    table.setDefaultRenderer(TimeDelta.class, new TableMarkDecoratorRenderer(timeDeltaRenderer));

    ((EventSource) configuration.getConfiguration()).addConfigurationListener(new ConfigurationListener() {
        @Override
        public void configurationChanged(ConfigurationEvent ce) {
            if (ce.getType() == AbstractConfiguration.EVENT_SET_PROPERTY && !ce.isBeforeUpdate()) {
                if (ce.getPropertyName().equals(ConfKeys.LOG_TABLE_FORMAT_DATE_FORMAT)) {
                    table.setDefaultRenderer(Date.class, new TableMarkDecoratorRenderer(new DateRenderer(
                            configuration.getString(ConfKeys.LOG_TABLE_FORMAT_DATE_FORMAT, "HH:mm:ss.SSS"))));
                    updateTimeColumnSize();
                } else if (ce.getPropertyName().equals(ConfKeys.LOG_TABLE_FORMAT_LEVEL_RENDERER)) {
                    table.setDefaultRenderer(Level.class,
                            new TableMarkDecoratorRenderer(new LevelRenderer(configuration.get(
                                    LevelRenderer.Mode.class, ConfKeys.LOG_TABLE_FORMAT_LEVEL_RENDERER,
                                    LevelRenderer.Mode.IconsOnly))));
                    updateLevelColumnSize();
                }
            }
        }
    });

    table.setDefaultRenderer(Boolean.class,
            new TableMarkDecoratorRenderer(table.getDefaultRenderer(Boolean.class)));
    table.setDefaultRenderer(Note.class, new TableMarkDecoratorRenderer(new NoteRenderer()));
    table.setDefaultRenderer(MarkerColors.class, new TableMarkDecoratorRenderer(new MarkTableRenderer()));
    table.setDefaultEditor(Note.class, new NoteTableEditor());
    table.setDefaultEditor(MarkerColors.class, new MarkTableEditor(otrosApplication));
    table.setDefaultRenderer(ClassWrapper.class,
            new TableMarkDecoratorRenderer(renderers.getClassWrapperRenderer()));
    sorter = new TableRowSorter<LogDataTableModel>(dataTableModel);
    for (int i = 0; i < dataTableModel.getColumnCount(); i++) {
        sorter.setSortable(i, false);
    }
    sorter.setSortable(TableColumns.ID.getColumn(), true);
    sorter.setSortable(TableColumns.TIME.getColumn(), true);
    table.setRowSorter(sorter);

    messageDetailListener = new MessageDetailListener(this, dateFormat, selectedMessageFormattersContainer,
            selectedMessageColorizersContainer);
    table.getSelectionModel().addListSelectionListener(messageDetailListener);
    dataTableModel.addNoteObserver(messageDetailListener);

    table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            boolean hasFocus = otrosApplication.getApplicationJFrame().isFocused();
            final boolean enabled = otrosApplication.getConfiguration()
                    .getBoolean(ConfKeys.JUMP_TO_CODE_AUTO_JUMP_ENABLED, false);
            if (hasFocus && enabled && !e.getValueIsAdjusting()) {
                try {
                    final LogData logData = dataTableModel
                            .getLogData(table.convertRowIndexToModel(e.getFirstIndex()));
                    LocationInfo li = new LocationInfo(logData.getClazz(), logData.getMethod(),
                            logData.getFile(), Integer.valueOf(logData.getLine()));
                    final JumpToCodeService jumpToCodeService = otrosApplication.getServices()
                            .getJumpToCodeService();
                    final boolean ideAvailable = jumpToCodeService.isIdeAvailable();
                    if (ideAvailable) {
                        LOGGER.fine("Jumping to " + li);
                        jumpToCodeService.jump(li);
                    }
                } catch (Exception e1) {
                    LOGGER.warning("Can't perform jump to code " + e1.getMessage());
                }

            }
        }
    });

    notes = new JTextArea();
    notes.setEditable(false);
    NoteObserver allNotesObserver = new AllNotesTextAreaObserver(notes);
    dataTableModel.addNoteObserver(allNotesObserver);

    addFiltersGUIsToPanel(filtersPanel);
    logsTablePanel.setLayout(new BorderLayout());
    logsTablePanel.add(new JScrollPane(table));
    JPanel messageDetailsPanel = new JPanel(new BorderLayout());
    messageDetailToolbar = new JToolBar("MessageDetail");
    messageDetailsPanel.add(messageDetailToolbar, BorderLayout.NORTH);
    messageDetailsPanel.add(logDetailWithRulerScrollPane);
    initMessageDetailsToolbar();

    jTabbedPane = new JTabbedPane();
    jTabbedPane.add("Message detail", messageDetailsPanel);
    jTabbedPane.add("All notes", new JScrollPane(notes));

    leftPanel.add(filtersPanel, "wrap, growx");
    leftPanel.add(new JSeparator(SwingConstants.HORIZONTAL), "wrap,growx");
    leftPanel.add(logsMarkersPanel, "wrap,growx");

    JSplitPane splitPaneLogsTableAndDetails = new JSplitPane(JSplitPane.VERTICAL_SPLIT, logsTablePanel,
            jTabbedPane);
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(leftPanel,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
            splitPaneLogsTableAndDetails);
    splitPane.setOneTouchExpandable(true);
    this.setLayout(new BorderLayout());
    this.add(splitPane);

    splitPaneLogsTableAndDetails.setDividerLocation(0.5d);
    splitPaneLogsTableAndDetails.setOneTouchExpandable(true);
    splitPane.setDividerLocation(leftPanel.getPreferredSize().width + 10);

    PopupListener popupListener = new PopupListener(new Callable<JPopupMenu>() {
        @Override
        public JPopupMenu call() throws Exception {
            return initTableContextMenu();
        }
    });
    table.addMouseListener(popupListener);
    table.addKeyListener(popupListener);

    PopupListener popupListenerMessageDetailMenu = new PopupListener(new Callable<JPopupMenu>() {
        @Override
        public JPopupMenu call() throws Exception {
            return initMessageDetailPopupMenu();
        }
    });
    logDetailTextArea.addMouseListener(popupListenerMessageDetailMenu);
    logDetailTextArea.addKeyListener(popupListenerMessageDetailMenu);

    dataTableModel.notifyAllNoteObservers(new NoteEvent(EventType.CLEAR, dataTableModel, null, 0));

    table.addKeyListener(new MarkRowBySpaceKeyListener(otrosApplication));
    initAcceptConditions();
}