All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.globalmentor.swing.ButtonContentPanel Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 1996-2009 GlobalMentor, Inc. 
 *
 * 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.globalmentor.swing;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A content panel that, besides the content component, contains a button. The button is connected to the content component; whenever the the content component
 * is clicked, the button is activated.
 * 

* The button is placed in the border of the component. *

* @author Garret Wilson */ public class ButtonContentPanel extends ContentPanel { /** The panel that allows a button and a label in the border. */ private final ContentPanel buttonPanel; /** @return The panel that allows a button and a label in the border. */ protected ContentPanel getButtonPanel() { return buttonPanel; } /** * The object that listens for mouse events on the content component and in response clicks the button. */ private MouseListener mouseListener = null; /** * @return The object that listens for mouse events on the content component and in response clicks the button. */ protected MouseListener getMouseListener() { if(mouseListener == null) { //if we haven't yet created a mouse listener (we must create it here, because this method is called from the constructor before the variable would be initialized) mouseListener = new MouseAdapter() { //create a mouse listener to listen for mouse clicks on the component public void mouseClicked(final MouseEvent mouseEvent) { //TODO test, maybe getButton().dispatchEvent(mouseEvent) getButton().doClick(); //TODO testing getButton().requestFocusInWindow(); //request focus for the button } }; } return mouseListener; //return the mouse listener } /** The button to display in the border of the panel. */ public AbstractButton getButton() { return (AbstractButton)getButtonPanel().getContentComponent(); } /** * Sets the button to display in the border of the panel. * @param button The button to connect to the content component and display in the border of the panel. */ public void setButton(final AbstractButton button) { getButtonPanel().setContentComponent(button); //put the button in the button panel } /** The label to display in the border of the panel. */ private JLabel label = null; /** The label to display in the border of the panel. */ public JLabel getLabel() { return label; } /** * Sets the label to display in the border of the panel. * @param newLabel The label to display in the border of the panel by the button. */ public void setLabel(final JLabel newLabel) { if(label != newLabel) { //if the label is really changing if(label != null) { //if we already have a button label.removeMouseListener(getMouseListener()); //remove the mouse listener from the old label remove(label); //remove the current label } label = newLabel; //store the label if(newLabel != null) { //if we were given a new label newLabel.addMouseListener(getMouseListener()); //listen for mouse clicks on the component getButtonPanel().add(newLabel, BorderLayout.EAST); //put the label in the button panel TODO i18n } } } /** * Sets the main content component in the center of the panel. *

* This version adds a listener for clicks to the content component, and in response selects the button. *

* @param newContentComponent The new component for the center of the panel, or null for no content component. */ public void setContentComponent(final Component newContentComponent) { final Component oldContentComponent = getContentComponent(); //get the current content component super.setContentComponent(newContentComponent); //set the content component normally if(oldContentComponent != newContentComponent) { //if the content component really changed //TODO fix for content components inside content components if(oldContentComponent != null) { //if we had a content component earlier oldContentComponent.removeMouseListener(getMouseListener()); //remove the mouse listener from the old component } if(newContentComponent != null) { //if we were given a new content component newContentComponent.addMouseListener(getMouseListener()); //listen for mouse clicks on the component } } } /** * Content component and button constructor. *

* The content component is guaranteed to be set before initializeUI is called. *

* @param contentComponent The new component for the center of the panel. * @param button The button to connect to the content component and display in the border of the panel. */ public ButtonContentPanel(final Component contentComponent, final AbstractButton button) { this(contentComponent, button, true); //create and initialize the component } /** * Initialize constructor. * @param contentComponent The new component for the center of the panel. * @param button The button to connect to the content component and display in the border of the panel. * @param initialize true if the panel should initialize itself by calling the initialization methods. */ public ButtonContentPanel(final Component contentComponent, final AbstractButton button, final boolean initialize) { super(contentComponent, false); //construct the panel with the content component, but don't initialize buttonPanel = new ContentPanel(); //create a new content panel for the border buttonPanel.setOpaque(false); //make the panel transparent---it's only there for layout add(buttonPanel, BorderLayout.WEST); //put the button panel in the border TODO i18n setButton(button); //set the button if(initialize) //if we should initialize initialize(); //initialize the panel } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy