All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsolutions.client.gwt.custom.table.SwitchableSelectionModel Maven / Gradle / Ivy

There is a newer version: 5.8.4
Show newest version
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *              C E D A R
 *          S O L U T I O N S       "Software done right."
 *           S O F T W A R E
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 2013 Kenneth J. Pronovici.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0.
 * See LICENSE for more information about the licensing terms.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Author   : Kenneth J. Pronovici 
 * Language : Java 6
 * Project  : Common Java Functionality
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.cedarsolutions.client.gwt.custom.table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionModel.AbstractSelectionModel;
import com.google.gwt.view.client.SetSelectionModel;

/**
 * Selection model that allows switching the selection type, for use with DataTable.
 *
 * 

* Code Source *

* *

* This code was initially copied from GWT under the terms of its license, and * was later modified to allow both single and multiple selection types. *

* *
* * * * * * * * * * * * * * * *
Source:{@link com.google.gwt.view.client.MultiSelectionModel}
Version:GWT 2.5.1
Date:December, 2013
*
* * @param Type of the associated table. * @author Kenneth J. Pronovici */ public class SwitchableSelectionModel extends AbstractSelectionModel implements SetSelectionModel { private SelectionType selectionType; private Map selectedSet; private Map> selectionChanges; public SwitchableSelectionModel(ProvidesKey keyProvider, SelectionType selectionType) { super(keyProvider); this.selectionType = selectionType; this.selectedSet = new HashMap(); this.selectionChanges = new HashMap>(); } public void setSelectionType(SelectionType selectionType) { this.selectionType = selectionType; this.selectedSet.clear(); this.selectionChanges.clear(); } public SelectionType getSelectionType() { return this.selectionType; } @Override public void clear() { this.selectionChanges.clear(); this.unselectAll(); this.scheduleSelectionChangeEvent(); } /** Unselect all items. */ private void unselectAll() { for (T value : this.selectedSet.values()) { this.selectionChanges.put(getKey(value), new SelectionChange(value, false)); } } @Override public Set getSelectedSet() { this.resolveChanges(); return new HashSet(this.selectedSet.values()); } /** Get the the selected object, really only makes sense when SelectionType.SINGLE. */ public T getSelectedObject() { this.resolveChanges(); if (this.selectedSet.isEmpty()) { return null; } else { return new ArrayList(this.selectedSet.values()).get(0); } } @Override public boolean isSelected(T object) { this.resolveChanges(); return this.selectedSet.containsKey(getKey(object)); } @Override public void setSelected(T object, boolean selected) { if (this.selectionType == SelectionType.MULTI) { this.selectionChanges.put(getKey(object), new SelectionChange(object, selected)); this.scheduleSelectionChangeEvent(); } else { if (object == null) { this.unselectAll(); this.scheduleSelectionChangeEvent(); } else { if (selected) { this.unselectAll(); this.selectionChanges.put(getKey(object), new SelectionChange(object, true)); this.scheduleSelectionChangeEvent(); } else { if (isSelected(object)) { this.selectionChanges.put(getKey(object), new SelectionChange(object, false)); this.scheduleSelectionChangeEvent(); } } } } } @Override protected void fireSelectionChangeEvent() { if (isEventScheduled()) { setEventCancelled(true); } this.resolveChanges(); } private void resolveChanges() { if (this.selectionChanges.isEmpty()) { return; } boolean changed = false; for (Map.Entry> entry : this.selectionChanges.entrySet()) { Object key = entry.getKey(); SelectionChange value = entry.getValue(); boolean selected = value.isSelected; T oldValue = this.selectedSet.get(key); if (selected) { this.selectedSet.put(key, value.item); Object oldKey = getKey(oldValue); if (!changed) { changed = (oldKey == null) ? (key != null) : !oldKey.equals(key); } } else { if (oldValue != null) { this.selectedSet.remove(key); changed = true; } } } this.selectionChanges.clear(); if (changed) { SelectionChangeEvent.fire(this); } } /** Available selection types. */ public enum SelectionType { SINGLE, MULTI, } /** Stores an item and its pending selection state. */ private static class SelectionChange { private final T item; private final boolean isSelected; SelectionChange(T item, boolean isSelected) { this.item = item; this.isSelected = isSelected; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy