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

📄 arrowdialog.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:ArrowDialog.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.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 com.jfimagine.jfgraph.shape.decorate.Arrow;
import com.jfimagine.jfdraw.gui.ToolFactory;
import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.jfdraw.gui.GUIConst;

import com.jfimagine.jfdraw.gui.GlobalSettings;


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

    private static ArrowDialog m_dialog;
    private static Arrow m_arrow;

    private JComboBox startArrowList;
    private JComboBox endArrowList;


    ImageIcon[] images;
    public static String[] arrowStrings = {
                                "none", 

                                "triangle", 
                                "triangle_empty", 
                                "trianglelines",
                                "line_inclined",
                                "line_inclined_intersect",

                                "circle_empty",
                                "circle",

                                "rectangle_empty",
                                "rectangle",

                                "diamond_empty",
                                "diamond_circle_empty",
                                "diamond_circle",

                                "trianglelines_reversed",
                                "vline_trianglelines_reversed",
                                "circle_trianglelines_reversed",

                                "circle_empty_vline",
                                
                                "vline",
                                "vline_vline",
                                "vline_vline_vline",

                                "vline_circle_empty",
                                "vline_vline_circle_empty",
                                "vline_vline_vline_circle_empty",

                                "vline_circle",
                                "vline_vline_circle",
                                "vline_vline_vline_circle",

                                "triangle_triangle",
                                "triangle_triangle_empty",

                                "trianglelines_trianglelines",
                                "vline_trianglelines",
                                "vline_trianglelines_trianglelines"
                                
                           };
                           
                           
    /**
     * Open an arrow modifier window, to input/modify the start and end arrow.
     *
     * @param modalFrame  Determines which frame the dialog depends on; it should be
     * a component in the dialog's controlling frame. 
     *
     * @param arrow initial arrow format.
     * @return A new arrow format.
     */
    public static Arrow getNewArrow(Frame modalFrame,Arrow arrow) {
    	return getNewArrow(modalFrame,null,arrow);
    }    

    /**
     * Open an arrow modifier window, to input/modify the start and end arrow.
     *
     * @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 arrow initial arrow format.
     * @return A new arrow format.
     */
    public static Arrow getNewArrow(Frame modalFrame,Component refComp,Arrow arrow) {
    	return showDialog(modalFrame,refComp,arrow);
    }    


    /**
     * 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 Arrow showDialog(Component frameComp,
                                    Component locationComp,
                                    Arrow arrow) {
        Frame frame = JOptionPane.getFrameForComponent(frameComp);
        m_dialog = new ArrowDialog(frame,
                                locationComp,
                                CADResource.getString("dialog.arrowHead"),
                                arrow);
        m_dialog.setVisible(true);
        return m_arrow;
    }


    private ArrowDialog(Frame frame,
                       Component locationComp,
                       String title,
                       Arrow arrow) {
        super(frame, title, true);
	setResizable(false);

	int	startArrow=Arrow.ARROWTYPE_NONE;
	int	endArrow=Arrow.ARROWTYPE_NONE;
	if (arrow!=null){
		startArrow	=arrow.getStartArrow();
		endArrow	=arrow.getEndArrow();
	}

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

        final JButton defaultButton = new JButton(CADResource.getString("button.defaultvalue"));
        defaultButton.setFont(GUIConst.FONT_BUTTON);
        defaultButton.setActionCommand("Default");
        defaultButton.addActionListener(this);
        

        //main part of the dialog
        
        //two labels.
        JLabel  startLabel	=new JLabel(CADResource.getString("label.arrow.startArrowHead"));
        startLabel.setFont(GUIConst.FONT_LABEL);
        startLabel.setPreferredSize(new Dimension(120, 32));

        JLabel  endLabel	=new JLabel(CADResource.getString("label.arrow.endArrowHead"));
        endLabel.setFont(GUIConst.FONT_LABEL);
        endLabel.setPreferredSize(new Dimension(120, 32));

        JPanel  labelPanel	=new JPanel();
        labelPanel.setLayout(new BoxLayout(labelPanel,BoxLayout.Y_AXIS));
        labelPanel.add(startLabel);
        labelPanel.add(Box.createRigidArea(new Dimension(0,30)));
        labelPanel.add(endLabel);
        labelPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));


        //Load the arrow images and create an array of indexes.
        Integer[] intArray = new Integer[arrowStrings.length];
        images = new ImageIcon[arrowStrings.length];
        for (int i = 0; i < arrowStrings.length; i++) {
           		intArray[i] = new Integer(i);
            		images[i] = ToolFactory.createIcon("arrow/"+arrowStrings[i] + "32");
            		if (images[i] != null) {
                		images[i].setDescription(arrowStrings[i]);
            		}
        }


        //Create the combo boxes.
        ComboBoxRenderer renderer1= new ComboBoxRenderer();
        renderer1.setPreferredSize(new Dimension(50, 32));
        startArrowList = new JComboBox(intArray);
        startArrowList.setRenderer(renderer1);
        startArrowList.setSelectedIndex(startArrow);
        startArrowList.setMaximumRowCount(5);   
        startArrowList.setPreferredSize(new Dimension(50,32));

        ComboBoxRenderer renderer2= new ComboBoxRenderer();
        renderer2.setPreferredSize(new Dimension(50, 32));
        endArrowList = new JComboBox(intArray);
        endArrowList.setRenderer(renderer2);
        endArrowList.setSelectedIndex(endArrow);
        endArrowList.setMaximumRowCount(5);
        endArrowList.setPreferredSize(new Dimension(50,32));

	
	//add combobox to container.
        JPanel  comboPanel	=new JPanel();
        comboPanel.setLayout(new BoxLayout(comboPanel,BoxLayout.Y_AXIS));
        comboPanel.add(startArrowList);
        comboPanel.add(Box.createRigidArea(new Dimension(0,20)));
        comboPanel.add(endArrowList);
        comboPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

	//add label container and combobox container to a new container(chooser).
        JPanel chooserPane = new JPanel();
        chooserPane.setLayout(new BoxLayout(chooserPane, BoxLayout.X_AXIS));
        chooserPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        chooserPane.add(Box.createHorizontalGlue());
        chooserPane.add(labelPanel);
        chooserPane.add(Box.createRigidArea(new Dimension(10, 0)));
        chooserPane.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, 10, 10, 10));
        //buttonPane.add(Box.createHorizontalGlue());
        buttonPane.add(Box.createRigidArea(new Dimension(15, 0)));
        buttonPane.add(defaultButton);
        buttonPane.add(Box.createRigidArea(new Dimension(15, 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.add(chooserPane, BorderLayout.CENTER);
        contentPane.add(buttonPane, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(locationComp);
    }


    //Handle clicks on the Set and Cancel buttons.
    public void actionPerformed(ActionEvent e) {
    	String cmd	=e.getActionCommand();
        if ("Confirm".equals(cmd) ||"Default".equals(cmd)) {
        	m_arrow		=new Arrow();
        	m_arrow.setStartArrow(startArrowList.getSelectedIndex());
        	m_arrow.setEndArrow(endArrowList.getSelectedIndex());

		if ("Default".equals(cmd)){        	
        		GlobalSettings settings	=GlobalSettings.getInstance();
        		settings.setArrow(m_arrow);
        		JOptionPane.showMessageDialog(null, CADResource.getString("message.defaultvalue.arrow"), CADResource.getString("sys.warn"), JOptionPane.ERROR_MESSAGE); 
        		
        		return;
        	}
        }else{
            	m_arrow		=null;
        }

        m_dialog.setVisible(false);
    }
    
    

    class ComboBoxRenderer extends JLabel
                           implements ListCellRenderer {

        public ComboBoxRenderer() {
            setOpaque(true);
            setHorizontalAlignment(CENTER);
            setVerticalAlignment(CENTER);
        }

        /*
         * This method finds the image and text corresponding
         * to the selected value and returns the label, set up
         * to display the text and image.
         */
        public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
            //Get the selected index. (The index param isn't
            //always valid, so just use the value.)
            int selectedIndex = ((Integer)value).intValue();

            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }

            //Set the icon and text.  If icon was null, say so.
            ImageIcon icon = images[selectedIndex];
            String arrow = arrowStrings[selectedIndex];
            setIcon(icon);
            if (icon == null) {
                setText(arrow);
                setFont(list.getFont());
            }

            return this;
        }

    }
    
        
}

⌨️ 快捷键说明

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