sparktabbedpane.java
来自「开源项目openfire的完整源程序」· Java 代码 · 共 620 行 · 第 1/2 页
JAVA
620 行
package org.jivesoftware.spark.component.tabbedPane;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.jivesoftware.Spark;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.ui.conferences.ConferenceUtils;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
/**
* Jive Software imlementation of a TabbedPane.
*
* @author Derek DeMoro
*/
public class SparkTabbedPane extends JPanel implements MouseListener {
private JPanel tabs;
private JPanel mainPanel;
private Window parentWindow;
private boolean closeButtonEnabled;
private Icon closeInactiveButtonIcon;
private Icon closeActiveButtonIcon;
private boolean popupAllowed;
private boolean activeButtonBold;
private Map<Component, JFrame> framesMap = new HashMap<Component, JFrame>();
private int tabPlacement = JTabbedPane.TOP;
private Color backgroundColor;
private Color borderColor;
/**
* The default Hand cursor.
*/
public static final Cursor HAND_CURSOR = new Cursor(Cursor.HAND_CURSOR);
/**
* The default Text Cursor.
*/
public static final Cursor DEFAULT_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR);
/**
* Listeners
*/
private List<SparkTabbedPaneListener> listeners = new ArrayList<SparkTabbedPaneListener>();
public SparkTabbedPane() {
createUI();
}
public SparkTabbedPane(int placement) {
this.tabPlacement = placement;
createUI();
}
private void createUI() {
setLayout(new BorderLayout());
tabs = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0)) {
public Dimension getPreferredSize() {
if (getParent() == null)
return getPreferredSize();
// calculate the preferred size based on the flow of components
FlowLayout flow = (FlowLayout)getLayout();
int w = getParent().getWidth();
int h = flow.getVgap();
int x = flow.getHgap();
int rowH = 0;
Dimension d;
Component[] comps = getComponents();
for (int i = 0; i < comps.length; i++) {
if (comps[i].isVisible()) {
d = comps[i].getPreferredSize();
if (x + d.width > w && x > flow.getHgap()) {
x = flow.getHgap();
h += rowH;
rowH = 0;
h += flow.getVgap();
}
rowH = Math.max(d.height, rowH);
x += d.width + flow.getHgap();
}
}
h += rowH;
return new Dimension(w, h);
}
};
final JPanel topPanel = new JPanel();
topPanel.setLayout(new GridBagLayout());
topPanel.setOpaque(false);
// Add Tabs panel to top of panel.
if (tabPlacement == JTabbedPane.TOP) {
topPanel.add(tabs,
new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(2, 0, 0, 0), 0, 0));
add(topPanel, BorderLayout.NORTH);
}
else {
topPanel.add(tabs,
new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 2, 0), 0, 0));
add(topPanel, BorderLayout.SOUTH);
}
// Create mainPanel
mainPanel = new JPanel(new CardLayout());
mainPanel.setBackground(Color.WHITE);
add(mainPanel, BorderLayout.CENTER);
// mainPanel.setBorder(BorderFactory.createLineBorder(Color.lightGray));
// Initialize close button
closeInactiveButtonIcon = SparkRes.getImageIcon(SparkRes.CLOSE_WHITE_X_IMAGE);
closeActiveButtonIcon = SparkRes.getImageIcon(SparkRes.CLOSE_DARK_X_IMAGE);
setOpaque(false);
tabs.setOpaque(false);
}
public SparkTab addTab(String text, Icon icon, final Component component, String tooltip) {
SparkTab tab = addTab(text, icon, component);
tab.setToolTipText(tooltip);
return tab;
}
public SparkTab addTab(String text, Icon icon, final Component component) {
final SparkTab tab = new SparkTab(icon, text);
if (getBackgroundColor() != null) {
tab.setBackgroundColor(backgroundColor);
}
if (getBorderColor() != null) {
tab.setBorderColor(borderColor);
}
tab.setTabPlacement(tabPlacement);
//tabs.add(tab, new GridBagConstraints(tabs.getComponentCount(), 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 0, 0, 0), 0, 0));
tabs.add(tab);
// Add Component to main panel
mainPanel.add(Integer.toString(component.hashCode()), component);
tab.addMouseListener(this);
boolean allowClosingThisTab = true;
LocalPreferences pref = SettingsManager.getLocalPreferences();
if (!pref.arePerisitedChatRoomsClosable()) {
allowClosingThisTab = ConferenceUtils.isChatRoomClosable(component);
}
//JOptionPane.showMessageDialog(this, "allowClosingThisTab:" + allowClosingThisTab);
// Add Close Button
if (isCloseButtonEnabled() && allowClosingThisTab) {
final JLabel closeButton = new JLabel(closeInactiveButtonIcon);
tab.addComponent(closeButton);
closeButton.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent mouseEvent) {
if (Spark.isWindows()) {
closeButton.setIcon(closeActiveButtonIcon);
}
setCursor(HAND_CURSOR);
}
public void mouseExited(MouseEvent mouseEvent) {
if (Spark.isWindows()) {
closeButton.setIcon(closeInactiveButtonIcon);
}
setCursor(DEFAULT_CURSOR);
}
public void mousePressed(MouseEvent mouseEvent) {
final SwingWorker closeTimerThread = new SwingWorker() {
public Object construct() {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
Log.error(e);
}
return true;
}
public void finished() {
close(tab, component);
}
};
closeTimerThread.start();
}
});
}
if (getSelectedIndex() == -1) {
setSelectedTab(tab);
}
fireTabAdded(tab, component, getIndex(tab));
return tab;
}
public int getSelectedIndex() {
Component[] comps = tabs.getComponents();
for (int i = 0; i < comps.length; i++) {
Component c = comps[i];
SparkTab tab = (SparkTab)c;
if (tab.isSelected()) {
return i;
}
}
return -1;
}
public void setSelectedIndex(int index) {
Component[] comps = tabs.getComponents();
if (index <= comps.length) {
SparkTab tab = (SparkTab)comps[index];
setSelectedTab(tab);
}
}
public int getTabCount() {
return tabs.getComponents().length;
}
public int indexOfComponent(Component comp) {
Component[] comps = mainPanel.getComponents();
for (int i = 0; i < comps.length; i++) {
Component c = comps[i];
if (c == comp) {
return i;
}
}
return -1;
}
public Component getComponentAt(int index) {
Component[] comps = mainPanel.getComponents();
for (int i = 0; i < comps.length; i++) {
Component c = comps[i];
if (i == index) {
return c;
}
}
return null;
}
public void removeTabAt(int index) {
SparkTab tab = getTabAt(index);
Component comp = getComponentAt(index);
close(tab, comp);
}
public SparkTab getTabAt(int index) {
Component[] comps = tabs.getComponents();
for (int i = 0; i < comps.length; i++) {
Component c = comps[i];
if (i == index) {
return (SparkTab)c;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?