Class: FileH

Inherits:
ArcadiaExt
  • Object
show all
Includes:
Autils
Defined in:
ext/ae-file-history/ae-file-history.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#htreeObject (readonly)

Returns the value of attribute htree.



63
64
65
# File 'ext/ae-file-history/ae-file-history.rb', line 63

def htree
  @htree
end

Instance Method Details

#add2history(_filename, _text_index = '1.0') ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'ext/ae-file-history/ae-file-history.rb', line 256

def add2history(_filename, _text_index='1.0')
  if _filename
    _filename = _filename.sub(Dir::pwd+'/',"")
    _filename = File.expand_path(_filename)
    _filename_index = _filename+";"+_text_index

    if File.exist?(history_file)
      f = File::open(history_file,'r')
      begin
        _lines = f.readlines.collect!{| line | line.chomp}
      ensure
        f.close unless f.nil?
      end
      f = File.new(history_file, "w")
      begin
        if f
          _l= conf('length').to_i
          f.syswrite(_filename_index+"\n")
          _lines[0.._l-2].each{|_line|
            _lfile = _line.split(';')
            if _lfile
              f.syswrite(_line+"\n") if (_lfile[0] != _filename)&&(_line.length > 0)
            end
          }
        end
      ensure
        f.close unless f.nil?
      end
    else
   			dir,fil =File.split(File.expand_path(history_file))
   			if !File.exist?(dir)
   			  Dir.mkdir(dir)
   			end
      f = File.new(history_file, "w+")
      begin
        f.syswrite(_filename+"\n") if f
      ensure
        f.close unless f.nil?
      end
    end
  end
end

#add_to_tree(_file) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/ae-file-history/ae-file-history.rb', line 195

def add_to_tree(_file)
_d, _f = File.split(File.expand_path(_file))
_d = _d.downcase if is_windows?
_f = _f.downcase if is_windows?
  _foreground = conf('color.foreground')
node = root.dir(_d)
  if !@htree.exist?(node.rif)
    @htree.insert('end', node.parent.rif ,node.rif, {
        'text' =>  node.rif ,
        'helptext' => node.rif,
        'deltax'=>-1,
        'fill' => _foreground,
        'font'=>@font,
        'image'=> image('KDir')
      }
    )
  end
  _file_node_rif = _d+'@@@'+_f
  if !@htree.exist?(_file_node_rif)
    @htree.insert('end', node.rif ,_file_node_rif, {
        'text' =>  _f ,
        'helptext' => _f,
        'deltax'=>-1,
        'fill' => _foreground,
        'font'=>@font_b,
        'image'=> image('KFile',_f)
      }
    )
  end
  
end

#build_treeObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/ae-file-history/ae-file-history.rb', line 155

def build_tree
  file_dir = Hash.new
  nodes = Array.new
  if File.exist?(history_file)
    f = File::open(history_file,'r')
    f_open = $arcadia['pers']['editor.files.open'].split("|") if $arcadia['pers']['editor.files.open']
    begin
      _lines = f.readlines.collect!{| line | line.chomp+"\n" }.to_s
      _lines.sort.each{|_line|
        _file = _line.split(';')[0]
        if FileTest::exist?(_file)
				dir,fil =File.split(File.expand_path(_file))
				dir = dir.downcase if is_windows?
				fil = fil.downcase if is_windows?
				file_dir[dir] = Array.new if file_dir[dir] == nil
				file_dir[dir] << fil if !file_dir[dir].include?(fil)
        end
      }
    ensure
      f.close unless f.nil?
    end
  end

  file_dir.keys.sort.each{|_dir|
  				node = root.dir(_dir)
		file_dir[_dir].each{|file|
		  TreeNode.new(node, 'KFile'){|_node|
            _node.rif= _dir+'@@@'+file
            _node.label=file
		  }
		}
  }

  @image_kdir = TkPhotoImage.new('dat' => BOOK_GIF)
  @image_kfile_rb = TkPhotoImage.new('dat' => RUBY_DOCUMENT_GIF)
  @image_kfile = TkPhotoImage.new('dat' => DOCUMENT_GIF)

build_tree_from_node(root)
end

#build_tree_from_node(_node) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/ae-file-history/ae-file-history.rb', line 237

def build_tree_from_node(_node)
  _foreground = conf('color.foreground')
  _sorted_sons = _node.sons.sort
  for inode in 0.._sorted_sons.length - 1
    _son = _sorted_sons[inode]
    @htree.insert('end', _son.parent.rif ,_son.rif, {
      'text' =>  _son.label ,
      'fill' => _foreground,
      'helptext' => _son.helptext,
      'deltax'=>-1,
      'font'=>@font,
      'image'=> image(_son.kind, _son.label)
    }
    )
    build_tree_from_node(_son)
  end
end

#history_fileObject



118
119
120
121
122
123
# File 'ext/ae-file-history/ae-file-history.rb', line 118

def history_file
   if !defined?(@arcadia_history_file)
   		@arcadia_history_file = @arcadia.local_dir+'/'+conf('file.name')
   end
   return @arcadia_history_file
end

#image(_kind, _label = '.rb') ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'ext/ae-file-history/ae-file-history.rb', line 227

def image(_kind, _label='.rb')
    if _kind == 'KDir'
      return @image_kdir
    elsif _kind == 'KFile' && _label.include?('.rb')
      return @image_kfile_rb
    else
      return @image_kfile
    end
end

#on_after_build(_event) ⇒ Object



113
114
115
116
# File 'ext/ae-file-history/ae-file-history.rb', line 113

def on_after_build(_event)
   #ArcadiaContractListener.new(self, EditorContract, :do_editor_event)
   Arcadia.add_listener(self, OpenBufferEvent)
end

#on_after_open_buffer(_event) ⇒ Object



125
126
127
128
129
130
# File 'ext/ae-file-history/ae-file-history.rb', line 125

def on_after_open_buffer(_event)
  if _event.file
    self.add2history(_event.file)
    add_to_tree(_event.file)
  end
end

#on_build(_event) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/ae-file-history/ae-file-history.rb', line 65

def on_build(_event)
   @h_stack = Array.new
   do_select_item = proc{|_self|
     if _self.selection_get[0].length >0
     	_selected = ""
     	_self.selection_get[0].each{|_block|
      	 _selected = _selected + _block.to_s + "\s" 
     	}
     	_selected = _selected.strip
     else
       _selected = _self.selection_get[0]
     end
     _dir, _file = _selected.split('@@@')
     if _file
       _file = File.expand_path( _file  , _dir ) 
   else
      _file = Tk.getOpenFile('initialdir'=>_dir)
   end
   if _file
     Arcadia.process_event(OpenBufferEvent.new(self,'file'=>_file))
       #EditorContract.instance.open_file(self,'file'=>_file)
     end
   }
   _background = conf('color.background')
   _foreground = conf('color.foreground')
   @font =  conf('font')
   @font_b = conf('font.bold')

  @htree = Tk::BWidget::Tree.new(self.frame){
     relief 'flat'
     showlines false
     deltay 18
     background _background
     crossfill _foreground
     #dragenabled true
     selectcommand proc{ do_select_item.call(self) } 
     place('relwidth' => 1,'relx' => 0,'x' => '0','y' => '0','relheight' => '1')
   }
   _scrollcommand = proc{|*args| @htree.yview(*args)}
   _scrollbar = TkScrollbar.new(self.frame){|s|
     width 8
     command _scrollcommand
   }.pack('side'=>'right', 'fill'=>'y')
   @htree.yscrollcommand proc{|first,last| _scrollbar.set(first,last)}
   
  self.build_tree
end

#rootObject

def do_editor_event(_event)

 case _event.signature 
 		when EditorContract::FILE_AFTER_OPEN
 		  if _event.context.file
		      self.add2history(_event.context.file)
		      add_to_tree(_event.context.file)
 			  end
 		when EditorContract::FILE_AFTER_CLOSE
 		  if _event.context.file 
 		end
 end

end



145
146
147
148
149
150
151
152
153
# File 'ext/ae-file-history/ae-file-history.rb', line 145

def root
  if !defined?(@root)
    @root = TreeNode.new(nil, 'KRoot'){|_node|
      _node.rif= 'root'
      _node.label=''
    }
  end
  return @root
end