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
| package projetPerso;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.FileInputStream;
import java.util.ArrayList;
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.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 = "F:\\2012-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)
{
names = new String[] {"Table "+ tableIndex};
listTables.add(table);
tableIndex++;
}
return listTables;
}
/*private JScrollPane getJScrollPane(JTree tree)
{
if (jScrollPane == null)
{
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(tree);
}
return jScrollPane;
}*/
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 tree
MyJTree jTree = new MyJTree(getNames());
tree = jTree;
//Create a panel by table
List<XWPFTable> tables = readAllTables();
for (int i = 0; i < tables.size(); i++)
{
jPanel = createJPanel(i);
if (jPanel.getName().equals(jTree.getPath()))
System.out.println("cool");
}
//Separated the application
splitHorizontal = new JSplitPane (
JSplitPane.HORIZONTAL_SPLIT,
tree, jPanel);
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;
}
/**
* <p>Create a JTable by JPanel</p>
*
* @return the panel
* @throws Exception
*/
private JPanel createJPanel(int tableIndex) throws Exception
{
if (jPanel == null)
{
jPanel = new JPanel();
JTable viewTable = new JTable();
List<XWPFTable> tables = readAllTables();
//index of rows
for (int rowIndex = 0; rowIndex < tables.get(tableIndex).getNumberOfRows(); rowIndex++)
{
col1 = tables.get(tableIndex).getRow(rowIndex).getCell(0).getText();
col2 = tables.get(tableIndex).getRow(rowIndex).getCell(1).getText();
String[] row = new String[]{col1, col2};
data = new String[][] {row};
//Create the tables
String name = "Table "+ tableIndex;
viewTable = createJTable(name, data);
//Create the panels
jPanel = resizeJTableColumns(viewTable, name);
jPanel.setName(name);
}
}
//System.out.println(tables.get(0).getText());
we.close();
document.close();
return jPanel;
}
/**
* <p>Create the JTable</p>
*
* @param name
* @param values
*
* @return the table
*/
private JTable createJTable(String name, String[][] values)
{
String[] columnNames = { "French", "English" };
JTable table = new JTable(values, columnNames);
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");
model.addRow(values);*/
return table;
}
//========================================
//getter and setter
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
} |
Partager