⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridformatdialog.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:GridFormatDialog.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.GraphicsEnvironment;
import java.awt.Font;

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

import com.jfimagine.jfgraph.shape.decorate.GridFormat;
import com.jfimagine.jfdraw.gui.ToolFactory;
import com.jfimagine.jfdraw.gui.resource.CADResource;   
import com.jfimagine.utils.commonutil.CommonUtil;
import com.jfimagine.jfdraw.gui.GUIConst;

 /**
 * GridFormatDialog class.  A class used to get a new grid format in gui by user.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
public class GridFormatDialog extends JDialog
                        implements ActionListener {
     

    private static String[] gridTypeAry = {
		CADResource.getString("label.gridformat.gridtype.none"),
		CADResource.getString("label.gridformat.gridtype.line"),
		CADResource.getString("label.gridformat.gridtype.cross"),
		CADResource.getString("label.gridformat.gridtype.dot")
		};

    private static String[] sizeTypeAry = {
    		CADResource.getString("label.gridformat.sizetype.inch"),
    		CADResource.getString("label.gridformat.sizetype.cm"),
    		CADResource.getString("label.gridformat.sizetype.mm")
    		};

    private static GridFormatDialog m_dialog;
    private static GridFormat m_gridFormat;

    private JComboBox gridTypeList;

    private JTextField gridSizeText;
    private JComboBox gridSizeTypeList;

    private JCheckBox snapToGridCheck;

    private JTextField snapSizeText;
    private JComboBox snapSizeTypeList;

                           
    /**
     * Open an grid format modifier window, to modify the grid format.
     *
     * @param modalFrame  Determines which frame the dialog depends on; it should be
     * a component in the dialog's controlling frame. 
     *
     * @param gridFormat initial grid format.
     * @return The grid format.
     */
    public static GridFormat getNewGridFormat(Frame modalFrame,GridFormat gridFormat) {
    	return getNewGridFormat(modalFrame,null,gridFormat);
    }    

    /**
     * Open an grid format modifier window, to modify the grid 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 gridFormat initial grid format.
     *
     * @return The grid format.
     */
    public static GridFormat getNewGridFormat(Frame modalFrame,Component refComp,GridFormat gridFormat) {
    	return showDialog(modalFrame,refComp,gridFormat);
    }    


    /**
     * 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 GridFormat showDialog(Component frameComp,
                                    Component locationComp,
                                    GridFormat gridFormat) {
        Frame frame = JOptionPane.getFrameForComponent(frameComp);
        m_dialog = new GridFormatDialog(frame,
                                locationComp,
                                CADResource.getString("dialog.gridFormat"),
                                gridFormat);
        m_dialog.setVisible(true);
        return m_gridFormat;
    }


    private GridFormatDialog(Frame frame,
                       Component locationComp,
                       String title,
                       GridFormat gridFormat) {
        super(frame, title, true);
	setResizable(false);
        
        //initiate initial grid format and this grid format.
        if (gridFormat==null)
                gridFormat      =new GridFormat();
        m_gridFormat  =new GridFormat();

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

        /************************************************
         *   grid type, size, combobox
         ************************************************/
        JLabel  gridTypeLabel	=new JLabel(CADResource.getString("label.grid.gridType"));
        gridTypeLabel.setFont(GUIConst.FONT_LABEL);
        gridTypeLabel.setPreferredSize(new Dimension(140, 32));

        JLabel  gridSizeLabel	=new JLabel(CADResource.getString("label.grid.gridSize"));
        gridSizeLabel.setFont(GUIConst.FONT_LABEL);
        gridSizeLabel.setPreferredSize(new Dimension(140, 32));


        gridTypeList 		=new JComboBox(gridTypeAry);
        gridTypeList.setFont(GUIConst.FONT_LABEL);
        gridTypeList.setSelectedIndex(gridFormat.getGridType());
        gridTypeList.setMaximumRowCount(gridTypeAry.length);
        gridTypeList.setPreferredSize(new Dimension(110, 25));
       

    	gridSizeText		=new JTextField();
        gridSizeText.setFont(GUIConst.FONT_LABEL);
    	gridSizeText.setText(CommonUtil.f2fs(gridFormat.getGridSize()));
        gridSizeText.setPreferredSize(new Dimension(50, 25));
        
        gridSizeTypeList 	=new JComboBox(sizeTypeAry);
        gridSizeTypeList.setFont(GUIConst.FONT_LABEL);
        gridSizeTypeList.setSelectedIndex(gridFormat.getGridSizeType());
        gridSizeTypeList.setMaximumRowCount(sizeTypeAry.length);
        gridSizeTypeList.setPreferredSize(new Dimension(60, 25));

        JPanel  gridSizePanel	=new JPanel();
        gridSizePanel.setLayout(new BoxLayout(gridSizePanel,BoxLayout.X_AXIS));
        gridSizePanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        gridSizePanel.add(gridSizeText);
        gridSizePanel.add(gridSizeTypeList);

        //-----------------------------	
	snapToGridCheck		=new JCheckBox(CADResource.getString("label.grid.snapToGrid"));
        snapToGridCheck.setFont(GUIConst.FONT_LABEL);
	snapToGridCheck.setSelected(gridFormat.getSnapToGrid());

        JLabel  snapSizeLabel	=new JLabel(CADResource.getString("label.grid.snapSize"));
        snapSizeLabel.setFont(GUIConst.FONT_LABEL);
        snapSizeLabel.setPreferredSize(new Dimension(140, 32));

    	snapSizeText		=new JTextField();
        snapSizeText.setFont(GUIConst.FONT_LABEL);
    	snapSizeText.setText(CommonUtil.f2fs(gridFormat.getSnapSize()));
        snapSizeText.setPreferredSize(new Dimension(50, 25));

        snapSizeTypeList 	=new JComboBox(sizeTypeAry);
        snapSizeTypeList.setFont(GUIConst.FONT_LABEL);
        snapSizeTypeList.setSelectedIndex(gridFormat.getSnapSizeType());
        snapSizeTypeList.setMaximumRowCount(sizeTypeAry.length);
        snapSizeTypeList.setPreferredSize(new Dimension(60, 25));

        JPanel  snapSizePanel	=new JPanel();
        snapSizePanel.setLayout(new BoxLayout(snapSizePanel,BoxLayout.X_AXIS));
        snapSizePanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        snapSizePanel.add(snapSizeText);
        snapSizePanel.add(snapSizeTypeList);
	

	/*******************************************************
	 *  add labels to a label container.
	 ******************************************************/
        JPanel  labelPanel	=new JPanel();
        labelPanel.setLayout(new BoxLayout(labelPanel,BoxLayout.Y_AXIS));
        labelPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

        labelPanel.add(gridTypeLabel);
        labelPanel.add(Box.createRigidArea(new Dimension(0,30)));
        labelPanel.add(gridSizeLabel);
        labelPanel.add(Box.createRigidArea(new Dimension(0,30)));
        labelPanel.add(snapToGridCheck);
        labelPanel.add(Box.createRigidArea(new Dimension(0,30)));
        labelPanel.add(snapSizeLabel);


	/*******************************************************
	 * add combobox and size panels to  container.
	 ******************************************************/
        JPanel  comboPanel	=new JPanel();
        comboPanel.setLayout(new BoxLayout(comboPanel,BoxLayout.Y_AXIS));
        comboPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));
        comboPanel.add(Box.createRigidArea(new Dimension(0,25)));
        comboPanel.add(gridTypeList);
        comboPanel.add(Box.createRigidArea(new Dimension(0,25)));
        comboPanel.add(gridSizePanel);
        comboPanel.add(Box.createRigidArea(new Dimension(0,25)));
        JPanel emptyPanel	=new JPanel();
        emptyPanel.setPreferredSize(new Dimension(110,25));
        comboPanel.add(emptyPanel);
        comboPanel.add(Box.createRigidArea(new Dimension(0,25)));
        comboPanel.add(snapSizePanel);
        comboPanel.add(Box.createRigidArea(new Dimension(0,25)));

	/*******************************************************
	 * add labels and top comboboxes to a top panel container.
	 ******************************************************/
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
        topPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
        topPanel.add(Box.createHorizontalGlue());
        topPanel.add(labelPanel);
        topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        topPanel.add(comboPanel);


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

        //Put everything together
        Container contentPane = getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        //contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
	contentPane.add(topPanel);
        contentPane.add(Box.createRigidArea(new Dimension(0,5)));
	contentPane.add(buttonPane);


        pack();
        setLocationRelativeTo(locationComp);
    }


    //Handle clicks on the Set and Cancel buttons.
    public void actionPerformed(ActionEvent e) {
         if ("Confirm".equals(e.getActionCommand())) {
                m_gridFormat.setGridType(gridTypeList.getSelectedIndex());
                m_gridFormat.setGridSize(CommonUtil.s2f(gridSizeText.getText()));
                m_gridFormat.setGridSizeType(gridSizeTypeList.getSelectedIndex());
                
                m_gridFormat.setSnapToGrid(snapToGridCheck.isSelected());
                m_gridFormat.setSnapSize(CommonUtil.s2f(snapSizeText.getText()));
                m_gridFormat.setSnapSizeType(snapSizeTypeList.getSelectedIndex());
               
                m_dialog.setVisible(false);
        }else{
           	m_gridFormat    =null;
                m_dialog.setVisible(false);
            	
        }
    }
    
        
}

⌨️ 快捷键说明

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