Skip to content

Fixed profile tab column resizing #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
public class ColumnResizer implements TableModelListener {

private final JTable table;
private final Integer maxWidth;
private final int minMargin;

public ColumnResizer(JTable table) {
this.table = table;
this.maxWidth = null;
minMargin = strWidth("ww");
}

public ColumnResizer(JTable table, Integer maxWidth) {
this.table = table;
this.maxWidth = maxWidth;
minMargin = strWidth("ww");
}

Expand All @@ -29,16 +37,22 @@ public void resize() {
for (int c = 0; c < cols; c++) {
TableColumn col = model.getColumn(c);
String header = String.valueOf(col.getHeaderValue());
int headerWidth = strWidth(header) + margin;
int minWidth = strWidth(header) + margin;

for (int r = 0; r < rows; r++) {
TableCellRenderer render = table.getCellRenderer(r, c);
Component component = table.prepareRenderer(render, r, c);
int rendererWidth = component.getPreferredSize().width;

int cellWidth = Math.max(rendererWidth + margin, 1);
minWidth = Math.max(minWidth, cellWidth);

if (maxWidth != null && minWidth >= maxWidth) {
col.setPreferredWidth(maxWidth);
break;
}

col.setPreferredWidth(Math.max(headerWidth, cellWidth));
col.setPreferredWidth(minWidth);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.console.table.renderer.CompositeTableCellRenderer;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -39,6 +37,8 @@ public void initialize(GraphConsoleView graphConsoleView, Project project) {
table.setDefaultEditor(Object.class, new CompositeTableCellEditor());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

ColumnResizer cr = new ColumnResizer(table, MAX_WIDTH);

messageBus.connect().subscribe(QueryExecutionProcessEvent.QUERY_EXECUTION_PROCESS_TOPIC, new QueryExecutionProcessEvent() {
@Override
public void executionStarted(ExecuteQueryPayload payload) {
Expand All @@ -64,7 +64,7 @@ public void resultReceived(ExecuteQueryPayload payload, GraphQueryResult result)

@Override
public void postResultReceived(ExecuteQueryPayload payload) {
updateColumnWidths();
cr.resize();
updateRowHeights();
}

Expand All @@ -78,28 +78,6 @@ public void executionCompleted(ExecuteQueryPayload payload) {
});
}

private void updateColumnWidths() {
for (int column = 0; column < table.getColumnCount(); column++) {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
int preferredWidth = tableColumn.getMinWidth();
int maxWidth = MAX_WIDTH;

for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
Component c = table.prepareRenderer(cellRenderer, row, column);
int width = c.getPreferredSize().width + table.getIntercellSpacing().width;
preferredWidth = Math.max(preferredWidth, width);

// We've exceeded the maximum width, no need to check other rows
if (preferredWidth >= MAX_WIDTH) {
preferredWidth = MAX_WIDTH;
break;
}
}
tableColumn.setPreferredWidth(preferredWidth);
}
}

public void updateRowHeights() {
for (int row = 0; row < table.getRowCount(); row++) {
int rowHeight = 1;
Expand Down