-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.tsx
More file actions
989 lines (885 loc) · 34 KB
/
CodeGenerator.tsx
File metadata and controls
989 lines (885 loc) · 34 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
import React, { useState, useEffect } from 'react';
import {
Box,
Paper,
Typography,
TextField,
Select,
MenuItem,
FormControl,
InputLabel,
Button,
List,
ListItem,
ListItemText,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Divider,
IconButton,
Tabs,
Tab,
SelectChangeEvent,
ListItemButton,
ListSubheader,
Accordion,
AccordionSummary,
AccordionDetails,
Chip,
TabScrollButton,
CircularProgress
} from '@mui/material';
import AddIcon from '@mui/icons-material/Add';
import EditIcon from '@mui/icons-material/Edit';
import DownloadIcon from '@mui/icons-material/Download';
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
import DeleteIcon from '@mui/icons-material/Delete';
import CloseIcon from '@mui/icons-material/Close';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import { CodeGenerationTemplate, CodeGenerationResult, Diagram, Metamodel, CodeGenerationProject, Model } from '../../models/types';
import { codeGenerationService } from '../../services/codegeneration.service';
import { diagramService } from '../../services/diagram.service';
import { metamodelService } from '../../services/metamodel.service';
import { modelService } from '../../services/model.service';
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
import { okaidia } from '@uiw/codemirror-theme-okaidia';
interface CodeGeneratorProps {
diagramId?: string; // Make diagramId optional
}
interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
}
function TabPanel(props: TabPanelProps) {
const { children, value, index, ...other } = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`code-tabpanel-${index}`}
{...other}
style={{ height: 'calc(100% - 48px)', overflow: 'auto' }}
>
{value === index && (
<Box sx={{ p: 2, height: '100%' }}>
{children}
</Box>
)}
</div>
);
}
// Custom IDE-like template editor component
const TemplateEditor = ({
value,
onChange
}: {
value: string;
onChange: (value: string) => void;
}) => {
return (
<Box sx={{
display: 'flex',
flexDirection: 'column',
height: '400px',
border: '1px solid #494949',
borderRadius: '4px',
overflow: 'hidden'
}}>
<Box sx={{
backgroundColor: '#333333',
color: 'white',
padding: '4px 8px',
borderBottom: '1px solid #494949',
display: 'flex',
justifyContent: 'space-between'
}}>
<Typography variant="caption">Template Editor</Typography>
<Typography variant="caption">Handlebars (JS)</Typography>
</Box>
<CodeMirror
value={value}
height="400px"
extensions={[javascript(), okaidia]}
onChange={onChange}
theme="dark"
/>
</Box>
);
};
// Interface for project template editing
interface ProjectTemplate {
id: string;
name: string;
language: 'java' | 'python';
content: string;
outputPattern: string;
isNew?: boolean;
}
const CodeGenerator: React.FC<CodeGeneratorProps> = ({ diagramId }) => {
const [loading, setLoading] = useState<boolean>(true);
const [diagram, setDiagram] = useState<Diagram | null>(null);
const [metamodels, setMetamodels] = useState<Metamodel[]>([]);
const [projects, setProjects] = useState<CodeGenerationProject[]>([]);
const [exampleProjects, setExampleProjects] = useState<CodeGenerationProject[]>([]);
const [selectedProject, setSelectedProject] = useState<string>('');
// Example templates state
const [exampleTemplates, setExampleTemplates] = useState<CodeGenerationTemplate[]>([]);
// Generated code
const [generatedCode, setGeneratedCode] = useState<CodeGenerationResult[]>([]);
// UI state
const [isProjectDialogOpen, setIsProjectDialogOpen] = useState(false);
const [selectedProjectForEditing, setSelectedProjectForEditing] = useState<CodeGenerationProject | null>(null);
const [activeTab, setActiveTab] = useState(0);
const [selectedFileIndex, setSelectedFileIndex] = useState<number | null>(null);
// Project editing state
const [projectName, setProjectName] = useState('');
const [projectDescription, setProjectDescription] = useState('');
const [projectTarget, setProjectTarget] = useState('');
// Template editing within project
const [projectTemplates, setProjectTemplates] = useState<ProjectTemplate[]>([]);
const [activeTemplateTab, setActiveTemplateTab] = useState(0);
useEffect(() => {
// Load data
const loadData = async () => {
setLoading(true);
try {
// Load all metamodels
const allMetamodels = metamodelService.getAllMetamodels();
setMetamodels(allMetamodels);
// If diagramId is provided, load that diagram
if (diagramId) {
const diagramData = diagramService.getDiagramById(diagramId);
setDiagram(diagramData || null);
}
// Load example templates
const allTemplates = codeGenerationService.getAllTemplates();
// Check if we need to load example templates
if (allTemplates.length === 0) {
codeGenerationService.loadExampleTemplates();
}
// Get updated templates
const updatedTemplates = codeGenerationService.getAllTemplates();
// Example templates are the ones with these specific names
const exampleNames = [
'Java Class Template',
'Python Class Template',
'Multi-Server Configuration',
'Complete Application'
];
// Filter to only get example templates
const examples = updatedTemplates.filter(t => exampleNames.includes(t.name));
setExampleTemplates(examples);
// Load projects
const allProjects = codeGenerationService.getAllProjects();
// Check if we need to load example projects
if (allProjects.length === 0) {
codeGenerationService.loadExampleProjects();
}
// Get updated projects
const updatedProjects = codeGenerationService.getAllProjects();
const exampleProjs = updatedProjects.filter(p => p.isExample);
const userProjs = updatedProjects.filter(p => !p.isExample);
setExampleProjects(exampleProjs);
setProjects(userProjs);
// Select first project if available
if (userProjs.length > 0) {
setSelectedProject(userProjs[0].id);
} else if (exampleProjs.length > 0) {
setSelectedProject(exampleProjs[0].id);
}
} catch (error) {
console.error('Error loading data:', error);
} finally {
setLoading(false);
}
};
loadData();
}, [diagramId]);
const handleProjectChange = (event: SelectChangeEvent<string>) => {
setSelectedProject(event.target.value);
};
const handleGenerateCode = () => {
if (!selectedProject) {
alert('Please select a project first');
return;
}
try {
// Get the selected project
const project = [...projects, ...exampleProjects].find(p => p.id === selectedProject);
if (!project) {
throw new Error('Selected project not found');
}
// Find models that conform to the project's target metamodel
const relatedModels = modelService.getModelsByMetamodelId(project.targetMetamodelId);
if (relatedModels.length === 0) {
throw new Error(`No models found for metamodel: ${project.targetMetamodelId}`);
}
// Smart model selection: prioritize diagram's model if available, or analyze template for model references
let selectedModel = relatedModels[0]; // Default to first model
// If we have a diagramId, use that diagram's model
if (diagramId) {
const diagram = diagramService.getDiagramById(diagramId);
if (diagram) {
const diagramModel = modelService.getModelById(diagram.modelId);
if (diagramModel && relatedModels.some(m => m.id === diagramModel.id)) {
selectedModel = diagramModel;
}
}
} else {
// For standalone code generation, analyze project templates to find referenced models
const projectTemplates = project.templates;
for (const template of projectTemplates) {
// Look for model references in the template (e.g., ManufacturingModel2, ManufacturingModel)
for (const model of relatedModels) {
if (template.templateContent.includes(model.name)) {
selectedModel = model;
break;
}
}
if (selectedModel !== relatedModels[0]) break; // Found a specific model reference
}
}
// Find the diagram for the selected model if it exists
const diagrams = diagramService.getAllDiagrams();
const matchingDiagram = diagrams.find(d => d.modelId === selectedModel.id);
let results: CodeGenerationResult[];
if (matchingDiagram) {
// Use diagram-based generation if a diagram exists
results = codeGenerationService.generateProjectCode(
matchingDiagram.id,
selectedProject
);
} else {
// Use model-based generation if no diagram exists
results = codeGenerationService.generateProjectCodeFromModel(
selectedModel.id,
selectedProject
);
}
setGeneratedCode(results);
setSelectedFileIndex(results.length > 0 ? 0 : null);
setActiveTab(2); // Switch to Generated Files tab
} catch (error) {
console.error('Error generating code:', error);
alert(`Error generating code: ${error instanceof Error ? error.message : String(error)}`);
}
};
const downloadFile = (content: string, filename: string) => {
const element = document.createElement('a');
const file = new Blob([content], { type: 'text/plain' });
element.href = URL.createObjectURL(file);
element.download = filename;
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
const handleTabChange = (event: React.SyntheticEvent, newValue: number) => {
setActiveTab(newValue);
};
// Template tab management
const addTemplateTab = () => {
const newTemplate: ProjectTemplate = {
id: `new-template-${Date.now()}`,
name: `Template ${projectTemplates.length + 1}`,
language: 'java',
content: '',
outputPattern: '{{name}}.java',
isNew: true
};
setProjectTemplates([...projectTemplates, newTemplate]);
setActiveTemplateTab(projectTemplates.length);
};
const removeTemplateTab = (index: number) => {
if (projectTemplates.length === 1) {
return; // Don't remove the last template
}
const newTemplates = [...projectTemplates];
newTemplates.splice(index, 1);
setProjectTemplates(newTemplates);
// Adjust active tab if necessary
if (activeTemplateTab >= newTemplates.length) {
setActiveTemplateTab(newTemplates.length - 1);
} else if (activeTemplateTab === index) {
setActiveTemplateTab(Math.max(0, index - 1));
}
};
const updateTemplateTab = (index: number, updates: Partial<ProjectTemplate>) => {
const newTemplates = [...projectTemplates];
newTemplates[index] = { ...newTemplates[index], ...updates };
setProjectTemplates(newTemplates);
};
const handleCreateProject = () => {
if (!projectName.trim()) {
alert('Please enter a project name');
return;
}
if (!projectTarget) {
alert('Please select a target metamodel');
return;
}
try {
// Create the project
const newProject = codeGenerationService.createProject(
projectName,
projectTarget,
projectDescription
);
// Add templates to the project
projectTemplates.forEach(template => {
codeGenerationService.addTemplateToProject(
newProject.id,
template.name,
template.language,
template.content,
template.outputPattern
);
});
// Update state
setProjects([...projects, newProject]);
// Select the new project
setSelectedProject(newProject.id);
// Close dialog
setIsProjectDialogOpen(false);
} catch (error) {
console.error('Error creating project:', error);
alert(`Error creating project: ${error instanceof Error ? error.message : String(error)}`);
}
};
const handleUpdateProject = () => {
if (!selectedProjectForEditing || !projectName || !projectTarget) {
alert('Please provide a project name and target metamodel');
return;
}
codeGenerationService.updateProject(
selectedProjectForEditing.id,
{
name: projectName,
description: projectDescription,
targetMetamodelId: projectTarget,
updatedAt: Date.now()
}
);
// Update templates in the project
// First, remove all templates
const currentProject = codeGenerationService.getProjectById(selectedProjectForEditing.id);
if (currentProject) {
// Remove existing templates
currentProject.templates.forEach(template => {
codeGenerationService.removeTemplateFromProject(selectedProjectForEditing.id, template.id);
});
// Add updated templates
projectTemplates.forEach(template => {
codeGenerationService.addTemplateToProject(
selectedProjectForEditing.id,
template.name,
template.language,
template.content,
template.outputPattern
);
});
}
// Refresh projects
const allProjects = codeGenerationService.getAllProjects();
setProjects(allProjects.filter(p => !p.isExample));
resetProjectForm();
setIsProjectDialogOpen(false);
setSelectedProjectForEditing(null);
};
const handleEditProject = (project: CodeGenerationProject) => {
setSelectedProjectForEditing(project);
setProjectName(project.name);
setProjectDescription(project.description || '');
setProjectTarget(project.targetMetamodelId);
// Set up template tabs
const templates: ProjectTemplate[] = project.templates.map(t => ({
id: t.id,
name: t.name,
language: t.language,
content: t.templateContent,
outputPattern: t.outputPattern
}));
setProjectTemplates(templates.length > 0 ? templates : [{
id: `new-template-${Date.now()}`,
name: 'Template 1',
language: 'java',
content: '',
outputPattern: '{{name}}.java',
isNew: true
}]);
setActiveTemplateTab(0);
setIsProjectDialogOpen(true);
};
const handleDeleteProject = (projectId: string) => {
if (window.confirm('Are you sure you want to delete this project?')) {
codeGenerationService.deleteProject(projectId);
// Check if it was a user project or example project
const isUserProject = projects.some(p => p.id === projectId);
if (isUserProject) {
setProjects(projects.filter(p => p.id !== projectId));
} else {
setExampleProjects(exampleProjects.filter(p => p.id !== projectId));
}
if (selectedProject === projectId) {
setSelectedProject('');
}
}
};
const resetProjectForm = () => {
setProjectName('');
setProjectDescription('');
setProjectTarget('');
// Initialize with one empty template
setProjectTemplates([{
id: `new-template-${Date.now()}`,
name: 'Template 1',
language: 'java',
content: '',
outputPattern: '{{name}}.java',
isNew: true
}]);
setActiveTemplateTab(0);
setSelectedProjectForEditing(null);
};
if (loading) {
return (
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%' }}>
<CircularProgress />
<Typography variant="h6" sx={{ ml: 2 }}>
Loading code generation data...
</Typography>
</Box>
);
}
return (
<Box sx={{ display: 'flex', flexDirection: 'column', height: '100%', p: 2 }}>
<Typography variant="h5" gutterBottom>
Code Generation
</Typography>
<Paper sx={{ p: 2, mb: 2 }}>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<FormControl sx={{ minWidth: 300 }}>
<InputLabel id="project-select-label">Project</InputLabel>
<Select
labelId="project-select-label"
value={selectedProject}
label="Project"
onChange={handleProjectChange}
>
{/* User Projects */}
{projects.length > 0 && (
<ListSubheader>Your Projects</ListSubheader>
)}
{projects.map((project) => (
<MenuItem key={project.id} value={project.id}>
{project.name} ({metamodels.find(m => m.id === project.targetMetamodelId)?.name || 'Unknown Metamodel'})
</MenuItem>
))}
{/* Example Projects */}
{exampleProjects.length > 0 && (
<ListSubheader>Example Projects</ListSubheader>
)}
{exampleProjects.map((project) => (
<MenuItem key={project.id} value={project.id}>
{project.name} ({metamodels.find(m => m.id === project.targetMetamodelId)?.name || 'Unknown Metamodel'})
</MenuItem>
))}
</Select>
</FormControl>
<Box sx={{ display: 'flex', gap: 2 }}>
<Button
variant="contained"
startIcon={<AddIcon />}
onClick={() => {
resetProjectForm();
setIsProjectDialogOpen(true);
}}
>
New Project
</Button>
<Button
variant="contained"
color="primary"
startIcon={<PlayArrowIcon />}
onClick={handleGenerateCode}
disabled={!selectedProject}
>
Generate Code
</Button>
</Box>
</Box>
</Paper>
<Paper sx={{ flexGrow: 1, display: 'flex', flexDirection: 'column', overflow: 'hidden' }}>
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
<Tabs value={activeTab} onChange={handleTabChange}>
<Tab label="Projects" />
<Tab label="Example Templates" />
<Tab label="Generated Files" />
</Tabs>
</Box>
<TabPanel value={activeTab} index={0}>
{/* Projects Tab */}
<Typography variant="h6" gutterBottom>Projects</Typography>
<List>
{projects.map(project => (
<ListItem
key={project.id}
secondaryAction={
<Box>
<IconButton edge="end" onClick={() => handleEditProject(project)}>
<EditIcon />
</IconButton>
<IconButton edge="end" onClick={() => handleDeleteProject(project.id)}>
<DeleteIcon />
</IconButton>
</Box>
}
>
<ListItemText
primary={project.name}
secondary={`Templates: ${project.templates.length} | Target Metamodel: ${
metamodelService.getMetamodelById(project.targetMetamodelId)?.name || 'Unknown'
}`}
/>
</ListItem>
))}
{projects.length === 0 && (
<Typography variant="body2" color="textSecondary" align="center" sx={{ py: 2 }}>
No projects defined yet. Create a project to generate code.
</Typography>
)}
</List>
<Box sx={{ mt: 2, display: 'flex', justifyContent: 'flex-end' }}>
<Button
variant="outlined"
startIcon={<AddIcon />}
onClick={() => {
resetProjectForm();
// Initialize with one empty template
setProjectTemplates([{
id: `new-template-${Date.now()}`,
name: 'Template 1',
language: 'java',
content: '',
outputPattern: '{{name}}.java',
isNew: true
}]);
setActiveTemplateTab(0);
setIsProjectDialogOpen(true);
}}
>
New Project
</Button>
</Box>
</TabPanel>
<TabPanel value={activeTab} index={1}>
{/* Example Templates Tab */}
<Typography variant="h6" gutterBottom>Example Templates</Typography>
<List>
{exampleTemplates.map(template => (
<ListItem
key={template.id}
secondaryAction={
<Box>
<IconButton edge="end" onClick={() => {
// Create a new project with this example template
resetProjectForm();
setProjectTemplates([{
id: `new-template-${Date.now()}`,
name: template.name,
language: template.language,
content: template.templateContent,
outputPattern: template.outputPattern,
isNew: true
}]);
setActiveTemplateTab(0);
setIsProjectDialogOpen(true);
}}>
<AddIcon />
</IconButton>
</Box>
}
>
<ListItemText
primary={template.name}
secondary={`Language: ${template.language} | Target: ${
metamodelService.getMetamodelById(template.targetMetamodelId)?.name || 'Unknown'
}`}
/>
</ListItem>
))}
{exampleTemplates.length === 0 && (
<Typography variant="body2" color="textSecondary" align="center" sx={{ py: 2 }}>
No example templates available.
</Typography>
)}
</List>
</TabPanel>
<TabPanel value={activeTab} index={2}>
{/* Generated Files Tab */}
{generatedCode.length > 0 ? (
<Box sx={{ display: 'flex', height: '100%' }}>
<List sx={{ width: 250, borderRight: '1px solid #eee', overflowY: 'auto' }}>
{generatedCode.map((file, index) => (
<ListItem
key={index}
disablePadding
secondaryAction={
<IconButton edge="end" onClick={() => downloadFile(file.content, file.filename)}>
<DownloadIcon />
</IconButton>
}
>
<ListItemButton
selected={selectedFileIndex === index}
onClick={() => setSelectedFileIndex(index)}
>
<ListItemText primary={file.filename} />
</ListItemButton>
</ListItem>
))}
</List>
<Box sx={{ flexGrow: 1, p: 2, overflowY: 'auto', height: '100%' }}>
{selectedFileIndex !== null && (
<>
<Box sx={{
display: 'flex',
justifyContent: 'space-between',
mb: 2,
backgroundColor: '#333333',
padding: '8px',
color: 'white',
borderTopLeftRadius: '4px',
borderTopRightRadius: '4px'
}}>
<Typography variant="subtitle1">
{generatedCode[selectedFileIndex].filename}
</Typography>
<Button
variant="outlined"
size="small"
startIcon={<DownloadIcon />}
onClick={() => downloadFile(
generatedCode[selectedFileIndex].content,
generatedCode[selectedFileIndex].filename
)}
sx={{ color: 'white', borderColor: 'rgba(255,255,255,0.3)' }}
>
Download
</Button>
</Box>
<Box sx={{
height: 'calc(100% - 60px)',
backgroundColor: '#1e1e1e',
borderBottomLeftRadius: '4px',
borderBottomRightRadius: '4px',
overflow: 'auto'
}}>
<CodeMirror
value={generatedCode[selectedFileIndex].content}
height="calc(100% - 60px)"
extensions={[javascript(), okaidia]}
theme="dark"
/>
</Box>
</>
)}
</Box>
</Box>
) : (
<Typography variant="body2" color="textSecondary" align="center">
No files generated yet. Select a project and generate code.
</Typography>
)}
</TabPanel>
</Paper>
{/* Project Dialog */}
<Dialog
open={isProjectDialogOpen}
onClose={() => {
setIsProjectDialogOpen(false);
resetProjectForm();
}}
maxWidth="lg"
fullWidth
>
<DialogTitle>
{selectedProjectForEditing ? 'Edit Project' : 'Create New Project'}
</DialogTitle>
<DialogContent>
<Box sx={{ pt: 1, display: 'flex', flexDirection: 'column', gap: 2 }}>
<Box sx={{ display: 'flex', flexDirection: { xs: 'column', md: 'row' }, gap: 2 }}>
<Box sx={{ flex: 3 }}>
<TextField
label="Project Name"
fullWidth
value={projectName}
onChange={(e) => setProjectName(e.target.value)}
/>
</Box>
<Box sx={{ flex: 1 }}>
<FormControl fullWidth>
<InputLabel id="project-target-select-label">Target Metamodel</InputLabel>
<Select
labelId="project-target-select-label"
value={projectTarget}
label="Target Metamodel"
onChange={(e) => setProjectTarget(e.target.value)}
>
{metamodels.map(mm => (
<MenuItem key={mm.id} value={mm.id}>{mm.name}</MenuItem>
))}
</Select>
</FormControl>
</Box>
</Box>
<Box>
<TextField
label="Project Description"
fullWidth
multiline
rows={2}
value={projectDescription}
onChange={(e) => setProjectDescription(e.target.value)}
/>
</Box>
<Divider sx={{ my: 1 }} />
<Typography variant="h6" gutterBottom>
Templates
</Typography>
<Box sx={{ borderBottom: 1, borderColor: 'divider', display: 'flex' }}>
<Tabs
value={activeTemplateTab}
onChange={(e, newValue) => setActiveTemplateTab(newValue)}
variant="scrollable"
scrollButtons="auto"
sx={{ flex: 1 }}
>
{projectTemplates.map((template, index) => (
<Tab
key={template.id}
label={
<Box sx={{ display: 'flex', alignItems: 'center' }}>
{template.name}
{projectTemplates.length > 1 && (
<IconButton
size="small"
onClick={(e) => {
e.stopPropagation();
removeTemplateTab(index);
}}
sx={{ ml: 1 }}
>
<CloseIcon fontSize="small" />
</IconButton>
)}
</Box>
}
/>
))}
</Tabs>
<Button
startIcon={<AddIcon />}
onClick={addTemplateTab}
sx={{ ml: 1 }}
>
Add Template
</Button>
</Box>
{projectTemplates.map((template, index) => (
<Box
key={template.id}
sx={{
display: activeTemplateTab === index ? 'flex' : 'none',
flexDirection: 'column',
gap: 2
}}
>
<Box sx={{ display: 'flex', gap: 2 }}>
<TextField
label="Template Name"
fullWidth
value={template.name}
onChange={(e) => updateTemplateTab(index, { name: e.target.value })}
/>
<FormControl sx={{ minWidth: 150 }}>
<InputLabel>Language</InputLabel>
<Select
value={template.language}
label="Language"
onChange={(e) => updateTemplateTab(index, { language: e.target.value as 'java' | 'python' })}
>
<MenuItem value="java">Java</MenuItem>
<MenuItem value="python">Python</MenuItem>
</Select>
</FormControl>
</Box>
<TextField
label="Output Filename Pattern"
fullWidth
value={template.outputPattern}
onChange={(e) => updateTemplateTab(index, { outputPattern: e.target.value })}
helperText="Use Handlebars syntax, e.g. {{name}}.java"
/>
<Typography variant="subtitle2" gutterBottom>
Template Content
</Typography>
<TemplateEditor
value={template.content}
onChange={(value) => updateTemplateTab(index, { content: value })}
/>
<Typography variant="caption" color="textSecondary">
Available helpers: <span style={{ color: '#569cd6' }}>{{capitalize name}}</span>, <span style={{ color: '#569cd6' }}>{{lowercase name}}</span>, <span style={{ color: '#569cd6' }}>{{camelCase name}}</span>, <span style={{ color: '#569cd6' }}>{{snakeCase name}}</span>
</Typography>
<Divider sx={{ my: 1 }} />
<Typography variant="subtitle2" gutterBottom>
Multi-element Access
</Typography>
<Typography variant="caption" color="textSecondary" component="div" sx={{ mb: 1 }}>
<Box component="ul" sx={{ m: 0, pl: 2 }}>
<li>Access elements by name: {{ElementName.property}} (e.g., {{Class_Server1.port}})</li>
<li>Access all elements: {{#each elements}}...{{/each}}</li>
<li>Access elements by class: {{#each elementsByClassName.Server}}...{{/each}}</li>
<li>Count elements by class: <strong>{{countElements "Robot_Class"}}</strong> (recommended)</li>
<li>Alternative count methods: {{countByClassName "Robot_Class"}}, {{elementsByClassName.Robot_Class.length}}</li>
<li>Compare values: {{#if (eq metaClassId "Server")}}...{{/if}}</li>
</Box>
</Typography>
<Typography variant="subtitle2" gutterBottom>
Metamodel Access
</Typography>
<Typography variant="caption" color="textSecondary" component="div" sx={{ mb: 1 }}>
<Box component="ul" sx={{ m: 0, pl: 2 }}>
<li>Access metamodel name: {{metamodel.name}}</li>
<li>Access metamodel classes: {{#each metamodel.classes}}...{{/each}}</li>
<li>Count metaclasses: {{metamodel.classes.length}}</li>
</Box>
</Typography>
</Box>
))}
</Box>
</DialogContent>
<DialogActions>
<Button
onClick={() => {
setIsProjectDialogOpen(false);
resetProjectForm();
}}
>
Cancel
</Button>
<Button
onClick={selectedProjectForEditing ? handleUpdateProject : handleCreateProject}
color="primary"
variant="contained"
>
{selectedProjectForEditing ? 'Update' : 'Create'}
</Button>
</DialogActions>
</Dialog>
</Box>
);
};
export default CodeGenerator;