trackeditor: add check for duplicate object names

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@9122 30fe4595-0a0c-4342-8851-515496e4dcbd
This commit is contained in:
iobyte 2023-08-21 17:14:52 +00:00
parent 140edbd52a
commit 44e30e3459
3 changed files with 140 additions and 103 deletions

View File

@ -22,43 +22,44 @@ import utils.circuit.TrackObject;
public class TrackObjectDialog extends JDialog
{
private EditorFrame editorFrame = null;
private GraphicObject graphicObject = null;
private ObjShapeObject objectShape = null;
private ObjectData objectData = null;
private GraphicObjectData graphicObjectData = null;
private Vector<TrackObject> objects = null;
private boolean ignoreActions = true;
private boolean changed = false;
private boolean isGraphicObject = false;
private EditorFrame editorFrame = null;
private GraphicObject graphicObject = null;
private ObjShapeObject objectShape = null;
private ObjectData objectData = null;
private GraphicObjectData graphicObjectDatum = null;
private Vector<GraphicObjectData> graphicObjectData = null;
private Vector<TrackObject> trackObjects = null;
private boolean ignoreActions = true;
private boolean changed = false;
private boolean isGraphicObject = false;
private int rgb = 0;
private int rgb = 0;
private JCheckBox defaultCheckBox = new JCheckBox();
private JCheckBox defaultCheckBox = new JCheckBox();
private JLabel nameLabel = new JLabel();
private JTextField nameTextField = new JTextField();
private JLabel nameLabel = new JLabel();
private JTextField nameTextField = new JTextField();
private JLabel objectLabel = new JLabel();
private JComboBox<String> objectComboBox = null;
private JLabel objectLabel = new JLabel();
private JComboBox<String> objectComboBox = null;
private JLabel colorLabel = new JLabel();
private JTextField colorTextField = new JTextField();
private JLabel colorLabel = new JLabel();
private JTextField colorTextField = new JTextField();
private JLabel trackLocationLabel = new JLabel();
private JTextField trackLocationTextField = new JTextField();
private JLabel trackLocationLabel = new JLabel();
private JTextField trackLocationTextField = new JTextField();
private JLabel imageLocationLabel = new JLabel();
private JTextField imageLocationTextField = new JTextField();
private JLabel imageLocationLabel = new JLabel();
private JTextField imageLocationTextField = new JTextField();
private JLabel orientationLabel = new JLabel();
private JTextField orientationTextField = new JTextField();
private JLabel orientationLabel = new JLabel();
private JTextField orientationTextField = new JTextField();
private JLabel heightLabel = new JLabel();
private JTextField heightTextField = new JTextField();
private JLabel heightLabel = new JLabel();
private JTextField heightTextField = new JTextField();
private JButton applyButton = new JButton();
private JButton cancelButton = new JButton();
private JButton applyButton = new JButton();
private JButton cancelButton = new JButton();
public TrackObjectDialog(EditorFrame editorFrame, ObjShapeObject objectShape, int x, int y)
{
@ -113,11 +114,12 @@ public class TrackObjectDialog extends JDialog
initialize(0, 0);
}
public TrackObjectDialog(EditorFrame editorFrame, boolean all, GraphicObjectData graphicObjectData)
public TrackObjectDialog(EditorFrame editorFrame, boolean all, GraphicObjectData graphicObjectDatum, Vector<GraphicObjectData> graphicObjectData)
{
super();
this.graphicObjectData = graphicObjectData;
setTitle("Edit Object");
this.graphicObjectDatum = graphicObjectDatum;
this.graphicObjectData = graphicObjectData;
setTitle("Edit Object");
this.editorFrame = editorFrame;
initialize(0, 0);
}
@ -134,9 +136,9 @@ public class TrackObjectDialog extends JDialog
return objectShape.getRGB();
}
if (graphicObjectData != null)
if (graphicObjectDatum != null)
{
return graphicObjectData.color;
return graphicObjectDatum.color;
}
return objectData.color;
@ -149,9 +151,9 @@ public class TrackObjectDialog extends JDialog
return;
}
if (graphicObjectData != null)
if (graphicObjectDatum != null)
{
graphicObjectData.color = rgb;
graphicObjectDatum.color = rgb;
return;
}
@ -165,9 +167,9 @@ public class TrackObjectDialog extends JDialog
return objectShape.getName();
}
if (graphicObjectData != null)
if (graphicObjectDatum != null)
{
return graphicObjectData.name;
return graphicObjectDatum.name;
}
return objectData.name;
@ -180,9 +182,9 @@ public class TrackObjectDialog extends JDialog
return;
}
if (graphicObjectData != null)
if (graphicObjectDatum != null)
{
graphicObjectData.name = name;
graphicObjectDatum.name = name;
return;
}
@ -196,9 +198,9 @@ public class TrackObjectDialog extends JDialog
return objectShape.getTrackLocation();
}
if (graphicObjectData != null)
if (graphicObjectDatum != null)
{
return new Point2D.Double(graphicObjectData.trackX, graphicObjectData.trackY);
return new Point2D.Double(graphicObjectDatum.trackX, graphicObjectDatum.trackY);
}
return new Point2D.Double(objectData.trackX, objectData.trackY);
@ -230,9 +232,12 @@ public class TrackObjectDialog extends JDialog
{
return graphicObject.getOrientation();
}
else if (graphicObjectData != null)
{
return graphicObjectData.orientation;
else if (graphicObjectDatum != null)
{
if (graphicObjectDatum.orientation != null)
{
return graphicObjectDatum.orientation;
}
}
return Double.NaN;
@ -244,9 +249,12 @@ public class TrackObjectDialog extends JDialog
{
return graphicObject.getHeight();
}
else if (graphicObjectData != null)
{
return graphicObjectData.height;
else if (graphicObjectDatum != null)
{
if (graphicObjectDatum.height != null)
{
return graphicObjectDatum.height;
}
}
return Double.NaN;
@ -254,12 +262,12 @@ public class TrackObjectDialog extends JDialog
private void initialize(int x, int y)
{
isGraphicObject = (objectShape != null && objectShape.getType().equals("graphic object")) || graphicObjectData != null;
isGraphicObject = (objectShape != null && objectShape.getType().equals("graphic object")) || graphicObjectDatum != null;
setLayout(null);
setSize(320, 279);
setResizable(false);
if (objectData == null && graphicObjectData == null)
if (objectData == null && graphicObjectDatum == null)
{
setLocation(x, y);
}
@ -291,11 +299,11 @@ public class TrackObjectDialog extends JDialog
});
// search for color in track objects first
objects = editorFrame.getTrackData().getTrackObjects();
trackObjects = editorFrame.getTrackData().getTrackObjects();
int objectIndex = -1;
for (int i = 0; i < objects.size(); i++)
for (int i = 0; i < trackObjects.size(); i++)
{
if (objects.get(i).getColor() == getRGB())
if (trackObjects.get(i).getColor() == getRGB())
{
defaultCheckBox.setSelected(false);
objectIndex = i;
@ -306,10 +314,10 @@ public class TrackObjectDialog extends JDialog
// search in default objects if not found
if (objectIndex == -1)
{
objects = editorFrame.getDefaultObjects();
for (int i = 0; i < objects.size(); i++)
trackObjects = editorFrame.getDefaultObjects();
for (int i = 0; i < trackObjects.size(); i++)
{
if (objects.get(i).getColor() == getRGB())
if (trackObjects.get(i).getColor() == getRGB())
{
defaultCheckBox.setSelected(true);
objectIndex = i;
@ -324,9 +332,9 @@ public class TrackObjectDialog extends JDialog
defaultCheckBox.setSelected(true);
}
for (int i = 0; i < objects.size(); i++)
for (int i = 0; i < trackObjects.size(); i++)
{
objectComboBox.addItem(objects.get(i).getName());
objectComboBox.addItem(trackObjects.get(i).getName());
}
if (isGraphicObject)
@ -453,9 +461,9 @@ public class TrackObjectDialog extends JDialog
trackLocationTextField.setText(String.format("%.3f", getTrackLocation().x) + ", " + String.format("%.3f", getTrackLocation().y));
setObjectName(objects.get(objectComboBox.getSelectedIndex()).getName());
setObjectName(trackObjects.get(objectComboBox.getSelectedIndex()).getName());
rgb = objects.get(objectComboBox.getSelectedIndex()).getColor();
rgb = trackObjects.get(objectComboBox.getSelectedIndex()).getColor();
Color backgroundColor = new Color((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff);
colorTextField.setBackground(backgroundColor);
@ -477,22 +485,22 @@ public class TrackObjectDialog extends JDialog
if (defaultCheckBox.isSelected())
{
objects = editorFrame.getDefaultObjects();
trackObjects = editorFrame.getDefaultObjects();
}
else
{
objects = editorFrame.getTrackData().getObjects();
trackObjects = editorFrame.getTrackData().getObjects();
}
ignoreActions = true;
objectComboBox.removeAllItems();
int objectIndex = -1;
for (int i = 0; i < objects.size(); i++)
for (int i = 0; i < trackObjects.size(); i++)
{
objectComboBox.addItem(objects.get(i).getName());
objectComboBox.addItem(trackObjects.get(i).getName());
if (objects.get(i).getColor() == getRGB())
if (trackObjects.get(i).getColor() == getRGB())
{
objectIndex = i;
}
@ -562,26 +570,33 @@ public class TrackObjectDialog extends JDialog
}
}
}
else if (graphicObjectData != null)
else if (graphicObjectDatum != null)
{
String newName = nameTextField.getText();
if (!newName.equals(graphicObjectData.name))
for (GraphicObjectData objectData : graphicObjectData)
{
// TODO: check for duplicate name
graphicObjectData.name = newName;
if (objectData != graphicObjectDatum)
{
if (objectData.name.equals(newName))
{
JOptionPane.showMessageDialog(this, "Object name already used!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
graphicObjectDatum.name = newName;
String orientationText = orientationTextField.getText();
if (orientationText == null || orientationText.isEmpty())
{
graphicObjectData.orientation = null;
graphicObjectDatum.orientation = null;
}
else
{
try
{
graphicObjectData.orientation = Double.parseDouble(orientationText);
graphicObjectDatum.orientation = Double.parseDouble(orientationText);
}
catch (NumberFormatException e)
{
@ -591,13 +606,13 @@ public class TrackObjectDialog extends JDialog
String heightText = heightTextField.getText();
if (heightText == null || heightText.isEmpty())
{
graphicObjectData.height = null;
graphicObjectDatum.height = null;
}
else
{
try
{
graphicObjectData.height = Double.parseDouble(heightText);
graphicObjectDatum.height = Double.parseDouble(heightText);
}
catch (NumberFormatException e)
{

View File

@ -26,6 +26,7 @@ import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import gui.EditorFrame;
@ -34,6 +35,15 @@ import utils.circuit.GraphicObject;
public class GraphicObjectProperties extends PropertyPanel
{
private final int ROW_INDEX = 0;
private final int NAME_INDEX = 1;
private final int OBJECT_INDEX = 2;
private final int COLOR_INDEX = 3;
private final int TRACK_X_INDEX = 4;
private final int TRACK_Y_INDEX = 5;
private final int ORIENTATION_INDEX = 6;
private final int HEIGHT_INDEX = 7;
private GraphicObjectTablePanel graphicObjectTablePanel = null;
private Vector<GraphicObject> graphicObjects = null;
private Vector<GraphicObjectData> data = new Vector<GraphicObjectData>();
@ -80,7 +90,7 @@ public class GraphicObjectProperties extends PropertyPanel
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
JLabel label = (JLabel)c;
if (column == 3)
if (column == COLOR_INDEX)
{
int rgb = Integer.decode(value.toString());
Color color = new Color((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff);
@ -96,7 +106,7 @@ public class GraphicObjectProperties extends PropertyPanel
}
else
{
if (column == 1)
if (column == NAME_INDEX)
{
// TODO this needs to change the other cells if there was a conflict that is now fixed
label.setBackground(Color.WHITE);
@ -176,7 +186,7 @@ public class GraphicObjectProperties extends PropertyPanel
public boolean isCellEditable(int row, int columnIndex)
{
if (columnIndex == 0 || columnIndex == 3)
if (columnIndex == ROW_INDEX || columnIndex == COLOR_INDEX)
{
return false;
}
@ -189,25 +199,25 @@ public class GraphicObjectProperties extends PropertyPanel
switch (columnIndex)
{
case 0:
case ROW_INDEX:
return rowIndex + 1;
case 1:
case NAME_INDEX:
return datum.name;
case 2:
case OBJECT_INDEX:
return getEditorFrame().getObjectColorName(datum.color);
case 3:
case COLOR_INDEX:
return String.format("0x%06X", datum.color);
case 4:
case TRACK_X_INDEX:
return datum.trackX;
case 5:
case TRACK_Y_INDEX:
return datum.trackY;
case 6:
case ORIENTATION_INDEX:
if (datum.orientation != null && !Double.isNaN(datum.orientation))
{
return datum.orientation;
}
return null;
case 7:
case HEIGHT_INDEX:
if (datum.height != null && !Double.isNaN(datum.height))
{
return datum.height;
@ -223,15 +233,11 @@ public class GraphicObjectProperties extends PropertyPanel
switch (columnIndex)
{
case 1:
case NAME_INDEX:
datum.name = (String) value;
fireTableCellUpdated(rowIndex, columnIndex);
break;
case 2:
{
datum.name = (String) value;
fireTableCellUpdated(rowIndex, columnIndex);
case OBJECT_INDEX:
if (value.equals("Unknown"))
{
datum.color = graphicObjects.get(rowIndex).getColor();
@ -240,22 +246,27 @@ public class GraphicObjectProperties extends PropertyPanel
{
datum.color = getEditorFrame().getObjectColor(datum.name);
}
fireTableCellUpdated(rowIndex, columnIndex + 1);
fireTableCellUpdated(rowIndex, COLOR_INDEX);
fireTableCellUpdated(rowIndex, columnIndex);
break;
}
case 4:
case COLOR_INDEX:
datum.color = (Integer) value;
fireTableCellUpdated(rowIndex, columnIndex);
fireTableCellUpdated(rowIndex, OBJECT_INDEX);
break;
case TRACK_X_INDEX:
datum.trackX = (Double) value;
fireTableCellUpdated(rowIndex, columnIndex);
break;
case 5:
case TRACK_Y_INDEX:
datum.trackY = (Double) value;
fireTableCellUpdated(rowIndex, columnIndex);
break;
case 6:
case ORIENTATION_INDEX:
datum.orientation = (Double) value;
fireTableCellUpdated(rowIndex, columnIndex);
break;
case 7:
case HEIGHT_INDEX:
datum.height = (Double) value;
fireTableCellUpdated(rowIndex, columnIndex);
break;
@ -309,8 +320,8 @@ public class GraphicObjectProperties extends PropertyPanel
model = new GraphicObjectTableModel(graphicObjects);
table = new JTable(model);
scrollPane = new JScrollPane(table);
table.getColumnModel().getColumn(0).setPreferredWidth(35);
table.getColumnModel().getColumn(2).setPreferredWidth(120);
table.getColumnModel().getColumn(ROW_INDEX).setPreferredWidth(35);
table.getColumnModel().getColumn(OBJECT_INDEX).setPreferredWidth(120);
table.setDefaultRenderer(Integer.class, new ColorRenderer());
table.setDefaultRenderer(String.class, new ColorRenderer());
table.setAutoCreateRowSorter(true);
@ -318,7 +329,7 @@ public class GraphicObjectProperties extends PropertyPanel
Set<String> names = getEditorFrame().getObjectColorNames();
setUpNameColumn(table, table.getColumnModel().getColumn(2), names);
setUpNameColumn(table, table.getColumnModel().getColumn(OBJECT_INDEX), names);
table.addMouseListener(new MouseAdapter()
{
@ -352,6 +363,17 @@ public class GraphicObjectProperties extends PropertyPanel
public JPopupMenu createPopupMenu(GraphicObjectTablePanel panel)
{
if (panel.table.isEditing())
{
int row = panel.table.getEditingRow();
int col = panel.table.getEditingColumn();
if (row < panel.table.getRowCount())
{
TableCellEditor cellEditor = panel.table.getCellEditor(row, col);
cellEditor.stopCellEditing();
}
}
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem editItem = new JMenuItem("Edit Object");
JMenuItem deleteItem = new JMenuItem("Delete Object");
@ -366,17 +388,17 @@ public class GraphicObjectProperties extends PropertyPanel
{
GraphicObjectData datum = data.elementAt(panel.table.convertRowIndexToModel(row));
TrackObjectDialog editObjectDialog = new TrackObjectDialog(getEditorFrame(), false, datum);
TrackObjectDialog editObjectDialog = new TrackObjectDialog(getEditorFrame(), false, datum, data);
editObjectDialog.setModal(true);
editObjectDialog.setVisible(true);
if (editObjectDialog.isChanged())
{
panel.model.setValueAt(datum.name, row, 1);
panel.model.setValueAt(datum.color, row, 3);
panel.model.setValueAt(datum.orientation, row, 6);
panel.model.setValueAt(datum.height, row, 7);
panel.model.setValueAt(datum.name, row, NAME_INDEX);
panel.model.setValueAt(datum.color, row, COLOR_INDEX);
panel.model.setValueAt(datum.orientation, row, ORIENTATION_INDEX);
panel.model.setValueAt(datum.height, row, HEIGHT_INDEX);
getEditorFrame().documentIsModified = true;
}

View File

@ -34,7 +34,7 @@ public class Properties
private static Properties instance = new Properties();
private Vector<ActionListener> propertiesListeners = new Vector<ActionListener>();
public final String title = "sd2-trackeditor";
public final String version = "1.3.17";
public final String version = "1.3.18";
private String path;
private double imageScale = 1;