Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 128 additions & 127 deletions utils/src/org/opensim/swingui/ComponentTitledBorder.java
Original file line number Diff line number Diff line change
@@ -1,139 +1,140 @@
/*
* Copyright (c) 2005-2008, Stanford University
* Use of the OpenSim software in source form is permitted provided that the following
* conditions are met:
* 1. The software is used only for non-commercial research and education. It may not
* be used in relation to any commercial activity.
* 2. The software is not distributed or redistributed. Software distribution is allowed
* only through https://simtk.org/home/opensim.
* 3. Use of the OpenSim software or derivatives must be acknowledged in all publications,
* presentations, or documents describing work in which OpenSim or derivatives are used.
* 4. Credits to developers may not be removed from executables
* created from modifications of the source.
* 5. Modifications of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR BUSINESS INTERRUPTION) OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/**
* MySwing: Advanced Swing Utilites
* Copyright 2005 Santhosh Kumar T
*
* 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.
*
* This file was downloaded from https://github.com/santhosh-tekuri/MyBlog
* on 2017 September 28. At this time, the README at this link said that the
* code may be used under the LGPL or Apache License 2.0, and we choose the
* Apache License 2.0.
*
* Christopher Dembia carried over previous modifications made by Eran
* Guendelman to a previous version of this file.
*/

package org.opensim.swingui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import java.awt.event.MouseMotionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
// [2017 Sep 28] These next two lines were carried over by Christopher Dembia
// from changes that Eran Guendelman made to a previous version of this file.
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/*
* ComponentTitledBorder taken from
* http://www.javalobby.org/java/forums/t33048.html
* Copyright (C) 2005 Santhosh Kumar T
*
* Modified by Eran Guendelman
*
* <p/>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p/>
* This library 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
* Lesser General Public License for more details.
*/

public class ComponentTitledBorder implements Border, MouseListener, SwingConstants, ChangeListener {
int offset = 5;

Component comp;
JComponent container;
Rectangle rect;
Border border;

public ComponentTitledBorder(Component comp, JComponent container, Border border){
this.comp = comp;
this.container = container;
this.border = border;
container.addMouseListener(this);

public class ComponentTitledBorder implements Border, MouseListener, MouseMotionListener, SwingConstants, ChangeListener{
int offset = 5;

Component comp;
JComponent container;
Rectangle rect;
Border border;

public ComponentTitledBorder(Component comp, final JComponent container, Border border){
this.comp = comp;
this.container = container;
this.border = border;
container.addMouseListener(this);
// [2017 Sep 28] These lines were carried over by Christopher Dembia
// from changes that Eran Guendelman made to a previous version of this
// file.
if(comp instanceof AbstractButton) ((AbstractButton)comp).addChangeListener(this);
}
}

// Added this so that if you programatically change the state of the button component in this border, the border repaints (forcing the button to repaint)
// -- Eran
// [2017 Sep 28] These lines were carried over by Christopher Dembia
// from changes that Eran Guendelman made to a previous version of this
// file. Eran Guendelman's original comment:
// Added this so that if you programatically change the state of the
// button component in this border, the border repaints (forcing the
// button to repaint)
public void stateChanged(ChangeEvent evt) {
if(evt.getSource()==comp) container.repaint();
if(evt.getSource()==comp) container.repaint();
}

public boolean isBorderOpaque(){
return true;
}

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height){
Insets borderInsets = border.getBorderInsets(c);
Insets insets = getBorderInsets(c);
int temp = (insets.top-borderInsets.top)/2;
border.paintBorder(c, g, x, y+temp, width, height-temp);
Dimension size = comp.getPreferredSize();
rect = new Rectangle(offset, 0, size.width, size.height);
SwingUtilities.paintComponent(g, comp, (Container)c, rect);
}

public Insets getBorderInsets(Component c){
Dimension size = comp.getPreferredSize();
Insets insets = border.getBorderInsets(c);
insets.top = Math.max(insets.top, size.height);
return insets;
}

public boolean isInside(MouseEvent me){
return rect!=null && rect.contains(me.getX(), me.getY());
}

private void dispatchEvent(MouseEvent me){
if(rect!=null && rect.contains(me.getX(), me.getY())){
Point pt = me.getPoint();
pt.translate(-offset, 0);
comp.setBounds(rect);
comp.dispatchEvent(new MouseEvent(comp, me.getID()
, me.getWhen(), me.getModifiers()
, pt.x, pt.y, me.getClickCount()
, me.isPopupTrigger(), me.getButton()));
if(!comp.isValid())
container.repaint();
}
}

public void mouseClicked(MouseEvent me){
dispatchEvent(me);
}


public void mouseEntered(MouseEvent me){
dispatchEvent(me);
}

public void mouseExited(MouseEvent me){
dispatchEvent(me);
}

public void mousePressed(MouseEvent me){
dispatchEvent(me);
}

public void mouseReleased(MouseEvent me){
dispatchEvent(me);
}

public void mouseDragged(MouseEvent me){
dispatchEvent(me);
}

public void mouseMoved(MouseEvent me){
// [2017 Sep 28] These lines were commented out by Christopher Dembia.
// if(rect!=null && rect.contains(me.getX(), me.getY()))
// System.out.println("");
dispatchEvent(me);
}

public boolean isBorderOpaque(){
return true;
}

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height){
Insets borderInsets = border.getBorderInsets(c);
Insets insets = getBorderInsets(c);
int temp = (insets.top-borderInsets.top)/2;
border.paintBorder(c, g, x, y+temp, width, height-temp);
Dimension size = comp.getPreferredSize();
rect = new Rectangle(offset, 0, size.width, size.height);
SwingUtilities.paintComponent(g, comp, (Container)c, rect);
}

public Insets getBorderInsets(Component c){
Dimension size = comp.getPreferredSize();
Insets insets = border.getBorderInsets(c);
insets.top = Math.max(insets.top, size.height);
return insets;
}

private void dispatchEvent(MouseEvent me){
if(rect!=null && rect.contains(me.getX(), me.getY())){
Point pt = me.getPoint();
pt.translate(-offset, 0);
comp.setBounds(rect);
comp.dispatchEvent(new MouseEvent(comp, me.getID()
, me.getWhen(), me.getModifiers()
, pt.x, pt.y, me.getClickCount()
, me.isPopupTrigger(), me.getButton()));
if(!comp.isValid())
container.repaint();
}
}

public void mouseClicked(MouseEvent me){
dispatchEvent(me);
}

public void mouseEntered(MouseEvent me){
dispatchEvent(me);
}

public void mouseExited(MouseEvent me){
dispatchEvent(me);
}

public void mousePressed(MouseEvent me){
dispatchEvent(me);
}

public void mouseReleased(MouseEvent me){
dispatchEvent(me);
}
}