|
1 | 1 | package com.duy.editor.theme; |
2 | 2 |
|
| 3 | +import android.content.Context; |
| 4 | +import android.core.text.SpannableStringBuilder; |
| 5 | +import android.core.widget.EditAreaView; |
3 | 6 | import android.os.Bundle; |
4 | 7 | import android.support.annotation.NonNull; |
5 | 8 | import android.support.annotation.Nullable; |
6 | 9 | import android.support.v4.app.Fragment; |
7 | 10 | import android.support.v7.widget.DividerItemDecoration; |
8 | 11 | import android.support.v7.widget.LinearLayoutManager; |
9 | 12 | import android.support.v7.widget.RecyclerView; |
| 13 | +import android.text.style.CharacterStyle; |
10 | 14 | import android.view.LayoutInflater; |
11 | 15 | import android.view.View; |
12 | 16 | import android.view.ViewGroup; |
| 17 | +import android.widget.TextView; |
13 | 18 |
|
14 | 19 | import com.duy.ccppcompiler.R; |
| 20 | +import com.duy.ide.editor.theme.ThemeLoader; |
15 | 21 | import com.duy.ide.editor.theme.model.EditorTheme; |
16 | 22 | import com.jecelyin.editor.v2.Preferences; |
| 23 | +import com.jecelyin.editor.v2.editor.Highlighter; |
| 24 | +import com.jecelyin.editor.v2.highlight.Buffer; |
| 25 | +import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView; |
| 26 | + |
| 27 | +import org.gjt.sp.jedit.Catalog; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Comparator; |
| 32 | +import java.util.HashMap; |
17 | 33 |
|
18 | 34 | public class EditorThemeFragment extends Fragment { |
19 | 35 | private RecyclerView mRecyclerView; |
20 | 36 | private EditorThemeAdapter mEditorThemeAdapter; |
21 | 37 | private Preferences mPreferences; |
22 | 38 |
|
23 | | - public static EditorThemeFragment newInstance() { |
24 | | - |
25 | | - Bundle args = new Bundle(); |
26 | | - |
27 | | - EditorThemeFragment fragment = new EditorThemeFragment(); |
28 | | - fragment.setArguments(args); |
29 | | - return fragment; |
30 | | - } |
31 | 39 |
|
32 | 40 | @Nullable |
33 | 41 | @Override |
@@ -57,4 +65,137 @@ private int findThemeIndex(EditorTheme editorTheme) { |
57 | 65 | return position; |
58 | 66 | } |
59 | 67 |
|
| 68 | + public static class EditorThemeAdapter extends RecyclerView.Adapter<EditorThemeAdapter.ViewHolder> implements FastScrollRecyclerView.SectionedAdapter { |
| 69 | + private final ArrayList<EditorTheme> mEditorThemes; |
| 70 | + private Context mContext; |
| 71 | + private OnThemeSelectListener onThemeSelectListener; |
| 72 | + |
| 73 | + public EditorThemeAdapter(Context context) { |
| 74 | + mContext = context; |
| 75 | + mEditorThemes = ThemeLoader.getAll(context); |
| 76 | + Collections.sort(mEditorThemes, new Comparator<EditorTheme>() { |
| 77 | + @Override |
| 78 | + public int compare(EditorTheme o1, EditorTheme o2) { |
| 79 | + return o1.getName().compareTo(o2.getName()); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + public int getPosition(EditorTheme editorTheme) { |
| 85 | + return mEditorThemes.indexOf(editorTheme); |
| 86 | + } |
| 87 | + |
| 88 | + @NonNull |
| 89 | + @Override |
| 90 | + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 91 | + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_theme, parent, false); |
| 92 | + return new ViewHolder(view); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onBindViewHolder(@NonNull final ViewHolder holder, int position) { |
| 97 | + final EditorTheme editorTheme = mEditorThemes.get(position); |
| 98 | + |
| 99 | + final String title = makeTitle(position, editorTheme); |
| 100 | + holder.mTxtName.setText(title); |
| 101 | + EditAreaView editorView = holder.mEditorView; |
| 102 | + |
| 103 | + Buffer buffer = new Buffer(); |
| 104 | + Highlighter highlighter = new Highlighter(); |
| 105 | + editorView.setTheme(editorTheme); |
| 106 | + editorView.setEnabled(false); |
| 107 | + editorView.setFocusable(false); |
| 108 | + editorView.setFocusableInTouchMode(false); |
| 109 | + |
| 110 | + buffer.setMode(Catalog.getModeByName("C++"), mContext); |
| 111 | + editorView.setText(new SpannableStringBuilder()); |
| 112 | + editorView.getText().insert(0, getSampleData()); |
| 113 | + buffer.setEditable(editorView.getText()); |
| 114 | + buffer.insert(0, getSampleData()); |
| 115 | + |
| 116 | + HashMap<Integer, ArrayList<? extends CharacterStyle>> colorsMap = new HashMap<>(); |
| 117 | + int lineCount = buffer.getLineManager().getLineCount(); |
| 118 | + highlighter.highlight(buffer, editorTheme, colorsMap, editorView.getText(), 0, lineCount - 1); |
| 119 | + |
| 120 | + holder.mBtnSelect.setOnClickListener(new View.OnClickListener() { |
| 121 | + @Override |
| 122 | + public void onClick(View v) { |
| 123 | + if (onThemeSelectListener != null) { |
| 124 | + onThemeSelectListener.onThemeSelected(editorTheme); |
| 125 | + } |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + private String makeTitle(int position, EditorTheme editorTheme) { |
| 131 | + return (position + 1) + ". " + editorTheme.getName(); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public int getItemCount() { |
| 136 | + return mEditorThemes.size(); |
| 137 | + } |
| 138 | + |
| 139 | + private CharSequence getSampleData() { |
| 140 | + return "// C++ Program to Find Largest Element of an Array\n" + |
| 141 | + "\n" + |
| 142 | + "// This program takes n number of element from user (where, n is specified by user) and stores data in an array. Then, this program displays the largest element of that array using loops.\n" + |
| 143 | + "\n" + |
| 144 | + "#include <iostream>\n" + |
| 145 | + "\n" + |
| 146 | + "using namespace std;\n" + |
| 147 | + "\n" + |
| 148 | + "int main() {\n" + |
| 149 | + " int i, n;\n" + |
| 150 | + " float arr[100];\n" + |
| 151 | + "\n" + |
| 152 | + " cout << \"Enter total number of elements(1 to 100): \";\n" + |
| 153 | + " cin >> n;\n" + |
| 154 | + " cout << endl;\n" + |
| 155 | + "\n" + |
| 156 | + " // Store number entered by the user\n" + |
| 157 | + " for (i = 0; i < n; ++i) {\n" + |
| 158 | + " cout << \"Enter Number \" << i + 1 << \" : \";\n" + |
| 159 | + " cin >> arr[i];\n" + |
| 160 | + " }\n" + |
| 161 | + "\n" + |
| 162 | + " // Loop to store largest number to arr[0]\n" + |
| 163 | + " for (i = 1; i < n; ++i) {\n" + |
| 164 | + " // Change < to > if you want to find the smallest element\n" + |
| 165 | + " if (arr[0] < arr[i])\n" + |
| 166 | + " arr[0] = arr[i];\n" + |
| 167 | + " }\n" + |
| 168 | + " cout << \"Largest element = \" << arr[0];\n" + |
| 169 | + "\n" + |
| 170 | + " return 0;\n" + |
| 171 | + "}"; |
| 172 | + } |
| 173 | + |
| 174 | + @NonNull |
| 175 | + @Override |
| 176 | + public String getSectionName(int position) { |
| 177 | + return mEditorThemes.get(position).getName(); |
| 178 | + } |
| 179 | + |
| 180 | + public void setOnThemeSelectListener(OnThemeSelectListener onThemeSelectListener) { |
| 181 | + this.onThemeSelectListener = onThemeSelectListener; |
| 182 | + } |
| 183 | + |
| 184 | + public interface OnThemeSelectListener { |
| 185 | + void onThemeSelected(EditorTheme theme); |
| 186 | + } |
| 187 | + |
| 188 | + static class ViewHolder extends RecyclerView.ViewHolder { |
| 189 | + View mBtnSelect; |
| 190 | + EditAreaView mEditorView; |
| 191 | + TextView mTxtName; |
| 192 | + |
| 193 | + public ViewHolder(View itemView) { |
| 194 | + super(itemView); |
| 195 | + mEditorView = itemView.findViewById(R.id.editor_view); |
| 196 | + mTxtName = itemView.findViewById(R.id.txt_name); |
| 197 | + mBtnSelect = itemView.findViewById(R.id.btn_select); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
60 | 201 | } |
0 commit comments