-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cobra
292 lines (236 loc) · 11.3 KB
/
Solution.cobra
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
194
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
use fastJSON
use System.Windows.Forms from System.Windows.Forms
use Qios.DevSuite.Components
use System.Drawing
class Solution
pro projects from var = List<of Project>()
pro startupProject from var as Project?
pro name from var = "New Solution"
pro fileName from var = ""
cue init
base.init
def save
if .fileName == ""
dlg = SaveFileDialog()
dlg.fileName = .fileName
dlg.filter = "Naja Solution files|*.najasln"
dlg.filterIndex = 1
if dlg.showDialog == DialogResult.OK
.fileName = dlg.fileName to !
else
return
try
System.IO.File.writeAllText(.fileName, fastJSON.JSON.instance.toJSON(this))
for project in .projects
project.save
catch ex as Exception
MessageBox.show(ex.message, ex.typeOf.name)
def load(fileName as String) as Solution is shared
try
result = fastJSON.JSON.instance.toObject(System.IO.File.readAllText(fileName),Solution.getType) to Solution
result.fileName = fileName #just in case it got overwritten in the deserialization
return result
catch ex as Exception
MessageBox.show(ex.toString, ex.typeOf.name)
return Solution()
class SolutionTree inherits TreeView
var _sln as Solution?
var _contextMenu as QContextMenu
var _components as System.ComponentModel.IContainer? #
pro solution as Solution?
get
return _sln
set
_sln = value
_initializeSolutionNodes
var _solutionNode as TreeNode?
cue init
base.init
_components =System.ComponentModel.Container()
_initializeContextMenu
cue init(sln as Solution)
base.init
.solution = sln
_components =System.ComponentModel.Container()
_initializeContextMenu
def findNodes(text as String) as List<of TreeNode>
return .findNodes(.nodes[0],text)
def findNodes(root as TreeNode, text as String) as List<of TreeNode>
result = List<of TreeNode>()
if root.text.toUpper==text.toUpper
result.add(root)
for node in root.nodes
subNodes = .findNodes(node, text)
if subNodes.count >0
result.addRange(subNodes)
return result
def _initializeContextMenu
_contextMenu = QContextMenu(_components)
mnuAddReference = QMenuItem()
mnuAddReference.itemName = "mnuAddReference"
mnuAddReference.title = "Add a Reference"
mnuAddReference.toolTip = "Add a Reference to the project."
_contextMenu.menuItems.add(mnuAddReference)
mnuAddFile = QMenuItem()
mnuAddFile.itemName = "mnuAddFile"
mnuAddFile.title = "Add Existing File"
mnuAddFile.toolTip = "Add existing file to the project."
_contextMenu.menuItems.add(mnuAddFile)
mnuRemoveFile = QMenuItem()
mnuRemoveFile.itemName = "mnuRemoveFile"
mnuRemoveFile.title = "Remove Existing File"
mnuRemoveFile.toolTip = "Remove file from the project."
_contextMenu.menuItems.add(mnuRemoveFile)
mnuNewFile = QMenuItem()
mnuNewFile.itemName = "mnuNewFile"
mnuNewFile.title = "Add New File"
mnuNewFile.toolTip = "Add a new source file to the project."
_contextMenu.menuItems.add(mnuNewFile)
mnuNewProject = QMenuItem()
mnuNewProject.itemName = "mnuNewProject"
mnuNewProject.title = "Add New Project"
mnuNewProject.toolTip = "Add a new project to the solution."
_contextMenu.menuItems.add(mnuNewProject)
mnuAddProject = QMenuItem()
mnuAddProject.itemName = "mnuAddProject"
mnuAddProject.title = "Add Existing Project"
mnuAddProject.toolTip = "Add existing project to the solution."
_contextMenu.menuItems.add(mnuAddProject)
mnuRename = QMenuItem()
mnuRename.itemName = "mnuRename"
mnuRename.title = "Rename"
mnuRename.toolTip = "Rename the item."
_contextMenu.menuItems.add(mnuRename)
listen _contextMenu.menuItemActivated, ref .contextMenu_MenuItemSelected
_contextMenu.addListener(this)
def contextMenu_MenuItemSelected(sender, e as QMenuEventArgs)
if e.menuItem is nil or e.menuItem.isSeparator, return
item = e.menuItem
#TODO: Add logic to handle the particular nodes available.
branch item.itemName
on "mnuAddFile"
newFile = NajaUtils.promptForOpenFileName
if newFile == "", return
.addFile(newFile, _solutionNode.nodes[0] to !)
#on "mnuNewFile"
# pass
#on "mnuNewProject"
# pass
#on "mnuAddProject"
# pass
on "mnuRename"
if .selectedNode == nil or .selectedNode.tag == nil
return
fileName = .selectedNode.tag.toString
if not System.IO.File.exists(fileName) and fileName <> "New File"
return
shortFileName = System.IO.Path.getFileName(fileName) to !
if NajaUtils.inputBox("Rename file","Enter a new file name:", inout shortFileName ) == DialogResult.OK
newFileName = System.IO.Path.combine(System.IO.Path.getDirectoryName(fileName),shortFileName)
if fileName <> "New File"
System.IO.File.move(fileName,newFileName)
.selectedNode.tag = newFileName
.selectedNode.text = shortFileName
.selectedNode.name = newFileName
on "mnuRemoveFile"
trace .selectedNode
if .selectedNode == nil or .selectedNode.tag == nil
return
fileName = .selectedNode.tag.toString
if not System.IO.File.exists(fileName) and fileName <> "New File"
return
.removeFile(.selectedNode to !, _solutionNode.nodes[0] to !)
on "mnuAddReference"
newFile = NajaUtils.promptForOpenFileName("DLL files (*.dll)|*.dll")
if newFile =="", return
.addReference(newFile, _solutionNode.nodes[0] to !)
else
MessageBox.show("Not implemented yet!")
def _initializeSolutionNodes
.nodes.clear
sln = TreeNode(name="Solution",text="Solution '[.solution.name]' ([.solution.projects.count] projects)")
_solutionNode = sln
sln.tag = .solution
.nodes.add(sln)
for project in .solution.projects
_initializeProjectNode(project, project == .solution.startupProject)
sln.expand
def _initializeProjectNode(project as Project, isStartup as bool) as TreeNode
pj = TreeNode(name="Project|[project.name]",text=project.name + " ")
if isStartup
pj.nodeFont = Font(pj.nodeFont,pj.nodeFont.style | FontStyle.Bold)
pj.tag = project
_solutionNode.nodes.add(pj)
#references
references = TreeNode(name="References|[project.name]",text="References")
pj.nodes.add(references)
for reference in project.references
prefix = ""
if reference in ["mscorlib.dll","System.dll","Cobra.Lang.dll"], prefix = "DEFAULT|"
references.nodes.add(TreeNode(name=prefix+reference,text=System.IO.Path.getFileName(reference).replace(".dll","") + " ",tag=reference))
#files
files = TreeNode(name="Files|[project.name]",text="Files")
pj.nodes.add(files)
for file in project.files
files.nodes.add(TreeNode(name=file,text=System.IO.Path.getFileName(file),tag=file))
files.expand
references.expand
pj.expand
return pj
def closeSolution
.solution = Solution()
def addProject(project as Project) as TreeNode
if _solutionNode is nil
.solution = Solution()
projectNode= _initializeProjectNode(project, false)
.solution.projects.add(project)
return projectNode
def removeProject(projectNode as TreeNode)
if _solutionNode is nil, return
_solutionNode.nodes.remove(projectNode)
.solution.projects.remove(projectNode.tag to Project)
def addReference(reference as String,projectNode as TreeNode) as TreeNode
referenceNode = TreeNode(name=reference,text=System.IO.Path.getFileName(reference).replace(".dll","") + " ",tag=reference)
projectNode.nodes["References|[(projectNode.tag to Project).name]"].nodes.add(referenceNode)
(projectNode.tag to Project).references.add(reference)
return referenceNode
def addFile(file as String, projectNode as TreeNode) as TreeNode
fileNode = TreeNode(name=file,text=System.IO.Path.getFileName(file),tag=file)
projName = (projectNode.tag to Project).name
projectNode.nodes["Files|[projName]"].nodes.add(fileNode)
(projectNode.tag to Project).files.add(file)
return fileNode
def removeReference(referenceNode as TreeNode, projectNode as TreeNode)
if referenceNode.name.startsWith("DEFAULT|"), return #can't get rid of the default references.
projectNode.nodes["References|[(projectNode.tag to Project).name]"].nodes.remove(referenceNode)
(projectNode.tag to Project).references.remove(referenceNode.text)
def removeFile(fileNode as TreeNode, projectNode as TreeNode)
projectNode.nodes["Files|[(projectNode.tag to Project).name]"].nodes.remove(fileNode)
(projectNode.tag to Project).files.remove(fileNode.text)
class Project
pro references from var = List<of String>()
pro files from var = List<of String>()
pro name from var = "New Project"
pro fileName from var = ""
cue init
base.init
def save
if .fileName == ""
dlg = SaveFileDialog()
dlg.fileName = .fileName
dlg.filter = "Cobra Project files|*.cobraproj"
dlg.filterIndex = 1
if dlg.showDialog == DialogResult.OK
.fileName = dlg.fileName to !
try
System.IO.File.writeAllText(.fileName, fastJSON.JSON.instance.toJSON(this))
catch ex as Exception
MessageBox.show(ex.message, ex.typeOf.name)
def load(fileName as String) as Project is shared
try
result = fastJSON.JSON.instance.toObject(System.IO.File.readAllText(fileName),Project) to Project
result.fileName = fileName #just in case it got overwritten in the deserialization
return result
catch ex as Exception
MessageBox.show(ex.message, ex.typeOf.name)
return Project()