Skip to content

Commit

Permalink
Add javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
jourquin committed Jan 31, 2018
1 parent 92283b2 commit 30c78c7
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
Expand Down
42 changes: 41 additions & 1 deletion src/javax/swing/CheckListModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,62 @@

import java.util.Collection;

/**
* Extended list model.
*
* @param <E> Type of element in this model.
*/
public interface CheckListModel<E> extends ListModel<E> {

/**
* Add an item to the check list.
*
* @param router Item
*/
public void addItem(E router);

/**
* Check an item.
*
* @param index Index of the item to check.
* @return Checked item
*/
public E checkItem(int index);

/**
* Check an item.
*
* @param item Item to check.
* @return Checked item
*/
public E checkItem(E item);

/** Clear the list. */
public void clear();

/**
* Returns a collection of checked item.
*
* @return Collection of checked items.
*/
public Collection<E> getCheckedItems();

@Override
public E getElementAt(int index);

/**
* Tests if an item is checked.
*
* @param index Index of the item to test.
* @return True if the item is checked
*/
public boolean isChecked(int index);

public E troggleItem(int index);
/**
* Toggle the state of an item.
*
* @param index Index of the item to toggle
* @return The item
*/
public E toggleItem(int index);
}
5 changes: 5 additions & 0 deletions src/javax/swing/CheckListSelectionListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

package javax.swing;

/**
* Selection listener.
*
* @param <T> Item
*/
public interface CheckListSelectionListener<T> {
void selectionChanged(T item);
}
6 changes: 4 additions & 2 deletions src/javax/swing/DefaultCheckListModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

public class DefaultCheckListModel<E> extends AbstractListModel<E> implements CheckListModel<E> {

private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 726416792816178211L;

private List<CheckableItem<E>> list;

/** Default constructor. */
public DefaultCheckListModel() {
list = new ArrayList<>();
}
Expand Down Expand Up @@ -101,7 +103,7 @@ public boolean isChecked(int index) {
}

@Override
public E troggleItem(int index) {
public E toggleItem(int index) {
CheckableItem<E> item = list.get(index);
item.setSelected(!item.isSelected());
fireContentsChanged(this, list.indexOf(item), list.indexOf(item));
Expand Down
56 changes: 55 additions & 1 deletion src/javax/swing/JCheckList.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@
/** A JList with checkable items. */
public class JCheckList<E> extends JList<E> {

/**
* Inner class representing a checkable item.
*
* @param <T> Item value
*/
public static class CheckableItem<T> {
private boolean isSelected;
private T value;

/**
* Constructor.
*
* @param value Item value
*/
public CheckableItem(T value) {
this.value = value;
isSelected = false;
Expand Down Expand Up @@ -59,6 +69,11 @@ public boolean equals(Object obj) {
return false;
}

/**
* Returns the item value.
*
* @return Item value
*/
public T getValue() {
return value;
}
Expand All @@ -71,10 +86,20 @@ public int hashCode() {
return result;
}

/**
* Tests if the item is selected.
*
* @return True if selected
*/
public boolean isSelected() {
return isSelected;
}

/**
* Selects the item.
*
* @param newValue True to select, false to unselect
*/
public void setSelected(boolean newValue) {
isSelected = newValue;
}
Expand All @@ -85,9 +110,11 @@ public String toString() {
}
}

/** Inner class to render the check list. */
protected class CheckListRenderer extends JCheckBox implements ListCellRenderer<Object> {
private static final long serialVersionUID = 1L;

/** Constructor. */
public CheckListRenderer() {
setBackground(UIManager.getColor("List.textBackground"));
setForeground(UIManager.getColor("List.textForeground"));
Expand Down Expand Up @@ -129,7 +156,7 @@ public void mouseClicked(MouseEvent e) {
if (index == -1) {
return;
}
E item = getModel().troggleItem(index);
E item = getModel().toggleItem(index);
Rectangle rect = getCellBounds(index, index);
repaint(rect);
fireCheckListSelectionChanged(item);
Expand All @@ -154,10 +181,20 @@ public void intervalRemoved(ListDataEvent e) {
};
}

/**
* Adds a selection listener.
*
* @param listener The CheckListSelectionListener to add
*/
public void addCheckListSelectionListener(CheckListSelectionListener<E> listener) {
listeners.add(listener);
}

/**
* Checks an item.
*
* @param element The item to check.
*/
public void checkItem(E element) {
E item = getModel().checkItem(element);
fireCheckListSelectionChanged(item);
Expand All @@ -181,6 +218,12 @@ protected void fireCheckListSelectionChanged(E item) {
}
}

/**
* Gets an item.
*
* @param index The index of gthe item to get.
* @return The item.
*/
public E getItem(int index) {
return model.getElementAt(index);
}
Expand All @@ -190,10 +233,21 @@ public CheckListModel<E> getModel() {
return model;
}

/**
* Tests if an item is checked.
*
* @param index The index of the item to check
* @return The item
*/
public boolean isChecked(int index) {
return model.isChecked(index);
}

/**
* Removes the selection listener.
*
* @param listener The listener to remove
*/
public void removeCheckListSelectionListener(CheckListSelectionListener<E> listener) {
listeners.remove(listener);
}
Expand Down

0 comments on commit 30c78c7

Please sign in to comment.