/** Creates a new view. */ public NetView() { initComponents(); scrollPane.setLayout(new PlacardScrollPaneLayout()); scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0)); setEditor(new DefaultDrawingEditor()); undo = new UndoRedoManager(); view.setDrawing(createDrawing()); view.getDrawing().addUndoableEditListener(undo); initActions(); undo.addPropertyChangeListener( new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { setHasUnsavedChanges(undo.hasSignificantEdits()); } }); ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels"); JPanel placardPanel = new JPanel(new BorderLayout()); javax.swing.AbstractButton pButton; pButton = ButtonFactory.createZoomButton(view); pButton.putClientProperty("Quaqua.Button.style", "placard"); pButton.putClientProperty("Quaqua.Component.visualMargin", new Insets(0, 0, 0, 0)); pButton.setFont(UIManager.getFont("SmallSystemFont")); placardPanel.add(pButton, BorderLayout.WEST); toggleGridButton = pButton = ButtonFactory.createToggleGridButton(view); pButton.putClientProperty("Quaqua.Button.style", "placard"); pButton.putClientProperty("Quaqua.Component.visualMargin", new Insets(0, 0, 0, 0)); pButton.setFont(UIManager.getFont("SmallSystemFont")); labels.configureToolBarButton(pButton, "view.toggleGrid.placard"); placardPanel.add(pButton, BorderLayout.EAST); scrollPane.add(placardPanel, JScrollPane.LOWER_LEFT_CORNER); toggleGridButton.setSelected(preferences.getBoolean("view.gridVisible", false)); view.setScaleFactor(preferences.getDouble("view.scaleFactor", 1d)); view.addPropertyChangeListener( new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { String name = evt.getPropertyName(); if ("scaleFactor".equals(name)) { preferences.putDouble("view.scaleFactor", (Double) evt.getNewValue()); firePropertyChange("scaleFactor", evt.getOldValue(), evt.getNewValue()); } } }); }
public void keyReleased(KeyEvent e) { int code = e.getKeyCode(); if (code == KeyEvent.VK_SPACE) { AbstractButton button = (AbstractButton) e.getSource(); button.putClientProperty(SPACEBAR_PRESSED, Boolean.FALSE); } }
public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if (code == KeyEvent.VK_SPACE) { AbstractButton button = (AbstractButton) e.getSource(); Boolean wasPressed = (Boolean) button.getClientProperty(SPACEBAR_PRESSED); if (wasPressed == null || wasPressed.booleanValue() == false) { button.putClientProperty(SPACEBAR_PRESSED, Boolean.TRUE); button.doClick(); } } }
/** * Adds the given NamedMediaType. * * <p>Marks the 'Any Type' as selected. */ private void addMediaType(NamedMediaType type, String toolTip) { Icon icon = type.getIcon(); Icon disabledIcon = null; Icon rolloverIcon = null; AbstractButton button = new JRadioButton(type.getName()); button.putClientProperty(MEDIA, type); button.putClientProperty(SELECTED, icon); if (icon != null) { disabledIcon = ImageManipulator.darken(icon); rolloverIcon = ImageManipulator.brighten(icon); } button.putClientProperty(DESELECTED, disabledIcon); button.setIcon(disabledIcon); button.setRolloverIcon(rolloverIcon); button.addItemListener(HIGHLIGHTER); button.setBorderPainted(false); button.setFocusPainted(false); button.setContentAreaFilled(false); button.setMargin(new Insets(0, 0, 0, 0)); button.setOpaque(false); button.addMouseListener(CLICK_FORWARDER); button.setPreferredSize(new Dimension(100, 22)); if (toolTip != null) { button.setToolTipText(toolTip); } GROUP.add(button); DitherPanel panel = new DitherPanel(DITHERER); panel.setDithering(false); panel.setLayout(new FlowLayout(FlowLayout.LEFT, 7, 1)); panel.add(button); panel.addMouseListener(CLICK_FORWARDER); panel.setBackground(UIManager.getColor("TabbedPane.background")); SCHEMAS.add(panel); if (type.getMediaType() == MediaType.getAnyTypeMediaType()) button.setSelected(true); else button.setSelected(false); }
/** * Make an {@link AbstractButton} be configured in a "toolbar-like" way, for instance showing only * the icon. * * @param actionButton Button to toolbarise */ protected void toolbarizeButton(AbstractButton actionButton) { Action action = actionButton.getAction(); if (action.getValue(SHORT_DESCRIPTION) == null) action.putValue(SHORT_DESCRIPTION, action.getValue(NAME)); actionButton.setBorder(new EmptyBorder(0, 2, 0, 2)); // actionButton.setHorizontalTextPosition(JButton.CENTER); // actionButton.setVerticalTextPosition(JButton.BOTTOM); if (action.getValue(Action.SMALL_ICON) != null) { // Don't show the text actionButton.putClientProperty("hideActionText", true); // Since hideActionText seems to be broken in Java 5 and/or OS X actionButton.setText(null); } }
/** Adds a new button to this toolbar */ public Component add(AbstractButton button) { boolean substanceLaf = javax.swing.UIManager.getLookAndFeel().getName().startsWith("Substance"); // $NON-NLS-1$ if (useCustomUI && !substanceLaf) { installButtonUI(button); } super.add(button); if (substanceLaf) { button.putClientProperty("substancelaf.componentFlat", Boolean.TRUE); // $NON-NLS-1$ } else { configureButton(button); installMouseHandler(button); } return button; }
@Override public void installUI(JComponent c) { AbstractButton button = (AbstractButton) c; ButtonInfo info = new ButtonInfo(button, this); button.putClientProperty(BUTTON_INFO_KEY, info); button.addMouseListener(info.basicListener); button.addMouseMotionListener(info.basicListener); button.addFocusListener(info.basicListener); button.addPropertyChangeListener(info.basicListener); button.addChangeListener(info.basicListener); button.addKeyListener(focusArrowListener); button.addComponentListener(componentListener); button.addKeyListener(keyArmingListener); button.setRequestFocusEnabled(false); button.setFocusable(true); button.addPropertyChangeListener(positionAndShapeListener); button.setOpaque(false); button.setRolloverEnabled(true); if (button.getIcon() != null) { Font font = UIManager.getFont("IconButton.font"); if (font != null) button.setFont(font); // miniature-ish } super.installUI(c); updateLayout(button, info); if (button.getFont() == null) { Font font = UIManager.getFont("Button.font"); if (font == null) { font = new Font("Default", 0, 13); } button.setFont(font); } }
protected void updateLayout(AbstractButton button, ButtonInfo buttonInfo) { Shape customShape = (Shape) button.getClientProperty(SHAPE); int width = button.getWidth(); int height = button.getHeight(); int horizontalPosition = getHorizontalPosition(button); int verticalPosition = getVerticalPosition(button); String key = width + " " + height + " " + horizontalPosition + " " + verticalPosition; button.putClientProperty("FilledButtonUI.validationKey", key); int dx = 0; int dy = 0; if (getFocusPainting(button) == PaintFocus.OUTSIDE || getFocusPainting(button) == PaintFocus.BOTH) { if (horizontalPosition == POS_LEFT || horizontalPosition == POS_ONLY) { dx += focusSize; width -= focusSize; } if (horizontalPosition == POS_RIGHT || horizontalPosition == POS_ONLY) { width -= focusSize; } if (verticalPosition == POS_TOP || verticalPosition == POS_ONLY) { dy += focusSize; height -= focusSize; } if (verticalPosition == POS_BOTTOM || verticalPosition == POS_ONLY) { height -= focusSize; } } else { if ((verticalPosition == POS_BOTTOM || verticalPosition == POS_ONLY) && fill.getShadowHighlight(button) != null) { height--; } } ButtonInfo info = getButtonInfo(button); boolean showSeparators = isShowingSeparators(button); shape.getShape( info.fill, info.stroke, width, height, horizontalPosition, verticalPosition, showSeparators, customShape); AffineTransform translation = AffineTransform.getTranslateInstance(dx, dy); info.fill.transform(translation); info.stroke.transform(translation); Font font = button.getFont(); if (font == null) font = new Font("Default", 0, 12); FontMetrics fm = button.getFontMetrics(font); info.viewRect.x = info.viewRect.y = info.textRect.x = info.textRect.y = info.textRect.width = info.textRect.height = 0; info.iconRect.x = info.iconRect.y = info.iconRect.width = info.iconRect.height = 0; info.viewRect.width = Short.MAX_VALUE; info.viewRect.height = Short.MAX_VALUE; SwingUtilities.layoutCompoundLabel( fm, button.getText(), button.getIcon(), button.getVerticalAlignment(), button.getHorizontalAlignment(), button.getVerticalTextPosition(), button.getHorizontalTextPosition(), info.viewRect, info.iconRect, info.textRect, button.getIconTextGap()); Insets textInsets = getTextPadding(); Insets iconInsets = getIconPadding(); Rectangle tempTextRect = new Rectangle(info.textRect); Rectangle tempIconRect = new Rectangle(info.iconRect); if (info.textRect.width > 0) { tempTextRect.y -= textInsets.top; tempTextRect.x -= textInsets.left; tempTextRect.width += textInsets.left + textInsets.right; tempTextRect.height += textInsets.top + textInsets.bottom; } if (info.iconRect.width > 0) { tempIconRect.y -= iconInsets.top; tempIconRect.x -= iconInsets.left; tempIconRect.width += iconInsets.left + iconInsets.right; tempIconRect.height += iconInsets.top + iconInsets.bottom; } Rectangle sum = getSum(new Rectangle[] {tempIconRect, tempTextRect}); Insets padding = getContentInsets(button); float centerX, centerY; if (button.getHorizontalAlignment() == SwingConstants.LEFT || button.getHorizontalAlignment() == SwingConstants.LEADING) { centerX = padding.left + sum.width / 2; } else if (button.getHorizontalAlignment() == SwingConstants.RIGHT || button.getHorizontalAlignment() == SwingConstants.TRAILING) { centerX = button.getWidth() - padding.right - sum.width / 2; } else { centerX = ((button.getWidth() - padding.left - padding.right)) / 2f; } // TODO: also take into account vertical alignment: centerY = ((button.getHeight() - padding.top - padding.bottom)) / 2f; float shiftX = centerX - (sum.width) / 2f - sum.x + padding.left; float shiftY = centerY - (sum.height) / 2f - sum.y + padding.top; if (customShape == null) { if (button.getVerticalAlignment() == SwingConstants.CENTER && button.getVerticalTextPosition() == SwingConstants.CENTER && info.textRect.width > 0) { int unusedAscent = getUnusedAscent(fm, font); int ascent = fm.getAscent() - unusedAscent; shiftY = (int) (-sum.y + centerY - ascent / 2 - unusedAscent + padding.top - textInsets.top); } } info.iconRect.setFrame( info.iconRect.x + shiftX, info.iconRect.y + shiftY, info.iconRect.width, info.iconRect.height); info.textRect.setRect( (int) (info.textRect.x + shiftX + .5f), (int) (info.textRect.y + shiftY + .5f), info.textRect.width, info.textRect.height); info.updateFillBounds(); }