TextModel Code


Here s a summary of TextModel as it stands now. I think you ll agree that it s much better. Was it worth a couple of hours? I think so. What do you think?

 using System; 
using System.IO;
using System.Collections;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
namespace Notepad {
class InsertAction {
private TextModel.Tags command;
private string menuString;
private string[] tagsToInsert;
private string[] tagsToSkip;
public InsertAction(TextModel.Tags tag, string menu, string[] inserts,
string[] skips) {
command = tag;
menuString = menu;
tagsToInsert = inserts;
tagsToSkip = skips;
}
public TextModel.Tags Command {
get { return command; }
}
public string MenuString {
get { return menuString; }
}
public string[] TagsToInsert {
get { return tagsToInsert; }
}
public string[] TagsToSkip {
get { return tagsToSkip; }
}
}
class TextModel {
private ArrayList lines;
private int selectionStart;
private static InsertAction[] insertActions = new InsertAction[] {
new InsertAction(
Tags.Pre,
"Insert &Pre",
new string[] { "<pre></pre>" },
new string[] { "<pre>" }),
new InsertAction(
Tags.Section,
"Insert &Section",
new string[] {"<sect1><title></title>","</sect1>" },
new string[] {"<sect1><title>" }), new InsertAction(
Tags.UnorderedList,
"Insert &UL",
new string[] {"<UL>","<LI></LI>","</UL>"},
new string[] {"<UL>", "<LI>" }),
new InsertAction(
Tags.OrderedList,
"Insert &OL",
new string[] {"<OL>","<LI></LI>","</OL>"},
new string[] {"<OL>", "<LI>" }),
new InsertAction(
Tags.Paragraph,
null,
new string[] { "<P></P>" },
new string[] { "<P>" }),
new InsertAction(
Tags.ListItem,
null,
new string[] { "<LI></LI>" },
new string[] { "<LI>" })
};
public InsertAction[] InsertActions {
get { return insertActions; }
}
public enum Tags {
Pre = 1,
Section = 2,
UnorderedList = 3,
ListItem = 4,
Paragraph = 5,
OrderedList = 6
}
public TextModel() {
lines = new ArrayList();
}
public ArrayList Lines {
get { return lines; }
set { lines = value; }
}
public void SetLines(String[] lines) {
this.Lines = new ArrayList(lines);
}
public String[] LinesArray() {
return (string[])lines.ToArray(typeof(string));
} public String TestText {
get {
StringBuilder b = new StringBuilder();
foreach(String s in lines) {
b.Append(s);
b.Append(System.Environment.NewLine);
}
b.Insert(SelectionStart,"");
return b.ToString();
}
}
public int SelectionStart {
get { return selectionStart; }
set { selectionStart = value; }
}
public void Enter() {
if (InListItem())
InsertTags(Tags.ListItem);
else
InsertTags(Tags.Paragraph);
}
private Boolean InListItem() {
if (LineContainingCursor() < 0 ) return false;
return ((string) lines[LineContainingCursor()]).StartsWith("<LI>");
}
public void InsertReturn() {
string front = FrontOfCursorLine();
string back = BackOfCursorLine();
lines[LineContainingCursor()] = front;
lines.Insert(LineContainingCursor()+1, back);
selectionStart += Environment.NewLine.Length;
}
public string FrontOfCursorLine() {
string line = (string) lines[LineContainingCursor()];
int position = PositionOfCursorInLine();
return line.Substring(0, position);
}
public string BackOfCursorLine() {
string line = (string) lines[LineContainingCursor()];
int position = PositionOfCursorInLine();
return line.Substring(position);
}
public int PositionOfCursorInLine() {
return selectionStart - FirstPositionOfLine(LineContainingCursor());
}
public void InsertTags(Tags command) {
InsertTags(ActionForCommand(command));
} public void InsertTags(InsertAction action) {
int cursorLine = LineContainingCursor();
lines.InsertRange(cursorLine+1, action.TagsToInsert);
selectionStart = NewSelectionStart(cursorLine + 1, action.TagsToSkip);
}
private InsertAction ActionForCommand(Tags command) {
foreach ( InsertAction action in InsertActions) {
if (action.Command == command)
return action;
}
return null;
}
private int NewSelectionStart(int cursorLine, string[] tags) {
return FirstPositionOfLine(cursorLine) + TotalTagLength(tags);
}
private int TotalTagLength(string[] tags) {
int result = (tags.Length -1) * Environment.NewLine.Length;
foreach (string tag in tags) {
result += tag.Length;
}
return result;
}
private int FirstPositionOfLine(int cursorLine) {
int length = 0;
for (int i = 0; i < cursorLine; i++)
length += ((String)lines[i]).Length + Environment.NewLine.Length;
return length;
}
public void ChangeToH2() {
ArrayList linesList = Lines;
String oldLine = (String) linesList[LineContainingCursor()];
Regex r = new Regex("<(?<prefix>.*)>(?<body>.*)</(?<suffix>.*)>");
Match m = r.Match(oldLine);
String newLine = "<H2>" + m.Groups["body"] + "</H2>";
linesList[LineContainingCursor()] = newLine;
Lines = linesList;
}
private int LineContainingCursor() {
if (lines.Count == 0)
return -1;
int length = 0;
int lineNr = 0;
int cr = Environment.NewLine.Length;
foreach ( String s in lines) {
if (length <= selectionStart
&& selectionStart < length+s.Length + cr )
break;
length += s.Length + cr;
lineNr++;
}
return lineNr;
}
public void Save(TextWriter w) { foreach (string line in lines )
w.WriteLine(line);
}
public void Load(TextReader r) {
String line;
lines = new ArrayList();
while((line = r.ReadLine()) != null) {
lines.Add(line);
}
}
}
}



Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net