IDEA-27179 (Repository view of Changes shows wrong dates)
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / committed / ChangeListGroupingStrategy.java
blob44934beb1d5330e214ddd4683f812bd9d8f3d864
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.
16 package com.intellij.openapi.vcs.changes.committed;
18 import com.intellij.openapi.vcs.VcsBundle;
19 import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList;
20 import org.jetbrains.annotations.NonNls;
22 import java.text.SimpleDateFormat;
23 import java.util.Calendar;
24 import java.util.Comparator;
25 import java.util.Date;
26 import java.util.Locale;
28 /**
29 * @author yole
31 public interface ChangeListGroupingStrategy {
32 void beforeStart();
33 boolean changedSinceApply();
34 String getGroupName(CommittedChangeList changeList);
35 Comparator<CommittedChangeList> getComparator();
37 class DateChangeListGroupingStrategy implements ChangeListGroupingStrategy {
38 @NonNls private final SimpleDateFormat myWeekdayFormat = new SimpleDateFormat("EEEE", Locale.ENGLISH);
39 @NonNls private final SimpleDateFormat myMonthFormat = new SimpleDateFormat("MMMM", Locale.ENGLISH);
40 @NonNls private final SimpleDateFormat myMonthYearFormat = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
41 private long myTimeToRecalculateAfter;
42 private Calendar myCurrentCalendar;
44 public String toString() {
45 return VcsBundle.message("date.group.title");
48 public boolean changedSinceApply() {
49 return System.currentTimeMillis() > myTimeToRecalculateAfter;
52 public void beforeStart() {
53 myCurrentCalendar = Calendar.getInstance();
54 // +- seconds etc
55 myCurrentCalendar.set(Calendar.HOUR, 0);
56 myCurrentCalendar.set(Calendar.MINUTE, 0);
58 myTimeToRecalculateAfter = myCurrentCalendar.getTimeInMillis() + 24 * 60 * 60 * 1000;
59 myCurrentCalendar.setTime(new Date());
62 public String getGroupName(final CommittedChangeList list) {
63 Calendar clCal = Calendar.getInstance();
64 clCal.setTime(list.getCommitDate());
65 if (myCurrentCalendar.get(Calendar.YEAR) == clCal.get(Calendar.YEAR)) {
66 if (myCurrentCalendar.get(Calendar.DAY_OF_YEAR) == clCal.get(Calendar.DAY_OF_YEAR)) {
67 return VcsBundle.message("date.group.today");
69 if (myCurrentCalendar.get(Calendar.WEEK_OF_YEAR) == clCal.get(Calendar.WEEK_OF_YEAR)) {
70 return myWeekdayFormat.format(list.getCommitDate());
72 if (myCurrentCalendar.get(Calendar.WEEK_OF_YEAR) == clCal.get(Calendar.WEEK_OF_YEAR)+1) {
73 return VcsBundle.message("date.group.last.week");
75 return myMonthFormat.format(list.getCommitDate());
77 return myMonthYearFormat.format(list.getCommitDate());
80 public Comparator<CommittedChangeList> getComparator() {
81 return new Comparator<CommittedChangeList>() {
82 public int compare(final CommittedChangeList o1, final CommittedChangeList o2) {
83 return -o1.getCommitDate().compareTo(o2.getCommitDate());
89 ChangeListGroupingStrategy USER = new ChangeListGroupingStrategy() {
90 public String toString() {
91 return VcsBundle.message("user.group.title");
94 public String getGroupName(final CommittedChangeList changeList) {
95 return changeList.getCommitterName();
98 public void beforeStart() {
101 public boolean changedSinceApply() {
102 return false;
105 public Comparator<CommittedChangeList> getComparator() {
106 return new Comparator<CommittedChangeList>() {
107 public int compare(final CommittedChangeList o1, final CommittedChangeList o2) {
108 int rc = o1.getCommitterName().compareToIgnoreCase(o2.getCommitterName());
109 if (rc == 0) {
110 return -o1.getCommitDate().compareTo(o2.getCommitDate());
112 return rc;