From d4b03c3787519d3712536c50518df892a4f4a4da Mon Sep 17 00:00:00 2001 From: Julia Beliaeva Date: Sun, 31 Jul 2016 09:26:23 +0300 Subject: [PATCH] [file-history] rename MyColumnWrapper -> FileHistoryColumnWrapper and move it out of FileHistoryPanelImpl --- .../vcs/history/FileHistoryColumnWrapper.java | 142 ++++++++++++++++++ .../vcs/history/FileHistoryPanelImpl.java | 118 +-------------- 2 files changed, 148 insertions(+), 112 deletions(-) create mode 100644 platform/vcs-impl/src/com/intellij/openapi/vcs/history/FileHistoryColumnWrapper.java diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/history/FileHistoryColumnWrapper.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/history/FileHistoryColumnWrapper.java new file mode 100644 index 000000000000..a6345c044d37 --- /dev/null +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/history/FileHistoryColumnWrapper.java @@ -0,0 +1,142 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.vcs.history; + +import com.intellij.ui.dualView.DualView; +import com.intellij.ui.dualView.DualViewColumnInfo; +import com.intellij.ui.table.TableView; +import com.intellij.util.ui.ColumnInfo; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import javax.swing.table.TableCellEditor; +import javax.swing.table.TableCellRenderer; +import javax.swing.table.TableColumn; +import java.awt.*; +import java.util.Comparator; +import java.util.Enumeration; + +abstract class FileHistoryColumnWrapper extends DualViewColumnInfo { + @NotNull private final ColumnInfo myBaseColumn; + + public FileHistoryColumnWrapper(@NotNull ColumnInfo additionalColumn) { + super(additionalColumn.getName()); + myBaseColumn = additionalColumn; + } + + public Comparator getComparator() { + final Comparator comparator = myBaseColumn.getComparator(); + if (comparator == null) return null; + return (o1, o2) -> { + if (o1 == null) return -1; + if (o2 == null) return 1; + VcsFileRevision revision1 = o1.getRevision(); + VcsFileRevision revision2 = o2.getRevision(); + if (revision1 == null) return -1; + if (revision2 == null) return 1; + return comparator.compare(revision1, revision2); + }; + } + + public String getName() { + return myBaseColumn.getName(); + } + + public void setName(String s) { + myBaseColumn.setName(s); + } + + public Class getColumnClass() { + return myBaseColumn.getColumnClass(); + } + + public boolean isCellEditable(FileHistoryPanelImpl.TreeNodeOnVcsRevision o) { + return myBaseColumn.isCellEditable(o.getRevision()); + } + + public void setValue(FileHistoryPanelImpl.TreeNodeOnVcsRevision o, Object aValue) { + //noinspection unchecked + myBaseColumn.setValue(o.getRevision(), (T)aValue); + } + + public TableCellRenderer getRenderer(FileHistoryPanelImpl.TreeNodeOnVcsRevision p0) { + return myBaseColumn.getRenderer(p0.getRevision()); + } + + public TableCellEditor getEditor(FileHistoryPanelImpl.TreeNodeOnVcsRevision item) { + return myBaseColumn.getEditor(item.getRevision()); + } + + public String getMaxStringValue() { + final String superValue = myBaseColumn.getMaxStringValue(); + if (superValue != null) return superValue; + return getMaxValue(myBaseColumn.getName()); + } + + @Nullable + private String getMaxValue(@NotNull String columnHeader) { + TableView table = getDualView().getFlatView(); + if (table.getRowCount() == 0) return null; + final Enumeration columns = table.getColumnModel().getColumns(); + int idx = 0; + while (columns.hasMoreElements()) { + TableColumn column = columns.nextElement(); + if (columnHeader.equals(column.getHeaderValue())) { + break; + } + ++idx; + } + if (idx >= table.getColumnModel().getColumnCount() - 1) return null; + final FontMetrics fm = table.getFontMetrics(table.getFont().deriveFont(Font.BOLD)); + final Object header = table.getColumnModel().getColumn(idx).getHeaderValue(); + double maxValue = fm.stringWidth((String)header); + String value = (String)header; + for (int i = 0; i < table.getRowCount(); i++) { + final Object at = table.getValueAt(i, idx); + if (at instanceof String) { + final int newWidth = fm.stringWidth((String)at); + if (newWidth > maxValue) { + maxValue = newWidth; + value = (String)at; + } + } + } + return value + "ww"; + } + + public int getAdditionalWidth() { + return myBaseColumn.getAdditionalWidth(); + } + + public int getWidth(JTable table) { + return myBaseColumn.getWidth(table); + } + + public boolean shouldBeShownIsTheTree() { + return true; + } + + public boolean shouldBeShownIsTheTable() { + return true; + } + + public Object valueOf(FileHistoryPanelImpl.TreeNodeOnVcsRevision o) { + return myBaseColumn.valueOf(o.getRevision()); + } + + protected abstract DualView getDualView(); +} diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/history/FileHistoryPanelImpl.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/history/FileHistoryPanelImpl.java index ca5a794eede4..39f67203e51a 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/history/FileHistoryPanelImpl.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/history/FileHistoryPanelImpl.java @@ -77,9 +77,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; -import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; -import javax.swing.table.TableColumn; import javax.swing.table.TableModel; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeCellRenderer; @@ -317,7 +315,12 @@ public class FileHistoryPanelImpl extends PanelWithActionsAndCloseButton impleme ArrayList result = new ArrayList<>(); if (additionalColumns != null) { for (ColumnInfo additionalColumn : additionalColumns) { - result.add(new MyColumnWrapper(additionalColumn)); + result.add(new FileHistoryColumnWrapper(additionalColumn) { + @Override + protected DualView getDualView() { + return myDualView; + } + }); } } return result; @@ -673,37 +676,6 @@ public class FileHistoryPanelImpl extends PanelWithActionsAndCloseButton impleme return myFilePath.getVirtualFileParent(); } - @Nullable - private String getMaxValue(@NotNull String name) { - TableView table = myDualView.getFlatView(); - if (table.getRowCount() == 0) return null; - final Enumeration columns = table.getColumnModel().getColumns(); - int idx = 0; - while (columns.hasMoreElements()) { - TableColumn column = columns.nextElement(); - if (name.equals(column.getHeaderValue())) { - break; - } - ++idx; - } - if (idx >= table.getColumnModel().getColumnCount() - 1) return null; - final FontMetrics fm = table.getFontMetrics(table.getFont().deriveFont(Font.BOLD)); - final Object header = table.getColumnModel().getColumn(idx).getHeaderValue(); - double maxValue = fm.stringWidth((String)header); - String value = (String)header; - for (int i = 0; i < table.getRowCount(); i++) { - final Object at = table.getValueAt(i, idx); - if (at instanceof String) { - final int newWidth = fm.stringWidth((String)at); - if (newWidth > maxValue) { - maxValue = newWidth; - value = (String)at; - } - } - } - return value + "ww"; - } - private void refreshRevisionsOrder() { final List list = myHistorySession.getRevisionList(); myRevisionsOrder.clear(); @@ -1510,84 +1482,6 @@ public class FileHistoryPanelImpl extends PanelWithActionsAndCloseButton impleme } } - private class MyColumnWrapper extends DualViewColumnInfo { - private final ColumnInfo myBaseColumn; - - public MyColumnWrapper(ColumnInfo additionalColumn) { - super(additionalColumn.getName()); - myBaseColumn = additionalColumn; - } - - public Comparator getComparator() { - final Comparator comparator = myBaseColumn.getComparator(); - if (comparator == null) return null; - return (o1, o2) -> { - if (o1 == null) return -1; - if (o2 == null) return 1; - VcsFileRevision revision1 = o1.myRevision; - VcsFileRevision revision2 = o2.myRevision; - if (revision1 == null) return -1; - if (revision2 == null) return 1; - return comparator.compare(revision1, revision2); - }; - } - - public String getName() { - return myBaseColumn.getName(); - } - - public void setName(String s) { - myBaseColumn.setName(s); - } - - public Class getColumnClass() { - return myBaseColumn.getColumnClass(); - } - - public boolean isCellEditable(TreeNodeOnVcsRevision o) { - return myBaseColumn.isCellEditable(o.myRevision); - } - - public void setValue(TreeNodeOnVcsRevision o, Object aValue) { - //noinspection unchecked - myBaseColumn.setValue(o.myRevision, (T)aValue); - } - - public TableCellRenderer getRenderer(TreeNodeOnVcsRevision p0) { - return myBaseColumn.getRenderer(p0.myRevision); - } - - public TableCellEditor getEditor(TreeNodeOnVcsRevision item) { - return myBaseColumn.getEditor(item.myRevision); - } - - public String getMaxStringValue() { - final String superValue = myBaseColumn.getMaxStringValue(); - if (superValue != null) return superValue; - return getMaxValue(myBaseColumn.getName()); - } - - public int getAdditionalWidth() { - return myBaseColumn.getAdditionalWidth(); - } - - public int getWidth(JTable table) { - return myBaseColumn.getWidth(table); - } - - public boolean shouldBeShownIsTheTree() { - return true; - } - - public boolean shouldBeShownIsTheTable() { - return true; - } - - public Object valueOf(TreeNodeOnVcsRevision o) { - return myBaseColumn.valueOf(o.myRevision); - } - } - private class RefreshFileHistoryAction extends RefreshAction implements DumbAware { public RefreshFileHistoryAction() { super(VcsBundle.message("action.name.refresh"), VcsBundle.message("action.description.refresh"), AllIcons.Actions.Refresh);