Source Code Cross Referenced for SWTHelper.java in  » Project-Management » Activity-Manager » jfb » tools » activitymgr » ui » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Project Management » Activity Manager » jfb.tools.activitymgr.ui.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2004-2006, Jean-François Brazeau. All rights reserved.
003:         * 
004:         * Redistribution and use in source and binary forms, with or without 
005:         * modification, are permitted provided that the following conditions are met:
006:         *
007:         *  1. Redistributions of source code must retain the above copyright notice,
008:         *     this list of conditions and the following disclaimer.
009:         * 
010:         *  2. Redistributions in binary form must reproduce the above copyright
011:         *     notice, this list of conditions and the following disclaimer in the
012:         *     documentation and/or other materials provided with the distribution.
013:         * 
014:         *  3. The name of the author may not be used to endorse or promote products
015:         *     derived from this software without specific prior written permission.
016:         *
017:         * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018:         * IMPLIEDWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020:         * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
022:         * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
023:         * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
025:         * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
026:         * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:         */
028:        package jfb.tools.activitymgr.ui.util;
029:
030:        import java.io.FileOutputStream;
031:        import java.io.IOException;
032:
033:        import org.apache.log4j.Logger;
034:        import org.apache.poi.hssf.usermodel.HSSFCell;
035:        import org.apache.poi.hssf.usermodel.HSSFCellStyle;
036:        import org.apache.poi.hssf.usermodel.HSSFRow;
037:        import org.apache.poi.hssf.usermodel.HSSFSheet;
038:        import org.apache.poi.hssf.usermodel.HSSFWorkbook;
039:        import org.apache.poi.hssf.util.HSSFColor;
040:        import org.eclipse.swt.graphics.Point;
041:        import org.eclipse.swt.widgets.FileDialog;
042:        import org.eclipse.swt.widgets.Shell;
043:        import org.eclipse.swt.widgets.Table;
044:        import org.eclipse.swt.widgets.TableColumn;
045:        import org.eclipse.swt.widgets.TableItem;
046:        import org.eclipse.swt.widgets.Tree;
047:        import org.eclipse.swt.widgets.TreeColumn;
048:        import org.eclipse.swt.widgets.TreeItem;
049:
050:        /**
051:         * Classe offrant des services d'aide à l'utilisation de l'API SWT.
052:         */
053:        public class SWTHelper {
054:
055:            /** Logger */
056:            private static Logger log = Logger.getLogger(SWTHelper.class);
057:
058:            /**
059:             * Centre un popup par rapport à sa fenêtre parent.
060:             * @param popupShell le popup.
061:             */
062:            public static void centerPopup(Shell popupShell) {
063:                // Définition de la positio du popup
064:                Point parentShellLocation = popupShell.getParent()
065:                        .getLocation();
066:                Point parentShellSize = popupShell.getParent().getSize();
067:                Point popupShellSize = popupShell.getSize();
068:                log.debug("parentShellSize = " + parentShellSize);
069:                log.debug("popupShellSize = " + popupShellSize);
070:                int x = parentShellLocation.x
071:                        + (parentShellSize.x - popupShellSize.x) / 2;
072:                int y = parentShellLocation.y
073:                        + (parentShellSize.y - popupShellSize.y) / 2;
074:                if (x < 0)
075:                    x = 0;
076:                if (y < 0)
077:                    y = 0;
078:                log.debug("x = " + x);
079:                log.debug("y = " + y);
080:                popupShell.setLocation(x, y);
081:                popupShell.setVisible(true);
082:            }
083:
084:            /**
085:             * Exporte un arbre SWT en fichier EXCEL.
086:             * @param tree l'arbre à exporter.
087:             * @throws UITechException levé en cas de pb I/O lors de la sauvegarde du fichier EXCEL.
088:             */
089:            public static void exportToWorkBook(Tree tree)
090:                    throws UITechException {
091:                // Demande du nom de fichier
092:                FileDialog fd = new FileDialog(tree.getShell());
093:                fd.setFilterExtensions(new String[] { "*.xls", "*" });
094:                String fileName = fd.open();
095:                // Si le nom est spécifié
096:                if (fileName != null) {
097:                    try {
098:                        // Correction du nom du fichier si besoin
099:                        if (!fileName.endsWith(".xls"))
100:                            fileName += ".xls";
101:                        // Sauvegarde du document
102:                        HSSFWorkbook wb = toWorkBook(tree);
103:                        FileOutputStream out = new FileOutputStream(fileName);
104:                        wb.write(out);
105:                        out.close();
106:                    } catch (IOException e) {
107:                        log.error("I/O exception", e);
108:                        throw new UITechException(
109:                                "I/O exception while exporting.", e);
110:                    }
111:                }
112:            }
113:
114:            /**
115:             * Convertit un arbre en classeur EXCEL.
116:             * @param tree l'arbre à convertir.
117:             * @return le classeur EXCEL.
118:             */
119:            public static HSSFWorkbook toWorkBook(Tree tree) {
120:                // Création du fichier EXCEL et du feuillet
121:                HSSFWorkbook wb = new HSSFWorkbook();
122:                HSSFSheet sheet = wb.createSheet("export");
123:                sheet.createFreezePane(0, 1, 0, 1);
124:                sheet.setColumnWidth((short) 0, (short) 10000);
125:                // Création du style de l'entête
126:                HSSFCellStyle headerCellStyle = wb.createCellStyle();
127:                headerCellStyle
128:                        .setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
129:                headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
130:                headerCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
131:                headerCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
132:                headerCellStyle
133:                        .setBorderLeft(headerCellStyle.getBorderBottom());
134:                headerCellStyle.setBorderRight(headerCellStyle
135:                        .getBorderBottom());
136:                headerCellStyle.setBorderTop(headerCellStyle.getBorderBottom());
137:                // Création de l'entête
138:                HSSFRow row = sheet.createRow((short) 0);
139:                TreeColumn[] columns = tree.getColumns();
140:                for (int i = 0; i < columns.length; i++) {
141:                    TreeColumn column = columns[i];
142:                    sheet.setColumnWidth((short) i,
143:                            (short) (column.getWidth() * 50));
144:                    HSSFCell cell = row.createCell((short) i);
145:                    cell.setCellValue(column.getText());
146:                    cell.setCellStyle(headerCellStyle);
147:                }
148:                // Création du style des cellules
149:                HSSFCellStyle bodyCellStyle = wb.createCellStyle();
150:                bodyCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
151:                bodyCellStyle.setBorderLeft(bodyCellStyle.getBorderBottom());
152:                bodyCellStyle.setBorderRight(bodyCellStyle.getBorderBottom());
153:                bodyCellStyle.setBorderTop(bodyCellStyle.getBorderBottom());
154:                // Exportation des lignes du tableau
155:                TreeItem[] items = tree.getItems();
156:                appendToWorkbook("", sheet, bodyCellStyle, items,
157:                        columns.length);
158:                // Retour du résultat
159:                return wb;
160:            }
161:
162:            /**
163:             * Génère des lignes dans le classeur EXCEL récursivement pour les éléments
164:             * d'arbres spécifiés et leurs éléments fils.
165:             * @param indent l'indentiation à appliquer (plus la profondeur dans l'arbre
166:             *    est élevée, plus l'indentation est longue).
167:             * @param sheet le feuillet EXCEL.
168:             * @param cellStyle le style de la cellule.
169:             * @param treeItems les élements.
170:             * @param columnsNb le nombre de colonnes à exporter dans le feuillet. 
171:             */
172:            private static void appendToWorkbook(String indent,
173:                    HSSFSheet sheet, HSSFCellStyle cellStyle,
174:                    TreeItem[] treeItems, int columnsNb) {
175:                log.debug("sheet.getLastRowNum() : " + sheet.getLastRowNum());
176:                int startRowNum = sheet.getLastRowNum() + 1;
177:                for (int i = 0; i < treeItems.length; i++) {
178:                    TreeItem treeItem = treeItems[i];
179:                    log.debug(" +-> TreeItem : " + i + ", expanded="
180:                            + treeItem.getExpanded() + ", data='"
181:                            + treeItem.getData() + "'");
182:                    if (treeItem.getData() != null) {
183:                        HSSFRow row = sheet.createRow((short) (sheet
184:                                .getLastRowNum() + 1));
185:                        String rowName = treeItem.getText(0);
186:                        log.debug("  +-> Row : '" + rowName + "'");
187:                        HSSFCell cell = row.createCell((short) 0);
188:                        cell.setCellValue(indent + rowName);
189:                        cell.setCellStyle(cellStyle);
190:                        for (int j = 1; j < columnsNb; j++) {
191:                            log.debug("  +-> Cell : " + j + ", '"
192:                                    + treeItem.getText(j) + "'");
193:                            cell = row.createCell((short) j);
194:                            cell.setCellStyle(cellStyle);
195:                            String cellValue = treeItem.getText(j);
196:                            try {
197:                                cell.setCellValue(Integer.parseInt(cellValue));
198:                            } catch (NumberFormatException e0) {
199:                                try {
200:                                    cell.setCellValue(Double
201:                                            .parseDouble(cellValue));
202:                                } catch (NumberFormatException e1) {
203:                                    cell.setCellValue(cellValue);
204:                                }
205:                            }
206:                        }
207:                        if (treeItem.getExpanded())
208:                            appendToWorkbook(indent + "    ", sheet, cellStyle,
209:                                    treeItem.getItems(), columnsNb);
210:                    }
211:                }
212:                int endRowNum = sheet.getLastRowNum();
213:                log.debug("startRowNum=" + startRowNum + ", endRowNum="
214:                        + endRowNum);
215:                if (!"".equals(indent) && (endRowNum - startRowNum >= 1)) {
216:                    log.debug(" -> grouped!");
217:                    sheet.groupRow((short) startRowNum, (short) endRowNum);
218:                }
219:            }
220:
221:            /**
222:             * Exporte un tableau SWT en fichier EXCEL.
223:             * @param table le tableau à exporter.
224:             * @throws UITechException levé en cas de pb I/O lors de la sauvegarde du fichier EXCEL.
225:             */
226:            public static void exportToWorkBook(Table table)
227:                    throws UITechException {
228:                // Demande du nom de fichier
229:                FileDialog fd = new FileDialog(table.getShell());
230:                fd.setFilterExtensions(new String[] { "*.xls", "*" });
231:                String fileName = fd.open();
232:                // Si le nom est spécifié
233:                if (fileName != null) {
234:                    try {
235:                        // Correction du nom du fichier si besoin
236:                        if (!fileName.endsWith(".xls"))
237:                            fileName += ".xls";
238:                        // Sauvegarde du document
239:                        HSSFWorkbook wb = toWorkBook(table);
240:                        FileOutputStream out = new FileOutputStream(fileName);
241:                        wb.write(out);
242:                        out.close();
243:                    } catch (IOException e) {
244:                        log.error("I/O exception", e);
245:                        throw new UITechException(
246:                                "I/O exception while exporting.", e);
247:                    }
248:                }
249:            }
250:
251:            /**
252:             * Convertit un tableau en classeur EXCEL.
253:             * @param table le tableau à convertir.
254:             * @return le classeur EXCEL.
255:             */
256:            public static HSSFWorkbook toWorkBook(Table table) {
257:                // Création du fichier EXCEL et du feuillet
258:                HSSFWorkbook wb = new HSSFWorkbook();
259:                HSSFSheet sheet = wb.createSheet("export");
260:                sheet.createFreezePane(0, 1, 0, 1);
261:                // Création du style de l'entête
262:                HSSFCellStyle headerCellStyle = wb.createCellStyle();
263:                headerCellStyle
264:                        .setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
265:                headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
266:                headerCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
267:                headerCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
268:                headerCellStyle
269:                        .setBorderLeft(headerCellStyle.getBorderBottom());
270:                headerCellStyle.setBorderRight(headerCellStyle
271:                        .getBorderBottom());
272:                headerCellStyle.setBorderTop(headerCellStyle.getBorderBottom());
273:                // Création de l'entête
274:                HSSFRow row = sheet.createRow((short) 0);
275:                TableColumn[] columns = table.getColumns();
276:                for (int i = 0; i < columns.length; i++) {
277:                    TableColumn column = columns[i];
278:                    sheet.setColumnWidth((short) i,
279:                            (short) (column.getWidth() * 50));
280:                    HSSFCell cell = row.createCell((short) i);
281:                    cell.setCellValue(column.getText());
282:                    cell.setCellStyle(headerCellStyle);
283:                }
284:                // Création du style des cellules
285:                HSSFCellStyle bodyCellStyle = wb.createCellStyle();
286:                bodyCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
287:                bodyCellStyle.setBorderLeft(bodyCellStyle.getBorderBottom());
288:                bodyCellStyle.setBorderRight(bodyCellStyle.getBorderBottom());
289:                bodyCellStyle.setBorderTop(bodyCellStyle.getBorderBottom());
290:                // Exportation des lignes du tableau
291:                TableItem[] items = table.getItems();
292:                appendToWorkbook(sheet, bodyCellStyle, items, columns.length);
293:                // Retour du résultat
294:                return wb;
295:            }
296:
297:            /**
298:             * Génère des lignes dans le classeur EXCEL pour les éléments d'un tableau.
299:             * @param sheet le feuillet EXCEL.
300:             * @param cellStyle le style de la cellule.
301:             * @param tableItems les élements.
302:             * @param columnsNb le nombre de colonnes à exporter dans le feuillet. 
303:             */
304:            private static void appendToWorkbook(HSSFSheet sheet,
305:                    HSSFCellStyle cellStyle, TableItem[] tableItems,
306:                    int columnsNb) {
307:                log.debug("sheet.getLastRowNum() : " + sheet.getLastRowNum());
308:                int startRowNum = sheet.getLastRowNum() + 1;
309:                for (int i = 0; i < tableItems.length; i++) {
310:                    TableItem tableItem = tableItems[i];
311:                    log.debug(" +-> TreeItem : " + i + ", data='"
312:                            + tableItem.getData() + "'");
313:                    if (tableItem.getData() != null) {
314:                        HSSFRow row = sheet.createRow((short) (sheet
315:                                .getLastRowNum() + 1));
316:                        String rowName = tableItem.getText(0);
317:                        log.debug("  +-> Row : '" + rowName + "'");
318:                        HSSFCell cell = row.createCell((short) 0);
319:                        cell.setCellValue(rowName);
320:                        cell.setCellStyle(cellStyle);
321:                        for (int j = 1; j < columnsNb; j++) {
322:                            log.debug("  +-> Cell : " + j + ", '"
323:                                    + tableItem.getText(j) + "'");
324:                            cell = row.createCell((short) j);
325:                            cell.setCellStyle(cellStyle);
326:                            String cellValue = tableItem.getText(j);
327:                            try {
328:                                cell.setCellValue(Integer.parseInt(cellValue));
329:                            } catch (NumberFormatException e0) {
330:                                try {
331:                                    cell.setCellValue(Double
332:                                            .parseDouble(cellValue));
333:                                } catch (NumberFormatException e1) {
334:                                    cell.setCellValue(cellValue);
335:                                }
336:                            }
337:                        }
338:                    }
339:                }
340:                int endRowNum = sheet.getLastRowNum();
341:                log.debug("startRowNum=" + startRowNum + ", endRowNum="
342:                        + endRowNum);
343:            }
344:
345:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.