[Roam-commit] SF.net SVN: roam: [130] Trunk/Source
Status: Alpha
Brought to you by:
jonfroehlich
|
From: <jon...@us...> - 2008-03-20 21:26:26
|
Revision: 130
http://roam.svn.sourceforge.net/roam/?rev=130&view=rev
Author: jonfroehlich
Date: 2008-03-20 14:26:21 -0700 (Thu, 20 Mar 2008)
Log Message:
-----------
1. fixed GetApproximateAge in TimeUtils, now printing our correct plurals
2. added DialogSafeForm hack to RoamForm
Modified Paths:
--------------
Trunk/Source/Roam.Common/Utils/TimeUtils.cs
Trunk/Source/Roam.Test/Roam.Test.csproj
Trunk/Source/Roam.UI/Forms/RoamForm.cs
Modified: Trunk/Source/Roam.Common/Utils/TimeUtils.cs
===================================================================
--- Trunk/Source/Roam.Common/Utils/TimeUtils.cs 2008-03-19 08:42:06 UTC (rev 129)
+++ Trunk/Source/Roam.Common/Utils/TimeUtils.cs 2008-03-20 21:26:21 UTC (rev 130)
@@ -42,139 +42,176 @@
return GetApproximateAge(timeSpan, 1);
}
+
+ public static string GetApproximateAge(TimeSpan timeSpan, int maxUnits)
+ {
+ return GetApproximateAge(timeSpan, maxUnits, false);
+ }
+
/// <summary>
/// Very approximate age (doesn't calculate leap year, assumes all months are 30 days long, etc.)
/// </summary>
/// <param name="timeSpan"></param>
/// <param name="maxUnits"></param>
/// <returns></returns>
- public static string GetApproximateAge(TimeSpan timeSpan, int maxUnits)
+ public static string GetApproximateAge(TimeSpan timeSpan, int maxUnits, bool verboseNames)
{
if (timeSpan.TotalDays > DaysInYear)
{
double years = timeSpan.TotalDays / DaysInYear;
+ string unit = (verboseNames) ? "year" : "yr";
if (maxUnits == 1)
{
- return string.Format("{0:0.0} years", years);
+ return FormatPlural(years, unit);
}
else
{
int yearsInteger = (int)Math.Floor(years);
double fraction = years - yearsInteger;
double daysLeft = fraction * DaysInYear;
- return string.Format("{0} year{1}, {2}",
- yearsInteger, (yearsInteger == 1) ? "" : "s",
- GetApproximateAge(TimeSpan.FromDays(daysLeft), maxUnits - 1));
+ return string.Format("{0} {1}{2}, {3}",
+ yearsInteger, unit, (yearsInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromDays(daysLeft), maxUnits - 1, verboseNames));
}
}
else if (timeSpan.TotalDays > DaysInMonth)
{
double months = timeSpan.TotalDays / DaysInMonth;
+ string unit = (verboseNames) ? "month" : "mo";
if (maxUnits == 1)
{
- return string.Format("{0:0.0} months", months);
+ return FormatPlural(months, unit);
}
else
{
int monthsInteger = (int)Math.Floor(months);
double fraction = months - monthsInteger;
double daysLeft = fraction * DaysInMonth;
- return string.Format("{0} month{1}, {2}",
- monthsInteger, (monthsInteger == 1) ? "" : "s",
- GetApproximateAge(TimeSpan.FromDays(daysLeft), maxUnits - 1));
+ return string.Format("{0} {1}{2}, {3}",
+ monthsInteger, unit, (monthsInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromDays(daysLeft), maxUnits - 1, verboseNames));
}
}
else if (timeSpan.TotalDays > DaysInWeek)
{
double weeks = timeSpan.TotalDays / DaysInWeek;
+ string unit = (verboseNames) ? "week" : "wk";
if (maxUnits == 1)
{
- return string.Format("{0:0.0} weeks", weeks);
+ return FormatPlural(weeks, unit);
}
else
{
int weeksInteger = (int)Math.Floor(weeks);
double fraction = weeks - weeksInteger;
double daysLeft = fraction * DaysInWeek;
- return string.Format("{0} week{1}, {2}",
- weeksInteger, (weeksInteger == 1) ? "" : "s",
- GetApproximateAge(TimeSpan.FromDays(daysLeft), maxUnits - 1));
+ return string.Format("{0} {1}{2}, {3}",
+ weeksInteger, unit, (weeksInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromDays(daysLeft), maxUnits - 1, verboseNames));
}
}
else if (timeSpan.TotalDays > 1)
{
double days = timeSpan.TotalDays;
+ string unit = "day";
if (maxUnits == 1)
{
- return string.Format("{0:0.0} days", days);
+ return FormatPlural(days, unit);
}
else
{
int daysInteger = (int)Math.Floor(days);
double fraction = days - daysInteger;
double hoursLeft = fraction;
- return string.Format("{0} day{1}, {2}",
- daysInteger, (daysInteger == 1) ? "" : "s",
- GetApproximateAge(TimeSpan.FromHours(hoursLeft), maxUnits - 1));
+ return string.Format("{0} {1}{2}, {3}",
+ daysInteger, unit, (daysInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromHours(hoursLeft), maxUnits - 1, verboseNames));
}
}
else if (timeSpan.TotalHours > 1)
{
double hours = timeSpan.TotalHours;
+ string unit = (verboseNames) ? "hour" : "hr";
if (maxUnits == 1)
{
- return string.Format("{0:0.0} hours", hours);
+ return FormatPlural(hours, unit);
}
else
{
int hoursInteger = (int)Math.Floor(hours);
double fraction = hours - hoursInteger;
double minutesLeft = fraction;
- return string.Format("{0} hour{1}, {2}",
- hoursInteger, (hoursInteger == 1) ? "" : "s",
+ return string.Format("{0} {1}{2}, {3}",
+ hoursInteger, unit, (hoursInteger == 1) ? "" : "s",
GetApproximateAge(TimeSpan.FromMinutes(minutesLeft), maxUnits - 1));
}
}
else if (timeSpan.TotalMinutes > 1)
{
double minutes = timeSpan.TotalMinutes;
+ string unit = (verboseNames) ? "minute" : "min";
if (maxUnits == 1)
{
- return string.Format("{0:0.0} minutes", minutes);
+ return FormatPlural(minutes, unit);
}
else
{
int minutesInteger = (int)Math.Floor(minutes);
double fraction = minutes - minutesInteger;
double secondsLeft = fraction;
- return string.Format("{0} minute{1}, {2}",
- minutesInteger, (minutesInteger == 1) ? "" : "s",
+ return string.Format("{0} {1}{2}, {3}",
+ minutesInteger, unit, (minutesInteger == 1) ? "" : "s",
GetApproximateAge(TimeSpan.FromSeconds(secondsLeft), maxUnits - 1));
}
}
else if (timeSpan.TotalSeconds > 1)
{
double seconds = timeSpan.TotalSeconds;
+ string unit = (verboseNames) ? "second" : "sec";
if (maxUnits == 1)
{
- return string.Format("{0:0.0} seconds", seconds);
+ return FormatPlural(seconds, unit);
}
else
{
int secondsInteger = (int)Math.Floor(seconds);
double fraction = seconds - secondsInteger;
double millisecondsLeft = fraction;
- return string.Format("{0} second{1}, {2}",
- secondsInteger, (secondsInteger == 1) ? "" : "s",
+ return string.Format("{0} {1}{2}, {3}",
+ secondsInteger, unit, (secondsInteger == 1) ? "" : "s",
GetApproximateAge(TimeSpan.FromMilliseconds(millisecondsLeft), maxUnits - 1));
}
}
else
{
- return string.Format("{0:0.0} milliseconds", timeSpan.TotalMilliseconds);
+ string unit = (verboseNames) ? "milliseconds" : "ms";
+ string s = string.Format("{0:0.0}", timeSpan.TotalMilliseconds);
+ if (s.Length >= "1000.0".Length)
+ {
+ double seconds = timeSpan.TotalSeconds;
+ unit = (verboseNames) ? "second" : "sec";
+ return FormatPlural(seconds, unit);
+ }
+ return string.Format("{0:0.0} {1}", timeSpan.TotalMilliseconds, unit);
}
}
+ private static string FormatPlural(double value, string unit)
+ {
+ string strValue = string.Format("{0:0.0}", value);
+
+ if (strValue == "1.0") { return string.Format("1 {0}", unit); }
+ else if (strValue[strValue.Length - 1] == '0')
+ {
+ int intValue = (int)value;
+ return string.Format("{0} {1}{2}", intValue, unit, (intValue == 1) ? "" : "s");
+ }
+ else
+ {
+ return string.Format("{0:0.0} {1}s", value, unit);
+ }
+ }
+
public static TimeSpan Interval(DateTime dt1, DateTime dt2)
{
return (TimeSpan)(dt2 - dt1);
Modified: Trunk/Source/Roam.Test/Roam.Test.csproj
===================================================================
--- Trunk/Source/Roam.Test/Roam.Test.csproj 2008-03-19 08:42:06 UTC (rev 129)
+++ Trunk/Source/Roam.Test/Roam.Test.csproj 2008-03-20 21:26:21 UTC (rev 130)
@@ -564,7 +564,7 @@
<DependentUpon>TextBoxFilteredListTest.cs</DependentUpon>
</Compile>
<Compile Include="UI\Controls\FontTest.cs">
- <SubType>Component</SubType>
+ <SubType>Form</SubType>
</Compile>
<Compile Include="UI\Controls\FontTest.Designer.cs">
<DependentUpon>FontTest.cs</DependentUpon>
@@ -702,7 +702,7 @@
<DependentUpon>SystemColorTest.cs</DependentUpon>
</Compile>
<Compile Include="UI\Controls\TabControlTest.cs">
- <SubType>Form</SubType>
+ <SubType>Component</SubType>
</Compile>
<Compile Include="UI\Controls\TabControlTest.Designer.cs">
<DependentUpon>TabControlTest.cs</DependentUpon>
Modified: Trunk/Source/Roam.UI/Forms/RoamForm.cs
===================================================================
--- Trunk/Source/Roam.UI/Forms/RoamForm.cs 2008-03-19 08:42:06 UTC (rev 129)
+++ Trunk/Source/Roam.UI/Forms/RoamForm.cs 2008-03-20 21:26:21 UTC (rev 130)
@@ -19,6 +19,7 @@
using System.Drawing;
using System.Text;
using System.Windows.Forms;
+using System.Diagnostics;
namespace Roam.UI.Forms
{
@@ -150,6 +151,7 @@
get { return base.DialogResult; }
set
{
+ bool threwException = false;
try
{
base.DialogResult = value;
@@ -158,6 +160,8 @@
{
//consume argument exception
//see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=328786
+ Debug.WriteLine(string.Format("RoamForm.DialogResult={0} : Exception: {1}", value, ae.Message));
+ threwException = true;
}
//The following is a workaround relating to the differences between form.ShowDialog()
@@ -173,7 +177,24 @@
//In addition, if a form was shown via form.Show(), setting its DialogResult
//has no effect. This changes that behavior such that it is consistent with
//form.ShowDialog()
- if (!IsClosed) { this.Close(); }
+ try
+ {
+ if (!IsClosed) { this.Close(); }
+ }
+ catch (ArgumentException ae)
+ {
+ //consume argument exception
+ //see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=328786
+ Debug.WriteLine(string.Format("RoamForm.DialogResult={0} : Exception: {1}", value, ae.Message));
+ threwException = true;
+ }
+
+ // If the dialog threw an exception on close then we probably ran into
+ // the bug described here: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=328786
+ // The only way I found to close a form at all in this case is to call dispose
+ // This is incredibly hacky and has some side effects but is the only known
+ // way to effectively close the dialog
+ if (threwException) { this.Dispose(); }
}
}
#endregion Properties
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|