| /*=============================================================================# |
| # Copyright (c) 2013, 2022 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.rj.server.gr; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.Comparator; |
| import java.util.List; |
| |
| import org.eclipse.statet.rj.server.GDCmdItem; |
| import org.eclipse.statet.rj.server.RJ; |
| |
| |
| public class RjsGraphicManager { |
| |
| |
| private static final Comparator<RjsGraphic> GRAPHIC_COMPARATOR= new Comparator<>() { |
| |
| @Override |
| public int compare(final RjsGraphic o1, final RjsGraphic o2) { |
| return o1.getDevId() - o2.getDevId(); |
| } |
| |
| }; |
| |
| |
| private final List<RjsGraphic> graphicList= new ArrayList<>(); |
| |
| private RjsGraphic activeGraphic; |
| |
| private int inOperation; |
| private RjsGraphic inOperationActiveGraphic; |
| |
| private final RJ rj; |
| |
| |
| public RjsGraphicManager(final RJ rj) { |
| this.rj= rj; |
| } |
| |
| |
| void registerGraphic(final RjsGraphic graphic) { |
| final int idx= Collections.binarySearch(this.graphicList, graphic, GRAPHIC_COMPARATOR); |
| if (idx >= 0) { |
| this.graphicList.set(idx, graphic); |
| } |
| else { |
| this.graphicList.add(-(idx+1), graphic); |
| } |
| } |
| |
| void unregisterGraphic(final RjsGraphic graphic) { |
| if (this.activeGraphic == graphic) { |
| this.activeGraphic= null; |
| } |
| if (this.inOperationActiveGraphic == graphic) { |
| this.inOperationActiveGraphic= null; |
| } |
| this.graphicList.remove(graphic); |
| } |
| |
| void activate(final RjsGraphic graphic) { |
| this.activeGraphic= graphic; |
| if (this.inOperation == 0) { |
| this.rj.sendMainCmd(new GDCmdItem.CSetActiveOn( |
| graphic.getDevId(), graphic.getSlot() )); |
| } |
| } |
| |
| void deactivate(final RjsGraphic graphic) { |
| if (this.activeGraphic == graphic) { |
| this.activeGraphic= null; |
| } |
| if (this.inOperation == 0) { |
| this.rj.sendMainCmd(new GDCmdItem.CSetActiveOff( |
| graphic.getDevId(), graphic.getSlot() )); |
| } |
| } |
| |
| |
| public RjsGraphic getGraphic(final int devId) { |
| for (int i= 0; i < this.graphicList.size(); i++) { |
| final RjsGraphic graphic= this.graphicList.get(i); |
| if (graphic.getDevId() < devId) { |
| continue; |
| } |
| else if (graphic.getDevId() > devId) { |
| break; |
| } |
| else { |
| return graphic; |
| } |
| } |
| return null; |
| } |
| |
| |
| protected void beginOperation() { |
| if (this.inOperation == 0) { |
| this.inOperationActiveGraphic= this.activeGraphic; |
| } |
| this.inOperation++; |
| } |
| |
| protected void endOperation() { |
| this.inOperation--; |
| if (this.inOperation == 0) { |
| if (this.inOperationActiveGraphic != this.activeGraphic) { |
| if (this.inOperationActiveGraphic != null) { |
| this.rj.sendMainCmd(new GDCmdItem.CSetActiveOff( |
| this.inOperationActiveGraphic.getDevId(), this.inOperationActiveGraphic.getSlot() )); |
| } |
| if (this.activeGraphic != null) { |
| this.rj.sendMainCmd(new GDCmdItem.CSetActiveOn( |
| this.activeGraphic.getDevId(), this.activeGraphic.getSlot() )); |
| } |
| } |
| this.inOperationActiveGraphic= null; |
| } |
| } |
| |
| } |