📄 decodersettings.java
字号:
package abchr.settings;
import abchr.audio.CLIDecoder;
import abchr.audio.SampleFactory;
import abchr.audio.SampleProcessor;
import guiutils.LineLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class DecoderSettings implements Settings {
private static final String[] path=new String[]{"Decoders"};
private static class CLIDecoderWrapper {
private CLIDecoder decoder;
private String s;
public CLIDecoderWrapper(CLIDecoder decoder) {
this.decoder=decoder;
s=decoder.getAcceptedExtensions()[0]+", "+decoder.getCommandLine();
}
public String toString(){return s;}
}
private static class Change {
public static final int ADD=0;
public static final int REMOVE=1;
public int type;
public CLIDecoder decoder;
public Change(int type,CLIDecoder decoder) {
this.decoder=decoder;
this.type=type;
}
}
private JPanel panel;
private DefaultListModel listModel=new DefaultListModel();
private JList list;
private JTextField extensionField;
private JTextField cmdLineField;
private List changes=new LinkedList();
public DecoderSettings() {
File f=new File("clicodecs.cfg");
BufferedReader reader=null;
try {
reader=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String line=null;
while((line=reader.readLine())!=null) {
String[] cmd=line.split(",");
if(cmd.length!=2) {
System.out.println("Invalid line in clicodecs.cfg: "+line);
continue;
}
try {
CLIDecoder decoder=new CLIDecoder(cmd[0].trim(),cmd[1].trim());
SampleFactory.registerDecoder(decoder);
} catch(IllegalArgumentException e) {
System.out.println("Invalid line in clicodecs.cfg: "+line);
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(reader!=null) {
try{reader.close();}catch(Exception e){}
}
}
}
public String[] getPath(){return path;}
private void initPanel() {
if(panel!=null){return;}
panel=new JPanel(new BorderLayout());
list=new JList(listModel);
panel.add(new JScrollPane(list),BorderLayout.CENTER);
JPanel bottomPanel=new JPanel(new GridBagLayout());
GridBagConstraints constraints=new GridBagConstraints();
constraints.gridy=0;
bottomPanel.add(new JLabel("Extension: "),constraints);
constraints.weightx=1.0f;
constraints.fill=GridBagConstraints.HORIZONTAL;
bottomPanel.add(extensionField=new JTextField(),constraints);
constraints.gridy=1;
constraints.weightx=0.0f;
constraints.fill=GridBagConstraints.NONE;
bottomPanel.add(new JLabel("Command: "),constraints);
constraints.weightx=1.0f;
constraints.fill=GridBagConstraints.HORIZONTAL;
bottomPanel.add(cmdLineField=new JTextField(),constraints);
cmdLineField.setToolTipText("Use the placeholders %i for input file, %o for output file");
JPanel buttonPanel=new JPanel(new LineLayout(LineLayout.RIGHT_ALIGN));
JButton addButton=new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
CLIDecoder decoder=new CLIDecoder(extensionField.getText(),cmdLineField.getText());
listModel.addElement(new CLIDecoderWrapper(decoder));
changes.add(new Change(Change.ADD,decoder));
extensionField.setText("");
cmdLineField.setText("");
} catch(IllegalArgumentException ex) {
JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(panel),"Invalid command line.\nYou must use %i and %o to specify the input and\noutput file arguments respectively","Error",JOptionPane.WARNING_MESSAGE);
}
}
});
buttonPanel.add(addButton);
JButton removeButton=new JButton("Remove");
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int index=list.getSelectedIndex();
if(index==-1){return;}
CLIDecoder decoder=((CLIDecoderWrapper)listModel.remove(index)).decoder;
changes.add(new Change(Change.REMOVE,decoder));
}
});
buttonPanel.add(removeButton);
constraints.gridy=2;
constraints.weightx=0.0f;
constraints.gridwidth=2;
bottomPanel.add(buttonPanel,constraints);
panel.add(bottomPanel,BorderLayout.SOUTH);
}
public JComponent getView() {
initPanel();
return panel;
}
public void apply() {
Change c;
for(Iterator it=changes.iterator();it.hasNext();) {
c=(Change)it.next();
switch(c.type) {
case Change.ADD:SampleFactory.registerDecoder(c.decoder);break;
case Change.REMOVE:SampleFactory.unregisterDecoder(c.decoder);break;
}
}
changes.clear();
BufferedWriter writer=null;
try {
File f=new File("clicodecs.cfg");
f.delete();
writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
CLIDecoderWrapper decoder;
for(int i=0,n=listModel.getSize();i<n;i++) {
decoder=(CLIDecoderWrapper)listModel.get(i);
writer.write(decoder.s.substring(1)+'\n');
}
} catch(IOException e) {
e.printStackTrace();
} finally {
if(writer!=null) {
try{writer.close();}catch(Exception e){}
}
}
}
public void reset() {
changes.clear();
listModel.clear();
SampleProcessor[] decoders=SampleFactory.getDecoders();
for(int i=0;i<decoders.length;i++) {
if(decoders[i] instanceof CLIDecoder){listModel.addElement(new CLIDecoderWrapper((CLIDecoder)decoders[i]));}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -