Skip to content

Commit 2339b83

Browse files
committed
implement diagnostic view
1 parent 51f62b0 commit 2339b83

File tree

5 files changed

+201
-105
lines changed

5 files changed

+201
-105
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.jecelyin.editor.v2.ui.diagnostic;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
import android.support.v7.widget.RecyclerView;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.TextView;
10+
11+
import com.duy.ccppcompiler.R;
12+
import com.duy.ccppcompiler.compiler.diagnostic.Diagnostic;
13+
14+
import java.io.File;
15+
import java.util.List;
16+
17+
/**
18+
* Created by Duy on 28-Apr-18.
19+
*/
20+
21+
public class DiagnosticAdapter extends RecyclerView.Adapter<DiagnosticAdapter.ViewHolder> {
22+
private List<Diagnostic> mDiagnostics;
23+
private Context mContext;
24+
private DiagnosticClickListener mDiagnosticClickListener;
25+
26+
public DiagnosticAdapter(List<Diagnostic> diagnostics, Context context) {
27+
this.mDiagnostics = diagnostics;
28+
this.mContext = context;
29+
}
30+
31+
32+
@NonNull
33+
@Override
34+
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
35+
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_diagnostic, parent, false);
36+
return new ViewHolder(view);
37+
}
38+
39+
@Override
40+
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
41+
final Diagnostic diagnostic = mDiagnostics.get(position);
42+
if (diagnostic.getLineNumber() != Diagnostic.NOPOS) {
43+
holder.txtLine.setText(String.valueOf(diagnostic.getLineNumber()));
44+
if (diagnostic.getColumnNumber() != Diagnostic.NOPOS) {
45+
holder.txtCol.setText(String.valueOf(diagnostic.getColumnNumber()));
46+
} else {
47+
holder.txtCol.getEditableText().clear();
48+
}
49+
} else {
50+
holder.txtLine.getEditableText().clear();
51+
holder.txtCol.getEditableText().clear();
52+
}
53+
Object source = diagnostic.getSource();
54+
if (source instanceof File) {
55+
holder.txtFile.setText(((File) source).getName());
56+
}
57+
holder.txtMessage.setText(diagnostic.getMessage(mContext));
58+
holder.root.setOnClickListener(new View.OnClickListener() {
59+
@Override
60+
public void onClick(View v) {
61+
if (mDiagnosticClickListener != null) {
62+
mDiagnosticClickListener.onDiagnosisClick(diagnostic, v);
63+
}
64+
}
65+
});
66+
}
67+
68+
@Override
69+
public int getItemCount() {
70+
return mDiagnostics.size();
71+
}
72+
73+
public void addAll(List<Diagnostic> diagnostics) {
74+
int start = mDiagnostics.size() - 1;
75+
mDiagnostics.addAll(diagnostics);
76+
notifyItemRangeInserted(start, start + diagnostics.size());
77+
}
78+
79+
public void remove(Diagnostic diagnostic) {
80+
int i = mDiagnostics.indexOf(diagnostic);
81+
if (i >= 0) {
82+
mDiagnostics.remove(i);
83+
notifyItemRemoved(i);
84+
}
85+
}
86+
87+
public void add(Diagnostic diagnostic) {
88+
mDiagnostics.add(diagnostic);
89+
notifyItemInserted(mDiagnostics.size() - 1);
90+
}
91+
92+
public void clear() {
93+
mDiagnostics.clear();
94+
notifyItemRangeRemoved(0, mDiagnostics.size() - 1);
95+
}
96+
97+
public void setDiagnosticClickListener(DiagnosticClickListener diagnosticClickListener) {
98+
this.mDiagnosticClickListener = diagnosticClickListener;
99+
}
100+
101+
102+
static class ViewHolder extends RecyclerView.ViewHolder {
103+
TextView txtLine, txtCol, txtMessage, txtFile;
104+
View root;
105+
106+
ViewHolder(View itemView) {
107+
super(itemView);
108+
txtLine = itemView.findViewById(R.id.txt_line);
109+
txtCol = itemView.findViewById(R.id.txt_col);
110+
txtMessage = itemView.findViewById(R.id.txt_message);
111+
txtFile = itemView.findViewById(R.id.txt_file);
112+
root = itemView;
113+
}
114+
}
115+
}

app/src/main/java/com/jecelyin/editor/v2/ui/diagnostic/FileDiagnosticView.java renamed to app/src/main/java/com/jecelyin/editor/v2/ui/diagnostic/DiagnosticClickListener.java

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,14 @@
1616

1717
package com.jecelyin.editor.v2.ui.diagnostic;
1818

19-
import android.support.v7.widget.RecyclerView;
19+
import android.view.View;
2020

2121
import com.duy.ccppcompiler.compiler.diagnostic.Diagnostic;
2222

23-
import java.util.List;
24-
2523
/**
2624
* Created by Duy on 28-Apr-18.
2725
*/
2826

29-
public class FileDiagnosticView implements DiagnosticContract.View {
30-
private RecyclerView recyclerView;
31-
32-
public FileDiagnosticView(RecyclerView recyclerView) {
33-
this.recyclerView = recyclerView;
34-
}
35-
36-
@Override
37-
public void show(List<Diagnostic> diagnostics) {
38-
39-
}
40-
41-
@Override
42-
public void remove(Diagnostic diagnostic) {
43-
44-
}
45-
46-
@Override
47-
public void add(Diagnostic diagnostic) {
48-
49-
}
50-
51-
@Override
52-
public void clear() {
53-
54-
}
55-
27+
public interface DiagnosticClickListener {
28+
public void onDiagnosisClick(Diagnostic diagnostic, View view);
5629
}

app/src/main/java/com/jecelyin/editor/v2/ui/diagnostic/DiagnosticContract.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public interface View {
3636
}
3737

3838
public interface Presenter {
39-
void onDiagnosticClick(View view, Diagnostic diagnostic);
39+
void onDiagnosticClick(android.view.View view, Diagnostic diagnostic);
4040
}
4141
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2018 Mr Duy
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.jecelyin.editor.v2.ui.diagnostic;
18+
19+
import android.content.Context;
20+
import android.support.v7.widget.LinearLayoutManager;
21+
import android.support.v7.widget.RecyclerView;
22+
import android.view.View;
23+
24+
import com.duy.ccppcompiler.compiler.diagnostic.Diagnostic;
25+
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
29+
/**
30+
* Created by Duy on 28-Apr-18.
31+
*/
32+
33+
public class DiagnosticView implements DiagnosticContract.View, DiagnosticClickListener {
34+
private RecyclerView mRecyclerView;
35+
private Context mContext;
36+
private DiagnosticContract.Presenter mPresenter;
37+
private DiagnosticAdapter mAdapter;
38+
39+
public DiagnosticView(RecyclerView recyclerView, Context context, DiagnosticContract.Presenter presenter) {
40+
this.mRecyclerView = recyclerView;
41+
this.mContext = context;
42+
mPresenter = presenter;
43+
init();
44+
}
45+
46+
private void init() {
47+
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
48+
mAdapter = new DiagnosticAdapter(new ArrayList<Diagnostic>(), mContext);
49+
mAdapter.setDiagnosticClickListener(this);
50+
mRecyclerView.setAdapter(mAdapter);
51+
}
52+
53+
@Override
54+
public void show(List<Diagnostic> diagnostics) {
55+
clear();
56+
addAll(diagnostics);
57+
}
58+
59+
private void addAll(List<Diagnostic> diagnostics) {
60+
mAdapter.addAll(diagnostics);
61+
}
62+
63+
@Override
64+
public void remove(Diagnostic diagnostic) {
65+
mAdapter.remove(diagnostic);
66+
}
67+
68+
@Override
69+
public void add(Diagnostic diagnostic) {
70+
mAdapter.add(diagnostic);
71+
}
72+
73+
@Override
74+
public void clear() {
75+
mAdapter.clear();
76+
}
77+
78+
@Override
79+
public void onDiagnosisClick(Diagnostic diagnostic, View view) {
80+
mPresenter.onDiagnosticClick(view, diagnostic);
81+
}
82+
}

app/src/main/java/com/jecelyin/editor/v2/ui/diagnostic/FileDiagnosticAdapter.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)