Mercurial > hg > Applications > TightVNC_orginal
comparison src/viewer_swing/java/com/glavsoft/viewer/ContainerManager.java @ 0:4689cc86d6cb
create TreeViewer2 Repository
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 03 Jul 2012 13:20:49 +0900 |
parents | |
children | e7ce2b2ffed8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4689cc86d6cb |
---|---|
1 // Copyright (C) 2010, 2011 GlavSoft LLC. | |
2 // All rights reserved. | |
3 // | |
4 //------------------------------------------------------------------------- | |
5 // This file is part of the TightVNC software. Please visit our Web site: | |
6 // | |
7 // http://www.tightvnc.com/ | |
8 // | |
9 // This program is free software; you can redistribute it and/or modify | |
10 // it under the terms of the GNU General Public License as published by | |
11 // the Free Software Foundation; either version 2 of the License, or | |
12 // (at your option) any later version. | |
13 // | |
14 // This program is distributed in the hope that it will be useful, | |
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 // GNU General Public License for more details. | |
18 // | |
19 // You should have received a copy of the GNU General Public License along | |
20 // with this program; if not, write to the Free Software Foundation, Inc., | |
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
22 //------------------------------------------------------------------------- | |
23 // | |
24 | |
25 package com.glavsoft.viewer; | |
26 | |
27 import com.glavsoft.viewer.swing.Surface; | |
28 import com.glavsoft.viewer.swing.UiSettings; | |
29 import com.glavsoft.viewer.swing.Utils; | |
30 | |
31 import javax.swing.*; | |
32 import java.awt.*; | |
33 import java.awt.event.*; | |
34 import java.io.Serializable; | |
35 | |
36 public class ContainerManager implements Serializable { | |
37 /** | |
38 * | |
39 */ | |
40 private static final long serialVersionUID = 1L; | |
41 private final Viewer viewer; | |
42 private JToggleButton zoomFitButton; | |
43 private JButton zoomInButton; | |
44 private JButton zoomOutButton; | |
45 private JButton zoomAsIsButton; | |
46 private JPanel outerPanel; | |
47 private JScrollPane scroller; | |
48 private Container container; | |
49 private JPanel buttonBar; | |
50 | |
51 public ContainerManager(Viewer viewer) { this.viewer = viewer; } | |
52 | |
53 | |
54 public Container createContainer(final Surface surface, boolean isSeparateFrame, boolean isApplet) { | |
55 outerPanel = System.getProperty("os.name").toLowerCase().startsWith("windows") ? | |
56 new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)) { | |
57 /** | |
58 * | |
59 */ | |
60 private static final long serialVersionUID = 1L; | |
61 | |
62 @Override | |
63 public Dimension getSize() { | |
64 return surface.getPreferredSize(); | |
65 } | |
66 } : | |
67 new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); | |
68 | |
69 outerPanel.add(surface); | |
70 | |
71 JFrame frame; | |
72 scroller = new JScrollPane(outerPanel); | |
73 if (isSeparateFrame) { | |
74 frame = new JFrame(); | |
75 if ( ! isApplet) { | |
76 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
77 } | |
78 java.util.List<Image> icons = Utils.getIcons(); | |
79 if (icons.size() != 0) { | |
80 frame.setIconImages(icons); | |
81 } | |
82 container = frame; | |
83 } else { | |
84 container = viewer; | |
85 } | |
86 container.setLayout(new BorderLayout(0, 0)); | |
87 container.add(scroller, BorderLayout.CENTER); | |
88 | |
89 if (isSeparateFrame) { | |
90 // frame.pack(); | |
91 outerPanel.setSize(surface.getPreferredSize()); | |
92 internalPack(null); | |
93 container.setVisible(true); | |
94 } | |
95 container.validate(); | |
96 return container; | |
97 } | |
98 | |
99 public void pack() { | |
100 final Dimension outerPanelOldSize = outerPanel.getSize(); | |
101 outerPanel.setSize(viewer.getSurface().getPreferredSize()); | |
102 if (container != viewer && ! viewer.isZoomToFitSelected()) { | |
103 internalPack(outerPanelOldSize); | |
104 } | |
105 scroller.validate(); | |
106 updateZoomButtonsState(); | |
107 viewer.updateFrameTitle(); | |
108 } | |
109 | |
110 private void internalPack(Dimension outerPanelOldSize) { | |
111 final Rectangle workareaRectangle = getWorkareaRectangle(); | |
112 final boolean isHScrollBar = scroller.getHorizontalScrollBar().isShowing(); | |
113 final boolean isVScrollBar = scroller.getVerticalScrollBar().isShowing(); | |
114 | |
115 container.validate(); | |
116 | |
117 final Insets containerInsets = container.getInsets(); | |
118 Dimension preferredSize = container.getPreferredSize(); | |
119 Rectangle preferredRectangle = new Rectangle(container.getLocation(), preferredSize); | |
120 | |
121 // if (workareaRectangle.contains(preferredRectangle)) { | |
122 if (null == outerPanelOldSize && workareaRectangle.contains(preferredRectangle)) { | |
123 ((JFrame)container).pack(); | |
124 } else { | |
125 Dimension minDimension = new Dimension( | |
126 containerInsets.left + containerInsets.right, containerInsets.top + containerInsets.bottom); | |
127 if (buttonBar != null) { | |
128 minDimension.width += buttonBar.getMinimumSize().width; | |
129 minDimension.height += buttonBar.getMinimumSize().height; | |
130 } | |
131 Dimension dim = new Dimension(preferredSize); | |
132 Point location = container.getLocation(); | |
133 if (isHScrollBar) { | |
134 dim.width = container.getWidth(); | |
135 } else { | |
136 if (isVScrollBar) dim.width += scroller.getVerticalScrollBar().getWidth(); | |
137 if (dim.width < minDimension.width) dim.width = minDimension.width; | |
138 | |
139 int dx = location.x - workareaRectangle.x; | |
140 int w = workareaRectangle.width - dx; | |
141 if (w < dim.width) { | |
142 int dw = dim.width - w; | |
143 if (dw < dx) { | |
144 location.x -= dw; | |
145 } else { | |
146 dim.width = workareaRectangle.width; | |
147 location.x = workareaRectangle.x; | |
148 } | |
149 } | |
150 } | |
151 if (isVScrollBar) { | |
152 dim.height = container.getHeight(); | |
153 } else { | |
154 | |
155 if (isHScrollBar) dim.height += scroller.getHorizontalScrollBar().getHeight(); | |
156 if (dim.height < minDimension.height) dim.height = minDimension.height; | |
157 | |
158 int dy = location.y - workareaRectangle.y; | |
159 int h = workareaRectangle.height - dy; | |
160 if (h < dim.height) { | |
161 int dh = dim.height - h; | |
162 if (dh < dy) { | |
163 // h = dim.height; | |
164 location.y -= dh; | |
165 } else { | |
166 // h = | |
167 dim.height = workareaRectangle.height; | |
168 location.y = workareaRectangle.y; | |
169 } | |
170 } | |
171 } | |
172 if ( ! location.equals(container.getLocation())) { | |
173 container.setLocation(location); | |
174 } | |
175 container.setSize(dim); | |
176 } | |
177 } | |
178 | |
179 private Rectangle getWorkareaRectangle() { | |
180 final GraphicsConfiguration graphicsConfiguration = container.getGraphicsConfiguration(); | |
181 final Rectangle screenBounds = graphicsConfiguration.getBounds(); | |
182 final Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfiguration); | |
183 | |
184 screenBounds.x += screenInsets.left; | |
185 screenBounds.y += screenInsets.top; | |
186 screenBounds.width -= screenInsets.left + screenInsets.right; | |
187 screenBounds.height -= screenInsets.top + screenInsets.bottom; | |
188 return screenBounds; | |
189 } | |
190 | |
191 void addZoomButtons(JPanel buttonBar, Insets margin) { | |
192 this.buttonBar = buttonBar; | |
193 buttonBar.add(Box.createHorizontalStrut(10)); | |
194 | |
195 zoomFitButton = new JToggleButton(Utils.getButtonIcon("zoom-fit")); | |
196 | |
197 zoomOutButton = new JButton(Utils.getButtonIcon("zoom-out")); | |
198 zoomOutButton.setToolTipText("Zoom Out"); | |
199 zoomOutButton.setMargin(margin); | |
200 buttonBar.add(zoomOutButton); | |
201 zoomOutButton.addActionListener(new ActionListener() { | |
202 @Override | |
203 public void actionPerformed(ActionEvent e) { | |
204 zoomFitButton.setSelected(false); | |
205 viewer.getUiSettings().zoomOut(); | |
206 } | |
207 }); | |
208 | |
209 zoomInButton = new JButton(Utils.getButtonIcon("zoom-in")); | |
210 zoomInButton.setToolTipText("Zoom In"); | |
211 zoomInButton.setMargin(margin); | |
212 buttonBar.add(zoomInButton); | |
213 zoomInButton.addActionListener(new ActionListener() { | |
214 @Override | |
215 public void actionPerformed(ActionEvent e) { | |
216 zoomFitButton.setSelected(false); | |
217 viewer.getUiSettings().zoomIn(); | |
218 } | |
219 }); | |
220 | |
221 zoomAsIsButton = new JButton(Utils.getButtonIcon("zoom-100")); | |
222 zoomAsIsButton.setToolTipText("Zoom 100%"); | |
223 zoomAsIsButton.setMargin(margin); | |
224 buttonBar.add(zoomAsIsButton); | |
225 zoomAsIsButton.addActionListener(new ActionListener() { | |
226 @Override | |
227 public void actionPerformed(ActionEvent e) { | |
228 zoomFitButton.setSelected(false); | |
229 viewer.getUiSettings().zoomAsIs(); | |
230 } | |
231 }); | |
232 | |
233 zoomFitButton.setToolTipText("Zoom to Fit Window"); | |
234 zoomFitButton.setMargin(margin); | |
235 buttonBar.add(zoomFitButton); | |
236 | |
237 zoomFitButton.addItemListener(new ItemListener() { | |
238 @Override | |
239 public void itemStateChanged(ItemEvent e) { | |
240 if (e.getStateChange() == ItemEvent.SELECTED) { | |
241 viewer.setZoomToFitSelected(true); | |
242 zoomToFit(); | |
243 updateZoomButtonsState(); | |
244 } else { | |
245 viewer.setZoomToFitSelected(false); | |
246 } | |
247 viewer.setSurfaceToHandleKbdFocus(); | |
248 } | |
249 }); | |
250 } | |
251 | |
252 private void zoomToFit() { | |
253 Dimension scrollerSize = scroller.getSize(); | |
254 Insets scrollerInsets = scroller.getInsets(); | |
255 viewer.getUiSettings().zoomToFit(scrollerSize.width - scrollerInsets.left - scrollerInsets.right, | |
256 scrollerSize.height - scrollerInsets.top - scrollerInsets.bottom, | |
257 viewer.getWorkingProtocol().getFbWidth(), viewer.getWorkingProtocol().getFbHeight()); | |
258 } | |
259 | |
260 void registerResizeListener(Container container) { | |
261 container.addComponentListener(new ComponentAdapter() { | |
262 @Override | |
263 public void componentResized(ComponentEvent e) { | |
264 if (viewer.isZoomToFitSelected()) { | |
265 zoomToFit(); | |
266 updateZoomButtonsState(); | |
267 viewer.updateFrameTitle(); | |
268 viewer.setSurfaceToHandleKbdFocus(); | |
269 } | |
270 } | |
271 }); | |
272 } | |
273 | |
274 void updateZoomButtonsState() { | |
275 zoomOutButton.setEnabled(viewer.getUiSettings().getScalePercent() > UiSettings.MIN_SCALE_PERCENT); | |
276 zoomInButton.setEnabled(viewer.getUiSettings().getScalePercent() < UiSettings.MAX_SCALE_PERCENT); | |
277 zoomAsIsButton.setEnabled(viewer.getUiSettings().getScalePercent() != 100); | |
278 } | |
279 } |