glimmer-cs-gladiator 0.1.2 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,18 @@
1
+ begin
2
+ require 'puts_debuggerer' if ENV['puts_debuggerer'].to_s.downcase == 'true'
3
+ rescue LoadError
4
+ # no op
5
+ end
6
+ require 'fileutils'
7
+ require 'os'
8
+
1
9
  require 'models/glimmer/gladiator/dir'
2
10
  require 'models/glimmer/gladiator/file'
3
11
 
12
+ require 'views/glimmer/gladiator/text_editor'
13
+
14
+ java_import 'org.eclipse.swt.custom.TreeEditor'
15
+
4
16
  Clipboard.implementation = Clipboard::Java
5
17
  Clipboard.copy(Clipboard.paste) # pre-initialize library to avoid slowdown during use
6
18
 
@@ -8,6 +20,8 @@ module Glimmer
8
20
  # Gladiator (Glimmer Editor)
9
21
  class Gladiator
10
22
  include Glimmer::UI::CustomShell
23
+
24
+ COMMAND_KEY = OS.mac? ? :command : :ctrl
11
25
 
12
26
  ## Add options like the following to configure CustomShell by outside consumers
13
27
  #
@@ -20,56 +34,147 @@ module Glimmer
20
34
  #
21
35
  before_body {
22
36
  Display.setAppName('Gladiator')
23
- local_dir = ENV['LOCAL_DIR'] || '.'
24
- @config_file_path = ::File.join(local_dir, '.gladiator')
25
- @config = {}
26
- Gladiator::Dir.local_dir.all_children # pre-caches children
27
- load_config
28
37
  @display = display {
29
38
  on_event_keydown { |key_event|
30
- if Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == 'f'
31
- if @text.swt_widget.getSelectionText && @text.swt_widget.getSelectionText.size > 0
32
- @find_text.swt_widget.setText @text.swt_widget.getSelectionText
39
+ if key_event.stateMask == swt(COMMAND_KEY) && extract_char(key_event) == 'f'
40
+ if @text_editor.text_widget.getSelectionText && @text_editor.text_widget.getSelectionText.size > 0
41
+ @find_text.swt_widget.setText @text_editor.text_widget.getSelectionText
33
42
  end
34
43
  @find_text.swt_widget.selectAll
35
44
  @find_text.swt_widget.setFocus
36
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command, :shift) && key_event.character.chr.downcase == 'c'
45
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY, :shift) && extract_char(key_event) == 'c'
37
46
  Clipboard.copy(Gladiator::Dir.local_dir.selected_child.path)
38
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command, :shift) && key_event.character.chr.downcase == 'g'
47
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY, :shift) && extract_char(key_event) == 'g'
39
48
  Gladiator::Dir.local_dir.selected_child.find_previous
40
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == 'g'
49
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY, :shift) && extract_char(key_event) == 'w'
50
+ @tab_folder.swt_widget.getItems.each do |tab_item|
51
+ Dir.local_dir.selected_child_path_history.delete(tab_item.getData('file_path'))
52
+ tab_item.getData('proxy').dispose
53
+ end
54
+ @tab_item = @text_editor = Dir.local_dir.selected_child = nil
55
+ @filter_text.swt_widget.selectAll
56
+ @filter_text.swt_widget.setFocus
57
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && key_event.character == 8721
58
+ other_tab_items.each do |tab_item|
59
+ Dir.local_dir.selected_child_path_history.delete(tab_item.getData('file_path'))
60
+ tab_item.getData('proxy').dispose
61
+ end
62
+ elsif key_event.stateMask == swt(COMMAND_KEY) && extract_char(key_event) == 'w'
63
+ if selected_tab_item
64
+ Dir.local_dir.selected_child_path_history.delete(Dir.local_dir.selected_child.path)
65
+ selected_tab_item.getData('proxy').dispose
66
+ if selected_tab_item.nil?
67
+ @tab_item = @text_editor = Dir.local_dir.selected_child = nil
68
+ @filter_text.swt_widget.selectAll
69
+ @filter_text.swt_widget.setFocus
70
+ else
71
+ @text_editor.text_widget.setFocus
72
+ end
73
+ end
74
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY, :shift) && extract_char(key_event) == ']'
75
+ @tab_folder.swt_widget.setSelection([@tab_folder.swt_widget.getSelectionIndex() + 1, @tab_folder.swt_widget.getItemCount - 1].min) if @tab_folder.swt_widget.getItemCount > 0
76
+ @text_editor.text_widget.setFocus
77
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY, :shift) && extract_char(key_event) == '['
78
+ @tab_folder.swt_widget.setSelection([@tab_folder.swt_widget.getSelectionIndex() - 1, 0].max) if @tab_folder.swt_widget.getItemCount > 0
79
+ @text_editor.text_widget.setFocus
80
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == '1'
81
+ @tab_folder.swt_widget.setSelection(0) if @tab_folder.swt_widget.getItemCount >= 1
82
+ @text_editor.text_widget.setFocus
83
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == '2'
84
+ @tab_folder.swt_widget.setSelection(1) if @tab_folder.swt_widget.getItemCount >= 2
85
+ @text_editor.text_widget.setFocus
86
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == '3'
87
+ @tab_folder.swt_widget.setSelection(2) if @tab_folder.swt_widget.getItemCount >= 3
88
+ @text_editor.text_widget.setFocus
89
+ @tab_folder.swt_widget.setSelection(3) if @tab_folder.swt_widget.getItemCount >= 4
90
+ @text_editor.text_widget.setFocus
91
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == '5'
92
+ @tab_folder.swt_widget.setSelection(4) if @tab_folder.swt_widget.getItemCount >= 5
93
+ @text_editor.text_widget.setFocus
94
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == '4'
95
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == '6'
96
+ @tab_folder.swt_widget.setSelection(5) if @tab_folder.swt_widget.getItemCount >= 6
97
+ @text_editor.text_widget.setFocus
98
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == '7'
99
+ @tab_folder.swt_widget.setSelection(6) if @tab_folder.swt_widget.getItemCount >= 7
100
+ @text_editor.text_widget.setFocus
101
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == '8'
102
+ @tab_folder.swt_widget.setSelection(7) if @tab_folder.swt_widget.getItemCount >= 8
103
+ @text_editor.text_widget.setFocus
104
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == '9'
105
+ @tab_folder.swt_widget.setSelection(@tab_folder.swt_widget.getItemCount - 1) if @tab_folder.swt_widget.getItemCount > 0
106
+ @text_editor.text_widget.setFocus
107
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == 'g'
41
108
  Gladiator::Dir.local_dir.selected_child.find_next
42
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == 'l'
109
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == 'l'
43
110
  @line_number_text.swt_widget.selectAll
44
111
  @line_number_text.swt_widget.setFocus
45
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == 'r'
112
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == 'r'
46
113
  @filter_text.swt_widget.selectAll
47
114
  @filter_text.swt_widget.setFocus
48
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == 't'
115
+ elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, COMMAND_KEY) && extract_char(key_event) == 't'
116
+ select_tree_item unless @rename_in_progress
49
117
  @tree.swt_widget.setFocus
118
+ elsif key_event.keyCode == swt(:esc)
119
+ if @text_editor
120
+ Dir.local_dir.selected_child_path = @text_editor.file.path
121
+ @text_editor.text_widget.setFocus
122
+ end
50
123
  end
51
124
  }
52
125
  }
126
+
127
+ local_dir = ENV['LOCAL_DIR'] || '.'
128
+ @config_file_path = ::File.join(local_dir, '.gladiator')
129
+ @config = {}
130
+ Gladiator::Dir.local_dir.all_children # pre-caches children
53
131
  }
54
132
 
55
133
  ## Uncomment after_body block to setup observers for widgets in body
56
134
  #
57
135
  after_body {
58
- observe(Gladiator::Dir.local_dir, 'selected_child') do
59
- @field_container.swt_widget.pack
136
+ observe(Gladiator::Dir.local_dir, 'children') do
137
+ select_tree_item unless @rename_in_progress
60
138
  end
61
- observe(Gladiator::Dir.local_dir, 'selected_child.line_numbers_content') do
62
- if @last_line_numbers_content != Gladiator::Dir.local_dir.selected_child.line_numbers_content
139
+ observe(Gladiator::Dir.local_dir, 'selected_child') do
140
+ select_tree_item unless @rename_in_progress
141
+ selected_file = Gladiator::Dir.local_dir.selected_child
142
+ found_tab_item = selected_tab_item
143
+ if found_tab_item
144
+ @tab_folder.swt_widget.setSelection(found_tab_item)
145
+ @tab_item = found_tab_item.getData('tab_item')
146
+ @text_editor = found_tab_item.getData('text_editor')
147
+ elsif selected_file
148
+ @tab_folder.content {
149
+ @tab_item = tab_item { |the_tab_item|
150
+ text selected_file.name
151
+ fill_layout :horizontal
152
+ @text_editor = the_text_editor = text_editor(file: selected_file)
153
+ on_event_show {
154
+ Gladiator::Dir.local_dir.selected_child = selected_file
155
+ @tab_item = the_tab_item
156
+ @text_editor = the_text_editor
157
+ }
158
+ }
159
+ @tab_item.swt_tab_item.setData('file_path', selected_file.path)
160
+ @tab_item.swt_tab_item.setData('tab_item', @tab_item)
161
+ @tab_item.swt_tab_item.setData('text_editor', @text_editor)
162
+ @tab_item.swt_tab_item.setData('proxy', @tab_item)
163
+ }
164
+ @tab_folder.swt_widget.setSelection(@tab_item.swt_tab_item)
63
165
  body_root.pack_same_size
64
- @last_line_numbers_content = Gladiator::Dir.local_dir.selected_child.line_numbers_content
65
166
  end
66
167
  end
168
+ observe(Gladiator::Dir.local_dir, 'selected_child') do
169
+ save_config
170
+ end
67
171
  observe(Gladiator::Dir.local_dir, 'selected_child.caret_position') do
68
172
  save_config
69
173
  end
70
174
  observe(Gladiator::Dir.local_dir, 'selected_child.top_index') do
71
175
  save_config
72
- end
176
+ end
177
+ load_config
73
178
  }
74
179
 
75
180
  ## Add widget content inside custom shell body
@@ -87,6 +192,15 @@ module Glimmer
87
192
  on_widget_disposed {
88
193
  Gladiator::Dir.local_dir.selected_child&.write_dirty_content
89
194
  }
195
+ on_control_resized {
196
+ save_config
197
+ }
198
+ on_control_moved {
199
+ save_config
200
+ }
201
+ on_shell_deactivated {
202
+ @text_editor&.file&.write_dirty_content
203
+ }
90
204
  composite {
91
205
  grid_layout 1, false
92
206
  layout_data(:fill, :fill, false, true) {
@@ -95,16 +209,13 @@ module Glimmer
95
209
  @filter_text = text {
96
210
  layout_data :fill, :center, true, false
97
211
  text bind(Gladiator::Dir.local_dir, 'filter')
98
- on_key_pressed { |key_event|
212
+ on_key_pressed { |key_event|
99
213
  if key_event.keyCode == swt(:tab) ||
100
214
  key_event.keyCode == swt(:cr) ||
101
- key_event.keyCode == swt(:lf) ||
102
215
  key_event.keyCode == swt(:arrow_up) ||
103
216
  key_event.keyCode == swt(:arrow_down)
104
217
  @list.swt_widget.select(0) if @list.swt_widget.getSelectionIndex() == -1
105
218
  @list.swt_widget.setFocus
106
- elsif key_event.keyCode == swt(:esc)
107
- @text.swt_widget.setFocus
108
219
  end
109
220
  }
110
221
  }
@@ -113,16 +224,17 @@ module Glimmer
113
224
  @list = list(:border, :h_scroll, :v_scroll) {
114
225
  layout_data(:fill, :fill, true, true) {
115
226
  #exclude bind(Gladiator::Dir.local_dir, :filter) {|f| !f}
227
+ minimum_height 400
116
228
  }
117
229
  #visible bind(Gladiator::Dir, 'local_dir.filter') {|f| !!f}
118
230
  selection bind(Gladiator::Dir.local_dir, :filtered_path)
119
- on_widget_selected {
231
+ on_mouse_up {
120
232
  Gladiator::Dir.local_dir.selected_child_path = @list.swt_widget.getSelection.first
121
233
  }
122
234
  on_key_pressed { |key_event|
123
- if Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :cr) || Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :lf)
235
+ if Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :cr)
124
236
  Gladiator::Dir.local_dir.selected_child_path = @list.swt_widget.getSelection.first
125
- @text.swt_widget.setFocus
237
+ @text_editor.text_widget.setFocus
126
238
  end
127
239
  }
128
240
  }
@@ -131,29 +243,80 @@ module Glimmer
131
243
  #exclude bind(Gladiator::Dir.local_dir, :filter) {|f| !!f}
132
244
  }
133
245
  #visible bind(Gladiator::Dir, 'local_dir.filter') {|f| !f}
134
- items bind(Gladiator::Dir, :local_dir), tree_properties(children: :children, text: :display_path)
135
- on_widget_selected {
136
- Gladiator::Dir.local_dir.selected_child_path = @tree.swt_widget.getSelection.first.getText
246
+ items bind(Gladiator::Dir, :local_dir), tree_properties(children: :children, text: :name)
247
+ menu {
248
+ @open_menu_item = menu_item {
249
+ text 'Open'
250
+ on_widget_selected {
251
+ Gladiator::Dir.local_dir.selected_child_path = extract_tree_item_path(@tree.swt_widget.getSelection.first)
252
+ }
253
+ }
254
+ menu_item(:separator)
255
+ menu_item {
256
+ text 'Delete'
257
+ on_widget_selected {
258
+ tree_item = @tree.swt_widget.getSelection.first
259
+ delete_tree_item(tree_item)
260
+ }
261
+ }
262
+ menu_item {
263
+ text 'Refresh'
264
+ on_widget_selected {
265
+ Gladiator::Dir.local_dir.refresh
266
+ }
267
+ }
268
+ menu_item {
269
+ text 'Rename'
270
+ on_widget_selected {
271
+ rename_selected_tree_item
272
+ }
273
+ }
274
+ menu_item {
275
+ text 'New Directory'
276
+ on_widget_selected {
277
+ add_new_directory_to_selected_tree_item
278
+ }
279
+ }
280
+ menu_item {
281
+ text 'New File'
282
+ on_widget_selected {
283
+ add_new_file_to_selected_tree_item
284
+ }
285
+ }
286
+ }
287
+ on_event_menudetect { |event|
288
+ path = extract_tree_item_path(@tree.swt_widget.getSelection.first)
289
+ @open_menu_item.swt_widget.setEnabled(!::Dir.exist?(path)) if path
290
+ }
291
+ on_mouse_up {
292
+ Gladiator::Dir.local_dir.selected_child_path = extract_tree_item_path(@tree.swt_widget.getSelection&.first)
293
+ @text_editor&.text_widget&.setFocus
137
294
  }
138
295
  on_key_pressed { |key_event|
139
- if Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :cr) || Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :lf)
140
- Gladiator::Dir.local_dir.selected_child_path = @tree.swt_widget.getSelection&.first&.getText
141
- @text.swt_widget.setFocus
296
+ if Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :cr)
297
+ Gladiator::Dir.local_dir.selected_child_path = extract_tree_item_path(@tree.swt_widget.getSelection&.first)
298
+ @text_editor&.text_widget&.setFocus
142
299
  end
143
300
  }
144
301
  on_paint_control {
145
302
  root_item = @tree.swt_widget.getItems.first
146
303
  if root_item && !root_item.getExpanded
147
- root_item.setExpanded true
304
+ root_item.setExpanded(true)
148
305
  end
149
306
  }
150
307
  }
151
308
  }
309
+
310
+ @tree_editor = TreeEditor.new(@tree.swt_widget);
311
+ @tree_editor.horizontalAlignment = swt(:left);
312
+ @tree_editor.grabHorizontal = true;
313
+ @tree_editor.minimumHeight = 20;
314
+
152
315
  }
153
- composite {
316
+ @editor_container = composite {
154
317
  grid_layout 1, false
155
318
  layout_data :fill, :fill, true, true
156
- @field_container = composite {
319
+ composite {
157
320
  grid_layout 2, false
158
321
  @file_path_label = styled_text(:none) {
159
322
  layout_data(:fill, :fill, true, false) {
@@ -175,28 +338,29 @@ module Glimmer
175
338
  }
176
339
  @line_number_text = text {
177
340
  layout_data(:fill, :fill, true, false) {
178
- minimum_width 300
341
+ minimum_width 400
179
342
  }
180
343
  text bind(Gladiator::Dir.local_dir, 'selected_child.line_number', on_read: :to_s, on_write: :to_i)
181
344
  on_key_pressed { |key_event|
182
345
  if key_event.keyCode == swt(:cr)
183
- @text.swt_widget.setFocus
346
+ @text_editor.text_widget.setFocus
184
347
  end
185
348
  }
349
+ on_verify_text { |event|
350
+ event.doit = !event.text.match(/^\d*$/).to_a.empty?
351
+ }
186
352
  }
187
353
  label {
188
354
  text 'Find:'
189
355
  }
190
356
  @find_text = text {
191
357
  layout_data(:fill, :fill, true, false) {
192
- minimum_width 300
358
+ minimum_width 400
193
359
  }
194
360
  text bind(Gladiator::Dir.local_dir, 'selected_child.find_text')
195
361
  on_key_pressed { |key_event|
196
362
  if key_event.keyCode == swt(:cr)
197
363
  Gladiator::Dir.local_dir.selected_child.find_next
198
- elsif key_event.keyCode == swt(:esc)
199
- @text.swt_widget.setFocus
200
364
  end
201
365
  }
202
366
  }
@@ -214,88 +378,12 @@ module Glimmer
214
378
  on_key_pressed { |key_event|
215
379
  if key_event.keyCode == swt(:cr)
216
380
  Gladiator::Dir.local_dir.selected_child.replace_next!
217
- elsif key_event.keyCode == swt(:esc)
218
- @text.swt_widget.setFocus
219
381
  end
220
382
  }
221
383
  }
222
384
  }
223
- composite {
224
- layout_data :fill, :fill, true, true
225
- grid_layout 2, false
226
- @line_numbers_text = text(:multi) {
227
- layout_data(:right, :fill, false, true)
228
- font name: 'Consolas', height: 15
229
- background color(:widget_background)
230
- foreground rgb(75, 75, 75)
231
- text bind(Gladiator::Dir.local_dir, 'selected_child.line_numbers_content')
232
- top_index bind(Gladiator::Dir.local_dir, 'selected_child.top_index')
233
- on_focus_gained {
234
- @text&.swt_widget.setFocus
235
- }
236
- on_key_pressed {
237
- @text&.swt_widget.setFocus
238
- }
239
- on_mouse_up {
240
- @text&.swt_widget.setFocus
241
- }
242
- }
243
- @text = text(:multi, :border, :h_scroll, :v_scroll) {
244
- layout_data :fill, :fill, true, true
245
- font name: 'Consolas', height: 15
246
- foreground rgb(75, 75, 75)
247
- text bind(Gladiator::Dir.local_dir, 'selected_child.dirty_content')
248
- focus true
249
- caret_position bind(Gladiator::Dir.local_dir, 'selected_child.caret_position')
250
- selection_count bind(Gladiator::Dir.local_dir, 'selected_child.selection_count')
251
- top_index bind(Gladiator::Dir.local_dir, 'selected_child.top_index')
252
- on_focus_lost {
253
- Gladiator::Dir.local_dir.selected_child&.write_dirty_content
254
- }
255
- on_key_pressed { |key_event|
256
- if Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == '/'
257
- Gladiator::Dir.local_dir.selected_child.comment_line!
258
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == 'k'
259
- Gladiator::Dir.local_dir.selected_child.kill_line!
260
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == 'd'
261
- Gladiator::Dir.local_dir.selected_child.duplicate_line!
262
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == '['
263
- Gladiator::Dir.local_dir.selected_child.outdent!
264
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == ']'
265
- Gladiator::Dir.local_dir.selected_child.indent!
266
- elsif key_event.keyCode == swt(:page_up)
267
- Gladiator::Dir.local_dir.selected_child.page_up
268
- key_event.doit = false
269
- elsif key_event.keyCode == swt(:page_down)
270
- Gladiator::Dir.local_dir.selected_child.page_down
271
- key_event.doit = false
272
- elsif key_event.keyCode == swt(:home)
273
- Gladiator::Dir.local_dir.selected_child.home
274
- key_event.doit = false
275
- elsif key_event.keyCode == swt(:end)
276
- Gladiator::Dir.local_dir.selected_child.end
277
- key_event.doit = false
278
- elsif key_event.stateMask == swt(:command) && key_event.keyCode == swt(:arrow_up)
279
- Gladiator::Dir.local_dir.selected_child.move_up!
280
- key_event.doit = false
281
- elsif key_event.stateMask == swt(:command) && key_event.keyCode == swt(:arrow_down)
282
- Gladiator::Dir.local_dir.selected_child.move_down!
283
- key_event.doit = false
284
- end
285
- }
286
- on_verify_text { |verify_event|
287
- key_code = verify_event.keyCode
288
- case key_code
289
- when swt(:tab)
290
- if Gladiator::Dir.local_dir.selected_child.selection_count.to_i > 0
291
- Gladiator::Dir.local_dir.selected_child.indent!
292
- verify_event.doit = false
293
- else
294
- verify_event.text = ' '
295
- end
296
- end
297
- }
298
- }
385
+ @tab_folder = tab_folder {
386
+ layout_data(:fill, :fill, true, true)
299
387
  }
300
388
  }
301
389
  }
@@ -306,24 +394,137 @@ module Glimmer
306
394
  config_yaml = ::File.read(@config_file_path)
307
395
  return if config_yaml.to_s.strip.empty?
308
396
  @config = YAML.load(config_yaml)
397
+ @config[:open_file_paths].to_a.each do |file_path|
398
+ Gladiator::Dir.local_dir.selected_child_path = file_path
399
+ end
309
400
  Gladiator::Dir.local_dir.selected_child_path = @config[:selected_child_path] if @config[:selected_child_path]
310
- Gladiator::Dir.local_dir.selected_child&.caret_position = Gladiator::Dir.local_dir.selected_child&.caret_position_for_caret_position_start_of_line(@config[:caret_position]) if @config[:caret_position]
311
- Gladiator::Dir.local_dir.selected_child&.top_index = @config[:top_index] if @config[:top_index]
401
+ Gladiator::Dir.local_dir.selected_child&.caret_position = Gladiator::Dir.local_dir.selected_child&.caret_position_for_caret_position_start_of_line(@config[:caret_position].to_i) if @config[:caret_position]
402
+ Gladiator::Dir.local_dir.selected_child&.top_index = @config[:top_index].to_i if @config[:top_index]
403
+ body_root.on_event_show {
404
+ swt_widget.setSize(@config[:shell_width], @config[:shell_height]) if @config[:shell_width] && @config[:shell_height]
405
+ swt_widget.setLocation(@config[:shell_x], @config[:shell_y]) if @config[:shell_x] && @config[:shell_y]
406
+ @loaded_config = true
407
+ }
408
+ else
409
+ @loaded_config = true
312
410
  end
313
411
  end
314
412
 
315
413
  def save_config
414
+ return unless @loaded_config
316
415
  child = Gladiator::Dir.local_dir.selected_child
317
416
  return if child.nil?
318
417
  @config = {
319
418
  selected_child_path: child.path,
320
419
  caret_position: child.caret_position,
321
420
  top_index: child.top_index,
421
+ shell_width: swt_widget&.getBounds&.width,
422
+ shell_height: swt_widget&.getBounds&.height,
423
+ shell_x: swt_widget&.getBounds&.x,
424
+ shell_y: swt_widget&.getBounds&.y,
425
+ open_file_paths: Dir.local_dir.selected_child_path_history,
322
426
  }
323
427
  config_yaml = YAML.dump(@config)
324
428
  ::File.write(@config_file_path, config_yaml) unless config_yaml.to_s.empty?
325
429
  rescue => e
326
430
  puts e.full_message
327
431
  end
432
+
433
+ def selected_tab_item
434
+ @tab_folder.swt_widget.getItems.detect { |ti| ti.getData('file_path') == Gladiator::Dir.local_dir.selected_child&.path }
435
+ end
436
+
437
+ def other_tab_items
438
+ @tab_folder.swt_widget.getItems.reject { |ti| ti.getData('file_path') == Gladiator::Dir.local_dir.selected_child&.path }
439
+ end
440
+
441
+ def extract_tree_item_path(tree_item)
442
+ return if tree_item.nil?
443
+ if tree_item.getParentItem
444
+ ::File.join(extract_tree_item_path(tree_item.getParentItem), tree_item.getText)
445
+ else
446
+ Dir.local_dir.path
447
+ end
448
+ end
449
+
450
+ def select_tree_item
451
+ return unless Gladiator::Dir.local_dir.selected_child&.name
452
+ tree_items_to_select = @tree.depth_first_search { |ti| ti.getData.path == Gladiator::Dir.local_dir.selected_child.path }
453
+ @tree.swt_widget.setSelection(tree_items_to_select)
454
+ end
455
+
456
+ def delete_tree_item(tree_item)
457
+ file = tree_item.getData
458
+ parent_path = ::File.dirname(file.path)
459
+ file.delete!
460
+ Dir.local_dir.refresh(async: false)
461
+ parent_tree_item = @tree.depth_first_search {|ti| ti.getData.path == parent_path}.first
462
+ @tree.swt_widget.showItem(parent_tree_item)
463
+ parent_tree_item.setExpanded(true)
464
+ # TODO close text editor tab
465
+ # if file.is_a?(::File)
466
+ # close tab
467
+ # end
468
+ end
469
+
470
+ def rename_selected_tree_item
471
+ Dir.local_dir.pause_refresh
472
+ tree_item = @tree.swt_widget.getSelection.first
473
+ rename_tree_item(tree_item)
474
+ end
475
+
476
+ def add_new_directory_to_selected_tree_item
477
+ Dir.local_dir.pause_refresh
478
+ tree_item = @tree.swt_widget.getSelection.first
479
+ directory_path = extract_tree_item_path(tree_item)
480
+ if !::Dir.exist?(directory_path)
481
+ tree_item = tree_item.getParentItem
482
+ directory_path = ::File.dirname(directory_path)
483
+ end
484
+ new_directory_path = ::File.expand_path(::File.join(directory_path, 'new_directory'))
485
+ FileUtils.mkdir_p(new_directory_path)
486
+ Dir.local_dir.refresh(async: false, force: true)
487
+ new_tree_item = @tree.depth_first_search {|ti| ti.getData.path == new_directory_path}.first
488
+ @tree.swt_widget.showItem(new_tree_item)
489
+ rename_tree_item(new_tree_item, true)
490
+ end
491
+
492
+ def add_new_file_to_selected_tree_item
493
+ Dir.local_dir.pause_refresh
494
+ tree_item = @tree.swt_widget.getSelection.first
495
+ directory_path = extract_tree_item_path(tree_item)
496
+ if !::Dir.exist?(directory_path)
497
+ tree_item = tree_item.getParentItem
498
+ directory_path = ::File.dirname(directory_path)
499
+ end
500
+ new_file_path = ::File.expand_path(::File.join(directory_path, 'new_file'))
501
+ FileUtils.touch(new_file_path)
502
+ Dir.local_dir.refresh(async: false, force: true)
503
+ new_tree_item = @tree.depth_first_search {|ti| ti.getData.path == new_file_path}.first
504
+ @tree.swt_widget.showItem(new_tree_item)
505
+ rename_tree_item(new_tree_item, true)
506
+ end
507
+
508
+ def rename_tree_item(tree_item, open_afterwards = false)
509
+ @tree.edit_tree_item(
510
+ tree_item,
511
+ after_write: -> (edited_tree_item) {
512
+ file = edited_tree_item.getData
513
+ file_path = file.path
514
+ # TODO rename file in tab title
515
+ Dir.local_dir.selected_child_path = file_path if open_afterwards
516
+ Dir.local_dir.resume_refresh
517
+ },
518
+ after_cancel: -> {
519
+ Dir.local_dir.resume_refresh
520
+ }
521
+ )
522
+ end
523
+
524
+ def extract_char(event)
525
+ event.keyCode.chr
526
+ rescue => e
527
+ nil
528
+ end
328
529
  end
329
- end
530
+ end