package alm;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import linearProgramming.*;
import lpsolve.LpSolveException;
public class ALMEditor extends JComponent implements MouseListener,
MouseMotionListener {
private JPopupMenu rightClickMenu;
private JMenuItem removeContentMenuItem;
private JMenuItem removeRowMenuItem;
private JMenuItem splitAreaHorizontalMenuItem;
private JMenuItem splitAreaVerticalMenuItem;
private JMenuItem removeColumnMenuItem;
private JMenuItem splitRowMenuItem;
private JMenuItem splitColumnMenuItem;
private JMenuItem addConstraintTabMenuItem;
private JMenuItem removeConstraintTabItem;
public enum Mode {
AreaEdit, ConstraintEdit, RowEdit, ColumnEdit
};
ALMLayout aLMEngine;
public List<Variable> selectedTabs;
public Variable selectedXTab;
public Variable selectedTab;
public Variable selectedYTab;
public Row selectedRow;
public Row mouseOverRow;
public Column selectedColumn;
public Column mouseOverColumn;
public Constraint selectedConstraint;
public boolean showXTab;
public boolean showYTab;
Variable mouseOverXTab;
Variable mouseOverYTab;
public Area selectedArea;
Area mouseOverArea;
// boolean values to determine mouse cursor shape when moving an area by ts
// corner
boolean moveNESW, moveNWSE;
boolean showHorizontalArrow;
boolean showVerticalArrow;
private Mode mode;
// Tolerance value for mouse clicks
final int TOL = 4;
public ALMEditor(Mode mode, ALMLayout aLMEngine) {
setSize(510, 356); // To be generalized..
this.aLMEngine = aLMEngine;
this.setMode(mode);
if (selectedArea == null && selectedXTab == null
&& selectedYTab == null) {
selectedArea = this.aLMEngine.getLayoutSpec().getAreas().get(0);
selectedRow = selectedArea.getRow();
selectedColumn = selectedArea.getColumn();
}
selectedTabs = new ArrayList<Variable>();
// this.SetStyle(
// ControlStyles.AllPaintingInWmPaint |
// ControlStyles.UserPaint |
// ControlStyles.DoubleBuffer, true);
this.rightClickMenu = new JPopupMenu();
rightClickMenu.setSize(292, 24);
// this.SuspendLayout();
//
// rightClickMenu
//
this.rightClickMenu.setName("Edit Menu");
// ALMEditor
//
// this.ContextMenuStrip = this.rightClickMenu;
// this.ContextMenuStrip.SuspendLayout();
// this.SuspendLayout();
// Menu Item for removing area content
//
this.removeContentMenuItem = new JMenuItem();
this.removeContentMenuItem.setName("Remove Area Content");
this.removeContentMenuItem.setSize(new Dimension(168, 22));
this.removeContentMenuItem.setText("Remove Area Content");
this.rightClickMenu.add(removeContentMenuItem);
// Menu Item for splitting an area Horizontally
//
this.splitAreaHorizontalMenuItem = new JMenuItem();
this.splitAreaHorizontalMenuItem.setName("Split Area Horizontally");
this.splitAreaHorizontalMenuItem.setSize(new Dimension(168, 22));
this.splitAreaHorizontalMenuItem.setText("Split Area Horizontally");
this.rightClickMenu.add(splitAreaHorizontalMenuItem);
// Menu Item for splitting an area Vertically
//
this.splitAreaVerticalMenuItem = new JMenuItem();
this.splitAreaVerticalMenuItem.setName("Split Area Vertically");
this.splitAreaVerticalMenuItem.setSize(new Dimension(168, 22));
this.splitAreaVerticalMenuItem.setText("Split Area Vertically");
this.rightClickMenu.add(splitAreaVerticalMenuItem);
// Menu Item for splitting a row
//
this.splitRowMenuItem = new JMenuItem();
this.splitRowMenuItem.setName("Split Row");
this.splitRowMenuItem.setSize(new Dimension(168, 22));
this.splitRowMenuItem.setText("Split Row");
// Menu Item for removing a row
this.removeRowMenuItem = new JMenuItem();
this.removeRowMenuItem.setName("Remove Row");
this.removeRowMenuItem.setSize(new Dimension(168, 22));
this.removeRowMenuItem.setText("Remove Row");
// Menu Item for removing a column
this.removeColumnMenuItem = new JMenuItem();
this.removeColumnMenuItem.setName("Remove Column");
this.removeColumnMenuItem.setSize(new Dimension(168, 22));
this.removeColumnMenuItem.setText("Remove Column");
// Menu Item for splitting a column
//
this.splitColumnMenuItem = new JMenuItem();
this.splitColumnMenuItem.setName("Split Column");
this.splitColumnMenuItem.setSize(new Dimension(168, 22));
this.splitColumnMenuItem.setText("Split Column");
// Menu Item for adding a tab to a constraint
//
this.addConstraintTabMenuItem = new JMenuItem();
this.addConstraintTabMenuItem.setName("Add Tab to Constarint");
this.addConstraintTabMenuItem.setSize(new Dimension(168, 22));
this.addConstraintTabMenuItem.setText("Add Tab to Cosntraint");
// Menu Item for removing a tab from a constraint
//
this.removeConstraintTabItem = new JMenuItem();
this.removeConstraintTabItem.setName("Remove Tab from Constarint");
this.removeConstraintTabItem.setSize(new Dimension(168, 22));
this.removeConstraintTabItem.setText("Remove Tab from Cosntraint");
// rightClickMenu
//
this.rightClickMenu.setName("Edit menu");
this.rightClickMenu.setSize(new Dimension(61, 4));
//
// this.ContextMenuStrip.ResumeLayout(false);
// this.ResumeLayout(false);
addMouseListener(this);
addMouseMotionListener(this);
this.removeContentMenuItem.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
try {
removeContentMenuItem_MouseDown();
System.out.println("removing");
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
this.splitAreaHorizontalMenuItem.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
try {
splitAreaHorizontalMenuItem_MouseDown();
System.out.println("horizontal");
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
this.splitAreaVerticalMenuItem.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
try {
splitAreaVerticalMenuItem_MouseDown();
System.out.println("vertical");
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
// Showing menus when right mouse button is clicked
if (e.getButton() == MouseEvent.BUTTON3) {
this.rightClickMenu.show(e.getComponent(), e.getX(), e.getY());
}
// If in Area mode the area over which the mouse cursor instanceof
// clicked(if there instanceof one)
// will become selected as well as any tabs if they are present.
if (this.getMode() == Mode.AreaEdit) {
this.selectedArea = findSelectedArea(new Point(e.getX(), e.getY()));
if (this.selectedArea != null) {
this.selectedXTab = getAreaXTabNearPoint(selectedArea,
new Point(e.getX(), e.getY()), TOL);
this.selectedYTab = getAreaYTabNearPoint(selectedArea,
new Point(e.getX(), e.getY()), TOL);
}
if (selectedXTab != null) {
this.showXTab = true;
}
if (selectedYTab != null) {
this.showYTab = true;
}
this.invalidate();
this.aLMEngine.editForm.update();
}
// In Constraint Mode, the constraint the cursor instanceof
// currently
// over will be selected.
else if (this.getMode() == Mode.ConstraintEdit) {
try {
this.aLMEngine.getLayoutSpec().save("presave.txt");
} catch (LinearProgrammingException e1) {
e1.printStackTrace();
}
boolean removeConstTab = false;
selectedTab = getTabNearPoint(new Point(e.getX(), e.getY()), TOL);
}
// In Row Mode, the constraint the cursor instanceof currently over
// will be selected.
else if (this.getMode() == Mode.RowEdit) {
selectedRow = findSelectedRow(new Point(e.getX(), e.getY()));
this.invalidate();
this.aLMEngine.editForm.update();
// this.aLMEngine.parent.PerformLayout();
}
// In Column Mode, the constraint the cursor instanceof currently
// over
// will be selected.
else if (this.getMode() == Mode.ColumnEdit) {
selectedColumn = findSelectedColumn(new Point(e.getX(), e.getY()));
this.invalidate();
this.aLMEngine.editForm.update();
// this.aLMEngine.parent.PerformLayout();
}
repaint();
}
public void mouseReleased(MouseEvent e) {
moveNESW = false;
moveNWSE = false;
if (this.getMode() == Mode.AreaEdit) {
if (selectedArea != null) {
// If there are no tabs selected,Swap the mouseover and the
// selected areas
if (selectedXTab == null && selectedYTab == null
&& mouseOverArea != null
&& mouseOverArea != selectedArea) {
XTab left = this.selectedArea.left;
XTab right = this.selectedArea.right;
YTab top = this.selectedArea.top;
YTab bottom = this.selectedArea.bottom;
this.selectedArea.top = this.mouseOverArea.top;
this.selectedArea.left = this.mouseOverArea.left;
this.selectedArea.right = this.mouseOverArea.right;
this.selectedArea.bottom = this.mouseOverArea.bottom;
this.mouseOverArea.top = top;
this.mouseOverArea.left = left;
this.mouseOverArea.right = right;
this.mouseOverArea.bottom = bottom;
}
// If there instanceof a xtab selected, replace it with the
// mouseoverXTab
if (selectedXTab != null && mouseOverXTab != null) {
if (selectedXTab == selectedArea.left)
selectedArea.left = (XTab) mouseOverXTab;
else
selectedArea.right = (XTab) mouseOverXTab;
}
// If there instanceof a ytab selected, replace it with the
// mouseoverYTab
if (selectedYTab != null && mouseOverYTab != null) {
if (selectedYTab == selectedArea.top)
selectedArea.top = (YTab) mouseOverYTab;
else
selectedArea.bottom = (YTab) mouseOverYTab;
}
}
// if (!this.aLMEngine.editForm.showYTabBox.isCursorSet())
this.showYTab = false;
// if (!this.aLMEngine.editForm.showXTabBox.isCursorSet())
this.showXTab = false;
this.mouseOverArea = null;
this.mouseOverXTab = null;
this.mouseOverYTab = null;
this.selectedXTab = null;
this.selectedYTab = null;
// this.Parent.PerformLayout();
this.invalidate();
this.aLMEngine.editForm.update();
} else if (this.getMode() == Mode.ConstraintEdit) {
// Check if selected Tab instanceof a part of the selected
// Constraint
if (selectedTab != null && selectedTabs.contains(selectedTab)) {
// In the case of a single variable in the constraint,
// change
// the value of the right hand side to be the value of the
// tab
//
if (this.selectedConstraint.getLeftSide().length == 1)
try {
this.selectedConstraint
.setRightSide(this.selectedTab.getValue());
} catch (Exception e1) {
e1.printStackTrace();
}
// In the case of two variables in the constraint, change
// the value of the right hand side to be the difference
// between
// the values of the two tabs
//
if (this.selectedConstraint.getLeftSide().length == 2)
try {
this.selectedConstraint
.setRightSide(this.selectedConstraint
.getLeftSide()[1].getVar().getValue()
- this.selectedConstraint.getLeftSide()[0]
.getVar().getValue());
} catch (Exception e1) {
e1.printStackTrace();
}
this.aLMEngine.editForm.update();
// this.Parent.PerformLayout();
this.invalidate();
try {
this.aLMEngine.getLayoutSpec().save("save.txt");
} catch (LinearProgrammingException e1) {
e1.printStackTrace();
}
}
} else if (this.getMode() == Mode.RowEdit) {
// If the selected row and the current row the mouse instanceof
// over
// are not
// then insert the selected row before the row.From the users
// perspective the gap where the selected row will fit
// instanceof
// highlighted.
if (mouseOverRow != null && selectedRow != null) {
try {
selectedRow.unlink();
} catch (Exception e1) {
e1.printStackTrace();
}
try {
this.selectedRow.insertBefore(mouseOverRow);
} catch (Exception e1) {
e1.printStackTrace();
}
this.mouseOverRow = null;
// this.Parent.PerformLayout();
this.invalidate();
}
}
else if (this.getMode() == Mode.ColumnEdit) {
// If the selected column and the current column the mouse
// instanceof over are not
// then insert the selected column before the column.From the
// users
// perspective the gap where the selected column will fit
// instanceof
// highlighted.
if (mouseOverColumn != null && selectedColumn != null) {
try {
selectedColumn.unlink();
} catch (Exception e1) {
e1.printStackTrace();
}
try {
this.selectedColumn.insertBefore(mouseOverColumn);
} catch (Exception e1) {
e1.printStackTrace();
}
this.mouseOverColumn = null;
// this.Parent.PerformLayout();
this.invalidate();
}
}
// aLMEngine.layoutContainer(this);
repaint();
}
public void mouseDragged(MouseEvent e) {
if (this.getMode() == Mode.AreaEdit) {
this.mouseOverArea = findSelectedArea(new Point(e.getX(), e.getY()));
if (mouseOverArea == selectedArea)
mouseOverArea = null;
this.mouseOverXTab = getXTabNearPoint(
new Point(e.getX(), e.getY()), TOL);
this.mouseOverYTab = getYTabNearPoint(
new Point(e.getX(), e.getY()), TOL);
if (!this.showXTab && !this.showYTab)
this.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}
repaint();
}
public void mouseMoved(MouseEvent e) {
// Check if the mouse instanceof being dragged over a valid area and
// highlight it.
// If a tab instanceof being dragged, display all tabs of the same type
// in the form.
if (this.getMode() == Mode.AreaEdit) {
this.mouseOverArea = findSelectedArea(new Point(e.getX(), e.getY()));
if (selectedArea != null) {
if (this.showXTab && !this.showYTab) {
this.setCursor(Cursor
.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
} else if (!this.showXTab && this.showYTab) {
this.setCursor(Cursor
.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
} else if (this.showXTab && this.showYTab) {
if (this.moveNESW)
this.setCursor(Cursor
.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
else if (this.moveNWSE)
this.setCursor(Cursor
.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
} else {
this.setCursor(Cursor.getDefaultCursor());
}
}
if (e.getButton() == MouseEvent.NOBUTTON && mouseOverArea != null) {
isNearAreaBorder(new Point(e.getX(), e.getY()), mouseOverArea);
mouseOverArea = null;
}
} else if (this.getMode() == Mode.ConstraintEdit) {
if (this.selectedTab != null) {
// Check if the selected Tab instanceof part of the selected
// Constraint
if (this.selectedTabs.contains(this.selectedTab)) {
// In the case of one variable in the constraint
if (selectedTab instanceof XTab)
this.selectedTab.setValue(e.getX());
else
this.selectedTab.setValue(e.getY());
}
}
}
// Highlight the gap that the selected row could fit into while
// being
// dragged.
else if (this.getMode() == Mode.RowEdit) {
if (selectedArea != null) {
for (Row row : this.aLMEngine.getLayoutSpec().getRows()) {
if (this.getYTabNearPoint(new Point(e.getX(), e.getY()),
TOL) != null
&& this.getYTabNearPoint(new Point(e.getX(), e
.getY()), TOL).getValue() == row.top.getValue())
mouseOverRow = row;
}
}
if (mouseOverRow == selectedRow || mouseOverRow == selectedRow.next)
mouseOverRow = null;
}
// Highlight the gap that the selected column could fit into while
// being
// dragged.
else if (this.getMode() == Mode.ColumnEdit) {
if (selectedArea != null) {
for (Column column : this.aLMEngine.getLayoutSpec().getColumns()) {
if (this.getXTabNearPoint(new Point(e.getX(), e.getY()),
TOL) != null
&& this.getXTabNearPoint(new Point(e.getX(), e
.getY()), TOL).getValue() == column.left.getValue())
mouseOverColumn = column;
}
}
if (mouseOverColumn == selectedColumn
|| mouseOverColumn == selectedColumn.next)
mouseOverColumn = null;
}
this.invalidate();
repaint();
}
void isNearAreaBorder(Point p, Area a) {
showHorizontalArrow = false;
showVerticalArrow = false;
if (p.getY() >= a.getTop().getValue() - TOL
&& p.getY() <= a.getBottom().getValue() + TOL) {
if (p.getX() <= a.getLeft().getValue() + TOL
&& p.getX() >= a.getLeft().getValue())
showHorizontalArrow = true;
else if (p.getX() <= a.getRight().getValue()
&& p.getX() >= a.getRight().getValue() - TOL)
showHorizontalArrow = true;
}
if (p.getX() >= a.getLeft().getValue() - TOL
&& p.getX() <= a.getRight().getValue() + TOL) {
if (p.getY() <= a.getTop().getValue() + TOL
&& p.getY() >= a.getTop().getValue())
showVerticalArrow = true;
else if (p.getY() <= a.getBottom().getValue()
&& p.getY() >= a.getBottom().getValue() - TOL)
showVerticalArrow = true;
}
if (showHorizontalArrow)
this.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
if (showVerticalArrow)
this.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
if (showHorizontalArrow && showVerticalArrow) {
if (p.getY() >= a.getTop().getValue() - TOL
&& p.getX() >= a.getRight().getValue() - TOL
&& p.getY() < a.getBottom().getValue() - TOL
&& p.getX() >= a.getLeft().getValue() + TOL
|| (p.getY() >= a.getBottom().getValue() - TOL
&& p.getX() >= a.getLeft().getValue() - TOL
&& p.getY() > a.getTop().getValue() + TOL && p.getX() <= a
.getRight().getValue()
- TOL)) {
this.setCursor(Cursor
.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
moveNESW = true;
} else {
this.setCursor(Cursor
.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
moveNWSE = true;
}
}
}
protected Area findSelectedArea(Point point) {
for (Area a : this.aLMEngine.getLayoutSpec().getAreas()) {
if (new Rectangle((int) a.getLeft().getValue(), (int) a.getTop().getValue(),
(int) (a.getRight().getValue() - a.getLeft().getValue()), (int) (a
.getBottom().getValue() - a.getTop().getValue()))
.contains(point)) {
return a;
}
}
return null;
}
protected Row findSelectedRow(Point point) {
for (Row row : this.aLMEngine.getLayoutSpec().getRows()) {
// if (this.GetYTabNearPoint(point, TOL) != null &&
// this.GetYTabNearPoint(point, TOL).value == row.top.value)
if (new Rectangle(0, (int) row.getTop().getValue(), this.getWidth() - 1,
(int) row.getBottom().getValue() - (int) row.getTop().getValue())
.contains(point)) {
return row;
}
}
return null;
}
protected Column findSelectedColumn(Point point) {
for (Column column : this.aLMEngine.getLayoutSpec().getColumns()) {
// if (this.GetYTabNearPoint(point, TOL) != null &&
// this.GetYTabNearPoint(point, TOL).value == row.top.value)
if (new Rectangle((int) column.left.getValue(), 0,
(int) column.right.getValue() - (int) column.left.getValue(), this
.getHeight() - 1).contains(point)) {
return column;
}
}
return null;
}
Variable getXTabNearPoint(Point p, int tolerance) {
Variable selectedTab = null;
int proximity = Integer.MAX_VALUE;
for (Variable v : aLMEngine.getLayoutSpec().getVariables()) {
if (v instanceof XTab && v.getValue() + tolerance > p.getX()
&& p.getX() > v.getValue() - tolerance) {
if (Math.abs(v.getValue() - p.getX()) < proximity) {
selectedTab = v;
proximity = (int) Math.abs(v.getValue() - p.getX());
return selectedTab;
}
}
}
return selectedTab;
}
Variable getYTabNearPoint(Point p, int tolerance) {
Variable selectedTab = null;
int proximity = Integer.MAX_VALUE;
for (Variable v : aLMEngine.getLayoutSpec().getVariables()) {
if (v instanceof YTab && v.getValue() + tolerance > p.getY()
&& p.getY() > v.getValue() - tolerance) {
if (Math.abs(v.getValue() - p.getY()) < proximity) {
selectedTab = v;
proximity = (int) Math.abs(v.getValue() - p.getY());
return selectedTab;
}
}
}
return selectedTab;
}
Variable getAreaXTabNearPoint(Area area, Point point, int tolerance) {
XTab selectedTab = null;
XTab nearestTab = (XTab) getXTabNearPoint(point, tolerance);
if (area.getRight() == nearestTab || area.getLeft() == nearestTab)
return nearestTab;
else
return null;
}
Variable getAreaYTabNearPoint(Area area, Point point, int tolerance) {
YTab selectedTab = null;
YTab nearestTab = (YTab) getYTabNearPoint(point, tolerance);
if (area.getTop() == nearestTab || area.getBottom() == nearestTab)
return nearestTab;
else
return null;
}
Variable getTabNearPoint(Point p, int tolerance) {
Variable selectedTab = null;
int proximity = Integer.MAX_VALUE;
for (Variable v : aLMEngine.getLayoutSpec().getVariables()) {
if (v instanceof XTab && v.getValue() + tolerance > p.getX()
&& p.getX() > v.getValue() - tolerance) {
if (Math.abs(v.getValue() - p.getX()) < proximity) {
selectedTab = v;
proximity = (int) Math.abs(v.getValue() - p.getX());
}
}
if (v instanceof YTab && v.getValue() + tolerance > p.getY()
&& p.getY() > v.getValue() - tolerance) {
if (Math.abs(v.getValue() - p.getY()) < proximity) {
selectedTab = v;
proximity = (int) Math.abs(v.getValue() - p.getY());
}
}
}
return selectedTab;
}
private void addTabToConstraint(Variable var) throws LinearProgrammingException {
// this.selectedConstraint =
// (LinearProgramming.Constraint)this.aLMEngine.editForm.constraintBox.
// SelectedItem;
// double[] coeffs = new double[selectedConstraint.LeftSide.Length + 1];
// Variable[] vars = new Variable[selectedConstraint.LeftSide.Length +
// 1];
Summand[] summands = new Summand[selectedConstraint.getLeftSide().length + 1];
for (int i = 0; i < selectedConstraint.getLeftSide().length; i++) {
// coeffs[i] = selectedConstraint.LeftSide[i].Coeff;
// vars[i] = selectedConstraint.LeftSide[i].Var;
summands[i]
.setCoeff(selectedConstraint.getLeftSide()[i].getCoeff());
summands[i].setVar(selectedConstraint.getLeftSide()[i].getVar());
}
// coeffs[selectedConstraint.LeftSide.Length] = 1;
// vars[selectedConstraint.LeftSide.Length] = var;
summands[selectedConstraint.getLeftSide().length].setCoeff(1);
summands[selectedConstraint.getLeftSide().length].setVar(var);
selectedConstraint.setLeftSide(summands);
}
private void removeTabFromConstraint(Variable var) throws LinearProgrammingException {
// LinearProgramming.Constraint selectedConstraint =
// (LinearProgramming.Constraint)this.aLMEngine.editForm.constraintBox.
// SelectedItem;
// double[] coeffs = new double[selectedConstraint.Coeffs.Length - 1];
// Variable[] vars = new Variable[selectedConstraint.Vars.Length - 1];
Summand[] summands = new Summand[selectedConstraint.getLeftSide().length + 1];
for (int i = 0, j = 0; i < selectedConstraint.getLeftSide().length; i++, j++) {
if (selectedConstraint.getLeftSide()[i].getVar() == var)
j--;
else {
// vars[j] = selectedConstraint.Vars[i];
// coeffs[j] = selectedConstraint.Coeffs[i];
summands[j] = selectedConstraint.getLeftSide()[i];
}
}
selectedConstraint.setLeftSide(summands);
}
public void paintComponent(Graphics g) {
this.setSize(this.getParent().getSize()); // Resizable
aLMEngine.layoutContainer(this);
// Paint all Areas
for (Area a : this.aLMEngine.getLayoutSpec().getAreas()) {
JComponent control = a.getContent();
if (control == null) {
continue;
}
if (control.getWidth() == 0 || control.getHeight() == 0)
continue;
BufferedImage image = new BufferedImage(control.getWidth(), control
.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics controlImage = image.getGraphics();
// JComponent control2 = new JPanel();
// control2.paint(controlImage);
control.paint(controlImage);
// control.repaint();
// control.repaint(0, 0, control.getWidth(), control.getHeight());
g.drawImage(image, (int) control.getLocation().getX(),
(int) control.getLocation().getY(), null);
}
if (this.getMode() == Mode.AreaEdit) {
// paint tabs if the option instanceof selected
if (showXTab || showYTab) {
for (Variable var : this.aLMEngine.getLayoutSpec().getVariables()) {
if (showXTab && var instanceof XTab) {
g.setColor(Color.BLUE);
g.drawLine((int) var.getValue(), 0, (int) var.getValue(), this
.getHeight() - 1);
} else if (showYTab && var instanceof YTab) {
g.setColor(Color.BLUE);
g.drawLine(0, (int) var.getValue(), this.getWidth() - 1,
(int) var.getValue());
}
}
}
// paint selected area
if (selectedArea != null) {
double left = selectedArea.left.getValue();
double top = selectedArea.top.getValue();
double right = selectedArea.right.getValue();
double bottom = selectedArea.bottom.getValue();
// draw selectedArea selection rectangle
g.setColor(Color.RED);
g
.drawRect(
(int) selectedArea.left.getValue(),
(int) selectedArea.top.getValue(),
(int) (selectedArea.right.getValue() - selectedArea.left.getValue()),
(int) (selectedArea.bottom.getValue() - selectedArea.top.getValue()));
// whole selectedArea instanceof is dragged
if (selectedXTab == null && selectedYTab == null
&& mouseOverArea != null) {
// draw destination rectangle
g.setColor(Color.GREEN);
g
.drawRect(
(int) mouseOverArea.left.getValue(),
(int) mouseOverArea.top.getValue(),
(int) (mouseOverArea.right.getValue() - mouseOverArea.left.getValue()),
(int) (mouseOverArea.bottom.getValue() - mouseOverArea.top.getValue()));
}
// tab(s) of selectedArea instanceof dragged
else if (mouseOverXTab != null || mouseOverYTab != null) {
// an xtab of the selectedArea instanceof being dragged
if (selectedXTab != null && mouseOverXTab != null) {
if (selectedXTab == selectedArea.left)
left = mouseOverXTab.getValue();
else
right = mouseOverXTab.getValue();
}
// an ytab of the selectedArea instanceof being dragged
if (selectedYTab != null && mouseOverYTab != null) {
if (selectedYTab == selectedArea.top)
top = mouseOverYTab.getValue();
else
bottom = mouseOverYTab.getValue();
}
// draw destination rectangle
g.setColor(Color.GREEN);
g.drawRect((int) left, (int) top, (int) (right - left),
(int) (bottom - top));
}
}
} else if (this.getMode() == Mode.ConstraintEdit) {
for (Variable var : this.aLMEngine.getLayoutSpec().getVariables()) {
if (var instanceof XTab && this.selectedTabs.contains(var)) {
g.setColor(Color.red);
g.drawLine((int) var.getValue(), 0, (int) var.getValue(), this
.getHeight() - 1);
} else if (var instanceof YTab
&& this.selectedTabs.contains(var)) {
g.setColor(Color.red);
g.drawLine(0, (int) var.getValue(), this.getWidth() - 1,
(int) var.getValue());
} else if (var instanceof XTab) {
g.setColor(Color.blue);
g.drawLine((int) var.getValue(), 0, (int) var.getValue(), this
.getHeight() - 1);
} else if (var instanceof YTab) {
g.setColor(Color.blue);
g.drawLine(0, (int) var.getValue(), this.getWidth() - 1,
(int) var.getValue());
}
if (var instanceof XTab && this.selectedTab == var) {
g.setColor(Color.green);
g.drawLine((int) var.getValue(), 0, (int) var.getValue(), this
.getHeight() - 1);
}
if (var instanceof YTab && this.selectedTab == var) {
g.setColor(Color.green);
g.drawLine(0, (int) var.getValue(), this.getWidth() - 1,
(int) var.getValue());
}
}
} else if (this.getMode() == Mode.RowEdit) {
for (Row row : this.aLMEngine.getLayoutSpec().getRows()) {
if (row == this.mouseOverRow) {
g.setColor(Color.green);
g.drawLine(0, (int) mouseOverRow.top.getValue(),
this.getWidth(), (int) mouseOverRow.top.getValue());
}
if (row == this.selectedRow) {
g.setColor(Color.red);
g.drawRect(0, (int) row.top.getValue() + 2, this.getWidth() - 2,
(int) row.bottom.getValue() - (int) row.top.getValue() - 5);
} else {
g.setColor(Color.blue);
g.drawRect(0, (int) row.top.getValue() + 2, this.getWidth() - 2,
(int) row.bottom.getValue() - (int) row.top.getValue() - 5);
}
}
} else if (this.getMode() == Mode.ColumnEdit) {
for (Column col : this.aLMEngine.getLayoutSpec().getColumns()) {
if (col == this.mouseOverColumn) {
g.setColor(Color.green);
g.drawLine((int) col.left.getValue(), 0, (int) col.left.getValue(),
this.getHeight() - 2);
}
if (col == this.selectedColumn) {
g.setColor(Color.red);
g.drawRect((int) col.left.getValue() + 2, 0,
(int) col.right.getValue() - (int) col.left.getValue() - 5,
this.getHeight() - 2);
} else {
g.setColor(Color.blue);
g.drawRect((int) col.left.getValue() + 2, 0,
(int) col.right.getValue() - (int) col.left.getValue() - 5,
this.getHeight() - 2);
}
}
}
}
private void removeContentMenuItem_MouseDown() throws LinearProgrammingException {
// If the selected areas content instanceof already null, remove it
if (this.selectedArea.content == null)
return;
// Add the current areas content to the invisbleControls list
this.aLMEngine.editForm.addInvisibleControl(this.selectedArea.content);
// Add the content as a selectable item in the control Box
this.aLMEngine.editForm.addControl(this.selectedArea.content.getName());
// Set the content as invisible
this.selectedArea.content.setVisible(false);
// Remove the Content ie set it to null
this.selectedArea.content = null;
// this.PerformLayout();
this.aLMEngine.layout(this);
this.invalidate();
this.aLMEngine.editForm.update();
}
void splitAreaHorizontalMenuItem_MouseDown() throws LpSolveException,
Exception {
YTab newTab = new YTab(this.aLMEngine.getLayoutSpec());
newTab.setValue(this.selectedArea.top.getValue()
+ (this.selectedArea.bottom.getValue() - this.selectedArea.top.getValue())
/ 2);
Area newArea = this.aLMEngine.getLayoutSpec().addArea(
this.selectedArea.left, this.selectedArea.top,
this.selectedArea.right, newTab, null);
newArea.preferredContentSize = new Dimension(
(int) this.selectedArea.preferredContentSize.getWidth() / 2,
(int) this.selectedArea.preferredContentSize.getHeight() / 2);
newArea.setAutoPreferredContentSize(false);
this.selectedArea.top = newTab;
this.aLMEngine.editForm.update();
// this.PerformLayout();
// this.aLMEngine.Layout();
this.invalidate();
}
// Allows for the splitting of the currently selected area along a vertical
// axis.
void splitAreaVerticalMenuItem_MouseDown() throws LinearProgrammingException {
// The currently selected area instanceof shrunk to half its width and
// forms the
// left side of the split, a new area with the same dimensions of the
// area forms the right side of the split
XTab newTab = new XTab(this.aLMEngine.getLayoutSpec());
newTab.setValue(this.selectedArea.left.getValue()
+ (this.selectedArea.right.getValue() - this.selectedArea.left.getValue())
/ 2);
Area newArea = this.aLMEngine.getLayoutSpec().addArea(newTab,
this.selectedArea.top, this.selectedArea.right,
this.selectedArea.bottom, null);
newArea.preferredContentSize = new Dimension(
(int) this.selectedArea.preferredContentSize.getWidth() / 2,
(int) this.selectedArea.preferredContentSize.getHeight() / 2);
newArea.setAutoPreferredContentSize(false);
this.selectedArea.right = newTab;
this.aLMEngine.editForm.update();
// this.PerformLayout();
// this.aLMEngine.Layout();
this.invalidate();
}
// Allows for the splitting of the currently selected row
void splitRowMenuItem_MouseDown(Object sender, MouseEvent e) throws LinearProgrammingException {
YTab newTab = new YTab(this.aLMEngine.getLayoutSpec());
Row newRow = this.aLMEngine.getLayoutSpec().addRow();
newRow.unlink();
newRow.bottom.setValue(selectedRow.bottom.getValue());
newTab.setValue(this.selectedRow.top.getValue()
+ ((this.selectedRow.bottom.getValue() - this.selectedRow.top.getValue()) / 2));
this.selectedRow.bottom.setValue(newTab.getValue());
newRow.insertAfter(selectedRow);
newRow.bottom.setValue(selectedRow.bottom.getValue());
this.aLMEngine.editForm.update();
// this.PerformLayout();
this.aLMEngine.layout(this);
this.invalidate();
}
// Remove the selected row
void removeRowMenuItem_MouseDown(Object sender, MouseEvent e) throws LinearProgrammingException {
for (Area a : this.aLMEngine.getLayoutSpec().getAreas()) {
if (a.row.equals(this.selectedRow)) {
this.aLMEngine.editForm.addInvisibleControl(this.selectedArea.content);
// Add the content as a selectable item in the control Box
this.aLMEngine.editForm.addControl((this.selectedArea.content.toString()
.substring(this.selectedArea.content.toString()
.lastIndexOf(":") + 1)));
// Set the content as invisible
this.selectedArea.content.setVisible(false);
// Remove the Content ie set it to null
this.selectedArea.content = null;
}
}
this.selectedRow.unlink();
this.aLMEngine.editForm.update();
// this.PerformLayout();
this.aLMEngine.layout(this);
this.invalidate();
}
// Remove the selected column
void removeColumnMenuItem_MouseDown(Object sender, MouseEvent e) throws LinearProgrammingException {
this.selectedColumn.unlink();
this.aLMEngine.editForm.update();
// this.PerformLayout();
this.aLMEngine.layout(this);
this.invalidate();
}
void splitColumnMenuItem_MouseDown(Object sender, MouseEvent e) {
}
// Adds the currently selected tab to the constraint.
void removeConstraintTabItem_MouseDown(Object sender, MouseEvent e) throws LinearProgrammingException {
if (this.selectedTab != null
&& this.selectedTabs.contains(this.selectedTab)
&& this.selectedConstraint.getLeftSide().length != 1) {
this.removeTabFromConstraint(selectedTab);
this.aLMEngine.editForm.update();
this.invalidate();
}
}
// Adds the currently selected tab to the constraint.
void addConstraintTabMenuItem_MouseDown(Object sender, MouseEvent e) throws LinearProgrammingException {
if (this.selectedTab != null
&& !this.selectedTabs.contains(this.selectedTab)) {
this.addTabToConstraint(selectedTab);
this.aLMEngine.editForm.update();
this.invalidate();
}
}
public void updateRightClickMenu() {
this.rightClickMenu.removeAll();
if (this.getMode() == Mode.AreaEdit) {
this.rightClickMenu.add(this.removeContentMenuItem);
this.rightClickMenu.add(this.splitAreaHorizontalMenuItem);
this.rightClickMenu.add(this.splitAreaVerticalMenuItem);
}
if (this.getMode() == Mode.ConstraintEdit) {
this.rightClickMenu.add(this.addConstraintTabMenuItem);
this.rightClickMenu.add(this.removeConstraintTabItem);
}
if (this.getMode() == Mode.RowEdit && aLMEngine.getLayoutSpec().getRows().size() != 0) {
this.rightClickMenu.add(this.removeRowMenuItem);
this.rightClickMenu.add(this.splitRowMenuItem);
}
if (this.getMode() == Mode.ColumnEdit
&& aLMEngine.getLayoutSpec().getColumns().size() != 0) {
this.rightClickMenu.add(this.splitColumnMenuItem);
this.rightClickMenu.add(this.removeColumnMenuItem);
}
}
public void quitEditMode() {
this.setMode(Mode.ColumnEdit);
}
public void setMode(Mode mode) {
this.mode = mode;
}
public Mode getMode() {
return mode;
}
}