Правки по работе DnD по передаче объектов.

This commit is contained in:
Programmist73 2022-11-21 23:56:37 +04:00
parent 9c340394fb
commit 781a54e437
3 changed files with 101 additions and 33 deletions

View File

@ -298,7 +298,7 @@
</grid> </grid>
</children> </children>
</grid> </grid>
<grid id="4ef93" binding="PanelObject" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="4ef93" binding="PanelObject" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<grid row="1" column="2" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <grid row="1" column="2" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -335,16 +335,6 @@
<text value="Доп. цвет"/> <text value="Доп. цвет"/>
</properties> </properties>
</component> </component>
<component id="da776" class="javax.swing.JLabel" binding="LabelTypeWindow">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="Форма"/>
</properties>
</component>
<grid id="f0d81" binding="PictureBoxPlane" layout-manager="BorderLayout" hgap="0" vgap="0"> <grid id="f0d81" binding="PictureBoxPlane" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints> <constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <grid row="1" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -353,6 +343,15 @@
<border type="none"/> <border type="none"/>
<children/> <children/>
</grid> </grid>
<hspacer id="8ca3b">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="5" height="-1"/>
<preferred-size width="5" height="-1"/>
<maximum-size width="5" height="-1"/>
</grid>
</constraints>
</hspacer>
</children> </children>
</grid> </grid>
<component id="d1cce" class="javax.swing.JButton" binding="ButtonAddObject"> <component id="d1cce" class="javax.swing.JButton" binding="ButtonAddObject">

View File

@ -35,7 +35,6 @@ public class FormPlaneConfig extends JFrame{
private JPanel GroubBoxSelectedCountWindow; private JPanel GroubBoxSelectedCountWindow;
private JSpinner NumericUpDownCountWindow; private JSpinner NumericUpDownCountWindow;
private JPanel MainPanel; private JPanel MainPanel;
private JLabel LabelTypeWindow;
private JLabel LabelSimpleWindow; private JLabel LabelSimpleWindow;
private JLabel LabelRectWindow; private JLabel LabelRectWindow;
private JLabel LabelTriangleWindow; private JLabel LabelTriangleWindow;
@ -47,7 +46,14 @@ public class FormPlaneConfig extends JFrame{
//событие //событие
Consumer<DrawingPlane> EventAddPlane; Consumer<DrawingPlane> EventAddPlane;
public void AddEvent(Consumer<DrawingPlane> ev){ //объект для контролирования работы DnD
Object dragObject;
//хранит в себе объект иллюминатор
Object enterObject;
public void AddEvent(Consumer<DrawingPlane> ev)
{
EventAddPlane = ev; EventAddPlane = ev;
} }
@ -86,7 +92,6 @@ public class FormPlaneConfig extends JFrame{
//рамки у Label //рамки у Label
LabelAddColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelAddColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelTypeWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelBaseColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelBaseColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelModifiedObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelModifiedObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelSimpleObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelSimpleObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
@ -123,7 +128,6 @@ public class FormPlaneConfig extends JFrame{
PictureBoxPlane.addMouseListener(defCursor); PictureBoxPlane.addMouseListener(defCursor);
LabelBaseColor.addMouseListener(defCursor); LabelBaseColor.addMouseListener(defCursor);
LabelAddColor.addMouseListener(defCursor); LabelAddColor.addMouseListener(defCursor);
LabelTypeWindow.addMouseListener(defCursor);
PanelBlack.addMouseListener(drag); PanelBlack.addMouseListener(drag);
PanelPurple.addMouseListener(drag); PanelPurple.addMouseListener(drag);
@ -140,12 +144,20 @@ public class FormPlaneConfig extends JFrame{
LabelRectWindow.addMouseListener(drag); LabelRectWindow.addMouseListener(drag);
LabelTriangleWindow.addMouseListener(drag); LabelTriangleWindow.addMouseListener(drag);
//передача объекта через событие
ButtonAddObject.addActionListener(e -> { ButtonAddObject.addActionListener(e -> {
EventAddPlane.accept(_plane); EventAddPlane.accept(_plane);
dispose(); dispose();
}); });
//лямбда-выражение для закрытия формы
ButtonCancel.addActionListener(e -> dispose()); ButtonCancel.addActionListener(e -> dispose());
AdditWindowDropObject(LabelSimpleWindow);
AdditWindowDropObject(LabelRectWindow);
AdditWindowDropObject(LabelTriangleWindow);
AdditWindowDropTarget(PictureBoxPlane);
} }
public void Drop(JComponent dropItem) public void Drop(JComponent dropItem)
@ -215,27 +227,84 @@ public class FormPlaneConfig extends JFrame{
revalidate(); revalidate();
} }
} }
else if(dropItem instanceof JLabel label && LabelTypeWindow.getMousePosition() != null && _plane!=null) }
void AdditWindowDropTarget(JComponent obj)
{
obj.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {super.mouseEntered(e);
Paddle_DragEnter(obj);
}
@Override
public void mouseExited(MouseEvent e) {super.mouseExited(e);
DragExit();
}
});
}
void AdditWindowDropObject(JComponent obj)
{
obj.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {super.mousePressed(e);
Paddle_MouseDown(obj);
}
@Override
public void mouseReleased(MouseEvent e) {super.mouseReleased(e);
Paddle_DragDrop();
}
});
}
void Paddle_MouseDown(Object sender)
{
IAdditionalDrawingObject windows;
switch (((JLabel)sender).getText()){
case "Простая":
windows = new DrawingAirplaneWindow();
break;
case "Треугольная":
windows = new DrawingTriangleAirplaneWindow();
break;
case "Квадратная":
windows = new DrawingRectAirplaneWindow();
break;
default:
return;
}
enterObject = windows;
}
void Paddle_DragEnter(Object sender)
{
if(enterObject != null && IAdditionalDrawingObject.class.isAssignableFrom(enterObject.getClass()) && _plane != null)
{ {
if(label == LabelSimpleWindow) setCursor(new Cursor(Cursor.HAND_CURSOR));
{ dragObject = sender;
_plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingAirplaneWindow()); }
Draw(_plane); }
}
if(label == LabelRectWindow) void Paddle_DragDrop()
{ {
_plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingRectAirplaneWindow()); if(dragObject == null)
Draw(_plane); {
} return;
}
if(label == LabelTriangleWindow) _plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), (IAdditionalDrawingObject)enterObject);
{ DragExit();
_plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingTriangleAirplaneWindow()); enterObject = null;
Draw(_plane); }
}
repaint(); void DragExit()
{
if(enterObject != null && dragObject != null)
{
setCursor(Cursor.getDefaultCursor());
Draw(_plane);
dragObject = null;
} }
} }
} }

View File

@ -143,7 +143,7 @@ public class MapsCollection
try (BufferedWriter writter = new BufferedWriter(new FileWriter(filename))) try (BufferedWriter writter = new BufferedWriter(new FileWriter(filename)))
{ {
writter.write(String.format("SaveOneMap" + System.lineSeparator() + "Data of map:")); writter.write(String.format("SaveOneMap" + System.lineSeparator() + "Data of map:" + System.lineSeparator()));
for(Map.Entry<String, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>> entry : _mapStorage.entrySet()) for(Map.Entry<String, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>> entry : _mapStorage.entrySet())
{ {