1/*
2 * Copyright 2024, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {assertDefined} from 'common/assert_utils';
18import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
19import {DiffNode} from './diff_node';
20import {DiffType} from './diff_type';
21
22export class UiPropertyTreeNode extends PropertyTreeNode implements DiffNode {
23  private diff: DiffType = DiffType.NONE;
24  private displayName: string = this.name;
25  private oldValue = 'null';
26
27  static from(node: PropertyTreeNode): UiPropertyTreeNode {
28    const displayNode = new UiPropertyTreeNode(
29      node.id,
30      node.name,
31      node.source,
32      (node as UiPropertyTreeNode).value,
33    );
34    if ((node as UiPropertyTreeNode).formatter) {
35      displayNode.setFormatter(
36        assertDefined((node as UiPropertyTreeNode).formatter),
37      );
38    }
39
40    displayNode.setIsRoot(node.isRoot());
41
42    const children = [...node.getAllChildren()].sort((a, b) =>
43      a.name < b.name ? -1 : 1,
44    );
45
46    children.forEach((child) => {
47      displayNode.addOrReplaceChild(UiPropertyTreeNode.from(child));
48    });
49    return displayNode;
50  }
51
52  setDiff(diff: DiffType): void {
53    this.diff = diff;
54  }
55
56  getDiff(): DiffType {
57    return this.diff;
58  }
59
60  setDisplayName(name: string) {
61    this.displayName = name;
62  }
63
64  getDisplayName(): string {
65    return this.displayName;
66  }
67
68  setOldValue(value: string) {
69    this.oldValue = value;
70  }
71
72  getOldValue(): string {
73    return this.oldValue;
74  }
75}
76