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
|
package EVENT;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
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 XSSFWorkbook wb;
private XSSFSheet sheet;
private XSSFCell cell;
private XSSFRow row;
private JButton btnYes;
private JButton btnNo;
// Constructor
public UtilityMethods() {
}
//Getter for the yes and no button
public JButton getBtnYes() {
return this.btnYes;
}
public JButton getBtnNo() {
return this.btnNo;
}
/**
* Create tabs based on file and panel
*
* @param tabPanel
* @param file
* @return
*/
public 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
);
IhmConverter.getHDMT().add(tabbedPane);
}
return tabbedPane;
}
/**
* Create a tab panel by excel feel
*
* @param fileName
* @param excelFile
* @return
*/
public List<JPanel> createTabPanel(File file) {
JTable spreadsheet = new JTable();
JPanel panel;
List<JPanel> lstPanel = new ArrayList<JPanel>();
try {
wb = new XSSFWorkbook(file);
// 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(sheet);
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 (InvalidFormatException e) {
e.printStackTrace();
} 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 JTable createJTableWithExcel(XSSFSheet 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);
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);
obj[rowContent][columnContent] = value;
}
}
}
table.setModel(new DefaultTableModel(obj, title));
return table;
}
/**
* The cell can contain different type of value that should be specifically
* recovered
*
* @param cell
* @return
*/
private 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 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
*
* @param ihm
*/
public void closeApplication(final IhmConverter ihm) {
btnYes = new JButton();
btnYes.addActionListener(new ActionEvents(this));
btnYes.setText("Yes, please");
btnYes.setToolTipText("Click Yes to quit the application");
btnYes.setMnemonic('Y');
btnNo = new JButton();
btnNo.addActionListener(new ActionEvents(this));
btnNo.setText("Oops no!");
btnNo.setToolTipText("Click No to return in the application");
btnNo.setMnemonic('N');
final Object[] options = { btnYes, btnNo };
JOptionPane optionPane = new JOptionPane(Final.getMsgExit(), // the message
JOptionPane.QUESTION_MESSAGE, // the type of message
JOptionPane.YES_NO_OPTION, // the type of dialog box
null, // A icon
options, // Array String for buttons text
options[0] // Where 0 is the index of preset button - one that
// will be chosen if we press Enter
);
JDialog dialog = optionPane.createDialog(Final.getMsgExitTitle());
dialog.setAlwaysOnTop(true);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.toFront();
dialog.setFocusable(true);
dialog.setVisible(true);
dialog.addKeyListener(new ActionEvents(ihm));
}
} |
Partager