~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.MacDev/MonoDevelop.MacDev.PlistEditor/PListEditorViewContent.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
2
 
// PListEditorViewContent.cs
3
 
//  
4
 
// Author:
5
 
//       Mike Krüger <mkrueger@xamarin.com>
6
 
// 
7
 
// Copyright (c) 2011 Xamarin <http://xamarin.com>
8
 
// 
9
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
 
// of this software and associated documentation files (the "Software"), to deal
11
 
// in the Software without restriction, including without limitation the rights
12
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 
// copies of the Software, and to permit persons to whom the Software is
14
 
// furnished to do so, subject to the following conditions:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
19
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 
// THE SOFTWARE.
26
 
 
27
 
using System;
28
 
using MonoDevelop.Ide.Gui;
29
 
using MonoDevelop.Core;
30
 
using MonoDevelop.Projects;
31
 
using MonoDevelop.Ide;
32
 
using MonoMac.Foundation;
33
 
using MonoDevelop.Ide.Gui.Content;
34
 
using MonoDevelop.Projects.Text;
35
 
using System.Text;
36
 
using System.IO;        
37
 
 
38
 
namespace MonoDevelop.MacDev.PlistEditor
39
 
{
40
 
        public class PListEditorViewContent : AbstractViewContent, ITextFile
41
 
        {
42
 
                PObjectContainer pobject;
43
 
                IPListDisplayWidget widget;
44
 
                Gtk.Widget control;
45
 
                
46
 
                public override Gtk.Widget Control { get { return control; } }
47
 
                
48
 
                public PListEditorViewContent (IPlistEditingHandler handler, Project proj)
49
 
                {
50
 
                        if (handler != null) {
51
 
                                widget =  new PListEditorWidget (handler, proj);
52
 
                                control = (Gtk.Widget) widget;
53
 
                        } else {
54
 
                                widget = new CustomPropertiesWidget ();
55
 
                                var csw =  new MonoDevelop.Components.CompactScrolledWindow ();
56
 
                                csw.Show ();
57
 
                                csw.AddWithViewport ((Gtk.Widget)widget);
58
 
                                control = csw;
59
 
                        }
60
 
                }
61
 
                
62
 
                public override void Load (string fileName)
63
 
                {
64
 
                        ContentName = fileName;
65
 
 
66
 
                        if (pobject == null) {
67
 
                                var dict = new PDictionary ();
68
 
                                if (dict.Reload (fileName)) {
69
 
                                        pobject = dict;
70
 
                                } else {
71
 
                                        var arr = new PArray ();
72
 
                                        if (!arr.Reload (fileName)) {
73
 
                                                MessageService.ShowError (GettextCatalog.GetString ("Can't load plist file {0}.", fileName));
74
 
                                                return;
75
 
                                        }
76
 
                                        pobject = arr;
77
 
                                }
78
 
                                
79
 
                                Buffer = null;
80
 
                                widget.SetPListContainer (pobject);
81
 
                                pobject.Changed += (sender, e) => {
82
 
                                        Buffer = null;
83
 
                                        IsDirty = true;
84
 
                                };
85
 
                        }
86
 
                        this.IsDirty = false;
87
 
                }
88
 
                
89
 
                public override void Save (string fileName)
90
 
                {
91
 
                        ContentName = fileName;
92
 
 
93
 
                        try {
94
 
                                pobject.Save (fileName);
95
 
                                this.IsDirty = false;
96
 
                        } catch (Exception e) {
97
 
                                MessageService.ShowException (e, GettextCatalog.GetString ("Error while writing plist"));
98
 
                        }
99
 
                }
100
 
                
101
 
                string Buffer {
102
 
                        get; set;
103
 
                }
104
 
 
105
 
                string ITextFile.Text {
106
 
                        get { EnsureBuffer (); return Buffer; }
107
 
                }
108
 
 
109
 
                char ITextFile.GetCharAt (int position)
110
 
                {
111
 
                        EnsureBuffer ();
112
 
                        return Buffer [position];
113
 
                }
114
 
                
115
 
                string ITextFile.GetText (int startPosition, int endPosition)
116
 
                {
117
 
                        EnsureBuffer ();
118
 
                        return Buffer.Substring (startPosition, endPosition - startPosition + 1);
119
 
                }
120
 
 
121
 
                int ITextFile.GetPositionFromLineColumn (int line, int column)
122
 
                {
123
 
                        EnsureBuffer ();
124
 
                        int lin = 1;
125
 
                        int col = 1;
126
 
                        for (int i = 0; i < Buffer.Length && lin <= line; i++) {
127
 
                                if (line == lin && column == col)
128
 
                                        return i;
129
 
                                if (Buffer[i] == '\r') {
130
 
                                        if (i + 1 < Buffer.Length && Buffer[i + 1] == '\n')
131
 
                                                i++;
132
 
                                        lin++; col = 1;
133
 
                                } else if (Buffer[i] == '\n') {
134
 
                                        lin++; col = 1;
135
 
                                } else
136
 
                                        col++;
137
 
                        }
138
 
                        return -1;
139
 
                }
140
 
 
141
 
                void ITextFile.GetLineColumnFromPosition (int position, out int line, out int column)
142
 
                {
143
 
                        EnsureBuffer ();
144
 
                        int lin = 1;
145
 
                        int col = 1;
146
 
                        for (int i = 0; i < position; i++) {
147
 
                                if (Buffer[i] == '\r') {
148
 
                                        if (i + 1 < position && Buffer[i + 1] == '\n')
149
 
                                                i++;
150
 
                                        lin++; col = 1;
151
 
                                } else if (Buffer[i] == '\n') {
152
 
                                        lin++; col = 1;
153
 
                                } else
154
 
                                        col++;
155
 
                        }
156
 
                        line = lin;
157
 
                        column = col;
158
 
                }
159
 
                
160
 
                FilePath ITextFile.Name {
161
 
                        get { return ContentName; }
162
 
                }
163
 
 
164
 
                int ITextFile.Length {
165
 
                        get { EnsureBuffer (); return Buffer.Length; }
166
 
                }
167
 
                
168
 
                void EnsureBuffer ()
169
 
                {
170
 
                        if (Buffer == null)
171
 
                                Buffer = pobject.ToXml () ?? "";
172
 
                }
173
 
        }
174
 
}
 
 
b'\\ No newline at end of file'