|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2011, 2025 Wind River Systems, Inc. and others. All rights reserved. |
| 3 | + * This program and the accompanying materials are made available under the terms |
| 4 | + * of the Eclipse Public License 2.0 which accompanies this distribution, and is |
| 5 | + * available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Wind River Systems - initial API and implementation |
| 11 | + * Max Weninger (Wind River) - [361363] [TERMINALS] Implement "Pin&Clone" for the "Terminals" view |
| 12 | + * Alexander Fedorov (ArSysOp) - further evolution |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.terminal.view.ui; |
| 15 | + |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import org.eclipse.ui.IViewReference; |
| 24 | +import org.eclipse.ui.IWorkbench; |
| 25 | +import org.eclipse.ui.IWorkbenchPage; |
| 26 | +import org.eclipse.ui.IWorkbenchWindow; |
| 27 | +import org.eclipse.ui.PlatformUI; |
| 28 | + |
| 29 | +/** |
| 30 | + * Encapsulates the pair of mandatory primary terminal view id and optional secondary id |
| 31 | + * |
| 32 | + * @see IUIConstants#ID |
| 33 | + * @see IViewReference#getSecondaryId() |
| 34 | + */ |
| 35 | +public record TerminalViewId(String primary, Optional<String> secondary) { |
| 36 | + |
| 37 | + public TerminalViewId() { |
| 38 | + this(IUIConstants.ID); |
| 39 | + } |
| 40 | + |
| 41 | + public TerminalViewId(String primary) { |
| 42 | + this(primary, Optional.empty()); |
| 43 | + } |
| 44 | + |
| 45 | + public TerminalViewId(String primary, String secondary) { |
| 46 | + this(primary, Optional.ofNullable(secondary)); |
| 47 | + } |
| 48 | + |
| 49 | + public TerminalViewId next() { |
| 50 | + return new TerminalViewId(primary, nextSecondaryId(primary)); |
| 51 | + } |
| 52 | + |
| 53 | + private static String nextSecondaryId(String primary) { |
| 54 | + Map<String, IViewReference> terminalViews = new HashMap<>(); |
| 55 | + int maxNumber = 0; |
| 56 | + for (IViewReference ref : viewReferences()) { |
| 57 | + if (ref.getId().equals(primary)) { |
| 58 | + if (ref.getSecondaryId() != null) { |
| 59 | + terminalViews.put(ref.getSecondaryId(), ref); |
| 60 | + int scondaryIdInt = Integer.parseInt(ref.getSecondaryId()); |
| 61 | + if (scondaryIdInt > maxNumber) { |
| 62 | + maxNumber = scondaryIdInt; |
| 63 | + } |
| 64 | + } else { |
| 65 | + // add the one with secondaryId == null with 0 by default |
| 66 | + terminalViews.put(Integer.toString(0), ref); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + if (terminalViews.size() == 0) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + int i = 0; |
| 75 | + for (; i < maxNumber; i++) { |
| 76 | + String secondaryIdStr = Integer.toString(i); |
| 77 | + if (!terminalViews.keySet().contains(secondaryIdStr)) { |
| 78 | + // found a free slot |
| 79 | + if (i == 0) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + return Integer.toString(i); |
| 83 | + } |
| 84 | + } |
| 85 | + // add a new one |
| 86 | + return Integer.toString(i + 1); |
| 87 | + } |
| 88 | + |
| 89 | + private static List<IViewReference> viewReferences() { |
| 90 | + if (!PlatformUI.isWorkbenchRunning()) { |
| 91 | + return Collections.emptyList(); |
| 92 | + } |
| 93 | + return Optional.of(PlatformUI.getWorkbench()).map(IWorkbench::getActiveWorkbenchWindow) |
| 94 | + .map(IWorkbenchWindow::getActivePage).map(IWorkbenchPage::getViewReferences).map(Arrays::asList) |
| 95 | + .orElseGet(Collections::emptyList); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments