1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package smed;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.WindowAdapter;
|
---|
9 | import java.awt.event.WindowEvent;
|
---|
10 | import java.util.Collection;
|
---|
11 |
|
---|
12 | import javax.swing.JFrame;
|
---|
13 | import javax.swing.SwingUtilities;
|
---|
14 | import javax.swing.WindowConstants;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
---|
20 |
|
---|
21 | import messages.Messages;
|
---|
22 | import panels.PanelMain;
|
---|
23 |
|
---|
24 | public class SmedAction extends JosmAction implements DataSelectionListener {
|
---|
25 |
|
---|
26 | private static final long serialVersionUID = 1L;
|
---|
27 | private static String editor = tr("SeaMap Editor");
|
---|
28 | public static JFrame editFrame = null;
|
---|
29 | private boolean isOpen = false;
|
---|
30 | public static PanelMain panelMain = null;
|
---|
31 |
|
---|
32 | public OsmPrimitive node = null;
|
---|
33 | private Collection<? extends OsmPrimitive> selection = null;
|
---|
34 |
|
---|
35 | public SmedAction() {
|
---|
36 | super(editor, "Smed", editor, null, true);
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public void actionPerformed(ActionEvent arg0) {
|
---|
41 | SwingUtilities.invokeLater(new Runnable() {
|
---|
42 | @Override
|
---|
43 | public void run() {
|
---|
44 | if (!isOpen) {
|
---|
45 | createFrame();
|
---|
46 | } else {
|
---|
47 | editFrame.toFront();
|
---|
48 | }
|
---|
49 | isOpen = true;
|
---|
50 | }
|
---|
51 | });
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected void createFrame() {
|
---|
55 | editFrame = new JFrame(editor);
|
---|
56 | editFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
---|
57 | editFrame.addWindowListener(new WindowAdapter() {
|
---|
58 | @Override
|
---|
59 | public void windowClosing(WindowEvent e) {
|
---|
60 | closeDialog();
|
---|
61 | }
|
---|
62 | });
|
---|
63 | editFrame.setSize(new Dimension(420, 430));
|
---|
64 | editFrame.setLocation(100, 200);
|
---|
65 | editFrame.setResizable(true);
|
---|
66 | editFrame.setAlwaysOnTop(true);
|
---|
67 | editFrame.setVisible(true);
|
---|
68 | editFrame.setLayout(null);
|
---|
69 | panelMain = new PanelMain(this);
|
---|
70 | panelMain.setBounds(10, 10, 400, 400);
|
---|
71 | node = null;
|
---|
72 | panelMain.syncPanel();
|
---|
73 | editFrame.add(panelMain);
|
---|
74 | SelectionEventManager.getInstance().addSelectionListener(this);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void closeDialog() {
|
---|
78 | if (isOpen) {
|
---|
79 | editFrame.setVisible(false);
|
---|
80 | editFrame.dispose();
|
---|
81 | }
|
---|
82 | isOpen = false;
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public void selectionChanged(SelectionChangeEvent event) {
|
---|
87 | OsmPrimitive nextNode = null;
|
---|
88 | selection = event.getSelection();
|
---|
89 |
|
---|
90 | for (OsmPrimitive osm : selection) {
|
---|
91 | nextNode = osm;
|
---|
92 | if (selection.size() == 1) {
|
---|
93 | if (nextNode.compareTo(node) != 0) {
|
---|
94 | node = nextNode;
|
---|
95 | panelMain.mark.parseMark(node);
|
---|
96 | }
|
---|
97 | } else {
|
---|
98 | node = null;
|
---|
99 | panelMain.mark.clrMark();
|
---|
100 | PanelMain.messageBar.setText(Messages.getString("OneNode"));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | if (nextNode == null) {
|
---|
104 | node = null;
|
---|
105 | panelMain.mark.clrMark();
|
---|
106 | PanelMain.messageBar.setText(Messages.getString("SelectNode"));
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | }
|
---|