Skip to content

Commit ad07669

Browse files
author
Ultimate Pea
authored
Merge pull request #7 from UCSDOalads/addIOComponentsbyUltimatePea
Add io componentsby ultimate pea
2 parents ad0a724 + b21e587 commit ad07669

21 files changed

+775
-72
lines changed

bin/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/actions/
2+
/icons/
3+
/paintcomponents/
4+
/painttools/
5+
/settings/
6+
/ui/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package actions;
2+
3+
import paintcomponents.DataInputTextfieldPaintComponent;
4+
import ui.PaintPanel;
5+
6+
public class AddDataInputBoxAction extends PaintAction {
7+
8+
public AddDataInputBoxAction(PaintPanel panel) {
9+
super(panel);
10+
}
11+
12+
@Override
13+
public boolean canPerformAction() {
14+
return true;
15+
}
16+
17+
@Override
18+
public void performAction() {
19+
DataInputTextfieldPaintComponent comp = new DataInputTextfieldPaintComponent("Data Input", panel.getWidth() /2, panel.getHeight()/2);
20+
panel.addPaintComponent(comp);
21+
panel.repaint();
22+
23+
}
24+
25+
@Override
26+
public String locationString() {
27+
return "Add/Data Input Box...";
28+
}
29+
30+
}

src/actions/AddTextBoxAction.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package actions;
2+
3+
import javax.swing.JOptionPane;
4+
5+
import paintcomponents.TextPaintComponent;
6+
import ui.PaintPanel;
7+
8+
public class AddTextBoxAction extends PaintAction {
9+
10+
public AddTextBoxAction(PaintPanel panel) {
11+
super(panel);
12+
}
13+
14+
@Override
15+
public boolean canPerformAction() {
16+
return true;
17+
}
18+
19+
@Override
20+
public void performAction() {
21+
String s = JOptionPane.showInputDialog("Please enter the text to display");
22+
panel.addPaintComponent(new TextPaintComponent(s, panel.getWidth() / 2, panel.getHeight()/2));
23+
panel.repaint();
24+
}
25+
26+
@Override
27+
public String locationString() {
28+
return "Add/Text Box...";
29+
}
30+
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package actions;
2+
3+
import java.util.ArrayList;
4+
5+
import javax.swing.JOptionPane;
6+
7+
import paintcomponents.DataInputTextfieldPaintComponent;
8+
import paintcomponents.PaintComponent;
9+
import ui.PaintPanel;
10+
11+
public class InputDataForDataInputBoxAction extends PaintAction {
12+
13+
public InputDataForDataInputBoxAction(PaintPanel panel) {
14+
super(panel);
15+
}
16+
17+
@Override
18+
public boolean canPerformAction() {
19+
ArrayList<PaintComponent> comps = panel.getSelectTool().getSelectedComponents();
20+
if(comps.size()!= 1) return false;
21+
if(comps.get(0) instanceof DataInputTextfieldPaintComponent){
22+
return true;
23+
}
24+
return false;
25+
}
26+
27+
@Override
28+
public void performAction() {
29+
DataInputTextfieldPaintComponent inputComp = (DataInputTextfieldPaintComponent) panel.getSelectTool().getSelectedComponents().get(0);
30+
String s = JOptionPane.showInputDialog("Please specify the message to push to the data input");
31+
inputComp.inputData(s);
32+
panel.repaint();
33+
}
34+
35+
@Override
36+
public String locationString() {
37+
return "Input/Input into Data Panel";
38+
}
39+
40+
}

src/actions/PaintAction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ public PaintAction(PaintPanel panel){
1111
}
1212

1313
public abstract boolean canPerformAction();
14+
/**
15+
* Performs this action
16+
* Subclassess must invoke panel.repaint if the action changes the panel
17+
*/
1418
public abstract void performAction();
19+
1520
public abstract String locationString();
1621

1722
}

src/actions/menu/ActionsMenuBar.java

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import javax.swing.JMenuBar;
88
import javax.swing.JMenuItem;
99

10+
import actions.AddDataInputBoxAction;
11+
import actions.AddTextBoxAction;
1012
import actions.ConstructLineSegmentAction;
1113
import actions.GeneratePolygonSourceJava;
14+
import actions.InputDataForDataInputBoxAction;
1215
import actions.PaintAction;
1316
import painttools.tools.SelectionToolListener;
1417
import ui.PaintPanel;
@@ -18,31 +21,34 @@ public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
1821
public ActionsMenuBar(PaintPanel panel){
1922
addAction(new GeneratePolygonSourceJava(panel));
2023
addAction(new ConstructLineSegmentAction(panel));
24+
addAction(new AddTextBoxAction(panel));
25+
addAction(new AddDataInputBoxAction(panel));
26+
addAction(new InputDataForDataInputBoxAction(panel));
2127

2228
}
2329

2430
private void addAction(PaintAction action) {
2531
String[] strings = action.locationString().split("/");
26-
Object insertionMenu = this;
32+
JMenu insertionMenu = null;
2733
//look for existing j menus
28-
for( int k = 0; k < strings.length-1; k++) {
29-
for (int i = 0; i < menuCount( insertionMenu );i++) {
30-
JMenuItem menu = obtainMenu(insertionMenu, i);
31-
if(menu.getText().equals(strings[k])){
32-
insertionMenu = menu;
33-
break;
34-
}
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;
3539
}
36-
//create a new if not found
37-
JMenu toInsert = new JMenu(strings[k]);
38-
insertMenu( insertionMenu, toInsert );
39-
insertionMenu = toInsert;
4040
}
41+
//create a new if not found
42+
if(insertionMenu == null){
43+
insertionMenu = new JMenu(strings[0]);
44+
this.add(insertionMenu);
45+
}
46+
4147
//assume 2 level depth
4248
//TODO Change here
4349
PaintActionMenuItem item = new PaintActionMenuItem(action);
4450
item.setEnabled(action.canPerformAction());
45-
item.setText(strings[strings.length-1]);
51+
item.setText(strings[1]);
4652
item.addActionListener(new ActionListener() {
4753

4854
@Override
@@ -52,33 +58,8 @@ public void actionPerformed(ActionEvent e) {
5258
}
5359
});
5460

55-
insertMenu( insertionMenu, item );
56-
}
57-
private int menuCount( Object a ) {
58-
if( a instanceof JMenu) {
59-
return ((JMenu) a).getItemCount();
60-
}
61-
if( a instanceof JMenuBar) {
62-
return ((JMenuBar) a).getMenuCount();
63-
}
64-
return -1;
65-
}
66-
private JMenuItem obtainMenu( Object a, int index ) {
67-
if( a instanceof JMenu) {
68-
return ((JMenu) a).getItem(index);
69-
}
70-
if( a instanceof JMenuBar) {
71-
return ((JMenuBar) a).getMenu(index);
72-
}
73-
return null;
74-
}
75-
private void insertMenu( Object a, JMenuItem toInsert ) {
76-
if( a instanceof JMenu) {
77-
((JMenu) a).add(toInsert);
78-
}
79-
if( a instanceof JMenuBar) {
80-
((JMenuBar) a).add(toInsert);
81-
}
61+
insertionMenu.add(item);
62+
8263
}
8364

8465
@Override
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package paintcomponents;
2+
3+
import java.util.Queue;
4+
import java.util.concurrent.LinkedBlockingQueue;
5+
6+
/**
7+
* This point consumes data and tries to pass the data along a connecting line
8+
* segment
9+
*
10+
* @author chenzb
11+
*
12+
*/
13+
public class DataFromPoint<T> extends SimplePoint {
14+
15+
private DataLineSegment<T> lineSegment;
16+
private DataFromPointDataProvider<T> provider;
17+
18+
/**
19+
* @return the lineSegment
20+
*/
21+
public DataLineSegment<T> getLineSegment() {
22+
return lineSegment;
23+
}
24+
25+
/**
26+
* @param lineSegment
27+
* the lineSegment to set
28+
*/
29+
public void setLineSegment(DataLineSegment<T> lineSegment) {
30+
this.lineSegment = lineSegment;
31+
}
32+
33+
public DataFromPoint(int x, int y) {
34+
super(x, y);
35+
}
36+
37+
/**
38+
* Fetches the data, users should not try to call this method, except from
39+
* DataToPoint class
40+
*
41+
* @param data
42+
* @throws DataFromPointNoDataProviderException if provider for this method is not set
43+
* @throws DataFromPointProviderCannotProvideDataException if the provider cannot provide such information
44+
*/
45+
protected T getData() throws DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException {
46+
if (this.provider == null){
47+
throw new DataFromPointNoDataProviderException();
48+
}
49+
if (!this.provider.canProvideInformationToDataFromPoint(this)){
50+
throw new DataFromPointProviderCannotProvideDataException();
51+
}
52+
return this.provider.provideInformationToDataFromPoint(this);
53+
}
54+
55+
public DataFromPointDataProvider<T> getProvider() {
56+
return provider;
57+
}
58+
59+
public void setProvider(DataFromPointDataProvider<T> provider) {
60+
this.provider = provider;
61+
}
62+
63+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package paintcomponents;
2+
3+
4+
public interface DataFromPointDataProvider<T> {
5+
6+
public T provideInformationToDataFromPoint(DataFromPoint<T> dataFromPoint);
7+
public boolean canProvideInformationToDataFromPoint(DataFromPoint<T> dataFromPoint);
8+
9+
10+
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package paintcomponents;
2+
3+
public class DataFromPointNoDataProviderException extends Exception {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package paintcomponents;
2+
3+
public class DataFromPointProviderCannotProvideDataException extends Exception {
4+
5+
}

0 commit comments

Comments
 (0)