Alejandro Acuña
2024-10-25 e51f4a713ed6e744c203c9493165584728a29c52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package art.servers.colorsserver.M.gui.diagrams;
 
import art.library.gui.FlatGUI;
import art.library.model.devices.colors.controller.M.M_ControllerConfiguration;
import art.library.model.devices.colors.controller.M.configuration.M_TrafficGroup;
import art.library.gui.flat.FlatButton;
import art.library.gui.flat.FlatPanel;
import art.library.gui.flat.FlatTableInput;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
 
 
 
public class DiagramConflicts extends FlatPanel
{
 
    private boolean editable = false;
    private M_ControllerConfiguration m_configuration = null;
    private FlatTableInput table1 = null; 
    private Color color = new FlatPanel().getLook().background;
 
    public DiagramConflicts (M_ControllerConfiguration m_configuration)
    {
         setConfiguration(m_configuration);
    }
    
     
    public void setConfiguration(M_ControllerConfiguration m_configuration)
    {
        this.m_configuration = m_configuration;
        this.removeAll();
        this.setLayout(new BorderLayout());
        this.add(new DiagramColumns(), BorderLayout.NORTH);
        this.add(new DiagramRows(), BorderLayout.WEST);
        this.add(new Diagram(), BorderLayout.CENTER);
        
    }
     
    
 
    public void editable(boolean editable)
    {
         this.editable = editable;
    }
    
    
    
 
    
    
    private class DiagramColumns extends FlatButton
    {
        private DiagramColumns()
        {
            setPreferredSize(new Dimension(0,22));
        }
        
        
        public void paint (Graphics g)
        {
            try
            {
                Graphics2D g2 = (Graphics2D)g;
                int w = getWidth();
                int h = getHeight();
                g2.setBackground(color);
                g2.clearRect(0, 0, w, h);
                Font font = new FlatButton().getLook().font;
                g2.setColor(new FlatButton().getLook().colorForeground);
                
                double dx =  (double)(w - 48) / ((double)m_configuration.ltrafficGroup.size()); 
                dx = Math.min(64, dx);
                        
                for (int i=0; i<m_configuration.ltrafficGroup.size(); i++)
                {
                    drawCenteredString(g2, "" + m_configuration.ltrafficGroup.get(i).number, new Rectangle2D.Double((dx * i) + 48, 0, dx, h), font);
                }
                
                g2.setColor(new FlatButton().getLook().colorBackground.darker());
                
                for (int i=0; i<=m_configuration.ltrafficGroup.size(); i++)
                {
                    g2.draw(new Line2D.Double((dx * i) + 48, 0, (dx * i) + 48, h));
                }
            }
            catch (Exception e)
            {
            }
        }
    }
    
    
    
    
 
    private class DiagramRows extends FlatButton
    {
        private DiagramRows()
        {
            setPreferredSize(new Dimension(48,0));
        }
        
        
        public void paint (Graphics g)
        {
            try
            {
                Graphics2D g2 = (Graphics2D)g;
                int w = getWidth();
                int h = getHeight();
                g2.setBackground(color);
                g2.clearRect(0, 0, w, h);
                Font font = new FlatButton().getLook().font;
                g2.setColor(new FlatButton().getLook().colorForeground);
                
                double dy =  (double)h / (double)m_configuration.ltrafficGroup.size(); 
                dy = Math.min(64, dy);
                        
                for (int i=0; i<m_configuration.ltrafficGroup.size(); i++)
                {
                    drawCenteredString(g2, "" + m_configuration.ltrafficGroup.get(i).number, new Rectangle2D.Double(0, dy * i, w, dy), font);
                }
                
                g2.setColor(new FlatButton().getLook().colorBackground.darker());
                
                for (int i=0; i<=m_configuration.ltrafficGroup.size(); i++)
                {
                    g2.draw(new Line2D.Double(0, dy * i,  w, dy * i));
                }
                
            }
            catch (Exception e)
            {
            }
        }
    }
    
    
    
    
    
    private class Diagram extends FlatButton
    {
        private int currenti = -1;
        private int currentj = -1;
        
        public Diagram ()
        {
            this.addMouseListener(new java.awt.event.MouseAdapter() 
            {
                public void mousePressed(java.awt.event.MouseEvent evt) 
                {
                    if (editable == true) selection(evt.getX(), evt.getY(), false);
                }
                public void mouseExited(java.awt.event.MouseEvent evt) 
                {
                    currenti = -1;
                    currentj = -1;
                    repaint();
                }
            });            
            
            this.addMouseMotionListener(new java.awt.event.MouseAdapter() 
            {
                public void mouseDragged(java.awt.event.MouseEvent evt) 
                {
                    if (editable == true) selection(evt.getX(), evt.getY(), true);
                }
                public void mouseMoved(java.awt.event.MouseEvent evt) 
                {
                    movement(evt.getX(), evt.getY());
                }
            });            
        }
        
        
        public void paint (Graphics g)
        {
            try
            {
                Graphics2D g2 = (Graphics2D)g;
                int w = getWidth();
                int h = getHeight();
                g2.setBackground(FlatGUI.lookDesign.getColor("window.color.background.content"));
                
                double dx =  (double)w / ((double)m_configuration.ltrafficGroup.size()); 
                double dy =  (double)h / (double)m_configuration.ltrafficGroup.size(); 
 
                dx = Math.min(64, dx);
                dy = Math.min(64, dy);
                g2.clearRect(0, 0, (int)(dx * m_configuration.ltrafficGroup.size()), (int)(dy * m_configuration.ltrafficGroup.size()));
                                
                
                g2.setColor(new FlatButton().getLook().colorBackground);
                
                for (int i=0; i<m_configuration.ltrafficGroup.size(); i++)
                {
                    g2.fill(new Rectangle2D.Double(dx * i, dy * i, dx, dy));
                }
 
                
                g2.setColor(Color.red);
                
                for (M_TrafficGroup group1 :  m_configuration.ltrafficGroup)
                {
                    for (Integer group2 : group1.lincompatibility)
                    {
                        int i = group1.number - 1;
                        int j = group2.intValue() - 1;
                        g2.fill(new Rectangle2D.Double(dx * i, dy * j, dx, dy));
                    }
                }
                
 
                g2.setColor(new FlatButton().getLook().colorBackground.darker());
                
                for (int i=0; i<=m_configuration.ltrafficGroup.size(); i++)
                {
                    g2.draw(new Line2D.Double(dx * i, 0, dx * i, dy * m_configuration.ltrafficGroup.size()));
                    g2.draw(new Line2D.Double(0, dy * i, dx * m_configuration.ltrafficGroup.size(), dy * i));
                }
                
                g2.setStroke(new BasicStroke(1.0f));
                
                if (editable == true)
                {
                    g2.setColor(Color.black);
                    g2.draw(new Rectangle2D.Double(dx * (currenti - 1), dy * (currentj - 1), dx, dy));
                }
                
                g2.setColor(new FlatButton().getLook().colorBackground.darker());
                g2.draw(new Rectangle2D.Double(0,0,dx * m_configuration.ltrafficGroup.size(),dy * m_configuration.ltrafficGroup.size()));
                
            }
            catch (Exception e)
            {
            }
        }
        
        
        private int lasti = -1;
        private int lastj = -1;
        
        private void selection (int x, int y, boolean dragging)
        {
            try
            {
                double dx =  (double)getWidth() / ((double)m_configuration.ltrafficGroup.size()); 
                double dy =  (double)getHeight() / (double)m_configuration.ltrafficGroup.size(); 
                dx = Math.min(64, dx);
                dy = Math.min(64, dy);
                
                int i = (int)((double)x / dx) + 1;
                int j = (int)((double)y / dy) + 1;
                if (i == j) return;
                currenti = i;
                currentj = j;
 
                if ((i != j) && ((lasti != i) || (lastj != j)) || (dragging == false))
                {
                    reverseIncompatibility(m_configuration.getTrafficGroup(i), m_configuration.getTrafficGroup(j));
                    lasti = i;
                    lastj = j;
                }
 
                repaint();
            }
            catch (Exception e)
            {
            }
        }
        
 
        private void movement (int x, int y)
        {
            try
            {
                double dx =  (double)getWidth() / ((double)m_configuration.ltrafficGroup.size()); 
                double dy =  (double)getHeight() / (double)m_configuration.ltrafficGroup.size(); 
                dx = Math.min(64, dx);
                dy = Math.min(64, dy);
                currenti = (int)((double)x / dx) + 1;
                currentj = (int)((double)y / dy) + 1;
                if (currenti > m_configuration.ltrafficGroup.size()) currenti = -1;
                if (currentj > m_configuration.ltrafficGroup.size()) currentj = -1;
                repaint();
            }
            catch (Exception e)
            {
            }
        }        
    }
    
 
    
    
 
 
    
    
    private void drawCenteredString(Graphics2D g2, String text, Rectangle2D rect, Font font) 
    {
        FontMetrics metrics = g2.getFontMetrics(font);
        double x = rect.getX() + (rect.getWidth() - metrics.stringWidth(text)) / 2;
        double y = rect.getY() + ((rect.getHeight() - metrics.getHeight()) / 2) + metrics.getAscent();
        g2.setFont(font);
        g2.drawString(text, (float)x, (float)y);
    }        
    
    
    
    private void reverseIncompatibility(M_TrafficGroup group1, M_TrafficGroup group2)
    {
        for (Integer group : group1.lincompatibility)
        {
           if (group.intValue() == group2.number)
           {
               group1.lincompatibility.remove(group);
               group2.lincompatibility.remove(new Integer(group1.number));
               return;
           }
        }
 
        
        group1.lincompatibility.add(new Integer(group2.number));
        group2.lincompatibility.add(new Integer(group1.number));
    }       
 
}