Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  FileList  (Read 1794 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Posted 2008-05-18 20:03:39 »

Here is a relatively simple Swing interface item that is much more useful for most games than a file chooser is. Basically, it scans an array of directories for files that end in an array of file extensions. Finally, the results are given in a JList, with an accept or cancel button. It also has the useful feature of being able to browse files that are both on your local file system and/or in your classpath. I haven't actually tested the classpath part when coming from a JAR yet, but I believe it should work.

Easy to use and customizable. I'm making a pure LWJGL version in the near future.

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  
import java.util.ArrayList;
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListSelectionModel;

/**
 * A FileList is a window that contains a JList. The JList automatically fills with all files in
 * a list of given directories of a list of given extensions. Finally, the JList notifies a
 * FileListener of the final choice.
 * @author Eli Delventhal
 *
 */

public class FileList extends JFrame implements ActionListener
{
   private FileListener listener;
   private ArrayList<File> files;
   
   private JList list;
   private JButton confirm = new JButton("Select");
   private JButton cancel  = new JButton("Cancel");
   
   public FileList(FileListener listen, String[] directories, String[] extensions, boolean allowMultiple)
   {
      super("Select a File");
      listener = listen;
     
      list = createFileList(directories,extensions);
      if (allowMultiple)
         ((DefaultListSelectionModel)list.getSelectionModel()).setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
      else
         ((DefaultListSelectionModel)list.getSelectionModel()).setSelectionMode(DefaultListSelectionModel.SINGLE_INTERVAL_SELECTION);
     
      Container cp = getContentPane();
      cp.setLayout(new BorderLayout());
      cp.add(new JScrollPane(list),BorderLayout.CENTER);
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(1,2));
      p.add(cancel);   p.add(confirm);
      cp.add(p,BorderLayout.SOUTH);
     
      cancel.addActionListener(this);
      confirm.addActionListener(this);
     
      setSize(400,600);
      setVisible(true);
   }
   
   private JList createFileList(String[] directories, String[] extensions)
   {
      ArrayList<String> allFiles = new ArrayList<String>();
     
      //Browse through all directories.
     for (int i = 0; i < directories.length; i++)
      {
         //Try this directory as a "physical" file directory.
        try
         {
            String[] files = new File(directories[i]).list(new ExtensionFilter(extensions));
            for (int j = 0; j < files.length; j++)
            {
               if (!allFiles.contains(directories[i] + files[j]))
                  allFiles.add(directories[i] + files[j]);
            }
         }
         catch (Exception e) {}
         
         //Try this directory as a classpath file directory.
        try
         {
            String[] files = new File(Thread.currentThread().getContextClassLoader().getResource(directories[i]).getFile()).list(new ExtensionFilter(extensions));
            for (int j = 0; j < files.length; j++)
            {
               if (!allFiles.contains(directories[i] + files[j]))
                  allFiles.add(directories[i] + files[j]);
            }
         }
         catch (Exception e) {}
      }
     
      //Return all files that were found to be okay.
     return new JList(allFiles.toArray());
   }

   public void actionPerformed(ActionEvent e)
   {
      Object s = e.getSource();
      if (s == cancel)
         dispose();
      else if (s == confirm)
      {
         Object[] o = list.getSelectedValues();
         String[] files = new String[o.length];
         for (int i = 0; i < o.length; i++)
            files[i] = o[i].toString();
         listener.filesSelected(files);
         dispose();
      }
   }
   
   /**
    * A simple file filter that allows files with any extensions from the list.
    * Automatically disallows files with no extension.
    * @author Eli Delventhal
    *
    */

   private class ExtensionFilter implements FilenameFilter
   {
      private String[] extensions;
     
      public ExtensionFilter(String[] ext)
      {
         extensions = ext;
      }
     
      public boolean accept(File directory, String name)
      {
         int lastPeriod = name.lastIndexOf(".");
         if (lastPeriod < 0 || lastPeriod >= name.length()-1)
            return false;
         
         String extension = name.substring(lastPeriod+1);
         for (int i = 0; i < extensions.length; i++)
            if (!extensions[i].equalsIgnoreCase(extension))
               return false;
         return true;
      }
     
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
/**
 * The FileListener is a simple interface to be used with FileList.
 * @author Eli Delventhal
 *
 */

public interface FileListener
{
   public void filesSelected(String[] files);
}


Example usage:
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  
public class PlanetPanel extends JPanel implements FileListener
...
new FileList(this,new String[]{"Systems/",FileManager.getResourceFolder().getAbsolutePath()+"/Systems/"},new String[]{"ps"},false);
...
/**
    * Called when files have been selected. The first selected file will be loaded.
    */

   public void filesSelected(String[] files)
   {
      if (files.length <= 0)
         return;
     
      ObjectInputStream is = null;
     
      //First, search for the file in local directories.
     try
      {
         is = new ObjectInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(files[0]));
      }
      catch (Exception e)
      {
         //If the file isn't found, it must not be in the classpath, so search local directories.
        try
         {
            is = new ObjectInputStream(new FileInputStream(new File(files[0])));
         }
         catch (Exception ex)
         {
            //If it still isn't found, show an exception and then return.
           ex.printStackTrace();
            return;
         }
      }
     
      //Finally, read the input stream.
     try
      {
         x = (Double) is.readObject();
         y = (Double) is.readObject();
         METERS_PER_PIXEL = (Double) is.readObject();
         TIMESTEPS_PER_RENDER = (Integer) is.readObject();
         bodies = (ArrayList<Body>) is.readObject();
         is.close();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
     
      repaint();
   }

See my work:
OTC Software
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2008-05-18 22:26:00 »

I tested this out with a JAR, and you can't use the File class to list the directory.

Anyone know how to list the contents of a directory within a JAR? I know how to do it via command line, but not within my code. Google searches reveal nothing.

See my work:
OTC Software
Offline cylab

JGO Ninja


Medals: 45



« Reply #2 - Posted 2008-05-19 00:52:31 »

Afaik you have to open the jar using JarInputStream and read out all the JarEntries and iterate over them yourself.

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2008-05-19 03:47:59 »

Afaik you have to open the jar using JarInputStream and read out all the JarEntries and iterate over them yourself.
Blech, that's what I figured. I don't think that's worth it - instead, I'll include a place for the user to specify the exact locations in the classpath to look. After all, you should know exactly what goes into your JAR, so you shouldn't need to check directory contents.

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (24 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!