import java.io.*; import java.awt.*; import java.net.*; import java.util.*; import java.text.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.plaf.*; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; public class DataUtil extends JDialog implements ActionListener { static JDialog dmDlg; static JFileChooser ascii_fc, dir_out_fc; static JPanel fullPanel, topGridPanel, selobjPanel, selobjTAPanel, topbutPanel; static JPanel dataOutPanel, outpathPanel, btmbutPanel, midGridPanel, outnamePanel; static JPanel fullPathPanel; static JButton selobj_but, SortBut, ConcBut, RvrBut, ExitBut, SaveBut, OpenBut; static JButton outpath_but, ResBut, DelBlnkBut, DupBut; static JTextArea selobjTA, dataOutTA; static JTextField outpathField, outnameField, fullPathField; static JScrollPane textScroller, textScrollerTA; static String fileListStg, curdirStg, dataout_DIR, exp_txtnm, full_proe_bat; static String bat_cmd, open_outnm, dataname, dataOutStg, dtStg; static String[] strings, outputStg; static File[] selectedFiles; DIR_Filter dir_filter = new DIR_Filter(); ascii_Filter ascii_filter = new ascii_Filter(); public DataUtil() { dmDlg=new JDialog(new JFrame()," Data Utility Tool ",true); dmDlg.setResizable( false ); dmDlg.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit(0); } }); try { UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch (Exception exc) {} dataout_DIR = curdirStg = System.getProperty( "user.dir" ); dtStg = getDateStg(); String outnameStg = dtStg + "_DataMan.txt"; String fullpathStg = dataout_DIR + "\\" + outnameStg; ascii_fc = new JFileChooser(curdirStg); dir_out_fc = new JFileChooser(dataout_DIR); fullPanel = new JPanel(); fullPanel.setBorder(new EmptyBorder(1,1,1,1)); fullPanel.setLayout(new BorderLayout()); topGridPanel = new JPanel(); GridBagLayout gbc0 = new GridBagLayout(); topGridPanel.setLayout(gbc0); // Select Object Panel selobjPanel = new JPanel(); // Select Object Text Area Label & Button JLabel selobjLabel = new JLabel(" Select Data To Manipulate:"); selobjPanel.add(selobjLabel); selobj_but = new JButton("Select File(s)"); selobj_but.addActionListener(this); selobjPanel.add(selobj_but); // ProE Object Text Area Panel selobjTAPanel= new JPanel(); selobjTAPanel.setLayout(new BorderLayout()); JLabel selobjTALabel = new JLabel("File(s) Selected List:"); selobjTAPanel.add(selobjTALabel, BorderLayout.NORTH); selobjTA = new JTextArea("",6,65); selobjTA.setLineWrap(false); textScrollerTA = new JScrollPane(selobjTA); selobjTAPanel.add(textScrollerTA); topbutPanel = new JPanel(); topbutPanel.setLayout ( new FlowLayout(FlowLayout.CENTER) ); ConcBut = new JButton("Concatenate / Combine"); ConcBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Concat(); }}); topbutPanel.add(ConcBut); SortBut = new JButton("Sort"); SortBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sortStrings(); }}); topbutPanel.add(SortBut); RvrBut = new JButton("Reverse"); RvrBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reverseStrings(); }}); topbutPanel.add(RvrBut); DelBlnkBut = new JButton("Delete Blank Lines"); DelBlnkBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { delBlank(); }}); topbutPanel.add(DelBlnkBut); DupBut = new JButton("Duplicate Column"); DupBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dupColumn(); }}); topbutPanel.add(DupBut); ResBut = new JButton("Reset All"); ResBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setResetAll(); }}); topbutPanel.add(ResBut); topGridPanel.add(selobjPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 1, 0)); topGridPanel.add(selobjTAPanel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 1, 0)); topGridPanel.add(topbutPanel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 1, 0)); midGridPanel = new JPanel(); GridBagLayout gbc1 = new GridBagLayout(); midGridPanel.setLayout(gbc1); // Data Output Field Panel dataOutPanel = new JPanel(); dataOutPanel.setLayout(new BorderLayout()); JLabel dataOutLabel = new JLabel("Data Output:"); dataOutPanel.add(dataOutLabel, BorderLayout.NORTH); dataOutTA = new JTextArea("",12,65); dataOutTA.setLineWrap(false); textScroller = new JScrollPane( dataOutTA ); dataOutPanel.add(textScroller, BorderLayout.SOUTH); // Output Path Panel outpathPanel= new JPanel(); JLabel outpathLabel = new JLabel("Data Output Path"); outpathPanel.add(outpathLabel); outpathField = new JTextField(dataout_DIR,25); outpathPanel.add(outpathField); outpath_but = new JButton("Select Dir/Folder"); outpath_but.addActionListener(this); outpathPanel.add(outpath_but); // Output Name Panel outnamePanel= new JPanel(); JLabel outnameLabel = new JLabel("Data Output File Name"); outnamePanel.add(outnameLabel); outnameField = new JTextField(outnameStg,20); outnamePanel.add(outnameField); // Full Output Path/Name Panel fullPathPanel= new JPanel(); JLabel fullPathLabel = new JLabel("Full Output File Path/Name"); fullPathPanel.add(fullPathLabel); fullPathField = new JTextField(fullpathStg,35); fullPathPanel.add(fullPathField); midGridPanel.add(dataOutPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 1, 0)); midGridPanel.add(outpathPanel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 1, 0)); midGridPanel.add(outnamePanel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 1, 0)); midGridPanel.add(fullPathPanel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 1, 0)); btmbutPanel = new JPanel(); btmbutPanel.setLayout ( new FlowLayout(FlowLayout.CENTER) ); SaveBut = new JButton("SAVE NEW DATA FILE"); SaveBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open_outnm = getoutName(); SaveSelected( open_outnm ); }}); btmbutPanel.add(SaveBut); OpenBut = new JButton("OPEN NEWLY SAVED DATA FILE"); OpenBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open_outnm = getoutName(); SaveSelected( open_outnm ); RunCmdInt( open_outnm ); }}); btmbutPanel.add(OpenBut); ExitBut = new JButton("EXIT"); ExitBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); btmbutPanel.add(ExitBut); fullPanel.add(topGridPanel, BorderLayout.NORTH); fullPanel.add(midGridPanel, BorderLayout.CENTER); fullPanel.add(btmbutPanel, BorderLayout.SOUTH); dmDlg.getContentPane().add(fullPanel, BorderLayout.NORTH); dmDlg.pack(); dmDlg.show(); } public void actionPerformed( ActionEvent evt ) { Object source = evt.getSource(); if ( source == selobj_but ) { ascii_fc.addChoosableFileFilter( ascii_filter ); ascii_fc.setMultiSelectionEnabled(true); int returnVal = ascii_fc.showOpenDialog( dmDlg ); if (returnVal == JFileChooser.APPROVE_OPTION) { selectedFiles = ascii_fc.getSelectedFiles(); for (int i = 0; i < selectedFiles.length; i++) { selobjTA.append( selectedFiles[i].getAbsolutePath() + "\n" ); } } else {} } else if ( source == outpath_but ) { dir_out_fc.addChoosableFileFilter( dir_filter ); dir_out_fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int returnVal = dir_out_fc.showOpenDialog( dmDlg ); if (returnVal == JFileChooser.APPROVE_OPTION) { dataout_DIR = dir_out_fc.getCurrentDirectory() + java.io.File.separator + dir_out_fc.getSelectedFile().getName(); outpathField.setText(dataout_DIR); fullPathField.setText(""); open_outnm = getoutName(); fullPathField.setText( open_outnm ); } else {} } } public String getoutName() { dataname = getoutname(); dataout_DIR = getdataout(); open_outnm = dataout_DIR + "\\" + dataname; return open_outnm; } public void setResetAll() { selobjTA.setText(""); dataOutTA.setText(""); dataout_DIR = System.getProperty( "user.dir" ); outpathField.setText( dataout_DIR ); outnameField.setText(""); dtStg = getDateStg(); String outnameStg = dtStg + "_DataMan.txt"; outnameField.setText( outnameStg ); fullPathField.setText(""); String fullpathStg = dataout_DIR + "\\" + outnameStg; fullPathField.setText( fullpathStg ); dir_out_fc = new JFileChooser(dataout_DIR); } public String getFileList() { fileListStg = selobjTA.getText(); return fileListStg; } public String getdataOut() { dataOutStg = dataOutTA.getText(); return dataOutStg; } public String getdataout() { dataout_DIR = outpathField.getText(); return dataout_DIR; } public String getoutname() { dataname = outnameField.getText(); return dataname; } public String getDateStg() { SimpleDateFormat df = new SimpleDateFormat ("MMddyy_HHmmss", Locale.getDefault()); Date tm = new Date(); String dtStg = df.format(tm); return dtStg; } public void SaveSelected( String open_outnm ) { dataOutStg = getdataOut(); try { PrintStream expfl = new PrintStream(new FileOutputStream(open_outnm)); expfl.println( dataOutStg ); expfl.flush(); expfl.close(); } catch(java.io.IOException IOEx) { System.out.println("Cannot create " + open_outnm + " file."); } } public void Concat() { fileListStg = getFileList(); outputStg = stringToArray( fileListStg ); for(int i = 0;i < outputStg.length;i++) { File datafile = new File(outputStg[i]); try { BufferedReader reader = null; reader = new BufferedReader(new FileReader(datafile)); String line = null; while ((line = reader.readLine()) != null) { try { dataOutTA.append( line + "\n" ); } catch (Exception exc) { System.out.println(exc.toString()); } } reader.close(); } catch (IOException exc2) { System.out.println("Exception: "); exc2.printStackTrace(); } } } public void sortStrings() { dataOutStg = getdataOut(); strings = stringToArray( dataOutStg ); for ( int i = 0; i < strings.length - 1; ++i ) { for ( int j = i + 1; j < strings.length; ++j ) { if ( strings[i].compareTo( strings[j] ) > 0 ) { String t = strings[i]; strings[i] = strings[j]; strings[j] = t; } } } dataOutTA.setText(""); for ( int k = 0; k < strings.length; k++) { dataOutTA.append( strings[k] + "\n" ); } } public void reverseStrings() { dataOutStg = getdataOut(); strings = stringToArray( dataOutStg ); String temp; int left = 0; int right = strings.length - 1; while (left < right) { temp = strings[left]; strings[left] = strings[right]; strings[right] = temp; right--; left++; } dataOutTA.setText(""); for ( int k = 0; k < strings.length; k++) { dataOutTA.append( strings[k] + "\n" ); } } public void RunCmdInt ( String open_outnm ) { String s = null; try { String wordpad = ("\"C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe\""); Process p = Runtime.getRuntime().exec("cmd /c " + wordpad + " " + open_outnm ); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); while ((s = stdInput.readLine()) != null) { System.out.println( s ); } while ((s = stdError.readLine()) != null) { System.out.println("Standard Error: " + s + "\n"); JOptionPane.showMessageDialog ( dmDlg, "Run Comman Failed", "Open Manually: " + open_outnm, JOptionPane.INFORMATION_MESSAGE ); } } catch (IOException e) { System.out.println("Exception: "); e.printStackTrace(); System.exit(-1); } } public static String[] stringToArray( String S) { if ( S == null || S.equals("")) return null; StringTokenizer t = new StringTokenizer(S, "\n", true); Vector v = new Vector(32); String s, u; int lineno = 0; while ( t.hasMoreTokens()) { if ( !(s = t.nextToken()).equals("\n")) if ( t.hasMoreTokens()) t.nextToken(); v.addElement( (Object) s); } v.trimToSize(); int top = v.size(); String[] a = new String[ top]; for ( int i = 0; i < top; i++) a[i] = (String) v.elementAt(i); v = null; return a; } public void delBlank() { dataOutStg = getdataOut(); String dataOutStg1 = StgReplace(dataOutStg, "\n\n\n\n\n\n\n\n\n\n", "\n" ); String dataOutStg2 = StgReplace(dataOutStg1,"\n\n\n\n\n\n\n\n\n", "\n" ); String dataOutStg3 = StgReplace(dataOutStg2,"\n\n\n\n\n\n\n\n", "\n" ); String dataOutStg4 = StgReplace(dataOutStg3,"\n\n\n\n\n\n\n", "\n" ); String dataOutStg5 = StgReplace(dataOutStg4,"\n\n\n\n\n\n", "\n" ); String dataOutStg6 = StgReplace(dataOutStg5,"\n\n\n\n\n", "\n" ); String dataOutStg7 = StgReplace(dataOutStg6,"\n\n\n\n", "\n" ); String dataOutStg8 = StgReplace(dataOutStg7,"\n\n\n", "\n" ); String dataOutStg9 = StgReplace(dataOutStg8,"\n\n", "\n" ); dataOutTA.setText(""); dataOutTA.append( dataOutStg9 ); } public static String StgReplace (String target, String from, String to) { int start = target.indexOf (from); if (start==-1) return target; int lf = from.length(); int tf = target.length(); char [] targetChars = target.toCharArray(); StringBuffer buffer = new StringBuffer(); int copyFrom=0; while (start != -1) { buffer.append (targetChars, copyFrom, start-copyFrom); buffer.append (to); copyFrom=start+lf; start = target.indexOf (from, copyFrom); } buffer.append (targetChars, copyFrom, targetChars.length-copyFrom); String finstg = buffer.toString(); return finstg; } public void dupColumn() { dataOutStg = getdataOut(); strings = stringToArray( dataOutStg ); for ( int i = 0; i < strings.length; ++i ) { strings[i] = strings[i] + " " + strings[i]; } dataOutTA.setText(""); for ( int k = 0; k < strings.length; k++) { dataOutTA.append( strings[k] + "\n" ); } } public static void main(String[] args) { DataUtil dmDlg; dmDlg = new DataUtil(); dmDlg.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit(0); } }); } }