glimmer-cs-gladiator 0.8.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,67 @@
1
+ module Glimmer
2
+ class Gladiator
3
+ class FileEditMenu
4
+ include Glimmer::UI::CustomWidget
5
+
6
+ # Either receives a gladiator instance for menu bar parent or file instance for text editor parent
7
+ options :gladiator, :file
8
+
9
+ body {
10
+ menu { |menu_proxy|
11
+ text '&Edit' unless menu_proxy.swt_menu_item.nil? # unless used on the text editor directly
12
+
13
+ menu_item {
14
+ text '&Undo'
15
+ # TODO disable if not undoable
16
+
17
+ on_widget_selected {
18
+ Command.undo(current_file)
19
+ }
20
+ }
21
+ menu_item {
22
+ text '&Redo'
23
+ # TODO disable if not redoable
24
+
25
+ on_widget_selected {
26
+ Command.redo(current_file)
27
+ }
28
+ }
29
+ menu_item(:separator)
30
+ menu_item {
31
+ text 'Cu&t'
32
+
33
+ on_widget_selected {
34
+ Command.do(current_file, :cut!)
35
+ }
36
+ }
37
+ menu_item {
38
+ text '&Copy'
39
+
40
+ on_widget_selected {
41
+ current_file.copy
42
+ }
43
+ }
44
+ menu_item {
45
+ text '&Paste'
46
+
47
+ on_widget_selected {
48
+ Command.do(current_file, :paste!)
49
+ }
50
+ }
51
+ menu_item(:separator)
52
+ menu_item {
53
+ text 'Select &All'
54
+
55
+ on_widget_selected {
56
+ current_file.select_all
57
+ }
58
+ }
59
+ }
60
+ }
61
+
62
+ def current_file
63
+ file.nil? ? gladiator.project_dir.selected_child : file
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,244 +1,244 @@
1
- # Copyright (c) 2020-2021 Andy Maleh
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining
4
- # a copy of this software and associated documentation files (the
5
- # "Software"), to deal in the Software without restriction, including
6
- # without limitation the rights to use, copy, modify, merge, publish,
7
- # distribute, sublicense, and/or sell copies of the Software, and to
8
- # permit persons to whom the Software is furnished to do so, subject to
9
- # the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be
12
- # included in all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- module Glimmer
23
- class Gladiator
24
- class FileExplorerTree
25
- include Glimmer::UI::CustomWidget
26
-
27
- options :gladiator, :foreground_color
28
-
29
- body {
30
- tree {
31
- #visible bind(self, 'project_dir.filter') {|f| !f}
32
- items bind(self, :project_dir), tree_properties(children: :children, text: :name)
33
- foreground foreground_color
34
- drag_source(:drop_copy) {
35
- transfer :text
36
- on_drag_set_data { |event|
37
- Gladiator.drag = true
38
- tree = event.widget.getControl
39
- tree_item = tree.getSelection.first
40
- event.data = tree_item.getData.path
41
- }
42
- }
43
-
44
- menu {
45
- @open_menu_item = menu_item {
46
- text 'Open'
47
- on_widget_selected {
48
- project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection.first)
49
- }
50
- }
51
- menu_item(:separator)
52
- menu_item {
53
- text 'Delete'
54
- on_widget_selected {
55
- tree_item = swt_widget.getSelection.first
56
- delete_tree_item(tree_item)
57
- }
58
- }
59
- menu_item {
60
- text 'Refresh'
61
- on_widget_selected {
62
- project_dir.refresh
63
- }
64
- }
65
- menu_item {
66
- text 'Rename'
67
- on_widget_selected {
68
- rename_selected_tree_item
69
- }
70
- }
71
- menu_item {
72
- text 'New Directory'
73
- on_widget_selected {
74
- add_new_directory_to_selected_tree_item
75
- }
76
- }
77
- menu_item {
78
- text 'New File'
79
- on_widget_selected {
80
- add_new_file_to_selected_tree_item
81
- }
82
- }
83
- }
84
-
85
- on_swt_menudetect { |event|
86
- path = extract_tree_item_path(swt_widget.getSelection.first)
87
- @open_menu_item.swt_widget.setEnabled(!::Dir.exist?(path)) if path
88
- }
89
- on_mouse_up {
90
- if Gladiator.drag_and_drop
91
- Gladiator.drag_and_drop = false
92
- else
93
- project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection&.first)
94
- gladiator.current_text_editor&.text_widget&.setFocus
95
- end
96
- }
97
- on_key_pressed { |key_event|
98
- if Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :cr)
99
- project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection&.first)
100
- gladiator.current_text_editor&.text_widget&.setFocus
101
- end
102
- }
103
- on_paint_control {
104
- root_item = swt_widget.getItems.first
105
- if root_item && !root_item.getExpanded
106
- root_item.setExpanded(true)
107
- end
108
- }
109
-
110
- }
111
- }
112
-
113
- def project_dir
114
- gladiator.project_dir
115
- end
116
-
117
- def current_text_editor
118
- gladiator.current_text_editor
119
- end
120
-
121
- def extract_tree_item_path(tree_item)
122
- return if tree_item.nil?
123
- if tree_item.getParentItem
124
- ::File.join(extract_tree_item_path(tree_item.getParentItem), tree_item.getText)
125
- else
126
- project_dir.path
127
- end
128
- end
129
-
130
- def select_tree_item
131
- return unless project_dir.selected_child&.name
132
- tree_items_to_select = body_root.depth_first_search { |ti| ti.getData.path == project_dir.selected_child.path }
133
- swt_widget.setSelection(tree_items_to_select)
134
- end
135
-
136
- def delete_tree_item(tree_item)
137
- return if tree_item.nil?
138
- file = tree_item.getData
139
- parent_path = ::File.dirname(file.path)
140
- if file.is_a?(Gladiator::Dir)
141
- file_paths = file.all_children.select {|f| f.is_a?(Gladiator::File)}.map(&:path)
142
- file.remove_all_observers
143
- else
144
- file_paths = [file.path]
145
- end
146
- file_paths.each do |file_path|
147
- found_tab_item = gladiator.find_tab_item(file_path)
148
- if found_tab_item
149
- project_dir.selected_child_path_history.delete(found_tab_item.getData('file_path'))
150
- found_tab_item.getData('proxy')&.dispose
151
- end
152
- end
153
- file.delete! # TODO consider supporting command undo/redo
154
- project_dir.refresh(async: false)
155
- parent_tree_item = body_root.depth_first_search {|ti| ti.getData.path == parent_path}.first
156
- swt_widget.showItem(parent_tree_item)
157
- parent_tree_item.setExpanded(true)
158
- rescue => e
159
- puts e.full_message
160
- end
161
-
162
- def add_new_directory_to_selected_tree_item
163
- project_dir.pause_refresh
164
- tree_item = swt_widget.getSelection.first
165
- directory_path = extract_tree_item_path(tree_item)
166
- return if directory_path.nil?
167
- if !::Dir.exist?(directory_path)
168
- tree_item = tree_item.getParentItem
169
- directory_path = ::File.dirname(directory_path)
170
- end
171
- new_directory_path = ::File.expand_path(::File.join(directory_path, 'new_directory'))
172
- FileUtils.mkdir_p(new_directory_path)
173
- project_dir.refresh(async: false, force: true)
174
- new_tree_item = body_root.depth_first_search {|ti| ti.getData.path == new_directory_path}.first
175
- swt_widget.showItem(new_tree_item)
176
- rename_tree_item(new_tree_item)
177
- end
178
-
179
- def add_new_file_to_selected_tree_item
180
- project_dir.pause_refresh
181
- tree_item = swt_widget.getSelection.first
182
- directory_path = extract_tree_item_path(tree_item)
183
- if !::Dir.exist?(directory_path)
184
- tree_item = tree_item.getParentItem
185
- directory_path = ::File.dirname(directory_path)
186
- end
187
- new_file_path = ::File.expand_path(::File.join(directory_path, 'new_file'))
188
- FileUtils.touch(new_file_path)
189
- # TODO look into refreshing only the parent directory to avoid slowdown
190
- project_dir.refresh(async: false, force: true)
191
- new_tree_item = body_root.depth_first_search {|ti| ti.getData.path == new_file_path}.first
192
- swt_widget.showItem(new_tree_item)
193
- rename_tree_item(new_tree_item, true)
194
- end
195
-
196
- def rename_selected_tree_item
197
- project_dir.pause_refresh
198
- tree_item = swt_widget.getSelection.first
199
- rename_tree_item(tree_item)
200
- end
201
-
202
- def rename_tree_item(tree_item, new_file = false)
203
- original_file = tree_item.getData
204
- current_file = project_dir.selected_child_path == original_file.path
205
- found_tab_item = gladiator.find_tab_item(original_file.path)
206
- found_text_editor = found_tab_item&.getData('text_editor')
207
- body_root.edit_tree_item(
208
- tree_item,
209
- after_write: -> (edited_tree_item) {
210
- file = edited_tree_item.getData
211
- file_path = file.path
212
- file.name
213
- if ::File.file?(file_path)
214
- if new_file
215
- project_dir.selected_child_path = file_path
216
- else
217
- found_text_editor&.file = file
218
- found_tab_item&.setData('file', file)
219
- found_tab_item&.setData('file_path', file.path)
220
- found_tab_item&.setText(file.name)
221
- body_root.pack_same_size
222
- if current_file
223
- project_dir.selected_child_path = file_path
224
- else
225
- selected_tab_item&.getData('text_editor')&.text_widget&.setFocus
226
- end
227
- async_exec {
228
- swt_widget.showItem(edited_tree_item)
229
- }
230
- end
231
- end
232
- project_dir.resume_refresh
233
- },
234
- after_cancel: -> {
235
- project_dir.resume_refresh
236
- }
237
- )
238
- end
239
-
240
- end
241
-
242
- end
243
-
244
- end
1
+ # Copyright (c) 2020-2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ module Glimmer
23
+ class Gladiator
24
+ class FileExplorerTree
25
+ include Glimmer::UI::CustomWidget
26
+
27
+ options :gladiator, :foreground_color
28
+
29
+ body {
30
+ tree {
31
+ #visible bind(self, 'project_dir.filter') {|f| !f}
32
+ items bind(self, :project_dir), tree_properties(children: :children, text: :name)
33
+ foreground foreground_color
34
+ drag_source(:drop_copy) {
35
+ transfer :text
36
+ on_drag_set_data { |event|
37
+ Gladiator.drag = true
38
+ tree = event.widget.getControl
39
+ tree_item = tree.getSelection.first
40
+ event.data = tree_item.getData.path
41
+ }
42
+ }
43
+
44
+ menu {
45
+ @open_menu_item = menu_item {
46
+ text 'Open'
47
+ on_widget_selected {
48
+ project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection.first)
49
+ }
50
+ }
51
+ menu_item(:separator)
52
+ menu_item {
53
+ text 'Delete'
54
+ on_widget_selected {
55
+ tree_item = swt_widget.getSelection.first
56
+ delete_tree_item(tree_item)
57
+ }
58
+ }
59
+ menu_item {
60
+ text 'Refresh'
61
+ on_widget_selected {
62
+ project_dir.refresh
63
+ }
64
+ }
65
+ menu_item {
66
+ text 'Rename'
67
+ on_widget_selected {
68
+ rename_selected_tree_item
69
+ }
70
+ }
71
+ menu_item {
72
+ text 'New Directory'
73
+ on_widget_selected {
74
+ add_new_directory_to_selected_tree_item
75
+ }
76
+ }
77
+ menu_item {
78
+ text 'New File'
79
+ on_widget_selected {
80
+ add_new_file_to_selected_tree_item
81
+ }
82
+ }
83
+ }
84
+
85
+ on_swt_menudetect { |event|
86
+ path = extract_tree_item_path(swt_widget.getSelection.first)
87
+ @open_menu_item.swt_widget.setEnabled(!::Dir.exist?(path)) if path
88
+ }
89
+ on_mouse_up {
90
+ if Gladiator.drag_and_drop
91
+ Gladiator.drag_and_drop = false
92
+ else
93
+ project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection&.first)
94
+ gladiator.current_text_editor&.text_widget&.setFocus
95
+ end
96
+ }
97
+ on_key_pressed { |key_event|
98
+ if Glimmer::SWT::SWTProxy.include?(key_event.keyCode, :cr)
99
+ project_dir.selected_child_path = extract_tree_item_path(swt_widget.getSelection&.first)
100
+ gladiator.current_text_editor&.text_widget&.setFocus
101
+ end
102
+ }
103
+ on_paint_control {
104
+ root_item = swt_widget.getItems.first
105
+ if root_item && !root_item.getExpanded
106
+ root_item.setExpanded(true)
107
+ end
108
+ }
109
+
110
+ }
111
+ }
112
+
113
+ def project_dir
114
+ gladiator.project_dir
115
+ end
116
+
117
+ def current_text_editor
118
+ gladiator.current_text_editor
119
+ end
120
+
121
+ def extract_tree_item_path(tree_item)
122
+ return if tree_item.nil?
123
+ if tree_item.getParentItem
124
+ ::File.join(extract_tree_item_path(tree_item.getParentItem), tree_item.getText)
125
+ else
126
+ project_dir.path
127
+ end
128
+ end
129
+
130
+ def select_tree_item
131
+ return unless project_dir.selected_child&.name
132
+ tree_items_to_select = body_root.depth_first_search { |ti| ti.getData.path == project_dir.selected_child.path }
133
+ swt_widget.setSelection(tree_items_to_select)
134
+ end
135
+
136
+ def delete_tree_item(tree_item)
137
+ return if tree_item.nil?
138
+ file = tree_item.getData
139
+ parent_path = ::File.dirname(file.path)
140
+ if file.is_a?(Gladiator::Dir)
141
+ file_paths = file.all_children.select {|f| f.is_a?(Gladiator::File)}.map(&:path)
142
+ file.remove_all_observers
143
+ else
144
+ file_paths = [file.path]
145
+ end
146
+ file_paths.each do |file_path|
147
+ found_tab_item = gladiator.find_tab_item(file_path)
148
+ if found_tab_item
149
+ project_dir.selected_child_path_history.delete(found_tab_item.getData('file_path'))
150
+ found_tab_item.getData('proxy')&.dispose
151
+ end
152
+ end
153
+ file.delete! # TODO consider supporting command undo/redo
154
+ project_dir.refresh(async: false)
155
+ parent_tree_item = body_root.depth_first_search {|ti| ti.getData.path == parent_path}.first
156
+ swt_widget.showItem(parent_tree_item)
157
+ parent_tree_item.setExpanded(true)
158
+ rescue => e
159
+ puts e.full_message
160
+ end
161
+
162
+ def add_new_directory_to_selected_tree_item
163
+ project_dir.pause_refresh
164
+ tree_item = swt_widget.getSelection.first
165
+ directory_path = extract_tree_item_path(tree_item)
166
+ return if directory_path.nil?
167
+ if !::Dir.exist?(directory_path)
168
+ tree_item = tree_item.getParentItem
169
+ directory_path = ::File.dirname(directory_path)
170
+ end
171
+ new_directory_path = ::File.expand_path(::File.join(directory_path, 'new_directory'))
172
+ FileUtils.mkdir_p(new_directory_path)
173
+ project_dir.refresh(async: false, force: true)
174
+ new_tree_item = body_root.depth_first_search {|ti| ti.getData.path == new_directory_path}.first
175
+ swt_widget.showItem(new_tree_item)
176
+ rename_tree_item(new_tree_item)
177
+ end
178
+
179
+ def add_new_file_to_selected_tree_item
180
+ project_dir.pause_refresh
181
+ tree_item = swt_widget.getSelection.first
182
+ directory_path = extract_tree_item_path(tree_item)
183
+ if !::Dir.exist?(directory_path)
184
+ tree_item = tree_item.getParentItem
185
+ directory_path = ::File.dirname(directory_path)
186
+ end
187
+ new_file_path = ::File.expand_path(::File.join(directory_path, 'new_file'))
188
+ FileUtils.touch(new_file_path)
189
+ # TODO look into refreshing only the parent directory to avoid slowdown
190
+ project_dir.refresh(async: false, force: true)
191
+ new_tree_item = body_root.depth_first_search {|ti| ti.getData.path == new_file_path}.first
192
+ swt_widget.showItem(new_tree_item)
193
+ rename_tree_item(new_tree_item, true)
194
+ end
195
+
196
+ def rename_selected_tree_item
197
+ project_dir.pause_refresh
198
+ tree_item = swt_widget.getSelection.first
199
+ rename_tree_item(tree_item)
200
+ end
201
+
202
+ def rename_tree_item(tree_item, new_file = false)
203
+ original_file = tree_item.getData
204
+ current_file = project_dir.selected_child_path == original_file.path
205
+ found_tab_item = gladiator.find_tab_item(original_file.path)
206
+ found_text_editor = found_tab_item&.getData('text_editor')
207
+ body_root.edit_tree_item(
208
+ tree_item,
209
+ after_write: -> (edited_tree_item) {
210
+ file = edited_tree_item.getData
211
+ file_path = file.path
212
+ file.name
213
+ if ::File.file?(file_path)
214
+ if new_file
215
+ project_dir.selected_child_path = file_path
216
+ else
217
+ found_text_editor&.file = file
218
+ found_tab_item&.setData('file', file)
219
+ found_tab_item&.setData('file_path', file.path)
220
+ found_tab_item&.setText(file.name)
221
+ if current_file
222
+ project_dir.selected_child_path = file_path
223
+ else
224
+ gladiator.selected_tab_item&.getData('text_editor')&.text_widget&.setFocus
225
+ end
226
+ async_exec {
227
+ swt_widget.showItem(edited_tree_item)
228
+ gladiator.body_root.pack_same_size
229
+ }
230
+ end
231
+ end
232
+ project_dir.resume_refresh
233
+ },
234
+ after_cancel: -> {
235
+ project_dir.resume_refresh
236
+ }
237
+ )
238
+ end
239
+
240
+ end
241
+
242
+ end
243
+
244
+ end