]> git.basschouten.com Git - openhab-addons.git/blob
2c3db6be1e5685bee935d80b4dbc91a83ba35698
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nanoleaf.internal.model;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.TreeMap;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * Represents layout of the light panels
24  *
25  * @author Martin Raepple - Initial contribution
26  */
27 @NonNullByDefault
28 public class Layout {
29
30     private int numPanels;
31     private int sideLength;
32
33     private @Nullable List<PositionDatum> positionData = null;
34
35     public int getNumPanels() {
36         return numPanels;
37     }
38
39     public void setNumPanels(int numPanels) {
40         this.numPanels = numPanels;
41     }
42
43     public int getSideLength() {
44         return sideLength;
45     }
46
47     public void setSideLength(int sideLength) {
48         this.sideLength = sideLength;
49     }
50
51     public @Nullable List<PositionDatum> getPositionData() {
52         return positionData;
53     }
54
55     public void setPositionData(List<PositionDatum> positionData) {
56         this.positionData = positionData;
57     }
58
59     /**
60      * Returns an text representation for a canvas layout.
61      *
62      * Note only canvas supported currently due to its easy geometry
63      *
64      * @return a String containing the layout
65      */
66     public String getLayoutView() {
67         if (positionData != null) {
68             String view = "";
69
70             int minx = Integer.MAX_VALUE;
71             int maxx = Integer.MIN_VALUE;
72             int miny = Integer.MAX_VALUE;
73             int maxy = Integer.MIN_VALUE;
74
75             final int noofDefinedPanels = positionData.size();
76             for (int index = 0; index < noofDefinedPanels; index++) {
77                 if (positionData != null) {
78                     @Nullable
79                     PositionDatum panel = positionData.get(index);
80
81                     if (panel != null) {
82                         if (panel.getPosX() < minx) {
83                             minx = panel.getPosX();
84                         }
85                         if (panel.getPosX() > maxx) {
86                             maxx = panel.getPosX();
87                         }
88                         if (panel.getPosY() < miny) {
89                             miny = panel.getPosY();
90                         }
91                         if (panel.getPosY() > maxy) {
92                             maxy = panel.getPosY();
93                         }
94                     }
95                 }
96             }
97
98             int shiftWidth = getSideLength() / 2;
99
100             if (shiftWidth == 0) {
101                 // seems we do not have squares here
102                 return "Cannot render layout. Please note that layout views are only supported for square panels.";
103             }
104
105             int lineY = maxy;
106             Map<Integer, PositionDatum> map;
107
108             while (lineY >= miny) {
109                 map = new TreeMap<>();
110                 for (int index = 0; index < noofDefinedPanels; index++) {
111
112                     if (positionData != null) {
113                         @Nullable
114                         PositionDatum panel = positionData.get(index);
115
116                         if (panel != null && panel.getPosY() == lineY) {
117                             map.put(panel.getPosX(), panel);
118                         }
119                     }
120                 }
121                 lineY -= shiftWidth;
122                 for (int x = minx; x <= maxx; x += shiftWidth) {
123                     if (map.containsKey(x)) {
124                         @Nullable
125                         PositionDatum panel = map.get(x);
126                         view += String.format("%5s ", panel.getPanelId());
127                     } else {
128                         view += "      ";
129                     }
130                 }
131                 view += System.lineSeparator();
132             }
133
134             return view;
135         } else {
136             return "";
137         }
138     }
139 }