001: package org.netbeans.modules.mashup.tables.wizard;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.InputStream;
006: import java.net.URL;
007: import java.util.Vector;
008: import javax.swing.InputVerifier;
009: import javax.swing.JComponent;
010: import javax.swing.JPanel;
011: import javax.swing.JTextField;
012: import javax.swing.SwingUtilities;
013: import javax.swing.table.DefaultTableModel;
014: import jxl.Cell;
015: import jxl.Sheet;
016: import jxl.Workbook;
017: import jxl.WorkbookSettings;
018: import net.java.hulp.i18n.Logger;
019: import org.netbeans.modules.etl.logger.Localizer;
020: import org.netbeans.modules.etl.logger.LogUtil;
021:
022: public final class SpreadsheetChooserVisualPanel extends JPanel {
023:
024: private SpreadsheetChooserPanel owner;
025: private static transient final Logger mLogger = LogUtil
026: .getLogger(SpreadsheetChooserVisualPanel.class.getName());
027: private static transient final Localizer mLoc = Localizer.get();
028: private boolean canAdvance = false;
029:
030: private int columns = -1;
031:
032: private String file;
033:
034: class NumberVerifier extends InputVerifier {
035: public boolean verify(JComponent input) {
036: String inp = ((JTextField) input).getText().trim();
037: try {
038: int i = Integer.parseInt(inp);
039: if (i > 100) {
040: return false;
041: }
042: } catch (NumberFormatException ex) {
043: return false;
044: }
045: return true;
046: }
047: }
048:
049: /**
050: * Creates new form SpreadsheetChooserVisualPanel
051: */
052: public SpreadsheetChooserVisualPanel(SpreadsheetChooserPanel panel) {
053: owner = panel;
054: initComponents();
055: rowsToShow.setInputVerifier(new NumberVerifier());
056: }
057:
058: @Override
059: public String getName() {
060: String nbBundle1 = mLoc.t("PRSR001: Choose a Sheet");
061: return Localizer.parse(nbBundle1);
062: }
063:
064: public void populateSheets(String fileName) {
065: file = fileName;
066: String[] sheets = null;
067: try {
068: // parse xls file and get results.
069: InputStream in = null;
070: File f = new File(fileName);
071: if (f.exists()) {
072: in = new FileInputStream(f);
073: } else {
074: in = new URL(fileName).openStream();
075: }
076: Workbook spreadsheetData = Workbook.getWorkbook(in,
077: getWorkbookSettings());
078: sheets = spreadsheetData.getSheetNames();
079: if (sheets.length != 0) {
080: totalRows.setText("Total Rows: "
081: + String.valueOf(spreadsheetData.getSheet(0)
082: .getRows()));
083: }
084: in.close();
085: } catch (Exception ex) {
086: ex.printStackTrace();
087: }
088: setSheetNames(sheets);
089: }
090:
091: private void setSheetNames(String[] sheetNames) {
092: sheetCombo.removeAllItems();
093: for (String sheetName : sheetNames) {
094: sheetCombo.addItem(sheetName);
095: }
096:
097: if (sheetCombo.getItemCount() != 0) {
098: sheetCombo.setSelectedIndex(0);
099: canAdvance = true;
100: }
101: }
102:
103: public String getSelectedSheetName() {
104: if (sheetCombo.getItemCount() != 0) {
105: return (String) sheetCombo.getSelectedItem();
106: }
107: return "";
108: }
109:
110: private WorkbookSettings getWorkbookSettings() {
111: WorkbookSettings settings = new WorkbookSettings();
112: settings.setDrawingsDisabled(true);
113: settings.setAutoFilterDisabled(true);
114: settings.setSuppressWarnings(true);
115: settings.setNamesDisabled(true);
116: settings.setIgnoreBlanks(true);
117: settings.setCellValidationDisabled(true);
118: settings.setFormulaAdjust(false);
119: settings.setPropertySets(false);
120: return settings;
121: }
122:
123: private DefaultTableModel getDataForSheet(String sheetName) {
124: DefaultTableModel model = new DefaultTableModel();
125: int colCount = -1;
126: try {
127: FileInputStream in = new FileInputStream(new File(file));
128: int max = 10;
129: try {
130: max = Integer.parseInt(rowsToShow.getText().trim());
131: } catch (NumberFormatException ex) {
132: max = 10;
133: rowsToShow.setText("10");
134: }
135: Workbook spreadSheetData = Workbook.getWorkbook(in,
136: getWorkbookSettings());
137: Sheet sheet = spreadSheetData.getSheet(sheetName);
138: for (int i = 0; i < sheet.getRows(); i++) {
139: if (i == max) {
140: break;
141: }
142: Cell[] cells = sheet.getRow(i);
143: model.setColumnCount(cells.length);
144: Vector rowVector = new Vector();
145: for (int j = 0; j < cells.length; j++) {
146: rowVector.add(cells[j].getContents());
147: }
148: model.addRow(rowVector);
149: if (colCount == -1) {
150: colCount = cells.length;
151: }
152: }
153: spreadSheetData.close();
154: if (in != null) {
155: in.close();
156: }
157: } catch (Exception ex) {
158: ex.printStackTrace();
159: }
160:
161: // set the column headers.
162: if (colCount != -1) {
163: columns = colCount;
164: Vector headerVector = new Vector();
165: for (int i = 0; i < colCount; i++) {
166: headerVector.add("Column " + String.valueOf(i + 1));
167: }
168: model.setColumnIdentifiers(headerVector);
169: }
170: return model;
171: }
172:
173: /** This method is called from within the constructor to
174: * initialize the form.
175: * WARNING: Do NOT modify this code. The content of this method is
176: * always regenerated by the Form Editor.
177: */
178: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
179: private void initComponents() {
180: jPanel2 = new javax.swing.JPanel();
181: jButton1 = new javax.swing.JButton();
182: jScrollPane1 = new javax.swing.JScrollPane();
183: jTable1 = new javax.swing.JTable();
184: totalRows = new javax.swing.JLabel();
185: jLabel3 = new javax.swing.JLabel();
186: rowsToShow = new javax.swing.JTextField();
187: error = new javax.swing.JLabel();
188: jLabel1 = new javax.swing.JLabel();
189: sheetCombo = new javax.swing.JComboBox();
190:
191: setMaximumSize(new java.awt.Dimension(500, 500));
192: setMinimumSize(new java.awt.Dimension(100, 100));
193: setPreferredSize(new java.awt.Dimension(415, 220));
194: org.openide.awt.Mnemonics.setLocalizedText(jButton1, "Preview");
195: jButton1.addActionListener(new java.awt.event.ActionListener() {
196: public void actionPerformed(java.awt.event.ActionEvent evt) {
197: jButton1ActionPerformed(evt);
198: }
199: });
200:
201: jScrollPane1.setBorder(javax.swing.BorderFactory
202: .createTitledBorder(" Preview"));
203: jTable1.setModel(new javax.swing.table.DefaultTableModel(
204: new Object[][] {
205:
206: }, new String[] {
207:
208: }));
209: jScrollPane1.setViewportView(jTable1);
210:
211: org.openide.awt.Mnemonics.setLocalizedText(totalRows,
212: "Total Rows:");
213:
214: org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
215: "Rows to Show:");
216:
217: rowsToShow.setText("10");
218:
219: error.setForeground(new java.awt.Color(255, 51, 102));
220:
221: org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Sheet");
222:
223: sheetCombo
224: .addActionListener(new java.awt.event.ActionListener() {
225: public void actionPerformed(
226: java.awt.event.ActionEvent evt) {
227: sheetComboActionPerformed(evt);
228: }
229: });
230:
231: org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(
232: jPanel2);
233: jPanel2.setLayout(jPanel2Layout);
234: jPanel2Layout
235: .setHorizontalGroup(jPanel2Layout
236: .createParallelGroup(
237: org.jdesktop.layout.GroupLayout.LEADING)
238: .add(
239: jPanel2Layout
240: .createSequentialGroup()
241: .add(
242: jPanel2Layout
243: .createParallelGroup(
244: org.jdesktop.layout.GroupLayout.LEADING)
245: .add(
246: jPanel2Layout
247: .createSequentialGroup()
248: .add(
249: jPanel2Layout
250: .createParallelGroup(
251: org.jdesktop.layout.GroupLayout.TRAILING,
252: false)
253: .add(
254: org.jdesktop.layout.GroupLayout.LEADING,
255: error,
256: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
257: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
258: Short.MAX_VALUE)
259: .add(
260: org.jdesktop.layout.GroupLayout.LEADING,
261: jPanel2Layout
262: .createSequentialGroup()
263: .add(
264: jLabel1)
265: .addPreferredGap(
266: org.jdesktop.layout.LayoutStyle.RELATED,
267: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
268: Short.MAX_VALUE)
269: .add(
270: sheetCombo,
271: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
272: 136,
273: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
274: .add(
275: org.jdesktop.layout.GroupLayout.LEADING,
276: jPanel2Layout
277: .createSequentialGroup()
278: .add(
279: totalRows)
280: .add(
281: 79,
282: 79,
283: 79)
284: .add(
285: jLabel3)))
286: .add(
287: 18,
288: 18,
289: 18)
290: .add(
291: rowsToShow,
292: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
293: 27,
294: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
295: .addPreferredGap(
296: org.jdesktop.layout.LayoutStyle.RELATED,
297: 88,
298: Short.MAX_VALUE)
299: .add(
300: jButton1))
301: .add(
302: jScrollPane1,
303: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
304: 412,
305: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
306: .addContainerGap()));
307: jPanel2Layout
308: .setVerticalGroup(jPanel2Layout
309: .createParallelGroup(
310: org.jdesktop.layout.GroupLayout.LEADING)
311: .add(
312: org.jdesktop.layout.GroupLayout.TRAILING,
313: jPanel2Layout
314: .createSequentialGroup()
315: .addContainerGap(
316: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
317: Short.MAX_VALUE)
318: .add(
319: jPanel2Layout
320: .createParallelGroup(
321: org.jdesktop.layout.GroupLayout.BASELINE)
322: .add(jLabel1)
323: .add(
324: sheetCombo,
325: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
326: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
327: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
328: .addPreferredGap(
329: org.jdesktop.layout.LayoutStyle.RELATED)
330: .add(
331: jPanel2Layout
332: .createParallelGroup(
333: org.jdesktop.layout.GroupLayout.BASELINE)
334: .add(totalRows)
335: .add(jLabel3)
336: .add(
337: rowsToShow,
338: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
339: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
340: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
341: .add(jButton1))
342: .addPreferredGap(
343: org.jdesktop.layout.LayoutStyle.RELATED)
344: .add(
345: error,
346: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
347: 11,
348: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
349: .add(8, 8, 8)
350: .add(
351: jScrollPane1,
352: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
353: 135,
354: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
355: .add(91, 91, 91)));
356:
357: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
358: this );
359: this .setLayout(layout);
360: layout.setHorizontalGroup(layout.createParallelGroup(
361: org.jdesktop.layout.GroupLayout.LEADING).add(
362: layout.createSequentialGroup().add(jPanel2,
363: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
364: 412, Short.MAX_VALUE).addContainerGap()));
365: layout.setVerticalGroup(layout.createParallelGroup(
366: org.jdesktop.layout.GroupLayout.LEADING).add(jPanel2,
367: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 222,
368: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE));
369: }// </editor-fold>//GEN-END:initComponents
370:
371: private void sheetComboActionPerformed(
372: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sheetComboActionPerformed
373: try {
374: FileInputStream in = new FileInputStream(new File(file));
375: Workbook spreadsheetData = Workbook.getWorkbook(in);
376: totalRows.setText("Total Rows: "
377: + String.valueOf(spreadsheetData.getSheet(
378: (String) sheetCombo.getSelectedItem())
379: .getRows()));
380: error.setText("");
381: } catch (Exception ex) {
382: error.setText("Unable to get Sheet data.");
383: }
384: Runnable run = new Runnable() {
385: public void run() {
386: jTable1.setModel(new DefaultTableModel());
387: }
388: };
389: SwingUtilities.invokeLater(run);
390: }//GEN-LAST:event_sheetComboActionPerformed
391:
392: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
393: final DefaultTableModel model = getDataForSheet((String) sheetCombo
394: .getSelectedItem());
395: Runnable run = new Runnable() {
396: public void run() {
397: jTable1.setModel(model);
398: }
399: };
400: SwingUtilities.invokeLater(run);
401: error.setText("");
402: }//GEN-LAST:event_jButton1ActionPerformed
403:
404: public boolean canAdvance() {
405: return (sheetCombo.getItemCount() != 0 && sheetCombo
406: .getSelectedIndex() != -1);
407: }
408:
409: // Variables declaration - do not modify//GEN-BEGIN:variables
410: private javax.swing.JLabel error;
411: private javax.swing.JButton jButton1;
412: private javax.swing.JLabel jLabel1;
413: private javax.swing.JLabel jLabel3;
414: private javax.swing.JPanel jPanel2;
415: private javax.swing.JScrollPane jScrollPane1;
416: private javax.swing.JTable jTable1;
417: private javax.swing.JTextField rowsToShow;
418: private javax.swing.JComboBox sheetCombo;
419: private javax.swing.JLabel totalRows;
420: // End of variables declaration//GEN-END:variables
421:
422: }
|