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
|
package EVENT;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import IHM.IhmConverter;
/**
* Some utility methods for the application
*
* @author t0163126
*/
public class UtilityMethods {
private static File file;
private static final XSSFWorkbook wb = new XSSFWorkbook(file);
// le new XSSFWorkbook(file); pose problème.
private static XSSFSheet sheet;
private static XSSFCell cell;
private static XSSFRow row;
/**
* Create tabs based on file and panel
*
* @param tabPanel
* @param file
* @return
*/
public static JTabbedPane createJTabbed(File file) {
JTabbedPane tabbedPane = new JTabbedPane();
List<JPanel> lstPanel = createTabPanel(file);
for (JPanel panel : lstPanel) {
tabbedPane.addTab(panel.getName(), // tab name
null, // tab icon
panel, // Tabs panel for the JTable
panel.getToolTipText() // tab tool tip text
);
}
return tabbedPane;
}
/**
* Create a tab panel by excel feel
*
* @param fileName
* @param excelFile
* @return
*/
public static List<JPanel> createTabPanel(File file) {
JTable spreadsheet = new JTable();
JPanel panel;
List<JPanel> lstPanel = new ArrayList<JPanel>();
try {
// Get the sheet number
int nbFeuilles = wb.getNumberOfSheets();
for (int i = 0; i < nbFeuilles; i++) {
sheet = wb.getSheetAt(i);
// Create one JTable by excel feel
spreadsheet = createJTableWithExcel(excelFile, i);
spreadsheet.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
JScrollPane elevator = new JScrollPane();
elevator.setViewportView(spreadsheet);
// Create a panel by excel feel number
panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(sheet
.getSheetName()));
panel.setBackground(Color.LIGHT_GRAY);
panel.add(elevator);
lstPanel.add(panel);
}
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
return lstPanel;
}
/**
* Create a JTable depending on the file and the entry sheet number
*
* @param file
* @param numSheet
* @return
*/
public static JTable createJTableWithExcel(int numSheet) {
// Create new table
JTable table = new JTable();
// Reading the excel file
try {
// Get the contents of the excel file according to the sheet number
sheet = wb.getSheetAt(numSheet);
// Count the line number
int nbRows = sheet.getLastRowNum();
// Count the column number
int nbColumns = numberMaxColumn(sheet);
System.out.println("In the sheet " + sheet.getSheetName()
+ " there are " + nbRows + " lines and " + nbColumns
+ " columns");
// Create new object array
Object[][] obj = new Object[nbRows][nbColumns];
// Array header
String[] title = new String[nbColumns];
for (int i = 0; i < nbColumns; i++) {
title[i] = "#" + i;
}
// Browsing the sheet and retrieves the lines one by one
for (int rowContent = 0; rowContent < nbRows; rowContent++) {
row = sheet.getRow(rowContent);
// Browsing the line and retrieves the columns
if (row != null) {
for (int columnContent = 0; columnContent < nbColumns; columnContent++) {
// Retrieves the cell and this value
cell = row.getCell((short) columnContent);
Object value = contentCell(cell);
// System.out.println(value.toString());
obj[rowContent][columnContent] = value;
}
}
}
wb.close();
table.setModel(new DefaultTableModel(obj, title));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return table;
}
/**
* The cell can contain different type of value that should be specifically
* recovered
*
* @param cell
* @return
*/
private static Object contentCell(XSSFCell c) {
Object value = null;
if (c == null) {
value = "";
} else if (c.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
value = c.getBooleanCellValue();
} else if (c.getCellType() == XSSFCell.CELL_TYPE_ERROR) {
value = c.getErrorCellValue();
} else if (c.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
value = c.getCellFormula();
} else if (c.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
value = c.getNumericCellValue();
} else if (c.getCellType() == XSSFCell.CELL_TYPE_STRING) {
value = c.getStringCellValue();
}
return value;
}
/**
* Retrieves the maximum number of column. If a line has 2 empty boxes at
* the end , they are not taken into account. So to retrieve the number of
* columns to initialize our table we must browse all lines
*
* @param sheet
* @return
*/
private static int numberMaxColumn(XSSFSheet st) {
int r = st.getLastRowNum();
int max = 0;
int s = 0;
while (s < r) {
if (st.getRow(s) != null) {
int c = st.getRow(s).getLastCellNum();
if (c > max) {
max = c;
}
}
s++;
}
return max + 1;
}
/**
* Closing the application
*/
public static void closeApplication() {
IhmConverter ihm = IhmConverter.getInstance();
Object[] options = {
"Yes, please", "Oops no!"
};
int retour = JOptionPane.showOptionDialog(ihm, Final.getMsgExit(),
Final.getMsgExitTitle(), JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (retour == JOptionPane.YES_OPTION) {
ihm.setVisible(false);
ihm.dispose();
System.exit(0);
} else if (retour == JOptionPane.NO_OPTION) {
// do nothing
}
}
} |
Partager