From a01ec2cc4e85f83ab5fdeecaf746a00b5553912f Mon Sep 17 00:00:00 2001 From: Julia Beliaeva Date: Sat, 9 May 2015 19:40:17 +0300 Subject: [PATCH] [vcs-log] rename ReferencePainter -> VcsRefPainter, LabelPainter -> TextLabelPainter, move them to separate files; rename paintLabel methods to just paint --- .../vcs/log/ui/frame/BranchesPanel.java | 19 +- .../vcs/log/ui/frame/DetailsPanel.java | 12 +- .../log/ui/frame/ReferencePopupComponent.java | 167 ++++++++++++++ .../log/ui/render/GraphCommitCellRender.java | 12 +- .../vcs/log/ui/render/LabelPainters.java | 217 ------------------ .../vcs/log/ui/render/TextLabelPainter.java | 177 ++++++++++++++ .../vcs/log/ui/render/VcsRefPainter.java | 60 +++++ 7 files changed, 426 insertions(+), 238 deletions(-) create mode 100644 platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/ReferencePopupComponent.java delete mode 100644 platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/LabelPainters.java create mode 100644 platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/TextLabelPainter.java create mode 100644 platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/VcsRefPainter.java diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/BranchesPanel.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/BranchesPanel.java index 6d368cd0b722..9da107563fad 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/BranchesPanel.java +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/BranchesPanel.java @@ -18,7 +18,7 @@ import com.intellij.vcs.log.impl.SingletonRefGroup; import com.intellij.vcs.log.impl.VcsLogUtil; import com.intellij.vcs.log.ui.VcsLogColorManagerImpl; import com.intellij.vcs.log.ui.VcsLogUiImpl; -import com.intellij.vcs.log.ui.render.LabelPainters.ReferencePainter; +import com.intellij.vcs.log.ui.render.VcsRefPainter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -44,7 +44,7 @@ public class BranchesPanel extends JPanel { private final VcsLogUiImpl myUI; private LinkedHashMap> myRefGroups; - private final ReferencePainter myReferencePainter; + private final VcsRefPainter myReferencePainter; @Nullable private Collection myRoots = null; private Map myRefPositions = ContainerUtil.newHashMap(); @@ -53,7 +53,7 @@ public class BranchesPanel extends JPanel { myDataHolder = dataHolder; myUI = UI; myRefGroups = getRefsToDisplayOnPanel(initialRefsModel); - myReferencePainter = new ReferencePainter(myUI.getColorManager(), true); + myReferencePainter = new VcsRefPainter(myUI.getColorManager(), true); setPreferredSize(new Dimension(-1, myReferencePainter.getHeight(this) + UIUtil.DEFAULT_VGAP + VERTICAL_SPACE)); @@ -119,8 +119,9 @@ public class BranchesPanel extends JPanel { if (myRoots == null || myRoots.contains(root)) { Color rootIndicatorColor = VcsLogColorManagerImpl.getIndicatorColor(myUI.getColorManager().getRootColor(root)); Rectangle rectangle = myReferencePainter - .paintLabel(group.getName(), g, paddingX, (getHeight() - myReferencePainter.getHeight(this)) / 2 - VERTICAL_SPACE, group.getBgColor(), - rootIndicatorColor); + .paint(group.getName(), g, paddingX, (getHeight() - myReferencePainter.getHeight(this)) / 2 - VERTICAL_SPACE, + group.getBgColor(), + rootIndicatorColor); paddingX += rectangle.width + SMALL_ROOTS_GAP; myRefPositions.put(rectangle.x, group); } @@ -179,7 +180,7 @@ public class BranchesPanel extends JPanel { @NotNull private final SingleReferenceComponent myRendererComponent; @NotNull private final ListCellRenderer myCellRenderer; - ReferencePopupComponent(@NotNull RefGroup group, @NotNull VcsLogUiImpl ui, @NotNull ReferencePainter referencePainter) { + ReferencePopupComponent(@NotNull RefGroup group, @NotNull VcsLogUiImpl ui, @NotNull VcsRefPainter referencePainter) { super(new BorderLayout()); myUi = ui; @@ -268,12 +269,12 @@ public class BranchesPanel extends JPanel { private static class SingleReferenceComponent extends JPanel { private static final int PADDING_Y = 2; private static final int PADDING_X = 5; - @NotNull private final ReferencePainter myReferencePainter; + @NotNull private final VcsRefPainter myReferencePainter; @Nullable private VcsRef myReference; public boolean mySelected; - public SingleReferenceComponent(@NotNull ReferencePainter referencePainter) { + public SingleReferenceComponent(@NotNull VcsRefPainter referencePainter) { myReferencePainter = referencePainter; } @@ -283,7 +284,7 @@ public class BranchesPanel extends JPanel { g.fillRect(0, 0, getWidth(), getHeight()); if (myReference != null) { - myReferencePainter.paintReference(myReference, g, PADDING_X, PADDING_Y); + myReferencePainter.paint(myReference, g, PADDING_X, PADDING_Y); } } diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/DetailsPanel.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/DetailsPanel.java index fa3ac58030fd..da7d750e4945 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/DetailsPanel.java +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/DetailsPanel.java @@ -43,7 +43,7 @@ import com.intellij.vcs.log.data.LoadingDetails; import com.intellij.vcs.log.data.VcsLogDataHolder; import com.intellij.vcs.log.data.VisiblePack; import com.intellij.vcs.log.ui.VcsLogColorManager; -import com.intellij.vcs.log.ui.render.LabelPainters.ReferencePainter; +import com.intellij.vcs.log.ui.render.VcsRefPainter; import com.intellij.vcs.log.ui.tables.GraphTableModel; import net.miginfocom.swing.MigLayout; import org.jetbrains.annotations.NotNull; @@ -456,12 +456,12 @@ class DetailsPanel extends JPanel implements ListSelectionListener { } private static class ReferencesPanel extends JPanel { - @NotNull private final ReferencePainter myReferencePainter; + @NotNull private final VcsRefPainter myReferencePainter; @NotNull private List myReferences; ReferencesPanel(@NotNull VcsLogColorManager colorManager) { super(new FlowLayout(FlowLayout.LEADING, 5, 2)); - myReferencePainter = new ReferencePainter(colorManager, false); + myReferencePainter = new VcsRefPainter(colorManager, false); myReferences = Collections.emptyList(); setOpaque(false); } @@ -484,10 +484,10 @@ class DetailsPanel extends JPanel implements ListSelectionListener { } private static class SingleReferencePanel extends JPanel { - @NotNull private final ReferencePainter myRefPainter; + @NotNull private final VcsRefPainter myRefPainter; @NotNull private VcsRef myReference; - SingleReferencePanel(@NotNull ReferencePainter referencePainter, @NotNull VcsRef reference) { + SingleReferencePanel(@NotNull VcsRefPainter referencePainter, @NotNull VcsRef reference) { myRefPainter = referencePainter; myReference = reference; setOpaque(false); @@ -495,7 +495,7 @@ class DetailsPanel extends JPanel implements ListSelectionListener { @Override protected void paintComponent(Graphics g) { - myRefPainter.paintReference(myReference, g, 0, 0); + myRefPainter.paint(myReference, g, 0, 0); } @Override diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/ReferencePopupComponent.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/ReferencePopupComponent.java new file mode 100644 index 000000000000..7c0ea6fab21e --- /dev/null +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/frame/ReferencePopupComponent.java @@ -0,0 +1,167 @@ +/* + * Copyright 2000-2015 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.vcs.log.ui.frame; + +import com.intellij.openapi.ui.popup.JBPopup; +import com.intellij.openapi.ui.popup.JBPopupFactory; +import com.intellij.ui.ListUtil; +import com.intellij.ui.components.JBList; +import com.intellij.ui.components.JBScrollPane; +import com.intellij.util.ui.UIUtil; +import com.intellij.vcs.log.RefGroup; +import com.intellij.vcs.log.VcsRef; +import com.intellij.vcs.log.ui.VcsLogUiImpl; +import com.intellij.vcs.log.ui.render.VcsRefPainter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +class ReferencePopupComponent extends JPanel { + @NotNull private final JBPopup myPopup; + @NotNull private final JBList myList; + @NotNull private final VcsLogUiImpl myUi; + @NotNull private final SingleReferenceComponent myRendererComponent; + @NotNull private final ListCellRenderer myCellRenderer; + + ReferencePopupComponent(@NotNull RefGroup group, @NotNull VcsLogUiImpl ui, @NotNull VcsRefPainter referencePainter) { + super(new BorderLayout()); + myUi = ui; + + myRendererComponent = new SingleReferenceComponent(referencePainter); + myCellRenderer = new ListCellRenderer() { + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + myRendererComponent.setReference((VcsRef)value); + myRendererComponent.setSelected(isSelected); + return myRendererComponent; + } + }; + + myList = createList(group); + myPopup = createPopup(); + add(new JBScrollPane(myList)); + } + + private JBList createList(RefGroup group) { + JBList list = new JBList(createListModel(group)); + list.setCellRenderer(myCellRenderer); + ListUtil.installAutoSelectOnMouseMove(list); + list.setSelectedIndex(0); + list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + jumpOnMouseClick(list); + jumpOnEnter(list); + + return list; + } + + private void jumpOnMouseClick(JBList list) { + list.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + jumpToSelectedRef(); + } + }); + } + + private void jumpOnEnter(JBList list) { + list.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + jumpToSelectedRef(); + } + } + }); + } + + private JBPopup createPopup() { + return JBPopupFactory.getInstance(). + createComponentPopupBuilder(this, myList). + setCancelOnClickOutside(true). + setCancelOnWindowDeactivation(true). + setFocusable(true). + setRequestFocus(true). + setResizable(true). + setDimensionServiceKey(myUi.getProject(), "Vcs.Log.Branch.Panel.RefGroup.Popup", false). + createPopup(); + } + + private static DefaultListModel createListModel(RefGroup group) { + DefaultListModel model = new DefaultListModel(); + for (final VcsRef vcsRef : group.getRefs()) { + model.addElement(vcsRef); + } + return model; + } + + @NotNull + JBPopup getPopup() { + return myPopup; + } + + private void jumpToSelectedRef() { + myPopup.cancel(); // close the popup immediately not to stay at the front if jumping to a commits takes long time. + VcsRef selectedRef = (VcsRef)myList.getSelectedValue(); + if (selectedRef != null) { + myUi.jumpToCommit(selectedRef.getCommitHash()); + } + } + + private static class SingleReferenceComponent extends JPanel { + private static final int PADDING_Y = 2; + private static final int PADDING_X = 5; + @NotNull private final VcsRefPainter myReferencePainter; + + @Nullable private VcsRef myReference; + public boolean mySelected; + + public SingleReferenceComponent(@NotNull VcsRefPainter referencePainter) { + myReferencePainter = referencePainter; + } + + @Override + protected void paintComponent(Graphics g) { + g.setColor(mySelected ? UIUtil.getListSelectionBackground() : UIUtil.getListBackground()); + g.fillRect(0, 0, getWidth(), getHeight()); + + if (myReference != null) { + myReferencePainter.paint(myReference, g, PADDING_X, PADDING_Y); + } + } + + @Override + public Dimension getPreferredSize() { + if (myReference == null) return super.getPreferredSize(); + Dimension size = myReferencePainter.getSize(myReference, this); + return new Dimension(size.width + 2 * PADDING_X, size.height + 2 * PADDING_Y); + } + + public void setReference(@NotNull VcsRef reference) { + myReference = reference; + } + + public void setSelected(boolean selected) { + mySelected = selected; + } + } +} diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/GraphCommitCellRender.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/GraphCommitCellRender.java index 49bfd96556c3..c9fdbe6dba94 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/GraphCommitCellRender.java +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/GraphCommitCellRender.java @@ -31,7 +31,7 @@ public class GraphCommitCellRender extends ColoredTableCellRenderer { @NotNull private final VcsLogDataHolder myDataHolder; @NotNull private final GraphCellPainter myPainter; @NotNull private final VcsLogGraphTable myGraphTable; - @NotNull private final LabelPainters.LabelPainter myLabelPainter; + @NotNull private final TextLabelPainter myTextLabelPainter; @NotNull private final IssueLinkRenderer myIssueLinkRenderer; @Nullable private PaintInfo myGraphImage; @@ -43,7 +43,7 @@ public class GraphCommitCellRender extends ColoredTableCellRenderer { myDataHolder = dataHolder; myPainter = painter; myGraphTable = table; - myLabelPainter = LabelPainters.createPainter(false); + myTextLabelPainter = TextLabelPainter.createPainter(false); myIssueLinkRenderer = new IssueLinkRenderer(dataHolder.getProject(), this); } @@ -55,7 +55,7 @@ public class GraphCommitCellRender extends ColoredTableCellRenderer { } public int getPreferredHeight() { - return myLabelPainter.calculateSize("", getFontMetrics(LabelPainters.getFont())).height + 4; + return myTextLabelPainter.calculateSize("", getFontMetrics(TextLabelPainter.getFont())).height + 4; } @Override @@ -66,9 +66,9 @@ public class GraphCommitCellRender extends ColoredTableCellRenderer { int paddingX = (myGraphImage != null ? myGraphImage.getWidth() : 0) + PrintParameters.LABEL_PADDING; Map labelsForReferences = collectLabelsForRefs(myRefs); for (Map.Entry entry : labelsForReferences.entrySet()) { - Dimension size = myLabelPainter.calculateSize(entry.getKey(), g.getFontMetrics(LabelPainters.getFont())); + Dimension size = myTextLabelPainter.calculateSize(entry.getKey(), g.getFontMetrics(TextLabelPainter.getFont())); int paddingY = (myGraphTable.getRowHeight() - size.height) / 2; - myLabelPainter.paintLabel((Graphics2D)g, entry.getKey(), paddingX, paddingY, entry.getValue()); + myTextLabelPainter.paint((Graphics2D)g, entry.getKey(), paddingX, paddingY, entry.getValue()); paddingX += size.width + PrintParameters.LABEL_PADDING; } } @@ -148,7 +148,7 @@ public class GraphCommitCellRender extends ColoredTableCellRenderer { int paddingX = 2 * PrintParameters.LABEL_PADDING; for (String label : collectLabelsForRefs(references).keySet()) { - Dimension size = myLabelPainter.calculateSize(label, this.getFontMetrics(LabelPainters.getFont())); + Dimension size = myTextLabelPainter.calculateSize(label, this.getFontMetrics(TextLabelPainter.getFont())); paddingX += size.width + PrintParameters.LABEL_PADDING; } return paddingX; diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/LabelPainters.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/LabelPainters.java deleted file mode 100644 index 973af640b84e..000000000000 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/LabelPainters.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright 2000-2015 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.vcs.log.ui.render; - -import com.intellij.openapi.util.registry.Registry; -import com.intellij.ui.JBColor; -import com.intellij.ui.SimpleColoredComponent; -import com.intellij.util.ui.UIUtil; -import com.intellij.vcs.log.VcsRef; -import com.intellij.vcs.log.ui.VcsLogColorManager; -import com.intellij.vcs.log.ui.VcsLogColorManagerImpl; -import org.jetbrains.annotations.NotNull; - -import javax.swing.*; -import java.awt.*; -import java.awt.geom.RoundRectangle2D; - -public class LabelPainters { - - public static LabelPainter createPainter(boolean paintFlag) { - boolean square = Registry.is("vcs.log.square.labels"); - if (paintFlag) { - return square ? new UnderlinedLabelPainter() : new FlagLabelPainter(); - } - else { - return new LabelPainter(square); - } - } - - public static Font getFont() { - return UIUtil.getLabelFont(); - } - - public static class ReferencePainter { - @NotNull private final VcsLogColorManager myColorManager; - @NotNull private final LabelPainter myLabelPainter; - - public ReferencePainter(@NotNull VcsLogColorManager colorManager, boolean paintRoot) { - myColorManager = colorManager; - myLabelPainter = createPainter(paintRoot && colorManager.isMultipleRoots()); - } - - public void paintReference(@NotNull VcsRef reference, @NotNull Graphics g, int paddingX, int paddingY) { - myLabelPainter.paintLabel((Graphics2D)g, reference.getName(), paddingX, paddingY, reference.getType().getBackgroundColor(), - VcsLogColorManagerImpl.getIndicatorColor(myColorManager.getRootColor(reference.getRoot()))); - } - - public Rectangle paintLabel(@NotNull String text, - @NotNull Graphics g, - int paddingX, - int paddingY, - @NotNull Color color, - @NotNull Color flagColor) { - return myLabelPainter.paintLabel((Graphics2D)g, text, paddingX, paddingY, color, flagColor); - } - - public Dimension getSize(@NotNull VcsRef reference, @NotNull JComponent component) { - return myLabelPainter.calculateSize(reference.getName(), component.getFontMetrics(getFont())); - } - - public int getHeight(@NotNull JComponent component) { - return myLabelPainter.calculateSize("", component.getFontMetrics(getFont())).height; - } - } - - public static class LabelPainter { - protected static final int TEXT_PADDING_X = 5; - protected static final int TOP_TEXT_PADDING = 2; - protected static final int BOTTOM_TEXT_PADDING = 1; - - private static final int LABEL_ARC = 5; - - private final boolean mySquare; - - public LabelPainter(boolean square) { - mySquare = square; - } - - protected Rectangle paintLabel(@NotNull Graphics2D g2, - @NotNull String text, - int paddingX, - int paddingY, - int textPadding, - @NotNull Color color) { - g2.setFont(getFont()); - g2.setStroke(new BasicStroke(1.5f)); - g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - - FontMetrics fontMetrics = g2.getFontMetrics(); - int width = fontMetrics.stringWidth(text) + 2 * TEXT_PADDING_X + textPadding; - int height = fontMetrics.getHeight() + TOP_TEXT_PADDING + BOTTOM_TEXT_PADDING; - - g2.setColor(color); - if (mySquare) { - g2.fillRect(paddingX, paddingY, width, height); - } - else { - g2.fill(new RoundRectangle2D.Double(paddingX, paddingY, width, height, LABEL_ARC, LABEL_ARC)); - } - - g2.setColor(JBColor.BLACK); - int x = paddingX + textPadding + TEXT_PADDING_X; - int y = paddingY + SimpleColoredComponent.getTextBaseLine(fontMetrics, height); - g2.drawString(text, x, y); - - return new Rectangle(x, y, width, height); - } - - public Rectangle paintLabel(@NotNull Graphics2D g2, @NotNull String text, int paddingX, int paddingY, @NotNull Color color) { - return paintLabel(g2, text, paddingX, paddingY, 0, color); - } - - public Rectangle paintLabel(@NotNull Graphics2D g2, - @NotNull String text, - int paddingX, - int paddingY, - @NotNull Color labelColor, - @NotNull Color flagColor) { - return paintLabel(g2, text, paddingX, paddingY, 0, labelColor); - } - - public Dimension calculateSize(@NotNull String text, @NotNull FontMetrics metrics) { - int width = metrics.stringWidth(text) + 2 * TEXT_PADDING_X; - int height = metrics.getHeight() + TOP_TEXT_PADDING + BOTTOM_TEXT_PADDING; - return new Dimension(width, height); - } - - } - - public static class FlagLabelPainter extends LabelPainter { - private static final int FLAG_WIDTH = 8; - private static final int FLAG_PADDING = 6; - private static final int FLAG_TOP_INDENT = 2; - - public FlagLabelPainter() { - super(false); - } - - @Override - public Rectangle paintLabel(@NotNull Graphics2D g2, - @NotNull String text, - int paddingX, - int paddingY, - @NotNull Color labelColor, - @NotNull Color flagColor) { - - Rectangle rectangle = - super.paintLabel(g2, text, paddingX, paddingY + FLAG_TOP_INDENT, FLAG_WIDTH + 2 * FLAG_PADDING - TEXT_PADDING_X, labelColor); - - g2.setColor(flagColor); - int x0 = paddingX + FLAG_PADDING; - int xMid = x0 + FLAG_WIDTH / 2; - int xRight = x0 + FLAG_WIDTH; - - int y0 = paddingY; - int yMid = y0 + 2 * rectangle.height / 3 - 2; - int yBottom = y0 + rectangle.height - 4; - - // something like a knight flag - Polygon polygon = new Polygon(new int[]{x0, xRight, xRight, xMid, x0}, new int[]{y0, y0, yMid, yBottom, yMid}, 5); - g2.fillPolygon(polygon); - - return new Rectangle(paddingX, paddingY, rectangle.width, rectangle.height + FLAG_TOP_INDENT); - } - - @Override - public Dimension calculateSize(@NotNull String text, @NotNull FontMetrics metrics) { - Dimension size = super.calculateSize(text, metrics); - return new Dimension(size.width + FLAG_WIDTH + 2 * FLAG_PADDING, size.height + FLAG_TOP_INDENT); - } - } - - public static class UnderlinedLabelPainter extends LabelPainter { - - private static final int LINE_GAP = 1; - private static final int LINE_HEIGHT = 4; - - public UnderlinedLabelPainter() { - super(true); - } - - @Override - public Rectangle paintLabel(@NotNull Graphics2D g2, - @NotNull String text, - int paddingX, - int paddingY, - @NotNull Color labelColor, - @NotNull Color flagColor) { - - Rectangle rectangle = super.paintLabel(g2, text, paddingX, paddingY, 0, labelColor); - - g2.setColor(flagColor); - g2.fillRect(paddingX, paddingY + rectangle.height + LINE_GAP, rectangle.width, LINE_HEIGHT); - - return new Rectangle(paddingX, paddingY, rectangle.width, rectangle.height + LINE_GAP + LINE_HEIGHT); - } - - @Override - public Dimension calculateSize(@NotNull String text, @NotNull FontMetrics metrics) { - Dimension size = super.calculateSize(text, metrics); - return new Dimension(size.width, size.height + LINE_GAP + LINE_HEIGHT); - } - } -} diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/TextLabelPainter.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/TextLabelPainter.java new file mode 100644 index 000000000000..de7759e2b443 --- /dev/null +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/TextLabelPainter.java @@ -0,0 +1,177 @@ +/* + * Copyright 2000-2015 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.vcs.log.ui.render; + +import com.intellij.openapi.util.registry.Registry; +import com.intellij.ui.JBColor; +import com.intellij.ui.SimpleColoredComponent; +import com.intellij.util.ui.UIUtil; +import org.jetbrains.annotations.NotNull; + +import java.awt.*; +import java.awt.geom.RoundRectangle2D; + +public class TextLabelPainter { + protected static final int TEXT_PADDING_X = 5; + protected static final int TOP_TEXT_PADDING = 2; + protected static final int BOTTOM_TEXT_PADDING = 1; + + private static final int LABEL_ARC = 5; + + private final boolean mySquare; + + public TextLabelPainter(boolean square) { + mySquare = square; + } + + public static TextLabelPainter createPainter(boolean paintFlag) { + boolean square = Registry.is("vcs.log.square.labels"); + if (paintFlag) { + return square ? new UnderlinedLabelPainter() : new FlagLabelPainter(); + } + else { + return new TextLabelPainter(square); + } + } + + public static Font getFont() { + return UIUtil.getLabelFont(); + } + + protected Rectangle paint(@NotNull Graphics2D g2, + @NotNull String text, + int paddingX, + int paddingY, + int textPadding, + @NotNull Color color) { + g2.setFont(getFont()); + g2.setStroke(new BasicStroke(1.5f)); + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + FontMetrics fontMetrics = g2.getFontMetrics(); + int width = fontMetrics.stringWidth(text) + 2 * TEXT_PADDING_X + textPadding; + int height = fontMetrics.getHeight() + TOP_TEXT_PADDING + BOTTOM_TEXT_PADDING; + + g2.setColor(color); + if (mySquare) { + g2.fillRect(paddingX, paddingY, width, height); + } + else { + g2.fill(new RoundRectangle2D.Double(paddingX, paddingY, width, height, LABEL_ARC, LABEL_ARC)); + } + + g2.setColor(JBColor.BLACK); + int x = paddingX + textPadding + TEXT_PADDING_X; + int y = paddingY + SimpleColoredComponent.getTextBaseLine(fontMetrics, height); + g2.drawString(text, x, y); + + return new Rectangle(x, y, width, height); + } + + public Rectangle paint(@NotNull Graphics2D g2, @NotNull String text, int paddingX, int paddingY, @NotNull Color color) { + return paint(g2, text, paddingX, paddingY, 0, color); + } + + public Rectangle paint(@NotNull Graphics2D g2, + @NotNull String text, + int paddingX, + int paddingY, + @NotNull Color labelColor, + @NotNull Color flagColor) { + return paint(g2, text, paddingX, paddingY, 0, labelColor); + } + + public Dimension calculateSize(@NotNull String text, @NotNull FontMetrics metrics) { + int width = metrics.stringWidth(text) + 2 * TEXT_PADDING_X; + int height = metrics.getHeight() + TOP_TEXT_PADDING + BOTTOM_TEXT_PADDING; + return new Dimension(width, height); + } + + private static class FlagLabelPainter extends TextLabelPainter { + private static final int FLAG_WIDTH = 8; + private static final int FLAG_PADDING = 6; + private static final int FLAG_TOP_INDENT = 2; + + public FlagLabelPainter() { + super(false); + } + + @Override + public Rectangle paint(@NotNull Graphics2D g2, + @NotNull String text, + int paddingX, + int paddingY, + @NotNull Color labelColor, + @NotNull Color flagColor) { + + Rectangle rectangle = + super.paint(g2, text, paddingX, paddingY + FLAG_TOP_INDENT, FLAG_WIDTH + 2 * FLAG_PADDING - TEXT_PADDING_X, labelColor); + + g2.setColor(flagColor); + int x0 = paddingX + FLAG_PADDING; + int xMid = x0 + FLAG_WIDTH / 2; + int xRight = x0 + FLAG_WIDTH; + + int y0 = paddingY; + int yMid = y0 + 2 * rectangle.height / 3 - 2; + int yBottom = y0 + rectangle.height - 4; + + // something like a knight flag + Polygon polygon = new Polygon(new int[]{x0, xRight, xRight, xMid, x0}, new int[]{y0, y0, yMid, yBottom, yMid}, 5); + g2.fillPolygon(polygon); + + return new Rectangle(paddingX, paddingY, rectangle.width, rectangle.height + FLAG_TOP_INDENT); + } + + @Override + public Dimension calculateSize(@NotNull String text, @NotNull FontMetrics metrics) { + Dimension size = super.calculateSize(text, metrics); + return new Dimension(size.width + FLAG_WIDTH + 2 * FLAG_PADDING - TEXT_PADDING_X, size.height + FLAG_TOP_INDENT); + } + } + + private static class UnderlinedLabelPainter extends TextLabelPainter { + + private static final int LINE_GAP = 1; + private static final int LINE_HEIGHT = 4; + + public UnderlinedLabelPainter() { + super(true); + } + + @Override + public Rectangle paint(@NotNull Graphics2D g2, + @NotNull String text, + int paddingX, + int paddingY, + @NotNull Color labelColor, + @NotNull Color flagColor) { + + Rectangle rectangle = super.paint(g2, text, paddingX, paddingY, 0, labelColor); + + g2.setColor(flagColor); + g2.fillRect(paddingX, paddingY + rectangle.height + LINE_GAP, rectangle.width, LINE_HEIGHT); + + return new Rectangle(paddingX, paddingY, rectangle.width, rectangle.height + LINE_GAP + LINE_HEIGHT); + } + + @Override + public Dimension calculateSize(@NotNull String text, @NotNull FontMetrics metrics) { + Dimension size = super.calculateSize(text, metrics); + return new Dimension(size.width, size.height + LINE_GAP + LINE_HEIGHT); + } + } +} diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/VcsRefPainter.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/VcsRefPainter.java new file mode 100644 index 000000000000..a2c2becc5812 --- /dev/null +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/VcsRefPainter.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2015 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.vcs.log.ui.render; + +import com.intellij.vcs.log.VcsRef; +import com.intellij.vcs.log.ui.VcsLogColorManager; +import com.intellij.vcs.log.ui.VcsLogColorManagerImpl; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; +import java.awt.*; + +public class VcsRefPainter { + @NotNull private final VcsLogColorManager myColorManager; + @NotNull private final TextLabelPainter myTextLabelPainter; + + public VcsRefPainter(@NotNull VcsLogColorManager colorManager, boolean paintRoot) { + myColorManager = colorManager; + myTextLabelPainter = TextLabelPainter.createPainter(paintRoot && colorManager.isMultipleRoots()); + } + + public void paint(@NotNull VcsRef reference, @NotNull Graphics g, int paddingX, int paddingY) { + myTextLabelPainter.paint((Graphics2D)g, reference.getName(), paddingX, paddingY, reference.getType().getBackgroundColor(), + VcsLogColorManagerImpl.getIndicatorColor(myColorManager.getRootColor(reference.getRoot()))); + } + + public Rectangle paint(@NotNull String text, + @NotNull Graphics g, + int paddingX, + int paddingY, + @NotNull Color color, + @NotNull Color flagColor) { + return myTextLabelPainter.paint((Graphics2D)g, text, paddingX, paddingY, color, flagColor); + } + + public Dimension getSize(@NotNull VcsRef reference, @NotNull JComponent component) { + return getSize(reference.getName(), component); + } + + public Dimension getSize(@NotNull String referenceName, @NotNull JComponent component) { + return myTextLabelPainter.calculateSize(referenceName, component.getFontMetrics(TextLabelPainter.getFont())); + } + + public int getHeight(@NotNull JComponent component) { + return myTextLabelPainter.calculateSize("", component.getFontMetrics(TextLabelPainter.getFont())).height; + } +}