OSDN Git Service

2375da6671de8e85ad10a0f211df8b78131e8e82
[everfolder/source.git] / source / workspace / EverFolder / src / com / yuji / ef / EverFolderActivity.java
1 package com.yuji.ef;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import android.content.BroadcastReceiver;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.database.sqlite.SQLiteDatabase;
12 import android.graphics.Bitmap;
13 import android.graphics.PixelFormat;
14 import android.os.Bundle;
15 import android.text.Editable;
16 import android.text.TextWatcher;
17 import android.view.Gravity;
18 import android.view.Menu;
19 import android.view.MenuItem;
20 import android.view.View;
21 import android.view.View.OnClickListener;
22 import android.view.WindowManager;
23 import android.view.inputmethod.InputMethodManager;
24 import android.widget.Button;
25 import android.widget.EditText;
26 import android.widget.LinearLayout;
27 import android.widget.TextView;
28
29 import com.yuji.ef.common.CommonUtil;
30 import com.yuji.ef.common.Constant;
31 import com.yuji.ef.dao.DatabaseHelper;
32 import com.yuji.ef.dao.DirNode;
33 import com.yuji.ef.dao.FileNode;
34 import com.yuji.ef.dao.LockDao;
35 import com.yuji.ef.dao.Node;
36 import com.yuji.ef.dao.Node.Status;
37 import com.yuji.ef.dao.NodeCacheDao;
38 import com.yuji.ef.dao.NodeComparator;
39 import com.yuji.ef.dao.NodeDao;
40 import com.yuji.ef.dao.NodeDaoImpl;
41 import com.yuji.ef.dao.RootNode;
42 import com.yuji.ef.utility.BaseActivity;
43 import com.yuji.ef.utility.BitmapCacheFactory;
44 import com.yuji.ef.utility.EvernoteIntentUtil;
45 import com.yuji.ef.utility.FolderUtil;
46 import com.yuji.ef.utility.ScaleUtil;
47
48 public class EverFolderActivity extends BaseActivity {
49         private enum ScreenStatus {
50                 Normal, AddNode, DeleteNode, RenameNode
51         };
52
53         private LinearLayout msgLayout;
54         private LinearLayout msgButtonLayout;
55         private Button msgOkButton;
56         private Button msgCancelButton;
57         private EditText msgEditText;
58
59         private IconFrameLayout layout;
60         private TextView confirmMsg;
61         private IconScrollView scrollView;
62         private LinearLayout buttonLayout;
63         private Button leftButton;
64         private Button rightButton;
65         // private Button addButton;
66         // private Button deleteButton;
67
68         private ScreenStatus status;
69         private NodeComparator nodeComparator = new NodeComparator();
70         private BitmapCacheFactory bitmapFactory = BitmapCacheFactory.getInstance();
71         private Node top = null;
72         private int indent = 0;
73         private int bmpPaddingLeft;
74         private int bmpWidth;
75         private int viewY;
76         private boolean lock = false;
77         private boolean isInit = true;
78
79         private BroadcastReceiver receiver = new BroadcastReceiver() {
80                 @Override
81                 public void onReceive(Context context, Intent intent) {
82                         String action = intent.getAction();
83
84                         if (action.compareTo(Constant.ACTION_UPDATE) == 0) {
85                                 LockDao lockDao = (LockDao) LockDao.getInstance();
86                                 lock = lockDao.lock(EverFolderActivity.this,
87                                                 Constant.LOCK_UPDATE_NOTE);
88                                 if (lock) {
89                                         updateList();
90                                 }
91                                 setStatus();
92                         }
93                 }
94         };
95
96         @Override
97         public void onCreate(Bundle savedInstanceState) {
98                 super.onCreate(savedInstanceState);
99                 setContentView(R.layout.main);
100
101                 try {
102                         IntentFilter intentFilter = new IntentFilter();
103                         intentFilter.addAction(Constant.ACTION_UPDATE);
104                         registerReceiver(receiver, intentFilter);
105
106                         scrollView = (IconScrollView) findViewById(R.id.scrollView);
107                         layout = (IconFrameLayout) findViewById(R.id.frameLayout);
108                         layout.setActivity(this);
109                         layout.setScrollView(scrollView);
110
111                         buttonLayout = (LinearLayout) findViewById(R.id.mainButtonLayout);
112                         leftButton = (Button) findViewById(R.id.leftButton);
113                         rightButton = (Button) findViewById(R.id.rightButton);
114                         // addButton = (Button) findViewById(R.id.addButton);
115                         // deleteButton = (Button) findViewById(R.id.deleteButton);
116
117                         msgLayout = (LinearLayout) findViewById(R.id.mainMsgLayout);
118                         confirmMsg = (TextView) findViewById(R.id.mainConfirmMsg);
119                         msgEditText = (EditText) findViewById(R.id.mainMsgEditText);
120                         msgButtonLayout = (LinearLayout) findViewById(R.id.mainMsgButtonLayout);
121                         msgOkButton = (Button) findViewById(R.id.mainMsgOkButton);
122                         msgCancelButton = (Button) findViewById(R.id.mainMsgCancelButton);
123
124                         leftButton.setOnClickListener(new OnClickListener() {
125                                 @Override
126                                 public void onClick(View v) {
127                                         leftButtonOnClick();
128                                 }
129                         });
130                         rightButton.setOnClickListener(new OnClickListener() {
131                                 @Override
132                                 public void onClick(View v) {
133                                         rightButtonOnClick();
134                                 }
135                         });
136                         // addButton.setOnClickListener(new OnClickListener() {
137                         // @Override
138                         // public void onClick(View v) {
139                         // addButtonOnClick();
140                         // }
141                         // });
142                         // deleteButton.setOnClickListener(new OnClickListener() {
143                         // @Override
144                         // public void onClick(View v) {
145                         // deleteButtonOnClick();
146                         // }
147                         // });
148
149                         msgEditText.addTextChangedListener(new TextWatcher() {
150                                 public void afterTextChanged(Editable s) {
151                                         if (status != ScreenStatus.DeleteNode) {
152                                                 String text = s.toString();
153                                                 msgOkButton.setEnabled(text.length() > 0);
154                                         } else {
155                                                 msgOkButton.setEnabled(true);
156                                         }
157                                 }
158
159                                 public void beforeTextChanged(CharSequence s, int start,
160                                                 int count, int after) {
161
162                                 }
163
164                                 public void onTextChanged(CharSequence s, int start,
165                                                 int before, int count) {
166
167                                 }
168                         });
169
170                         msgOkButton.setOnClickListener(new OnClickListener() {
171                                 @Override
172                                 public void onClick(View v) {
173                                         msgOkButtonOnClick(v);
174                                 }
175                         });
176                         msgCancelButton.setOnClickListener(new OnClickListener() {
177                                 @Override
178                                 public void onClick(View v) {
179                                         msgCancelOnClick(v);
180                                 }
181                         });
182
183                         ScaleUtil scaleUtil = ScaleUtil.getInstance();
184                         scaleUtil.init(this);
185
186                         bitmapFactory.init(this);
187                         Node node = new DirNode(null, null);
188                         Bitmap bmp = null;
189                         bmp = bitmapFactory.get(node.getStatusIconId());
190                         bmpPaddingLeft = bmp.getWidth();
191                         bmp = bitmapFactory.get(node.getIconId());
192                         bmpWidth = bmp.getWidth();
193
194                         float fSize = 48 * (1 / 2.0f);
195                         leftButton.setTextSize(fSize);
196                         rightButton.setTextSize(fSize);
197                 } catch (Exception e) {
198                         // TODO
199                         e.printStackTrace();
200                 }
201         }
202
203         @Override
204         public boolean onCreateOptionsMenu(Menu menu) {
205                 boolean ret = super.onCreateOptionsMenu(menu);
206                 int index = 0;
207
208                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE, "カット");
209                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE, "ペースト");
210                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE, "追加");
211                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE, "削除");
212                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE, "リネーム");
213                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE, "設定");
214                 return ret;
215         }
216
217         @Override
218         public boolean onMenuOpened(int featureId, Menu menu) {
219                 boolean ret = super.onMenuOpened(featureId, menu);
220
221                 // #29304
222                 if (menu == null){
223                         return false;
224                 }
225                 
226                 MenuItem item;
227
228                 boolean cutButtonFlag = false;
229                 boolean pastButtonFlag = false;
230                 boolean addButtonFlag = false;
231                 boolean deleteButtonFlag = false;
232                 boolean renameButtonFlag = false;
233                 boolean confButtonFlag = true;
234
235                 if (status == ScreenStatus.Normal) {
236                         LabelIconView target = layout.getSelectedTarget();
237                         NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
238                         long id = -1;
239                         Node node = null;
240                         if (target != null) {
241                                 id = target.getNodeId();
242                                 node = dao.searchById(id);
243                         }
244
245                         if (isDirNode(target, node, false) || isFileNode(node)) {
246                                 cutButtonFlag = true;
247                         }
248                         if (isDirNode(target, node, true) && isCut()) {
249                                 pastButtonFlag = true;
250                         }
251                         if (isDirNode(target, node, true)) {
252                                 addButtonFlag = true;
253                         }
254                         if (isDirNode(target, node, false) && !isNoteBook(node)
255                                         && CommonUtil.isNull(node.getChildrenString())) {
256                                 deleteButtonFlag = true;
257                         }
258                         if (isDirNode(target, node, false) && !isNoteBook(node)) {
259                                 renameButtonFlag = true;
260                         }
261                 }
262
263                 int index = 0;
264                 item = menu.getItem(index++);
265                 item.setVisible(cutButtonFlag);
266                 item = menu.getItem(index++);
267                 item.setVisible(pastButtonFlag);
268                 item = menu.getItem(index++);
269                 item.setVisible(addButtonFlag);
270                 item = menu.getItem(index++);
271                 item.setVisible(deleteButtonFlag);
272                 item = menu.getItem(index++);
273                 item.setVisible(renameButtonFlag);
274                 item = menu.getItem(index++);
275                 item.setVisible(confButtonFlag);
276
277                 return ret;
278         }
279
280         private boolean isFileNode(Node node) {
281                 if (node == null || !(node instanceof FileNode)) {
282                         return false;
283                 }
284                 return true;
285         }
286
287         private boolean isDirNode(LabelIconView target, Node node,
288                         boolean containRoot) {
289                 if (target == null) {
290                         return false;
291                 }
292                 if (node == null) {
293                         return false;
294                 }
295                 if (!containRoot && (node instanceof RootNode)) {
296                         return false;
297                 }
298                 if (!(node instanceof DirNode)) {
299                         return false;
300                 }
301                 return true;
302         }
303
304         private boolean isNoteBook(Node node) {
305                 if (node == null) {
306                         return false;
307                 }
308                 if (CommonUtil.isNull(node.getGuid())) {
309                         return false;
310                 }
311                 return true;
312         }
313
314         private boolean isCut() {
315                 LabelIconView srcTarget = layout.getCutTarget();
316                 if (srcTarget == null) {
317                         return false;
318                 }
319                 return true;
320         }
321
322         @Override
323         public boolean onOptionsItemSelected(MenuItem item) {
324                 Intent intent;
325                 try {
326                         switch (item.getItemId()) {
327                         case Menu.FIRST + 0:
328                                 selectMenuCut();
329                                 break;
330                         case Menu.FIRST + 1:
331                                 selectMenuPast();
332                                 break;
333                         case Menu.FIRST + 2:
334                                 selectMenuAdd();
335                                 break;
336                         case Menu.FIRST + 3:
337                                 selectMenuDelete();
338                                 break;
339                         case Menu.FIRST + 4:
340                                 selectMenuRename();
341                                 break;
342                         case Menu.FIRST + 5:
343                                 // dummy();
344                                 // dummy2();
345                                 intent = new Intent(this, (Class<?>) SettingActivity.class);
346                                 startActivityForResult(intent, 0);
347                                 break;
348                         default:
349                                 break;
350                         }
351                 } catch (Exception e) {
352                         terminate(e);
353                 }
354                 return super.onOptionsItemSelected(item);
355         }
356
357         private void dummy() {
358                 NodeDaoImpl dao = (NodeDaoImpl) NodeCacheDao.getInstance();
359                 List<Node> list = dao.search();
360                 for (Node node : list) {
361                         System.out.println(node.toString());
362                 }
363         }
364
365         private void dummy2() {
366                 LockDao dao = (LockDao) LockDao.getInstance();
367                 boolean b;
368
369                 try {
370                         b = dao.lock(this, "key");
371                         b = dao.lock(this, "key");
372                         dao.unlock(new Object(), "key");
373                         b = dao.lock(this, "key");
374                         dao.unlock(this, "key");
375                         b = dao.lock(this, "key");
376                         b = dao.lock(this, "key");
377                         b = dao.lock(this, "key");
378
379                 } catch (Exception e) {
380                         e.printStackTrace();
381                 }
382
383         }
384
385         private void selectMenuAdd() {
386                 setStatus(ScreenStatus.AddNode);
387         }
388
389         private void selectMenuDelete() {
390                 setStatus(ScreenStatus.DeleteNode);
391         }
392
393         private void selectMenuRename() {
394                 LabelIconView target = layout.getSelectedTarget();
395                 if (target == null) {
396                         return;
397                 }
398
399                 long id = target.getNodeId();
400                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
401                 Node node = dao.searchById(id);
402                 if (node != null) {
403                         String text = node.getName();
404                         msgEditText.setText(text);
405                         setStatus(ScreenStatus.RenameNode);
406                 }
407         }
408
409         private void selectMenuCut() {
410                 LabelIconView target = layout.getSelectedTarget();
411                 if (target == null) {
412                         return;
413                 }
414
415                 layout.setCutTarget(target);
416                 layout.refresh();
417         }
418
419         private void selectMenuPast() {
420                 LabelIconView srcTarget = layout.getCutTarget();
421                 if (srcTarget == null) {
422                         return;
423                 }
424
425                 LabelIconView dstTarget = layout.getSelectedTarget();
426                 if (dstTarget == null) {
427                         return;
428                 }
429
430                 layout.setCutTarget(null);
431
432                 long srcId = srcTarget.getNodeId();
433                 long dstId = dstTarget.getNodeId();
434                 execute(srcId, dstId);
435         }
436
437         private void updateList() {
438                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
439                 top = dao.searchRoot();
440                 if (top == null) {
441                         // TODO
442                 }
443                 layout.removeAllViews();
444                 updateList(top);
445                 layout.refresh();
446         }
447
448         private void updateList(Node parent) {
449                 viewY = 10;
450
451                 // List<Node> list = parent.getChildren();
452                 // for (Node node : list){
453                 // updateList(node, x);
454                 // }
455                 updateList(parent, 0);
456                 layout.setMinimumHeight(viewY);
457                 
458                 scrollView.invalidate();
459                 
460 //              layout.invalidate();
461
462 //              this.runOnUiThread(new Runnable() {
463 //                      @Override
464 //                      public void run() {
465 //                              // ビューを再描画し,UI上で画像変更を反映
466 //                              scrollView.invalidate();
467 //                      }
468 //              });
469         }
470         
471         private void updateList(Node node, int depth) {
472                 int N = (int) (bmpWidth * 0.6);
473                 int M = (int) (bmpWidth * 1.2);
474                 int x = N * (depth - indent);
475
476                 if (depth - indent >= 0
477                                 && !(depth - indent == 0 && node instanceof FileNode)) {
478                         LabelIconView view = new LabelIconView(this);
479                         node.setView(view);
480
481                         Bitmap bmp1 = null;
482                         int id = node.getStatusIconId();
483                         if (id >= 0) {
484                                 // DirNode
485                                 bmp1 = bitmapFactory.get(node.getStatusIconId());
486                         } else {
487                                 // FileNode
488                                 x += bmpPaddingLeft;
489                         }
490                         Bitmap bmp2 = bitmapFactory.get(node.getIconId());
491                         createIconImageView(view, bmp1, bmp2, node.getName(), x, viewY);
492                         // LabelIconView s = view.getStatusImageView();
493                         // if (s != null) {
494                         // s.setNodeId(node.getId());
495                         // }
496                         view.setNodeId(node.getId());
497                 }
498                 viewY += M;
499
500                 if (node.getStatus() != Node.Status.OPEN) {
501                         return;
502                 }
503                 List<Long> idList = node.getChildren();
504                 if (idList == null) {
505                         return;
506                 }
507
508                 List<Node> list = new ArrayList<Node>();
509                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
510                 for (Long id : idList) {
511                         Node n = dao.searchById(id);
512                         if (n == null){
513                                 // TODO
514                                 continue;
515                         }
516                         if (n instanceof DirNode){
517                                 DirNode d = (DirNode)n;
518                                 if (isNoteBook(d) && !d.isSelected()){
519                                         continue;
520                                 }
521                         }
522                         list.add(n);
523                 }
524                 Collections.sort(list, nodeComparator);
525                 for (Node n : list) {
526                         updateList(n, depth + 1);
527                 }
528         }
529
530         private void createIconImageView(LabelIconView v, Bitmap bmp1, Bitmap bmp2,
531                         String text, int x, int y) {
532                 v.init(x, y, bmp1, bmp2, text);
533
534                 android.view.WindowManager.LayoutParams params = new WindowManager.LayoutParams();
535                 params.gravity = Gravity.TOP | Gravity.LEFT;
536                 params.height = WindowManager.LayoutParams.WRAP_CONTENT;
537                 params.width = WindowManager.LayoutParams.WRAP_CONTENT;
538                 params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
539                                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
540                                 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
541                                 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
542                 params.format = PixelFormat.TRANSLUCENT;
543                 params.windowAnimations = 0;
544                 params.x = x;
545                 params.y = y;
546                 v.setLayoutParams(params);
547
548                 // LabelIconView siv = v.getStatusImageView();
549                 // if (siv != null) {
550                 // layout.addView(siv, false);
551                 // }
552
553                 layout.addView(v);
554                 v.layout(x, y, x + v.getWidth(), y + v.getHeight());
555         }
556
557         @Override
558         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
559                 // updateList();
560                 // setStatus();
561                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
562                 if (dao.isEmpty()) {
563                         finish();
564                 }
565                 isInit = true;
566         }
567
568         @Override
569         protected void onStart() {
570                 super.onStart();
571         }
572
573         @Override
574         protected void onResume() {
575                 super.onResume();
576
577                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
578                 if (dao.isEmpty()) {
579                         if (isInit) {
580                                 Intent intent = new Intent(this,
581                                                 (Class<?>) SettingActivity.class);
582                                 startActivityForResult(intent, 0);
583
584                                 isInit = false;
585                         } else {
586                                 finish();
587                                 isInit = true;
588                         }
589                 }
590
591                 LockDao lockDao = (LockDao) LockDao.getInstance();
592                 lock = lockDao.lock(this, Constant.LOCK_UPDATE_NOTE);
593
594                 if (isInit) {
595                         status = ScreenStatus.Normal;
596
597                         if (lock) {
598                                 updateList();
599                         } else {
600                                 layout.removeAllViews();
601                         }
602                         setStatus();
603                         isInit = false;
604                 } else {
605                         setStatus();
606                 }
607         }
608
609         @Override
610         protected void onPause() {
611                 // ロック未取得でも解除する(ゴミ掃除)
612                 LockDao lockDao = (LockDao) LockDao.getInstance();
613                 lockDao.unlock(this, Constant.LOCK_UPDATE_NOTE);
614                 lock = false;
615
616                 super.onPause();
617         }
618
619         private void leftButtonOnClick() {
620                 moveButtonOnClick(-1);
621                 setStatus();
622         }
623
624         private void rightButtonOnClick() {
625                 moveButtonOnClick(1);
626                 setStatus();
627         }
628
629         private void moveButtonOnClick(int dx) {
630                 int mx = 0;
631
632                 if (dx < 0) {
633                         if (indent > 0) {
634                                 mx = -1;
635                         }
636                 } else {
637                         mx = 1;
638                 }
639                 if (mx != 0) {
640                         indent += mx;
641                         removeAllNodeView();
642                         updateList();
643                 }
644         }
645
646         // private void addButtonOnClick() {
647         // setStatus(ScreenStatus.AddNode);
648         // }
649         //
650         // private void deleteButtonOnClick() {
651         // FolderUtil util = FolderUtil.getInstance();
652         // LabelIconView target = layout.getSelectedTarget();
653         // long id = target.getNodeId();
654         // NodeDao dao = (NodeDao) NodeDao.getInstance();
655         // Node node = dao.searchById(id);
656         // if (node != null) {
657         // util.deleteNode(node);
658         // // TODO 三角マークも消さないといけないのでは
659         // layout.removeView(target);
660         // updateList();
661         // setStatus();
662         // }
663         // }
664
665         private void msgOkButtonOnClick(View v) {
666                 FolderUtil util = FolderUtil.getInstance();
667                 LabelIconView target = layout.getSelectedTarget();
668                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
669
670                 if (status == ScreenStatus.AddNode) {
671                         long id = target.getNodeId();
672                         Node parent = dao.searchById(id);
673                         if (parent != null) {
674                                 String text = msgEditText.getText().toString();
675                                 DirNode node = new DirNode(text, null);
676                                 util.addDirNode(parent, node);
677                         }
678                         layout.setCutTarget(null, false);
679                 } else if (status == ScreenStatus.DeleteNode) {
680                         long id = target.getNodeId();
681                         Node node = dao.searchById(id);
682                         if (node != null) {
683                                 util.deleteNode(node);
684                                 layout.removeView(target);
685                         }
686                         layout.setCutTarget(null, false);
687                 } else if (status == ScreenStatus.RenameNode) {
688                         long id = target.getNodeId();
689                         Node node = dao.searchById(id);
690                         if (node != null) {
691                                 String text = msgEditText.getText().toString();
692
693                                 dao.updateName(node, text);
694                         }
695                         layout.setCutTarget(null, false);
696                 }
697                 
698                 closeIME(v);
699                 
700                 layout.clearTarget();
701                 updateList();
702                 setStatus(ScreenStatus.Normal);
703         }
704
705         private void msgCancelOnClick(View v) {
706                 closeIME(v);
707                 
708                 setStatus(ScreenStatus.Normal);
709         }
710
711         private void closeIME(View v){
712                 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
713                 imm.hideSoftInputFromWindow(v.getWindowToken(),0);
714         }
715         
716         public void execute(long srcId, long dstId) {
717                 try {
718                         removeAllNodeView();
719
720                         NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
721
722                         if (srcId < 0) {
723                                 // TODO
724                         }
725                         if (dstId < 0) {
726                                 // TODO
727                         }
728
729                         Node src = dao.searchById(srcId);
730                         Node dst = null;
731                         if (srcId == dstId) {
732                                 dst = src;
733                         } else {
734                                 dst = dao.searchById(dstId);
735                         }
736
737                         if (dstId == src.getParent()) {
738                                 // 同じ場所に移動
739                                 return;
740                         }
741                         if (!isFileNode(src) && !isMovable(srcId, dstId)) {
742                                 // 再帰的な移動
743                                 return;
744                         }
745
746                         Node sp = dao.searchById(src.getParent());
747                         if (sp == null) {
748                                 // TODO
749                         }
750                         Node dp = null;
751                         if (src.getParent() == dst.getParent()) {
752                                 dp = sp;
753                         } else {
754                                 dp = dao.searchById(dst.getParent());
755                         }
756                         if (dp == null) {
757                                 // TODO
758                         }
759
760                         // TODO DBへのアクセス
761                         // sp.remove(src.getId());
762                         SQLiteDatabase db = DatabaseHelper.getInstance()
763                                         .getSQLiteDatabase();
764                         db.beginTransaction();
765                         try {
766                                 dao.removeChildrenIdNT(sp, src.getId());
767                                 if (dst instanceof DirNode) {
768                                         // dst.add(src.getId());
769                                         dao.updateParentNT(src, dst.getId());
770                                         dao.addChildrenIdNT(dst, src.getId());
771                                 } else if (dst instanceof FileNode) {
772                                         // dp.add(src.getId());
773                                         dao.updateParentNT(src, dp.getId());
774                                         dao.addChildrenIdNT(dp, src.getId());
775                                 }
776
777                                 db.setTransactionSuccessful();
778                         } finally {
779                                 db.endTransaction();
780                         }
781                 } finally {
782                         updateList();
783                 }
784         }
785
786         private boolean isMovable(long srcId, long dstId) {
787                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
788
789                 while (dstId >= 0) {
790                         if (srcId == dstId) {
791                                 return false;
792                         }
793                         Node node = dao.searchById(dstId);
794                         if (node == null) {
795                                 // TODO エラー
796                                 return false;
797                         }
798                         if (!(node instanceof DirNode)) {
799                                 return true;
800                         }
801                         dstId = node.getParent();
802                 }
803                 return true;
804         }
805
806         public void execute(long id) {
807                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
808
809                 Node node = dao.searchById(id);
810                 if (node == null) {
811                         // TODO
812                 }
813                 node.toggleStatus();
814                 Status status = node.getStatus();
815
816                 dao.updateStatus(node, status);
817
818                 // removeAllNodeView();
819                 updateList();
820         }
821
822         public void executeView(long id) {
823                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
824
825                 Node node = dao.searchById(id);
826                 if (!isFileNode(node)) {
827                         return;
828                 }
829                 String guid = node.getGuid();
830
831                 EvernoteIntentUtil util = EvernoteIntentUtil.getInstance();
832                 util.viewNote(this, guid);
833         }
834
835         public void targetSelectedChanged(boolean status) {
836                 setStatus();
837         }
838
839         public void removeAllNodeView() {
840                 layout.removeAllViews();
841         }
842
843         private void setStatus() {
844                 setStatus(status);
845         }
846
847         private void setStatus(ScreenStatus status) {
848                 try {
849                         if (this.status != status) {
850                                 this.status = status;
851                         }
852
853                         if (!lock) {
854                                 msgLayout.setVisibility(View.VISIBLE);
855                                 confirmMsg.setText(getString(R.string.mainUpdateMsg));
856                                 msgEditText.setVisibility(View.GONE);
857                                 msgButtonLayout.setVisibility(View.GONE);
858                                 status = ScreenStatus.Normal;
859                                 return;
860                         }
861
862                         if (status == ScreenStatus.Normal) {
863                                 msgLayout.setVisibility(View.GONE);
864                                 // mainMsgEditText.setText("");
865
866                                 boolean leftButtonFlag = true;
867                                 boolean rightButtonFlag = true;
868                                 // boolean addButtonFlag = true;
869                                 // boolean deleteButtonFlag = true;
870
871                                 if (indent <= 0) {
872                                         leftButtonFlag = false;
873                                 }
874
875                                 // LabelIconView target = layout.getSelectedTarget();
876                                 // NodeDao dao = (NodeDao) NodeDao.getInstance();
877                                 // long id = -1;
878                                 // Node node = null;
879                                 // if (target != null) {
880                                 // id = target.getNodeId();
881                                 // node = dao.searchById(id);
882                                 // }
883                                 // if (target == null || node == null || !(node instanceof
884                                 // DirNode))
885                                 // {
886                                 // addButtonFlag = false;
887                                 // }
888                                 // if (target == null || (node != null && node instanceof
889                                 // RootNode))
890                                 // {
891                                 // deleteButtonFlag = false;
892                                 // }
893                                 layout.setElabledTouchEvent(true);
894
895                                 leftButton.setEnabled(leftButtonFlag);
896                                 rightButton.setEnabled(rightButtonFlag);
897                                 // addButton.setEnabled(addButtonFlag);
898                                 // deleteButton.setEnabled(deleteButtonFlag);
899                         } else if (status == ScreenStatus.AddNode) {
900                                 msgLayout.setVisibility(View.VISIBLE);
901                                 msgEditText.setVisibility(View.VISIBLE);
902                                 msgButtonLayout.setVisibility(View.VISIBLE);
903                                 confirmMsg.setText(getString(R.string.mainAddMsg));
904                                 msgEditText.setText("");
905
906                                 layout.setElabledTouchEvent(false);
907
908                                 leftButton.setEnabled(false);
909                                 rightButton.setEnabled(false);
910                         } else if (status == ScreenStatus.DeleteNode) {
911                                 msgLayout.setVisibility(View.VISIBLE);
912                                 msgEditText.setVisibility(View.GONE);
913                                 msgButtonLayout.setVisibility(View.VISIBLE);
914                                 confirmMsg.setText(getString(R.string.mainDeleteMsg));
915
916                                 layout.setElabledTouchEvent(false);
917
918                                 leftButton.setEnabled(false);
919                                 rightButton.setEnabled(false);
920                         } else if (status == ScreenStatus.RenameNode) {
921                                 msgLayout.setVisibility(View.VISIBLE);
922                                 msgEditText.setVisibility(View.VISIBLE);
923                                 msgButtonLayout.setVisibility(View.VISIBLE);
924                                 confirmMsg.setText(getString(R.string.mainRenameMsg));
925
926                                 layout.setElabledTouchEvent(false);
927
928                                 leftButton.setEnabled(false);
929                                 rightButton.setEnabled(false);
930                         }
931                 } catch (Throwable e) {
932                         // TODO
933                         e.printStackTrace();
934                 }
935         }
936 }