SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
EditorTab.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.IO;
8 
9 namespace SeciControls
10 {
11  class EditTab : TabItem
12  {
13  private TextBox _textbox;
14  private String _filename = "";
15  private String _shortfilename = "";
16  private DateTime _lastmodified;
17 
18  public String Filename { get { return _filename; } set { _filename = value; } }
19 
20  public EditTab(String filename)
21  {
22  setFilename(filename);
23 
24  _textbox = new TextBox();
25  String text = "";
26  loadFile(filename, ref text);
27  _textbox.Text = text;
28  _textbox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
29  _textbox.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
30  _textbox.AcceptsReturn = true;
31  _textbox.AcceptsTab = true;
32  _textbox.TextChanged += new TextChangedEventHandler(_textbox_TextChanged);
33 
34  this.AddChild(_textbox);
35  }
36 
37  public void ReloadFile()
38  {
39  String text = "";
40  loadFile(_filename, ref text);
41  _textbox.Text = text;
43  }
44 
45  private Boolean loadFile(string filename, ref String text)
46  {
47  try
48  {
49  FileInfo fi = new FileInfo(filename);
50 
51  if (fi.Exists)
52  {
53  getModifiedTime(filename);
54 
55  using (StreamReader sr = new StreamReader(filename))
56  {
57  text = sr.ReadToEnd();
58  }
59 
60  return true;
61  }
62  else
63  {
64  text = "Could not find file: " + filename;
65  return false;
66  }
67  }
68  catch
69  {
70  text = "Could not load file: " + filename;
71  return false;
72  }
73  }
74 
75  private void getModifiedTime(string filename)
76  {
77  FileInfo fi = new FileInfo(filename);
78  _lastmodified = fi.LastWriteTime;
79  }
80 
81  public void SaveText(string filename)
82  {
83  try
84  {
85  using (StreamWriter sw = new StreamWriter(filename, false))
86  {
87  sw.Write(_textbox.Text);
88  setFilename(filename);
89  getModifiedTime(filename);
90  }
91  }
92  catch
93  {
94  //log this
95  }
96  }
97 
98  private void setFilename(string filename)
99  {
100  _filename = filename;
101  String[] filepath = filename.Split('\\');
102  _shortfilename = filepath[filepath.Length - 1];
103  this.Header = _shortfilename;
104  }
105 
106  private void _textbox_TextChanged(object sender, TextChangedEventArgs e)
107  {
108  this.Header = _shortfilename + " *";
109  }
110  }
111 }
void _textbox_TextChanged(object sender, TextChangedEventArgs e)
Definition: EditorTab.cs:106
void setFilename(string filename)
Definition: EditorTab.cs:98
void getModifiedTime(string filename)
Definition: EditorTab.cs:75
EditTab(String filename)
Definition: EditorTab.cs:20
Boolean loadFile(string filename, ref String text)
Definition: EditorTab.cs:45
DateTime _lastmodified
Definition: EditorTab.cs:16
void SaveText(string filename)
Definition: EditorTab.cs:81