Subversion Repositories bacoAlunos

Rev

Rev 1565 | Rev 1568 | 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;
67
        private ArrayList<Col> cols;
68
 
1567 jmachado 69
 
1553 jmachado 70
        public Row() {
71
            setCols(new ArrayList<Col>());
72
        }
73
 
74
        public void setCols(ArrayList<Col> cols) {
75
            this.cols = cols;
76
        }
77
 
78
        public void setTypeNormal() { type = "normal"; }
79
        public void setTypeHeader() { type = "header"; }
80
        public void setTypeFooter() { type = "footer"; }
81
 
1567 jmachado 82
 
83
 
1553 jmachado 84
        public Col addCol()
85
        {
86
            Col col = new Col();
87
            cols.add(col);
88
            return col;
89
        }
90
 
91
        public Col addColNumberLeft(1.5.0/docs/api/java/lang/String.html">String value)
92
        {   Col col = new Col(value);
93
            col.setTypeNumber();
94
            col.setAlignLeft();
95
            cols.add(col);
96
            return col;
97
        }
98
        public Col addColNumberRight(1.5.0/docs/api/java/lang/String.html">String value)
1567 jmachado 99
        {   Col col = new Col(value);
1553 jmachado 100
            col.setTypeNumber();
101
            col.setAlignRight();
102
            cols.add(col);
103
            return col;
104
        }
105
        public Col addColNumberCenter(1.5.0/docs/api/java/lang/String.html">String value)
106
        {   Col col = new Col(value);
107
            col.setTypeNumber();
108
            col.setAlignCenter();
109
            cols.add(col);
110
            return col;
111
        }
112
 
113
        public Col addColTextLeft(1.5.0/docs/api/java/lang/String.html">String value)
114
        {   Col col = new Col(value);
115
            col.setTypeText();
116
            col.setAlignLeft();
117
            cols.add(col);
118
            return col;
119
        }
120
        public Col addColTextRight(1.5.0/docs/api/java/lang/String.html">String value)
121
        {   Col col = new Col(value);
122
            col.setTypeText();
123
            col.setAlignRight();
124
            cols.add(col);
125
            return col;
126
        }
127
        public Col addColTextCenter(1.5.0/docs/api/java/lang/String.html">String value)
128
        {   Col col = new Col(value);
129
            col.setTypeText();
130
            col.setAlignCenter();
131
            cols.add(col);
132
            return col;
133
        }
134
 
135
        public Col addColPercentageLeft(1.5.0/docs/api/java/lang/String.html">String value)
136
        {   Col col = new Col(value);
137
            col.setTypePercentage();
138
            col.setAlignLeft();
139
            cols.add(col);
140
            return col;
141
        }
142
        public Col addColPercentageRight(1.5.0/docs/api/java/lang/String.html">String value)
143
        {   Col col = new Col(value);
144
            col.setTypePercentage();
145
            col.setAlignRight();
146
            cols.add(col);
147
            return col;
148
        }
149
        public Col addColPercentageCenter(1.5.0/docs/api/java/lang/String.html">String value)
150
        {   Col col = new Col(value);
151
            col.setTypePercentage();
152
            col.setAlignCenter();
153
            cols.add(col);
154
            return col;
155
        }
1554 jmachado 156
 
157
        public Col addColPercentageLeft(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
158
        {   Col col = new Col(value);
159
            col.setTypePercentage();
160
            col.setAlignLeft();
161
            col.setUsePercentageColor(usePercentageColors);
162
            cols.add(col);
163
            return col;
164
        }
165
        public Col addColPercentageRight(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
166
        {   Col col = new Col(value);
167
            col.setTypePercentage();
168
            col.setAlignRight();
169
            col.setUsePercentageColor(usePercentageColors);
170
            cols.add(col);
171
            return col;
172
        }
173
        public Col addColPercentageCenter(1.5.0/docs/api/java/lang/String.html">String value,boolean usePercentageColors)
174
        {   Col col = new Col(value);
175
            col.setTypePercentage();
176
            col.setAlignCenter();
177
            col.setUsePercentageColor(usePercentageColors);
178
            cols.add(col);
179
            return col;
180
        }
181
 
182
 
183
        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)
184
        {   Col col = new Col(value);
185
            col.setPercentDefined(percent);
186
            col.setTypePercentageDefined();
187
            col.setAlignLeft();
188
            col.setUsePercentageColor(usePercentageColors);
189
            cols.add(col);
190
            return col;
191
        }
192
        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)
193
        {   Col col = new Col(value);
194
            col.setPercentDefined(percent);
195
            col.setTypePercentageDefined();
196
            col.setAlignRight();
197
            col.setUsePercentageColor(usePercentageColors);
198
            cols.add(col);
199
            return col;
200
        }
201
        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)
202
        {   Col col = new Col(value);
203
            col.setPercentDefined(percent);
204
            col.setTypePercentageDefined();
205
            col.setAlignCenter();
206
            col.setUsePercentageColor(usePercentageColors);
207
            cols.add(col);
208
            return col;
209
        }
210
 
211
 
1553 jmachado 212
        public Col addColLabelLeft(1.5.0/docs/api/java/lang/String.html">String value)
213
        {   Col col = new Col(value);
214
            col.setTypeLabel();
215
            col.setAlignLeft();
216
            cols.add(col);
217
            return col;
218
        }
219
        public Col addColLabelRight(1.5.0/docs/api/java/lang/String.html">String value)
220
        {   Col col = new Col(value);
221
            col.setTypeLabel();
222
            col.setAlignRight();
223
            cols.add(col);
224
            return col;
225
        }
226
        public Col addColLabelCenter(1.5.0/docs/api/java/lang/String.html">String value)
227
        {   Col col = new Col(value);
228
            col.setTypeLabel();
229
            col.setAlignCenter();
230
            cols.add(col);
231
            return col;
232
        }
233
 
234
        public 1.5.0/docs/api/java/lang/String.html">String getType() { return type; }
235
        public ArrayList<Col> getCols() { return cols; }
236
 
237
        public void setType(1.5.0/docs/api/java/lang/String.html">String type) {
238
            this.type = type;
239
        }
240
 
241
 
242
        public static class Col implements 1.5.0/docs/api/java/io/Serializable.html">Serializable
243
        {
244
            public Col(1.5.0/docs/api/java/lang/String.html">String value) {
245
                this.value = value;
246
            }
247
            public Col() {
248
            }
249
 
250
            //center, left, right
251
            1.5.0/docs/api/java/lang/String.html">String align = null;
252
            1.5.0/docs/api/java/lang/String.html">String value = null;
1554 jmachado 253
            1.5.0/docs/api/java/lang/String.html">String percentDefined = null;
254
            //number, percentage, percentageDefined, text, label
1553 jmachado 255
            1.5.0/docs/api/java/lang/String.html">String type = null;
1554 jmachado 256
            boolean usePercentageColor = false;
257
            1.5.0/docs/api/java/lang/String.html">String backgroundColor = "";
1565 jmachado 258
            1.5.0/docs/api/java/lang/String.html">String backgroundColorPercentage = "";
1567 jmachado 259
            1.5.0/docs/api/java/lang/String.html">String fontWeight = "";
260
            int colspan = 0;
1553 jmachado 261
 
1567 jmachado 262
            public 1.5.0/docs/api/java/lang/String.html">String getFontWeight() {
263
                return fontWeight;
264
            }
1554 jmachado 265
 
1567 jmachado 266
            public void setFontWeight(1.5.0/docs/api/java/lang/String.html">String fontWeight) {
267
                this.fontWeight = fontWeight;
268
            }
269
 
270
            public int getColspan() {
271
                return colspan;
272
            }
273
 
274
            public void setColspan(int colspan) {
275
                this.colspan = colspan;
276
            }
277
 
1554 jmachado 278
            public 1.5.0/docs/api/java/lang/String.html">String getBackgroundColor() {
279
                return backgroundColor;
280
            }
281
 
282
            public void setBackgroundColor(1.5.0/docs/api/java/lang/String.html">String backgroundColor) {
283
                this.backgroundColor = backgroundColor;
284
            }
285
 
1565 jmachado 286
            public 1.5.0/docs/api/java/lang/String.html">String getBackgroundColorPercentage() {
287
                return backgroundColorPercentage;
288
            }
289
 
290
            public void setBackgroundColorPercentage(1.5.0/docs/api/java/lang/String.html">String backgroundColorPercentage) {
291
                this.backgroundColorPercentage = backgroundColorPercentage;
292
            }
293
 
1553 jmachado 294
            public void setAlignLeft() { align = "left"; }
295
            public void setAlignRight() { align = "right"; }
296
            public void setAlignCenter() { align = "center"; }
297
            public void setTypeNumber() { type = "number"; }
298
            public void setTypeText() { type = "text"; }
299
            public void setTypePercentage() { type = "percentage"; }
1554 jmachado 300
            public void setTypePercentageDefined() { type = "percentageDefined"; }
1553 jmachado 301
            public void setTypeLabel() { type = "label"; }
302
 
1554 jmachado 303
            public boolean isUsePercentageColor() {
304
                return usePercentageColor;
305
            }
306
 
1565 jmachado 307
            public void setUsePercentageColor(boolean usePercentageColor)
308
            {
1554 jmachado 309
                this.usePercentageColor = usePercentageColor;
1565 jmachado 310
                //new Approach for percentage color using degrade
311
                if(usePercentageColor)
312
                {
313
                    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(",","."));
314
                    percentage = percentage / 100.0;
315
 
1567 jmachado 316
                    setBackgroundColorPercentage(getColorGradientForPercentage(percentage));
1565 jmachado 317
                }
318
 
1554 jmachado 319
            }
320
 
1553 jmachado 321
            public 1.5.0/docs/api/java/lang/String.html">String getValue() { return value; }
322
            public void setValue(1.5.0/docs/api/java/lang/String.html">String value) { this.value = value; }
1554 jmachado 323
 
324
            public 1.5.0/docs/api/java/lang/String.html">String getPercentDefined() {
325
                return percentDefined;
326
            }
327
 
1565 jmachado 328
            public void setPercentDefined(1.5.0/docs/api/java/lang/String.html">String percentDefined)             {
329
                if(percentDefined.contains("."))
330
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf("."));
331
                else if(percentDefined.contains(","))
332
                    this.percentDefined = percentDefined.substring(0,percentDefined.indexOf(","));
1554 jmachado 333
                this.percentDefined = percentDefined;
334
            }
1553 jmachado 335
        }
336
    }
337
 
1567 jmachado 338
    /**
339
     *
340
     * @param percentageDecimal between 0 and 1
341
     * @return
342
     */
343
    public static 1.5.0/docs/api/java/lang/String.html">String getColorGradientForPercentage(double percentageDecimal)
344
    {
345
        if(percentageDecimal < 0.5)
346
        {
347
            int green = ChartBuilderUtil.getGreenComponentGradient(percentageDecimal);
348
            return "rgb(255," + green + ",0)";
349
        }
350
        else
351
        {
352
 
353
            int red = ChartBuilderUtil.getRedComponentGradient(percentageDecimal);
354
            //int red = (int) (255.0*((1.0-percentageDecimal)*2));
355
            return "rgb(" + red + ",255,0)";
356
        }
357
    }
358
 
359
 
360
 
1553 jmachado 361
    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 {
362
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream out = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
363
        //fos1 = new FileOutputStream("/Volumes/Home/jorgemachado/Desktop/docenteReport.xml");
364
        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);
365
 
366
 
367
        xe1.writeObject(this);
368
        xe1.flush();
369
        xe1.close();
370
        //Dom4jUtil.writeSout(Dom4jUtil.parse(out.toString()));
371
        return Dom4jUtil.toW3c(Dom4jUtil.parse(out.toString()));
372
 
373
    }
374
 
375
}