pagesettingdialog.java

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

JAVA
347
字号
/**
 *    $Id:PageSettingDialog.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.Color;

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.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import javax.swing.ListCellRenderer;
import javax.swing.JList;
import javax.swing.JTextField;

import com.jfimagine.jfgraph.shape.union.JFPage;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfdraw.gui.ToolFactory;
import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.jfdraw.gui.GUIConst; 
import com.jfimagine.jfgraph.shape.decorate.CanvasFormat;
import com.jfimagine.utils.commonutil.CommonUtil;

 /**
 * PageSettingDialog class.  A class is used to set the properties of a page.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.1.1 $
 */  
public class PageSettingDialog extends JDialog
                        implements ActionListener {

    private static PageSettingDialog m_dialog;
    private static JFPage m_page;
    
    private JCheckBox hidePortsCheck;
    private JCheckBox disablePortSnappingCheck; 
    
    private JTextField scaleText;
    private JComboBox scaleUnitsCombo;
    private JComboBox screenUnitsCombo;


    private JTextField canvasWidthText;
    private JComboBox canvasWidthUnitCombo;
    private JTextField canvasHeightText;
    private JComboBox canvasHeightUnitCombo;

    
    private static boolean m_modified	=false;
    
    String []	scaleUnitsItems = {ShapeConst.MEASURE_NAME_IN,
    				   ShapeConst.MEASURE_NAME_FT,	
    				   ShapeConst.MEASURE_NAME_YD,
    				   ShapeConst.MEASURE_NAME_MI,
    				   
    				   ShapeConst.MEASURE_NAME_MM,
    				   ShapeConst.MEASURE_NAME_CM,
    				   ShapeConst.MEASURE_NAME_DM,
    				   ShapeConst.MEASURE_NAME_M,
    				   ShapeConst.MEASURE_NAME_KM};

    String []	screenUnitsItems = {ShapeConst.MEASURE_NAME_IN,
    				    ShapeConst.MEASURE_NAME_CM};	
    
                           
    /**
     * Open an page setting modifier window, to modify the line format.
     *
     * @param modalFrame  Determines which frame the dialog depends on; it should be
     * a component in the dialog's controlling frame. 
     *
     * @param page A page to be modified.
     * @return True if modified page, false otherwise.
     */
    public static boolean newPageSetting(Frame modalFrame,JFPage page) {
    	return newPageSetting(modalFrame,null,page);
    }    

    /**
     * Open an page setting modifier window, to modify the line format.
     *
     * @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 page A page to be modified.
     *
     * @return True if modified page, false otherwise.
     */
    public static boolean newPageSetting(Frame modalFrame,Component refComp,JFPage page) {
    	return showDialog(modalFrame,refComp,page);
    }    


    /**
     * 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 boolean showDialog(Component frameComp,
                                    Component locationComp,
                                    JFPage page) {
        Frame frame = JOptionPane.getFrameForComponent(frameComp);
        m_dialog = new PageSettingDialog(frame,
                                locationComp,
                                CADResource.getString("dialog.pagesetting"),
                                page);
	m_modified  =false;                                
        m_dialog.setVisible(true);
        return m_modified;
    }


    private PageSettingDialog(Frame frame,
                       Component locationComp,
                       String title,
                       JFPage page) {
        super(frame, title, true);
	setResizable(false);
        
        m_page	=page;

        //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);

        //main part of the dialog
	hidePortsCheck			=new JCheckBox(CADResource.getString("label.pagesetting.hideports"));
	hidePortsCheck.setFont(GUIConst.FONT_LABEL);

	disablePortSnappingCheck	=new JCheckBox(CADResource.getString("label.pagesetting.disableportsnapping"));
	disablePortSnappingCheck.setFont(GUIConst.FONT_LABEL);

	if (page!=null){
		hidePortsCheck.setSelected(page.isHidePorts());
		disablePortSnappingCheck.setSelected(page.isDisablePortSnapping());
	}
	
	//add checkboxes to container.
        JPanel  comboContainer	=new JPanel();
        comboContainer.setLayout(new BoxLayout(comboContainer,BoxLayout.X_AXIS));
        comboContainer.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        comboContainer.add(hidePortsCheck);
        comboContainer.add(Box.createRigidArea(new Dimension(5,0)));
        comboContainer.add(disablePortSnappingCheck);
        comboContainer.add(Box.createRigidArea(new Dimension(150,0)));

	//scale units, screen units.
    	
    	scaleText	=new JTextField(CommonUtil.f2fs(page.getScaleValue()));
    	scaleText.setPreferredSize(new Dimension(50,20));
    	scaleText.setFont(GUIConst.FONT_LABEL);
   	
    	scaleUnitsCombo	=new JComboBox(scaleUnitsItems);
    	scaleUnitsCombo.setSelectedItem(ShapeConst.getMeasureName(page.getScaleUnit()));
    	scaleUnitsCombo.setEditable(false);
    	scaleUnitsCombo.setPreferredSize(new Dimension(90,25));
    	scaleUnitsCombo.setFont(GUIConst.FONT_LABEL);

    	screenUnitsCombo =new JComboBox(screenUnitsItems);
    	screenUnitsCombo.setSelectedItem(ShapeConst.getMeasureName(page.getScreenUnit()));
    	screenUnitsCombo.setEditable(false);
    	screenUnitsCombo.setPreferredSize(new Dimension(90,25));
    	screenUnitsCombo.setFont(GUIConst.FONT_LABEL);
	
	JLabel scaleLabel  =new JLabel(CADResource.getString("label.pagesetting.scale")+":");
	scaleLabel.setFont(GUIConst.FONT_LABEL);

	JLabel equalsLabel  =new JLabel(CADResource.getString("label.pagesetting.equals"));
	equalsLabel.setFont(GUIConst.FONT_LABEL);

	JLabel screenSizeLabel  =new JLabel(CADResource.getString("label.pagesetting.screensize"));
	screenSizeLabel.setFont(GUIConst.FONT_LABEL);

	//add scale to container.
        JPanel  scaleContainer	=new JPanel();
        scaleContainer.setLayout(new BoxLayout(scaleContainer,BoxLayout.X_AXIS));
        scaleContainer.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        scaleContainer.add(scaleLabel);
        scaleContainer.add(Box.createRigidArea(new Dimension(5,0)));

        scaleContainer.add(scaleText);
        scaleContainer.add(Box.createRigidArea(new Dimension(5,0)));

        scaleContainer.add(scaleUnitsCombo);
        scaleContainer.add(Box.createRigidArea(new Dimension(5,0)));

        scaleContainer.add(equalsLabel);
        scaleContainer.add(Box.createRigidArea(new Dimension(5,0)));

        scaleContainer.add(screenUnitsCombo);
        scaleContainer.add(Box.createRigidArea(new Dimension(5,0)));

        scaleContainer.add(screenSizeLabel);
        
	
	//canvas size
	CanvasFormat canvasFormat	=page.getCanvasFormat();    	

    	canvasWidthText	=new JTextField(CommonUtil.f2fs(canvasFormat.getWidth()));
    	canvasWidthText.setPreferredSize(new Dimension(50,20));
    	canvasWidthText.setFont(GUIConst.FONT_LABEL);

    	canvasWidthUnitCombo	=new JComboBox(screenUnitsItems);
    	canvasWidthUnitCombo.setSelectedItem(ShapeConst.getMeasureName(canvasFormat.getWidthUnit()));
    	canvasWidthUnitCombo.setEditable(false);
    	canvasWidthUnitCombo.setPreferredSize(new Dimension(90,25));
    	canvasWidthUnitCombo.setFont(GUIConst.FONT_LABEL);

    	canvasHeightText	=new JTextField(CommonUtil.f2fs(canvasFormat.getHeight()));
    	canvasHeightText.setPreferredSize(new Dimension(50,20));
    	canvasHeightText.setFont(GUIConst.FONT_LABEL);

    	canvasHeightUnitCombo	=new JComboBox(screenUnitsItems);
    	canvasHeightUnitCombo.setSelectedItem(ShapeConst.getMeasureName(canvasFormat.getHeightUnit()));
    	canvasHeightUnitCombo.setEditable(false);
    	canvasHeightUnitCombo.setPreferredSize(new Dimension(90,25));
    	canvasHeightUnitCombo.setFont(GUIConst.FONT_LABEL);

	
	JLabel canvasSizeLabel  =new JLabel(CADResource.getString("label.pagesetting.canvassize"));
	canvasSizeLabel.setFont(GUIConst.FONT_LABEL);

	JLabel canvasWidthLabel  =new JLabel(CADResource.getString("label.pagesetting.width")+":");
	canvasWidthLabel.setFont(GUIConst.FONT_LABEL);

	JLabel canvasHeightLabel  =new JLabel(CADResource.getString("label.pagesetting.height")+":");
	canvasHeightLabel.setFont(GUIConst.FONT_LABEL);
		

	//add canvas size to container.
        JPanel  canvasSizeContainer	=new JPanel();
        canvasSizeContainer.setLayout(new BoxLayout(canvasSizeContainer,BoxLayout.X_AXIS));
        canvasSizeContainer.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        canvasSizeContainer.add(canvasSizeLabel);
        canvasSizeContainer.add(Box.createRigidArea(new Dimension(5,0)));

        canvasSizeContainer.add(canvasWidthLabel);
        canvasSizeContainer.add(Box.createRigidArea(new Dimension(5,0)));

        canvasSizeContainer.add(canvasWidthText);
        canvasSizeContainer.add(canvasWidthUnitCombo);
        canvasSizeContainer.add(Box.createRigidArea(new Dimension(5,0)));

        canvasSizeContainer.add(canvasHeightLabel);
        canvasSizeContainer.add(Box.createRigidArea(new Dimension(5,0)));

        canvasSizeContainer.add(canvasHeightText);
        canvasSizeContainer.add(canvasHeightUnitCombo);
	

        //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.createHorizontalGlue());
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPane.add(confirmButton);
        buttonPane.add(Box.createRigidArea(new Dimension(15, 0)));
        buttonPane.add(cancelButton);

        //Put everything together, using the content pane's BorderLayout.
        Container contentPane = getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));
        contentPane.add(comboContainer);
        contentPane.add(scaleContainer);
        contentPane.add(canvasSizeContainer);
        contentPane.add(buttonPane);
        //contentPane.add(container, 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())) {
         	if (m_page!=null){
			m_page.setHidePorts(hidePortsCheck.isSelected());
			m_page.setDisablePortSnapping(disablePortSnappingCheck.isSelected());	
			
			m_page.setScaleValue(CommonUtil.s2f(scaleText.getText()));
			m_page.setScaleUnit(ShapeConst.getMeasureTypeByName((String)scaleUnitsCombo.getSelectedItem()));
			m_page.setScreenUnit(ShapeConst.getMeasureTypeByName((String)screenUnitsCombo.getSelectedItem()));
			
			CanvasFormat canvasFormat	=m_page.getCanvasFormat();
			canvasFormat.setWidth(CommonUtil.s2f(canvasWidthText.getText()));
			canvasFormat.setWidthUnit(ShapeConst.getMeasureTypeByName((String)canvasWidthUnitCombo.getSelectedItem()));

			canvasFormat.setHeight(CommonUtil.s2f(canvasHeightText.getText()));
			canvasFormat.setHeightUnit(ShapeConst.getMeasureTypeByName((String)canvasHeightUnitCombo.getSelectedItem()));
			
         	}
         	m_modified	=true;
                m_dialog.setVisible(false);
        }else{
        	m_modified	=false;
                m_dialog.setVisible(false);
        }
    }


        
}

⌨️ 快捷键说明

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