This new version, allows to move, resize and select buttons.
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* The <code>bArray</code> class is designed to manage selection, movement and
* resizement of a <code>JButton</code> collection.
*
* @author Javier Marrero 羖varez (totu)
* @version 0.1, 03/05/07
*/
public class bArray {
/**
* The bidimensional <code>JComponent</code> <i>array</y> to store each <code>JButton</code> and their JLabels.
*/
private JComponent[][] array;
/**
* <code>lastElement</i> indicates index of last element introduced. If it's set to -1, it indicates <code>array</code> it's clear.
*/
private int lastElement;
/**
* <code>selectionArray</code> contains selected JButtons.
*/
private Vector selectionArray;
/**
* <code>lastX</code> contains last x mouse position.
*/
private int lastX;
/**
* <code>lastY</code> contains last y mouse position.
*/
private int lastY;
/**
* Constructs a bArray of the specified size.
* <i>lastElement</i> it's initialized to -1.
* @param size Capacity of array.
*/
public bArray(int size) {
array= new JComponent[size][5];
lastElement= -1;
selectionArray = new Vector(10, 5);
}
/**
* Constructs a bArray of size 10.
* <code>lastElement</code> it's initialized to -1.
*/
public bArray() {
this(10);
}
/**
* Returns the index of selected JButton.
* It may return -1 in case JButton it's not found.
*
* @param jb the selected JButton to be found on current bArray.
* @return the index of selected JButton.
*/
public int containsButton(JButton jb) {
int i;
for (i = 0; i <= lastElement; i++) {
if (array[i][0] == jb) {
return i;
}
}
return -1;
}
/**
* Returns the associated resize dots of the specified index.
*
* @param index the selected array index to search.
* @return a JLabel array containing the 4 JLabels in selected index.
* @exception invalidArrayPositionException
*/
public JLabel[] getDots(int index) {
if ((index < 0) || (index > lastElement)) {
throw new invalidArrayPositionException();
}
JLabel[] dots = new JLabel[array[0].length - 1];
int i;
for (i = 0; i < dots.length; i++) {
dots[i] = (JLabel) array[index][i + 1];
}
return dots;
}
/**
* Returns the associated JButton to specified label.
*
* @param jl The label which associated button it's going to be returned.
* @return Returns the associated JButton to specified label.
*/
public JButton getNorthLabelAssociatedButton(JLabel jl){
return getLabelAssociatedButton(jl,1);
}
/**
* Returns the associated JButton to specified label.
*
* @param jl The label which associated button it's going to be returned.
* @return Returns the associated JButton to specified label.
*/
public JButton getWestLabelAssociatedButton(JLabel jl){
return getLabelAssociatedButton(jl,2);
}
/**
* Returns the associated JButton to specified label.
*
* @param jl The label which associated button it's going to be returned.
* @return Returns the associated JButton to specified label.
*/
public JButton getSouthLabelAssociatedButton(JLabel jl){
return getLabelAssociatedButton(jl,3);
}
/**
* Returns the associated JButton to specified label.
*
* @param jl The label which associated button it's going to be returned.
* @return Returns the associated JButton to specified label.
*/
public JButton getEastLabelAssociatedButton(JLabel jl){
return getLabelAssociatedButton(jl,4);
}
/**
* Returns the associated JButton to specified label and type.
*
* @param jl The label which associated button it's going to be returned.
* @param labelType Type of label. Starting in 1(North) and finishing in 4(East).
* @return Returns the associated JButton to specified label.
* @exception notAssociatedLabelException
*/
public JButton getLabelAssociatedButton(JLabel jl, int labelType){
int i;
for(i=0; i <= lastElement; i++){
if(array[i][labelType] == jl)
return (JButton) array[i][0];
}
throw new notAssociatedLabelException("Not associated label.");
}
/**
* Returns de button associated to the specified index.
*
* @param index The index where you want to look for button.
* @return Returns de button associated to the specified index.
* @exception invalidArrayPositionException
*/
public JButton getButton(int index) {
if ((index < 0) || (index > lastElement)) {
throw new invalidArrayPositionException();
}
return (JButton) array[index][0];
}
/**
* Adds a button and their associated labels.
* Labels array must be of size four and be ordered in Counter Clock Order (North, West...).
*
* @param jb JButton to be added.
* @param dots JLabel array of size four. (North, West, South, East)
* @return <i>lastElement</i>
* @exception fullArrayException
* @exception IndexOutBoundException
*/
public int addGroup(JButton jb, JLabel[] dots) {
if (lastElement == array.length - 1) {
throw new fullArrayException("This array it's full");
}
int i;
array[lastElement + 1][0] = jb;
for (i = 1; i < array[0].length; i++) {
array[lastElement + 1][i] = dots[i - 1];
}
array[lastElement + 1][1].setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
array[lastElement + 1][2].setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
array[lastElement + 1][3].setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
array[lastElement + 1][4].setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
return ++lastElement;
}
/**
* Returns <i>lastElement</i>
*
* @return <i>lastElement</i>
*/
public int getLastGroupIndex() {
return lastElement;
}
/**
* Returns length of <i>array</i>.
*
* @return Returns length of <i>array</i>.
*/
public int getMaximumGroups() {
return array.length;
}
/**
* Clear references to buttons and labels.
* <i>array</i> references to a new bidimensional array of same size as previous one.
* All buttons are deselected, and lastElement set to -1.
*/
public void clear() {
array= new JComponent[array.length][array[0].length];
lastElement = -1;
}
/**
* Makes visible labels associated to a button.
* Use deprecated method show().
*
* @param jb JButton which associated labels are going to be shown.
* @exception invalidArrayPositionException
*/
public void showLabels(JButton jb) {
showLabels(containsButton(jb));
}
/**
* Makes visible labels associated to a button.
* Use deprecated method show().
*
* @param index index of button which associated labels are going to be shown.
* @exception invalidArrayPositionException
*/
public void showLabels(int index) {
if ((index < 0) || (index > lastElement)) {
throw new invalidArrayPositionException("Invalid Position");
}
intj;
JLabel jl;
for (j = 1; j < array[0].length; j++) {
jl = (JLabel) array[index][j];
jl.show();
}
}
/**
* Makes not visible labels associated to a button.
* Use deprecated method hide().
*
* @param jb JButton which associated labels are going to be hidden.
* @exception invalidArrayPositionException
*/
public void hideLabels(JButton jb) {
hideLabels(containsButton(jb));
}
/**
* Makes not visible labels associated to a button.
* Use deprecated method hide().
*
* @param index index of button which associated labels are going to be hidden.
* @exception invalidArrayPositionException
*/
public void hideLabels(int index) {
if ((index < 0) || (index > lastElement)) {
throw new invalidArrayPositionException("Invalid Position");
}
intj;
JLabel jl;
for (j = 1; j < array[0].length; j++) {
jl = (JLabel) array[index][j];
jl.hide();
}
}
/**
* Makes a visible selection of a button.
* If button is selected and ctrlPressed it's true then button will be substracted from selection.
* If are multiple buttons selected and it's clicked on a button without ctrl key be pressed then all
* other buttons are deselected and that one selected.
*
* @param jb Button to be selected.
* @param ctrlPressed Indicates if ctrl key was pressed.
*/
public void select(JButton jb, boolean ctrlPressed) {
select(this.containsButton(jb), ctrlPressed);
}
/**
* Makes a visible selection of a button.
* If button is selected and ctrlPressed it's true then button will be substracted from selection.
* If are multiple buttons selected and it's clicked on a button without ctrl key be pressed then all
* other buttons are deselected and that one selected.
*
* @param index Array position of button to be selected.
* @param ctrlPressed Indicates if ctrl key was pressed.
*/
public void select(int index, boolean ctrlPressed) {
if(selectionArray.isEmpty()){
selectionArray.add(getButton(index));
showLabels(index);
} else{
if (ctrlPressed) {
if (isSelected(index)) {
deselect((JButton) array[index][0]);
} else{
selectionArray.add(getButton(index));
showLabels(index);
}
} else{
this.clearSelection();
selectionArray.add(getButton(index));
showLabels(index);
}
}
}
/**
* Visually deselect a button.
*
* @param jb Button to be sustracted.
*/
public void deselect(JButton jb) {
this.hideLabels(jb);
selectionArray.remove(jb);
}
/**
* Visually deselects all buttons.
*/
public void clearSelection() {
while (!selectionArray.isEmpty()) {
deselect((JButton) selectionArray.lastElement());
}
}
/**
* Reallocate resize dots (labels) of a specified button.
*
* @param jb Button which resize dots are going to be reallocated.
*/
public void reallocateDots(JButton jb){
reallocateDots(containsButton(jb));
}
/**
* Reallocate resize dots (labels) of a specified button.
*
* @param index Array position of button which resize dots are going to be reallocated.
*/
public void reallocateDots(int index){
JButton jb = getButton(index);
JLabel[] jl= getDots(index);
jl[0].setLocation(jb.getX() + (jb.getWidth() / (jl[0].getWidth()/2)), jb.getY() -jl[0].getHeight());
jl[1].setLocation(jb.getX() - jl[1].getWidth(), jb.getY() + (jb.getHeight() / 2) -(jl[1].getHeight() /2) );
jl[2].setLocation(jb.getX() + (jb.getWidth() / (jl[2].getWidth()/2)), jb.getY() + jb.getHeight());
jl[3].setLocation(jb.getX() + jb.getWidth(), jb.getY() + (jb.getHeight() / 2) -(jl[3].getHeight() /2) );
}
/**
* Returns if a button it's selected.
*
* @param index Array positon of button to be checked.
* @return Returns if a button it's selected.
*/
public boolean isSelected(int index){
return isSelected(getButton(index));
}
/**
* Returns if a button it's selected.
*
* @param jb Button to be checked.
* @return Returns if a button it's selected.
*/
public boolean isSelected(JButton jb){
return selectionArray.contains(jb);
}
/**
* Returns if more than one buttons are selected.
*
* @return Returns if more than one buttons are selected.
*/
public boolean isMoreThanOneSelected(){
return selectionArray.size() > 1;
}
/**
* Returns if a label it's a North Label.
*
* @param jl Label to be checked.
* @return Returns if a label it's a North Label.
*/
public boolean isNorthLabel(JLabel jl) {
return isLabel(1,jl);
}
/**
* Returns if a label it's a East Label.
*
* @param jl Label to be checked.
* @return Returns if a label it's a East Label.
*/
public boolean isEastLabel(JLabel jl) {
return isLabel(4,jl);
}
/**
* Returns if a label it's a South Label.
*
* @param jl Label to be checked.
* @return Returns if a label it's a South Label.
*/
public boolean isSouthLabel(JLabel jl) {
return isLabel(3,jl);
}
/**
* Returns if a label it's a West Label.
*
* @param jl Label to be checked.
* @return Returns if a label it's a West Label.
*/
public boolean isWestLabel(JLabel jl) {
return isLabel(2,jl);
}
/**
* Returns if a label of the specified type it's on array.
* Type: 1-North, 2-West, 3-Soth, 4-East.
*
* @param type Type of label.
* @param jl Label to be checked.
* @return Returns if a label of the specified type it's on array.
*/
private boolean isLabel(int type, JLabel jl){
int i;
for(i=0; i <= lastElement; i++){
if(array[i][type] == jl)
return true;
}
return false;
}
/**
* Resize specified button according to a north label drag event.
*
* @param jb Button to be resized.
* @param yVariation Variation of y coordinate. Usually you get it by evt.getY() on <label>MouseDragged event.
*/
public void northLabelDragged(JButton jb, int yVariation){
if((jb.getHeight() - yVariation) > jb.getMinimumSize().getHeight() /*&& (jb.getHeight() - yVariation) < jb.getMaximumSize().getHeight()*/){
jb.setSize(jb.getWidth(), jb.getHeight() - yVariation);
jb.setLocation(jb.getX(), jb.getY() + yVariation);
reallocateDots(jb);
}
}
/**
* Resize specified button according to a west label drag event.
*
* @param jb Button to be resized.
* @param xVariation Variation of x coordinate. Usually you get it by evt.getX() on <label>MouseDragged event.
*/
public void westLabelDragged(JButton jb, int xVariation) {
if((jb.getWidth() - xVariation) > jb.getMinimumSize().getWidth() /*&& (jb.getWidth() - xVariation) < jb.getMaximumSize().getWidth()*/) {
jb.setSize(jb.getWidth() - xVariation, jb.getHeight());
jb.setLocation(jb.getX() + xVariation, jb.getY());
reallocateDots(jb);
}
}
/**
* Resize specified button according to a south label drag event.
*
* @param jb Button to be resized.
* @param yVariation Variation of y coordinate. Usually you get it by evt.getY() on <label>MouseDragged event.
*/
public void southLabelDragged(JButton jb, int yVariation){
if((jb.getHeight() + yVariation) > jb.getMinimumSize().getHeight() /*&& (jb.getHeight() + yVariation) < jb.getMaximumSize().getHeight()*/){
jb.setSize(jb.getWidth(), jb.getHeight() + yVariation);
reallocateDots(jb);
}
}
/**
* Resize specified button according to an east label drag event.
*
* @param jb Button to be resized.
* @param xVariation Variation of x coordinate. Usually you get it by evt.getX() on <label>MouseDragged event.
*/
public void eastLabelDragged(JButton jb, int xVariation) {
if((jb.getWidth() + xVariation) > jb.getMinimumSize().getWidth() /*&& (jb.getWidth() - xVariation) < jb.getMaximumSize().getWidth()*/) {
jb.setSize(jb.getWidth() + xVariation, jb.getHeight());
reallocateDots(jb);
}
}
/**
* Moves button in order to a mouse drag event.
* Button cursor it's set to <code>Cursor.MOVE_CURSOR</code>.
* It's convenient that mousePosition to be a clone of method getMousePosition().
* Usual way to invoke this method: myBArray.moveButton((JButton) evt.getSource(), (Point) this.getMousePosition().clone(), this)
* being <i>this</i>, JFrame where this method has been invoked.
*
* NOTE: Be sure to invoke setLastMousePosition on mousePressed event only,
* then move althougth on mousePressed and mouseDragged event.
*
* @param jb Button to move.
* @param mousePosition Position of the mouse in the instant this method it's called.
* @param caller Form caller of this method.
*/
public void moveButton(JButton jb, Point mousePosition, JFrame caller){
clearSelection();
caller.repaint();
Point p = new Point();
p.setLocation(lastX + mousePosition.getX(), lastY + mousePosition.getY());
jb.setLocation(p);
jb.setCursor(new Cursor(Cursor.MOVE_CURSOR));
}
/**
* Set mouse position before moving a button.
* It's convenient that mousePosition to be a clone of method getMousePosition().
* Usual way to invoke this method: myBArray.setLastMousePosition((JButton) evt.getSource(), (Point) this.getMousePosition().clone())
*
* @param jb Button wich it's going to be moved after invoke this method.
* @param mousePosition Mouse position when this method it's invoked.
*/
public void setLastMousePosition(JButton jb, Point mousePosition){
lastX = jb.getX() - (int) mousePosition.getX();
lastY = jb.getY() - (int) mousePosition.getY();
}
}
Message was edited by:
totu
Message was edited by:
totu