2024-10-07 00:58:29 +03:00
|
|
|
|
package Common.Database.Tables;
|
2024-10-09 23:37:58 +03:00
|
|
|
|
import Common.Current_;
|
2024-10-14 15:19:13 +03:00
|
|
|
|
import Common.Database.Objects.DBObject;
|
2024-10-14 20:17:29 +03:00
|
|
|
|
import Common.MainModule_;
|
2024-10-16 21:58:46 +03:00
|
|
|
|
import Common.Passes.PassCode_;
|
2024-10-14 15:19:13 +03:00
|
|
|
|
import Common.Utils.TextLog;
|
2024-10-08 00:39:13 +03:00
|
|
|
|
import Common.Visual.DataSetControlForm;
|
2024-10-15 15:13:57 +03:00
|
|
|
|
import Common.Visual.UI;
|
2024-10-08 22:33:49 +03:00
|
|
|
|
import Common.Visual.Windows.Dialog.DBObjectDialog;
|
|
|
|
|
|
import Common.Visual.Windows.Dialog.DialogFields;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
//-
|
2024-09-18 13:37:11 +03:00
|
|
|
|
public LinkedHashMap<Class, Object> selections = new LinkedHashMap<>();
|
|
|
|
|
|
//---
|
2023-09-17 22:13:42 +03:00
|
|
|
|
public String Name;
|
|
|
|
|
|
public Class<K> k; //класс первичного ключа.
|
|
|
|
|
|
public Class<D> d; //класс объектов.
|
|
|
|
|
|
public LinkedHashMap<K, D> Data = new LinkedHashMap<>(); //наполнение
|
|
|
|
|
|
//-
|
2024-10-17 21:24:55 +03:00
|
|
|
|
protected DataSetControlForm ui = null;
|
2023-12-26 19:12:30 +03:00
|
|
|
|
//--
|
2023-09-17 22:13:42 +03:00
|
|
|
|
public DataSet(Class<K> k_in, Class<D> d_in) {
|
|
|
|
|
|
k = k_in;
|
|
|
|
|
|
d = d_in;
|
|
|
|
|
|
Name = d.getSimpleName();
|
|
|
|
|
|
}
|
2024-10-17 21:24:55 +03:00
|
|
|
|
public DataSetControlForm getUI() {
|
|
|
|
|
|
return ui;
|
2024-10-17 17:22:33 +03:00
|
|
|
|
}
|
2024-10-17 20:04:16 +03:00
|
|
|
|
public void mountUI(JPanel mountPanel_in) {
|
|
|
|
|
|
UI.Clear(mountPanel_in);
|
2024-10-17 21:24:55 +03:00
|
|
|
|
ui = createUI(mountPanel_in);
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
public void ClearUI() {
|
2024-10-17 21:24:55 +03:00
|
|
|
|
if ((ui != null) && ui.isShown()) {
|
|
|
|
|
|
ui.ClearSelection();
|
|
|
|
|
|
ui.Clear();
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public void RefreshUI() {
|
2024-10-17 21:24:55 +03:00
|
|
|
|
if (ui != null) ui.Refresh();
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
public int getRowCountUI() {
|
2024-10-17 21:24:55 +03:00
|
|
|
|
return ui.getRowCount();
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
2024-10-17 22:09:18 +03:00
|
|
|
|
public void SetCurrentObjectByUI(Object pk) {
|
2024-10-17 21:24:55 +03:00
|
|
|
|
if (ui != null) {
|
|
|
|
|
|
ui.ClearSelection(); //сброс текущего объекта и всего что с ним связано.
|
|
|
|
|
|
ui.Select(pk);
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-17 20:04:16 +03:00
|
|
|
|
protected DataSetControlForm createUI(JPanel mountPanel) {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2024-10-16 22:13:47 +03:00
|
|
|
|
public void SelectAll(boolean flag) {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
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) {
|
2024-10-15 15:13:57 +03:00
|
|
|
|
return UI.Warning(getSingleDescription() + " " + object.getBDialogName() + " будет удален(а)");
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
2024-10-16 18:58:23 +03:00
|
|
|
|
public boolean ShowDeleteObjectsDialog(int toDeleteCount) {
|
|
|
|
|
|
return UI.Warning(getPluralDescription() + " в количестве " + toDeleteCount + " будут удалены)");
|
|
|
|
|
|
}
|
2023-09-17 22:13:42 +03:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2024-10-16 18:58:23 +03:00
|
|
|
|
public int getSelectedCount() {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
return (int) Data.values().stream().filter(d -> d.isVisible() && d.isSelected()).count();
|
|
|
|
|
|
}
|
2024-10-16 18:58:23 +03:00
|
|
|
|
public Vector<D> getSelectedItems() {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
return Data.values().stream().filter(d -> d.isVisible() && d.isSelected()).collect(Collectors.toCollection(Vector::new));
|
|
|
|
|
|
}
|
2024-10-16 18:58:23 +03:00
|
|
|
|
public Vector<K> getSelectedKeys() {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
return Data.values().stream().filter(DBObject::isSelected).map(d -> (K) d.getPK()).collect(Collectors.toCollection(Vector::new));
|
|
|
|
|
|
}
|
|
|
|
|
|
//--
|
2023-12-26 19:12:30 +03:00
|
|
|
|
public void ShowUI() {
|
2024-10-17 21:24:55 +03:00
|
|
|
|
if (ui != null) {
|
|
|
|
|
|
ui.Show();
|
2023-12-26 19:12:30 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public void ShowUI(Object key) {
|
2024-10-17 21:24:55 +03:00
|
|
|
|
if (ui != null) {
|
|
|
|
|
|
ui.Show(key);
|
2023-12-26 19:12:30 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-08 00:39:13 +03:00
|
|
|
|
//------------------------------------------------------------------------------------
|
2024-10-09 23:37:58 +03:00
|
|
|
|
public Current_ CurrentName() {
|
2024-10-08 00:39:13 +03:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
public boolean CheckCurrent(TextLog log) {
|
2024-10-16 00:13:09 +03:00
|
|
|
|
return MainModule_.instance.Check(log, CurrentName());
|
2024-10-08 00:39:13 +03:00
|
|
|
|
}
|
2024-10-16 18:58:23 +03:00
|
|
|
|
public boolean CheckSelectedOrCurrent(TextLog log) {
|
2024-10-17 17:22:33 +03:00
|
|
|
|
if ((getSelectedCount() == 0) && (CurrentName() == null || (getCurrent() == null))) {
|
2024-10-16 18:58:23 +03:00
|
|
|
|
log.Writeln_(getPluralDescription() + ":");
|
|
|
|
|
|
log.Writeln_("Отсутствуют отмеченные объекты, или текущий объект!");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2024-10-08 00:39:13 +03:00
|
|
|
|
public void dropCurrent() {
|
2024-10-16 00:13:09 +03:00
|
|
|
|
MainModule_.instance.set(CurrentName(), null);
|
2024-10-08 00:39:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
public D getCurrent() {
|
2024-10-16 00:13:09 +03:00
|
|
|
|
return (D) MainModule_.instance.get(CurrentName());
|
2024-10-08 00:39:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
public void setCurrent(D o) {
|
2024-10-16 00:13:09 +03:00
|
|
|
|
MainModule_.instance.set(CurrentName(), o);
|
2024-10-08 00:39:13 +03:00
|
|
|
|
}
|
2024-10-16 18:58:23 +03:00
|
|
|
|
public Vector<D> getSelectedOrCurrent() {
|
2024-03-22 23:08:51 +03:00
|
|
|
|
Vector<D> res = new Vector<>();
|
2024-10-16 18:58:23 +03:00
|
|
|
|
if (getSelectedCount() > 0)
|
|
|
|
|
|
res = getSelectedItems();
|
2024-03-22 23:08:51 +03:00
|
|
|
|
else {
|
2024-10-16 18:58:23 +03:00
|
|
|
|
if ((CurrentName() != null) && (getCurrent() != null)) {
|
|
|
|
|
|
res.add(getCurrent());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
2024-10-17 17:22:33 +03:00
|
|
|
|
public Vector<K> getSelectedOrCurrentKeys() {
|
2024-10-16 18:58:23 +03:00
|
|
|
|
Vector<K> res = new Vector<>();
|
|
|
|
|
|
if (getSelectedCount() > 0)
|
|
|
|
|
|
res = getSelectedKeys();
|
|
|
|
|
|
else {
|
|
|
|
|
|
if ((CurrentName() != null) && (getCurrent() != null)) {
|
|
|
|
|
|
res.add((K) getCurrent().getPK());
|
2024-03-22 23:08:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
2024-10-08 00:39:13 +03:00
|
|
|
|
public void SaveLastSelections() {
|
2024-10-17 22:54:15 +03:00
|
|
|
|
if (ui!=null) {
|
2024-10-08 00:39:13 +03:00
|
|
|
|
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() {
|
2024-10-17 22:54:15 +03:00
|
|
|
|
if (ui!=null) {
|
2024-10-08 00:39:13 +03:00
|
|
|
|
Object lastPk = selections.get(getClass());
|
|
|
|
|
|
if ((CurrentName() != null) && (lastPk != null)) {
|
2024-10-17 21:24:55 +03:00
|
|
|
|
ui.Select(lastPk);
|
2024-10-08 00:39:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//-------------------------------------------------------------------------------------
|
2024-10-17 17:22:33 +03:00
|
|
|
|
public PassCode_ getDeletePassCode() {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|