All files / src/generator componentGenerators.ts

100% Statements 144/144
93.24% Branches 69/74
100% Functions 26/26
100% Lines 140/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                      407x       103x       12x       6x       189x       97x               45x 40x 40x   45x 40x     35x                 18x 1x     17x   17x 12x         8x   1x   1x   1x   5x         75x 57x     18x 3x     46x       70x 33x 33x 33x   33x 91x 20x     71x 1x     70x   70x 22x 22x 21x       70x 70x 70x     33x 70x     33x                     12x 12x 6x 6x 6x   6x               6x 6x     6x 5x 5x 5x 5x 3x   5x   5x                     6x 6x   6x 6x 6x   6x                       8x 8x       8x   8x 8x 8x 3x 3x   5x 5x     8x   8x 8x 3x 3x 3x       8x     8x     8x   8x                     33x 33x   33x 33x 6x     33x 33x 8x 8x       33x   103x     33x                                 103x 33x         33x 33x 6x     33x 33x 8x                       33x                         75x 52x     23x 23x 1x     22x                       75x 75x 75x 75x 75x   75x 33x 33x   42x     75x       75x 17x     75x 42x     75x   75x 75x 3x 3x 3x       75x 75x 75x   75x    
import type { ComponentModel } from "../components/ComponentModel";
import {
  applyInlineStyleCode,
  escapeJava,
  getComponentInitCode,
  getComponentPropsCode,
  getListenerCode,
} from "./codeHelpers";
import { getComponentSwingType } from "./swingMappings";
 
function getComponentType(comp: ComponentModel): string {
  return comp.type as string;
}
 
function isMenuBarComponent(comp: ComponentModel): boolean {
  return getComponentType(comp) === "MenuBar";
}
 
function isMenuComponent(comp: ComponentModel): boolean {
  return getComponentType(comp) === "Menu";
}
 
function isMenuItemComponent(comp: ComponentModel): boolean {
  return getComponentType(comp) === "MenuItem";
}
 
function isToolBarComponent(comp: ComponentModel): boolean {
  return getComponentType(comp) === "ToolBar";
}
 
function isPanelComponent(comp: ComponentModel): boolean {
  return getComponentType(comp) === "Panel";
}
 
function getOrderedChildren(
  parent: ComponentModel,
  componentMap: Map<string, ComponentModel>,
  allComponents: ComponentModel[],
): ComponentModel[] {
  const orderedByParent = (parent.children ?? [])
    .map((childId) => componentMap.get(childId))
    .filter((comp): comp is ComponentModel => Boolean(comp));
 
  if (orderedByParent.length > 0) {
    return orderedByParent;
  }
 
  return allComponents.filter((comp) => comp.parentId === parent.id);
}
 
function collectDescendantIds(
  parent: ComponentModel,
  componentMap: Map<string, ComponentModel>,
  allComponents: ComponentModel[],
  visited: Set<string>,
): void {
  if (visited.has(parent.id)) {
    return;
  }
 
  visited.add(parent.id);
 
  for (const child of getOrderedChildren(parent, componentMap, allComponents)) {
    collectDescendantIds(child, componentMap, allComponents, visited);
  }
}
 
function getToolBarBorderPosition(comp: ComponentModel): string {
  switch (comp.position) {
    case "bottom":
      return "BorderLayout.SOUTH";
    case "left":
      return "BorderLayout.WEST";
    case "right":
      return "BorderLayout.EAST";
    default:
      return "BorderLayout.NORTH";
  }
}
 
function hasPanelChildren(panel: ComponentModel, allComponents: ComponentModel[]): boolean {
  if (!isPanelComponent(panel)) {
    return false;
  }
 
  if ((panel.children?.length ?? 0) > 0) {
    return true;
  }
 
  return allComponents.some((candidate) => candidate.parentId === panel.id);
}
 
function sortRegularComponentsForGeneration(regularComponents: ComponentModel[]): ComponentModel[] {
  const componentMap = new Map(regularComponents.map((component) => [component.id, component]));
  const visiting = new Set<string>();
  const visited = new Set<string>();
  const ordered: ComponentModel[] = [];
 
  const visit = (component: ComponentModel): void => {
    if (visited.has(component.id)) {
      return;
    }
 
    if (visiting.has(component.id)) {
      return;
    }
 
    visiting.add(component.id);
 
    if (component.parentId) {
      const parent = componentMap.get(component.parentId);
      if (parent) {
        visit(parent);
      }
    }
 
    visiting.delete(component.id);
    visited.add(component.id);
    ordered.push(component);
  };
 
  for (const component of regularComponents) {
    visit(component);
  }
 
  return ordered;
}
 
function generateMenuChildrenCode(
  parent: ComponentModel,
  componentMap: Map<string, ComponentModel>,
  allComponents: ComponentModel[],
  methodNames: Map<string, string>,
  generatedIds: Set<string>,
  lines: string[],
): void {
  for (const child of getOrderedChildren(parent, componentMap, allComponents)) {
    if (isMenuComponent(child)) {
      Eif (!generatedIds.has(child.id)) {
        lines.push(`    ${child.variableName} = new JMenu("${escapeJava(child.text)}");`);
        generatedIds.add(child.id);
      }
      generateMenuChildrenCode(
        child,
        componentMap,
        allComponents,
        methodNames,
        generatedIds,
        lines,
      );
      lines.push(`    ${parent.variableName}.add(${child.variableName});`);
      continue;
    }
 
    if (isMenuItemComponent(child)) {
      Eif (!generatedIds.has(child.id)) {
        lines.push(`    ${child.variableName} = new JMenuItem("${escapeJava(child.text)}");`);
        const methodName = methodNames.get(child.id);
        if (methodName) {
          lines.push(`    ${child.variableName}.addActionListener(e -> ${methodName}());`);
        }
        generatedIds.add(child.id);
      }
      lines.push(`    ${parent.variableName}.add(${child.variableName});`);
    }
  }
}
 
function generateMenuBar(
  menuBar: ComponentModel,
  componentMap: Map<string, ComponentModel>,
  allComponents: ComponentModel[],
  methodNames: Map<string, string>,
): string[] {
  const lines: string[] = [`    ${menuBar.variableName} = new JMenuBar();`];
  const generatedIds = new Set<string>([menuBar.id]);
 
  generateMenuChildrenCode(menuBar, componentMap, allComponents, methodNames, generatedIds, lines);
  lines.push(`    frame.setJMenuBar(${menuBar.variableName});`);
  lines.push("");
 
  return lines;
}
 
function generateToolBar(
  toolBar: ComponentModel,
  componentMap: Map<string, ComponentModel>,
  allComponents: ComponentModel[],
  customIds: Set<string>,
  customClassNames: Map<string, string>,
  methodNames: Map<string, string>,
): string[] {
  const orientation =
    toolBar.orientation === "vertical" ? "JToolBar.VERTICAL" : "JToolBar.HORIZONTAL";
  const lines: string[] = [
    `    ${toolBar.variableName} = new JToolBar();`,
    `    ${toolBar.variableName}.setOrientation(${orientation});`,
  ];
  applyInlineStyleCode(lines, toolBar);
 
  for (const child of getOrderedChildren(toolBar, componentMap, allComponents)) {
    const isCustom = customIds.has(child.id);
    if (isCustom) {
      const customClassName = customClassNames.get(child.id) as string;
      lines.push(`    ${child.variableName} = new ${customClassName}();`);
    } else {
      lines.push(getComponentInitCode(child, getComponentSwingType(child)));
      applyInlineStyleCode(lines, child);
    }
 
    lines.push(...getComponentPropsCode(child));
 
    const methodName = methodNames.get(child.id);
    if (methodName) {
      const listenerCode = getListenerCode(child, methodName);
      Eif (listenerCode) {
        lines.push(listenerCode);
      }
    }
 
    lines.push(`    ${toolBar.variableName}.add(${child.variableName});`);
  }
 
  lines.push(
    `    getContentPane().add(${toolBar.variableName}, ${getToolBarBorderPosition(toolBar)});`,
  );
  lines.push("");
 
  return lines;
}
 
function partitionComponentsByGenerationPhase(
  components: ComponentModel[],
  componentMap: Map<string, ComponentModel>,
): {
  menuBars: ComponentModel[];
  regularComponents: ComponentModel[];
  toolBars: ComponentModel[];
} {
  const menuBars = components.filter(isMenuBarComponent);
  const toolBars = components.filter(isToolBarComponent);
 
  const menuComponentIds = new Set<string>();
  for (const menuBar of menuBars) {
    collectDescendantIds(menuBar, componentMap, components, menuComponentIds);
  }
 
  const toolBarChildIds = new Set<string>();
  for (const toolBar of toolBars) {
    for (const child of getOrderedChildren(toolBar, componentMap, components)) {
      toolBarChildIds.add(child.id);
    }
  }
 
  const regularComponents = components.filter(
    (comp) =>
      !menuComponentIds.has(comp.id) && !isToolBarComponent(comp) && !toolBarChildIds.has(comp.id),
  );
 
  return { menuBars, regularComponents, toolBars };
}
 
export interface HierarchicalCodeResult {
  menuBars: ComponentModel[];
  regularComponents: ComponentModel[];
  toolBars: ComponentModel[];
  menuBarLines: string[];
  toolBarLines: string[];
}
 
export function generateHierarchicalCode(
  components: ComponentModel[],
  customIds: Set<string>,
  customClassNames: Map<string, string>,
  methodNames: Map<string, string>,
): HierarchicalCodeResult {
  const componentMap = new Map(components.map((comp) => [comp.id, comp]));
  const { menuBars, regularComponents, toolBars } = partitionComponentsByGenerationPhase(
    components,
    componentMap,
  );
 
  const menuBarLines: string[] = [];
  for (const menuBar of menuBars) {
    menuBarLines.push(...generateMenuBar(menuBar, componentMap, components, methodNames));
  }
 
  const toolBarLines: string[] = [];
  for (const toolBar of toolBars) {
    toolBarLines.push(
      ...generateToolBar(
        toolBar,
        componentMap,
        components,
        customIds,
        customClassNames,
        methodNames,
      ),
    );
  }
 
  return {
    menuBars,
    regularComponents: sortRegularComponentsForGeneration(regularComponents),
    toolBars,
    menuBarLines,
    toolBarLines,
  };
}
 
function resolvePanelParent(
  comp: ComponentModel,
  componentMap: Map<string, ComponentModel>,
): ComponentModel | undefined {
  if (!comp.parentId) {
    return undefined;
  }
 
  const parent = componentMap.get(comp.parentId);
  if (!parent || parent.id === comp.id || !isPanelComponent(parent)) {
    return undefined;
  }
 
  return parent;
}
 
export function generateComponentCode(
  comp: ComponentModel,
  customIds: Set<string>,
  customClassNames: Map<string, string>,
  methodNames: Map<string, string>,
  hasToolBars: boolean,
  componentMap: Map<string, ComponentModel>,
  allComponents: ComponentModel[],
): string[] {
  const lines: string[] = [];
  const isCustom = customIds.has(comp.id);
  const panelParent = resolvePanelParent(comp, componentMap);
  const boundsX = panelParent && comp.parentOffset ? comp.parentOffset.x : comp.x;
  const boundsY = panelParent && comp.parentOffset ? comp.parentOffset.y : comp.y;
 
  if (isCustom) {
    const customClassName = customClassNames.get(comp.id) as string;
    lines.push(`    ${comp.variableName} = new ${customClassName}();`);
  } else {
    lines.push(getComponentInitCode(comp, getComponentSwingType(comp)));
  }
 
  lines.push(
    `    ${comp.variableName}.setBounds(${boundsX}, ${boundsY}, ${comp.width}, ${comp.height});`,
  );
 
  if (hasPanelChildren(comp, allComponents)) {
    lines.push(`    ${comp.variableName}.setLayout(null);`);
  }
 
  if (!isCustom) {
    applyInlineStyleCode(lines, comp);
  }
 
  lines.push(...getComponentPropsCode(comp));
 
  const methodName = methodNames.get(comp.id);
  if (methodName) {
    const listenerCode = getListenerCode(comp, methodName);
    Eif (listenerCode) {
      lines.push(listenerCode);
    }
  }
 
  const addTarget = panelParent?.variableName ?? (hasToolBars ? "canvasPanel" : "this");
  lines.push(`    ${addTarget}.add(${comp.variableName});`);
  lines.push("");
 
  return lines;
}