package cc.glsn.v15.moviemenu; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; import java.io.FileNotFoundException; import java.util.LinkedList; import java.util.TreeMap; import java.util.Vector; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.ListSelectionModel; import javax.swing.ScrollPaneConstants; public class MovieMenu implements KeyListener, MouseListener { public static final String DefaultRoot="m:"; public static final Color BGColor=Color.DARK_GRAY; public static int ImageHeight = 150; public static int ItemWidth = 800; public static final String FontName="Franklin Gothic Medium"; public static int FontSize=14; public boolean PlayerRunning=false; public static Color getHiColor() { return new Color(0,0,80); } public static void main(String[] args) throws FileNotFoundException { String Root=DefaultRoot; if (args.length>0) Root=args[0]; new MovieMenu(Root); } JFrame Frame; JScrollPane Scrolly; String RootDir; LinkedList RelLocation; TreeMap LastSelected; TreeMap > ItemCache; JList ItemList; JTextArea Title; ImageLoader ImageCache; MovieMenuConfig MMConf; public MovieMenu(String rootDir) throws FileNotFoundException { MMConf=new MovieMenuConfig(rootDir); ImageHeight=MMConf.getImageHeight(); FontSize=MMConf.getFontSize(); ItemWidth=MMConf.getWindowWidth(); System.out.println("Starting..."); ImageCache=new ImageLoader(); clearCache(); RootDir=rootDir; RelLocation=new LinkedList(); LastSelected=new TreeMap(); Title=new JTextArea(1,100); Title.setEditable(false); Title.setFocusable(false); Title.setForeground(Color.YELLOW); Title.setFont(new Font(FontName,0,MMConf.getFontSize())); Title.setBackground(BGColor); /*for(Font F : GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) { System.out.println(F); }*/ Frame=new JFrame("MovieMenu"); Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Frame.pack(); Frame.setSize(MMConf.getWindowWidth(),MMConf.getWindowHeight()); Frame.setBackground(BGColor); Frame.addKeyListener(this); Frame.getContentPane().setBackground(BGColor); ItemList=new JList(); ItemList.addKeyListener(this); ItemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); ItemList.setLayoutOrientation(JList.VERTICAL); ItemList.setCellRenderer(new MovieItemRender()); ItemList.setBackground(BGColor); ItemList.addMouseListener(this); ItemList.setLayout(new FlowLayout(FlowLayout.LEFT)); Scrolly=new JScrollPane(ItemList,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); Scrolly.getViewport().setBackground(BGColor); Scrolly.setBackground(BGColor); Frame.setContentPane(new JPanel(new java.awt.GridBagLayout())); { GridBagConstraints c=new GridBagConstraints(); c.gridheight=1; c.gridwidth=GridBagConstraints.REMAINDER; c.fill=GridBagConstraints.NONE; c.insets=new java.awt.Insets(0,0,0,0); c.anchor=GridBagConstraints.WEST; //c.weightx=1.0; //c.weighty=1.0; Frame.getContentPane().add(Title,c); } { GridBagConstraints c=new GridBagConstraints(); c.gridheight=1; c.gridwidth=GridBagConstraints.REMAINDER; c.fill=GridBagConstraints.BOTH; c.insets=new java.awt.Insets(0,0,0,0); c.anchor=GridBagConstraints.WEST; c.weightx=100.0; c.weighty=100.0; Frame.getContentPane().add(Scrolly,c); } scanDirectory(); //Scrolly.setSize(720,480); //Scrolly.setPreferredSize(720,480); //Scrolly.setPreferredSize(new Dimension(720,480)); //Frame.pack(); Frame.setVisible(true); //Frame.repaint(); //Frame.setSize(720,480); //Scrolly.repaint(); //Frame.repaint(); //System.out.println(Frame.getSize()); } synchronized void moveDown(String S) { if (S.equals("..")) { moveUp(); return; } LastSelected.put(getPathStr(),ItemList.getSelectedIndex()); RelLocation.add(S); scanDirectory(); } synchronized void moveUp() { LastSelected.put(getPathStr(),ItemList.getSelectedIndex()); if (RelLocation.size()>0) RelLocation.removeLast(); scanDirectory(); } synchronized String getPathStr() { StringBuilder Path=new StringBuilder(); Path.append(RootDir); for(String S : RelLocation) { Path.append('/'); Path.append(S); } return Path.toString(); } synchronized void scanDirectory() { StringBuilder Loc=new StringBuilder(); Loc.append("MovieRoot:"); for(String S : RelLocation) { Loc.append('/'); Loc.append(S); } Title.setText(Loc.toString()); Vector Items; String PathStr=getPathStr(); if (ItemCache.containsKey(PathStr)) { Items=ItemCache.get(PathStr); } else { Items=new Vector(); File DirFile=new File(PathStr); TreeMap M=new TreeMap(); for(String S : DirFile.list()) { File F=new File(PathStr + "/" + S); if (F.isDirectory()) { M.put("000" + S, S); } else { M.put("010" + S,S); } } for(String S : M.values()) { File F=new File(PathStr + "/" + S); if ((F.isDirectory()) || (S.endsWith(".tag"))) { Items.add(new MovieItem(this,PathStr,S)); } } if (RelLocation.size()>0) { //Items.add(new MovieItem(this,PathStr,"..")); } ItemCache.put(PathStr,Items); } ItemList.setListData(Items); if (!LastSelected.containsKey(PathStr)) { LastSelected.put(PathStr,0); } int idx=LastSelected.get(PathStr); ItemList.setSelectedIndex(idx); //ItemList.setSelectedIndex(0); ItemList.scrollRectToVisible(ItemList.getCellBounds(idx,idx)); ItemList.repaint(); } private void action() { MovieItem M=(MovieItem)ItemList.getSelectedValue(); M.action(); } private void edit() { MovieItem M=(MovieItem)ItemList.getSelectedValue(); M.edit(); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub //System.out.println(e); if (e.getKeyCode()==KeyEvent.VK_LEFT) { moveUp(); } if (e.getKeyCode()==KeyEvent.VK_RIGHT) { action(); } if (e.getKeyCode()==KeyEvent.VK_ENTER) { action(); } if (e.getKeyCode()==KeyEvent.VK_SPACE) { action(); } if (e.getKeyCode() == KeyEvent.VK_E) { edit(); } if (e.getKeyCode()==KeyEvent.VK_F5) { clearCache(); scanDirectory(); } } public void clearCache() { ItemCache=new TreeMap > (); getImageLoader().clearCache(); } public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } public void mouseClicked(MouseEvent e) { if (e.getButton()==MouseEvent.BUTTON1) { //action(); int idx=ItemList.locationToIndex(e.getPoint()); java.awt.Rectangle R=ItemList.getCellBounds(idx,idx); if (R.contains(e.getPoint())) action(); } else { moveUp(); } //if ()>=0) action(); } public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } public ImageLoader getImageLoader() {return ImageCache;} }