/*----------------------------------------------------------------------
Prof-It for C#
Copyright (c) 2004 Klaus Lehner, University of Linz
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
----------------------------------------------------------------------*/
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace at.jku.ssw.ProfIt{
/// <summary>
/// This class encapsulates the configuration of Prof-It, such as
/// colors, the last opened solutions, etc...
/// </summary>
[Serializable]
public class Configuration {
[NonSerialized]
private static Configuration currentConfig;
[NonSerialized]
private static string fileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ProfIt\\" + "ProfIt.config";
private ColoredFont blockFont;
private ImportCounters importCounters;
private ResetCounters resetCounters;
private bool[][] tableColumns;
private RangeCollection colorRanges;
private bool colorRangesActive;
private ArrayList lastSolutions;
static Configuration() {
currentConfig = new Configuration();
currentConfig.blockFont = new ColoredFont(new Font("Courier New", 10), Color.Black, Color.LightGray);
currentConfig.importCounters = ImportCounters.All | ImportCounters.AskMe;
currentConfig.resetCounters = ResetCounters.AskMe;
currentConfig.tableColumns = new bool[3][];
currentConfig.tableColumns[(int)Tables.Blocks] = new bool[] { false, true, true, true, true};
currentConfig.tableColumns[(int)Tables.Methods] = new bool[] { true, true, true, true, true, true };
currentConfig.tableColumns[(int)Tables.Classes] = new bool[] { true, true, true, true, true, true };
currentConfig.colorRanges = new RangeCollection();
currentConfig.colorRangesActive = false;
currentConfig.lastSolutions = new ArrayList();
}
public static ColoredFont BlockFont {
get { return currentConfig.blockFont; }
set { currentConfig.blockFont = value; Store(); }
}
public static ImportCounters ImportCountersStrategy {
get { return currentConfig.importCounters; }
set { currentConfig.importCounters = value; Store(); }
}
public static ResetCounters ResetCountersStrategy {
get { return currentConfig.resetCounters; }
set { currentConfig.resetCounters = value; Store(); }
}
public static bool IsColumnVisible(Tables table, int column) {
return currentConfig.tableColumns[(int)table][column];
}
public static void SetColumnVisible(Tables table, int column, bool value) {
currentConfig.tableColumns[(int)table][column] = value;
Store();
}
public static RangeCollection ColorRanges {
set { currentConfig.colorRanges = value; Store(); }
get { return currentConfig.colorRanges; }
}
public static bool ColorRangesActive {
set { currentConfig.colorRangesActive = value; Store(); }
get { return currentConfig.colorRangesActive; }
}
public static ArrayList LastSolutions {
get { return currentConfig.lastSolutions; }
}
public static void AddSolution(string fileName) {
if (currentConfig.lastSolutions.Contains(fileName)) {
currentConfig.lastSolutions.Remove(fileName);
} else {
while (currentConfig.lastSolutions.Count > 5) currentConfig.lastSolutions.RemoveAt(4);
}
currentConfig.lastSolutions.Insert(0, fileName);
Store();
}
public static void Load() {
try {
FileStream myStream = File.OpenRead(fileName);
BinaryFormatter formatter = new BinaryFormatter();
Configuration conf = (Configuration)formatter.Deserialize(myStream);
myStream.Close();
currentConfig = conf;
} catch {}
}
public static void Store() {
FileStream myStream = File.Create(fileName);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(myStream, currentConfig);
myStream.Close();
}
}
public enum ImportCounters { All=1, Reset=2, None=4, AskMe=8 }
public enum ResetCounters { Always=1, Never=2, AskMe=4 }
public enum Tables { Blocks=0, Methods=1, Classes=2 }
[Serializable]
public class Range {
private int from;
private int to;
private ColoredFont font;
public Range(int from, int to, ColoredFont font) {
this.from = from; this.to = to;
this.font = font;
}
public int From { get { return from; } }
public int To { get { return to; } }
public ColoredFont Font { get { return font; } }
public override string ToString() {
if (from == int.MinValue) return "less than " + to;
if (to == int.MaxValue) return "greater than " + from;
if (from == to) { return "equal to " + from; }
else { return "from " + from + " to " + to; }
}
}
[Serializable]
public class RangeCollection : IEnumerable {
private ArrayList ranges = new ArrayList();
private static RangeComparer rangeComparer = new RangeComparer();
public void AddRange(Range range) {
ranges.Add(range);
ranges.Sort(rangeComparer);
}
public Range this[int i] {
get { if (i>=ranges.Count) return null; else return (Range)ranges[i]; }
}
public Range GetRange(int cnt) {
foreach (Range range in ranges) {
if (range.From <= cnt && range.To >= cnt)
return range;
}
return null;
}
public Range Contain(Range range) {
foreach (Range r in ranges) {
if ((range.From >= r.From && range.From <= r.To) ||
(range.To >= r.From && range.To <= r.To)) return r;
}
return null;
}
#region IEnumerable Members
public IEnumerator GetEnumerator() {
return ranges.GetEnumerator();
}
#endregion
private class RangeComparer : IComparer {
#region IComparer Members
public int Compare(object x, object y) {
return ((Range)x).From - ((Range)y).From;
}
#endregion
}
}
}
|