public class ClassBox extends JPanel implements MouseListener, MouseMotionListener, FocusListener
{
// Constructor
public ClassBox(TickFrame frame, JPanel panel, Document d, JPanel status) throws IllegalArgumentException
{
this.mpanel = panel;
this.spanel = status;
this.frame = frame;
this.document = d;
this.setSize(new Dimension(0,0)); // initial dimension of box
// final size set after getting coordinates from mouse
this.setBorder(BorderFactory.createLineBorder(Color.black,1));
this.setBackground(Color.white);
// listeners
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.addFocusListener(this);
// vertical box layout for aligning class/method labels
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
// dialog for class name
c_name = (String) JOptionPane.showInputDialog(frame,
"Enter the class name",
"Name?",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"Enter class name here...");
if(c_name == null)
{
IllegalArgumentException iae = new IllegalArgumentException();
throw iae;
}
// dialog for # class methods
Object options[] = {"1", "2", "3", "more...", "cancel"};
int no1 = JOptionPane.showOptionDialog(frame,
"How many class methods?",
"# methods?",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
// selection = cancel
if(no1 == 4)
{
IllegalArgumentException iae = new IllegalArgumentException();
throw iae;
}
// selection = more, read input and convert to integer
if(no1 == 3)
{
String no2 = (String) JOptionPane.showInputDialog(frame,
"How many class methods=",
"# methods?",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"Enter the number of methods here...");
if(no2 == null)
{
IllegalArgumentException iae = new IllegalArgumentException();
throw iae;
}
else
{
// cast Integer from String
Integer no3 = new Integer(no2);
no1 = no3.intValue() - 1;
}
}
// dialog for method names
c_methods = new Vector((no1+1),1);
for(int i=0; i<(no1+1);i++)
{
String method_names = (String) JOptionPane.showInputDialog(frame,
"Enter method name #" + (i+1),
"Methods",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"Enter the method name here...");
if(method_names == null)
{
for(int j=1;j<=i;j++)
{
document.decrementMethod(spanel);
}
IllegalArgumentException iae = new IllegalArgumentException();
throw iae;
}
else
{
c_methods.add(i,method_names);
// increment counter of methods
document.incrementMethod((String) c_methods.elementAt(i),spanel);
}
}
// info dialog
JOptionPane.showMessageDialog(frame, "Click anywhere in the panel to indicate the top left corner
and release mouse to indicate bottom right corner.");
// change cursor
Cursor chc = new Cursor(Cursor.CROSSHAIR_CURSOR);
mpanel.setCursor(chc);
}
protected void paintComponent(Graphics g)
{
System.out.println("paintComponent");
// set location for classboxes as registered in array
for(int i=1;i<document.noclass;i++)
{
Register[] r = document.regarray;
// set location
r[i].getBox().setLocation(r[i].getX(),r[i].getY());
// set size - standard || customized
if(r[i].getBox().getBoxWidth() < 50 || r[i].getBox().getBoxHeight() < 50)
{
r[i].getBox().setSize(50,50); // minimum size 50x50
}
else
{
r[i].getBox().setSize(r[i].getBox().getBoxWidth(),r[i].getBox().getBoxHeight());
}
// add label
r[i].getBox().addLabel(r[i].getBox().c_name, r[i].getBox().classMetrics,
r[i].getBox().className);
for(int j=0;j<r[i].getBox().c_methods.size();j++)
{
r[i].getBox().addLabel((String)r[i].getBox().c_methods.get(i),
r[i].getBox().methodMetrics, r[i].getBox().methodName);
}
}
}
// methods for coordinates of box (top left) - get & set
public int getX1coord()
{
return c_x1;
}
public int getY1coord()
{
return c_y1;
}
public void setCoordP(int x,int y)
{
c_x1 = x;
System.out.println("c_x1 "+c_x1);
c_y1 = y;
System.out.println("c_y1 "+c_y1);
}
// methods for coordinates of box (bottom right) - get & set
public int getX2coord()
{
return c_x2;
}
public int getY2coord()
{
return c_y2;
}
public void setCoordR(int x,int y)
{
c_x2 = x;
c_y2 = y
}
// methods for width, height of box - get & set
public int getBoxWidth()
{
c_width = c_x2 - c_x1;
return c_width;
}
public int getBoxHeight()
{
c_height = c_y2 - c_y1;
return c_height;
}
public void setBoxWidth(int width)
{
c_width = width;
}
public void setBoxHeight(int height)
{
c_height = height;
}
// label for class + methods
public void addLabel(String text, FontMetrics fontMetrics, Font font)
{
this.add(Box.createRigidArea(new Dimension(0,5)));
this.add(Box.createRigidArea(new Dimension(5,0)));
JLabel label = new JLabel(text, SwingConstants.CENTER); // align text in centre
label.setFont(font); // set Font
if(text.equals(c_name)) label.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.black));
if(fontMetrics.stringWidth(text)>this.getBoxWidth()) // label text > box width
{
char[] letters = text.toCharArray();
int ellipslength = fontMetrics.stringWidth(ellipsis);
int emptylength = fontMetrics.stringWidth(empty);
StringBuilder shortname = new StringBuilder();
int i = 0;
// build label letter by letter
while((fontMetrics.stringWidth(shortname.toString()) + ellipslength + emptylength) < this.getBoxWidth())
{
shortname.append(letters[i]);
i++;
}
shortname.append(ellipsis);
label.setText(shortname.toString());
}
this.add(label); // add label to box
this.add(Box.createHorizontalGlue());
this.validate();
this.repaint();
}
// increase number of methods - either by double-click or context-menu
public void addMethods()
{
// add new item to Vector
c_methods.add((String) JOptionPane.showInputDialog(frame,
"Enter method name #" + (c_methods.size()+1),
"Methods",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"Enter the new method name here..."));
// add new label to box
this.addLabel((String)c_methods.lastElement(),this.methodMetrics,this.methodName);
// increase number of methods in status panel (and underlying document)
document.incrementMethod((String) c_methods.lastElement(),spanel);
}
public void resize() // called from context-menu only
{
//dialog for new width
String widthStr = (String) JOptionPane.showInputDialog(frame,
"What is the new width of your box?",
"Resize",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"");
// cast Integer from String
Integer width = new Integer(widthStr);
//dialog for new height
String heightStr = (String) JOptionPane.showInputDialog(frame,
"What is the new height of your box?",
"Resize",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"");
// cast Integer from String
Integer height = new Integer(heightStr);
this.setBoxWidth(width);
this.setBoxHeight(height);
this.setSize(width,height);
this.validate();
}
// mouseListener + mouseMotionListener
public void mousePressed(MouseEvent evt)
{
if(State.enable == false)
{
this.setBorder(BorderFactory.createLineBorder(Color.red,2));
}
else
{
this.setBorder(BorderFactory.createLineBorder(Color.blue,2));
}
dragFromX = evt.getX() - this.getX1coord(); // calculate dragSource - X
dragFromY = evt.getY() - this.getY1coord(); // calculate dragSource - Y
canDrag = true;
}
public void mouseDragged(MouseEvent evt)
{
int x = evt.getX() - dragFromX;
int y = evt.getY() - dragFromY; // top of box off screen
if(canDrag)
{
this.setCoordP(x,y);
// set new coordinates of box after converting
this.setLocation(SwingUtilities.convertPoint(this,this.getX1coord(),this.getY1coord(),mpanel));
}
}
public void mouseClicked(MouseEvent evt)
{
this.requestFocus(); // focus on box
if(evt.getClickCount() == 2) this.addMethods(); // detect double-click
if(State.enable == false)
{
this.setBorder(BorderFactory.createLineBorder(Color.red,2));
}
else
{
this.setBorder(BorderFactory.createLineBorder(Color.blue,2));
}
// if arrow created - box might be either source or destination of arrow
try
{
// State = 0: arrow initiated, no source set
if(State.getInstance().getState() == 0)
{
frame.getActiveArrow().setCurrent(this); // set current box as source
State.getInstance().changeState(State.TRANS_2); // change state to "source identified"
return;
}
// State = 1: source set, no destination
if(State.getInstance().getState() == 1)
{
frame.getActiveArrow().setConnect(this,spanel); // set current box as destination
State.getInstance().changeState(State.TRANS_3); // change state to "both source & dest. identified"
return;
}
}
catch(NullPointerException npe) // no arrow instance was created
{
return;
}
}
public void mouseReleased(MouseEvent evt)
{
this.setBorder(BorderFactory.createLineBorder(Color.black,1)); // remove highlight
if(evt.isPopupTrigger()) // mouse click = right
{
popmen = new ContextMenu(this);
popmen.show(evt.getComponent(),evt.getX(),evt.getY()); // show context menu
}
// Point classbox = this.getLocation();
// this.setCoordP((int)classbox.getX(),(int)classbox.getY());
}
public void mouseEntered(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}
public void mouseMoved(MouseEvent evt) {}
// focusListener
public void focusGained(FocusEvent evt)
{
if(State.enable == false)
{
this.setBorder(BorderFactory.createLineBorder(Color.red,2));
}
else
{
this.setBorder(BorderFactory.createLineBorder(Color.blue,2));
}
}
public void focusLost(FocusEvent evt)
{
this.setBorder(BorderFactory.createLineBorder(Color.black,1));
}
}