new Apply Patch UI
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / ui / CommitLegendPanel.java
blob6c0613ccf43e544119dff62e1db260f4d09ee1e5
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.openapi.vcs.changes.ui;
19 import com.intellij.openapi.vcs.FileStatus;
20 import com.intellij.openapi.vcs.VcsBundle;
21 import com.intellij.openapi.vcs.changes.Change;
22 import com.intellij.ui.SeparatorFactory;
23 import com.intellij.util.ui.UIUtil;
25 import javax.swing.*;
26 import java.awt.*;
27 import java.util.Collections;
28 import java.util.List;
30 /**
31 * @author max
33 public class CommitLegendPanel {
34 private JLabel myModifiedShown;
35 private JLabel myModifiedIncluded;
36 private JLabel myNewShown;
37 private JLabel myNewIncluded;
38 private JLabel myDeletedIncluded;
39 private JLabel myTotalShown;
40 private JLabel myTotalIncluded;
41 private JPanel myRootPanel;
42 private JLabel myDeletedShown;
43 private JPanel myModifiedPanel;
44 private JLabel myModifiedLabel;
45 private JPanel myNewPanel;
46 private JLabel myNewLabel;
47 private JPanel myDeletedPanel;
48 private JLabel myDeletedLabel;
49 private JPanel myHeadingPanel;
51 private final InfoCalculator myInfoCalculator;
53 public CommitLegendPanel(InfoCalculator infoCalculator) {
54 myInfoCalculator = infoCalculator;
55 final Color background = UIUtil.getListBackground();
56 myModifiedPanel.setBackground(background);
57 myNewPanel.setBackground(background);
58 myDeletedPanel.setBackground(background);
60 myModifiedLabel.setForeground(FileStatus.MODIFIED.getColor());
61 myModifiedLabel.putClientProperty("Quaqua.Component.visualMargin", new Insets(0, 0, 0, 0));
62 myNewLabel.setForeground(FileStatus.ADDED.getColor());
63 myNewLabel.putClientProperty("Quaqua.Component.visualMargin", new Insets(0, 0, 0, 0));
64 myDeletedLabel.setForeground(FileStatus.DELETED.getColor());
65 myDeletedLabel.putClientProperty("Quaqua.Component.visualMargin", new Insets(0, 0, 0, 0));
67 boldLabel(myModifiedLabel, true);
68 boldLabel(myNewLabel, true);
69 boldLabel(myDeletedLabel, true);
72 public JPanel getComponent() {
73 return myRootPanel;
76 public void update() {
77 final int deleted = myInfoCalculator.getDeleted();
78 final int modified = myInfoCalculator.getModified();
79 final int cntNew = myInfoCalculator.getNew();
81 final int includedDeleted = myInfoCalculator.getIncludedDeleted();
82 final int includedModified = myInfoCalculator.getIncludedModified();
83 final int includedNew = myInfoCalculator.getIncludedNew();
85 updateCategory(myTotalShown, myTotalIncluded, deleted + modified + cntNew, includedDeleted + includedModified + includedNew);
86 updateCategory(myModifiedShown, myModifiedIncluded, modified, includedModified);
87 updateCategory(myNewShown, myNewIncluded, cntNew, includedNew);
88 updateCategory(myDeletedShown, myDeletedIncluded, deleted, includedDeleted);
91 private void createUIComponents() {
92 myHeadingPanel = (JPanel)SeparatorFactory.createSeparator(VcsBundle.message("commit.legend.summary"), null);
95 private interface Filter<T> {
96 boolean matches(T item);
99 private static void updateCategory(JLabel totalLabel,
100 JLabel includedLabel,
101 int totalCnt,
102 int includedCnt) {
103 updateLabel(totalLabel, totalCnt, false);
104 updateLabel(includedLabel, includedCnt, totalCnt != includedCnt);
107 private static void updateLabel(JLabel label, int count, boolean bold) {
108 label.setText(String.valueOf(count));
109 label.setEnabled(bold || count != 0);
110 boldLabel(label, bold);
113 private static void boldLabel(final JLabel label, final boolean bold) {
114 label.setFont(label.getFont().deriveFont(bold ? Font.BOLD : Font.PLAIN));
117 public interface InfoCalculator {
118 int getNew();
119 int getModified();
120 int getDeleted();
121 int getIncludedNew();
122 int getIncludedModified();
123 int getIncludedDeleted();
126 public static class ChangeInfoCalculator implements InfoCalculator {
127 private List<Change> myDisplayedChanges;
128 private List<Change> myIncludedChanges;
130 public ChangeInfoCalculator() {
131 myDisplayedChanges = Collections.emptyList();
132 myIncludedChanges = Collections.emptyList();
135 public void update(final List<Change> displayedChanges, final List<Change> includedChanges) {
136 myDisplayedChanges = displayedChanges;
137 myIncludedChanges = includedChanges;
140 public int getNew() {
141 return countMatchingItems(myDisplayedChanges, NEW_FILTER);
144 public int getModified() {
145 return countMatchingItems(myDisplayedChanges, MODIFIED_FILTER);
148 public int getDeleted() {
149 return countMatchingItems(myDisplayedChanges, DELETED_FILTER);
152 public int getIncludedNew() {
153 return countMatchingItems(myIncludedChanges, NEW_FILTER);
156 public int getIncludedModified() {
157 return countMatchingItems(myIncludedChanges, MODIFIED_FILTER);
160 public int getIncludedDeleted() {
161 return countMatchingItems(myIncludedChanges, DELETED_FILTER);
164 private static final Filter<Change> MODIFIED_FILTER = new Filter<Change>() {
165 public boolean matches(final Change item) {
166 return item.getType() == Change.Type.MODIFICATION || item.getType() == Change.Type.MOVED;
169 private static final Filter<Change> NEW_FILTER = new Filter<Change>() {
170 public boolean matches(final Change item) {
171 return item.getType() == Change.Type.NEW;
174 private static final Filter<Change> DELETED_FILTER = new Filter<Change>() {
175 public boolean matches(final Change item) {
176 return item.getType() == Change.Type.DELETED;
180 private static <T> int countMatchingItems(List<T> items, Filter<T> filter) {
181 int count = 0;
182 for (T item : items) {
183 if (filter.matches(item)) count++;
186 return count;