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

📄 basicoutlookbarui.java

📁 一个demo是关于swing
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * $ $ License.
 *
 * Copyright $ L2FProd.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.l2fprod.common.swing.plaf.basic;

import com.l2fprod.common.swing.JOutlookBar;
import com.l2fprod.common.swing.PercentLayout;
import com.l2fprod.common.swing.PercentLayoutAnimator;
import com.l2fprod.common.swing.plaf.OutlookBarUI;
import com.l2fprod.common.util.JVM;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.LookAndFeel;
import javax.swing.Scrollable;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

/**
 * BasicOutlookBarUI. <br>
 *  
 */
public class BasicOutlookBarUI extends BasicTabbedPaneUI implements
  OutlookBarUI {

  public static ComponentUI createUI(JComponent c) {
    return new BasicOutlookBarUI();
  }

  private ContainerListener tabListener;
  private Map buttonToTab;
  private Map tabToButton;
  private Component nextVisibleComponent;
  private PercentLayoutAnimator animator;
  
  public JScrollPane makeScrollPane(Component component) {
    // the component is not scrollable, wraps it in a ScrollableJPanel
    JScrollPane scroll = new JScrollPane();
    scroll.setBorder(BorderFactory.createEmptyBorder());
    if (component instanceof Scrollable) {
      scroll.getViewport().setView(component);
    } else {
      scroll.getViewport().setView(new ScrollableJPanel(component));
    }
    scroll.setOpaque(false);
    scroll.getViewport().setOpaque(false);
    return scroll;
  }

  protected void installDefaults() {
    super.installDefaults();

    TabLayout layout = new TabLayout();
    tabPane.setLayout(layout);
    // ensure constraints is correct for existing components
    layout.setLayoutConstraints(tabPane);
    updateTabLayoutOrientation();

    buttonToTab = new HashMap();
    tabToButton = new HashMap();

    LookAndFeel.installBorder(tabPane, "OutlookBar.border");
    LookAndFeel.installColors(tabPane, "OutlookBar.background",
      "OutlookBar.foreground");

    tabPane.setOpaque(true);
    
    // add buttons for the current components already added in this panel
    Component[] components = tabPane.getComponents();
    for (int i = 0, c = components.length; i < c; i++) {
      tabAdded(components[i]);
    }
  }

  protected void uninstallDefaults() {
    // remove all buttons created for components
    List tabs = new ArrayList(buttonToTab.values());
    for (Iterator iter = tabs.iterator(); iter.hasNext(); ) {
      Component tab = (Component)iter.next();
      tabRemoved(tab);
    }        
    super.uninstallDefaults();    
  }
  
  protected void installListeners() {
    tabPane.addContainerListener(tabListener = createTabListener());
    super.installListeners();
  }

  protected ContainerListener createTabListener() {
    return new ContainerTabHandler();
  }

  protected PropertyChangeListener createPropertyChangeListener() {
    return new PropertyChangeHandler();
  }

  protected void uninstallListeners() {
    super.uninstallListeners();
    tabPane.removeContainerListener(tabListener);
  }

  public Rectangle getTabBounds(JTabbedPane pane, int index) {
    Component tab = pane.getComponentAt(index);
    return tab.getBounds();
  }

  public int getTabRunCount(JTabbedPane pane) {
    return 0;
  }

  public int tabForCoordinate(JTabbedPane pane, int x, int y) {
    int index = -1;
    for (int i = 0, c = pane.getTabCount(); i < c; i++) {
      if (pane.getComponentAt(i).contains(x, y)) {
        index = i;
        break;
      }
    }
    return index;
  }

  protected int indexOfComponent(Component component) {
    int index = -1;
    Component[] components = tabPane.getComponents();
    for (int i = 0; i < components.length; i++) {
      if (components[i] == component) {
        index = i;
        break;
      }
    }
    return index;
  }

  protected TabButton createTabButton() {
    TabButton button = new TabButton();
    button.setOpaque(true);
    return button;
  }

  protected void tabAdded(final Component newTab) {
    TabButton button = (TabButton)tabToButton.get(newTab);
    if (button == null) {
      button = createTabButton();
      buttonToTab.put(button, newTab);
      tabToButton.put(newTab, button);
      button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          Component current = getVisibleComponent();
          Component target = newTab;
          
          // animate the tabPane if there is a current tab selected and if the
          // tabPane allows animation
          if (((JOutlookBar)tabPane).isAnimated() && current != target
            && current != null && target != null) {
            if (animator != null) {
              animator.stop();
            }
            animator = new PercentLayoutAnimator(tabPane,
              (PercentLayout)tabPane.getLayout()) {
              public void stop() {
                super.stop();
                tabPane.setSelectedComponent(newTab);
                nextVisibleComponent = null;
              }
            };
            nextVisibleComponent = newTab;
            animator.setTargetPercent(current, 1.0f, 0.0f);
            animator.setTargetPercent(newTab, 0.0f, 1.0f);
            animator.start();
          } else {
            nextVisibleComponent = null;
            tabPane.setSelectedComponent(newTab);
          }
        }
      });
    } else {
      // the tab is already in the list, remove the button, it will be
      // added again later
      tabPane.remove(button);
    }

    // update the button with the tab information
    updateTabButtonAt(tabPane.indexOfComponent(newTab));

    int index = indexOfComponent(newTab);
    tabPane.add(button, index);

    // workaround for nullpointerexception in setRolloverTab
    // introduced by J2SE 5
    if (JVM.current().isOneDotFive()) {
      assureRectsCreated(tabPane.getTabCount());
    }
  }

  protected void tabRemoved(Component removedTab) {
    TabButton button = (TabButton)tabToButton.get(removedTab);

⌨️ 快捷键说明

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