?? basictoolbarui.java
字號:
JRootPane rootPane = new JRootPane() { private boolean packing = false; public void validate() { super.validate(); if (!packing) { packing = true; pack(); packing = false; } } }; rootPane.setOpaque(true); return rootPane; } }; frame.getRootPane().setName("ToolBar.FloatingFrame"); frame.setResizable(false); WindowListener wl = createFrameListener(); frame.addWindowListener(wl); return frame; } /** * Creates a window which contains the toolbar after it has been * dragged out from its container * @return a <code>RootPaneContainer</code> object, containing the toolbar. */ protected RootPaneContainer createFloatingWindow(JToolBar toolbar) { class ToolBarDialog extends JDialog { public ToolBarDialog(Frame owner, String title, boolean modal) { super(owner, title, modal); } public ToolBarDialog(Dialog owner, String title, boolean modal) { super(owner, title, modal); } // Override createRootPane() to automatically resize // the frame when contents change protected JRootPane createRootPane() { JRootPane rootPane = new JRootPane() { private boolean packing = false; public void validate() { super.validate(); if (!packing) { packing = true; pack(); packing = false; } } }; rootPane.setOpaque(true); return rootPane; } } JDialog dialog; Window window = SwingUtilities.getWindowAncestor(toolbar); if (window instanceof Frame) { dialog = new ToolBarDialog((Frame)window, toolbar.getName(), false); } else if (window instanceof Dialog) { dialog = new ToolBarDialog((Dialog)window, toolbar.getName(), false); } else { dialog = new ToolBarDialog((Frame)null, toolbar.getName(), false); } dialog.getRootPane().setName("ToolBar.FloatingWindow"); dialog.setTitle(toolbar.getName()); dialog.setResizable(false); WindowListener wl = createFrameListener(); dialog.addWindowListener(wl); return dialog; } protected DragWindow createDragWindow(JToolBar toolbar) { Window frame = null; if(toolBar != null) { Container p; for(p = toolBar.getParent() ; p != null && !(p instanceof Window) ; p = p.getParent()); if(p != null && p instanceof Window) frame = (Window) p; } if(floatingToolBar == null) { floatingToolBar = createFloatingWindow(toolBar); } if (floatingToolBar instanceof Window) frame = (Window) floatingToolBar; DragWindow dragWindow = new DragWindow(frame); return dragWindow; } /** * Returns a flag to determine whether rollover button borders * are enabled. * * @return true if rollover borders are enabled; false otherwise * @see #setRolloverBorders * @since 1.4 */ public boolean isRolloverBorders() { return rolloverBorders; } /** * Sets the flag for enabling rollover borders on the toolbar and it will * also install the apropriate border depending on the state of the flag. * * @param rollover if true, rollover borders are installed. * Otherwise non-rollover borders are installed * @see #isRolloverBorders * @since 1.4 */ public void setRolloverBorders( boolean rollover ) { rolloverBorders = rollover; if ( rolloverBorders ) { installRolloverBorders( toolBar ); } else { installNonRolloverBorders( toolBar ); } } /** * Installs rollover borders on all the child components of the JComponent. * <p> * This is a convenience method to call <code>setBorderToRollover</code> * for each child component. * * @param c container which holds the child components (usally a JToolBar) * @see #setBorderToRollover * @since 1.4 */ protected void installRolloverBorders ( JComponent c ) { // Put rollover borders on buttons Component[] components = c.getComponents(); for ( int i = 0; i < components.length; ++i ) { if ( components[ i ] instanceof JComponent ) { ( (JComponent)components[ i ] ).updateUI(); setBorderToRollover( components[ i ] ); } } } /** * Installs non-rollover borders on all the child components of the JComponent. * A non-rollover border is the border that is installed on the child component * while it is in the toolbar. * <p> * This is a convenience method to call <code>setBorderToNonRollover</code> * for each child component. * * @param c container which holds the child components (usally a JToolBar) * @see #setBorderToNonRollover * @since 1.4 */ protected void installNonRolloverBorders ( JComponent c ) { // Put non-rollover borders on buttons. These borders reduce the margin. Component[] components = c.getComponents(); for ( int i = 0; i < components.length; ++i ) { if ( components[ i ] instanceof JComponent ) { ( (JComponent)components[ i ] ).updateUI(); setBorderToNonRollover( components[ i ] ); } } } /** * Installs normal borders on all the child components of the JComponent. * A normal border is the original border that was installed on the child * component before it was added to the toolbar. * <p> * This is a convenience method to call <code>setBorderNormal</code> * for each child component. * * @param c container which holds the child components (usally a JToolBar) * @see #setBorderToNonRollover * @since 1.4 */ protected void installNormalBorders ( JComponent c ) { // Put back the normal borders on buttons Component[] components = c.getComponents(); for ( int i = 0; i < components.length; ++i ) { setBorderToNormal( components[ i ] ); } } /** * Sets the border of the component to have a rollover border which * was created by <code>createRolloverBorder</code>. * * @param c component which will have a rollover border installed * @see #createRolloverBorder * @since 1.4 */ protected void setBorderToRollover(Component c) { if (c instanceof AbstractButton) { AbstractButton b = (AbstractButton)c; Border border = (Border)borderTable.get(b); if (border == null || border instanceof UIResource) { borderTable.put(b, b.getBorder()); } // Only set the border if its the default border if (b.getBorder() instanceof UIResource) { b.setBorder(getRolloverBorder(b)); } rolloverTable.put(b, b.isRolloverEnabled()? Boolean.TRUE: Boolean.FALSE); b.setRolloverEnabled(true); } } private Border getRolloverBorder(AbstractButton b) { Object borderProvider = UIManager.get("ToolBar.rolloverBorderProvider"); if(borderProvider == null) { return rolloverBorder; } return ((BorderProvider) borderProvider).getRolloverBorder(b); } /** * Sets the border of the component to have a non-rollover border which * was created by <code>createNonRolloverBorder</code>. * * @param c component which will have a non-rollover border installed * @see #createNonRolloverBorder * @since 1.4 */ protected void setBorderToNonRollover(Component c) { if (c instanceof AbstractButton) { AbstractButton b = (AbstractButton)c; Border border = (Border)borderTable.get(b); if (border == null || border instanceof UIResource) { borderTable.put(b, b.getBorder()); } // Only set the border if its the default border if (b.getBorder() instanceof UIResource) { if (b instanceof JToggleButton) { ((JToggleButton)b).setBorder(nonRolloverToggleBorder); } else { b.setBorder(nonRolloverBorder); } } rolloverTable.put(b, b.isRolloverEnabled()? Boolean.TRUE: Boolean.FALSE); b.setRolloverEnabled(false); } } /** * Sets the border of the component to have a normal border. * A normal border is the original border that was installed on the child * component before it was added to the toolbar. * * @param c component which will have a normal border re-installed * @see #createNonRolloverBorder * @since 1.4 */ protected void setBorderToNormal(Component c) { if (c instanceof AbstractButton) { AbstractButton b = (AbstractButton)c; Border border = (Border)borderTable.remove(b); b.setBorder(border); Boolean value = (Boolean)rolloverTable.remove(b); if (value != null) { b.setRolloverEnabled(value.booleanValue()); } } } public void setFloatingLocation(int x, int y) { floatingX = x; floatingY = y; } public boolean isFloating() { return floating; } public void setFloating(boolean b, Point p) { if (toolBar.isFloatable() == true) { if (dragWindow != null) dragWindow.setVisible(false); this.floating = b; if (b == true) { if (dockingSource == null) { dockingSource = toolBar.getParent(); dockingSource.remove(toolBar); } constraintBeforeFloating = calculateConstraint(); if ( propertyListener != null ) UIManager.addPropertyChangeListener( propertyListener ); if (floatingToolBar == null) floatingToolBar = createFloatingWindow(toolBar); floatingToolBar.getContentPane().add(toolBar,BorderLayout.CENTER); if (floatingToolBar instanceof Window) ((Window)floatingToolBar).pack(); if (floatingToolBar instanceof Window) ((Window)floatingToolBar).setLocation(floatingX, floatingY); if (floatingToolBar instanceof Window) ((Window)floatingToolBar).show(); } else { if (floatingToolBar == null) floatingToolBar = createFloatingWindow(toolBar); if (floatingToolBar instanceof Window) ((Window)floatingToolBar).setVisible(false); floatingToolBar.getContentPane().remove(toolBar); String constraint = getDockingConstraint(dockingSource, p); if (constraint == null) { constraint = BorderLayout.NORTH; } int orientation = mapConstraintToOrientation(constraint); setOrientation(orientation); if (dockingSource== null) dockingSource = toolBar.getParent(); if ( propertyListener != null ) UIManager.removePropertyChangeListener( propertyListener ); dockingSource.add(constraint, toolBar); } dockingSource.invalidate(); Container dockingSourceParent = dockingSource.getParent(); if (dockingSourceParent != null) dockingSourceParent.validate(); dockingSource.repaint(); } } private int mapConstraintToOrientation(String constraint) { int orientation = toolBar.getOrientation(); if ( constraint != null ) { if ( constraint.equals(BorderLayout.EAST) || constraint.equals(BorderLayout.WEST) ) orientation = JToolBar.VERTICAL; else if ( constraint.equals(BorderLayout.NORTH) || constraint.equals(BorderLayout.SOUTH) ) orientation = JToolBar.HORIZONTAL; } return orientation; } public void setOrientation(int orientation) { toolBar.setOrientation( orientation ); if (dragWindow !=null) dragWindow.setOrientation(orientation); } /** * Gets the color displayed when over a docking area */ public Color getDockingColor() { return dockingColor; } /** * Sets the color displayed when over a docking area */ public void setDockingColor(Color c) { this.dockingColor = c; } /** * Gets the color displayed when over a floating area */ public Color getFloatingColor() { return floatingColor; } /** * Sets the color displayed when over a floating area */ public void setFloatingColor(Color c) { this.floatingColor = c; } private boolean isBlocked(Component comp, Object constraint) { if (comp instanceof Container) { Container cont = (Container)comp; LayoutManager lm = cont.getLayout(); if (lm instanceof BorderLayout) { BorderLayout blm = (BorderLayout)lm; Component c = blm.getLayoutComponent(cont, constraint); return (c != null && c != toolBar); } } return false; } public boolean canDock(Component c, Point p) { return (p != null && getDockingConstraint(c, p) != null); } private String calculateConstraint() { String constraint = null; LayoutManager lm = dockingSource.getLayout(); if (lm instanceof BorderLayout) { constraint = (String)((BorderLayout)lm).getConstraints(toolBar); } return (constraint != null) ? constraint : constraintBeforeFloating; } private String getDockingConstraint(Component c, Point p) { if (p == null) return constraintBeforeFloating; if (c.contains(p)) { dockingSensitivity = (toolBar.getOrientation() == JToolBar.HORIZONTAL) ? toolBar.getSize().height : toolBar.getSize().width; // North (Base distance on height for now!) if (p.y < dockingSensitivity && !isBlocked(c, BorderLayout.NORTH)) { return BorderLayout.NORTH; } // East (Base distance on height for now!) if (p.x >= c.getWidth() - dockingSensitivity && !isBlocked(c, BorderLayout.EAST)) { return BorderLayout.EAST; } // West (Base distance on height for now!) if (p.x < dockingSensitivity && !isBlocked(c, BorderLayout.WEST)) { return BorderLayout.WEST; } if (p.y >= c.getHeight() - dockingSensitivity && !isBlocked(c, BorderLayout.SOUTH)) { return BorderLayout.SOUTH; } } return null; } protected void dragTo(Point position, Point origin) { if (toolBar.isFloatable() == true) { try { if (dragWindow == null) dragWindow = createDragWindow(toolBar); Point offset = dragWindow.getOffset(); if (offset == null) { Dimension size = toolBar.getPreferredSize(); offset = new Point(size.width/2, size.height/2); dragWindow.setOffset(offset); } Point global = new Point(origin.x+ position.x, origin.y+position.y); Point dragPoint = new Point(global.x- offset.x, global.y- offset.y); if (dockingSource == null)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -