]> git.basschouten.com Git - openhab-addons.git/blob
f1fa9d29deb9df37d58eab2868c371ee537a87b4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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             int lineY = maxy;
101             Map<Integer, PositionDatum> map;
102
103             while (lineY >= miny) {
104                 map = new TreeMap<>();
105                 for (int index = 0; index < noofDefinedPanels; index++) {
106
107                     if (positionData != null) {
108                         @Nullable
109                         PositionDatum panel = positionData.get(index);
110
111                         if (panel != null && panel.getPosY() == lineY)
112                             map.put(panel.getPosX(), panel);
113                     }
114                 }
115                 lineY -= shiftWidth;
116                 for (int x = minx; x <= maxx; x += shiftWidth) {
117                     if (map.containsKey(x)) {
118                         @Nullable
119                         PositionDatum panel = map.get(x);
120                         view += String.format("%5s ", panel.getPanelId());
121                     } else
122                         view += "      ";
123                 }
124                 view += "\n";
125             }
126
127             return view;
128         } else
129             return "";
130     }
131 }