source: osm/applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewerTree.java@ 34759

Last change on this file since 34759 was 34759, checked in by donvip, 6 years ago

use lambdas for listeners

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1// License: GPL. For details, see Readme.txt file.
2package org.openstreetmap.gui.jmapviewer;
3
4import java.awt.BorderLayout;
5import java.awt.Dimension;
6import java.awt.event.MouseAdapter;
7import java.awt.event.MouseEvent;
8import java.util.List;
9
10import javax.swing.JLabel;
11import javax.swing.JMenuItem;
12import javax.swing.JPanel;
13import javax.swing.JPopupMenu;
14import javax.swing.JSplitPane;
15import javax.swing.event.TreeModelEvent;
16import javax.swing.event.TreeModelListener;
17
18import org.openstreetmap.gui.jmapviewer.checkBoxTree.CheckBoxNodePanel;
19import org.openstreetmap.gui.jmapviewer.checkBoxTree.CheckBoxTree;
20import org.openstreetmap.gui.jmapviewer.interfaces.MapObject;
21
22/**
23 * Tree of layers for JMapViewer component
24 * @author galo
25 */
26public class JMapViewerTree extends JPanel {
27 /** Serial Version UID */
28 private static final long serialVersionUID = 3050203054402323972L;
29
30 private JMapViewer map;
31 private CheckBoxTree tree;
32 private JPanel treePanel;
33 private JSplitPane splitPane;
34
35 public JMapViewerTree(String name) {
36 this(name, false);
37 }
38
39 public JMapViewerTree(String name, boolean treeVisible) {
40 super();
41 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
42
43 tree = new CheckBoxTree(name);
44 treePanel = new JPanel(new BorderLayout());
45 treePanel.add(tree, BorderLayout.CENTER);
46 treePanel.add(new JLabel("<html><center>Use right mouse button to<br />show/hide texts</center></html>"), BorderLayout.SOUTH);
47 map = new JMapViewer();
48
49 splitPane.setOneTouchExpandable(true);
50 splitPane.setDividerLocation(150);
51
52 //Provide minimum sizes for the two components in the split pane
53 Dimension minimumSize = new Dimension(100, 50);
54 //tree.setMinimumSize(minimumSize);
55 map.setMinimumSize(minimumSize);
56 createRefresh();
57 setLayout(new BorderLayout());
58 setTreeVisible(treeVisible);
59 tree.addNodeListener(new MouseAdapter() {
60 @Override
61 public void mousePressed(MouseEvent e) {
62 maybeShowPopup(e);
63 }
64
65 @Override
66 public void mouseReleased(MouseEvent e) {
67 maybeShowPopup(e);
68 }
69
70 private void maybeShowPopup(MouseEvent e) {
71 if (e.isPopupTrigger()) {
72 AbstractLayer layer = ((CheckBoxNodePanel) e.getComponent()).getData().getAbstractLayer();
73 if (layer != null)
74 JMapViewerTree.this.createPopupMenu(layer).show(e.getComponent(), e.getX(), e.getY());
75 }
76 }
77 });
78 }
79
80 private JPopupMenu createPopupMenu(final AbstractLayer layer) {
81 JMenuItem menuItemShow = new JMenuItem("show texts");
82 JMenuItem menuItemHide = new JMenuItem("hide texts");
83
84 //Create the popup menu.
85 JPopupMenu popup = new JPopupMenu();
86
87 // Create items
88 if (layer.isVisibleTexts() == null) {
89 popup.add(menuItemShow);
90 popup.add(menuItemHide);
91 } else if (layer.isVisibleTexts()) popup.add(menuItemHide);
92 else popup.add(menuItemShow);
93
94 menuItemShow.addActionListener(e -> {
95 setVisibleTexts(layer, true);
96 if (layer.getParent() != null) layer.getParent().calculateVisibleTexts();
97 map.repaint();
98 });
99 menuItemHide.addActionListener(e -> {
100 setVisibleTexts(layer, false);
101 if (layer.getParent() != null) layer.getParent().calculateVisibleTexts();
102 map.repaint();
103 });
104
105 return popup;
106 }
107
108 private static void setVisibleTexts(AbstractLayer layer, boolean visible) {
109 layer.setVisibleTexts(visible);
110 if (layer instanceof LayerGroup) {
111 LayerGroup group = (LayerGroup) layer;
112 if (group.getLayers() != null)
113 for (AbstractLayer al: group.getLayers()) {
114 setVisibleTexts(al, visible);
115 }
116 }
117 }
118
119 public Layer addLayer(String name) {
120 Layer layer = new Layer(name);
121 this.addLayer(layer);
122 return layer;
123 }
124
125 public JMapViewerTree addLayer(Layer layer) {
126 tree.addLayer(layer);
127 return this;
128 }
129
130 public JMapViewerTree addLayer(MapObject element) {
131 //element.getLayer().add(element);
132 return addLayer(element.getLayer());
133 }
134
135 public Layer removeFromLayer(MapObject element) {
136 element.getLayer().getElements().remove(element);
137 return element.getLayer();
138 }
139
140 public static int size(List<?> list) {
141 return list == null ? 0 : list.size();
142 }
143
144 public JMapViewer getViewer() {
145 return map;
146 }
147
148 public CheckBoxTree getTree() {
149 return tree;
150 }
151
152 public void addMapObject(MapObject o){
153
154 }
155
156 public void setTreeVisible(boolean visible) {
157 removeAll();
158 revalidate();
159 if (visible) {
160 splitPane.setLeftComponent(treePanel);
161 splitPane.setRightComponent(map);
162 add(splitPane, BorderLayout.CENTER);
163 } else add(map, BorderLayout.CENTER);
164 repaint();
165 }
166
167 private void createRefresh() {
168 tree.getModel().addTreeModelListener(new TreeModelListener() {
169 @Override
170 public void treeNodesChanged(final TreeModelEvent e) {
171 repaint();
172 }
173
174 @Override
175 public void treeNodesInserted(TreeModelEvent arg0) {
176 // TODO Auto-generated method stub
177 }
178
179 @Override
180 public void treeNodesRemoved(TreeModelEvent arg0) {
181 // TODO Auto-generated method stub
182 }
183
184 @Override
185 public void treeStructureChanged(TreeModelEvent arg0) {
186 // TODO Auto-generated method stub
187 }
188 });
189 }
190}
Note: See TracBrowser for help on using the repository browser.