فهرست منبع

Created Label Hints and added one on the IterationsLabel.

Ludwig Tietze 6 سال پیش
والد
کامیت
0764ba7156
4فایلهای تغییر یافته به همراه101 افزوده شده و 3 حذف شده
  1. 2 0
      src/ui/view/GUI.java
  2. 74 0
      src/ui/view/LabelHint.java
  3. 24 3
      src/ui/view/TimePanel.java
  4. 1 0
      src/ui/view/Util.java

+ 2 - 0
src/ui/view/GUI.java

@@ -46,6 +46,8 @@ import java.util.stream.Collectors;
  */
 public class GUI implements CategoryListener {
 	
+	static final Color PALE_RED=new Color(255, 192, 192);
+	
 	/**
 	 * Menu on the Top containing File, Edit View Help etc
 	 */

+ 74 - 0
src/ui/view/LabelHint.java

@@ -0,0 +1,74 @@
+package ui.view;
+
+import javax.swing.JLabel;
+
+public class LabelHint implements Runnable {
+
+	private JLabel label;
+	private String original;
+	private int remainingTime, defaultTime;
+	private static final int DEFAULT_TIME=1000,//milliseconds
+							 DELAY=50;
+	private boolean running=false,
+					keepLength=true;
+	
+	public LabelHint(JLabel label){
+		this(label, DEFAULT_TIME);
+	}
+	
+	public LabelHint(JLabel label, int ms){
+		this.label=label;
+		original=label.getText();
+		this.defaultTime=ms;
+		setRemaining(ms);
+	}
+	
+	public void go(String hint){
+		//the keepLengthStuff should maybe be done by the caller
+		if(keepLength&&hint.length()<original.length()){
+			boolean right=true;
+			while(hint.length()<original.length()){
+				if(right)hint=hint+' ';
+				else hint=' '+hint;
+				right=!right;
+			}
+		}
+		label.setText(hint);
+		setRemaining(defaultTime);
+		if(!running){
+			running=true;
+			new Thread(this).start();
+		}
+	}
+	
+	public void go(String hint, int ms){
+		setRemaining(ms);
+		go(hint);
+	}
+	private void setRemaining(int ms) {
+		remainingTime=ms/DELAY;
+	}
+	
+	public void setKeepLength(boolean b){
+		keepLength=b;
+	}
+
+	@Override
+	public void run() {
+		while(running){
+			remainingTime--;
+			if(remainingTime==0)running=false;
+			try {
+				Thread.sleep(DELAY);
+			} catch (InterruptedException e) {}
+		}
+		//This is supposed to catch one-in-a-million
+		//cases of Thread anomalies.
+		if(remainingTime!=0)go(label.getText());
+		else label.setText(original);
+	}
+
+	public void reset() {
+		label.setText(original);
+	}
+}

+ 24 - 3
src/ui/view/TimePanel.java

@@ -1,6 +1,7 @@
 package ui.view;
 
 import ui.controller.Control;
+import ui.controller.SingletonControl;
 import ui.model.Model;
 
 import javax.swing.*;
@@ -39,6 +40,7 @@ public class TimePanel extends JPanel implements ActionListener{
 	private final JPanel timeBtnPanel = new JPanel();
 	private final JPanel iterationsPanel=new JPanel();
 	private final JPanel timePanel=new JPanel();
+	private LabelHint iterationsLblHint;
 	JSlider timeSlider = new JSlider() {
 		/**
 		 *
@@ -275,9 +277,29 @@ public class TimePanel extends JPanel implements ActionListener{
 		btnAndSpeedPanel.add(speedPanel, BorderLayout.CENTER);
 		iterationsPanel.setLayout(new GridLayout(3,1));
 		iterationsPanel.add(iterationsLabel, BorderLayout.NORTH);
+		iterationsLblHint=new LabelHint(iterationsLabel);
 		iterationsField=new JTextField(6);//Considering hundreds of thousands in an extreme case
 		iterationsField.setText(""+cont.getModel().getIterations());
 		iterationsField.addActionListener(this);
+		iterationsField.addCaretListener((e)->
+			{
+				try{
+					int a=Integer.parseInt(iterationsField.getText());
+					iterationsField.setBackground(Color.WHITE);//red stings
+					if(
+							a!=
+							SingletonControl
+								.getInstance()
+									.getControl()
+										.getModel()
+											.getIterations()
+					  )iterationsLblHint.go("Press ENTER");
+				}catch(NumberFormatException n){
+					iterationsLblHint.go("Invalid");
+					iterationsField.setBackground(GUI.PALE_RED);//red stings
+				}
+			}
+		);
 		iterationsPanel.add(iterationsField);
 		iterationsPanel.add(new JLabel(), BorderLayout.SOUTH);
 		timePanel.setLayout(new BorderLayout());
@@ -334,6 +356,7 @@ public class TimePanel extends JPanel implements ActionListener{
 	public void actionPerformed(ActionEvent arg0) {//I dislike anon classes.
 		try{
 			int iterations=Integer.parseInt(iterationsField.getText());
+			iterationsLblHint.reset();
 			boolean resetField=true;
 			if(iterations<1)iterations=1;
 			else if(iterations>MAX_ITERATIONS)iterations=MAX_ITERATIONS;
@@ -345,8 +368,6 @@ public class TimePanel extends JPanel implements ActionListener{
 			timeSlider.setMajorTickSpacing((int)Math.ceil(((double)controller.getModel().getIterations())/20));
 			timeSlider.setMinorTickSpacing((int)Math.ceil(((double)controller.getModel().getIterations())/100));//Even though the final mark can't actually be reached.
 			parentGUI.updateIterations();//First one in the chain
-		}catch(NumberFormatException e){
-			
-		}
+		}catch(NumberFormatException e){}
 	}
 }

+ 1 - 0
src/ui/view/Util.java

@@ -10,6 +10,7 @@ import java.io.InputStream;
 import java.util.HashMap;
 
 import javax.imageio.ImageIO;
+import javax.swing.JLabel;
 
 public class Util {