zoomdialog.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 220 行

JAVA
220
字号
/**
 *    $Id:ZoomDialog.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfdraw.gui.dialog;
 

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Container;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JComboBox;


import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.utils.commonutil.CommonUtil;
import com.jfimagine.jfdraw.gui.GUIConst;


 /**
 * ZoomDialog class.  A class used to get a new zoom scale in gui by user.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
public class ZoomDialog extends JDialog
                        implements ActionListener {
    
    /** Minimum zoom scale allowed */
    public static final double	ZOOMSCALE_MIN		=0.25;
    /** Maximum zoom scale allowed */
    public static final double	ZOOMSCALE_MAX		=5.0;
    

    private static final double zoomStep		=0.5;
    /**
     *  get next smaller zoom scale according current zoom scale
     *  @return a smaller zoom scale.
     */
    public static double nextSmallZoomScale(double zoomScale){
	        zoomScale	-=zoomStep;
	        if (zoomScale<ZOOMSCALE_MIN)
	        	zoomScale	=ZOOMSCALE_MIN;
	        return zoomScale;
    }	

    /**
     *  get next bigger zoom scale according current zoom scale
     *  @return a bigger zoom scale.
     */
    public static double nextBigZoomScale(double zoomScale){
	        zoomScale	+=zoomStep;
	        if (zoomScale>ZOOMSCALE_MAX)
	        	zoomScale	=ZOOMSCALE_MAX;
	        return zoomScale;
    }	


    private static ZoomDialog m_dialog;
    private static double m_zoomScale	=1.0;

    String []	zoomItems = {"50","100","150","200","300","400","500"};
    JComboBox zoomList;

    /**
     * Open a window, to input/modify a new zoom Scale.
     *
     * @param modalFrame  Determines which frame the dialog depends on; it should be
     * a component in the dialog's controlling frame. 
     *
     * @param initialValue initial zoom scale for modify
     * @return A new zoom scale.
     */
    public static double getNewZoomScale(Frame modalFrame,double initialValue) {
    	return getNewZoomScale(modalFrame,null,initialValue);
    }    

    /**
     * Open a window, to input/modify a new zoom Scale.
     *
     * @param modalFrame  Determines which frame the dialog depends on; it should be
     * a component in the dialog's controlling frame. 
     *
     * @param refComp  This should be null if you want the dialog
     * to come up with its left corner in the center of the screen;
     * otherwise, it should be the component on top of which the
     * dialog should appear.
     *
     * @param initialValue initial zoom scale for modify
     * @return Valid string if user enter/modified text, empty string if user canceled.
     */
    public static double getNewZoomScale(Frame modalFrame,Component refComp,double initialValue) {
    	return showDialog(modalFrame,refComp,initialValue);
    }    

    /**
     * Set up and show the dialog.  The first Component argument
     * determines which frame the dialog depends on; it should be
     * a component in the dialog's controlling frame. The second
     * Component argument should be null if you want the dialog
     * to come up with its left corner in the center of the screen;
     * otherwise, it should be the component on top of which the
     * dialog should appear.
     */
    private static double showDialog(Component frameComp,
                                    Component locationComp,
                                    double initialValue) {
        Frame frame = JOptionPane.getFrameForComponent(frameComp);
        m_dialog = new ZoomDialog(frame,
                                locationComp,
                                CADResource.getString("dialog.zoomScale"),
                                initialValue);
        m_dialog.setVisible(true);
        return m_zoomScale;
    }


    private ZoomDialog(Frame frame,
                       Component locationComp,
                       String title,
                       double initialValue) {
        super(frame, title, true);
	setResizable(false);

        //Create and initialize the buttons.
        JButton cancelButton = new JButton(CADResource.getString("button.cancel"));
        cancelButton.setFont(GUIConst.FONT_BUTTON);
        cancelButton.addActionListener(this);
        //
        final JButton confirmButton = new JButton(CADResource.getString("button.confirm"));
        confirmButton.setFont(GUIConst.FONT_BUTTON);
        confirmButton.setActionCommand("Confirm");
        confirmButton.addActionListener(this);
        getRootPane().setDefaultButton(confirmButton);

	
	if (initialValue<ZOOMSCALE_MIN)
		initialValue	=ZOOMSCALE_MIN;
	else if (initialValue>ZOOMSCALE_MAX)
		initialValue	=ZOOMSCALE_MAX;
	
	m_zoomScale	=initialValue;
 	zoomList	=new JComboBox(zoomItems);
 	zoomList.setFont(GUIConst.FONT_LABEL);
 	zoomList.setPreferredSize(new Dimension(50,20));
 	zoomList.setEditable(true);
	zoomList.setSelectedItem(CommonUtil.i2s((int)(m_zoomScale * 100)));

	
	//a top panel for placing zoom label, zoom list and zoom percent label.
        JPanel zoomPanel = new JPanel();
        zoomPanel.setLayout(new BoxLayout(zoomPanel, BoxLayout.X_AXIS));
        JLabel zoomLabel = new JLabel(CADResource.getString("label.zoom.zoomScale"));
        zoomLabel.setFont(GUIConst.FONT_LABEL);
        zoomPanel.add(zoomLabel);
        zoomPanel.add(Box.createRigidArea(new Dimension(5,0)));
        zoomPanel.add(zoomList);
        JLabel percentLabel = new JLabel("%");
        percentLabel.setFont(GUIConst.FONT_LABEL);
        zoomPanel.add(percentLabel);
        zoomPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        //Lay out the buttons from left to right.
        JPanel buttonPane = new JPanel();
        //buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        buttonPane.add(Box.createRigidArea(new Dimension(30, 0)));
        buttonPane.add(confirmButton);
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPane.add(cancelButton);

        //Put everything together, using the content pane's BorderLayout.
        Container contentPane = getContentPane();
        contentPane.add(zoomPanel, BorderLayout.CENTER);
        contentPane.add(buttonPane, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(locationComp);
    }

    //Handle clicks on the Set and Cancel buttons.
    public void actionPerformed(ActionEvent e) {
        if ("Confirm".equals(e.getActionCommand())) {
        	double zoomScale	=CommonUtil.s2f((String)(zoomList.getSelectedItem()))/100;
        	if (zoomScale==0)
        		zoomScale	=1;
        	else if (zoomScale<ZOOMSCALE_MIN)
        		zoomScale	=ZOOMSCALE_MIN;
        	else if (zoomScale>ZOOMSCALE_MAX)
        		zoomScale	=ZOOMSCALE_MAX;
		
		m_zoomScale	=zoomScale;		
        }else{
        	//do nothing, don't change m_zoomScale
        }

        m_dialog.setVisible(false);
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?