📄 panelwindowcontainer.java
字号:
/* * PanelWindowContainer.java - holds dockable windows * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 2000, 2004 Slava Pestov * * 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 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 org.gjt.sp.jedit.gui;//{{{ Importsimport javax.swing.border.*;import javax.swing.plaf.metal.*;import javax.swing.*;import java.awt.event.*;import java.awt.font.*;import java.awt.geom.AffineTransform;import java.awt.*;import java.util.*;import org.gjt.sp.jedit.*;//}}}/** * A container for dockable windows. This class should never be used * directly. * @author Slava Pestov * @version $Id: PanelWindowContainer.java,v 1.76 2004/03/19 22:42:21 spestov Exp $ * @since jEdit 4.0pre1 */public class PanelWindowContainer implements DockableWindowContainer{ //{{{ PanelWindowContainer constructor public PanelWindowContainer(DockableWindowManager wm, String position, int dimension) { this.wm = wm; this.position = position; //{{{ Button box setup buttonPanel = new JPanel(new ButtonLayout()); buttonPanel.setBorder(new EmptyBorder(1,1,1,1)); closeBox = new JButton(GUIUtilities.loadIcon("closebox.gif")); closeBox.setRequestFocusEnabled(false); closeBox.setToolTipText(jEdit.getProperty("view.docking.close-tooltip")); if(OperatingSystem.isMacOSLF()) closeBox.putClientProperty("JButton.buttonType","toolbar"); closeBox.setMargin(new Insets(0,0,0,0)); closeBox.addActionListener(new ActionHandler()); menuBtn = new JButton(GUIUtilities.loadIcon("ToolbarMenu.gif")); menuBtn.setRequestFocusEnabled(false); menuBtn.setToolTipText(jEdit.getProperty("view.docking.menu-tooltip")); if(OperatingSystem.isMacOSLF()) menuBtn.putClientProperty("JButton.buttonType","toolbar"); menuBtn.setMargin(new Insets(0,0,0,0)); menuBtn.addMouseListener(new MenuMouseHandler()); buttonGroup = new ButtonGroup(); // JDK 1.4 workaround buttonGroup.add(nullButton = new JToggleButton()); //}}} dockables = new ArrayList(); buttons = new ArrayList(); dockablePanel = new DockablePanel(); this.dimension = dimension; } //}}} //{{{ register() method public void register(final DockableWindowManager.Entry entry) { dockables.add(entry); //{{{ Create button int rotation; if(position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM)) rotation = RotatedTextIcon.NONE; else if(position.equals(DockableWindowManager.LEFT)) rotation = RotatedTextIcon.CCW; else if(position.equals(DockableWindowManager.RIGHT)) rotation = RotatedTextIcon.CW; else throw new InternalError("Invalid position: " + position); JToggleButton button = new JToggleButton(); button.setMargin(new Insets(1,1,1,1)); button.setRequestFocusEnabled(false); button.setIcon(new RotatedTextIcon(rotation,button.getFont(), entry.title)); button.setActionCommand(entry.factory.name); button.addActionListener(new ActionHandler()); button.addMouseListener(new MenuMouseHandler()); if(OperatingSystem.isMacOSLF()) button.putClientProperty("JButton.buttonType","toolbar"); //}}} buttonGroup.add(button); buttons.add(button); entry.btn = button; wm.revalidate(); } //}}} //{{{ unregister() method public void unregister(DockableWindowManager.Entry entry) { if(entry.factory.name.equals(mostRecent)) mostRecent = null; buttonPanel.remove(entry.btn); buttons.remove(entry.btn); entry.btn = null; dockables.remove(entry); if(entry.win != null) dockablePanel.remove(entry.win); if(current == entry) { current = null; show(null); } else { wm.revalidate(); dockablePanel.repaint(); buttonPanel.repaint(); } } //}}} //{{{ remove() method public void remove(final DockableWindowManager.Entry entry) { if(entry.factory.name.equals(mostRecent)) mostRecent = null; if(entry.win != null) { dockablePanel.remove(entry.win); entry.win = null; } if(current == entry) { current = null; show(null); } else { wm.revalidate(); dockablePanel.repaint(); } } //}}} //{{{ showMostRecent() method public void showMostRecent() { if(dockables.size() == 0) { Toolkit.getDefaultToolkit().beep(); return; } if(mostRecent == null) { mostRecent = ((DockableWindowManager.Entry) dockables.get(0)).factory.name; } wm.showDockableWindow(mostRecent); } //}}} //{{{ show() method public void show(final DockableWindowManager.Entry entry) { if(current == entry) { if(entry != null) { if(entry.win instanceof DefaultFocusComponent) { ((DefaultFocusComponent)entry.win) .focusOnDefaultComponent(); } else { entry.win.requestDefaultFocus(); } } return; } if(entry != null) { if(current == null) { // we didn't have a component previously, so // create a border dockablePanel.setBorder(new DockBorder(position)); } mostRecent = entry.factory.name; this.current = entry; if(entry.win.getParent() != dockablePanel) dockablePanel.add(entry.factory.name,entry.win); dockablePanel.showDockable(entry.factory.name); entry.btn.setSelected(true); if(entry.win instanceof DefaultFocusComponent) { ((DefaultFocusComponent)entry.win) .focusOnDefaultComponent(); } else { entry.win.requestDefaultFocus(); } } else { current = null; nullButton.setSelected(true); // removing last component, so remove border dockablePanel.setBorder(null); wm.getView().getTextArea().requestFocus(); } wm.revalidate(); dockablePanel.repaint(); } //}}} //{{{ isVisible() method public boolean isVisible(DockableWindowManager.Entry entry) { return current == entry; } //}}} //{{{ getCurrent() method /** * Returns the name of the dockable in this container. * @since jEdit 4.2pre1 */ public String getCurrent() { if(current == null) return null; else return current.factory.name; } //}}} //{{{ getDimension() method /** * Returns the width or height (depending on position) of the dockable * window container. * @since jEdit 4.2pre1 */ public int getDimension() { return dimension; } //}}} //{{{ getDockables() method public String[] getDockables() { String[] retVal = new String[dockables.size()]; for(int i = 0; i < dockables.size(); i++) { DockableWindowManager.Entry entry = (DockableWindowManager.Entry) dockables.get(i); retVal[i] = entry.factory.name; } return retVal; } //}}} //{{{ Package-private members static final int SPLITTER_WIDTH = 10; DockablePanel dockablePanel; JPanel buttonPanel; //{{{ save() method void save() { jEdit.setIntegerProperty("view.dock." + position + ".dimension", dimension); if(current == null) jEdit.unsetProperty("view.dock." + position + ".last"); else { jEdit.setProperty("view.dock." + position + ".last", current.factory.name); } } //}}} //{{{ setDimension() method void setDimension(int dimension) { if(dimension != 0) this.dimension = dimension - SPLITTER_WIDTH; } //}}} //{{{ sortDockables() method void sortDockables() { buttonPanel.removeAll(); buttonPanel.add(closeBox); buttonPanel.add(menuBtn); Collections.sort(buttons,new DockableWindowCompare()); for(int i = 0; i < buttons.size(); i++) { buttonPanel.add((AbstractButton)buttons.get(i)); } } //}}} //{{{ getWrappedDimension() method /** * Returns the width or height of wrapped rows or columns. */ int getWrappedDimension(int dimension) { return ((ButtonLayout)buttonPanel.getLayout()) .getWrappedDimension(buttonPanel,dimension); } //}}} //}}} //{{{ Private members private DockableWindowManager wm; private String position; private JButton closeBox; private JButton menuBtn; private ButtonGroup buttonGroup; private JToggleButton nullButton; private int dimension; private ArrayList dockables; private ArrayList buttons; private DockableWindowManager.Entry current; private JPopupMenu popup; // remember the most recent dockable private String mostRecent; //}}} //{{{ Inner classes //{{{ DockableWindowCompare class static class DockableWindowCompare implements Comparator { public int compare(Object o1, Object o2) { String name1 = ((AbstractButton)o1).getActionCommand(); String name2 = ((AbstractButton)o2).getActionCommand(); return MiscUtilities.compareStrings( jEdit.getProperty(name1 + ".title",""), jEdit.getProperty(name2 + ".title",""), true); } } //}}} //{{{ ActionHandler class class ActionHandler implements ActionListener { public void actionPerformed(ActionEvent evt) { if(popup != null && popup.isVisible()) popup.setVisible(false); if(evt.getSource() == closeBox) show(null); else { if(wm.isDockableWindowVisible(evt.getActionCommand())) show(null); else wm.showDockableWindow(evt.getActionCommand()); } } } //}}} //{{{ MenuMouseHandler class class MenuMouseHandler extends MouseAdapter { public void mousePressed(MouseEvent evt) { if(popup != null && popup.isVisible()) { popup.setVisible(false); return; } Component comp = (Component)evt.getSource(); String dockable; if(comp instanceof JToggleButton) dockable = ((JToggleButton)comp).getActionCommand(); else dockable = getCurrent(); if(comp == menuBtn || GUIUtilities.isPopupTrigger(evt)) { if(dockable == null) { popup = wm.createPopupMenu(PanelWindowContainer.this,null,false); } else { popup = wm.createPopupMenu(PanelWindowContainer.this,dockable,false); } int x, y; boolean point; if(comp == menuBtn) { x = 0; y = menuBtn.getHeight(); point = false; } else { x = evt.getX(); y = evt.getY(); point = true; } GUIUtilities.showPopupMenu(popup, comp,x,y,point); } } } //}}} //{{{ DockBorder class static class DockBorder implements Border { String position; Insets insets; Color color1; Color color2; Color color3; //{{{ DockBorder constructor DockBorder(String position) { this.position = position; insets = new Insets( position.equals(DockableWindowManager.BOTTOM) ? SPLITTER_WIDTH : 0, position.equals(DockableWindowManager.RIGHT) ? SPLITTER_WIDTH : 0, position.equals(DockableWindowManager.TOP) ? SPLITTER_WIDTH : 0, position.equals(DockableWindowManager.LEFT) ? SPLITTER_WIDTH : 0); } //}}} //{{{ paintBorder() method public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { updateColors(); if(color1 == null || color2 == null || color3 == null) return; if(position.equals(DockableWindowManager.BOTTOM)) paintHorizBorder(g,x,y,width); else if(position.equals(DockableWindowManager.RIGHT)) paintVertBorder(g,x,y,height); else if(position.equals(DockableWindowManager.TOP)) { paintHorizBorder(g,x,y + height - SPLITTER_WIDTH,width); } else if(position.equals(DockableWindowManager.LEFT)) { paintVertBorder(g,x + width - SPLITTER_WIDTH,y,height); } } //}}} //{{{ getBorderInsets() method public Insets getBorderInsets(Component c) { return insets; } //}}} //{{{ isBorderOpaque() method public boolean isBorderOpaque() { return false; } //}}} //{{{ paintHorizBorder() method private void paintHorizBorder(Graphics g, int x, int y, int width) { g.setColor(color3); g.fillRect(x,y,width,SPLITTER_WIDTH); for(int i = 0; i < width / 4 - 1; i++) { g.setColor(color1); g.drawLine(x + i * 4 + 2,y + 3, x + i * 4 + 2,y + 3); g.setColor(color2); g.drawLine(x + i * 4 + 3,y + 4, x + i * 4 + 3,y + 4); g.setColor(color1); g.drawLine(x + i * 4 + 4,y + 5, x + i * 4 + 4,y + 5); g.setColor(color2); g.drawLine(x + i * 4 + 5,y + 6, x + i * 4 + 5,y + 6); } } //}}} //{{{ paintVertBorder() method private void paintVertBorder(Graphics g, int x, int y, int height) { g.setColor(color3); g.fillRect(x,y,SPLITTER_WIDTH,height); for(int i = 0; i < height / 4 - 1; i++) { g.setColor(color1); g.drawLine(x + 3,y + i * 4 + 2, x + 3,y + i * 4 + 2); g.setColor(color2); g.drawLine(x + 4,y + i * 4 + 3, x + 4,y + i * 4 + 3); g.setColor(color1); g.drawLine(x + 5,y + i * 4 + 4, x + 5,y + i * 4 + 4); g.setColor(color2); g.drawLine(x + 6,y + i * 4 + 5, x + 6,y + i * 4 + 5); } } //}}} //{{{ updateColors() method private void updateColors() { if(UIManager.getLookAndFeel() instanceof MetalLookAndFeel) { color1 = MetalLookAndFeel.getControlHighlight(); color2 = MetalLookAndFeel.getControlDarkShadow();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -