svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / gui / DefaultViewInformationArea.java @ 43580
History | View | Annotate | Download (4.8 KB)
1 | 43576 | jjdelcerro | /*
|
---|---|---|---|
2 | * Copyright (C) 2017 gvSIG Association
|
||
3 | *
|
||
4 | * This program is free software: you can redistribute it and/or modify
|
||
5 | * it under the terms of the GNU General Public License as published by
|
||
6 | * the Free Software Foundation, either version 3 of the License, or
|
||
7 | * (at your option) any later version.
|
||
8 | *
|
||
9 | * This program is distributed in the hope that it will be useful,
|
||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
12 | * GNU General Public License for more details.
|
||
13 | *
|
||
14 | * You should have received a copy of the GNU General Public License
|
||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
16 | */
|
||
17 | package org.gvsig.app.project.documents.view.gui; |
||
18 | |||
19 | import java.awt.BorderLayout; |
||
20 | import java.util.ArrayList; |
||
21 | import java.util.Collections; |
||
22 | import java.util.Comparator; |
||
23 | import java.util.HashMap; |
||
24 | import java.util.List; |
||
25 | import java.util.Map; |
||
26 | import javax.swing.Icon; |
||
27 | import javax.swing.JComponent; |
||
28 | import javax.swing.JPanel; |
||
29 | import javax.swing.JTabbedPane; |
||
30 | import org.gvsig.tools.swing.api.Component; |
||
31 | 43580 | jjdelcerro | import org.slf4j.Logger; |
32 | import org.slf4j.LoggerFactory; |
||
33 | 43576 | jjdelcerro | |
34 | /**
|
||
35 | *
|
||
36 | * @author jjdelcerro
|
||
37 | */
|
||
38 | public class DefaultViewInformationArea implements ViewInformationArea { |
||
39 | |||
40 | 43580 | jjdelcerro | protected final Logger logger = LoggerFactory.getLogger(DefaultViewInformationArea.class); |
41 | |||
42 | 43576 | jjdelcerro | private static class Element implements Component { |
43 | |||
44 | private final JComponent component; |
||
45 | private final String id; |
||
46 | private final String label; |
||
47 | private final Icon icon; |
||
48 | private final int priority; |
||
49 | private String tip; |
||
50 | |||
51 | public Element(JComponent component, String id, int priority, String label, Icon icon, String tip) { |
||
52 | this.component = component;
|
||
53 | this.id = id;
|
||
54 | this.priority = priority;
|
||
55 | this.label = label;
|
||
56 | this.icon = icon;
|
||
57 | this.tip = tip;
|
||
58 | } |
||
59 | |||
60 | public int getPriority() { |
||
61 | return this.priority; |
||
62 | } |
||
63 | |||
64 | @Override
|
||
65 | public JComponent asJComponent() { |
||
66 | return this.component; |
||
67 | } |
||
68 | |||
69 | public String getLabel() { |
||
70 | return this.label; |
||
71 | } |
||
72 | |||
73 | public String getTip() { |
||
74 | return this.tip; |
||
75 | } |
||
76 | |||
77 | public Icon getIcon() { |
||
78 | return this.icon; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | private final Map<String,Element> elements; |
||
83 | private JPanel panel; |
||
84 | 43580 | jjdelcerro | private JTabbedPane tab; |
85 | 43576 | jjdelcerro | |
86 | public DefaultViewInformationArea() {
|
||
87 | this.elements = new HashMap<>(); |
||
88 | this.panel = null; |
||
89 | 43580 | jjdelcerro | this.tab = null; |
90 | 43576 | jjdelcerro | } |
91 | |||
92 | @Override
|
||
93 | public void add(JComponent component, String id, int priority, String label, Icon icon, String tip) { |
||
94 | Element element = new Element(component, id, priority, label, icon, tip); |
||
95 | this.elements.put(id, element);
|
||
96 | this.update();
|
||
97 | } |
||
98 | |||
99 | @Override
|
||
100 | public void remove(String id) { |
||
101 | this.elements.remove(id);
|
||
102 | this.update();
|
||
103 | } |
||
104 | |||
105 | private void update() { |
||
106 | if( this.panel == null ) { |
||
107 | return;
|
||
108 | } |
||
109 | this.panel.removeAll();
|
||
110 | if( this.elements.isEmpty() ) { |
||
111 | return;
|
||
112 | } |
||
113 | List<Element> elements = new ArrayList(this.elements.values()); |
||
114 | if( elements.size()==1 ) { |
||
115 | Element element = elements.get(0); |
||
116 | this.panel.add(element.asJComponent(), BorderLayout.CENTER); |
||
117 | return;
|
||
118 | } |
||
119 | Collections.sort(elements, new Comparator<Element>() { |
||
120 | @Override
|
||
121 | public int compare(Element o1, Element o2) { |
||
122 | return Integer.compare(o1.getPriority(),o2.getPriority()); |
||
123 | } |
||
124 | }); |
||
125 | JTabbedPane tab = new JTabbedPane(); |
||
126 | tab.setTabPlacement(JTabbedPane.BOTTOM);
|
||
127 | tab.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
|
||
128 | for( Element element : elements ) { |
||
129 | tab.addTab( |
||
130 | element.getLabel(), |
||
131 | element.getIcon(), |
||
132 | element.asJComponent(), |
||
133 | element.getTip() |
||
134 | ); |
||
135 | } |
||
136 | 43580 | jjdelcerro | int index = 0; |
137 | if( this.tab != null ) { |
||
138 | index = this.tab.getSelectedIndex();
|
||
139 | } |
||
140 | if( index < 0 ) { |
||
141 | index = 0;
|
||
142 | } else if( index >= tab.getTabCount() ) { |
||
143 | index = tab.getTabCount()-1;
|
||
144 | } |
||
145 | try {
|
||
146 | tab.setSelectedIndex(index); |
||
147 | } catch(Exception ex) { |
||
148 | logger.warn("Can't restore the selected tab", ex);
|
||
149 | } |
||
150 | this.tab = tab;
|
||
151 | this.panel.add(this.tab, BorderLayout.CENTER); |
||
152 | 43576 | jjdelcerro | } |
153 | |||
154 | @Override
|
||
155 | public JComponent asJComponent() { |
||
156 | if( this.panel == null ) { |
||
157 | this.panel = new JPanel(); |
||
158 | this.panel.setLayout(new BorderLayout()); |
||
159 | this.update();
|
||
160 | } |
||
161 | return this.panel; |
||
162 | } |
||
163 | |||
164 | } |