⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testconfigsetuppanel.java

📁 Java version of ABC/HR comparator v0.5. by schnofler. Runs on Sun JRE 1.5 or later
💻 JAVA
字号:
package abchr.gui;

import abchr.RawTestConfig;
import abchr.audio.*;
import guiutils.*;

import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestConfigSetupPanel extends JPanel {
	/*private class AddSamplesBrowseButton extends AbstractBrowseButton {
		public AddSamplesBrowseButton(String text,Component parent,JFileChooser fileChooser,boolean multiple) {
			super(text,parent,fileChooser,multiple);
		}

		protected void addFiles(File[] files){
			TestConfigSetupPanel.this.addFiles(files);
		}

		public void actionPerformed(ActionEvent e) {
			super.actionPerformed(e);
		}
	}*/

	private JFileChooser fileChooser=new SimpleFileChooser();

	private RawTestConfig testConfig=new RawTestConfig();

	private JTextField originalField=new JTextField(20);
	private DefaultListModel listModel=new DefaultListModel();
	private JList sampleList=new JList(listModel);
	private List originalControls=new ArrayList(4);
	private List samplesControls=new ArrayList(4);
	private JButton calcOffsetsButton=new JButton("Calculate Offsets");
	private JButton calcGainButton=new JButton("Calculate Gain");
	private final JTextField fileField=new JTextField();

	public TestConfigSetupPanel() {
		fileChooser.resetChoosableFileFilters();
		fileChooser.setFileFilter(SampleFactory.getFileFilter());

		JPanel p;
		this.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		this.setLayout(new FlexibleGridLayout(8,2,10,5,true));

		this.add(new JLabel("Original Sample"));
		p=new JPanel(new BorderLayout());
		p.add(originalField,BorderLayout.CENTER);
		JButton browseOriginalButton=new TextFieldBrowseButton(this,fileChooser,originalField,false);
		p.add(browseOriginalButton,BorderLayout.EAST);
		this.add(p);
		this.add(new JComponent(){});

		JPanel controlsPanel=new JPanel(new LineLayout(LineLayout.RIGHT_ALIGN));
		makeOriginalControls();
		AttributableView view;
		for(int i=0;i<originalControls.size();i++) {
			view=(AttributableView)originalControls.get(i);
			controlsPanel.add(new JLabel(view.getName()));
			controlsPanel.add(view.getComponent());
		}
		this.add(controlsPanel);

		JLabel label=new JLabel("Test Samples");
		label.setVerticalAlignment(JLabel.TOP);
		this.add(label);
		sampleList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if(sampleList.getSelectedIndex()==-1){return;}
				SampleConfig sample=(SampleConfig)sampleList.getSelectedValue();
				for(int i=0,n=samplesControls.size();i<n;i++) {
					((AttributableView)samplesControls.get(i)).setModel(sample);
				}
			}
		});
		final JPopupMenu listPopupMenu=new JPopupMenu();
		JMenuItem item=new JMenuItem("Remove");
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(sampleList.getSelectedIndex()!=-1) {
					testConfig.remove(sampleList.getSelectedIndex());
					listModel.remove(sampleList.getSelectedIndex());
				}
			}
		});
		listPopupMenu.add(item);
		item=new JMenuItem("Remove All");
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listModel.clear();
				testConfig.clear();
			}
		});
		listPopupMenu.add(item);
		sampleList.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e){popup(e);}
			public void mouseReleased(MouseEvent e){popup(e);}
			private void popup(MouseEvent e) {
				if(e.isPopupTrigger()) {
					sampleList.setSelectedIndex(sampleList.locationToIndex(new Point(e.getX(),e.getY())));
					listPopupMenu.show(sampleList,e.getX(),e.getY());
				}
			}
		});
		this.add(new JScrollPane(sampleList));
		this.add(new JComponent(){});
		p=new JPanel(new BorderLayout());
		p.add(fileField,BorderLayout.CENTER);
		JPanel p2=new JPanel(new LineLayout(5,5,LineLayout.CENTER,true));
		p2.add(new TextFieldBrowseButton(this,fileChooser,fileField,true));
		JButton addSampleButton=new JButton("Add");
		addSampleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(fileField.getText().equals("")){return;}
				String[] s=fileField.getText().split(";");
				if(addFiles(s)){fileField.setText("");}
			}
		});
		p2.add(addSampleButton);
		p.add(p2,BorderLayout.EAST);
		this.add(p);
		this.add(new JComponent(){});
		makeSamplesControls();
		controlsPanel=new JPanel(new LineLayout(LineLayout.RIGHT_ALIGN));
		for(int i=0,n=samplesControls.size();i<n;i++) {
			view=(AttributableView)samplesControls.get(i);
			controlsPanel.add(new JLabel(view.getName()));
			controlsPanel.add(view.getComponent());
		}
		this.add(controlsPanel);

		this.add(new JComponent(){});
		p=new JPanel(new LineLayout(LineLayout.RIGHT_ALIGN));
		calcOffsetsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){calcOffsets();}
		});
		p.add(calcOffsetsButton);
		calcGainButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){calcGain();}
		});
		p.add(calcGainButton);
		this.add(p);

		this.add(new JComponent(){});
		JCheckBox hashCheckBox=new JCheckBox("Add verification hashes");
		hashCheckBox.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				hashBoxChanged(((JCheckBox)e.getSource()).isSelected());
			}
		});
		this.add(hashCheckBox);
	}

	private void hashBoxChanged(boolean value) {
		testConfig.setVerification(value);
	}

	private void makeSamplesControls() {
		samplesControls.add(new OffsetSpinner());
		samplesControls.add(new GainSpinner());
	}

	private void makeOriginalControls() {
		originalControls.add(new OffsetSpinner(testConfig.getOriginal()));
		originalControls.add(new GainSpinner(testConfig.getOriginal()));
	}

	private WaitMessageDialog waitDialog=new WaitMessageDialog(JOptionPane.getFrameForComponent(this),"Please Wait");

	private class LabelChanger implements Runnable {
		String s;
		public LabelChanger(String s){this.s=s;}
		public void run(){waitDialog.setText(s);}
	}

	private void calcOffsets() {
		try {
			updateOriginal();
		} catch(IllegalArgumentException e) {
			JOptionPane.showMessageDialog(this,"Syntax error in orignal sample field.","Error",JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(testConfig.getOriginal().getFilename().equals("")) {
			JOptionPane.showMessageDialog(this,"Select an original sample first.","Error",JOptionPane.ERROR_MESSAGE);
			return;
		}
		SwingWorker sw=new SwingWorker() {
			public Object construct() {
				try {
					SwingUtilities.invokeLater(new LabelChanger("Decoding original"));
					Sample[] samples=new Sample[testConfig.getSampleCount()+1];
					samples[0]=SampleFactory.createSample(testConfig.getOriginal());
					for(int i=0,n=testConfig.getSampleCount();i<n;i++) {
						SwingUtilities.invokeLater(new LabelChanger("Decoding sample "+(i+1)));
						samples[i+1]=SampleFactory.createSample(testConfig.get(i));
					}
					SwingUtilities.invokeLater(new LabelChanger("Calculating offsets"));
					OffsetCalculator.configureOffsets(samples);
					SampleOffset.setOffset(testConfig.getOriginal(),SampleOffset.getOffset(samples[0]));
					for(int i=0,n=testConfig.getSampleCount();i<n;i++) {
						SampleOffset.setOffset(testConfig.get(i),SampleOffset.getOffset(samples[i+1]));
					}
					return null;
				} catch(IOException e) {
					return e;
				} catch(UnsupportedAudioFileException e) {
					return e;
				}
			}

			public void finished(){waitDialog.setVisible(false);}
		};
		sw.start();
		waitDialog.show(JOptionPane.getFrameForComponent(calcOffsetsButton));
		Exception e=(Exception)sw.get();
		if(e!=null) {
			JOptionPane.showMessageDialog(this,e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
		}
		updateView();
	}

	private void calcGain() {
		try {
			updateOriginal();
		} catch(IllegalArgumentException e) {
			JOptionPane.showMessageDialog(this,"Syntax error in orignal sample field.","Error",JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(testConfig.getOriginal().getFilename().equals("")) {
			JOptionPane.showMessageDialog(this,"Select an original sample first.","Error",JOptionPane.ERROR_MESSAGE);
			return;
		}
		SwingWorker sw=new SwingWorker() {
			public Object construct() {
				try {
					SwingUtilities.invokeLater(new LabelChanger("Decoding original"));
					Sample original=SampleFactory.createSample(testConfig.getOriginal());
					Sample[] samples=new Sample[testConfig.getSampleCount()];
					for(int i=0,n=testConfig.getSampleCount();i<n;i++) {
						SwingUtilities.invokeLater(new LabelChanger("Decoding sample "+(i+1)));
						samples[i]=SampleFactory.createSample(testConfig.get(i));
					}
					SwingUtilities.invokeLater(new LabelChanger("Calculating gain"));
					OffsetCalculator.configureGains(original,samples);
					SampleGain.setGain(testConfig.getOriginal(),SampleGain.getGain(original));
					for(int i=0,n=testConfig.getSampleCount();i<n;i++) {
						SampleGain.setGain(testConfig.get(i),SampleGain.getGain(samples[i]));
					}
					return null;
				} catch(IOException e) {
					return e;
				} catch(UnsupportedAudioFileException e) {
					return e;
				}
			}

			public void finished(){waitDialog.setVisible(false);}
		};
		sw.start();
		waitDialog.show(JOptionPane.getFrameForComponent(calcGainButton));
		Exception e=(Exception)sw.get();
		if(e!=null) {
			JOptionPane.showMessageDialog(this,e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
		}
		updateView();
	}

	private boolean addFiles(String[] strings) {
		StringBuffer erroneousFiles=new StringBuffer();
		SampleConfig[] configs=new SampleConfig[strings.length];
		for(int i=0;i<strings.length;i++) {
			try {
				SampleConfig config=SampleConfig.makeFromString(strings[i]);
				SampleOffset.setOffset(config,0);
				configs[i]=config;
			} catch(Exception e) {
				erroneousFiles.append("   ");
				erroneousFiles.append(strings[i]);
				erroneousFiles.append('\n');
			}
		}
		if(erroneousFiles.length()!=0) {
			JOptionPane.showMessageDialog(this,"The file(s)\n"+erroneousFiles+"is invalid.","Error",JOptionPane.ERROR_MESSAGE);
			return false;
		}
		for(int i=0;i<configs.length;i++) {
			testConfig.add(configs[i]);
			listModel.addElement(configs[i]);
		}
		return true;
	}

	private void updateOriginal() {
		SampleConfig orig=testConfig.getOriginal();
		SampleConfig sc=SampleConfig.makeFromString(originalField.getText());
		orig.setFilename(sc.getFilename());
		SampleProcessing.setProcessingQueue(orig,SampleProcessing.getProcessingQueue(sc));
	}

	public void updateConfig() throws IllegalStateException {
		if(originalField.getText().equals("")) {
			throw new IllegalStateException("You have to select a reference sample.");
		}
		if(testConfig.getSampleCount()==0) {
			throw new IllegalStateException("You might want to select some test samples first.");
		}
		try {
			updateOriginal();
		} catch(IllegalArgumentException e) {
			throw new IllegalStateException("Syntax error in original field.");
		}
	}

	private void updateView() {
		SampleConfig original=testConfig.getOriginal();
		//originalField.setText(original.getFilename());
		for(int i=0;i<originalControls.size();i++) {
			((AttributableView)originalControls.get(i)).setModel(original);
		}
		listModel.clear();
		for(int i=0,n=testConfig.getSampleCount();i<n;i++) {
			listModel.addElement(testConfig.get(i));
		}
	}

	public void setModel(RawTestConfig testConfig) {
		this.testConfig=testConfig;
		updateView();
	}

	public RawTestConfig getModel(){return testConfig;}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -