package cc.glsn.v15.moviemenu; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.GridBagConstraints; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Scanner; import java.util.StringTokenizer; import javax.swing.JPanel; import javax.swing.JTextArea; import cc.glsn.Util; public class MovieItem extends JPanel { private static final long serialVersionUID = -6447250942918269283L; MovieMenu MM; String Path; String FileName; boolean Dir; String BaseName; JTextArea Title; JTextArea Desc; Component Line; Component Image; public MovieItem(MovieMenu mm,String path, String filename) { super(new java.awt.GridBagLayout()); MM=mm; Path=path; FileName=filename; Line=MM.getImageLoader().getLine(); File F=new File(Path + "/" + FileName); if (F.isDirectory()) { Dir=true; BaseName=FileName; } else { StringTokenizer Stok=new StringTokenizer(FileName,"."); BaseName=Stok.nextToken(); } String TitleText=BaseName; if (Dir) { TitleText="[" + BaseName + "]"; } File ImgFile=findImageFile(MM.RootDir +"/ALL/" + BaseName); if (Dir) ImgFile=findImageFile(Path + "/" + BaseName + "/folder"); if ((ImgFile!=null) && (ImgFile.exists())) { Image=MM.getImageLoader().getImageComponent(ImgFile); } File DescFile=new File(MM.RootDir +"/ALL/" + BaseName + ".txt"); if (Dir) DescFile=new File(Path + "/" + BaseName + "/folder.txt"); if (DescFile.exists()) { try { FileInputStream fis=new FileInputStream(DescFile); String FileText=Util.readInputStream(fis); fis.close(); Scanner S=new Scanner(FileText); TitleText=S.nextLine(); StringBuilder DescText=new StringBuilder(); while(S.hasNextLine()) { DescText.append(S.nextLine()); DescText.append('\n'); } Desc=new JTextArea(7,45); Desc.setColumns(45); Desc.setLineWrap(true); Desc.setWrapStyleWord(true); Desc.setFont(new Font(MovieMenu.FontName,Font.PLAIN,MovieMenu.FontSize)); Desc.setForeground(Color.YELLOW); Desc.setText(DescText.toString()); //Desc.setRows(Desc.getLineCount()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Title=new JTextArea(TitleText); Title.setBackground(MovieMenu.BGColor); Title.setForeground(Color.WHITE); Title.setFont(new Font(MovieMenu.FontName,Font.BOLD,MovieMenu.FontSize)); Title.setText(TitleText); addItems(); } private void addItems() { { 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.NORTHWEST; add(Line,c); } if (Image!=null) { GridBagConstraints c=new GridBagConstraints(); c.gridheight=2; c.gridwidth=1; c.fill=GridBagConstraints.NONE; c.insets=new java.awt.Insets(0,0,0,0); c.anchor=GridBagConstraints.WEST; add(Image,c); } { GridBagConstraints c=new GridBagConstraints(); c.gridheight=1; c.gridwidth=GridBagConstraints.REMAINDER; c.fill=GridBagConstraints.NONE; c.insets=new java.awt.Insets(1,1,1,1); c.anchor=GridBagConstraints.NORTHWEST; add(Title,c); } if (Desc!=null) { GridBagConstraints c=new GridBagConstraints(); c.gridheight=1; c.gridwidth=GridBagConstraints.REMAINDER; c.fill=GridBagConstraints.SOUTH; c.insets=new java.awt.Insets(1,1,1,1); c.anchor=GridBagConstraints.NORTHWEST; add(Desc,c); } } private void setAllColors(Color C) { Title.setBackground(C); setBackground(C); if (Desc!=null) Desc.setBackground(C); if (Line!=null) Line.setBackground(C); } public void setSelected(boolean isSelected) { if (isSelected) { setAllColors(MovieMenu.getHiColor()); } else { setAllColors(MovieMenu.BGColor); } } public void action() { if (Dir) { MM.moveDown(FileName); } else { new MoviePlayer().start(); } } public void edit() { String EditStr; if (Dir) { EditStr=Path + "/" + FileName + "/folder.txt"; } else { EditStr=MM.RootDir + "/ALL/" + BaseName + ".txt"; } System.out.println("EditStr: " + EditStr); try { File F=new File(EditStr); if (!F.exists()) { F.createNewFile(); } new RunEditor(EditStr).start(); } catch(IOException e) { e.printStackTrace(); } } private File findImageFile(String Base) { ArrayList Ext=new ArrayList();; Ext.add(".PNG"); Ext.add(".png"); Ext.add(".JPG"); Ext.add(".jpg"); Ext.add(".GIF"); Ext.add(".gif"); for(String S : Ext) { File F=new File(Base + S); //System.out.println("Trying: " + F); if (F.exists()) return F; } return null; } class MoviePlayer extends Thread { public void run() { if (MM.PlayerRunning) return; MM.PlayerRunning=true; System.out.println("Playing: " + BaseName); String[] CmdArr=new String[2]; CmdArr[0]=MM.MMConf.getStartCmd(); CmdArr[1]=BaseName + ".ISO"; try { Process P=Runtime.getRuntime().exec(CmdArr); new StreamCopy(P.getErrorStream(),System.out).start(); new StreamCopy(P.getInputStream(),System.out).start(); P.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } MM.PlayerRunning=false; } } class RunEditor extends Thread { String FilePath; public RunEditor(String str) { FilePath=str; } public void run() { String[] CmdArr=new String[2]; CmdArr[0]=MM.MMConf.getEditCmd(); CmdArr[1]=FilePath; try { Process P=Runtime.getRuntime().exec(CmdArr); new StreamCopy(P.getErrorStream(),System.out).start(); new StreamCopy(P.getInputStream(),System.out).start(); P.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class StreamCopy extends Thread { InputStream In; OutputStream Out; public StreamCopy(InputStream in, OutputStream out) { In=in; Out=out; } public void run() { try { byte Buff[]=new byte[1024]; while(true) { int r; r = In.read(Buff); if (r<0) return; Out.write(Buff, 0, r); } } catch (IOException e) { return; } } } }