Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

Commit

Permalink
Add "select/deselect all" checkbox (#89)
Browse files Browse the repository at this point in the history
* Added checkbox at the beginning of EntryHeader
* Added 'de-select all' checkbox behaviour
* Added a new View in test-app to test new behaviour
  • Loading branch information
siempredelao authored and TomasKypta committed May 2, 2016
1 parent 02638d1 commit 1be58bd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/com/avast/android/butterknifezelezny/form/Entry.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avast.android.butterknifezelezny.form;


import com.avast.android.butterknifezelezny.iface.OnCheckBoxStateChangedListener;
import com.avast.android.butterknifezelezny.model.Element;

import javax.swing.*;
Expand All @@ -16,6 +17,7 @@ public class Entry extends JPanel {
protected EntryList mParent;
protected Element mElement;
protected ArrayList<String> mGeneratedIDs;
protected OnCheckBoxStateChangedListener mListener;
// ui
protected JCheckBox mCheck;
protected JLabel mType;
Expand All @@ -25,6 +27,14 @@ public class Entry extends JPanel {
protected Color mNameDefaultColor;
protected Color mNameErrorColor = new Color(0x880000);

public JCheckBox getCheck() {
return mCheck;
}

public void setListener(final OnCheckBoxStateChangedListener onStateChangedListener) {
this.mListener = onStateChangedListener;
}

public Entry(EntryList parent, Element element, ArrayList<String> ids) {
mElement = element;
mParent = parent;
Expand Down Expand Up @@ -103,6 +113,10 @@ private void checkState() {
mID.setEnabled(false);
mName.setEnabled(false);
}

if (mListener != null) {
mListener.changeState(mCheck.isSelected());
}
}

// classes
Expand Down
34 changes: 33 additions & 1 deletion src/com/avast/android/butterknifezelezny/form/EntryHeader.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
package com.avast.android.butterknifezelezny.form;

import com.avast.android.butterknifezelezny.iface.OnCheckBoxStateChangedListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class EntryHeader extends JPanel {

protected JCheckBox mAllCheck;
protected JLabel mType;
protected JLabel mID;
protected JLabel mEvent;
protected JLabel mName;
protected OnCheckBoxStateChangedListener mAllListener;

public void setAllListener(final OnCheckBoxStateChangedListener onStateChangedListener) {
this.mAllListener = onStateChangedListener;
}

public EntryHeader() {
mAllCheck = new JCheckBox();
mAllCheck.setPreferredSize(new Dimension(40, 26));
mAllCheck.setSelected(false);
mAllCheck.addItemListener(new AllCheckListener());

mType = new JLabel("Element");
mType.setPreferredSize(new Dimension(100, 26));
mType.setFont(new Font(mType.getFont().getFontName(), Font.BOLD, mType.getFont().getSize()));
Expand All @@ -28,7 +43,9 @@ public EntryHeader() {
mName.setFont(new Font(mName.getFont().getFontName(), Font.BOLD, mName.getFont().getSize()));

setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
add(Box.createRigidArea(new Dimension(52, 0)));
add(Box.createRigidArea(new Dimension(1, 0)));
add(mAllCheck);
add(Box.createRigidArea(new Dimension(11, 0)));
add(mType);
add(Box.createRigidArea(new Dimension(12, 0)));
add(mID);
Expand All @@ -38,4 +55,19 @@ public EntryHeader() {
add(mName);
add(Box.createHorizontalGlue());
}

public JCheckBox getAllCheck() {
return mAllCheck;
}

// classes

private class AllCheckListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent itemEvent) {
if (mAllListener != null) {
mAllListener.changeState(itemEvent.getStateChange() == ItemEvent.SELECTED);
}
}
}
}
36 changes: 35 additions & 1 deletion src/com/avast/android/butterknifezelezny/form/EntryList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.avast.android.butterknifezelezny.iface.ICancelListener;
import com.avast.android.butterknifezelezny.iface.IConfirmListener;
import com.avast.android.butterknifezelezny.iface.OnCheckBoxStateChangedListener;
import com.avast.android.butterknifezelezny.model.Element;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
Expand Down Expand Up @@ -32,6 +33,32 @@ public class EntryList extends JPanel {
protected JLabel mHolderLabel;
protected JButton mConfirm;
protected JButton mCancel;
protected EntryHeader mEntryHeader;

private OnCheckBoxStateChangedListener allCheckListener = new OnCheckBoxStateChangedListener() {
@Override
public void changeState(boolean checked) {
for (final Entry entry : mEntries) {
entry.setListener(null);
entry.getCheck().setSelected(checked);
entry.setListener(singleCheckListener);
}
}
};

private OnCheckBoxStateChangedListener singleCheckListener = new OnCheckBoxStateChangedListener() {
@Override
public void changeState(boolean checked) {
boolean result = true;
for (Entry entry : mEntries) {
result &= entry.getCheck().isSelected();
}

mEntryHeader.setAllListener(null);
mEntryHeader.getAllCheck().setSelected(result);
mEntryHeader.setAllListener(allCheckListener);
}
};

public EntryList(Project project, Editor editor, ArrayList<Element> elements, ArrayList<String> ids, boolean createHolder, IConfirmListener confirmListener, ICancelListener cancelListener) {
mProject = project;
Expand All @@ -57,16 +84,19 @@ protected void addInjections() {
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS));
contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPanel.add(new EntryHeader());
mEntryHeader = new EntryHeader();
contentPanel.add(mEntryHeader);
contentPanel.add(Box.createRigidArea(new Dimension(0, 5)));

JPanel injectionsPanel = new JPanel();
injectionsPanel.setLayout(new BoxLayout(injectionsPanel, BoxLayout.PAGE_AXIS));
injectionsPanel.add(Box.createRigidArea(new Dimension(0, 5)));

int cnt = 0;
boolean selectAllCheck = true;
for (Element element : mElements) {
Entry entry = new Entry(this, element, mGeneratedIDs);
entry.setListener(singleCheckListener);

if (cnt > 0) {
injectionsPanel.add(Box.createRigidArea(new Dimension(0, 5)));
Expand All @@ -75,7 +105,11 @@ protected void addInjections() {
cnt++;

mEntries.add(entry);

selectAllCheck &= entry.getCheck().isSelected();
}
mEntryHeader.getAllCheck().setSelected(selectAllCheck);
mEntryHeader.setAllListener(allCheckListener);
injectionsPanel.add(Box.createVerticalGlue());
injectionsPanel.add(Box.createRigidArea(new Dimension(0, 5)));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.avast.android.butterknifezelezny.iface;

public interface OnCheckBoxStateChangedListener {
void changeState(boolean checked);
}
8 changes: 7 additions & 1 deletion test-app/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame"/>
tools:ignore="MergeRootFrame">

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_activity_textview"/>

</FrameLayout>

0 comments on commit 1be58bd

Please sign in to comment.