previewproxybase.java

来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 2,255 行 · 第 1/5 页

JAVA
2,255
字号
    final JMenu fileMenu = new JMenu(resources.getString("menu.file.name"));
    final Character mnemonic = (Character) resources.getObject("menu.file.mnemonic");
    fileMenu.setMnemonic(mnemonic.charValue());

    final Iterator it = exportPlugIns.iterator();
    final boolean addedItem = it.hasNext();
    while (it.hasNext())
    {
      final ExportPlugin plugIn = (ExportPlugin) it.next();
      final ExportAction action = (ExportAction) pluginActions.get(plugIn);
      if (plugIn.isSeparated())
      {
        fileMenu.addSeparator();
      }
      fileMenu.add(createMenuItem(action));
    }
    if (addedItem == true)
    {
      fileMenu.addSeparator();
    }

    fileMenu.add(createMenuItem(getCloseAction()));
    return fileMenu;
  }

  /**
   * Creates and returns the navigation menu of the preview base. The actions must
   * be assigned in this method.
   *
   * @return A ready-made navigation Menu.
   */
  protected JMenu createNavigationMenu ()
  {
    final ResourceBundle resources = getResources();
    // the navigation menu ...
    final JMenu navMenu = new JMenu(resources.getString("menu.navigation.name"));
    final Character mnemonic = (Character) resources.getObject("menu.navigation.mnemonic");
    navMenu.setMnemonic(mnemonic.charValue());

    navMenu.add(createMenuItem(getGotoAction()));
    navMenu.addSeparator();
    navMenu.add(createMenuItem(getFirstPageAction()));
    navMenu.add(createMenuItem(getPreviousPageAction()));
    navMenu.add(createMenuItem(getNextPageAction()));
    navMenu.add(createMenuItem(getLastPageAction()));
    return navMenu;
  }

  /**
   * Creates and returns the zoom menu of the preview base. The actions must
   * be assigned in this method.
   *
   * @return A ready-made zoom menu.
   */
  protected JMenu createZoomMenu ()
  {
    final ResourceBundle resources = getResources();
    // the navigation menu ...
    final JMenu zoomMenu = new JMenu(resources.getString("menu.zoom.name"));
    final Character mnemonic = (Character) resources.getObject("menu.zoom.mnemonic");
    zoomMenu.setMnemonic(mnemonic.charValue());

    zoomMenu.add(createMenuItem(getZoomInAction()));
    zoomMenu.add(createMenuItem(getZoomOutAction()));
    zoomMenu.add(new JSeparator());

    final DowngradeActionMap zoomActionMap = getZoomActionMap();
    for (int i = 0; i < ZOOM_FACTORS.length; i++)
    {
      final Action action = new ZoomSetAction(i);
      zoomActionMap.put(action.getValue(Action.NAME), action);
      zoomMenu.add(createMenuItem(action));
    }
    return zoomMenu;
  }

  /**
   * Creates and returns the help menu of the preview base. The actions must
   * be assigned in this method.
   *
   * @return A ready-made help menu.
   */
  protected JMenu createHelpMenu ()
  {
    final ResourceBundle resources = getResources();
    // then the help menu
    final JMenu helpMenu = new JMenu(resources.getString("menu.help.name"));
    final Character mnemonic = (Character) resources.getObject("menu.help.mnemonic");
    helpMenu.setMnemonic(mnemonic.charValue());
    helpMenu.add(createMenuItem(getAboutAction()));
    return helpMenu;
  }

  /**
   * Creates a button using the given action properties for the button's initialisation.
   *
   * @param action  the action used to set up the button.
   *
   * @return a button based on the supplied action.
   */
  protected JButton createButton(final Action action)
  {
    final JButton button = new ActionButton(action);
    if (isLargeIconsEnabled())
    {
      final Icon icon = (Icon) action.getValue("ICON24");
      if (icon != null)
      {
        button.setIcon(icon);
      }
    }
    button.setMargin(new Insets(0, 0, 0, 0));
    button.setText(null);
    FloatingButtonEnabler.getInstance().addButton(button);
    return button;
  }

  /**
   * Creates a menu item based on the supplied action.
   *
   * @param action  the action.
   *
   * @return the menu item.
   */
  protected JMenuItem createMenuItem(final Action action)
  {
    final JMenuItem menuItem = new ActionMenuItem(action);
    final KeyStroke accelerator = (KeyStroke) action.getValue(ActionDowngrade.ACCELERATOR_KEY);
    if (accelerator != null)
    {
      menuItem.setAccelerator(accelerator);
    }
    return menuItem;
  }

  /**
   * Returns the toolbar used in this component. If the toolbar is disabled
   * in the global report configuration, then <code>null</code> is returned.
   *
   * @return the toolbar of this component or null
   */
  protected final JToolBar getToolbar()
  {
    return toolbar;
  }

  /**
   * Creates and returns a toolbar containing controls for print, page forward and backward, zoom
   * in and out, and an about box.
   */
  protected void initializeToolBar()
  {
    // Maybe the toolbar is disabled, then we don't want to touch it.
    if (toolbar == null)
    {
      return;
    }

    final Iterator it = exportPlugIns.iterator();
    final boolean addedItem = it.hasNext();
    while (it.hasNext())
    {
      final ExportPlugin plugIn = (ExportPlugin) it.next();
      if (plugIn.isAddToToolbar() == false)
      {
        continue;
      }
      final ExportAction action = (ExportAction) pluginActions.get(plugIn);
      if (plugIn.isSeparated())
      {
        toolbar.addSeparator();
      }
      toolbar.add(createButton(action));
    }
    if (addedItem == true)
    {
      toolbar.addSeparator();
    }
    if (isToolbarActionEnabled(ACTION_NAVIGATION_PROPERTY))
    {
      toolbar.add(createButton(getFirstPageAction()));
      toolbar.add(createButton(getPreviousPageAction()));
      toolbar.add(createButton(getNextPageAction()));
      toolbar.add(createButton(getLastPageAction()));
      toolbar.addSeparator();
    }
    if (isToolbarActionEnabled(ACTION_ZOOM_PROPERTY))
    {
      toolbar.add(createButton(getZoomInAction()));
      toolbar.add(createButton(getZoomOutAction()));
      toolbar.addSeparator();
      toolbar.add(createZoomPane());
      toolbar.addSeparator();
    }
    if (isToolbarActionEnabled(ACTION_ABOUT_PROPERTY))
    {
      toolbar.add(createButton(getAboutAction()));
    }
  }

  /**
   * Returns true, if the toolbar is floatable, false otherwise.
   *
   * @return true when the toolbar is floatable.
   */
  public boolean isToolbarFloatable()
  {
    return toolbarFloatable;
  }

  /**
   * Defines whether the toolbar is floatable.
   *
   * @param b  a flag that indicates whether or not the toolbar is floatable.
   */
  public void setToolbarFloatable(final boolean b)
  {
    final boolean oldValue = this.toolbarFloatable;
    this.toolbarFloatable = b;
    firePropertyChange("toolbarFloatable", oldValue, b);
  }

  protected JComboBox createZoomSelector ()
  {
    final DefaultComboBoxModel model = new DefaultComboBoxModel();
    for (int i = 0; i < ZOOM_FACTORS.length; i++)
    {
      model.addElement(String.valueOf((int) (ZOOM_FACTORS[i] * 100)) + " %");
    }
    JComboBox zoomSelect = new JComboBox(model);
    zoomSelect.setActionCommand("ZoomSelect");
    zoomSelect.setSelectedIndex(DEFAULT_ZOOM_INDEX);
    zoomSelect.addActionListener(createZoomSelectAction());
    zoomSelect.setAlignmentX(Component.RIGHT_ALIGNMENT);
    return zoomSelect;
  }

  /**
   * Creates a panel containing a combobox with available zoom-values.
   *
   * @return a panel containing a combobox with zoom values.
   */
  protected JComponent createZoomPane()
  {
    final JPanel zoomPane = new JPanel();
    zoomPane.setLayout(new FlowLayout(FlowLayout.LEFT));
    zoomPane.add(zoomSelect);

    return zoomPane;
  }

  /**
   * Updates the states of all buttons to reflect the state of the assigned ReportPane.
   */
  protected void validateButtons()
  {
    if (lockInterface == true)
    {
      // do not reenable any buttons.
      // but make sure they remain locked.
      disableButtons();
      return;
    }

    final int pn = reportPane.getPageNumber();
    final int mp = reportPane.getNumberOfPages();

    getLastPageAction().setEnabled(pn < mp);
    getNextPageAction().setEnabled(pn < mp);

    // no previous page, so dont enable
    getPreviousPageAction().setEnabled(pn > 1);
    getFirstPageAction().setEnabled(pn > 1);
    // no goto, if there is only one page
    getGotoAction().setEnabled(mp > 1);

    final Iterator it = exportPlugIns.iterator();
    while (it.hasNext())
    {
      final ExportPlugin ep = (ExportPlugin) it.next();
      final ExportAction ea = (ExportAction) pluginActions.get(ep);
      if (ep.isControlPlugin() == false)
      {
        ea.setEnabled(mp > 0);
      }
    }

    zoomSelect.setEnabled(true);
    final Object[] keys = zoomActionMap.keys();
    for (int i = 0; i < keys.length; i++)
    {
      zoomActionMap.get(keys[i]).setEnabled(true);
    }
    getZoomOutAction().setEnabled(zoomSelect.getSelectedIndex() != 0);
    getZoomInAction().setEnabled(zoomSelect.getSelectedIndex() != (ZOOM_FACTORS.length - 1));
  }

  /**
   * Returns the zoom selection combobox. Use this to enable or diable
   * it, but dont modify it, or be doomed.
   * @return the zoom selection combobox.
   */
  protected JComboBox getZoomSelect()
  {
    return zoomSelect;
  }

  /**
   * Disables the buttons.
   */
  protected void disableButtons()
  {
    getGotoAction().setEnabled(false);
    getLastPageAction().setEnabled(false);
    getNextPageAction().setEnabled(false);
    getPreviousPageAction().setEnabled(false);
    getFirstPageAction().setEnabled(false);
    final Object[] keys = zoomActionMap.keys();
    for (int i = 0; i < keys.length; i++)
    {
      zoomActionMap.get(keys[i]).setEnabled(false);
    }
    zoomSelect.setEnabled(false);

    final Iterator it = pluginActions.values().iterator();
    while (it.hasNext())
    {
      final ExportAction ea = (ExportAction) it.next();
      ea.setEnabled(false);
    }
  }

  /**
   * Returns the export action map containing all export related actions.
   * This map contains the actions for the export plugins.
   *
   * @return the export action map.
   */
  public DowngradeActionMap getExportActionMap()
  {
    return exportActionMap;
  }

  /**
   * Returns the base action map containing all basic actions.
   * This map contains such actions like the Close-Action or the
   * About-Action.
   *
   * @return the export action map.
   */
  public DowngradeActionMap getBaseActionMap()
  {
    return baseActionMap;
  }

  /**
   * Returns the navigation action map containing all navigation related
   * actions. This map contains the various "Goto..." actions.
   *
   * @return the export action map.
   */
  public DowngradeActionMap getNavigationActionMap()
  {
    return navigationActionMap;
  }

  /**
   * Returns the zoom action map containing all zoom related actions.
   * This map contains actions controling the zoom level of the report pane.
   *
   * @return the export action map.
   */
  public DowngradeActionMap getZoomActionMap()
  {
    return zoomActionMap;
  }

  /**
   * Returns the repagination report progress dialog.
   * @return the repaginiation progress dialog.
   */
  protected ReportProgressDialog getProgressDialog()
  {
    return progressDialog;
  }

  /**
   * Returns true if large icons are enabled for the toolbar.
   *
   * @return true if large icons are enabled.
   */
  public boolean isLargeIconsEnabled()
  {
    return largeIconsEnabled;
  }

  /**
   * Sets a flag that controls whether or not large icons are used in the toolbar.
   *
   * @param b  the new value of the flag.
   */
  public void setLargeIconsEnabled(final boolean b)
  {
    final boolean oldValue = largeIconsEnabled;
    largeIconsEnabled = b;
    firePropertyChange(LARGE_ICONS_PROPERTY, oldValue, b);
  }

  /**
   * Disposes the preview frame.
   */
  public void dispose()
  {
    freeResources();
    // Silly Swing keeps at least one reference in the RepaintManager to support DoubleBuffering
    // I dont want this here, as PreviewFrames are evil and resource expensive ...

    // I hope this helps as well ...
    // Setting the repaint manager to null is invalid, as the silly swing
    // seems to loose all update requests.
    //
    // So we have to choose beween a memory leak or an invalid repaint operation
    RepaintManager.currentManager(this).removeInvalidComponent(this);
    RepaintManager.currentManager(this).markCompletelyClean(this);
  }

  /**
   * Performs a minor dispose operation and interrupts the repagination
   * worker.
   */
  protected final void freeResources ()
  {
    try
    {
      // make sure that the pagination worker does no longer waste our time.
      // this won't kill the export worker ...
      repaginationWorker.interrupt();
    }
    catch (SecurityException se)
    {
      // ignore
    }
    if (progressDialog.isVisible())
    {
      progressDialog.setVisible(false);
      progressDialog.dispose();
    }
    // cleanup the report pane, removes some cached resources ...
    reportPane.dispose();
  }

⌨️ 快捷键说明

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