Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
15 arune 1
/*
2
 * File created by arune
3
 * at 2004-jan-06 13:30:00
4
 */
5
package Macbeth.Modules.modSmallDisplays;
6
 
7
import Macbeth.System.MbPacket;
8
 
9
import java.util.Vector;
10
 
11
 
12
/**
13
 * A representation of one of the displays on the arne-network,
14
 * it also handles everything that can be done with it.
15
 * @author arune
16
 */
17
public class DisplayObject {
18
 
19
    //todo: vore trevligt ifall displayen scrollade text om den var längre än displayens bredd (men max 40 tecken)
20
    //ska detta göras i macbeth? helst inte, se om man kan få scroll i avren
21
    //om det går i avren så får man i displaymodulen fixa så man inte klipper ner rader till displayens bredd
22
    private final int ARNEMAXPACKETSIZE = 15;
23
    // place what type of display this is, location ...
24
    private String sInfo;
25
    // arne name for this display
26
    private String sTarget;
27
    // The number of columns of the display
28
    private int iWidth;
29
    // The number of rows of the display
30
    private int iHeight;
31
    // Holds the to-be contents of the display, sNewContents[0] is top-row, sNewContents[iHeight-1] is bottom-row
32
    private String[] sNewContents;
33
    // Holds the CURRENT contents of the display
34
    private String[] sCurrContents;
35
 
36
    private static final int LCDSETADDRESS = 128;
37
    private static final int LCDTEXTWRITE = 129;
38
    private static final int LCDNULL = 160;
39
    private static final int LCDCLEAR = 130;
40
    private static final int LCDSETBL = 164;
41
    private static final int LCDFADEBL = 165;
42
    private static final int LCDFADESPEED = 166;
43
    private static final int LCDCGRAMWRITE = 166;
44
 
45
    //queue-vectors
46
    private Vector rowPrio0 = new Vector();
47
    private Vector rowPrio1 = new Vector();
48
    private Vector rowPrio2 = new Vector();
49
    //queue-objects gets moved into this vector when placed on display
50
    private Vector rowsCurrent = new Vector();
51
 
52
    private Vector freeRows = new Vector();
53
    //private Vector takenRows = new Vector();
54
 
55
    //stores an addresslayout of the display
56
    private Vector rowAddresses = new Vector();
57
    //stores a list of what modules that sends data to this display
58
    private Vector moduleUsers = new Vector();
59
 
60
    /**
61
     * Creates a new instance of DisplayObject.
62
     * @param target arne name for this display.
63
     * @param info The name of this object, what type of display this is, location, and so on.
64
     * @param width The number of columns of the display.
65
     * @param height The number of rows of the display.
66
     * @param rows A list of first-position-addresses.
67
     * @param users A list of modules that sends data to this display.
68
     */
69
    public DisplayObject(String target, String info, int width, int height, Vector rows, Vector users) {
70
        sInfo = info;
71
        sTarget = target;
72
        iWidth = width;
73
        iHeight = height;
74
        rowAddresses = rows;
75
        moduleUsers = users;
76
 
77
        //initiate our contentsholders
78
        if (iHeight > 0) {
79
            sNewContents = new String[iHeight];
80
            sCurrContents = new String[iHeight];
81
            for (int i = 0; i < iHeight; i++) {
82
                //kor rowFixer for att fixa radernas längd direkt
83
                sNewContents[i] = rowFixer("");
84
                sCurrContents[i] = rowFixer("............................................");
85
 
86
            }
87
        }
88
        //todo: initiera rowsCurrent
89
 
90
        for (int i = 0; i < iHeight; i++) {
91
            RowObject dummyRowObject = new RowObject();
92
            dummyRowObject.rowNumber = i;
93
            dummyRowObject.TTL = 0;
94
            //System.out.println("init rowelement: " + i);
95
            if (i == 0) {
96
                dummyRowObject.row = "-Macbeth-";
97
                writeRow(dummyRowObject.rowNumber, dummyRowObject.row);
98
            } else if (i==1) {
99
                dummyRowObject.row = "Booting up";
100
                writeRow(dummyRowObject.rowNumber, dummyRowObject.row);
101
            }
102
            rowsCurrent.add(dummyRowObject);
103
            //... ???
104
 
105
 
106
        }
107
 
108
    }
109
 
110
    /**
111
     *
112
     * @return the number of rows on this display.
113
     */
114
    public int getRows() {
115
        return iHeight;
116
    }
117
    /**
118
     * For testing if this display should show data from modulename.
119
     * @param modName A modulename to test.
120
     * @return true if modulename exists in this displays lists of modulenames.
121
     */
122
    public boolean displayModuleData(String modName) {
123
        for (int j = 0; j < moduleUsers.size(); j++) {     //sok igenom alla modulnamn
124
            if (((String)moduleUsers.elementAt(j)).equals(modName)) {
125
                return true;
126
            }
127
        }
128
        return false;
129
    }
130
    /**
131
     *
132
     * @return the number of columns on this display.
133
     */
134
    public int getCols() {
135
        return iWidth;
136
    }
137
 
138
    /**
139
     * Write text at specified pos, row=0 and col=0 is topleftmost char on display.
140
     * @param row Specifies row to write.
141
     * @param col Specifies what column to start text.
142
     * @param newText The text to write.
143
     */
144
    public void write(int row, int col, String newText) {
145
        if ((row < iWidth) && (col < iHeight)) {
146
            sNewContents[row] = rowFixer(sNewContents[row].substring(0,col) + newText);
147
        }
148
    }
149
 
150
    /**
151
     * Write a new line.
152
     * @param row What row to write.
153
     * @param newText What text to write.
154
     */
155
    public void writeRow(int row, String newText) {
156
        sNewContents[row] = rowFixer(newText);
157
 
158
    }
159
    /**
160
     * Write an empty line / clear a line.
161
     * @param row What row to clear.
162
     */
163
    public void clearRow(int row) {
164
        this.writeRow(row, "");
165
    }
166
 
167
    /**
168
     * Write a new line at the bottom.
169
     * @param newText What text to write.
170
     */
171
    public void writeLastRow(String newText) {
172
        this.writeRow(iHeight-1, newText);
173
    }
174
 
175
    /**
176
     * Write a new line at the top.
177
     * @param newText What text to write.
178
     */
179
    public void writeFirstRow(String newText) {
180
        this.writeRow(0, newText);
181
    }
182
 
183
 
184
    /**
185
     * Move text on row rowToMove left nrOfCols steps.
186
     * @param rowToMove What row to move.
187
     * @param nrOfCols How many columns to move text on row.
188
     */
189
    public void shiftLeft(int rowToMove, int nrOfCols) {
190
        if (nrOfCols > 0 && nrOfCols < iWidth) {
191
            sNewContents[rowToMove] = rowFixer(sNewContents[rowToMove].substring(nrOfCols));
192
        }
193
    }
194
 
195
    /**
196
     * Move text on row rowToMove left, place newText there (move current text so newText fits).
197
     * @param rowToMove What row to move.
198
     * @param newText What text to write.
199
     */
200
    public void shiftLeft(int rowToMove, String newText) {
201
        if (newText.length() > 0 && newText.length() < iWidth) {
202
            sNewContents[rowToMove] = rowFixer(sNewContents[rowToMove].substring(newText.length()) + newText);
203
        }
204
    }
205
 
206
    /**
207
     * Move text on row rowToMove right nrOfCols steps.
208
     * @param rowToMove What row to move.
209
     * @param nrOfCols How many columns to move text on row.
210
     */
211
    public void shiftRight(int rowToMove, int nrOfCols) {
212
        if (nrOfCols > 0 && nrOfCols < iWidth) {
213
            String sDummy = "";
214
            for (int i = 0; i < nrOfCols; i++) {
215
                sDummy = sDummy + " ";
216
            }
217
            sNewContents[rowToMove] = rowFixer(sDummy + sNewContents[rowToMove]);
218
        }
219
    }
220
 
221
    /**
222
     * Move text on row rowToMove right, place newText there (move current text so newText fits).
223
     * @param rowToMove What row to move.
224
     * @param newText What text to write.
225
     */
226
    public void shiftRight(int rowToMove, String newText) {
227
        if (rowToMove < iHeight) {
228
            sNewContents[rowToMove] = rowFixer(newText + sNewContents[rowToMove]);
229
        }
230
    }
231
 
232
    /**
233
     * Move all rows up, nrOfRows step.
234
     * @param nrOfRows The number of rows to scroll.
235
     */
236
    public void scrollUp(int nrOfRows) {
237
        if (nrOfRows > 0 && iHeight > nrOfRows) {
238
            for (int i = 0; i < iHeight; i++) {
239
                sNewContents[i] = sNewContents[i+nrOfRows];
240
            }
241
            for (int i = iHeight - 1; i >= iHeight - nrOfRows; i--) {
242
                sNewContents[i] = rowFixer("");
243
            }
244
        } else if (nrOfRows > 0) {
245
            sNewContents[iHeight - 1] = rowFixer("");
246
        }
247
    }
248
 
249
    /**
250
     * Just scroll one row, and specify the new row.
251
     * @param newRow What text to write on new row.
252
     */
253
    public void scrollUp(String newRow) {
254
        if (iHeight > 1) {
255
            for (int i = 0; i < iHeight; i++) {
256
                sNewContents[i] = sNewContents[i+1];
257
            }
258
            sNewContents[iHeight - 1] = rowFixer(newRow);
259
        }
260
    }
261
 
262
    /**
263
     * Move all rows down, nrOfRows step.
264
     * @param nrOfRows The number of rows to scroll.
265
     */
266
    public void scrollDown(int nrOfRows) {
267
 
268
    }
269
 
270
    /**
271
     * Just scroll one row, and specify the new row.
272
     * @param newRow What text to write on new row.
273
     */
274
    public void scrollDown(String newRow) {
275
        if (iHeight > 1) {
276
            for (int i = iHeight - 1; i > 0; i--) {
277
                sNewContents[i] = sNewContents[i-1];
278
            }
279
            sNewContents[0] = rowFixer(newRow);
280
        }
281
    }
282
 
283
    /**
284
     * This method compares the current data on the display and the
285
     * "virtual" display, it makes packages of the data, just to be
286
     * transmitted to display. It is efficient and doesn't send text
287
     * if its already on display.
288
     * //todo: effektivisera genom att kolla om addresseringen (och ev null) far plats i aktuellt paket.
289
     * @return a vector with MBPackets, ready to be transmitted.
290
     */
291
    public Vector getDataToSend() {
292
        //first place data in strings as packages
293
        Vector sVector = new Vector();
294
        //current package, gets stored away in sVector when filled
295
        String sCurrentPacket = "";
296
 
297
        //newPacket just says that address&CO should be written if true
298
        boolean newPacket = true;
299
        char cTemp = 0;
300
        //for each row on display
301
        for (int row = 0; row < iHeight; row++) {
302
            String sNewDum = sNewContents[row];
303
            String sCurDum = sCurrContents[row];
304
            String delayedChars = "";
305
            newPacket = true;           //for each row, address should be written to packet
306
 
307
            //for each column on display
308
            for (int col = 0; col < iWidth; col++) {
309
                if (sNewDum.charAt(col) == sCurDum.charAt(col)) {
310
                    if (!newPacket) {
311
                        delayedChars = delayedChars + sNewDum.charAt(col);
312
                    }
313
                } else {                //if chars at column differed
314
                    if (!newPacket) {       //if packet already started
315
                        if (delayedChars.length() <= 4) {
316
                            if (sCurrentPacket.length() > ARNEMAXPACKETSIZE - 1 - delayedChars.length()) {
317
                                //if all delayedChars does not fit, create a new packet, without delayedChars
318
                                String dummyString = sCurrentPacket;
319
                                sVector.addElement(dummyString);
320
                                sCurrentPacket = "";
321
                                //write setadress
322
                                cTemp = LCDSETADDRESS;
323
                                sCurrentPacket = sCurrentPacket + cTemp;
324
                                //write the address   (rowAddresses.elementAt(row) + col  ???)
325
                                cTemp = (char)(Integer.parseInt((String)rowAddresses.elementAt(row)) + col);
326
                                sCurrentPacket = sCurrentPacket + cTemp;
327
                                //write settext
328
                                cTemp = LCDTEXTWRITE;
329
                                sCurrentPacket = sCurrentPacket + cTemp;
330
                            } else {    //if delayedchars and a new char fits in packet then
331
                                //add delayedchars to packet
332
                                sCurrentPacket = sCurrentPacket + delayedChars;
333
                            }
334
                            //write char
335
                            sCurrentPacket = sCurrentPacket + sNewDum.charAt(col);
336
 
337
                            delayedChars = "";
338
                        } else {        //if more than 4 delayedChars
339
                            delayedChars = "";
340
                            //check if there is space in packet, if not, create a new packet and dont write null to it
341
                            if (sCurrentPacket.length() > ARNEMAXPACKETSIZE - 5) {
342
                                String dummyString = sCurrentPacket;
343
                                sVector.addElement(dummyString);
344
                                sCurrentPacket = "";
345
                            } else {
346
                                //write null
347
                                cTemp = LCDNULL;    //(null = 0xA0 = 160)
348
                                sCurrentPacket = sCurrentPacket + cTemp;
349
                            }
350
 
351
                            //write setadress
352
                            cTemp = LCDSETADDRESS;
353
                            sCurrentPacket = sCurrentPacket + cTemp;
354
                            //write the address   (rowAddresses.elementAt(row) + col  ???)
355
                            cTemp = (char)(Integer.parseInt((String)rowAddresses.elementAt(row)) + col);
356
                            sCurrentPacket = sCurrentPacket + cTemp;
357
                            //write settext
358
                            cTemp = LCDTEXTWRITE;
359
                            sCurrentPacket = sCurrentPacket + cTemp;
360
 
361
                            //write character
362
                            sCurrentPacket = sCurrentPacket + sNewDum.charAt(col);
363
 
364
                        }
365
 
366
                    } else {                //if new packet (or just request to write address)
367
                        //check if it fits (yes it could be almost full if we just changed display row)
368
                        if (sCurrentPacket.length() > ARNEMAXPACKETSIZE - 4) {
369
                            String dummyString = sCurrentPacket;
370
                            sVector.addElement(dummyString);
371
                            sCurrentPacket = "";
372
                        }
373
 
374
                        if (sCurrentPacket.length() > 0) {
375
                            //new line, then send null
376
                            cTemp = LCDNULL;
377
                            sCurrentPacket = sCurrentPacket + cTemp;
378
                        }
379
                        //write setadress   //(dont destroy contents of sCurrentPacket, if we just changed display
380
                                            // row we just want to address at the end of existing packet)
381
                        cTemp = LCDSETADDRESS;
382
                        sCurrentPacket = sCurrentPacket + cTemp;
383
                        //write the address
384
                        cTemp = (char)(Integer.parseInt((String)rowAddresses.elementAt(row)) + col);
385
                        sCurrentPacket = sCurrentPacket + cTemp;
386
                        //write settext
387
                        cTemp = LCDTEXTWRITE;
388
                        sCurrentPacket = sCurrentPacket + cTemp;
389
                        //write character
390
                        sCurrentPacket = sCurrentPacket + sNewDum.charAt(col);
391
 
392
                        newPacket = false;
393
                    }
394
                }
395
            }
396
        }
397
        if (sCurrentPacket.length() > 0) {
398
            sVector.addElement(sCurrentPacket);
399
        }
400
        for (int i = 0; i < iHeight; i++) {
401
            //sCurrContents[i] = "....................";//sNewContents[i];
402
            sCurrContents[i] = sNewContents[i];
403
        }
404
 
405
        //sVector is now a vector with each packet as strings
406
 
407
        Vector pVector = new Vector();
408
        MbPacket p;
409
 
410
        //repack to MbPackets and return them
411
        for (int i = 0; i < sVector.size(); i++){
412
            p = new MbPacket();
413
            sCurrentPacket = (String)sVector.elementAt(i);
414
            sCurrentPacket = lcdDisplayReplace(sCurrentPacket);
415
            //System.out.println("apan: " + sCurrentPacket + " " + i);
416
            if (sCurrentPacket.length() > 0) {
417
                //ARNE packet in XML-format
418
                String contents = "<arnepacket bytes=\"" + sCurrentPacket.length() + "\""
419
                                   + " destnode=\"" + sTarget + "\">";
420
                for (int col = 0; col < sCurrentPacket.length(); col++) {
421
                    contents = contents + "<byte id=\"" + (col +1) + "\" value=\""
422
                                        + (int)sCurrentPacket.charAt(col) + "\"/>";
423
                }
424
                contents = contents + "</arnepacket>";
425
                //System.out.println("****start****");
426
                //System.out.println("sCurrentPacket=" + sCurrentPacket);
427
                //System.out.println("****contents:****");
428
                //System.out.println(contents);
429
                //System.out.println("****end****");
430
 
431
                p.setContents(contents);
432
                pVector.addElement(p);
433
            }
434
        }
435
 
436
        return pVector;
437
 
438
    }
439
 
440
 
441
    private String lcdDisplayReplace(String stringToReplace) {
442
        char dispChar;
443
        stringToReplace = stringToReplace.replace('å','a');
444
        dispChar = 225;
445
        stringToReplace = stringToReplace.replace('ä', dispChar);
446
        dispChar = 239;
447
        stringToReplace = stringToReplace.replace('ö',dispChar);
448
        stringToReplace = stringToReplace.replace('Å','A');
449
        dispChar = 225;
450
        stringToReplace = stringToReplace.replace('Ä', dispChar);
451
        dispChar = 239;
452
        stringToReplace = stringToReplace.replace('Ö',dispChar);
453
        dispChar = 245;
454
        stringToReplace = stringToReplace.replace('ü',dispChar);
455
        dispChar = 245;
456
        stringToReplace = stringToReplace.replace('Ü',dispChar);
457
        stringToReplace = stringToReplace.replace('é','e');
458
        stringToReplace = stringToReplace.replace('É','E');
459
        stringToReplace = stringToReplace.replace('\\','/');
460
 
461
        return stringToReplace;
462
    }
463
 
464
    /**
465
     * User can add a row to display-queue.
466
     * @param rowText Textrow to be added to queue.
467
     * @param prio The priority of the message; 0, 1 or 2.
468
     * @param timeToLive Set the minimum time the message should be displayed.
469
     * @param isSpecial Specifies if rowText isn't text but a command.
470
     * @param id
471
     * @param rowPos is "" if not used, "T+x" for Top+x, "B-x" for Bottom-x or "M+x"/"M-x" for Middle+/-x.
472
     */
473
    public void addRowToQueue(String rowText, int prio, int timeToLive, boolean isSpecial, int id, String rowPos) {
474
        RowObject dummyRow = new RowObject();
475
        dummyRow.row = rowText;
476
        dummyRow.TTL = timeToLive;
477
        dummyRow.isSpecial = isSpecial;
478
        dummyRow.id = id;
479
        boolean useRow = true;
480
 
481
        if (prio > 2 || prio < 0) {
482
            prio = 2;
483
        }
484
 
485
        //dummyRow.rowNumber
486
        if (rowPos.length() > 0) {
487
            int iDummy = -1;
488
            boolean bUseRowNum = false;
489
 
490
            if (rowPos.length() == 1) {
491
                iDummy = 0;
492
                bUseRowNum = true;
493
            } else if (rowPos.length() == 3) {
494
                //todo: storre an 3 da? en display med mer an 9 rader...?
495
                try {
496
                    if (rowPos.substring(1,2).compareTo("+") == 0) {
497
                        iDummy = Integer.parseInt(rowPos.substring(2,3));
498
                    } else if (rowPos.substring(1,2).compareTo("-") == 0) {
499
                        iDummy = Integer.parseInt(rowPos.substring(1,3));
500
                    }
501
                    bUseRowNum = true;
502
                } catch (NumberFormatException e) {
503
                System.out.println("exception with rownumber!");
504
                }
505
            }
506
            //System.out.println("iDummy: " + iDummy);
507
 
508
            if (bUseRowNum == true) {
509
                if (rowPos.charAt(0) == 'T') {
510
                    //iDummy = iDummy;
511
                } else if (rowPos.charAt(0) == 'B') {
512
                    iDummy = iDummy + iHeight-1;
513
                } else if (rowPos.charAt(0) == 'M') {
514
                    iDummy = iDummy + (int)((iHeight-1)/2);
515
                } else {
516
                    //dummyRow.rowNumber = null;
517
                }
518
 
519
                if (iDummy < 0 || iDummy >= iHeight) {
520
                    useRow = false;
521
                }
522
 
523
            }
524
            dummyRow.rowNumber = iDummy;        //set even if not used
525
            if (iDummy >= 0) {
526
                //System.out.println("rownumber: " + iDummy);
527
                dummyRow.useRowNum = true;
528
            }
529
 
530
        }
531
 
532
        //put message in priority-queues
533
        if (dummyRow.row.length() > 0 && useRow) {  //useRow is false if rownumber if out of range
534
            //System.out.println("put in queue");
535
            boolean checkNext = true;
536
            //check here if id > 0, if so then check if message with same id is in currentRows
537
            //if not in currentRows then check queue (prio accordning to object.prio), replace if found
538
            //if no message with the same id is found then place message last in queue
539
            if (dummyRow.id > 0) {
540
                //System.out.println("id > 0");
541
                for (int i = 0; i < rowsCurrent.size() && checkNext; i++) {
542
                    RowObject dummyCurrentRow = (RowObject)rowsCurrent.elementAt(i);
543
                    if (dummyCurrentRow.id == dummyRow.id){
544
                        //System.out.println("replacing on display");
545
                        rowsCurrent.remove(i);                                   //remove old
546
                        dummyRow.rowNumber = dummyCurrentRow.rowNumber;          //if not set then set to the row to be placed
547
                        rowsCurrent.addElement(dummyRow);                        //place new stuff
548
                        if (dummyRow.TTL < 0) {                                  //om raden redan ska betraktas som ledig
549
                            if (!freeRows.contains(dummyCurrentRow)) {           //och tidigare rad inte redan var ledig
550
                                freeRows.addElement(dummyRow);
551
                            }
552
                        } else {
553
                            if (freeRows.contains(dummyCurrentRow)) {           //och tidigare rad inte redan var ledig
554
                                freeRows.remove(dummyCurrentRow);
555
                            }
556
                        }
557
                        writeRow(dummyRow.rowNumber, dummyRow.row);
558
                        checkNext = false;
559
 
560
                    }
561
                }
562
 
563
                for (int j = 0; j < rowPrio0.size() && checkNext; j++) {
564
                    //System.out.println("replacing in queue");
565
                    RowObject queueDummyRow = (RowObject)rowPrio0.elementAt(j);
566
                    if (queueDummyRow.id == dummyRow.id) {
567
                        rowPrio0.remove(j);
568
                        rowPrio0.insertElementAt(dummyRow, j);
569
                        checkNext = false;
570
                    }
571
                }
572
                for (int j = 0; j < rowPrio1.size() && checkNext; j++) {
573
                    RowObject queueDummyRow = (RowObject)rowPrio1.elementAt(j);
574
                    if (queueDummyRow.id == dummyRow.id) {
575
                        rowPrio1.remove(j);
576
                        rowPrio1.insertElementAt(dummyRow, j);
577
                        checkNext = false;
578
                    }
579
                }
580
                for (int j = 0; j < rowPrio2.size() && checkNext; j++) {
581
                    RowObject queueDummyRow = (RowObject)rowPrio2.elementAt(j);
582
                    if (queueDummyRow.id == dummyRow.id) {
583
                        rowPrio2.remove(j);
584
                        rowPrio2.insertElementAt(dummyRow, j);
585
                        checkNext = false;
586
                    }
587
                }
588
            }
589
 
590
            if (checkNext) {
591
                if (prio == 0) {
592
                    //System.out.println("adding to prio0");
593
                    rowPrio0.addElement(dummyRow);
594
                } else if (prio == 1) {
595
                    //System.out.println("adding to prio1");
596
                    rowPrio1.addElement(dummyRow);
597
                } else if (prio == 2) {
598
                    //System.out.println("adding to prio2");
599
                    rowPrio2.addElement(dummyRow);
600
                }
601
            }
602
 
603
        }
604
 
605
        manageQueues();     //do this for enabling data to be sent more then 1 time per sec
606
    }
607
 
608
 
609
    /**
610
     * This method should be run once every second, to update queue and
611
     * ttl for messages.
612
     */
613
    public void manageQueues() {
614
        RowObject dummyRow;
615
 
616
        for (int i = 0; i < rowsCurrent.size(); i++) {
617
            dummyRow = (RowObject)rowsCurrent.elementAt(i);
618
            if (dummyRow.TTL >= 0) {
619
                //System.out.println("Counting down ttl for: " + dummyRow.rowNumber + ", text: " + dummyRow.row);
620
                if (dummyRow.TTL == 0) {
621
                    freeRows.addElement(dummyRow);                  //added at the end (rows free longest at the beginning)
622
                    //System.out.println("rowelement added to freerows: " + dummyRow.rowNumber + ", text: " + dummyRow.row);
623
                }
624
                dummyRow.TTL = dummyRow.TTL - 1;
625
            }
626
        }
627
 
628
        //glom inte initiera vektorn, alla rader ska vara med i den
629
 
630
        //gor en koll av TTL nar raden flyttas till rowsCurrent, if ttl<0 then add to freerows
631
 
632
 
633
        for (int i = 0; i < freeRows.size(); i++) {
634
            boolean checkNextQueue = true;
635
 
636
            for (int j = 0; j < rowPrio0.size() && checkNextQueue; j++) {
637
                //System.out.println("Prio0queueelement: " + j + ", freerow: " + i);
638
                RowObject queueDummyRow = (RowObject)rowPrio0.elementAt(j);
639
                dummyRow = (RowObject)freeRows.elementAt(i);
640
                if (dummyRow.rowNumber == queueDummyRow.rowNumber || !queueDummyRow.useRowNum) {
641
                    //System.out.println("Place row, prio0element" + ", text: " + queueDummyRow.row);
642
                    freeRows.remove(i);                                     //not free anymore
643
                    rowPrio0.remove(j);                                     //remove queue-item
644
                    queueDummyRow.rowNumber = dummyRow.rowNumber;           //if not set then set to the row to be placed
645
                    rowsCurrent.remove(dummyRow);                           //remove old stuff
646
                    rowsCurrent.addElement(queueDummyRow);                  //place new stuff
647
                    if (queueDummyRow.TTL < 0) {                            //om raden redan ska betraktas som ledig
648
                        freeRows.addElement(queueDummyRow);
649
                    }
650
                    writeRow(queueDummyRow.rowNumber, queueDummyRow.row);
651
                    i = i - 1;
652
                    checkNextQueue = false;
653
                }
654
            }
655
            for (int j = 0; j < rowPrio1.size() && checkNextQueue; j++) {
656
                //System.out.println("Prio1queueelement: " + j + ", freerow: " + i);
657
                RowObject queueDummyRow = (RowObject)rowPrio1.elementAt(j);
658
                dummyRow = (RowObject)freeRows.elementAt(i);
659
                if (dummyRow.rowNumber == queueDummyRow.rowNumber || !queueDummyRow.useRowNum) {
660
                    //System.out.println("Place row, prio1element" + ", text: " + queueDummyRow.row);
661
                    freeRows.remove(i);                                     //not free anymore
662
                    rowPrio1.remove(j);                                     //remove queue-item
663
                    queueDummyRow.rowNumber = dummyRow.rowNumber;           //if not set then set to the row to be placed
664
                    rowsCurrent.remove(dummyRow);                           //remove old stuff
665
                    rowsCurrent.addElement(queueDummyRow);                  //place new stuff
666
                    if (queueDummyRow.TTL < 0) {                            //om raden redan ska betraktas som ledig
667
                        freeRows.addElement(queueDummyRow);
668
                    }
669
                    writeRow(queueDummyRow.rowNumber, queueDummyRow.row);
670
                    i = i - 1;
671
                    checkNextQueue = false;
672
                }
673
            }
674
            for (int j = 0; j < rowPrio2.size() && checkNextQueue; j++) {
675
                RowObject queueDummyRow = (RowObject)rowPrio2.elementAt(j);
676
                dummyRow = (RowObject)freeRows.elementAt(i);
677
                if (dummyRow.rowNumber == queueDummyRow.rowNumber || !queueDummyRow.useRowNum) {
678
                    freeRows.remove(i);                                     //not free anymore
679
                    rowPrio2.remove(j);                                     //remove queue-item
680
                    queueDummyRow.rowNumber = dummyRow.rowNumber;           //if not set then set to the row to be placed
681
                    rowsCurrent.remove(dummyRow);                           //remove old stuff
682
                    rowsCurrent.addElement(queueDummyRow);                  //place new stuff
683
                    if (queueDummyRow.TTL < 0) {                            //om raden redan ska betraktas som ledig
684
                        freeRows.addElement(queueDummyRow);
685
                    }
686
                    writeRow(queueDummyRow.rowNumber, queueDummyRow.row);
687
                    i = i - 1;
688
                    checkNextQueue = false;
689
                }
690
            }
691
 
692
            //System.out.println("apan: ja");
693
 
694
        }
695
 
696
    }
697
 
698
    /**
699
     * Keeps the rows the same length as display-width
700
     * Cut or fill with spaces.
701
     * @param sRow String to fix.
702
     * @return a string, sRow, but with the same length as the display-width.
703
     */
704
    private String rowFixer(String sRow) {
705
        if (sRow.length() > iWidth) {
706
            return sRow.substring(0, iWidth);
707
        } else if (sRow.length() < iWidth) {
708
            String sDummy = "";
709
            for (int i = 0; i < iWidth - sRow.length(); i++) {
710
                sDummy = sDummy + " ";
711
            }
712
            return sRow + sDummy;
713
        } else {
714
            return sRow;
715
        }
716
    }
717
 
718
    /**
719
     * A queue-object
720
     */
721
    private class RowObject {
722
        protected int TTL;
723
        protected String row;
724
        //protected boolean isDisplaying;
725
        protected boolean isSpecial;    //row definierar en speciell egenskap som t.ex. cleardisplay osv om true
726
                                //fixa in detta senare
727
 
728
        protected int rowNumber=0;
729
        protected boolean useRowNum = false;
730
        protected int id;
731
    }
732
}