OSDN Git Service

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