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

📄 editorframe.java

📁 用java实现UML建模,由高手编写..适合有一定能力的java程序员学习
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
Violet - A program for editing UML diagrams.

Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.horstmann.violet.framework;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.beans.DefaultPersistenceDelegate;
import java.beans.Encoder;
import java.beans.ExceptionListener;
import java.beans.Expression;
import java.beans.PersistenceDelegate;
import java.beans.PropertyVetoException;
import java.beans.Statement;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ResourceBundle;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import com.horstmann.violet.ArrowHead;
import com.horstmann.violet.BentStyle;
import com.horstmann.violet.LineStyle;

/**
   This desktop frame contains panes that show graphs.
*/
public class EditorFrame extends JFrame
{
   /**
      Constructs a blank frame with a desktop pane
      but no graph windows.
      @param appClassName the fully qualified app class name.
      It is expected that the resources are appClassName + "Strings"
      and appClassName + "Version" (the latter for version-specific
      resources)
   */
   public EditorFrame(Class appClass)
   {  
      String appClassName = appClass.getName();
      appResources = ResourceBundle.getBundle(appClassName + "Strings");
      appFactory = new ResourceFactory(appResources);
      versionResources = ResourceBundle.getBundle(appClassName + "Version");
      editorResources = 
         ResourceBundle.getBundle("com.horstmann.violet.framework.EditorStrings");      
      ResourceFactory factory = new ResourceFactory(editorResources);

      preferences = PreferencesService.getInstance(appClass);
      
      String laf = preferences.get("laf", null);
      if (laf != null) changeLookAndFeel(laf);

      recentFiles = new ArrayList(); 
      File lastDir = new File(".");
      if (!preferences.isWebStart())
      {
         String recent = preferences.get("recent", "");
         if (recent.length() > 0)
            recentFiles.addAll(Arrays.asList(recent.split("[|]")));
         
         if (recentFiles.size() > 0)
            lastDir = new File((String) recentFiles.get(0)).getParentFile();
      }
      fileService = FileService.getInstance(lastDir);      
      
      setTitle(appResources.getString("app.name"));
      Dimension screenSize 
         = Toolkit.getDefaultToolkit().getScreenSize();
  
      int screenWidth = (int)screenSize.getWidth();
      int screenHeight = (int)screenSize.getHeight();

      setBounds(screenWidth / 16, screenHeight / 16,
         screenWidth * 7 / 8, screenHeight * 7 / 8);

      setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
      addWindowListener(new
         WindowAdapter()
         {
            public void windowClosing(WindowEvent event)
            {
               exit();
            }
         });

      desktop = new JDesktopPane();
      setContentPane(desktop);

      defaultExtension = appResources.getString("files.extension");

      violetFilter = new ExtensionFilter(
         appResources.getString("files.name"), 
         new String[] { defaultExtension });
      exportFilter = new ExtensionFilter(
         editorResources.getString("files.image.name"), 
         editorResources.getString("files.image.extension"));

      // set up menus

      
      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);
      JMenu fileMenu = factory.createMenu("file");
      menuBar.add(fileMenu);

      newMenu = factory.createMenu("file.new");
      fileMenu.add(newMenu);

      fileMenu.add(factory.createMenuItem(
         "file.open", this, "openFile"));

      recentFilesMenu = factory.createMenu("file.recent");
      buildRecentFilesMenu();
      fileMenu.add(recentFilesMenu);
      
      JMenuItem fileSaveItem = factory.createMenuItem(
            "file.save", this, "save"); 
      fileMenu.add(fileSaveItem);
      if (fileService.isWebStart()) fileSaveItem.setEnabled(false);

      fileMenu.add(factory.createMenuItem(
         "file.save_as", this, "saveAs"));

      fileMenu.add(factory.createMenuItem(
         "file.export_image", this, "exportImage"));

      fileMenu.add(factory.createMenuItem(
         "file.print", this, "print"));

      fileMenu.add(factory.createMenuItem(
         "file.exit", this, "exit"));

            
      JMenu editMenu = factory.createMenu("edit");
      menuBar.add(editMenu);

      editMenu.add(factory.createMenuItem(
         "edit.properties", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               final GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               GraphPanel panel = frame.getGraphPanel();
               panel.editSelected();
            }
         }));

      editMenu.add(factory.createMenuItem("edit.delete", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               GraphPanel panel = frame.getGraphPanel();
               panel.removeSelected();
            }
         }));

      editMenu.add(factory.createMenuItem(
         "edit.select_next", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               GraphPanel panel = frame.getGraphPanel();
               panel.selectNext(1);
            }
         }));

      editMenu.add(factory.createMenuItem(
         "edit.select_previous", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               Graph graph = frame.getGraph();
               GraphPanel panel = frame.getGraphPanel();
               panel.selectNext(-1);
            }
         }));

      JMenu viewMenu = factory.createMenu("view");
      menuBar.add(viewMenu);

      viewMenu.add(factory.createMenuItem(
         "view.zoom_out", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               GraphPanel panel = frame.getGraphPanel();
               panel.changeZoom(-1);
            }
         }));

      viewMenu.add(factory.createMenuItem(
         "view.zoom_in", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               GraphPanel panel = frame.getGraphPanel();
               panel.changeZoom(1);
            }
         }));
      
      viewMenu.add(factory.createMenuItem(
            "view.grow_drawing_area", new
            ActionListener()
            {
               public void actionPerformed(ActionEvent event)
               {
                  GraphFrame frame 
                     = (GraphFrame) desktop.getSelectedFrame();
                  if (frame == null) return;
                  Graph g = frame.getGraph();
                  Rectangle2D bounds = g.getBounds((Graphics2D) frame.getGraphics());
                  bounds.add(frame.getGraphPanel().getBounds());
                  g.setMinBounds(new Rectangle2D.Double(0, 0, 
                        GROW_SCALE_FACTOR * bounds.getWidth(), 
                        GROW_SCALE_FACTOR * bounds.getHeight()));
                  frame.getGraphPanel().revalidate();
                  frame.repaint();
               }
            }));
      
      viewMenu.add(factory.createMenuItem(
            "view.clip_drawing_area", new
            ActionListener()
            {
               public void actionPerformed(ActionEvent event)
               {
                  GraphFrame frame 
                     = (GraphFrame) desktop.getSelectedFrame();
                  if (frame == null) return;
                  Graph g = frame.getGraph();
                  Rectangle2D bounds = g.getBounds((Graphics2D) frame.getGraphics());
                  g.setMinBounds(null); 
                  frame.getGraphPanel().revalidate();
                  frame.repaint();
               }
            }));

      viewMenu.add(factory.createMenuItem(
         "view.smaller_grid", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               GraphPanel panel = frame.getGraphPanel();
               panel.changeGridSize(-1);
            }
         }));

      viewMenu.add(factory.createMenuItem(
         "view.larger_grid", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               GraphPanel panel = frame.getGraphPanel();
               panel.changeGridSize(1);
            }
         }));

      final JCheckBoxMenuItem hideGridItem;
      viewMenu.add(hideGridItem = (JCheckBoxMenuItem) factory.createCheckBoxMenuItem(
         "view.hide_grid", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               GraphPanel panel = frame.getGraphPanel();
               JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) event.getSource();               
               panel.setHideGrid(menuItem.isSelected());
            }
         }));

      viewMenu.addMenuListener(new
            MenuListener()
            {
               public void menuSelected(MenuEvent event)
               {
                  GraphFrame frame 
                     = (GraphFrame) desktop.getSelectedFrame();
                  if (frame == null) return;
                  GraphPanel panel = frame.getGraphPanel();
                  hideGridItem.setSelected(panel.getHideGrid());
               }
               public void menuDeselected(MenuEvent event)
               {
               }
               public void menuCanceled(MenuEvent event)
               {                  
               }
            });
      
      JMenu lafMenu = factory.createMenu("view.change_laf");
      viewMenu.add(lafMenu);
      
      UIManager.LookAndFeelInfo[] infos =
         UIManager.getInstalledLookAndFeels();
      for (int i = 0; i < infos.length; i++)
      {
         final UIManager.LookAndFeelInfo info = infos[i];
         JMenuItem item = new JMenuItem(info.getName());
         lafMenu.add(item);
         item.addActionListener(new

⌨️ 快捷键说明

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