WordTestingScreen

public class WordTestingScreen extends BaseScreen {
	private static final long serialVersionUID = 1L;
	private WordTestingScreen thisFrame;
	
	//	Component
	JLabel lbl_previous ;
	JLabel lbl_showIsRight;
	JLabel lbl_next;
	JLabel lbl_currentWordIndex;
	JLabel lbl_totalTestWords;
	JLabel lbl_wordEN;
	JLabel lbl_symbol;
	JLabel lbl_wordCN;
	JLabel lbl_process;
	
	WordInputJTextPane txt_wordEN;
	JTextArea txt_wordCN;
	JTextArea txt_showCondition;
	JTextField txt_symbol;
	
	JCheckBox jcb_showCondition;
	JCheckBox jcb_miniModel;
	JCheckBox jcb_forgetTag;
	
	JButton btn_commit ;
	JButton btn_cancel;
	
	WordTestingActionListener wordTestActionListener = new WordTestingActionListener();
	
	private int left_label_width = 65;
	private int left_txt_width = 345;
	private int line_heigh = 5;
	JScrollPane scrollCondition;
	JScrollPane scrollWordCn;
	
	// data
	private WordSearchScreen parentFrame;
	List<Word> dataList ;
//	private WordHander[] wordHanders = new WordHander[]{};
	
	// business
	private WordTestScreenService service = new WordTestScreenService();
	Integer currIndex = -1;
	private Word currWord = null;
	WordTestingMouseListener wordTestingMouseListener = new WordTestingMouseListener();
	private WordTestingKeyListener wordTestingKeyListener = new WordTestingKeyListener();
	private WordMiniTestingScreen miniFrame ;
	Boolean commitFlag = true;
	
	public WordTestingScreen(WordSearchScreen parentFrame) {
		super(parentFrame);
		thisFrame = this;
		this.parentFrame = (WordSearchScreen) super.parentFrame;
		initialData();
		initialJFrame();
		refreshUI();
	}

	
	
	private void initialData() {
		thisFrame.dataList = service.randomWordList(parentFrame.searchWords);
		service.initialTestFlag(dataList);
		if(dataList.size()>0){
			currIndex = 0;
			currWord = dataList.get(currIndex);
		}
	}

	private void initialJFrame() {
		thisFrame = this;
		thisFrame.setSize(new Dimension(450, 330));
		super.setSubFrame(this);
		thisFrame.setTitle("Word Testing Screen");
		initialBody();
	}

	private void initialBody() {
		ComponentFactory compFactory = new ComponentFactory();
		
		// line1
		lbl_previous = new JLabel("Previous");
//		lbl_previous.setBorder(new LineBorder(Color.black));
		ComponentUtil.setBounds(65, 20, thisFrame, lbl_previous, ComponentUtil.INNER_LEFT_TOP, 15);
		lbl_previous.addMouseListener(wordTestingMouseListener);
		thisFrame.add(lbl_previous);
		
		lbl_showIsRight = new JLabel("[-]");
		ComponentUtil.setBounds(20, 20, lbl_previous, lbl_showIsRight, ComponentUtil.RIGHT_CENTER);
		thisFrame.add(lbl_showIsRight);
		
		lbl_next = new JLabel("Next");
		ComponentUtil.setBounds(30, 20, thisFrame, lbl_next, ComponentUtil.INNER_RIGHT_TOP, 15);
		lbl_next.setLocation(lbl_next.getX()+10, lbl_next.getY());
		lbl_next.addMouseListener(wordTestingMouseListener);
		thisFrame.add(lbl_next);
		
		lbl_currentWordIndex = new JLabel("12");
		ComponentUtil.setBounds(30, 20, lbl_showIsRight, lbl_currentWordIndex, ComponentUtil.RIGHT_CENTER, 15);
		thisFrame.add(lbl_currentWordIndex);
		
		lbl_totalTestWords = new JLabel("/  12");
		ComponentUtil.setBounds(35, 20, lbl_currentWordIndex, lbl_totalTestWords, ComponentUtil.RIGHT_CENTER,-3);
		thisFrame.add(lbl_totalTestWords);
		
		lbl_process = new JLabel("progress: 100.00% / right: 100.00%");
//		lbl_process.setBorder(new LineBorder(Color.red));
		ComponentUtil.setBounds(195, 20, lbl_totalTestWords, lbl_process, ComponentUtil.RIGHT_CENTER,15);
		thisFrame.add(lbl_process);
		
		// line2
		lbl_wordEN = new JLabel("Word EN:");
		ComponentUtil.setBounds(left_label_width, 20, lbl_previous, lbl_wordEN, ComponentUtil.BOTTOM_LEFT, line_heigh);
		thisFrame.add(lbl_wordEN);
		
//		txt_wordEN = compFactory.getJTextField();
//		ComponentUtil.setBounds(left_txt_width, 20, lbl_wordEN, txt_wordEN, ComponentUtil.RIGHT_CENTER);
		txt_wordEN = compFactory.getWordInputTextPane();
		ComponentUtil.setBounds(left_txt_width, 20, lbl_wordEN, txt_wordEN, ComponentUtil.RIGHT_CENTER);
		txt_wordEN.setRightWordEn(currWord.getFldWorden());
		txt_wordEN.setWordBuffer(new String[dataList.size()]);
		txt_wordEN.addKeyListener(wordTestingKeyListener);
		thisFrame.add(txt_wordEN);
		 
		
		// 
		lbl_symbol = new JLabel("Symbol:");
		ComponentUtil.setBounds(left_label_width, 20, lbl_wordEN, lbl_symbol, ComponentUtil.BOTTOM_LEFT, line_heigh);
		thisFrame.add(lbl_symbol);
		
		txt_symbol = compFactory.getJTextField();
		ComponentUtil.setBounds(left_txt_width, 20, lbl_symbol, txt_symbol, ComponentUtil.RIGHT_CENTER);
		thisFrame.add(txt_symbol);
		
		// line3
		lbl_wordCN = new JLabel("Word CN:");
		ComponentUtil.setBounds(left_label_width, 20, lbl_symbol, lbl_wordCN, ComponentUtil.BOTTOM_LEFT, line_heigh);
		thisFrame.add(lbl_wordCN);
		
		txt_wordCN = compFactory.getJTextArea();
		scrollWordCn = compFactory.getScrollableJTextArea(txt_wordCN, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		ComponentUtil.setBounds(left_txt_width, 100, lbl_wordCN, scrollWordCn, ComponentUtil.RIGHT_TOP);
		thisFrame.add(scrollWordCn);
		
		// line 4
		jcb_showCondition = new JCheckBox("Show Condition");
		ComponentUtil.setBounds(120, 20, lbl_wordCN, jcb_showCondition, ComponentUtil.BOTTOM_LEFT,80);
		jcb_showCondition.addActionListener(wordTestActionListener);
		jcb_showCondition.setActionCommand("showCondition");
		jcb_showCondition.setSelected(true);
		thisFrame.add(jcb_showCondition);
		
		jcb_miniModel = new JCheckBox("Mini Model");
		ComponentUtil.setBounds(120, 20, jcb_showCondition, jcb_miniModel, ComponentUtil.RIGHT_CENTER);
		jcb_miniModel.addActionListener(wordTestActionListener);
		jcb_miniModel.setActionCommand("miniModel");
		thisFrame.add(jcb_miniModel);
		
		jcb_forgetTag = new JCheckBox("Forget Tag");
		ComponentUtil.setBounds(120, 20, jcb_miniModel, jcb_forgetTag, ComponentUtil.RIGHT_CENTER);
		jcb_forgetTag.addActionListener(wordTestActionListener);
		jcb_forgetTag.setActionCommand("forgettag");
		thisFrame.add(jcb_forgetTag);
		
		// line 5
		txt_showCondition = compFactory.getJTextArea();
		scrollCondition = compFactory.getScrollableJTextArea(txt_showCondition, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER, JScrollPane.VERTICAL_SCROLLBAR_NEVER);
		ComponentUtil.setBounds(left_txt_width, 50, scrollWordCn, scrollCondition, ComponentUtil.BOTTOM_CENTER,20);
		thisFrame.add(scrollCondition);
		
		// line 6
		btn_cancel = new JButton("Cancel");
		ComponentUtil.setBounds(80, 25, scrollCondition, btn_cancel, ComponentUtil.BOTTOM_RIGHT, 10);
		thisFrame.add(btn_cancel);
		btn_cancel.addActionListener(wordTestActionListener);
		btn_cancel.setActionCommand("cancel");
		
		btn_commit = new JButton("Commit");
		ComponentUtil.setBounds(80, 25, btn_cancel, btn_commit, ComponentUtil.LEFT_CENTER, 5);
		thisFrame.add(btn_commit);
		btn_commit.setActionCommand("commit");
		btn_commit.addActionListener(wordTestActionListener);
		
	}
	
	public void refreshUI() {
		if(currIndex<0){
			return;
		}
		currWord = dataList.get(currIndex);
		// test result
//		currWord.setFldErrorfag(wordHanders[currIndex].handlerResult());
		
		if(currWord.getFldErrorfag().equals("1")){
			lbl_showIsRight.setText("[E]");
		}else if(currWord.getFldErrorfag().equals("0")){
			lbl_showIsRight.setText("[R]");
		}else{
			lbl_showIsRight.setText("[-]");
		}
		
		lbl_currentWordIndex.setText((currIndex+1)+"");
		lbl_totalTestWords.setText("/  " + dataList.size());
		
		txt_symbol.setText(currWord.getFldSymbol());
		
		txt_wordEN.setText(txt_wordEN.getWordBuffer()[currIndex]);
		txt_wordEN.setRightWordEn(currWord.getFldWorden());
//		txt_wordEN.setText(currWord.getFldWorden());
		
		txt_wordCN.setText(currWord.getFldWordcn().replace('\t', '\n'));
		
		txt_showCondition.setText(service.getConditionString(parentFrame.conditionMap));
		
		jcb_forgetTag.setSelected(("1".equals(currWord.getFldForgettag())?true:false));
		
		Object[] infos = service.getProgressInfo(dataList);
		String processInfo = (String) infos[1];
		lbl_process.setText(processInfo);
		if(commitFlag && (double)infos[0] == 100){
			int option = JOptionPane.showConfirmDialog(this,"你已完成所有练习,现在需要提交吗?",  "已完成", JOptionPane.YES_NO_OPTION);
			if(option == JOptionPane.YES_OPTION){
				commit();
			}else{
				commitFlag = false;
			}
		}
	}
	
	class WordTestingActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			String actionCommand = e.getActionCommand();
			System.out.println(actionCommand);
			if(actionCommand.equalsIgnoreCase("showCondition")){
				hideConditionModel(jcb_showCondition.isSelected());
			}else if(actionCommand.equalsIgnoreCase("miniModel")){
				miniModel();
			}else if(actionCommand.equalsIgnoreCase("forgettag")){
				if(jcb_forgetTag.isSelected()){
					currWord.setFldForgettag("1");
				}else{
					currWord.setFldForgettag("0");
				}
				System.out.println(currWord);
			}else if(actionCommand.equalsIgnoreCase("commit")){
				saveUserInput();
				refreshUI();
				commit();
			}else if(actionCommand.equalsIgnoreCase("cancel")){
				thisFrame.exitScreen();
				if(miniFrame!=null){
					miniFrame.dispose();
				}
			}
		}
	}

	private void miniModel() {
		if(miniFrame==null){
			miniFrame = new WordMiniTestingScreen(parentFrame,thisFrame);
		}else{
			miniFrame.currIndex = thisFrame.currIndex;
			miniFrame.refreshUI();
			miniFrame.commitFlag = thisFrame.commitFlag;
		}
		miniFrame.setVisible(true);
		thisFrame.setVisible(false);
	}
	
	private void commit(){
		String result = service.getResult(dataList);
		JOptionPane.showMessageDialog(null,result ,"测试完成" , JOptionPane.INFORMATION_MESSAGE); 
		parentFrame.search();
		parentFrame.setVisible(true);
		thisFrame.dispose();
		if(miniFrame != null){
			miniFrame.dispose();
		}
	}
	
	private void saveUserInput(){
		if(currIndex < 0){
			return;
		}
		currWord = dataList.get(currIndex);
//		wordHanders[currIndex].setRealWordEN(currWord.getFldWorden())//
//				.setInputWordIndex(currIndex)//
//				.setInputWordEN(txt_wordEN.getText());
		currWord.setFldErrorfag(txt_wordEN.handlerResult());
		txt_wordEN.getWordBuffer()[currIndex] = txt_wordEN.getText();
	}
	
	private void hideConditionModel(boolean flag){
		if(flag){
			ComponentUtil.setBounds(80, 25, scrollCondition, btn_cancel, ComponentUtil.BOTTOM_RIGHT, 10);
			ComponentUtil.setBounds(80, 25, btn_cancel, btn_commit, ComponentUtil.LEFT_CENTER, 5);
			scrollCondition.setVisible(flag);
			thisFrame.setSize(new Dimension(450, 330));
		}else{
			scrollCondition.setVisible(flag);
			thisFrame.setSize(new Dimension(450, 275));
			ComponentUtil.setBounds(80, 25, scrollWordCn, btn_cancel, ComponentUtil.BOTTOM_RIGHT, 25);
			ComponentUtil.setBounds(80, 25, btn_cancel, btn_commit, ComponentUtil.LEFT_CENTER, 5);
		}
	}
	
	class WordTestingKeyListener implements KeyListener{
		private boolean ctrl_flag = false;
		private boolean alt_flag = false;
		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode() == 17){
				ctrl_flag = true;
			}
			if(e.getKeyCode() == 18){
				alt_flag = true;
			}
			
			if((ctrl_flag || alt_flag) && e.getKeyCode() == 39 ){ // -->
				nextWord();
			}else if((ctrl_flag || alt_flag)  && e.getKeyCode() == 37){ // <--
				prevWord();
			}else if((ctrl_flag || alt_flag)  && e.getKeyCode() == 68){
				miniModel();
			}else if((ctrl_flag || alt_flag)  && e.getKeyCode() == 70){
				ctrl_flag = alt_flag = false;
				thisFrame.setExtendedState(JFrame.ICONIFIED);
			}else if(ctrl_flag && e.getKeyCode() == 83){
				txt_wordEN.setText(currWord.getFldWorden());
			}else if(ctrl_flag && e.getKeyCode() == 82){ 	//r
				changeForgetTag();
			}
		}
		private void changeForgetTag() {
			jcb_forgetTag.setSelected(!jcb_forgetTag.isSelected());
			currWord.setFldForgettag((jcb_forgetTag.isSelected()?"1":"0"));
		}
		@Override
		public void keyReleased(KeyEvent e) {
			if(e.getKeyCode() == 17){
				ctrl_flag = false;
			}else if(e.getKeyCode() == 18){
				alt_flag = false;
			}
			if(e.getKeyCode() == 10){
				txt_wordEN.setText(txt_wordEN.getText().replace("\r", "").replace("\n", ""));
				nextWord();
			}
			if(ctrl_flag && e.getKeyCode() == 83){
				txt_wordEN.setText("");
			}
		}

		@Override
		public void keyTyped(KeyEvent e) {
			
		}
		
	}
	
	private void prevWord(){
		if(dataList.size()<=0){
			return ;
		}
		saveUserInput();
		if(currIndex -1 <0){
			currIndex = dataList.size()-1;
		}else{
			currIndex--;
		}
		refreshUI();
	}
	
	private void nextWord(){
		if(dataList.size()<=0){
			return ;
		}
		saveUserInput();
		if(currIndex +1 >= dataList.size()){
			currIndex = 0;
		}else{
			currIndex++;
		}
		refreshUI();
	}
	
	class WordTestingMouseListener extends MouseAdapter{
		String actionCommand = "";

		public void mouseClicked(MouseEvent e) {
			String actionCommand = "";
			if(e.getComponent() instanceof JLabel){
				actionCommand = ((JLabel)e.getComponent()).getText();
			}
			if(actionCommand.equalsIgnoreCase("Previous") ){
				prevWord();
			}else if(actionCommand.equalsIgnoreCase("Next") ){
				nextWord();
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值