Mercurial > hg > Applications > TreeVNC
view 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 |
line wrap: on
line source
// Copyright (C) 2010, 2011 GlavSoft LLC. // All rights reserved. // //------------------------------------------------------------------------- // This file is part of the TightVNC software. Please visit our Web site: // // http://www.tightvnc.com/ // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. //------------------------------------------------------------------------- // package com.glavsoft.viewer; import com.glavsoft.viewer.swing.Surface; import com.glavsoft.viewer.swing.UiSettings; import com.glavsoft.viewer.swing.Utils; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.Serializable; public class ContainerManager implements Serializable { /** * */ private static final long serialVersionUID = 1L; private final Viewer viewer; private JToggleButton zoomFitButton; private JButton zoomInButton; private JButton zoomOutButton; private JButton zoomAsIsButton; private JPanel outerPanel; private JScrollPane scroller; private Container container; private JPanel buttonBar; public ContainerManager(Viewer viewer) { this.viewer = viewer; } public Container createContainer(final Surface surface, boolean isSeparateFrame, boolean isApplet) { outerPanel = System.getProperty("os.name").toLowerCase().startsWith("windows") ? new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)) { /** * */ private static final long serialVersionUID = 1L; @Override public Dimension getSize() { return surface.getPreferredSize(); } } : new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); outerPanel.add(surface); JFrame frame; scroller = new JScrollPane(outerPanel); if (isSeparateFrame) { frame = new JFrame(); if ( ! isApplet) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } java.util.List<Image> icons = Utils.getIcons(); if (icons.size() != 0) { frame.setIconImages(icons); } container = frame; } else { container = viewer; } container.setLayout(new BorderLayout(0, 0)); container.add(scroller, BorderLayout.CENTER); if (isSeparateFrame) { // frame.pack(); outerPanel.setSize(surface.getPreferredSize()); internalPack(null); container.setVisible(true); } container.validate(); return container; } public void pack() { final Dimension outerPanelOldSize = outerPanel.getSize(); outerPanel.setSize(viewer.getSurface().getPreferredSize()); if (container != viewer && ! viewer.isZoomToFitSelected()) { internalPack(outerPanelOldSize); } scroller.validate(); updateZoomButtonsState(); viewer.updateFrameTitle(); } private void internalPack(Dimension outerPanelOldSize) { final Rectangle workareaRectangle = getWorkareaRectangle(); final boolean isHScrollBar = scroller.getHorizontalScrollBar().isShowing(); final boolean isVScrollBar = scroller.getVerticalScrollBar().isShowing(); container.validate(); final Insets containerInsets = container.getInsets(); Dimension preferredSize = container.getPreferredSize(); Rectangle preferredRectangle = new Rectangle(container.getLocation(), preferredSize); // if (workareaRectangle.contains(preferredRectangle)) { if (null == outerPanelOldSize && workareaRectangle.contains(preferredRectangle)) { ((JFrame)container).pack(); } else { Dimension minDimension = new Dimension( containerInsets.left + containerInsets.right, containerInsets.top + containerInsets.bottom); if (buttonBar != null) { minDimension.width += buttonBar.getMinimumSize().width; minDimension.height += buttonBar.getMinimumSize().height; } Dimension dim = new Dimension(preferredSize); Point location = container.getLocation(); if (isHScrollBar) { dim.width = container.getWidth(); } else { if (isVScrollBar) dim.width += scroller.getVerticalScrollBar().getWidth(); if (dim.width < minDimension.width) dim.width = minDimension.width; int dx = location.x - workareaRectangle.x; int w = workareaRectangle.width - dx; if (w < dim.width) { int dw = dim.width - w; if (dw < dx) { location.x -= dw; } else { dim.width = workareaRectangle.width; location.x = workareaRectangle.x; } } } if (isVScrollBar) { dim.height = container.getHeight(); } else { if (isHScrollBar) dim.height += scroller.getHorizontalScrollBar().getHeight(); if (dim.height < minDimension.height) dim.height = minDimension.height; int dy = location.y - workareaRectangle.y; int h = workareaRectangle.height - dy; if (h < dim.height) { int dh = dim.height - h; if (dh < dy) { // h = dim.height; location.y -= dh; } else { // h = dim.height = workareaRectangle.height; location.y = workareaRectangle.y; } } } if ( ! location.equals(container.getLocation())) { container.setLocation(location); } container.setSize(dim); } } private Rectangle getWorkareaRectangle() { final GraphicsConfiguration graphicsConfiguration = container.getGraphicsConfiguration(); final Rectangle screenBounds = graphicsConfiguration.getBounds(); final Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfiguration); screenBounds.x += screenInsets.left; screenBounds.y += screenInsets.top; screenBounds.width -= screenInsets.left + screenInsets.right; screenBounds.height -= screenInsets.top + screenInsets.bottom; return screenBounds; } void addZoomButtons(JPanel buttonBar, Insets margin) { this.buttonBar = buttonBar; buttonBar.add(Box.createHorizontalStrut(10)); zoomFitButton = new JToggleButton(Utils.getButtonIcon("zoom-fit")); zoomOutButton = new JButton(Utils.getButtonIcon("zoom-out")); zoomOutButton.setToolTipText("Zoom Out"); zoomOutButton.setMargin(margin); buttonBar.add(zoomOutButton); zoomOutButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { zoomFitButton.setSelected(false); viewer.getUiSettings().zoomOut(); } }); zoomInButton = new JButton(Utils.getButtonIcon("zoom-in")); zoomInButton.setToolTipText("Zoom In"); zoomInButton.setMargin(margin); buttonBar.add(zoomInButton); zoomInButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { zoomFitButton.setSelected(false); viewer.getUiSettings().zoomIn(); } }); zoomAsIsButton = new JButton(Utils.getButtonIcon("zoom-100")); zoomAsIsButton.setToolTipText("Zoom 100%"); zoomAsIsButton.setMargin(margin); buttonBar.add(zoomAsIsButton); zoomAsIsButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { zoomFitButton.setSelected(false); viewer.getUiSettings().zoomAsIs(); } }); zoomFitButton.setToolTipText("Zoom to Fit Window"); zoomFitButton.setMargin(margin); buttonBar.add(zoomFitButton); zoomFitButton.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { viewer.setZoomToFitSelected(true); zoomToFit(); updateZoomButtonsState(); } else { viewer.setZoomToFitSelected(false); } viewer.setSurfaceToHandleKbdFocus(); } }); } private void zoomToFit() { Dimension scrollerSize = scroller.getSize(); Insets scrollerInsets = scroller.getInsets(); viewer.getUiSettings().zoomToFit(scrollerSize.width - scrollerInsets.left - scrollerInsets.right, scrollerSize.height - scrollerInsets.top - scrollerInsets.bottom, viewer.getWorkingProtocol().getFbWidth(), viewer.getWorkingProtocol().getFbHeight()); } void registerResizeListener(Container container) { container.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { if (viewer.isZoomToFitSelected()) { zoomToFit(); updateZoomButtonsState(); viewer.updateFrameTitle(); viewer.setSurfaceToHandleKbdFocus(); } } }); } void updateZoomButtonsState() { zoomOutButton.setEnabled(viewer.getUiSettings().getScalePercent() > UiSettings.MIN_SCALE_PERCENT); zoomInButton.setEnabled(viewer.getUiSettings().getScalePercent() < UiSettings.MAX_SCALE_PERCENT); zoomAsIsButton.setEnabled(viewer.getUiSettings().getScalePercent() != 100); } }