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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
package EVENT;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.BorderFactory;
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.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import IHM.IhmConverter;
/**
* Some utility methods for the application
*
* @author t0163126
*/
public class UtilityMethods {
private Workbook wb;
private Sheet sheet;
private Cell cell;
private Row row;
// Constructor
public UtilityMethods() {
}
/**
* Create tabs based on file and panel
*
* @param tabPanel
* @param file
* @return
*/
public boolean populateJTabbedPane(File file) {
// Celui-ci est l'onglet principal, c'est à lui que les sous-onglets
// reviennent
final JTabbedPane tabbedPrincipal = IhmConverter.getHDMT();
try {
wb = WorkbookFactory.create(file);
// on récupère ses sous-onglets grâce à la méthode createPanels
List<JPanel> lstPanel = createPanels(wb);
if (lstPanel.isEmpty()) {
return false;
} else {
// on ajoute les sous-onglets à l'onglet principal au fur et à
// mesure
for (JPanel panel : lstPanel) {
tabbedPrincipal.addTab(panel.getName(), panel);
}
// maintenant qu'il est rempli de sous onglet je met un
// changeListener
tabbedPrincipal.addChangeListener(new Changes(tabbedPrincipal));
}
wb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (EncryptedDocumentException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
return true;
}
/**
* Create a tab panel by excel feel
*
* @param fileName
* @param excelFile
* @return panel list
*/
private List<JPanel> createPanels(Workbook workbook) {
JTable spreadsheet = null;
// celui-ci est un sous-onglet auquel on va ajouter à l'onglet principal
JPanel panel = null;
// celui-ci va récupérer la list de tous les sous-onglets
List<JPanel> lstPanel = new ArrayList<JPanel>();
int nbFeuilles = 0;
try {
final Double ratio = 0.009900990099009901;
ZipSecureFile.setMinInflateRatio(ratio);
nbFeuilles = workbook.getNumberOfSheets();
for (int i = 0; i < nbFeuilles; i++) {
sheet = workbook.getSheetAt(i);
// Create one spreadsheet by excel feel
spreadsheet = sheetToJTable(sheet);
if (spreadsheet != null) {
spreadsheet.setEnabled(false);
spreadsheet.setFont(Final.getFont());
spreadsheet
.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
JScrollPane elevator = new JScrollPane(spreadsheet,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
elevator.getViewport().setBackground(Color.WHITE);
// Create a tab by excel feel number
panel = new JPanel();
panel.setLayout(new BorderLayout());
// TitledBorder
panel.setBorder(BorderFactory.createTitledBorder(sheet
.getSheetName()));
panel.setName(sheet.getSheetName());
panel.setBackground(Color.LIGHT_GRAY);
// on lui ajoute un ascenseur
panel.add(elevator, BorderLayout.CENTER);
lstPanel.add(panel);
}
}
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (EncryptedDocumentException e) {
e.printStackTrace();
}
return lstPanel;
}
/**
* Create a JTable depending on the file and the entry sheet number
*
* @param file
* @param numSheet
* @return the table
*/
private JTable sheetToJTable(Sheet sheet) {
// Create new table
JTable table = new JTable();
// Reading the excel file
// Count the line number
int nbRows = sheet.getLastRowNum();
// Count the column number
int nbColumns = numberMaxColumn(sheet);
// Create new object array
Object[][] obj = new Object[nbRows][nbColumns];
// table header
String[] header = new String[nbColumns];
// 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 but begging at "dbs"
cell = row.getCell((short) columnContent);
final Object value = contentCell(cell);
obj[rowContent][columnContent] = value;
}
}
}
// recovery of adequate lines
final Object[][] recorvery = new Object[nbRows][nbColumns];
int index = -1; // indicates the index of the next line to recover, except of -1
int hideColumn = 0; //memorize the DBS column for hide it
for (int i = 0; i < nbRows; i++) {
if (index < 0) { // we found nothing, so we are searching
for (int j = 0; j < nbColumns; j++) {
if ("dbs".equals(obj[i][j])) { // we found a line that contains "dbs"
index = 0;
hideColumn = j;
break; // needn't to search in this line
}
}
if (index == 0) { // the first line including "dbs" : the headers
// Recovering the headers
for (int j = 0; j < nbColumns; j++) {
header[j] = String.valueOf(obj[i][j]);
}
}
}
if (index >= 0) { // we found a line that contained "dbs", then copy
// copy the lines after the first that contains "dbs" including this one
for (int j = 0; j < nbColumns; j++) { // copy the full line
recorvery[index][j] = obj[i][j];
}
index++; // we advance in the recovery table
}
}
Object [] r = recorvery;
for (int j = 0; j < r.length-1; j++) {
}
if (index >= 0) {
// we found lines : are recovered
Object[][] result = Arrays.copyOf(recorvery, index); // index corresponds to the number of rows found
Object[][] newResult = copyArray(result, hideColumn); //copy with a hide column
table.setModel(new DefaultTableModel(newResult, header));
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
} else {
// if we don't found "dbs"
return null;
}
return table;
}
private Object[][] copyArray(Object[][] source, int...columnsToRemove) {
Set<Integer> columns = new HashSet<Integer>();
for(int i : columnsToRemove) {
columns.add(i);
}
final Object[][] target = new Object[source.length][];
for(int indexLine = 0; indexLine<source.length; indexLine++) {
final Object[] row = source[indexLine];
target[indexLine] = new Object[row.length-columns.size()];
int indexColumnTarget = 0;
for(int indexColonneSource=0; indexColonneSource<row.length; indexColonneSource++) {
if ( !columns.contains(indexColonneSource) ) {
target[indexLine][indexColumnTarget++] = source[indexLine][indexColonneSource];
}
}
}
return target;
}
/**
* The cell can contain different type of value that should be specifically
* recovered
*
* @param cell
* @return
*/
private Object contentCell(Cell c) {
Object value = null;
if (c == null) {
value = "";
} else if (c.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
value = c.getBooleanCellValue();
} else if (c.getCellType() == Cell.CELL_TYPE_ERROR) {
value = c.getErrorCellValue();
} else if (c.getCellType() == Cell.CELL_TYPE_FORMULA) {
value = c.getCellFormula();
} else if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
value = c.getNumericCellValue();
} else if (c.getCellType() == Cell.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 int numberMaxColumn(Sheet 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;
}
} |
Partager