/******************************************************************************* * Copyright (c) 2015-2026 MINRES Technologies GmbH and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * MINRES Technologies GmbH - initial API and implementation *******************************************************************************/ package com.minres.scviewer.database.ui; import java.util.ArrayList; import java.util.List; import com.minres.scviewer.database.EmptyWaveform; import com.minres.scviewer.database.IWaveform; public class TrackEntry { public enum HierState { NONE, CLOSED, OPENED } public enum ValueDisplay { DEFAULT, BINARY, SIGNED, UNSIGNED } public enum WaveDisplay { DEFAULT, STEP_WISE, CONTINOUS } IWaveformStyleProvider styleProvider = null; final public IWaveform waveform; public int vOffset=0; public int height=0; public boolean selected=false; public HierState hierState=HierState.NONE; public List waveforms = new ArrayList<>(); public String currentValue=""; public ValueDisplay valueDisplay = ValueDisplay.DEFAULT; public WaveDisplay waveDisplay = WaveDisplay.DEFAULT; public TrackEntry() { this.waveform = null; this.styleProvider=null; } public TrackEntry(IWaveform waveform, IWaveformStyleProvider styleProvider) { this.waveform = waveform; this.styleProvider=styleProvider; this.hierState = (waveform!=null && waveform.getChildNodes().size()>0)?HierState.CLOSED:HierState.NONE; } public TrackEntry(TrackEntry[] waveform, IWaveformStyleProvider styleProvider) { this(new EmptyWaveform(), styleProvider); this.hierState = HierState.CLOSED; for (TrackEntry iWaveform : waveform) { waveforms.add(iWaveform); } } /** A group retains its header when expanded. Children are ordinary tracks or groups. */ public static TrackEntry group(String name, List children, IWaveformStyleProvider styles) { TrackEntry group = new TrackEntry(children.toArray(new TrackEntry[0]), styles); group.waveform.setName(name); group.hierState = HierState.OPENED; return group; } public boolean isGroup() { return waveform instanceof EmptyWaveform && hierState != HierState.NONE; } /** Returns the actual sibling list owning this entry, including collapsed groups. */ public static List siblingsOf(List roots, TrackEntry track) { if (roots.stream().anyMatch(entry -> entry == track)) return roots; for (TrackEntry entry : roots) { if (!entry.isGroup()) continue; List siblings = siblingsOf(entry.waveforms, track); if (siblings != null) return siblings; } return null; } /** Group only siblings, so selecting a group and its descendant cannot create a cycle. */ public static List groupingSiblings(List roots, List selected) { if (selected.isEmpty()) return null; List siblings = siblingsOf(roots, selected.get(0)); return siblings != null && selected.stream().allMatch(track -> siblingsOf(roots, track) == siblings) ? siblings : null; } public static TrackEntry groupTracks(List roots, List selected, String name, IWaveformStyleProvider styles) { List siblings = groupingSiblings(roots, selected); if (siblings == null) return null; List members = siblings.stream() .filter(entry -> selected.stream().anyMatch(track -> track == entry)).toList(); int index = siblings.indexOf(members.get(0)); TrackEntry group = group(name, members, styles); siblings.removeAll(members); siblings.add(index, group); return group; } public static boolean ungroupTracks(List roots, TrackEntry group) { List siblings = siblingsOf(roots, group); if (!group.isGroup() || siblings == null) return false; int index = siblings.indexOf(group); siblings.remove(index); siblings.addAll(index, group.waveforms); return true; } /** Remove selected entries from their owning lists, leaving empty group headers intact. */ public static void removeTracks(List roots, List selected) { for (TrackEntry track : new ArrayList<>(selected)) { track.selected = false; List siblings = siblingsOf(roots, track); if (siblings == null) continue; for (int index = 0; index < siblings.size(); index++) { if (siblings.get(index) == track) { siblings.remove(index); break; } } } } public static List visibleTracks(List roots) { List result = new ArrayList<>(); for (TrackEntry entry : roots) { result.add(entry); if (entry.hierState == HierState.OPENED) result.addAll(visibleTracks(entry.waveforms)); } return result; } @Override public boolean equals(Object obj) { if(obj instanceof TrackEntry){ TrackEntry o = (TrackEntry) obj; return waveform==o.waveform && vOffset==o.vOffset; } return false; } }