Refactoring: Added comments to every class, as well as a copyright
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / PathLayout.java
diff --git a/src/eu/alefzero/owncloud/ui/fragment/PathLayout.java b/src/eu/alefzero/owncloud/ui/fragment/PathLayout.java
new file mode 100644 (file)
index 0000000..e9714c7
--- /dev/null
@@ -0,0 +1,117 @@
+/* ownCloud Android client application\r
+ *   Copyright (C) 2011  Bartek Przybylski\r
+ *\r
+ *   This program is free software: you can redistribute it and/or modify\r
+ *   it under the terms of the GNU General Public License as published by\r
+ *   the Free Software Foundation, either version 3 of the License, or\r
+ *   (at your option) any later version.\r
+ *\r
+ *   This program is distributed in the hope that it will be useful,\r
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ *   GNU General Public License for more details.\r
+ *\r
+ *   You should have received a copy of the GNU General Public License\r
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ */\r
+package eu.alefzero.owncloud.ui.fragment;\r
+\r
+import java.util.LinkedList;\r
+import java.util.Stack;\r
+\r
+import eu.alefzero.owncloud.R;\r
+import eu.alefzero.owncloud.R.drawable;\r
+\r
+import android.content.Context;\r
+import android.util.AttributeSet;\r
+import android.widget.HorizontalScrollView;\r
+import android.widget.ImageView;\r
+import android.widget.LinearLayout;\r
+import android.widget.ScrollView;\r
+import android.widget.TextView;\r
+\r
+/**\r
+ * Part of the ActionBar Layout\r
+ * @author Bartek Przybylski\r
+ *\r
+ */\r
+public class PathLayout extends LinearLayout {\r
+\r
+  private LinkedList<String> paths;\r
+  ScrollView internalScroll;\r
+  LinearLayout view;\r
+\r
+  public PathLayout(Context context) {\r
+    super(context);\r
+    initialize();\r
+  }\r
+  \r
+  public PathLayout(Context context, AttributeSet attrs) {\r
+    super(context, attrs);\r
+    initialize();\r
+  }\r
+\r
+  public String pop() {\r
+    if (paths.size() == 0) {\r
+      return null;\r
+    }\r
+    int start = paths.size()*2-2;\r
+    int count = 2;\r
+    if (paths.size() == 1) {\r
+      start++;\r
+      count--;\r
+    }\r
+    view.removeViews(start, count);\r
+    return paths.removeLast();\r
+  }\r
+\r
+  public void addPath(String path) {\r
+    for (String s : path.split("/")) if (s.length() != 0) push(s);\r
+  }\r
+  \r
+  public void push(String path) {\r
+    // its weird that we cannot declare static imgView as path separator\r
+    if (paths.size() != 0) {\r
+      ImageView iv = new ImageView(getContext());\r
+      iv.setImageDrawable(getResources().getDrawable(R.drawable.breadcrumb));\r
+      iv.setPadding(2, 0, 2, 0);\r
+      view.addView(iv);\r
+    }\r
+    TextView tv = new TextView(getContext());\r
+    tv.setLayoutParams(getLayoutParams());\r
+    tv.setText(path);\r
+    view.addView(tv);\r
+    HorizontalScrollView hsv = (HorizontalScrollView) internalScroll.getChildAt(0);\r
+    hsv.smoothScrollTo(hsv.getMaxScrollAmount()*2, 0);\r
+    paths.addLast(path);\r
+  }\r
+  \r
+  public String peek() {\r
+    return paths.peek();\r
+  }\r
+\r
+  public String getFullPath() {\r
+    String ret = new String();\r
+    for (int i = 0; i < paths.size(); i++) {\r
+      ret += "/" + paths.get(i);\r
+    }\r
+    return ret;\r
+  }\r
+  \r
+  private void initialize() {\r
+    paths = new LinkedList<String>();\r
+    internalScroll = new ScrollView(getContext());\r
+    internalScroll.setFillViewport(true);\r
+    HorizontalScrollView hsv = new HorizontalScrollView(getContext());\r
+    hsv.setSmoothScrollingEnabled(true);\r
+    internalScroll.addView(hsv);\r
+    view = new LinearLayout(getContext());\r
+    addView(internalScroll);\r
+    hsv.addView(view);\r
+    ImageView iv = new ImageView(getContext());\r
+    iv.setImageDrawable(getResources().getDrawable(R.drawable.breadcrumb));\r
+    view.addView(iv);\r
+  }\r
+\r
+}\r