Skip to content

Instantly share code, notes, and snippets.

@moomoohk
Last active December 12, 2015 04:28
Show Gist options
  • Save moomoohk/4714717 to your computer and use it in GitHub Desktop.
Save moomoohk/4714717 to your computer and use it in GitHub Desktop.
package psi;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class StringThing extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public static String[] signLines;
public static String[] configLines;
public static String wildcard = "\'*\'";
public static void main(String[] args)
{
StringThing configLines = new StringThing("Enter config lines", "", "[hi" + wildcard + "]", wildcard, wildcard);
configLines.setVisible(true);
}
public StringThing(boolean storeDefaultText)
{
this("", "", "", "", "", storeDefaultText);
}
public StringThing(String title, boolean storeDefaultText)
{
this(title, "", "", "", "", storeDefaultText);
}
public StringThing(String title)
{
this(title, "", "", "", "", true);
}
public StringThing()
{
this("", "", "", "", "", true);
}
public StringThing(String title, String line1, String line2, String line3, String line4)
{
this(title, line1, line2, line3, line4, true);
}
public StringThing(String title, String line1, String line2, String line3, String line4, boolean storeDefaultText)
{
super();
int width = 230, height = 230;
setBounds(width, height, width, height);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JLabel header = new JLabel(title, null, JLabel.CENTER);
header.setBounds(10, 5, width - 10, 20);
add(header);
final AwesomeTextField field1 = new AwesomeTextField(line1);
final AwesomeTextField field2 = new AwesomeTextField(line2);
final AwesomeTextField field3 = new AwesomeTextField(line3);
final AwesomeTextField field4 = new AwesomeTextField(line4);
field1.storeDefaultText(storeDefaultText);
field2.storeDefaultText(storeDefaultText);
field3.storeDefaultText(storeDefaultText);
field4.storeDefaultText(storeDefaultText);
JLabel label1 = new JLabel("Line 1");
JLabel label2 = new JLabel("Line 2");
JLabel label3 = new JLabel("Line 3");
JLabel label4 = new JLabel("Line 4");
final JButton done = new JButton("Done");
int fieldX = (width / 2) - 40;
int fieldWidth = 140;
int labelX = 20;
int labelWidth = 50;
field1.setBounds(fieldX, 40, fieldWidth, 25);
field2.setBounds(fieldX, 70, fieldWidth, 25);
field3.setBounds(fieldX, 100, fieldWidth, 25);
field4.setBounds(fieldX, 130, fieldWidth, 25);
label1.setBounds(labelX, 40, labelWidth, 25);
label2.setBounds(labelX, 70, labelWidth, 25);
label3.setBounds(labelX, 100, labelWidth, 25);
label4.setBounds(labelX, 130, labelWidth, 25);
int buttonWidth = 70;
done.setBounds((width / 2) - (buttonWidth / 2), 160, buttonWidth, 30);
done.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent paramActionEvent)
{
setVisible(false);
if (configLines == null)
{
configLines = new String[4];
configLines[0] = field1.getText();
configLines[1] = field2.getText();
configLines[2] = field3.getText();
configLines[3] = field4.getText();
StringThing signLines = new StringThing("Enter sign lines", field1.getText(), field2.getText(), field3.getText(), field4.getText(), false);
signLines.setVisible(true);
dispose();
}
else
{
signLines = new String[4];
signLines[0] = field1.getText();
signLines[1] = field2.getText();
signLines[2] = field3.getText();
signLines[3] = field4.getText();
boolean success = false;
JFrame f = new JFrame();
int width = 100, height = 100;
JLabel label = new JLabel();
int labelWidth = 110, labelHeight = 30;
try
{
success = compare(signLines, configLines);
label = new JLabel(("" + success).toUpperCase());
label.setBounds((width / 2) - (labelWidth / 2) + 15, 20, labelWidth, labelHeight);
}
catch (IllegalStateException e)
{
e.printStackTrace();
width = 900;
height = 100;
labelWidth = 870;
labelHeight = 30;
label = new JLabel("Can't have more than 1 wildcard in the same line!");
label.setBounds((width / 2) - (labelWidth / 2) + 15, 20, labelWidth, labelHeight);
}
label.setFont(new Font("Dialog", Font.PLAIN, 36));
f.setBounds(width, height, width, height);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);
f.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent paramKeyEvent)
{
}
public void keyReleased(KeyEvent paramKeyEvent)
{
}
public void keyPressed(KeyEvent paramKeyEvent)
{
if (paramKeyEvent.getKeyCode() == 27)
System.exit(0);
}
});
label.setFont(new Font("Dialog", Font.PLAIN, 36));
f.add(label);
f.setVisible(true);
}
}
});
KeyListener entrEsc = new KeyListener()
{
public void keyTyped(KeyEvent paramKeyEvent)
{
}
public void keyReleased(KeyEvent paramKeyEvent)
{
}
public void keyPressed(KeyEvent paramKeyEvent)
{
if (paramKeyEvent.getKeyCode() == 10)
done.doClick();
if (paramKeyEvent.getKeyCode() == 27)
System.exit(0);
}
};
field1.addKeyListener(entrEsc);
field2.addKeyListener(entrEsc);
field3.addKeyListener(entrEsc);
field4.addKeyListener(entrEsc);
done.addKeyListener(entrEsc);
add(field1);
add(field2);
add(field3);
add(field4);
add(label1);
add(label2);
add(label3);
add(label4);
add(done);
}
public static boolean compare(String[] sign, String[] config) throws IllegalStateException
{
boolean success = true;
for (int i = 0; i < sign.length; i++)
{
if (config[i].length() == 0 && sign[i].length() != 0)
success = false;
if (config[i].contains(wildcard) && config[i].substring(config[i].indexOf(wildcard) + wildcard.length(), config[i].length()).contains(wildcard))
throw new IllegalStateException("Can't have more than one wildcard in the same line!");
if (config[i].contains(wildcard) && config[i].length() > wildcard.length())
{
int place = config[i].indexOf(wildcard);
try
{
if (!config[i].substring(0, place).equals(sign[i].substring(0, place)) || !config[i].substring(place + wildcard.length(), config[i].length()).equals(sign[i].substring(sign[i].indexOf(config[i].substring(place + wildcard.length(), config[i].length())), sign[i].length())))
success = false;
}
catch (Exception e)
{
success = false;
}
}
if (!config[i].contains(wildcard) && config[i].length() > 0 && config[i].equals(sign[i]))
success = false;
if (success == false)
break;
}
return success;
}
public class AwesomeTextField extends JTextField
{
/**
*
*/
private static final long serialVersionUID = 1L;
private boolean storeDefaultText;
private String defaultText;
public AwesomeTextField()
{
this("");
}
public AwesomeTextField(final String defaultText)
{
super(defaultText);
storeDefaultText = true;
this.defaultText = defaultText;
setForeground(Color.gray);
addFocusListener(new FocusListener()
{
public void focusLost(FocusEvent paramFocusEvent)
{
if (getSuperText().equals(""))
{
setForeground(Color.gray);
setText(defaultText);
}
}
public void focusGained(FocusEvent paramFocusEvent)
{
if (!getForeground().equals(Color.black) && getSuperText().equals(defaultText))
{
setForeground(Color.black);
setText("");
}
}
});
}
public String getSuperText()
{
return super.getText();
}
public String getText()
{
if (super.getText().equals(defaultText))
if (storeDefaultText)
return defaultText;
else
return "";
return super.getText();
}
public void storeDefaultText(boolean f)
{
storeDefaultText = f;
}
public boolean storesDefaultText()
{
return storeDefaultText;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment