Skip to content

Commit 02d224f

Browse files
committed
Revert Action Menubar Changes, Add Intermediate Layer DataTextPaintComponent, Modify DataInputTextfieldPaintComponent to use the intermediate layer
1 parent efe1fa0 commit 02d224f

File tree

3 files changed

+82
-71
lines changed

3 files changed

+82
-71
lines changed

src/actions/menu/ActionsMenuBar.java

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@ public ActionsMenuBar(PaintPanel panel){
2929

3030
private void addAction(PaintAction action) {
3131
String[] strings = action.locationString().split("/");
32-
Object insertionMenu = this;
32+
JMenu insertionMenu = null;
3333
//look for existing j menus
34-
for( int k = 0; k < strings.length-1; k++) {
35-
for (int i = 0; i < menuCount( insertionMenu );i++) {
36-
JMenuItem menu = obtainMenu(insertionMenu, i);
37-
if(menu.getText().equals(strings[k])){
38-
insertionMenu = menu;
39-
break;
40-
}
34+
for (int i = 0; i < getMenuCount();i++) {
35+
JMenu menu = getMenu(i);
36+
if(menu.getText().equals(strings[0])){
37+
insertionMenu = menu;
38+
break;
4139
}
42-
//create a new if not found
43-
JMenu toInsert = new JMenu(strings[k]);
44-
insertMenu( insertionMenu, toInsert );
45-
insertionMenu = toInsert;
4640
}
41+
//create a new if not found
42+
if(insertionMenu == null){
43+
insertionMenu = new JMenu(strings[0]);
44+
this.add(insertionMenu);
45+
}
46+
4747
//assume 2 level depth
4848
//TODO Change here
4949
PaintActionMenuItem item = new PaintActionMenuItem(action);
5050
item.setEnabled(action.canPerformAction());
51-
item.setText(strings[strings.length-1]);
51+
item.setText(strings[1]);
5252
item.addActionListener(new ActionListener() {
5353

5454
@Override
@@ -58,33 +58,8 @@ public void actionPerformed(ActionEvent e) {
5858
}
5959
});
6060

61-
insertMenu( insertionMenu, item );
62-
}
63-
private int menuCount( Object a ) {
64-
if( a instanceof JMenu) {
65-
return ((JMenu) a).getItemCount();
66-
}
67-
if( a instanceof JMenuBar) {
68-
return ((JMenuBar) a).getMenuCount();
69-
}
70-
return -1;
71-
}
72-
private JMenuItem obtainMenu( Object a, int index ) {
73-
if( a instanceof JMenu) {
74-
return ((JMenu) a).getItem(index);
75-
}
76-
if( a instanceof JMenuBar) {
77-
return ((JMenuBar) a).getMenu(index);
78-
}
79-
return null;
80-
}
81-
private void insertMenu( Object a, JMenuItem toInsert ) {
82-
if( a instanceof JMenu) {
83-
((JMenu) a).add(toInsert);
84-
}
85-
if( a instanceof JMenuBar) {
86-
((JMenuBar) a).add(toInsert);
87-
}
61+
insertionMenu.add(item);
62+
8863
}
8964

9065
@Override

src/paintcomponents/DataInputTextfieldPaintComponent.java

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,30 @@
77

88
import settings.Defaults;
99

10-
public class DataInputTextfieldPaintComponent extends TextPaintComponent {
10+
public class DataInputTextfieldPaintComponent extends DataTextPaintComponent {
11+
1112

1213
private static final int HORIZONTAL_OFFSET = 10;
1314
private DataFromPoint<String> fromPoint;
14-
private RectanglePaintComponent rect;
15-
private Color defaultColor;
16-
private Color selectedColor;
1715

18-
1916
public DataInputTextfieldPaintComponent(String displayingText, int x,
2017
int y) {
2118
super(displayingText, x, y);
22-
fromPoint = new DataFromPoint<>(x - 50, y);
23-
defaultColor = Defaults.sharedDefaults().defaultColorForDataInputTextfield();
24-
selectedColor = Defaults.sharedDefaults().defaultColorForSelectedDataInputTextfield();
19+
this.fromPoint = new DataFromPoint<>(x, y);
2520
}
2621

27-
28-
2922
@Override
30-
protected void paintNotSelected(Graphics g) {
31-
g.setColor(defaultColor);
32-
((Graphics2D)g).setStroke(new BasicStroke(1));
33-
super.paintNotSelected(g);
23+
protected void paintSelected(Graphics g) {
24+
super.paintSelected(g);
3425
updateFromPointPosition();
35-
updateAndPaintBoudingRectangle(g);
36-
fromPoint.paintNotSelected(g);
37-
26+
fromPoint.paint(g);
3827
}
3928

40-
4129
@Override
42-
protected void paintSelected(Graphics g) {
43-
g.setColor(selectedColor);
44-
((Graphics2D)g).setStroke(new BasicStroke(1));
45-
super.paintSelected(g);;
30+
protected void paintNotSelected(Graphics g) {
31+
super.paintNotSelected(g);
4632
updateFromPointPosition();
47-
updateAndPaintBoudingRectangle(g);
48-
fromPoint.paintSelected(g);
49-
}
50-
51-
private void updateAndPaintBoudingRectangle(Graphics g){
52-
rect = new RectanglePaintComponent(getX(), getY(), (int)this.bounds.getWidth(), (int)this.bounds.getHeight());
53-
//select rectangle according to current select status
54-
if(isSelected())rect.select(); else rect.deselect();
55-
rect.paint(g);
33+
fromPoint.paint(g);
5634
}
5735

5836
/**
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package paintcomponents;
2+
3+
import java.awt.BasicStroke;
4+
import java.awt.Color;
5+
import java.awt.Graphics;
6+
import java.awt.Graphics2D;
7+
8+
import settings.Defaults;
9+
10+
/**
11+
* A text component with a bounding box,
12+
* this.bounds is updated in paint method
13+
* @author chenzb
14+
*
15+
*/
16+
public class DataTextPaintComponent extends TextPaintComponent {
17+
18+
19+
private RectanglePaintComponent rect;
20+
private Color defaultColor;
21+
private Color selectedColor;
22+
23+
public DataTextPaintComponent(String displayingText, int x, int y) {
24+
super(displayingText, x, y);
25+
defaultColor = Defaults.sharedDefaults().defaultColorForDataInputTextfield();
26+
selectedColor = Defaults.sharedDefaults().defaultColorForSelectedDataInputTextfield();
27+
}
28+
29+
30+
31+
32+
33+
@Override
34+
protected void paintNotSelected(Graphics g) {
35+
g.setColor(defaultColor);
36+
((Graphics2D)g).setStroke(new BasicStroke(1));
37+
super.paintNotSelected(g);
38+
updateAndPaintBoudingRectangle(g);
39+
40+
}
41+
42+
43+
@Override
44+
protected void paintSelected(Graphics g) {
45+
g.setColor(selectedColor);
46+
((Graphics2D)g).setStroke(new BasicStroke(1));
47+
super.paintSelected(g);;
48+
updateAndPaintBoudingRectangle(g);
49+
}
50+
51+
private void updateAndPaintBoudingRectangle(Graphics g){
52+
rect = new RectanglePaintComponent(getX(), getY(), (int)this.bounds.getWidth(), (int)this.bounds.getHeight());
53+
//select rectangle according to current select status
54+
if(isSelected())rect.select(); else rect.deselect();
55+
rect.paint(g);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)