OSDN Git Service

c55a64a32032ce16484091752719abe13ca858bd
[everfolder/source.git] / source / workspace / EverFolder / src / com / yuji / ef / IconFrameLayout.java
1 package com.yuji.ef;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.content.Context;
7 import android.graphics.Color;
8 import android.graphics.PorterDuff.Mode;
9 import android.graphics.Rect;
10 import android.util.AttributeSet;
11 import android.view.MotionEvent;
12 import android.view.View;
13 import android.view.View.OnLongClickListener;
14 import android.view.animation.Animation;
15 import android.view.animation.Animation.AnimationListener;
16 import android.view.animation.AnimationUtils;
17 import android.widget.FrameLayout;
18
19 public class IconFrameLayout extends FrameLayout implements OnLongClickListener {
20         private EverFolderActivity activity = null;
21
22         private LabelIconView target = null;
23         private LabelIconView destTarget = null;
24         private LabelIconView selectedTarget = null;
25         private LabelIconView cutTarget = null;
26         private List<LabelIconView> labelIconViewList = new ArrayList<LabelIconView>();
27
28         private Animation anime;
29         private int currentX;
30         private int currentY;
31         private int offsetX;
32         private int offsetY;
33         private boolean longClickFlg = false;
34         private IconScrollView scrollView;
35         private Context context = this.getContext();
36         private boolean elabledTouchEvent = true;
37         
38         public IconFrameLayout(Context context) {
39                 super(context);
40         }
41
42         public IconFrameLayout(Context context, AttributeSet attrs) {
43                 super(context, attrs);
44         }
45
46         public IconFrameLayout(Context context, AttributeSet attrs, int defStyle) {
47                 super(context, attrs, defStyle);
48         }
49
50         public void setActivity(EverFolderActivity activity) {
51                 this.activity = activity;
52         }
53
54         @Override
55         protected void onLayout(boolean changed, int left, int top, int right,
56                         int bottom) {
57                 super.onLayout(changed, left, top, right, bottom);
58
59                 int size = this.getChildCount();
60                 for (int i = 0; i < size; i++) {
61                         LabelIconView vv = (LabelIconView) this.getChildAt(i);
62                         int x2 = vv.getInitX();
63                         int y2 = vv.getInitY();
64                         // vv.init(x2, y2);
65                         vv.layout(x2, y2, x2 + vv.getWidth(), y2 + vv.getHeight());
66                 }
67         }
68
69         public LabelIconView getSelectedTarget() {
70                 return selectedTarget;
71         }
72
73         public void addView(LabelIconView child) {
74                 addView(child, true);
75         }
76
77         public void addView(LabelIconView child, boolean isAddView) {
78                 if (isAddView) {
79                         super.addView(child);
80                 }
81                 labelIconViewList.add(child);
82         }
83
84         public void removeAllViews() {
85                 super.removeAllViews();
86                 labelIconViewList.clear();
87         }
88
89         public void moveTop(LabelIconView child) {
90                 this.removeView(child);
91                 this.addView(child);
92         }
93
94         private LabelIconView getView(int x, int y, LabelIconView v,
95                         List<LabelIconView> list, boolean flag) {
96                 Rect rect = new Rect();
97                 LabelIconView sv;
98
99                 if (v != null) {
100                         if (flag) {
101                                 v.getHitRect(rect);
102                                 if (rect.contains(x, y)) {
103                                         return v;
104                                 }
105                         }
106                 }
107                 for (LabelIconView view : list) {
108                         if (view != v) {
109                                 view.getHitRect(rect);
110                                 if (rect.contains(x, y)) {
111                                         return view;
112                                 }
113                         }
114                 }
115                 return null;
116         }
117
118         public void setScrollView(final IconScrollView scrollView) {
119                 this.scrollView = scrollView;
120                 this.setOnLongClickListener(this);
121                 // this.setLongClickable(true);
122
123                 anime = AnimationUtils.loadAnimation(context, R.anim.sample);
124                 anime.setAnimationListener(new AnimationListener() {
125                         public void onAnimationStart(Animation animation) {
126                         }
127
128                         public void onAnimationRepeat(Animation animation) {
129                         }
130
131                         public void onAnimationEnd(Animation animation) {
132                                 // if (isBt2Click) {
133                                 // bt2.performClick();
134                                 // isBt2Click = false;
135                                 // }
136                                 target = null;
137                                 destTarget = null;
138
139                                 scrollView.setScrollable(true);
140                                 scrollView.requestDisallowInterceptTouchEvent(false);
141                                 scrollView.invalidate();
142                         }
143                 });
144         }
145
146         public void refresh() {
147                 if (selectedTarget != null) {
148                         long id = selectedTarget.getNodeId();
149                         for (LabelIconView view : labelIconViewList) {
150                                 if (view.getNodeId() == id) {
151                                         selectedTarget = view;
152                                         target = view;
153                                         selectedTarget.setAlpha(255);
154                                         selectedTarget.setColorFilter(Color.RED, Mode.LIGHTEN);
155                                 }
156                         }
157                 }
158                 if (cutTarget != null) {
159                         long id = cutTarget.getNodeId();
160                         for (LabelIconView view : labelIconViewList) {
161                                 if (view.getNodeId() == id) {
162                                         cutTarget = view;
163                                         cutTarget.clearColorFilter();
164                                         cutTarget.setAlpha(64);
165                                 }
166                         }
167                 }
168         }
169
170         public void clearTarget() {
171                 if (selectedTarget != null){
172                         selectedTarget.clearColorFilter();
173                         selectedTarget = null;
174                 }
175         }
176         
177         public LabelIconView getCutTarget() {
178                 return cutTarget;
179         }
180
181         public void setCutTarget(LabelIconView cutTarget) {
182                 this.cutTarget = cutTarget;
183                 selectedTarget = null;
184                 target = null;
185         }
186
187         @Override
188         public boolean onTouchEvent(MotionEvent event) {
189                 try {
190                         if (!isElabledTouchEvent()){
191                                 return super.onTouchEvent(event);
192                         }
193
194                         int x = (int) event.getRawX();
195                         int y = (int) event.getRawY();
196
197                         int dx = scrollView.getLeft();
198                         int dy = scrollView.getTop();
199
200                         x -= dx;
201                         y -= dy;
202
203                         int sx = x + scrollView.getScrollX();
204                         int sy = y + scrollView.getScrollY();
205                         LabelIconView v = getView(sx, sy, target, labelIconViewList, true);
206                         LabelIconView obj = (LabelIconView) v;
207
208                         // int size = labelIconViewList.size();
209                         if (target == null) {
210                                 target = obj;
211                         }
212                         if (target == null) {
213                                 return super.onTouchEvent(event);
214                         }
215
216                         // コメントにすると何が起きるか?
217                         // scrollView.requestDisallowInterceptTouchEvent(true);
218
219                         if (event.getAction() == MotionEvent.ACTION_MOVE) {
220                                 if (!longClickFlg) {
221                                         return super.onTouchEvent(event);
222                                 }
223                                 if (target instanceof StatusIconView) {
224                                         return super.onTouchEvent(event);
225                                 }
226                                 obj = target;
227
228                                 int sh = scrollView.getHeight();
229                                 int lh = this.getHeight();
230                                 int maxY = lh - sh;
231                                 int py = scrollView.getScrollY();
232                                 int sdy = 10;
233                                 int N = 100;
234                                 if (y < N) {
235                                         if (py > 0) {
236                                                 if (py < sdy) {
237                                                         sdy = py;
238                                                 }
239                                                 scrollView.smoothScrollTo(0, py - sdy);
240                                                 currentY -= sdy;
241                                         }
242                                 } else if (sh - y < N) {
243                                         if (py < maxY) {
244                                                 if (maxY - py < sdy) {
245                                                         sdy = maxY - py;
246                                                 }
247                                                 scrollView.smoothScrollTo(0, py + sdy);
248                                                 currentY += sdy;
249                                         }
250                                 }
251
252                                 int diffX = offsetX - x;
253                                 int diffY = offsetY - y;
254
255                                 currentX -= diffX;
256                                 currentY -= diffY;
257                                 obj.layout(currentX, currentY, currentX + obj.getWidth(),
258                                                 currentY + obj.getHeight());
259
260                                 offsetX = x;
261                                 offsetY = y;
262
263                                 v = getView(sx, sy, target, labelIconViewList, false);
264                                 if (destTarget == null) {
265                                         if (v != null) {
266                                                 destTarget = v;
267                                                 destTarget.setAlpha(128);
268                                         }
269                                 } else {
270                                         if (v != destTarget) {
271                                                 destTarget.setAlpha(255);
272                                                 destTarget = v;
273                                                 if (destTarget != null) {
274                                                         destTarget.setAlpha(128);
275                                                 }
276                                         }
277                                 }
278                         } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
279                                 // if (obj == target){
280                                 if (obj != null) {
281                                         if (obj != target) {
282                                                 target.clearColorFilter();
283                                                 target = obj;
284                                         }
285                                         // if (target instanceof StatusIconView){
286                                         if (target.getSelectedView(sx - target.getLeft(), sy
287                                                         - target.getTop()) instanceof StatusIconView) {
288 //                                              StatusIconView siv = (StatusIconView) target;
289 //                                              LabelIconView rv = siv.getRoot();
290 //                                              long id = rv.getNodeId();
291                                                 long id = target.getNodeId();
292                                                 
293                                                 activity.execute(id);
294                                                 return super.onTouchEvent(event);
295                                                 //return true;
296                                         }
297
298                                         if (selectedTarget != null && selectedTarget == target) {
299                                                 long id = selectedTarget.getNodeId();
300
301                                                 target.clearColorFilter();
302                                                 target = null;
303                                                 selectedTarget = target;
304                                                 activity.targetSelectedChanged(true);
305
306                                                 activity.executeView(id);
307
308                                                 return super.onTouchEvent(event);
309                                         }
310                                         
311                                         // ここに移動
312                                         scrollView.requestDisallowInterceptTouchEvent(true);
313
314                                         offsetX = x;
315                                         offsetY = y;
316                                         // TODO
317                                         currentX = obj.getLeft();
318                                         currentY = obj.getTop();
319
320                                         target.setAlpha(255);
321                                         target.setColorFilter(Color.RED, Mode.LIGHTEN);
322                                         selectedTarget = target;
323                                         activity.targetSelectedChanged(true);
324
325                                         // コメントにしたが、デグレードするかも
326                                         //scrollView.setScrollable(false);
327                                 } else {
328                                         target.clearColorFilter();
329                                         target = null;
330                                         selectedTarget = target;
331                                         activity.targetSelectedChanged(false);
332                                 }
333                                 // return false;
334                         } else if (event.getAction() == MotionEvent.ACTION_UP) {
335                                 if (!longClickFlg) {
336                                         return super.onTouchEvent(event);
337                                 }
338                                 if (target instanceof StatusIconView) {
339                                         return super.onTouchEvent(event);
340                                         //return true;
341                                 }
342                                 obj = target;
343
344                                 // obj.setVisibility(View.GONE);
345                                 if (destTarget == null || destTarget instanceof StatusIconView) {
346                                         obj.setAnimation(anime);
347                                         obj.startAnimation(anime);
348                                         // layout.removeView(obj);
349
350                                         int srcX = target.getInitX();
351                                         int srcY = target.getInitY();
352                                         target.layout(srcX, srcY, srcX + target.getWidth(), srcY
353                                                         + target.getHeight());
354                                         target.setAlpha(255);
355                                 } else {
356                                         long src = target.getNodeId();
357                                         long dst = destTarget.getNodeId();
358                                         activity.execute(src, dst);
359
360                                         // TODO
361                                         target = null;
362                                         destTarget = null;
363
364                                         // T29171
365                                         clearTarget();
366
367                                         scrollView.setScrollable(true);
368                                         scrollView.requestDisallowInterceptTouchEvent(false);
369                                         scrollView.invalidate();
370                                 }
371
372                                 longClickFlg = false;
373                         }
374
375                         return super.onTouchEvent(event);
376                 } catch (Exception e) {
377                         e.printStackTrace();
378                         return false;
379                 }
380         }
381
382         @Override
383         public boolean onLongClick(View view) {
384                 if (!isElabledTouchEvent()){
385                         return true;
386                 }
387                 if (target == null) {
388                         return false;
389                 }
390                 if (target instanceof StatusIconView) {
391                         return false;
392                 }
393
394                 LabelIconView v = (LabelIconView) target;
395                 v.setAlpha(128);
396                 v.clearColorFilter();
397
398                 moveTop(target);
399
400                 longClickFlg = true;
401                 return true;
402         }
403         
404         public boolean isElabledTouchEvent() {
405                 return elabledTouchEvent;
406         }
407
408         public void setElabledTouchEvent(boolean elabledTouchEvent) {
409                 this.elabledTouchEvent = elabledTouchEvent;
410         }       
411 }