001: package org.netbeans.modules.mashup.tables.wizard;
002:
003: import java.awt.Component;
004: import java.awt.Container;
005: import java.awt.Dimension;
006: import java.awt.event.KeyAdapter;
007: import java.awt.event.KeyEvent;
008: import java.io.File;
009: import java.util.ArrayList;
010: import javax.swing.JButton;
011: import javax.swing.JFileChooser;
012: import javax.swing.JPanel;
013: import javax.swing.SwingUtilities;
014: import javax.swing.table.DefaultTableModel;
015: import javax.swing.table.TableColumnModel;
016: import net.java.hulp.i18n.Logger;
017: import org.netbeans.modules.etl.logger.Localizer;
018: import org.netbeans.modules.etl.logger.LogUtil;
019:
020: public final class FileSelectionVisualPanel extends JPanel {
021:
022: private static transient final Logger mLogger = LogUtil
023: .getLogger(FileSelectionVisualPanel.class.getName());
024: private static transient final Localizer mLoc = Localizer.get();
025:
026: /**
027: * PropertyChangeListener class listens to the selection of file
028: */
029: private class InPropertyListener implements
030: java.beans.PropertyChangeListener {
031:
032: /**
033: * This method gets called when a bound property is changed.
034: *
035: * @param evt A PropertyChangeEvent object describing the event source and the
036: * property that has changed.
037: */
038: public void propertyChange(java.beans.PropertyChangeEvent evt) {
039: owner.fireChangeEvent();
040: }
041: }
042:
043: private static File currDir;
044: private boolean canAdvance = false;
045: private FileSelectionPanel owner;
046:
047: /**
048: * Creates new form FileSelectionVisualPanel
049: */
050: public FileSelectionVisualPanel(FileSelectionPanel panel) {
051: owner = panel;
052: initComponents();
053:
054: requestFocus();
055:
056: fileChooser.addPropertyChangeListener(new InPropertyListener());
057: disableCancelButton(fileChooser);
058: fileChooser.setPreferredSize(new Dimension(500, 450));
059:
060: url.addKeyListener(new KeyAdapter() {
061:
062: @Override
063: public void keyReleased(KeyEvent e) {
064: if (url.getText().trim().length() == 0) {
065: addEntry.setEnabled(false);
066: } else {
067: addEntry.setEnabled(true);
068: }
069: }
070: });
071:
072: // set file filter
073: FlatfileFilter ffFilter = new FlatfileFilter();
074: ffFilter.setDescription("Flat Files (CSV, TXT, XLS)");
075: ffFilter.addType(".csv");
076: ffFilter.addType(".txt");
077: ffFilter.addType(".xls");
078: ffFilter.addType(".dat");
079: fileChooser.addChoosableFileFilter(ffFilter);
080: fileChooser.setAcceptAllFileFilterUsed(true);
081:
082: removeButton.setEnabled(false);
083: addEntry.setEnabled(false);
084: setTableColumnSize();
085:
086: }
087:
088: @Override
089: public String getName() {
090: String nbBundle3 = mLoc.t("PRSR001: Choose Data source");
091: return Localizer.parse(nbBundle3);
092: }
093:
094: public boolean canAdvance() {
095: return canAdvance;
096: }
097:
098: /** This method is called from within the constructor to
099: * initialize the form.
100: * WARNING: Do NOT modify this code. The content of this method is
101: * always regenerated by the Form Editor.
102: */
103: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
104: private void initComponents() {
105:
106: fileChooser = new javax.swing.JFileChooser();
107: panel1 = new javax.swing.JPanel();
108: jLabel3 = new javax.swing.JLabel();
109: url = new javax.swing.JTextField();
110: addEntry = new javax.swing.JButton();
111: jScrollPane1 = new javax.swing.JScrollPane();
112: jTable1 = new javax.swing.JTable();
113: removeButton = new javax.swing.JButton();
114: error = new javax.swing.JLabel();
115:
116: fileChooser.setApproveButtonText("Add");
117: fileChooser.setApproveButtonToolTipText("Add Table Source");
118: fileChooser
119: .setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
120: fileChooser.setToolTipText("Choose a file");
121: fileChooser.setAutoscrolls(true);
122: fileChooser.setBorder(javax.swing.BorderFactory
123: .createTitledBorder("Choose a File"));
124: fileChooser.setMultiSelectionEnabled(true);
125: fileChooser
126: .addActionListener(new java.awt.event.ActionListener() {
127: public void actionPerformed(
128: java.awt.event.ActionEvent evt) {
129: fileChooserActionPerformed(evt);
130: }
131: });
132:
133: org.openide.awt.Mnemonics.setLocalizedText(jLabel3, "URL");
134:
135: org.openide.awt.Mnemonics.setLocalizedText(addEntry, "Add");
136: addEntry.addActionListener(new java.awt.event.ActionListener() {
137: public void actionPerformed(java.awt.event.ActionEvent evt) {
138: addEntryActionPerformed(evt);
139: }
140: });
141:
142: org.jdesktop.layout.GroupLayout panel1Layout = new org.jdesktop.layout.GroupLayout(
143: panel1);
144: panel1.setLayout(panel1Layout);
145: panel1Layout
146: .setHorizontalGroup(panel1Layout
147: .createParallelGroup(
148: org.jdesktop.layout.GroupLayout.LEADING)
149: .add(
150: panel1Layout
151: .createSequentialGroup()
152: .add(jLabel3)
153: .add(50, 50, 50)
154: .add(
155: url,
156: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
157: 353, Short.MAX_VALUE)
158: .addPreferredGap(
159: org.jdesktop.layout.LayoutStyle.RELATED)
160: .add(addEntry)
161: .addContainerGap()));
162: panel1Layout.setVerticalGroup(panel1Layout.createParallelGroup(
163: org.jdesktop.layout.GroupLayout.LEADING).add(
164: panel1Layout.createParallelGroup(
165: org.jdesktop.layout.GroupLayout.BASELINE).add(
166: url,
167: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
168: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
169: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
170: .add(addEntry).add(jLabel3)));
171:
172: jScrollPane1.setBorder(javax.swing.BorderFactory
173: .createTitledBorder("Selected Table Source"));
174:
175: jTable1.setModel(new javax.swing.table.DefaultTableModel(
176: new Object[][] {
177:
178: }, new String[] { "#", "Table Source" }) {
179: Class[] types = new Class[] { java.lang.Integer.class,
180: java.lang.String.class };
181: boolean[] canEdit = new boolean[] { false, false };
182:
183: public Class getColumnClass(int columnIndex) {
184: return types[columnIndex];
185: }
186:
187: public boolean isCellEditable(int rowIndex, int columnIndex) {
188: return canEdit[columnIndex];
189: }
190: });
191: jTable1
192: .setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
193: jScrollPane1.setViewportView(jTable1);
194:
195: org.openide.awt.Mnemonics.setLocalizedText(removeButton,
196: "Remove");
197: removeButton
198: .addActionListener(new java.awt.event.ActionListener() {
199: public void actionPerformed(
200: java.awt.event.ActionEvent evt) {
201: removeButtonActionPerformed(evt);
202: }
203: });
204:
205: error.setForeground(new java.awt.Color(255, 0, 51));
206:
207: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
208: this );
209: this .setLayout(layout);
210: layout
211: .setHorizontalGroup(layout
212: .createParallelGroup(
213: org.jdesktop.layout.GroupLayout.LEADING)
214: .add(
215: panel1,
216: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
217: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
218: Short.MAX_VALUE)
219: .add(
220: fileChooser,
221: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
222: 489, Short.MAX_VALUE)
223: .add(
224: layout
225: .createSequentialGroup()
226: .add(
227: layout
228: .createParallelGroup(
229: org.jdesktop.layout.GroupLayout.TRAILING)
230: .add(
231: layout
232: .createSequentialGroup()
233: .addContainerGap(
234: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
235: Short.MAX_VALUE)
236: .add(
237: error,
238: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
239: 392,
240: Short.MAX_VALUE))
241: .add(
242: jScrollPane1,
243: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
244: 402,
245: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
246: .addPreferredGap(
247: org.jdesktop.layout.LayoutStyle.RELATED,
248: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
249: Short.MAX_VALUE)
250: .add(
251: removeButton,
252: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
253: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
254: Short.MAX_VALUE)
255: .addContainerGap()));
256: layout
257: .setVerticalGroup(layout
258: .createParallelGroup(
259: org.jdesktop.layout.GroupLayout.LEADING)
260: .add(
261: layout
262: .createSequentialGroup()
263: .add(
264: panel1,
265: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
266: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
267: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
268: .addPreferredGap(
269: org.jdesktop.layout.LayoutStyle.RELATED)
270: .add(
271: fileChooser,
272: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
273: 254,
274: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
275: .addPreferredGap(
276: org.jdesktop.layout.LayoutStyle.RELATED)
277: .add(
278: layout
279: .createParallelGroup(
280: org.jdesktop.layout.GroupLayout.LEADING)
281: .add(
282: org.jdesktop.layout.GroupLayout.TRAILING,
283: layout
284: .createSequentialGroup()
285: .add(
286: jScrollPane1,
287: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
288: 99,
289: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
290: .addPreferredGap(
291: org.jdesktop.layout.LayoutStyle.RELATED,
292: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
293: Short.MAX_VALUE)
294: .add(
295: error,
296: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
297: 14,
298: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
299: .add(
300: removeButton))));
301: }// </editor-fold>//GEN-END:initComponents
302:
303: private void removeButtonActionPerformed(
304: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
305: removeFromModel();
306: }//GEN-LAST:event_removeButtonActionPerformed
307:
308: private void addEntryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addEntryActionPerformed
309: String urlAdd = url.getText().trim();
310: addToModel(urlAdd);
311: }//GEN-LAST:event_addEntryActionPerformed
312:
313: private void fileChooserActionPerformed(
314: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileChooserActionPerformed
315: if (evt.getActionCommand().equalsIgnoreCase(
316: JFileChooser.APPROVE_SELECTION)) {
317: File files[] = fileChooser.getSelectedFiles();
318: if ((files == null) || (files.length <= 0)) {
319: String nbBundle2 = mLoc
320: .t("PRSR001: Bad file selection. Please try again.");
321: String msg = Localizer.parse(nbBundle2);
322: error.setText(msg);
323: return;
324: }
325:
326: DefaultTableModel model = (DefaultTableModel) jTable1
327: .getModel();
328:
329: for (File file : files) {
330: Object[] obj = new Object[2];
331: obj[0] = model.getRowCount() + 1;
332: obj[1] = file.getAbsolutePath();
333: model.addRow(obj);
334: }
335:
336: setTableModel(model);
337: canAdvance = true;
338: removeButton.setEnabled(true);
339: setErrorText("");
340: owner.fireChangeEvent();
341: }
342: }//GEN-LAST:event_fileChooserActionPerformed
343:
344: public void setErrorText(String string) {
345: error.setText(string);
346: }
347:
348: private void disableCancelButton(Component c) {
349: if (c instanceof Container) {
350: Component[] comps = ((Container) c).getComponents();
351: for (int i = 0; i < comps.length; i++) {
352: if (comps[i] instanceof JButton) {
353: if ("Cancel".equalsIgnoreCase(((JButton) comps[i])
354: .getText())) {
355: ((Container) c).remove(comps[i]);
356: break;
357: }
358: } else {
359: disableCancelButton(comps[i]);
360: }
361: }
362: }
363: }
364:
365: /**
366: * Sets the root directory based on the value from last page
367: *
368: * @param newDir root string
369: */
370: public void setCurrentDirectory(File newDir) {
371: currDir = (newDir == null) ? new File(".") : newDir;
372: fileChooser.setCurrentDirectory(currDir);
373: }
374:
375: public DefaultTableModel getSelectedTables() {
376: return (DefaultTableModel) jTable1.getModel();
377: }
378:
379: private void addToModel(String urlAdd) {
380: DefaultTableModel model = (DefaultTableModel) jTable1
381: .getModel();
382: Object[] obj = new Object[2];
383: obj[0] = model.getRowCount() + 1;
384: obj[1] = urlAdd;
385: model.addRow(obj);
386: setTableModel(model);
387: canAdvance = true;
388: removeButton.setEnabled(true);
389: owner.fireChangeEvent();
390: setErrorText("");
391: url.setText("");
392: }
393:
394: private void removeFromModel() {
395: DefaultTableModel model = (DefaultTableModel) jTable1
396: .getModel();
397: for (int i = 0; i < model.getRowCount(); i++) {
398: if (jTable1.isRowSelected(i)) {
399: model.removeRow(i);
400: }
401: }
402:
403: // adjust the sequence number
404: for (int i = 0; i < model.getRowCount(); i++) {
405: model.setValueAt(i + 1, i, 0);
406: }
407:
408: setTableModel(model);
409: if (model.getRowCount() == 0) {
410: canAdvance = false;
411: removeButton.setEnabled(false);
412: String nbBundle1 = mLoc
413: .t("PRSR001: No table available for processing.");
414: setErrorText(Localizer.parse(nbBundle1));
415: }
416: owner.fireChangeEvent();
417: }
418:
419: private void setTableModel(final DefaultTableModel model) {
420: Runnable run = new Runnable() {
421:
422: public void run() {
423: Object[] obj = new Object[2];
424: obj[0] = "#";
425: obj[1] = "Table Source";
426: model.setColumnIdentifiers(obj);
427: jTable1.setModel(model);
428: setTableColumnSize();
429: }
430: };
431: SwingUtilities.invokeLater(run);
432: }
433:
434: private void setTableColumnSize() {
435: TableColumnModel tableColumnModel = jTable1.getColumnModel();
436: tableColumnModel.getColumn(0).setMinWidth(5);
437: tableColumnModel.getColumn(0).setMaxWidth(30);
438: tableColumnModel.getColumn(0).setPreferredWidth(30);
439: }
440:
441: public class FlatfileFilter extends
442: javax.swing.filechooser.FileFilter {
443:
444: protected String description;
445: protected ArrayList<String> exts = new ArrayList<String>();
446:
447: public void addType(String s) {
448: exts.add(s);
449: }
450:
451: /** Return true if the given file is accepted by this filter. */
452: public boolean accept(File f) {
453: // Little trick: if you don't do this, only directory names
454: // ending in one of the extentions appear in the window.
455: if (f.isDirectory()) {
456: return true;
457: } else if (f.isFile()) {
458: for (String ext : exts) {
459: if (f.getName().endsWith(ext)) {
460: return true;
461: }
462: }
463: }
464: // A file that didn't match, or a weirdo (e.g. UNIX device file?).
465: return false;
466: }
467:
468: /** Set the printable description of this filter. */
469: public void setDescription(String s) {
470: description = s;
471: }
472:
473: /** Return the printable description of this filter. */
474: public String getDescription() {
475: return description;
476: }
477: }
478:
479: // Variables declaration - do not modify//GEN-BEGIN:variables
480: private javax.swing.JButton addEntry;
481: private javax.swing.JLabel error;
482: private javax.swing.JFileChooser fileChooser;
483: private javax.swing.JLabel jLabel3;
484: private javax.swing.JScrollPane jScrollPane1;
485: private javax.swing.JTable jTable1;
486: private javax.swing.JPanel panel1;
487: private javax.swing.JButton removeButton;
488: private javax.swing.JTextField url;
489: // End of variables declaration//GEN-END:variables
490: }
|