package example.javafiletree; import java.awt.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.*; import java.io.*; import javax.swing.*; import javax.swing.text.html.ListView; import javax.swing.tree.*; public class MainWin extends javax.swing.JFrame { /** Creates new form MainWin */ public MainWin() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { toolBar = new javax.swing.JToolBar(); runButton = new javax.swing.JButton(); statusBar = new javax.swing.JPanel(); jSplitPane1 = new javax.swing.JSplitPane(); jScrollTreePane = new javax.swing.JScrollPane(); treeView = new javax.swing.JTree(); jSplitPane2 = new javax.swing.JSplitPane(); jScrolListlPane = new javax.swing.JScrollPane(); listView = new javax.swing.JList(); jTabbedPane1 = new javax.swing.JTabbedPane(); jScrollPane1 = new javax.swing.JScrollPane(); textArea = new javax.swing.JTextArea(); jScrollPane2 = new javax.swing.JScrollPane(); textPane = new javax.swing.JTextPane(); jScrollPane3 = new javax.swing.JScrollPane(); editorPane = new javax.swing.JEditorPane(); jScrollPane4 = new javax.swing.JScrollPane(); tableView = new javax.swing.JTable(); pictureView = new javax.swing.JPanel(); pictureLabel = new javax.swing.JLabel(); menuBar = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenu(); runMenuItem = new javax.swing.JMenuItem(); editMenu = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); toolBar.setRollover(true); runButton.setText("Run"); runButton.setFocusable(false); runButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); runButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); runButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { runButtonClick(evt); } }); toolBar.add(runButton); javax.swing.GroupLayout statusBarLayout = new javax.swing.GroupLayout(statusBar); statusBar.setLayout(statusBarLayout); statusBarLayout.setHorizontalGroup( statusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 722, Short.MAX_VALUE) ); statusBarLayout.setVerticalGroup( statusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 16, Short.MAX_VALUE) ); jSplitPane1.setDividerLocation(200); treeView.setRequestFocusEnabled(false); treeView.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() { public void valueChanged(javax.swing.event.TreeSelectionEvent evt) { treeViewValueChanged(evt); } }); jScrollTreePane.setViewportView(treeView); jSplitPane1.setLeftComponent(jScrollTreePane); jSplitPane2.setDividerLocation(200); listView.setModel(new javax.swing.AbstractListModel() { String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); listView.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { listViewValueChanged(evt); } }); jScrolListlPane.setViewportView(listView); jSplitPane2.setLeftComponent(jScrolListlPane); textArea.setColumns(20); textArea.setRows(5); jScrollPane1.setViewportView(textArea); jTabbedPane1.addTab("tab1", jScrollPane1); jScrollPane2.setViewportView(textPane); jTabbedPane1.addTab("tab2", jScrollPane2); jScrollPane3.setViewportView(editorPane); jTabbedPane1.addTab("tab3", jScrollPane3); tableView.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4" } )); jScrollPane4.setViewportView(tableView); jTabbedPane1.addTab("tab4", jScrollPane4); javax.swing.GroupLayout pictureViewLayout = new javax.swing.GroupLayout(pictureView); pictureView.setLayout(pictureViewLayout); pictureViewLayout.setHorizontalGroup( pictureViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pictureViewLayout.createSequentialGroup() .addGap(34, 34, 34) .addComponent(pictureLabel) .addContainerGap(271, Short.MAX_VALUE)) ); pictureViewLayout.setVerticalGroup( pictureViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pictureViewLayout.createSequentialGroup() .addGap(60, 60, 60) .addComponent(pictureLabel) .addContainerGap(280, Short.MAX_VALUE)) ); jTabbedPane1.addTab("tab5", pictureView); jSplitPane2.setRightComponent(jTabbedPane1); jSplitPane1.setRightComponent(jSplitPane2); fileMenu.setText("File"); runMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK)); runMenuItem.setText("Run"); runMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { runButtonClick(evt); } }); fileMenu.add(runMenuItem); menuBar.add(fileMenu); editMenu.setText("Edit"); menuBar.add(editMenu); setJMenuBar(menuBar); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(toolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 722, Short.MAX_VALUE) .addComponent(statusBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 722, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(statusBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) ); pack(); }// //GEN-END:initComponents private void showInfo(String s) { textArea.append (s + "\r\n"); } private DefaultTreeModel m = null; private void runButtonClick(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonClick // import javax.swing.tree.*; DefaultMutableTreeNode n = new DefaultMutableTreeNode ("Abc"); m = new DefaultTreeModel (n); treeView.setModel (m); // import java.io.*; File [] subdirs = File.listRoots(); int cnt = 0; for (int i = 0; i < subdirs.length; i++) { File f = subdirs [i]; if (f.isDirectory()) { Info info = new Info (); info.name = f.getAbsolutePath (); info.path = f.getAbsolutePath (); DefaultMutableTreeNode t = new DefaultMutableTreeNode (info); info.node = t; m.insertNodeInto (t, n, cnt); cnt ++; } } }//GEN-LAST:event_runButtonClick private MyModel list_model = new MyModel (); private void showFiles (Info top) { File dir = new File (top.path); File [] subitems = dir.listFiles (); if (subitems != null) { int cnt = 0; for (int i = 0; i < subitems.length; i++) { File f = subitems [i]; if (f.isFile()) cnt ++; } list_model.table = new Info [cnt]; int k = 0; for (int i = 0; i < subitems.length; i++) { File f = subitems [i]; if (f.isFile()) { Info info = new Info (); info.name = f.getName (); info.path = f.getAbsolutePath (); list_model.table [k] = info; k ++; } } } listView.setModel (list_model); list_model.refresh (); } private void directoryClick (Info top) { showFiles (top); if (top.ready) return; top.ready = true; File dir = new File (top.path); File [] subdirs = dir.listFiles (); int cnt = 0; if (subdirs != null) for (int i = 0; i < subdirs.length; i++) { File f = subdirs [i]; if (f.isDirectory()) { Info info = new Info (); info.name = f.getName (); info.path = f.getAbsolutePath (); DefaultMutableTreeNode t = new DefaultMutableTreeNode (info); info.node = t; m.insertNodeInto (t, top.node, cnt); cnt ++; } } } String [] textExts = { ".txt", ".cpp", ".c"}; String [] imageExts = { ".jpg", ".jpeg", ".bmp"}; private boolean hasExt (String name, String [] exts) { boolean result = false; for (int i = 0 ; i < exts.length; i ++) if (name.endsWith( exts[i])) result = true; return result; } private void fileClick (String path) { if (hasExt (path, textExts)) { textArea.setText (""); try { FileInputStream stream = new FileInputStream(path); InputStreamReader tmp = new InputStreamReader (stream); BufferedReader reader = new BufferedReader (tmp); while (reader.ready ()) { String s = reader.readLine (); textArea.append(s + "\n"); } stream.close (); } catch (FileNotFoundException ex) { } catch (IOException ex) { } } if (hasExt (path, imageExts)) { // try { // import java.awt.*; // import javax.imageio.*; // File f = new File (path); // Image image = ImageIO.read(f); // ImageIcon ico = new ImageIcon (image); ImageIcon ico = new ImageIcon (path); pictureLabel.setIcon (ico); // } catch (IOException ex) { } } } private void treeViewValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeViewValueChanged TreePath [] paths = evt.getPaths (); for (int i = 0; i < paths.length; i ++) { TreePath p = paths [i]; if (evt.isAddedPath(p)) { // showInfo ("Selected " + p); Object obj = p.getLastPathComponent (); // showInfo ("Type " + obj.getClass().getName()); if (obj instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj; Object user = node.getUserObject (); showInfo ("Type " + user.getClass().getName()); if (user instanceof Info) { Info info = (Info) user; showInfo ("Selected " + info.path); showInfo ("Node " + info.node); directoryClick (info); } } } } }//GEN-LAST:event_treeViewValueChanged private void listViewValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listViewValueChanged int inx = listView.getSelectedIndex(); if (inx >= 0 && inx <= list_model.getSize()) { Object obj = list_model.getElementAt (inx); if (obj instanceof Info) { Info info = (Info) obj; if (info != null) fileClick (info.path); } } }//GEN-LAST:event_listViewValueChanged /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainWin().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenu editMenu; private javax.swing.JEditorPane editorPane; private javax.swing.JMenu fileMenu; private javax.swing.JScrollPane jScrolListlPane; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane4; private javax.swing.JScrollPane jScrollTreePane; private javax.swing.JSplitPane jSplitPane1; private javax.swing.JSplitPane jSplitPane2; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JList listView; private javax.swing.JMenuBar menuBar; private javax.swing.JLabel pictureLabel; private javax.swing.JPanel pictureView; private javax.swing.JButton runButton; private javax.swing.JMenuItem runMenuItem; private javax.swing.JPanel statusBar; private javax.swing.JTable tableView; private javax.swing.JTextArea textArea; private javax.swing.JTextPane textPane; private javax.swing.JToolBar toolBar; private javax.swing.JTree treeView; // End of variables declaration//GEN-END:variables }