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
| package projetPerso;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
public class PersoExtractor extends JFrame
{
private final static String FILE_PATH = "E:\\2014-2016\\franglais\\vocabulaire_anglais.docx";
private JSplitPane splitHorizontal;
private JSplitPane splitVertical;
private JPanel jPanel = null;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTree tree;
private XWPFDocument document;
private XWPFWordExtractor we;
private List<XWPFTable> tables;
private XWPFTableRow row;
private String[] data = new String[]{};
private String[] names = new String[]{};
private String col1 = "";
private String col2 = "";
private int tableIndex = 0;
private int nbTables = 0;
private int numberOfCell;
/**
* <p>Constructor no-argument</p>
*
* @throws Exception
*/
public PersoExtractor() throws Exception
{
this.setTitle("FrAnglais");
this.getContentPane().add(getJContentPane());
this.pack();
this.setVisible(true);
}
private List<XWPFTable> readAllTables() throws Exception
{
tableIndex = 0;
List<XWPFTable> listTables = new ArrayList<XWPFTable>();
document = new XWPFDocument (
new FileInputStream(FILE_PATH));
// using XWPFWordExtractor Class
we = new XWPFWordExtractor(document);
tables = document.getTables();
//index of tables
for (XWPFTable table : tables)
{
nbTables = tables.size();
//table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
//index of rows
while (tableIndex <= nbTables)
{
names = new String[] {"Table "+ tableIndex};
tableIndex++;
//break;
listTables.add(table);
}
}
return listTables;
}
/*private JScrollPane getJScrollPane(JTree tree)
{
if (jScrollPane == null)
{
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(tree);
}
return jScrollPane;
}*/
private JTable createJTable(String name, String[] values)
{
JTable table = new JTable();
table.setName(name);
table.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addColumn("French");
model.addColumn("English");
String[] data = new String[values.length];
for (int cellContent = 0; cellContent <= values.length; cellContent++)
{
data = new String[cellContent];
model.addRow(data);
}
return table;
}
private JPanel resizeJTableColumns(JTable table, String name)
{
JPanel panel = new JPanel();
if (table != null) {
table.setEnabled(false);
//table.setFont(Final.getFont());
// table column adjust
int col = 0, larg = 0, row = 0, width = 0;
JTableHeader header = table.getTableHeader();
Enumeration<TableColumn> columns = table
.getColumnModel().getColumns();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
while (columns.hasMoreElements()) { // maximum length of the
// text or title of a
// column
TableColumn column = (TableColumn) columns
.nextElement();
col = header.getColumnModel().getColumnIndex(
column.getIdentifier());
width = (int) table
.getTableHeader()
.getDefaultRenderer()
.getTableCellRendererComponent(table,
column.getIdentifier(), false, false,
-1, col).getPreferredSize().getWidth();
for (row = 0; row < table.getRowCount(); row++) {
int preferedWidth = (int) table
.getCellRenderer(row, col)
.getTableCellRendererComponent(table,
table.getValueAt(row, col),
false, false, row, col)
.getPreferredSize().getWidth();
width = Math.max(width, preferedWidth);
}
header.setResizingColumn(column);
// System.out.println("column : "+ column);
larg = width + table.getIntercellSpacing().width;
larg = larg + 20;
column.setWidth(larg);
}
JScrollPane elevator = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
elevator.getViewport().setBackground(Color.WHITE);
panel.setLayout(new BorderLayout());
// TitledBorder
panel.setBorder(BorderFactory.createTitledBorder(name));
panel.setName(name);
panel.setBackground(Color.LIGHT_GRAY);
// on lui ajoute un ascenseur
panel.add(elevator, BorderLayout.CENTER);
}
return panel;
}
/**
* <p>Content Pane</p>
*
* @return ContentPane
* @throws Exception
*/
private JPanel getJContentPane() throws Exception
{
//Create the panel
JPanel panel = getJPanel();
//Create the tree
tree = new MyJTree(getNames());
//Separated the application
splitHorizontal = new JSplitPane (
JSplitPane.HORIZONTAL_SPLIT,
tree, panel);
splitHorizontal.setDividerLocation(175);
splitHorizontal.setOneTouchExpandable(true);
if (jContentPane == null)
{
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(splitHorizontal, BorderLayout.CENTER);
//jContentPane.add(getJButton(), BorderLayout.EAST);
}
return jContentPane;
}
private JPanel getJPanel() throws Exception
{
if (jPanel == null)
{
jPanel = new JPanel();
JTable viewTable = new JTable();
List<XWPFTable> tables = readAllTables();
//Index of tables
tableIndex = 0;
for (XWPFTable table : tables)
{
//index of rows
for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++)
{
row = table.getRow(rowIndex);
numberOfCell = row.getTableCells().size();
data = new String[numberOfCell];
//index of columns
for (int colIndex = 0; colIndex < numberOfCell; colIndex++)
{
//column
col1 = table.getRow(rowIndex).getCell(0).getText();
col2 = table.getRow(rowIndex).getCell(1).getText();
data = new String[] {col1, col2};
}
}
tableIndex++;
}
//Create the tables
tableIndex = 0;
while (tableIndex <= nbTables)
{
//Create the tables
String name = "Table "+ tableIndex;
viewTable = createJTable(name, data);
jPanel = resizeJTableColumns(viewTable, name);
jPanel.setName(name);
tableIndex++;
//break;
}
}
we.close();
document.close();
return jPanel;
}
//========================================
//getter and setter
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
} |
Partager