symbolchooser.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 910 行 · 第 1/3 页

JAVA
910
字号
// **********************************************************************// // <copyright>// //  BBN Technologies//  10 Moulton Street//  Cambridge, MA 02138//  (617) 873-8000// //  Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/tools/symbology/milStd2525/SymbolChooser.java,v $// $RCSfile: SymbolChooser.java,v $// $Revision: 1.4.2.11 $// $Date: 2006/11/14 23:04:53 $// $Author: kratkiew $// // **********************************************************************package com.bbn.openmap.tools.symbology.milStd2525;import com.bbn.openmap.event.ListenerSupport;import com.bbn.openmap.gui.DimensionQueryPanel;import com.bbn.openmap.image.AcmeGifFormatter;import com.bbn.openmap.image.BufferedImageHelper;import com.bbn.openmap.image.ImageFormatter;import com.bbn.openmap.io.FormatException;import com.bbn.openmap.omGraphics.DrawingAttributes;import com.bbn.openmap.util.ArgParser;import com.bbn.openmap.util.Debug;import com.bbn.openmap.util.FileUtils;import com.bbn.openmap.util.PaletteHelper;import javax.swing.*;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.TreePath;import javax.swing.tree.TreeSelectionModel;import java.awt.*;import java.awt.event.*;import java.awt.image.BufferedImage;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.Serializable;import java.util.*;import java.util.List;/** * The SymbolChooser is a GUI symbol builder. It can be used in stand-alone mode * to create image files, or be integrated into a java application to create * ImageIcons. * <P> * To bring up this chooser, run this class as a standalone application, or call * showDialog(..) */public class SymbolChooser extends JPanel implements ActionListener {    public final static String CREATE_IMAGE_CMD = "CREATE_IMAGE_CMD";    public final static String NAMEFIELD_CMD = "NAMEFIELD_CMD";    public final static String EMPTY_FEATURE_LIST = null;    public final static int DEFAULT_ICON_DIMENSION = 100;    public final static String EMPTY_CODE = "---------------";    protected static ImageIcon DEFAULT_SYMBOL_IMAGE;    protected DrawingAttributes drawingAttributes = new DrawingAttributes();    protected ImageIcon symbolImage;    protected DefaultMutableTreeNode currentSymbol = null;    protected SymbolTreeHolder currentSymbolTreeHolder;    protected SymbolReferenceLibrary library;    protected List trees;    protected DimensionQueryPanel dqp;    protected JButton clearFeaturesButton;    protected JButton createImageFileButton;    protected JTextField nameField;    protected JLabel symbolImageLabel;    protected JScrollPane treeView;    protected JPanel optionPanel;    protected Dimension iconDimension;    protected boolean allowCreateImage = true;    public SymbolChooser(SymbolReferenceLibrary srl) {        library = srl;        try {            trees = createNodes(srl);        } catch (FormatException fe) {            Debug.output("SymbolChooser(): Caught FormatException reading data: "                    + fe.getMessage());        }        init(srl, trees);    }    /**     * Update the GUI with the contents of the provided SymbolTreeHolder,     * reflecting a new set of symbols.     *      * @param sth     */    public void setSelectedTreeHolder(SymbolTreeHolder sth) {        treeView.setViewportView(sth.getTree());        optionPanel.removeAll();        optionPanel.add(sth.getOptionPanel());        sth.handleNodeSelection((DefaultMutableTreeNode) sth.tree.getLastSelectedPathComponent());        revalidate();    }    /**     * Convenience function to get a standard blank image for those SymbolParts     * that are not found by the SymbolImageMaker.     *      * @return DEFAULT_SYMBOL_IMAGE     */    public static ImageIcon getNotFoundImageIcon() {        if (DEFAULT_SYMBOL_IMAGE == null) {            BufferedImage bi = new BufferedImage(DEFAULT_ICON_DIMENSION, DEFAULT_ICON_DIMENSION, BufferedImage.TYPE_INT_RGB);            Graphics g = bi.getGraphics();            g.setColor(Color.LIGHT_GRAY);            g.fillRect(0, 0, DEFAULT_ICON_DIMENSION, DEFAULT_ICON_DIMENSION);            DEFAULT_SYMBOL_IMAGE = new ImageIcon(bi);        }        return DEFAULT_SYMBOL_IMAGE;    }    /**     * Create the GUI based on the contents of the SymbolReferenceLibrary and     * the SymbolPartTrees created from the options.     *      * @param srl     * @param trees     */    protected void init(SymbolReferenceLibrary srl, List trees) {        // ///////////////////        // Create the tree window by creating the scroll pane and add        // the tree to it.        GridBagLayout outergridbag = new GridBagLayout();        GridBagConstraints outerc = new GridBagConstraints();        JPanel setChoicePanel = new JPanel();        JLabel setChoiceLabel = new JLabel("Symbol Set:");        JComboBox setChoices = new JComboBox(trees.toArray());        setChoices.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                JComboBox jcb = (JComboBox) e.getSource();                setSelectedTreeHolder((SymbolTreeHolder) jcb.getSelectedItem());            }        });        currentSymbolTreeHolder = (SymbolTreeHolder) setChoices.getSelectedItem();        setChoicePanel.add(setChoiceLabel);        setChoicePanel.add(setChoices);        treeView = new JScrollPane(currentSymbolTreeHolder.getTree());        setLayout(outergridbag);        outerc.fill = GridBagConstraints.BOTH;        outerc.gridwidth = GridBagConstraints.REMAINDER;        outerc.weighty = 0.0;        outerc.insets = new Insets(5, 10, 5, 10);        outergridbag.setConstraints(setChoicePanel, outerc);        add(setChoicePanel);        outerc.weightx = 1.0;        outerc.weighty = 1.0;        outerc.gridwidth = GridBagConstraints.RELATIVE;        outergridbag.setConstraints(treeView, outerc);        add(treeView);        // Add the symbol preview area to the right of the tree        JPanel symbolPanel = PaletteHelper.createVerticalPanel(" Current Symbol ");        setImageIcon(getNotFoundImageIcon());        symbolPanel.add(symbolImageLabel);        outerc.weightx = 0.0;        outerc.gridwidth = GridBagConstraints.REMAINDER;        outergridbag.setConstraints(symbolPanel, outerc);        dqp = new DimensionQueryPanel(getDesiredIconDimension());        outergridbag.setConstraints(dqp, outerc);        symbolPanel.add(dqp);        add(symbolPanel);        // ///////////////////        optionPanel = PaletteHelper.createVerticalPanel(" Symbol Attributes ");        optionPanel.add(((SymbolTreeHolder) setChoices.getSelectedItem()).getOptionPanel());        outergridbag.setConstraints(optionPanel, outerc);        add(optionPanel);        // ///////////////////        // gridbag2 is for the name panel and the recent symbols.        GridBagLayout gridbag2 = new GridBagLayout();        GridBagConstraints c2 = new GridBagConstraints();        JPanel namePanel = new JPanel();        namePanel.setLayout(gridbag2);        c2.weightx = 0;        c2.anchor = GridBagConstraints.WEST;        JLabel nameLabel = new JLabel("Symbol Code: ");        gridbag2.setConstraints(nameLabel, c2);        namePanel.add(nameLabel);        c2.fill = GridBagConstraints.HORIZONTAL;        c2.weightx = 1.0;        if (nameField == null) {            nameField = new JTextField(EMPTY_CODE);        }        nameField.addActionListener(this);        nameField.setActionCommand(NAMEFIELD_CMD);        gridbag2.setConstraints(nameField, c2);        namePanel.add(nameField);        createImageFileButton = new JButton("Create Image File");        createImageFileButton.addActionListener(this);        createImageFileButton.setActionCommand(CREATE_IMAGE_CMD);        createImageFileButton.setEnabled(false);        createImageFileButton.setVisible(allowCreateImage);        c2.weightx = 0.0;        gridbag2.setConstraints(createImageFileButton, c2);        namePanel.add(createImageFileButton);        outerc.weighty = 0.0;        outerc.gridwidth = GridBagConstraints.REMAINDER;        outergridbag.setConstraints(namePanel, outerc);        add(namePanel);        // ///////////////////        // Just call this to make sure that the stuff in the name        // field matches the selected JTree        setSelectedTreeHolder(currentSymbolTreeHolder);    }    public void actionPerformed(ActionEvent ae) {        String command = ae.getActionCommand();        if (command == CREATE_IMAGE_CMD && library != null && nameField != null) {            try {                setDesiredIconDimension(dqp.getDimension());            } catch (NumberFormatException e) {                JOptionPane.showMessageDialog(this,                        "Width and height must be integers.",                        "Warning",                        JOptionPane.WARNING_MESSAGE);                return;            }            Dimension d = getDesiredIconDimension();            ImageIcon ii = library.getIcon(getCode(), d);            if (ii == null) {                createImageFileButton.setEnabled(false);                return;            }            try {                BufferedImage bi = BufferedImageHelper.getBufferedImage(ii.getImage(),                        0,                        0,                        (int) d.getWidth(),                        (int) d.getHeight(),                        BufferedImage.TYPE_INT_ARGB);                ImageFormatter formatter = new AcmeGifFormatter();                byte[] imageBytes = formatter.formatImage(bi);                String newFileName = FileUtils.getFilePathToSaveFromUser("Create File To Save");                if (newFileName != null) {                    FileOutputStream fos = new FileOutputStream(newFileName);                    fos.write(imageBytes);                    fos.flush();                    fos.close();                }            } catch (InterruptedException e) {                e.printStackTrace();            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }        }        if (command == NAMEFIELD_CMD) {            handleManualNameFieldUpdate(getCode());        }    }    /**     * Update the GUI to react to code typed into the string window.     *      * @param text     */    protected void handleManualNameFieldUpdate(String text) {        if (text == null)            return;        if (text.length() > 15) {

⌨️ 快捷键说明

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