glimmer-cs-gladiator 0.1.1 → 0.1.6

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,53 +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.line_numbers_content') do
59
- if @last_line_numbers_content != Gladiator::Dir.local_dir.selected_child.line_numbers_content
136
+ observe(Gladiator::Dir.local_dir, 'children') do
137
+ select_tree_item unless @rename_in_progress
138
+ end
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)
60
165
  body_root.pack_same_size
61
- @last_line_numbers_content = Gladiator::Dir.local_dir.selected_child.line_numbers_content
62
166
  end
63
167
  end
168
+ observe(Gladiator::Dir.local_dir, 'selected_child') do
169
+ save_config
170
+ end
64
171
  observe(Gladiator::Dir.local_dir, 'selected_child.caret_position') do
65
172
  save_config
66
173
  end
67
174
  observe(Gladiator::Dir.local_dir, 'selected_child.top_index') do
68
175
  save_config
69
- end
176
+ end
177
+ load_config
70
178
  }
71
179
 
72
180
  ## Add widget content inside custom shell body
@@ -84,6 +192,15 @@ module Glimmer
84
192
  on_widget_disposed {
85
193
  Gladiator::Dir.local_dir.selected_child&.write_dirty_content
86
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
+ }
87
204
  composite {
88
205
  grid_layout 1, false
89
206
  layout_data(:fill, :fill, false, true) {
@@ -92,16 +209,13 @@ module Glimmer
92
209
  @filter_text = text {
93
210
  layout_data :fill, :center, true, false
94
211
  text bind(Gladiator::Dir.local_dir, 'filter')
95
- on_key_pressed { |key_event|
212
+ on_key_pressed { |key_event|
96
213
  if key_event.keyCode == swt(:tab) ||
97
214
  key_event.keyCode == swt(:cr) ||
98
- key_event.keyCode == swt(:lf) ||
99
215
  key_event.keyCode == swt(:arrow_up) ||
100
216
  key_event.keyCode == swt(:arrow_down)
101
217
  @list.swt_widget.select(0) if @list.swt_widget.getSelectionIndex() == -1
102
218
  @list.swt_widget.setFocus
103
- elsif key_event.keyCode == swt(:esc)
104
- @text.swt_widget.setFocus
105
219
  end
106
220
  }
107
221
  }
@@ -110,16 +224,17 @@ module Glimmer
110
224
  @list = list(:border, :h_scroll, :v_scroll) {
111
225
  layout_data(:fill, :fill, true, true) {
112
226
  #exclude bind(Gladiator::Dir.local_dir, :filter) {|f| !f}
227
+ minimum_height 400
113
228
  }
114
229
  #visible bind(Gladiator::Dir, 'local_dir.filter') {|f| !!f}
115
230
  selection bind(Gladiator::Dir.local_dir, :filtered_path)
116
- on_widget_selected {
231
+ on_mouse_up {
117
232
  Gladiator::Dir.local_dir.selected_child_path = @list.swt_widget.getSelection.first
118
233
  }
119
234
  on_key_pressed { |key_event|
120
- 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)
121
236
  Gladiator::Dir.local_dir.selected_child_path = @list.swt_widget.getSelection.first
122
- @text.swt_widget.setFocus
237
+ @text_editor.text_widget.setFocus
123
238
  end
124
239
  }
125
240
  }
@@ -128,26 +243,77 @@ module Glimmer
128
243
  #exclude bind(Gladiator::Dir.local_dir, :filter) {|f| !!f}
129
244
  }
130
245
  #visible bind(Gladiator::Dir, 'local_dir.filter') {|f| !f}
131
- items bind(Gladiator::Dir, :local_dir), tree_properties(children: :children, text: :display_path)
132
- on_widget_selected {
133
- 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
134
294
  }
135
295
  on_key_pressed { |key_event|
136
- if Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :cr) || Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :lf)
137
- Gladiator::Dir.local_dir.selected_child_path = @tree.swt_widget.getSelection.first.getText
138
- @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
139
299
  end
140
300
  }
141
301
  on_paint_control {
142
302
  root_item = @tree.swt_widget.getItems.first
143
303
  if root_item && !root_item.getExpanded
144
- root_item.setExpanded true
304
+ root_item.setExpanded(true)
145
305
  end
146
306
  }
147
307
  }
148
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
+
149
315
  }
150
- composite {
316
+ @editor_container = composite {
151
317
  grid_layout 1, false
152
318
  layout_data :fill, :fill, true, true
153
319
  composite {
@@ -172,28 +338,29 @@ module Glimmer
172
338
  }
173
339
  @line_number_text = text {
174
340
  layout_data(:fill, :fill, true, false) {
175
- minimum_width 300
341
+ minimum_width 400
176
342
  }
177
343
  text bind(Gladiator::Dir.local_dir, 'selected_child.line_number', on_read: :to_s, on_write: :to_i)
178
- on_key_pressed { |key_event|
344
+ on_key_pressed { |key_event|
179
345
  if key_event.keyCode == swt(:cr)
180
- @text.swt_widget.setFocus
346
+ @text_editor.text_widget.setFocus
181
347
  end
182
348
  }
349
+ on_verify_text { |event|
350
+ event.doit = !event.text.match(/^\d*$/).to_a.empty?
351
+ }
183
352
  }
184
353
  label {
185
354
  text 'Find:'
186
355
  }
187
356
  @find_text = text {
188
357
  layout_data(:fill, :fill, true, false) {
189
- minimum_width 200
358
+ minimum_width 400
190
359
  }
191
360
  text bind(Gladiator::Dir.local_dir, 'selected_child.find_text')
192
- on_key_pressed { |key_event|
361
+ on_key_pressed { |key_event|
193
362
  if key_event.keyCode == swt(:cr)
194
363
  Gladiator::Dir.local_dir.selected_child.find_next
195
- elsif key_event.keyCode == swt(:esc)
196
- @text.swt_widget.setFocus
197
364
  end
198
365
  }
199
366
  }
@@ -202,97 +369,21 @@ module Glimmer
202
369
  }
203
370
  @replace_text = text {
204
371
  layout_data(:fill, :fill, true, false) {
205
- minimum_width 200
372
+ minimum_width 300
206
373
  }
207
374
  text bind(Gladiator::Dir.local_dir, 'selected_child.replace_text')
208
375
  on_focus_gained {
209
376
  Gladiator::Dir.local_dir.selected_child.ensure_find_next
210
377
  }
211
- on_key_pressed { |key_event|
378
+ on_key_pressed { |key_event|
212
379
  if key_event.keyCode == swt(:cr)
213
380
  Gladiator::Dir.local_dir.selected_child.replace_next!
214
- elsif key_event.keyCode == swt(:esc)
215
- @text.swt_widget.setFocus
216
381
  end
217
382
  }
218
383
  }
219
384
  }
220
- composite {
221
- layout_data :fill, :fill, true, true
222
- grid_layout 2, false
223
- @line_numbers_text = text(:multi) {
224
- layout_data(:right, :fill, false, true)
225
- font name: 'Consolas', height: 15
226
- background color(:widget_background)
227
- foreground rgb(75, 75, 75)
228
- text bind(Gladiator::Dir.local_dir, 'selected_child.line_numbers_content')
229
- top_index bind(Gladiator::Dir.local_dir, 'selected_child.top_index')
230
- on_focus_gained {
231
- @text&.swt_widget.setFocus
232
- }
233
- on_key_pressed {
234
- @text&.swt_widget.setFocus
235
- }
236
- on_mouse_up {
237
- @text&.swt_widget.setFocus
238
- }
239
- }
240
- @text = text(:multi, :border, :h_scroll, :v_scroll) {
241
- layout_data :fill, :fill, true, true
242
- font name: 'Consolas', height: 15
243
- foreground rgb(75, 75, 75)
244
- text bind(Gladiator::Dir.local_dir, 'selected_child.dirty_content')
245
- focus true
246
- caret_position bind(Gladiator::Dir.local_dir, 'selected_child.caret_position')
247
- selection_count bind(Gladiator::Dir.local_dir, 'selected_child.selection_count')
248
- top_index bind(Gladiator::Dir.local_dir, 'selected_child.top_index')
249
- on_focus_lost {
250
- Gladiator::Dir.local_dir.selected_child&.write_dirty_content
251
- }
252
- on_key_pressed { |key_event|
253
- if Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == '/'
254
- Gladiator::Dir.local_dir.selected_child.comment_line!
255
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == 'k'
256
- Gladiator::Dir.local_dir.selected_child.kill_line!
257
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == 'd'
258
- Gladiator::Dir.local_dir.selected_child.duplicate_line!
259
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == '['
260
- Gladiator::Dir.local_dir.selected_child.outdent!
261
- elsif Glimmer::SWT::SWTProxy.include?(key_event.stateMask, :command) && key_event.character.chr.downcase == ']'
262
- Gladiator::Dir.local_dir.selected_child.indent!
263
- elsif key_event.keyCode == swt(:page_up)
264
- Gladiator::Dir.local_dir.selected_child.page_up
265
- key_event.doit = false
266
- elsif key_event.keyCode == swt(:page_down)
267
- Gladiator::Dir.local_dir.selected_child.page_down
268
- key_event.doit = false
269
- elsif key_event.keyCode == swt(:home)
270
- Gladiator::Dir.local_dir.selected_child.home
271
- key_event.doit = false
272
- elsif key_event.keyCode == swt(:end)
273
- Gladiator::Dir.local_dir.selected_child.end
274
- key_event.doit = false
275
- elsif key_event.stateMask == swt(:command) && key_event.keyCode == swt(:arrow_up)
276
- Gladiator::Dir.local_dir.selected_child.move_up!
277
- key_event.doit = false
278
- elsif key_event.stateMask == swt(:command) && key_event.keyCode == swt(:arrow_down)
279
- Gladiator::Dir.local_dir.selected_child.move_down!
280
- key_event.doit = false
281
- end
282
- }
283
- on_verify_text { |verify_event|
284
- key_code = verify_event.keyCode
285
- case key_code
286
- when swt(:tab)
287
- if Gladiator::Dir.local_dir.selected_child.selection_count.to_i > 0
288
- Gladiator::Dir.local_dir.selected_child.indent!
289
- verify_event.doit = false
290
- else
291
- verify_event.text = ' '
292
- end
293
- end
294
- }
295
- }
385
+ @tab_folder = tab_folder {
386
+ layout_data(:fill, :fill, true, true)
296
387
  }
297
388
  }
298
389
  }
@@ -303,24 +394,137 @@ module Glimmer
303
394
  config_yaml = ::File.read(@config_file_path)
304
395
  return if config_yaml.to_s.strip.empty?
305
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
306
400
  Gladiator::Dir.local_dir.selected_child_path = @config[:selected_child_path] if @config[:selected_child_path]
307
- 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]
308
- 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
309
410
  end
310
411
  end
311
412
 
312
413
  def save_config
414
+ return unless @loaded_config
313
415
  child = Gladiator::Dir.local_dir.selected_child
314
416
  return if child.nil?
315
417
  @config = {
316
418
  selected_child_path: child.path,
317
419
  caret_position: child.caret_position,
318
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,
319
426
  }
320
427
  config_yaml = YAML.dump(@config)
321
428
  ::File.write(@config_file_path, config_yaml) unless config_yaml.to_s.empty?
322
429
  rescue => e
323
430
  puts e.full_message
324
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
325
529
  end
326
- end
530
+ end