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

📄 exampleapplication.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    DbfTableModel model = new DbfTableModel(1);

                    model.setDecimalCount(0, (byte) 0);
                    model.setLength(0, (byte) 10);
                    model.setColumnName(0, "Column1");
                    model.setType(0, (byte) DbfTableModel.TYPE_CHARACTER);

                    _drawableLayer.setModel(model);
                    _addShape.setEnabled(true);
                    _setModel.setEnabled(false);
                } catch (Exception exception) {
                    System.out.println(exception);
                }

            }
        });

        _addShape = new JMenuItem("Add Shape");
        _addShape.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                EsriPolylineList shapeData = new EsriPolylineList();
                ArrayList tabularData = new ArrayList();
                float[] part0 = new float[] { 35.0f, -120.0f, -25.0f, -95.0f,
                        56.0f, -30.0f };
                float[] part1 = new float[] { -15.0f, -110.0f, 13.0f, -80.0f,
                        -25.0f, 10.0f };
                EsriPolyline poly0 = new EsriPolyline(part0, OMGraphic.DECIMAL_DEGREES, OMGraphic.LINETYPE_RHUMB);
                EsriPolyline poly1 = new EsriPolyline(part1, OMGraphic.DECIMAL_DEGREES, OMGraphic.LINETYPE_RHUMB);
                shapeData.add(poly0); //part 1
                shapeData.add(poly1); //part 2
                shapeData.generate(_mapBean.getProjection());
                tabularData.add(0, "a value");
                _drawableLayer.addRecord(shapeData, tabularData);
                _drawableLayer.repaint();
            }
        });

        _openFileChooser = new JMenuItem("Add Shape File");
        _openFileChooser.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileFilter(new EsriFilter());
                int returnVal = fileChooser.showOpenDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        File shp = fileChooser.getSelectedFile();
                        String s = shp.getCanonicalPath();
                        int pos1 = s.lastIndexOf('.');
                        String name = s.substring(0, pos1);
                        File shx = new File(s.substring(0, pos1) + ".shx");
                        File dbf = new File(s.substring(0, pos1) + ".dbf");
                        EsriLayer layer = new EsriLayer(name, dbf.toURL(), shp.toURL(), shx.toURL());
                        _layerHandler.addLayer(layer);
                    } catch (Exception exception) {
                        System.out.println(exception);
                    }
                }
            }
        });

        _exit = new JMenuItem("Exit");
        _exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        _saveFileChooser = new JMenuItem("Save File");
        _saveFileChooser.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                EsriLayer layer = pickEsriLayer();
                if (layer != null) {
                    JFileChooser fileChooser = new JFileChooser();
                    fileChooser.setFileFilter(new EsriFilter());
                    int returnVal = fileChooser.showSaveDialog(null);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        try {
                            File file = fileChooser.getSelectedFile();
                            String path = file.getCanonicalPath();
                            EsriGraphicList list = layer.getEsriGraphicList();

                            ShpOutputStream pos = new ShpOutputStream(new FileOutputStream(path
                                    + ".shp"));
                            int[][] indexData = pos.writeGeometry(list);

                            ShxOutputStream xos = new ShxOutputStream(new FileOutputStream(path
                                    + ".shx"));
                            xos.writeIndex(indexData,
                                    list.getType(),
                                    list.getExtents());

                            DbfOutputStream dos = new DbfOutputStream(new FileOutputStream(path
                                    + ".dbf"));
                            dos.writeModel(layer.getModel());
                        } catch (Exception exception) {
                            System.out.println(exception);
                        }
                    }
                } else {
                    // Add a dialog
                }
            }
        });
        _fileMenu.add(_openFileChooser);
        _fileMenu.add(_saveFileChooser);
        _fileMenu.add(new JSeparator());
        _fileMenu.add(_showTable);
        _fileMenu.add(new JSeparator());
        _fileMenu.add(_httpExample);
        _fileMenu.add(new JSeparator());
        _fileMenu.add(_setModel);
        _addShape.setEnabled(false); //Disable ability for user to
                                     // add a shape until they
                                     // initialize the DbfTableModel
        _fileMenu.add(_addShape);
        _fileMenu.add(new JSeparator());
        _fileMenu.add(_exit);

        _defaultHelpMenu = new DefaultHelpMenu();

        _menuBar.add(_fileMenu);
        _menuBar.add(_layersMenu);
        _menuBar.add(_defaultHelpMenu);

        _omts.add(_mouseModePanel);
        ;
        _toolPanel.add((Tool) _omts);

        setJMenuBar(_menuBar);
        getContentPane().add(_toolPanel, BorderLayout.NORTH);
        getContentPane().add(_mapBean, BorderLayout.CENTER);
        setVisible(true);
    }

    /**
     * Displays a new window containing the tabular data for the
     * passed-in layer
     * 
     * @param layer The layer whose data is to be displayed
     */
    public void showTable(final EsriLayer layer) {
        JFrame frame = new JFrame("Table");
        DbfTableModel model = layer.getModel();
        JTable table = new JTable(model);
        JScrollPane pane = new JScrollPane(table);
        frame.getContentPane().add(pane, BorderLayout.CENTER);

        ListSelectionModel lsm = table.getSelectionModel();
        lsm.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting()) {
                    return;
                }
                ListSelectionModel lsm2 = (ListSelectionModel) e.getSource();
                if (lsm2.isSelectionEmpty()) {
                    //no rows are selected
                } else {
                    int index = lsm2.getMinSelectionIndex();
                    EsriGraphicList list = layer.getEsriGraphicList();
                    OMGraphic graphic = list.getOMGraphicAt(index);
                    graphic.select();
                    list.generate(_mapBean.getProjection());
                    layer.repaint();
                }
            }
        });
        frame.setSize(400, 300);
        frame.setVisible(true);
    }

    /**
     * Strips file extension from a string that represents a file
     * 
     * @param file The file reference from which to strip file
     *        extensions
     * @param extension The extension to check for and to strip if it
     *        exists
     * @return A string less the respective extension
     */
    private String stripExtension(String file, String extension) {
        if (file.endsWith(extension)) {
            int index = file.lastIndexOf('.');
            String s = file.substring(0, index);
            return s;
        }
        return file;
    }

    /**
     * Main method to facilitate testing and to run as stand alone
     * application.
     */
    public static void main(String args[]) {
        com.bbn.openmap.util.Debug.init();
        ExampleApplication example = new ExampleApplication();
        example.setVisible(true);
    }

    /**
     * Used by the JFileChooser component to list only files whose
     * extension ends in '.shp'.
     */
    private class EsriFilter extends javax.swing.filechooser.FileFilter {
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }

            String extension = getExtension(f);
            if (extension != null) {
                if (extension.equalsIgnoreCase("shp")) {
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }

        /**
         * Extracts the extension from the given file
         * 
         * @param f The file from whose extension to extract
         */
        private String getExtension(File f) {
            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');

            if (i > 0 && i < s.length() - 1) {
                ext = s.substring(i + 1).toLowerCase();
            }
            return ext;
        }

        /**
         * Sets the description string that will appear on the
         * JFileChooser component as its initialized
         */
        public String getDescription() {
            return "Feature Data Source";
        }
    }
}

⌨️ 快捷键说明

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