1 | package org.honours.widgets;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Rectangle;
|
---|
5 | import java.awt.event.MouseEvent;
|
---|
6 | import java.awt.event.MouseListener;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.swing.JPanel;
|
---|
11 |
|
---|
12 | import org.expeditee.gui.DisplayIO;
|
---|
13 | import org.expeditee.gui.Frame;
|
---|
14 | import org.expeditee.gui.FrameIO;
|
---|
15 | import org.expeditee.gui.MessageBay;
|
---|
16 | import org.expeditee.items.Item;
|
---|
17 | import org.expeditee.items.ItemParentStateChangedEvent;
|
---|
18 | import org.expeditee.items.Text;
|
---|
19 | import org.expeditee.items.widgets.DataFrameWidget;
|
---|
20 | import org.expeditee.items.widgets.WidgetCorner;
|
---|
21 |
|
---|
22 | public class CollectionSpaceDropBox extends DataFrameWidget {
|
---|
23 |
|
---|
24 | private JPanel _dropBox;
|
---|
25 | String[] _args = null;
|
---|
26 |
|
---|
27 | public CollectionSpaceDropBox(Text source, String[] args){
|
---|
28 | super(source, new JPanel(), 280,280,-1,400,400,-1);
|
---|
29 | _dropBox = (JPanel)_swingComponent;
|
---|
30 | _dropBox.setBackground(Color.getHSBColor(336, 28, 84));
|
---|
31 |
|
---|
32 | //add mouse actions to drop box.
|
---|
33 | _dropBox.addMouseListener(new MouseListener() {
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public void mouseReleased(MouseEvent e) {
|
---|
37 | MessageBay.displayMessage("Mouse released");
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public void mousePressed(MouseEvent e) {
|
---|
42 | MessageBay.displayMessage("Mouse Pressed");
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | //TODO: mouseExited not always called especially if
|
---|
47 | //mouse is moved very quickly.
|
---|
48 | public void mouseExited(MouseEvent e) {
|
---|
49 |
|
---|
50 | //Get all images in the drop box area of this current frame.
|
---|
51 | Frame curr = DisplayIO.getCurrentFrame();
|
---|
52 | String collectionSpaceName = curr.getFramesetName();
|
---|
53 |
|
---|
54 | List<Item> imagesOnCurrentPanel = retrievePanel(curr);
|
---|
55 |
|
---|
56 | //Then clear the drop boxes on the other frames.
|
---|
57 | for(int i = 1; i <= FrameIO.getLastNumber(collectionSpaceName); i++){
|
---|
58 | Frame f = FrameIO.LoadFrame(collectionSpaceName + i);
|
---|
59 | if(f == null)
|
---|
60 | continue;
|
---|
61 |
|
---|
62 | if(f == curr)
|
---|
63 | continue;
|
---|
64 |
|
---|
65 | clearPanel(f);
|
---|
66 |
|
---|
67 | addItemsBackToPanel(f,imagesOnCurrentPanel);
|
---|
68 |
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | //Then update the cleared boxes so they look like the box on this current frame.
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public void mouseEntered(MouseEvent e) {
|
---|
79 | MessageBay.displayMessage("Mouse Entered");
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public void mouseClicked(MouseEvent e) {
|
---|
84 | MessageBay.displayMessage("Mouse Clicked");
|
---|
85 |
|
---|
86 |
|
---|
87 | }
|
---|
88 | });
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | private static void addItemsBackToPanel(Frame curr, List<Item> imagesOnCurrentPanel){
|
---|
93 | MessageBay.displayMessage("Reached addItemsBackToPanel. Items to add" +
|
---|
94 | " back in: " + imagesOnCurrentPanel.size());
|
---|
95 | if(imagesOnCurrentPanel.size() > 0){
|
---|
96 | for(Item item : imagesOnCurrentPanel){
|
---|
97 | Item copy = item.copy();
|
---|
98 | copy.onParentStateChanged(new ItemParentStateChangedEvent(
|
---|
99 | curr,ItemParentStateChangedEvent.EVENT_TYPE_ADDED));
|
---|
100 | copy.setParent(curr);
|
---|
101 | curr.addItem(copy);
|
---|
102 |
|
---|
103 | curr.setSaved();
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | private static void clearPanel(Frame curr){
|
---|
109 | Rectangle area = getArea(curr.getItems());
|
---|
110 | ArrayList<Item>clear = getItemsWithinArea(curr.getItems(),area);
|
---|
111 |
|
---|
112 | if(clear == null)
|
---|
113 | return;
|
---|
114 |
|
---|
115 | if(clear.size() > 0){
|
---|
116 | int iter = clear.size() - 1;
|
---|
117 |
|
---|
118 | while(iter > -1){
|
---|
119 | Item currItem = clear.get(iter);
|
---|
120 | currItem.onParentStateChanged(new ItemParentStateChangedEvent(curr,
|
---|
121 | ItemParentStateChangedEvent.EVENT_TYPE_REMOVED));
|
---|
122 | curr.removeItem(currItem);
|
---|
123 | curr.setSaved();
|
---|
124 | iter--;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private static ArrayList<Item> retrievePanel(Frame curr){
|
---|
130 |
|
---|
131 | List<Item> items = curr.getItems();
|
---|
132 | ArrayList<Item> retrieve = new ArrayList<Item>();
|
---|
133 |
|
---|
134 | Rectangle area = getArea(items);
|
---|
135 |
|
---|
136 | if(area != null){
|
---|
137 | retrieve = getItemsWithinArea(items,area);
|
---|
138 | }
|
---|
139 |
|
---|
140 | return retrieve;
|
---|
141 | }
|
---|
142 |
|
---|
143 | private static ArrayList<Item> getItemsWithinArea(List<Item> items, Rectangle area){
|
---|
144 | if(area == null)
|
---|
145 | return null;
|
---|
146 | ArrayList<Item> retrieve = new ArrayList<Item>();
|
---|
147 |
|
---|
148 | for(Item item : items){
|
---|
149 | if(item instanceof Text){
|
---|
150 |
|
---|
151 | Text t = (Text)item;
|
---|
152 |
|
---|
153 | if(t.getText().startsWith("@i:")){
|
---|
154 | if(t.getX() > area.x && t.getX() < area.x+area.width){
|
---|
155 | if(t.getY() > area.y && t.getY() < area.y+area.height){
|
---|
156 | retrieve.add(t);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | return retrieve;
|
---|
165 | }
|
---|
166 |
|
---|
167 | private static Rectangle getArea(List<Item> items){
|
---|
168 |
|
---|
169 | Rectangle rect = null;
|
---|
170 | int iter = 0;
|
---|
171 | boolean foundPanel = false;
|
---|
172 |
|
---|
173 | while(iter < items.size() && !foundPanel){
|
---|
174 |
|
---|
175 | Item item = items.get(iter);
|
---|
176 | if(item instanceof WidgetCorner){
|
---|
177 | WidgetCorner wc = (WidgetCorner)item;
|
---|
178 | Text wcsource = (Text)wc.getWidgetSource().getSource();
|
---|
179 |
|
---|
180 | if(wcsource.getText().startsWith("@iw: org.honours.widgets.CollectionSpaceDropBox") ||
|
---|
181 | wcsource.getText().startsWith("@iw:org.honours.widgets.CollectionSpaceDropBox")){
|
---|
182 |
|
---|
183 | rect = wc.getEnclosedRectangle();
|
---|
184 | foundPanel = true;
|
---|
185 | }
|
---|
186 |
|
---|
187 | }
|
---|
188 |
|
---|
189 | iter++;
|
---|
190 | }
|
---|
191 |
|
---|
192 | return rect;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | protected String[] getArgs() {
|
---|
198 | // TODO Auto-generated method stub
|
---|
199 | return null;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|