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

📄 editorframe.java

📁 用java实现UML建模,由高手编写..适合有一定能力的java程序员学习
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            ActionListener()
            {
               public void actionPerformed(ActionEvent event)
               {
                  String laf = info.getClassName();
                  changeLookAndFeel(laf);
                  preferences.put("laf", laf);
               }
            });
      }

      JMenu windowMenu = factory.createMenu("window");
      menuBar.add(windowMenu);

      windowMenu.add(factory.createMenuItem(
         "window.next", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               JInternalFrame[] frames = desktop.getAllFrames();
               for (int i = 0; i < frames.length; i++)
               {
                  if (frames[i] == desktop.getSelectedFrame())
                  {
                     i++; 
                     if (i == frames.length) i = 0;
                     try
                     {
                        frames[i].toFront();
                        frames[i].setSelected(true); 
                     }
                     catch (PropertyVetoException exception)
                     {
                     }
                     return;
                  }
               }
            }
         }));

      windowMenu.add(factory.createMenuItem(
         "window.previous", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               JInternalFrame[] frames = desktop.getAllFrames();
               for (int i = 0; i < frames.length; i++)
               {
                  if (frames[i] == desktop.getSelectedFrame())
                  {
                     if (i == 0) i = frames.length;
                     i--; 
                     try
                     {
                        frames[i].toFront();
                        frames[i].setSelected(true); 
                     }
                     catch (PropertyVetoException exception)
                     {
                     }
                     return;
                  }
               }
            }
         }));

      windowMenu.add(factory.createMenuItem(
         "window.maximize", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               try
               {
                  frame.setMaximum(true);
               }
               catch (PropertyVetoException exception)
               {
               }
            }
         }));

      windowMenu.add(factory.createMenuItem(
         "window.restore", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               try
               {
                  frame.setMaximum(false);
               }
               catch (PropertyVetoException exception)
               {
               }
            }
         }));

      windowMenu.add(factory.createMenuItem(
         "window.close", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               GraphFrame frame 
                  = (GraphFrame)desktop.getSelectedFrame();
               if (frame == null) return;
               try
               {
                  frame.setClosed(true);
               }
               catch (PropertyVetoException exception)
               {
               }
            }
         }));

      JMenu helpMenu = factory.createMenu("help");
      menuBar.add(helpMenu);

      helpMenu.add(factory.createMenuItem(
         "help.about", this, "showAboutDialog"));

      helpMenu.add(factory.createMenuItem(
         "help.license", new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               try
               {
                  BufferedReader reader 
                     = new BufferedReader(
                        new InputStreamReader(
                           getClass().getResourceAsStream(
                              "license.txt")));
                  JTextArea text = new JTextArea(10, 50);
                  String line;
                  while ((line = reader.readLine()) != null)
                  {
                     text.append(line);
                     text.append("\n");
                  }   
                  text.setCaretPosition(0);
                  text.setEditable(false);
                  JOptionPane.showInternalMessageDialog(
                     desktop, 
                     new JScrollPane(text),
                     null, 
                     JOptionPane.INFORMATION_MESSAGE);
               }
               catch (IOException exception) {}
            }
         }));
   }

   /**
    * Changes the look and feel
    * @param lafName the name of the new look and feel
    */
   private void changeLookAndFeel(String lafName)
   {
      try
      {
         UIManager.setLookAndFeel(lafName);
         SwingUtilities.updateComponentTreeUI(EditorFrame.this);
      }
      catch (ClassNotFoundException ex) {}
      catch (InstantiationException ex) {}
      catch (IllegalAccessException ex) {}
      catch (UnsupportedLookAndFeelException ex) {}
   }
   
   /**
      Adds a graph type to the File->New menu.
      @param resourceName the name of the menu item resource
      @param graphClass the class object for the graph
   */
   public void addGraphType(String resourceName,
      final Class graphClass)
   {
      newMenu.add(appFactory.createMenuItem(resourceName, new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               try
               {
                  GraphFrame frame = new GraphFrame(
                        (Graph) graphClass.newInstance());
                  addInternalFrame(frame);
               }
               catch (Exception exception)
               {
                  exception.printStackTrace();
               }
            }
         }));
   }

   /**
      Reads the command line arguments.
      @param args the command line arguments
   */
   public void readArgs(String[] args)
   {
      if (args.length == 0)
         showAboutDialog();
      else
      {
         for (int i = 0; i < args.length; i++)
         {
            open(args[i]);
         }
      }   
      setTitle();
   }
   
   /**
    * Opens a file with the given name, or switches to the frame if it is already open.
    * @param name the file name
    */
   private void open(String name)
   {
      JInternalFrame[] frames = desktop.getAllFrames();
      for (int i = 0; i < frames.length; i++)
      {
         if (frames[i] instanceof GraphFrame)
         {
            GraphFrame frame = (GraphFrame)frames[i];
            if (frame.getFileName().equals(name)) 
            {
               try
               {
                  frame.toFront();
                  frame.setSelected(true); 
               }
               catch (PropertyVetoException exception)
               {
               }
               return;
            }
         }
      }      
      
      try
      {              
         Graph graph = read(new FileInputStream(name));
         GraphFrame frame = new GraphFrame(graph);
         addInternalFrame(frame);
         frame.setFileName(name);              
      }
      catch (IOException exception)
      {
         JOptionPane.showInternalMessageDialog(desktop, 
               exception);
      }      
   }   

   /**
      Creates an internal frame on the desktop.
      @param c the component to display in the internal frame
      @param t the title of the internal frame.
   */
   private void addInternalFrame(final JInternalFrame iframe)
   {  
      iframe.setResizable(true);
      iframe.setClosable(true);
      iframe.setMaximizable(true);
      iframe.setIconifiable(true);
      int frameCount = desktop.getAllFrames().length;      
      desktop.add(iframe);
      // position frame
      int emptySpace 
         = FRAME_GAP * Math.max(ESTIMATED_FRAMES, frameCount);
      int width = Math.max(desktop.getWidth() / 2, 
            desktop.getWidth() - emptySpace);            
      int height = Math.max(desktop.getHeight() / 2, 
         desktop.getHeight() - emptySpace);

      iframe.reshape(frameCount * FRAME_GAP, 
         frameCount * FRAME_GAP, width, height);
      iframe.show();

      iframe.addInternalFrameListener(new
         InternalFrameAdapter()
         {
            public void internalFrameActivated(InternalFrameEvent event)
            {
               setTitle();
            }
            public void internalFrameDeactivated(InternalFrameEvent event)
            {
               setTitle();
            }
         });

      // select the frame--might be vetoed
      try
      {  
         iframe.setSelected(true);
      }
      catch(PropertyVetoException e)
      {
      }
   }

   /**
      Sets the frame title.
   */
   private void setTitle()
   {
      String appName = appResources.getString("app.name");
      GraphFrame frame 
         = (GraphFrame)desktop.getSelectedFrame();
      if (frame == null) 
         setTitle(appName);
      else
      {
         String fileName = frame.getFileName();
         if (fileName == null)
            setTitle(appName);
         else
            setTitle(appName + " - " + fileName);
      }
   }
   
   /**
    * Adds a file name to the "recent files" list and rebuilds the "recent files" menu. 
    * @param newFile the file name to add
    */
   private void addRecentFile(final String newFile)
   {
      recentFiles.remove(newFile);
      if (newFile == null || newFile.equals("")) return;
      recentFiles.add(0, newFile);
      buildRecentFilesMenu();
   }
   
   /**
    * Rebuilds the "recent files" menu.
    */
   private void buildRecentFilesMenu()
   {
      recentFilesMenu.removeAll();
      for (int i = 0; i < recentFiles.size(); i++)
      {
         final String file = (String) recentFiles.get(i); 
         String name = new File(file).getName();
         if (i < 10) name = i + " " + name;
         else if (i == 10) name = "0 " + name;         
         JMenuItem item = new JMenuItem(name);
         if (i < 10) item.setMnemonic((char)('0' + i));
         else if (i == 10) item.setMnemonic('0');
         recentFilesMenu.add(item);
         item.addActionListener(new
               ActionListener()
               {
                  public void actionPerformed(ActionEvent event)
                  {
                     open(file);
                  }
               });
      }      
   }

   /**
      Asks the user to open a graph file.
   */
   public void openFile()
   {  
      try
      {
         FileService.Open open = fileService.open(null, null, violetFilter);
         InputStream in = open.getInputStream();
         if (in != null)
         {      
            Graph graph = read(in);
            GraphFrame frame = new GraphFrame(graph);
            addInternalFrame(frame);
            frame.setFileName(open.getName());
            addRecentFile(open.getName());
            setTitle();

⌨️ 快捷键说明

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