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