Fixed actionlisteners

This commit is contained in:
2015-03-17 13:27:21 +01:00
parent 8bf08d4e1d
commit c21af8db1a
14 changed files with 529 additions and 371 deletions
+114 -90
View File
@@ -21,21 +21,22 @@ import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class PropertiesPanel extends JPanel {
public class PropertiesPanel extends JPanel
{
DrawObject selectedObject = null;
Panel p = null;
Dimension spacerDimension = new Dimension(200,30);
Dimension spacerDimension = new Dimension(200, 30);
JLabel nameLabel;
JTextField locationXField;
JTextField locationYField;
JTextField scaleField;
JTextField rotationField;
PropertiesPanel() {
PropertiesPanel()
{
super();
BoxLayout box = new BoxLayout(this, BoxLayout.PAGE_AXIS);
setLayout(box);
@@ -52,8 +53,8 @@ public class PropertiesPanel extends JPanel {
propPanel.setMaximumSize(new Dimension(200, 60));
propPanel.add(propLabel);
add(propPanel);
//NAME
// NAME
JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
nameLabel = new JLabel("No Selection");
nameLabel.setFont(new Font("Segoe UI", Font.PLAIN, 20));
@@ -61,11 +62,11 @@ public class PropertiesPanel extends JPanel {
namePanel.setMaximumSize(new Dimension(200, 40));
namePanel.add(nameLabel);
add(namePanel);
// SPACER
add(Box.createRigidArea(spacerDimension));
//BUTTONS
// BUTTONS
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel buttonLabel = new JLabel("Actions");
buttonLabel.setFont(new Font("Segoe UI", Font.ITALIC, 20));
@@ -73,13 +74,15 @@ public class PropertiesPanel extends JPanel {
buttonPanel.setMaximumSize(new Dimension(200, 40));
buttonPanel.add(buttonLabel);
add(buttonPanel);
//DELETE
// DELETE
JPanel deletePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton deleteButton = new JButton("Delete");
deleteButton.setFont(new Font("Segoe UI", Font.PLAIN, 20));
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
p.removeObject(selectedObject);
}
});
@@ -87,7 +90,7 @@ public class PropertiesPanel extends JPanel {
deletePanel.setMaximumSize(new Dimension(200, 50));
deletePanel.add(deleteButton);
add(deletePanel);
// SPACER
add(Box.createRigidArea(spacerDimension));
@@ -109,26 +112,29 @@ public class PropertiesPanel extends JPanel {
locationXField = new JTextField();
locationXField.setPreferredSize(new Dimension(150, 25));
{
locationXField.addKeyListener(new KeyListener() {
locationXField.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent e) {
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e) {
try {
public void keyReleased(KeyEvent e)
{
try
{
locationXField.setBackground(Color.WHITE);
double xpos = Double.parseDouble(locationXField
.getText());
selectedObject.setPosition(new Point2D.Double(xpos,
selectedObject.getPosition().getY()));
double xpos = Double.parseDouble(locationXField.getText());
selectedObject.setPosition(new Point2D.Double(xpos, selectedObject.getPosition().getY()));
p.update();
} catch (NumberFormatException nfe) {
}
catch (NumberFormatException nfe)
{
locationXField.setBackground(Color.RED);
}
}
public void keyPressed(KeyEvent e) {
}
public void keyPressed(KeyEvent e)
{}
});
}
locationXPanel.add(locationXField);
@@ -143,29 +149,32 @@ public class PropertiesPanel extends JPanel {
locationYField = new JTextField();
locationYField.setPreferredSize(new Dimension(150, 25));
{
locationYField.addKeyListener(new KeyListener() {
locationYField.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent e) {
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e) {
try {
public void keyReleased(KeyEvent e)
{
try
{
locationYField.setBackground(Color.WHITE);
double ypos = Double.parseDouble(locationYField
.getText());
selectedObject.setPosition(new Point2D.Double(
selectedObject.getPosition().getX(), ypos));
double ypos = Double.parseDouble(locationYField.getText());
selectedObject.setPosition(new Point2D.Double(selectedObject.getPosition().getX(), ypos));
;
p.update();
} catch (NumberFormatException nfe) {
}
catch (NumberFormatException nfe)
{
locationYField.setBackground(Color.RED);
}
}
public void keyPressed(KeyEvent e) {
}
public void keyPressed(KeyEvent e)
{}
});
}
locationYPanel.add(locationYField);
@@ -189,25 +198,29 @@ public class PropertiesPanel extends JPanel {
scaleField = new JTextField();
scaleField.setPreferredSize(new Dimension(150, 25));
{
scaleField.addKeyListener(new KeyListener() {
scaleField.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent e) {
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e) {
try {
public void keyReleased(KeyEvent e)
{
try
{
scaleField.setBackground(Color.WHITE);
double scale = Double.parseDouble(scaleField
.getText());
double scale = Double.parseDouble(scaleField.getText());
selectedObject.setScale(scale);
p.update();
} catch (NumberFormatException nfe) {
}
catch (NumberFormatException nfe)
{
scaleField.setBackground(Color.RED);
}
}
public void keyPressed(KeyEvent e) {
}
public void keyPressed(KeyEvent e)
{}
});
}
scalePanel2.add(scaleField);
@@ -231,26 +244,30 @@ public class PropertiesPanel extends JPanel {
rotationField = new JTextField();
rotationField.setPreferredSize(new Dimension(150, 25));
{
rotationField.addKeyListener(new KeyListener() {
rotationField.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent e) {
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e) {
try {
public void keyReleased(KeyEvent e)
{
try
{
rotationField.setBackground(Color.WHITE);
int rotation = Integer.parseInt(rotationField
.getText()) % 360;
int rotation = Integer.parseInt(rotationField.getText()) % 360;
rotationField.setText(rotation + "");
selectedObject.setRotation(rotation);
p.update();
} catch (NumberFormatException nfe) {
}
catch (NumberFormatException nfe)
{
rotationField.setBackground(Color.RED);
}
}
public void keyPressed(KeyEvent e) {
}
public void keyPressed(KeyEvent e)
{}
});
}
rotationPanel2.add(rotationField);
@@ -259,76 +276,83 @@ public class PropertiesPanel extends JPanel {
// SPACER
add(Box.createRigidArea(spacerDimension));
// EXAMPLE
JPanel examplePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel exampleLabel = new JLabel("Example");
exampleLabel.setFont(new Font("Segoe UI", Font.ITALIC, 20));
examplePanel.setPreferredSize(new Dimension(200, 100));
examplePanel.setMaximumSize(new Dimension(200, 100));
examplePanel.add(exampleLabel);
add(examplePanel);
// AGENDA
JPanel agendaPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel agendaLabel = new JLabel("Agenda");
agendaLabel.setFont(new Font("Segoe UI", Font.ITALIC, 20));
agendaPanel.setPreferredSize(new Dimension(200, 100));
agendaPanel.setMaximumSize(new Dimension(200, 100));
agendaPanel.add(agendaLabel);
add(agendaPanel);
enableComponents(this, false);
}
public void setSelected(DrawObject drOb) {
public void setSelected(DrawObject drOb)
{
selectedObject = drOb;
fillFields();
enableComponents(this, true);
}
public void clearSelected() {
public void clearSelected()
{
enableComponents(this, false);
clearFields(this);
selectedObject = null;
}
public void update() {
public void update()
{
fillFields();
}
private void enableComponents(Container container, boolean enable) {
private void enableComponents(Container container, boolean enable)
{
Component[] components = container.getComponents();
for (Component component : components) {
for (Component component : components)
{
component.setEnabled(enable);
if (component instanceof Container) {
if (component instanceof Container)
{
enableComponents((Container) component, enable);
}
}
}
private void clearFields(Container container) {
private void clearFields(Container container)
{
Component[] components = container.getComponents();
for (Component component : components) {
if (component instanceof JTextField) {
for (Component component : components)
{
if (component instanceof JTextField)
{
((JTextField) component).setText("");
((JTextField) component).setBackground(Color.WHITE);
}
if (component instanceof Container) {
if (component instanceof Container)
{
clearFields((Container) component);
}
}
nameLabel.setText("No Selection");
}
private void fillFields() {
if (selectedObject != null) {
private void fillFields()
{
if (selectedObject != null)
{
nameLabel.setText(selectedObject.getName());
locationXField.setText(Math.round(selectedObject.getPosition()
.getX()) + "");
locationYField.setText(Math.round(selectedObject.getPosition()
.getY()) + "");
scaleField
.setText((double) Math.round(selectedObject.getScale() * 10)
/ 10 + "");
rotationField
.setText(Math.round(selectedObject.getRotation() % 360)
+ "");
locationXField.setText(Math.round(selectedObject.getPosition().getX()) + "");
locationYField.setText(Math.round(selectedObject.getPosition().getY()) + "");
scaleField.setText((double) Math.round(selectedObject.getScale() * 10) / 10 + "");
rotationField.setText(Math.round(selectedObject.getRotation() % 360) + "");
}
}
public void setPanel(Panel p) {
public void setPanel(Panel p)
{
this.p = p;
}
}