Subversion Repositories bacoAlunos

Rev

Rev 1568 | Rev 1570 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1563 jmachado 1
package pt.estgp.estgweb.services.questionarios.utils;
1553 jmachado 2
 
3
import org.dom4j.DocumentException;
4
import org.w3c.dom.Document;
5
import pt.estgp.estgweb.utils.Dom4jUtil;
6
 
7
import javax.xml.transform.TransformerException;
8
import java.io.ByteArrayOutputStream;
9
import java.io.IOException;
10
import java.io.Serializable;
11
import java.util.ArrayList;
12
 
13
/**
14
 * Created by jorgemachado on 11/11/16.
15
 */
16
public class DataTable implements 1.5.0/docs/api/java/io/Serializable.html">Serializable
17
{
18
 
19
 
20
 
21
    public DataTable() {
22
 
23
        setRows(new ArrayList<Row>());
24
    }
25
 
26
 
27
    private ArrayList<Row> rows;
28
 
29
    public Row addRow()
30
    {
31
        Row row = new Row();
32
        rows.add(row);
33
        return row;
34
    }
35
    public Row addRowHeader()
36
    {
37
        Row row = new Row();
38
        row.setTypeHeader();
39
        rows.add(row);
40
        return row;
41
    }
42
    public Row addRowNormal()
43
    {
44
        Row row = new Row();
45
        row.setTypeNormal();
46
        rows.add(row);
47
        return row;
48
    }
49
    public Row addRowFooter()
50
    {
51
        Row row = new Row();
52
        row.setTypeFooter();
53
        rows.add(row);
54
        return row;
55
    }
56
 
57
    public ArrayList<Row> getRows() { return rows; }
58
 
59
    public void setRows(ArrayList<Row> rows) {
60
        this.rows = rows;
61
    }
62
 
63
    public static class Row implements 1.5.0/docs/api/java/io/Serializable.html">Serializable
64
    {
65
        //header, footer, normal
66
        1.5.0/docs/api/java/lang/String.html">String type = null;
1569 jmachado 67
        boolean invisible = false;
1553 jmachado 68
        private ArrayList<Col> cols;
69
 
1567 jmachado 70
 
1553 jmachado 71
        public Row() {
72
            setCols(new ArrayList<Col>());
73
        }
74
 
1569 jmachado 75
        public boolean isInvisible() {
76
            return invisible;
77
        }
78
 
79
        public void setInvisible(boolean invisible) {
80
            this.invisible = invisible;
81
        }
82
 
1553 jmachado 83
        public void setCols(ArrayList<Col> cols) {
84
            this.cols = cols;
85
        }
86
 
87
        public void setTypeNormal() { type = "normal"; }
88
        public void setTypeHeader() { type = "header"; }
89
        public void setTypeFooter() { type = "footer"; }
90
 
1567 jmachado 91
 
92
 
1553 jmachado 93
        public Col addCol()
94
        {
95
            Col col = new Col();
96
            cols.add(col);
97
            return col;
98
        }
99
 
100
        public Col addColNumberLeft(1.5.0/docs/api/java/lang/String.html">String value)
101
        {   Col col = new Col(value);
102
            col.setTypeNumber();
103
            col.setAlignLeft();
104
            cols.add(col);
105
            return col;
106
        }
107
        public Col addColNumberRight(1.5.0/docs/api/java/lang/String.html">String value)
1567 jmachado 108
        {   Col col = new Col(value);
1553 jmachado 109
            col.setTypeNumber();
110
            col.setAlignRight();
111
            cols.add(col);
112
            return col;
113
        }
114
        public Col addColNumberCenter(1.5.0/docs/api/java/lang/String.html">String value)
115
        {   Col col = new Col(value);
116
            col.setTypeNumber();
117
            col.setAlignCenter();
118
            cols.add(col);
119
            return col;
120
        }
121
 
122
        public Col addColTextLeft(1.5.0/docs/api/java/lang/String.html">String value)
123
        {   Col col = new Col(value);
124
            col.setTypeText();
125
            col.setAlignLeft();
126
            cols.add(col);
127
            return col;
128
        }
129
        public Col addColTextRight(1.5.0/docs/api/java/lang/String.html">String value)
130
        {   Col col = new Col(value);
131
            col.setTypeText();
132
            col.setAlignRight();
133
            cols.add(col);
134
            return col;
135
        }
136
        public Col addColTextCenter(1.5.0/docs/api/java/lang/String.html">String value)
137
        {   Col col = new Col(value);
138
            col.setTypeText();
139
            col.setAlignCenter();
140
            cols.add(col);
141
            return col;
142
        }
143
 
144
        public Col addColPercentageLeft(1.5.0/docs/api/java/lang/String.html">String value)
145
        {   Col col = new Col(value);
146
            col.setTypePercentage();
147
            col.setAlignLeft();
148
            cols.add(col);
149
            return col;
150
        }
151
        public Col addColPercentageRight(1.5.0/docs/api/java/lang/String.html">String value)
152
        {   Col col = new Col(value);
153
            col.setTypePercentage();
154
            col.setAlignRight();
155
            cols.add(col);
156
            return col;
157
        }
158
        public Col addColPercentageCenter(1.5.0/docs/api/java/lang/String.html">String value)
159
        {   Col col = new Col(value);
160
            col.setTypePercentage();
161
            col.setAlignCenter();
162
            cols.add(col);
163
            return col;
164
        }
1554 jmachado 165
 
166
        public Col addColPercentageLeft(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
167
        {   Col col = new Col(value);
168
            col.setTypePercentage();
169
            col.setAlignLeft();
170
            col.setUsePercentageColor(usePercentageColors);
171
            cols.add(col);
172
            return col;
173
        }
174
        public Col addColPercentageRight(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
175
        {   Col col = new Col(value);
176
            col.setTypePercentage();
177
            col.setAlignRight();
178
            col.setUsePercentageColor(usePercentageColors);
179
            cols.add(col);
180
            return col;
181
        }
182
        public Col addColPercentageCenter(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
183
        {   Col col = new Col(value);
184
            col.setTypePercentage();
185
            col.setAlignCenter();
186
            col.setUsePercentageColor(usePercentageColors);
187
            cols.add(col);
188
            return col;
189
        }
190
 
191
 
192
        public Col addColPercentageDefinedLeft(1.5.0/docs/api/java/lang/String.html">String value,1.5.0/docs/api/java/lang/String.html">String percent,boolean usePercentageColors)
193
        {   Col col = new Col(value);
194
            col.setPercentDefined(percent);
195
            col.setTypePercentageDefined();
196
            col.setAlignLeft();
197
            col.setUsePercentageColor(usePercentageColors);
198
            cols.add(col);
199
            return col;
200
        }
201
        public Col addColPercentageDefinedRight(1.5.0/docs/api/java/lang/String.html">String value,1.5.0/docs/api/java/lang/String.html">String percent,boolean usePercentageColors)
202
        {   Col col = new Col(value);
203
            col.setPercentDefined(percent);
204
            col.setTypePercentageDefined();
205
            col.setAlignRight();
206
            col.setUsePercentageColor(usePercentageColors);
207
            cols.add(col);
208
            return col;
209
        }
210
        public Col addColPercentageDefinedCenter(1.5.0/docs/api/java/lang/String.html">String value,1.5.0/docs/api/java/lang/String.html">String percent,boolean usePercentageColors)
211
        {   Col col = new Col(value);
212
            col.setPercentDefined(percent);
213
            col.setTypePercentageDefined();
214
            col.setAlignCenter();
215
            col.setUsePercentageColor(usePercentageColors);
216
            cols.add(col);
217
            return col;
218
        }
219
 
220
 
1553 jmachado 221
        public Col addColLabelLeft(1.5.0/docs/api/java/lang/String.html">String value)
222
        {   Col col = new Col(value);
223
            col.setTypeLabel();
224
            col.setAlignLeft();
225
            cols.add(col);
226
            return col;
227
        }
228
        public Col addColLabelRight(1.5.0/docs/api/java/lang/String.html">String value)
229
        {   Col col = new Col(value);
230
            col.setTypeLabel();
231
            col.setAlignRight();
232
            cols.add(col);
233
            return col;
234
        }
235
        public Col addColLabelCenter(1.5.0/docs/api/java/lang/String.html">String value)
236
        {   Col col = new Col(value);
237
            col.setTypeLabel();
238
            col.setAlignCenter();
239
            cols.add(col);
240
            return col;
241
        }
242
 
243
        public 1.5.0/docs/api/java/lang/String.html">String getType() { return type; }
244
        public ArrayList<Col> getCols() { return cols; }
245
 
246
        public void setType(1.5.0/docs/api/java/lang/String.html">String type) {
247
            this.type = type;
248
        }
249
 
250
 
251
        public static class Col implements 1.5.0/docs/api/java/io/Serializable.html">Serializable
252
        {
253
            public Col(1.5.0/docs/api/java/lang/String.html">String value) {
254
                this.value = value;
255
            }
256
            public Col() {
257
            }
258
 
1569 jmachado 259
 
1553 jmachado 260
            //center, left, right
1568 jmachado 261
            1.5.0/docs/api/java/lang/String.html">String width = "";
1553 jmachado 262
            1.5.0/docs/api/java/lang/String.html">String align = null;
263
            1.5.0/docs/api/java/lang/String.html">String value = null;
1554 jmachado 264
            1.5.0/docs/api/java/lang/String.html">String percentDefined = null;
265
            //number, percentage, percentageDefined, text, label
1553 jmachado 266
            1.5.0/docs/api/java/lang/String.html">String type = null;
1554 jmachado 267
            boolean usePercentageColor = false;
268
            1.5.0/docs/api/java/lang/String.html">String backgroundColor = "";
1565 jmachado 269
            1.5.0/docs/api/java/lang/String.html">String backgroundColorPercentage = "";
1567 jmachado 270
            1.5.0/docs/api/java/lang/String.html">String fontWeight = "";
271
            int colspan = 0;
1553 jmachado 272
 
1568 jmachado 273
            public 1.5.0/docs/api/java/lang/String.html">String getWidth() {
274
                return width;
275
            }
276
 
277
            public void setWidth(1.5.0/docs/api/java/lang/String.html">String width) {
278
                this.width = width;
279
            }
280
 
1567 jmachado 281
            public 1.5.0/docs/api/java/lang/String.html">String getFontWeight() {
282
                return fontWeight;
283
            }
1554 jmachado 284
 
1567 jmachado 285
            public void setFontWeight(1.5.0/docs/api/java/lang/String.html">String fontWeight) {
286
                this.fontWeight = fontWeight;
287
            }
288
 
289
            public int getColspan() {
290
                return colspan;
291
            }
292
 
293
            public void setColspan(int colspan) {
294
                this.colspan = colspan;
295
            }
296
 
1554 jmachado 297
            public 1.5.0/docs/api/java/lang/String.html">String getBackgroundColor() {
298
                return backgroundColor;
299
            }
300
 
301
            public void setBackgroundColor(1.5.0/docs/api/java/lang/String.html">String backgroundColor) {
302
                this.backgroundColor = backgroundColor;
303
            }
304
 
1565 jmachado 305
            public 1.5.0/docs/api/java/lang/String.html">String getBackgroundColorPercentage() {
306
                return backgroundColorPercentage;
307
            }
308
 
309
            public void setBackgroundColorPercentage(1.5.0/docs/api/java/lang/String.html">String backgroundColorPercentage) {
310
                this.backgroundColorPercentage = backgroundColorPercentage;
311
            }
312
 
1553 jmachado 313
            public void setAlignLeft() { align = "left"; }
314
            public void setAlignRight() { align = "right"; }
315
            public void setAlignCenter() { align = "center"; }
316
            public void setTypeNumber() { type = "number"; }
317
            public void setTypeText() { type = "text"; }
318
            public void setTypePercentage() { type = "percentage"; }
1554 jmachado 319
            public void setTypePercentageDefined() { type = "percentageDefined"; }
1553 jmachado 320
            public void setTypeLabel() { type = "label"; }
321
 
1554 jmachado 322
            public boolean isUsePercentageColor() {
323
                return usePercentageColor;
324
            }
325
 
1565 jmachado 326
            public void setUsePercentageColor(boolean usePercentageColor)
327
            {
1554 jmachado 328
                this.usePercentageColor = usePercentageColor;
1565 jmachado 329
                //new Approach for percentage color using degrade
330
                if(usePercentageColor)
331
                {
332
                    double percentage = type.equals("percentageDefined") ? 1.5.0/docs/api/java/lang/Double.html">Double.parseDouble(percentDefined.replace(",",".")) : 1.5.0/docs/api/java/lang/Double.html">Double.parseDouble(value.replace(",","."));
333
                    percentage = percentage / 100.0;
334
 
1567 jmachado 335
                    setBackgroundColorPercentage(getColorGradientForPercentage(percentage));
1565 jmachado 336
                }
337
 
1554 jmachado 338
            }
339
 
1553 jmachado 340
            public 1.5.0/docs/api/java/lang/String.html">String getValue() { return value; }
341
            public void setValue(1.5.0/docs/api/java/lang/String.html">String value) { this.value = value; }
1554 jmachado 342
 
343
            public 1.5.0/docs/api/java/lang/String.html">String getPercentDefined() {
344
                return percentDefined;
345
            }
346
 
1565 jmachado 347
            public void setPercentDefined(1.5.0/docs/api/java/lang/String.html">String percentDefined)             {
348
                if(percentDefined.contains("."))
349
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf("."));
350
                else if(percentDefined.contains(","))
351
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf(","));
1554 jmachado 352
                this.percentDefined = percentDefined;
353
            }
1553 jmachado 354
        }
355
    }
356
 
1567 jmachado 357
    /**
358
     *
359
     * @param percentageDecimal between 0 and 1
360
     * @return
361
     */
362
    public static 1.5.0/docs/api/java/lang/String.html">String getColorGradientForPercentage(double percentageDecimal)
363
    {
364
        if(percentageDecimal < 0.5)
365
        {
366
            int green = ChartBuilderUtil.getGreenComponentGradient(percentageDecimal);
367
            return "rgb(255," + green + ",0)";
368
        }
369
        else
370
        {
371
 
372
            int red = ChartBuilderUtil.getRedComponentGradient(percentageDecimal);
373
            //int red = (int) (255.0*((1.0-percentageDecimal)*2));
374
            return "rgb(" + red + ",255,0)";
375
        }
376
    }
377
 
378
 
379
 
1553 jmachado 380
    public 5+0%2Fdocs%2Fapi+Document">Document serialize() throws DocumentException, 1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException, 1.5.0/docs/api/java/io/IOException.html">IOException {
381
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream out = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
382
        //fos1 = new FileOutputStream("/Volumes/Home/jorgemachado/Desktop/docenteReport.xml");
383
        java.beans.1.5.0/docs/api/java/beans/XMLEncoder.html">XMLEncoder xe1 = new java.beans.1.5.0/docs/api/java/beans/XMLEncoder.html">XMLEncoder(out);
384
 
385
 
386
        xe1.writeObject(this);
387
        xe1.flush();
388
        xe1.close();
389
        //Dom4jUtil.writeSout(Dom4jUtil.parse(out.toString()));
390
        return Dom4jUtil.toW3c(Dom4jUtil.parse(out.toString()));
391
 
392
    }
393
 
394
}