WordMiniTestingScreen

public class WordMiniTestingScreen extends BaseScreen implements MouseMotionListener{
	private static final long serialVersionUID = 1L;
	protected float initOpac = 1;//0.45f;// 0.25f;
	private WordMiniTestingScreen thisFrame ;
	private MiniMouseListener mouseListener ;
	private Point movePoint = new Point();
	private JLabel lbl_result;
	private JLabel lbl_currentWordIndex;
	private JLabel lbl_x;
	private JLabel lbl_totalWords;
	private JLabel lbl_previous;
	private JLabel lbl_next;
	private JLabel lbl_commit;
	private JLabel lbl_detailModel;
	private JLabel lbl_exit;
	private WordInputJTextPane txt_wordEN;
	private OneLineJTextArea txt_wordCN;
	private JCheckBox jcb_forgetTag;
	private WordTestingScreen detalFrame;
	private JLabel lbl_process;
	
	// data
	private WordSearchScreen parentFrame;
	private List<Word> dataList ;
	
	// business
	private WordTestScreenService service = new WordTestScreenService();
	Integer currIndex = -1;
	private Word currWord = null;
	private WordTestingKeyListener wordTestingKeyListener = new WordTestingKeyListener();
//	public static void main(String[] args) {
//		new WordMiniTestingScreen().setVisible(true);
//	}
	
//	public WordMiniTestingScreen(){
//		initialJFrame();
//	}
	
	boolean commitFlag = true;
	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){
				detailModel();
			}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());
			}
		}

		@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) {
			
		}
		
	}
	
	public WordMiniTestingScreen(WordSearchScreen parentFrame, WordTestingScreen detalFrame) {
		super(parentFrame);
		this.detalFrame = detalFrame;
		this.parentFrame = parentFrame ;
		initialJFrame();
	}
	
	@SuppressWarnings("restriction")
	private void initialJFrame(){
		thisFrame = this;
		super.setSubFrame(thisFrame);
		thisFrame.setUndecorated(true);
		 
		thisFrame.setSize(new Dimension(800, 50));
		mouseListener = new MiniMouseListener(thisFrame);
		thisFrame.addMouseMotionListener(thisFrame);
		thisFrame.addMouseListener(mouseListener);
		com.sun.awt.AWTUtilities.setWindowOpacity(thisFrame,initOpac);
		initialData();
		initialBody();
		refreshUI();
	}
	public void refreshUI() {
		if(currIndex<0){
			return;
		}
		currWord = dataList.get(currIndex);
//		// test result
		currWord.setFldErrorfag(wordHanders[currIndex].handlerResult());
//		
		if(currWord.getFldErrorfag().equals("1")){
			lbl_result.setText("[ E ]");
		}else if(currWord.getFldErrorfag().equals("0")){
			lbl_result.setText("[ R ]");
		}else{
			lbl_result.setText("[ - ]");
		}

		lbl_currentWordIndex.setText((currIndex+1)+"");
		lbl_totalWords.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());
//		
		String wordCN = currWord.getFldWordcn().replace('\t', '\n');
		txt_wordCN.setText(wordCN.substring(0,wordCN.length()-1));
		txt_wordCN.startDisplay();
//		
//		txt_showCondition.setText(service.getConditionString(parentFrame.conditionMap));
//		
		Object[] infos = service.getProgressInfo(dataList);
		String processInfo = ((String) infos[1]).replace("progress:", "").replace("right:", "").replace(" ", "").replace("%", "");
		lbl_process.setText(processInfo);
		
		jcb_forgetTag.setSelected(("1".equals(currWord.getFldForgettag())?true:false));
		
		if(commitFlag && (double)infos[0] == 100){
			int option = JOptionPane.showConfirmDialog(this,"你已完成所有练习,现在需要提交吗?",  "已完成", JOptionPane.YES_NO_OPTION);
			if(option == JOptionPane.YES_OPTION){
				commit();
			}else{
				commitFlag = false;
			}
		}
	}

	private void initialData() {
		thisFrame.dataList = detalFrame.dataList;
		thisFrame.commitFlag = detalFrame.commitFlag;
//		service.initialTestFlag(dataList);
		if(dataList.size()>0){
			currIndex = detalFrame.currIndex;
			currWord = dataList.get(currIndex);
		}
	}
	
	private void initialBody() {
		ComponentFactory compFactory = new ComponentFactory();
		Font btnFont = new Font("Arial", Font.BOLD, 11);
		
		lbl_previous = new JLabel("<<");
		lbl_previous.setFont(btnFont);
		ComponentUtil.setBounds(20, 20, thisFrame, lbl_previous, ComponentUtil.INNER_LEFT_TOP, 5);
		lbl_previous.setLocation(lbl_previous.getX()+5, lbl_previous.getY());
		lbl_previous.addMouseListener(mouseListener);
		thisFrame.add(lbl_previous);
		
		lbl_result = new JLabel("[ R ]");
		lbl_result.setFont(btnFont);
		ComponentUtil.setBounds(25, 20, lbl_previous, lbl_result, ComponentUtil.RIGHT_CENTER);
		thisFrame.add(lbl_result);
		
		lbl_next = new JLabel(">>");
		lbl_next.setFont(btnFont);
		ComponentUtil.setBounds(25, 20, lbl_result, lbl_next, ComponentUtil.RIGHT_CENTER,2);
		lbl_next.addMouseListener(mouseListener);
		thisFrame.add(lbl_next);
		
		txt_wordEN = compFactory.getWordInputTextPane();
		txt_wordEN.setBackground(thisFrame.getBackground());
		ComponentUtil.setBounds(620, 18, lbl_next, txt_wordEN, ComponentUtil.RIGHT_CENTER);
		txt_wordEN.setWordBuffer(detalFrame.txt_wordEN.getWordBuffer());
		txt_wordEN.addKeyListener(wordTestingKeyListener);
		thisFrame.add(txt_wordEN);
		
		jcb_forgetTag = new JCheckBox("F");
		ComponentUtil.setBounds(35, 20, txt_wordEN, jcb_forgetTag, ComponentUtil.RIGHT_CENTER,5);
		jcb_forgetTag.addMouseListener(mouseListener);
		thisFrame.add(jcb_forgetTag);
		
		lbl_commit = new JLabel("C");
		ComponentUtil.setBounds(10, 20, jcb_forgetTag, lbl_commit, ComponentUtil.RIGHT_CENTER,5);
		lbl_commit.addMouseListener(mouseListener);
		thisFrame.add(lbl_commit);
		
		lbl_exit = new JLabel("E");
		ComponentUtil.setBounds(10, 20, lbl_commit, lbl_exit, ComponentUtil.RIGHT_CENTER,7);
		lbl_exit.addMouseListener(mouseListener);
		thisFrame.add(lbl_exit);
		
		lbl_detailModel = new JLabel("D");
		ComponentUtil.setBounds(10, 20, lbl_exit, lbl_detailModel, ComponentUtil.RIGHT_CENTER,7);
		lbl_detailModel.addMouseListener(mouseListener);
		thisFrame.add(lbl_detailModel);
		
		// line2
		lbl_currentWordIndex = new JLabel("10");
		ComponentUtil.setBounds(30, 20, lbl_previous, lbl_currentWordIndex, ComponentUtil.BOTTOM_LEFT,1);
		lbl_currentWordIndex.setLocation(lbl_currentWordIndex.getX()-6, lbl_currentWordIndex.getY());
		lbl_currentWordIndex.setHorizontalAlignment(SwingConstants.CENTER);
		thisFrame.add(lbl_currentWordIndex);
		
		lbl_x = new JLabel("/");
		ComponentUtil.setBounds(5, 20, lbl_currentWordIndex, lbl_x, ComponentUtil.RIGHT_CENTER,2);
		thisFrame.add(lbl_x);
		
		lbl_totalWords = new JLabel("50");
		lbl_totalWords.setHorizontalAlignment(SwingConstants.CENTER);
		ComponentUtil.setBounds(30, 20, lbl_x, lbl_totalWords, ComponentUtil.RIGHT_CENTER);
		thisFrame.add(lbl_totalWords);
		
		txt_wordCN = new OneLineJTextArea(); //compFactory.getJTextArea();
		txt_wordCN.setBackground(thisFrame.getBackground());
		JScrollPane scrollWordCN = compFactory.getScrollableJTextArea(txt_wordCN, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER, JScrollPane.VERTICAL_SCROLLBAR_NEVER);
		ComponentUtil.setBounds(620, 18, lbl_totalWords, scrollWordCN, ComponentUtil.RIGHT_CENTER,11);
		thisFrame.add(scrollWordCN);
		
		lbl_process = new JLabel("100 / 100");
//		lbl_process.setBorder(new LineBorder(Color.red));
		ComponentUtil.setBounds(80, 20, scrollWordCN, lbl_process, ComponentUtil.RIGHT_CENTER,8);
		thisFrame.add(lbl_process);
	}
	
	public void mouseDragged(MouseEvent me) {
		thisFrame.setLocation(thisFrame.getX()+ (me.getX()-(int)movePoint.getX()), thisFrame.getY() + (me.getY()-(int)movePoint.getY()));
	}
	public void mouseMoved(MouseEvent arg0) {
		movePoint.setLocation(arg0.getX(), arg0.getY());
	}
	
	private void saveUserInput(){
		if(currIndex < 0){
			return;
		}
		currWord = dataList.get(currIndex);
		currWord.setFldErrorfag(txt_wordEN.handlerResult());
		txt_wordEN.getWordBuffer()[currIndex] = txt_wordEN.getText();
	}
	
	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();
	}
	
	private void commit() {
		String result = service.getResult(dataList);
		JOptionPane.showMessageDialog(null,result ,"测试完成" , JOptionPane.INFORMATION_MESSAGE); 
		parentFrame.search();
		parentFrame.setVisible(true);
		thisFrame.dispose();	
		detalFrame.dispose();
	}

	private void detailModel() {
		detalFrame.jcb_miniModel.setSelected(false);
		detalFrame.currIndex = thisFrame.currIndex;
		detalFrame.commitFlag = thisFrame.commitFlag;
		detalFrame.refreshUI();
		detalFrame.setVisible(true);
		thisFrame.setVisible(false);
	}
	@SuppressWarnings("restriction")
	class MiniMouseListener implements MouseListener {
		WordMiniTestingScreen thisFrame;

		public MiniMouseListener(WordMiniTestingScreen thisFrame) {
			this.thisFrame = thisFrame;
		}

		public void mouseClicked(MouseEvent e) {
			String actionCommand = "";
			if (e.getComponent() instanceof JLabel) {
				actionCommand = ((JLabel) e.getComponent()).getText();
			} else if (e.getComponent() instanceof JCheckBox) {
				actionCommand = ((JCheckBox) e.getComponent()).getText();
			} else {
				System.out.println("no jlabel source");
				return;
			}
			if (actionCommand.equals("<<")) {
				prevWord();
			} else if (actionCommand.equals(">>")) {
				nextWord();
			} else if (actionCommand.equals("C")) {
				saveUserInput();
				refreshUI();
				commit();
			} else if (actionCommand.equals("E")) {
				thisFrame.exitScreen();
				detalFrame.dispose();
			} else if (actionCommand.equals("D")) {
				detailModel();
			} else if (actionCommand.equalsIgnoreCase("F")) {
				if(jcb_forgetTag.isSelected()){
					currWord.setFldForgettag("1");
				}else{
					currWord.setFldForgettag("0");
				}
			}
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			if (e.getComponent() instanceof Component) {
				com.sun.awt.AWTUtilities.setWindowOpacity(thisFrame, 0.8f);
			}
			if (e.getComponent() instanceof JLabel) {
				e.getComponent().setForeground(Color.PINK);
			}
		}

		@Override
		public void mouseExited(MouseEvent e) {
			boolean isOutside = outsideOfFrame(thisFrame, e.getXOnScreen(), e.getYOnScreen());
			if (isOutside) {
				com.sun.awt.AWTUtilities.setWindowOpacity(thisFrame, thisFrame.initOpac);
			}
			if (e.getComponent() instanceof JLabel) {
				e.getComponent().setForeground(Color.BLACK);
			}
		}

		private boolean outsideOfFrame(JFrame frame, int pointX, int pointY) {
			int localX = (int) frame.getLocation().getX();
			int localY = (int) frame.getLocation().getY();
			int width = frame.getWidth();
			int height = frame.getHeight();
			if (pointX <= localX || pointX >= (localX + width) || pointY <= localY || pointY >= (localY + height)) {
				return true;
			}
			return false;
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值