Files
VisualSapfor/src/Common/Database/Tables/DataSet.java
2024-10-17 22:18:10 +03:00

256 lines
8.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package Common.Database.Tables;
import Common.Current_;
import Common.Database.Objects.DBObject;
import Common.MainModule_;
import Common.Passes.PassCode_;
import Common.Utils.TextLog;
import Common.Visual.DataSetControlForm;
import Common.Visual.UI;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import Common.Visual.Windows.Dialog.DialogFields;
import javax.swing.*;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Vector;
import java.util.stream.Collectors;
public class DataSet<K, D extends DBObject> extends DataSetAnchestor {
//-
public LinkedHashMap<Class, Object> selections = new LinkedHashMap<>();
//---
public String Name;
public Class<K> k; //класс первичного ключа.
public Class<D> d; //класс объектов.
public LinkedHashMap<K, D> Data = new LinkedHashMap<>(); //наполнение
//-
protected DataSetControlForm ui = null;
//--
public DataSet(Class<K> k_in, Class<D> d_in) {
k = k_in;
d = d_in;
Name = d.getSimpleName();
}
public DataSetControlForm getUI() {
return ui;
}
protected void createFilters() {
}
public void mountUI(JPanel mountPanel_in) {
UI.Clear(mountPanel_in);
ui = createUI(mountPanel_in);
}
public void ClearUI() {
if ((ui != null) && ui.isShown()) {
ui.ClearSelection();
ui.Clear();
}
}
public void RefreshUI() {
if (ui != null) ui.Refresh();
}
public int getRowCountUI() {
return ui.getRowCount();
}
public void SetCurrentObjectByUI(Object pk) {
if (ui != null) {
ui.ClearSelection(); //сброс текущего объекта и всего что с ним связано.
ui.Select(pk);
}
}
protected DataSetControlForm createUI(JPanel mountPanel) {
return null;
}
public boolean hasUI() {
return ui != null;
}
public void SelectAll(boolean flag) {
for (D object : Data.values()) {
if (object.isVisible())
object.Select(flag);
}
RefreshUI();
}
public D getFirstRecord() {
return Data.values().stream().findFirst().orElse(null);
}
public Vector<D> getOrderedRecords(Comparator<D> comparator) {
Vector<D> res = new Vector<>(Data.values());
res.sort(comparator);
return res;
}
@SuppressWarnings("unchecked")
public DBObjectDialog<D, ? extends DialogFields> getDialog() {
return null;
}
public boolean ShowAddObjectDialog(DBObject object) {
return getDialog().ShowDialog(getSingleDescription() + ": добавление", object);
}
public boolean ShowEditObjectDialog(DBObject object) {
DBObjectDialog dialog = getDialog();
dialog.edit = true;
dialog.SetEditLimits();
return dialog.ShowDialog(getSingleDescription() + ": редактирование", object);
}
public boolean ViewObject(DBObject object) {
DBObjectDialog dialog = getDialog();
dialog.SetReadonly();
dialog.ShowDialog(getSingleDescription() + ": просмотр", object);
return false;
}
public boolean ShowDeleteObjectDialog(DBObject object) {
return UI.Warning(getSingleDescription() + " " + object.getBDialogName() + " будет удален(а)");
}
public boolean ShowDeleteObjectsDialog(int toDeleteCount) {
return UI.Warning(getPluralDescription() + " в количестве " + toDeleteCount + " будут удалены)");
}
public String QName() {
return "\"" + Name + "\"";
}
public String getPKName() {
return "";
} //получить имя ключевого поля. нужно для таблиц.
public String getPluralDescription() {
return "";
}
public String getSingleDescription() {
return "";
}
//-
public void put(Object key, D object) {
Data.put((K) key, object);
}
public D get(Object key) {
return Data.get(key);
}
public Object getFieldAt(D object, int columnIndex) {
return null;
}
public void clear() {
Data.clear();
}
public int size() {
return Data.size();
}
public boolean containsKey(Object key) {
return Data.containsKey(key);
}
//-
public Vector<K> getVisibleKeys() {
Comparator<D> comparator = getComparator();
Vector<K> res = new Vector<>();
if (comparator == null) {
for (K key : Data.keySet())
if (Data.get(key).isVisible())
res.add(key);
} else {
Vector<D> raw = new Vector<>();
for (D object : Data.values()) {
if (object.isVisible())
raw.add(object);
}
raw.sort(comparator);
for (D object : raw)
res.add((K) object.getPK());
}
return res;
}
protected Comparator<D> getComparator() {
return null;
}
public int getSelectedCount() {
return (int) Data.values().stream().filter(d -> d.isVisible() && d.isSelected()).count();
}
public Vector<D> getSelectedItems() {
return Data.values().stream().filter(d -> d.isVisible() && d.isSelected()).collect(Collectors.toCollection(Vector::new));
}
public Vector<K> getSelectedKeys() {
return Data.values().stream().filter(DBObject::isSelected).map(d -> (K) d.getPK()).collect(Collectors.toCollection(Vector::new));
}
//--
// применить значение фильтра к фильру объекта напирмер Message.filterValue = text;
public void changeColumnFilterValue(int columnIndex, String text) {
}
public Object getColumnFilterValue(int columnIndex) {
return "";
}
//--
public void ShowUI() {
if (ui != null) {
ui.Show();
}
}
public void ShowUI(Object key) {
if (ui != null) {
ui.Show(key);
}
}
//------------------------------------------------------------------------------------
public Current_ CurrentName() {
return null;
}
public boolean CheckCurrent(TextLog log) {
return MainModule_.instance.Check(log, CurrentName());
}
public boolean CheckSelectedOrCurrent(TextLog log) {
if ((getSelectedCount() == 0) && (CurrentName() == null || (getCurrent() == null))) {
log.Writeln_(getPluralDescription() + ":");
log.Writeln_("Отсутствуют отмеченные объекты, или текущий объект!");
return false;
}
return true;
}
public void dropCurrent() {
MainModule_.instance.set(CurrentName(), null);
}
public D getCurrent() {
return (D) MainModule_.instance.get(CurrentName());
}
public void setCurrent(D o) {
MainModule_.instance.set(CurrentName(), o);
}
public Vector<D> getSelectedOrCurrent() {
Vector<D> res = new Vector<>();
if (getSelectedCount() > 0)
res = getSelectedItems();
else {
if ((CurrentName() != null) && (getCurrent() != null)) {
res.add(getCurrent());
}
}
return res;
}
public Vector<K> getSelectedOrCurrentKeys() {
Vector<K> res = new Vector<>();
if (getSelectedCount() > 0)
res = getSelectedKeys();
else {
if ((CurrentName() != null) && (getCurrent() != null)) {
res.add((K) getCurrent().getPK());
}
}
return res;
}
public void SaveLastSelections() {
if (hasUI()) {
Object lastPk = null;
if ((CurrentName() != null) && (getCurrent() != null))
lastPk = getCurrent().getPK();
if (!selections.containsKey(getClass()))
selections.put(getClass(), lastPk);
else selections.replace(getClass(), lastPk);
}
}
public void RestoreLastSelections() {
if (hasUI()) {
Object lastPk = selections.get(getClass());
if ((CurrentName() != null) && (lastPk != null)) {
ui.Select(lastPk);
}
}
}
//-------------------------------------------------------------------------------------
public PassCode_ getDeletePassCode() {
return null;
}
}