2024-10-07 00:58:29 +03:00
|
|
|
|
package Common.Database.Tables;
|
|
|
|
|
|
import Common_old.Current;
|
|
|
|
|
|
import Common.Visual.DataSetFilter;
|
|
|
|
|
|
import Common_old.UI.DataSetControlForm;
|
2024-10-07 17:46:38 +03:00
|
|
|
|
import Common.Visual.DataMenuBar;
|
2024-10-07 00:58:29 +03:00
|
|
|
|
import Common_old.UI.Tables.ColumnFilter;
|
|
|
|
|
|
import Common_old.UI.UI;
|
|
|
|
|
|
import Common_old.UI.Windows.Dialog.DBObjectDialog;
|
|
|
|
|
|
import Common_old.UI.Windows.Dialog.DialogFields;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
import Common.Utils.TextLog;
|
2024-10-07 00:58:29 +03:00
|
|
|
|
import Common.Database.Objects.DBObject;
|
|
|
|
|
|
import Common.Visual.CommonUI;
|
2024-10-07 17:46:38 +03:00
|
|
|
|
import Common.Visual.FilterInterface;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
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<>(); //наполнение
|
|
|
|
|
|
//-
|
|
|
|
|
|
public DataSetControlForm ui_;
|
2024-10-07 17:46:38 +03:00
|
|
|
|
protected FilterInterface f_ui; // отображение количества объектов
|
2023-09-17 22:13:42 +03:00
|
|
|
|
//-
|
2023-12-26 19:12:30 +03:00
|
|
|
|
public LinkedHashMap<Integer, ColumnFilter> columnsFilters = new LinkedHashMap<>(); //текстовые фильтры столбцов
|
|
|
|
|
|
//--
|
|
|
|
|
|
protected Vector<DataSetFilter<D>> filters = new Vector<>();
|
|
|
|
|
|
protected void createFilters() {
|
|
|
|
|
|
}
|
|
|
|
|
|
//--
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
public void mountUI(JPanel content_in) {
|
2024-10-07 17:46:38 +03:00
|
|
|
|
CommonUI.Clear(content_in);
|
2023-09-17 22:13:42 +03:00
|
|
|
|
//-->
|
|
|
|
|
|
ui_ = createUI();
|
|
|
|
|
|
ui_.setContent(content_in);
|
|
|
|
|
|
//-->
|
2024-10-07 00:58:29 +03:00
|
|
|
|
if ( CommonUI.menuBars.containsKey(getClass())) {
|
|
|
|
|
|
DataMenuBar bar = CommonUI.menuBars.get(getClass());
|
2023-09-17 22:13:42 +03:00
|
|
|
|
content_in.add(bar, BorderLayout.NORTH);
|
2024-10-07 00:58:29 +03:00
|
|
|
|
setFilterUI(count -> CommonUI.menuBars.get(getClass()).countLabel.setText(String.valueOf(count)));
|
2023-09-17 22:13:42 +03:00
|
|
|
|
if (ui_.hasCheckBox())
|
|
|
|
|
|
bar.createSelectionButtons(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
content_in.add(ui_.getDataPanel(), BorderLayout.CENTER);
|
2023-12-26 19:12:30 +03:00
|
|
|
|
//----
|
|
|
|
|
|
createFilters();
|
|
|
|
|
|
if (!filters.isEmpty()) {
|
2024-10-07 00:58:29 +03:00
|
|
|
|
DataMenuBar menuBar = CommonUI.menuBars.get(getClass());
|
2023-12-26 19:12:30 +03:00
|
|
|
|
for (DataSetFilter<D> filter : filters)
|
|
|
|
|
|
menuBar.addMenus(filter.menu);
|
|
|
|
|
|
}
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
public DataSetControlForm getUi() {
|
|
|
|
|
|
return ui_;
|
|
|
|
|
|
}
|
2024-10-07 17:46:38 +03:00
|
|
|
|
public void setFilterUI(FilterInterface ui_in) {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
f_ui = ui_in;
|
|
|
|
|
|
}
|
|
|
|
|
|
public void ClearUI() {
|
|
|
|
|
|
if ((ui_ != null) && ui_.isShown()) {
|
|
|
|
|
|
ui_.ClearSelection();
|
|
|
|
|
|
ui_.Clear();
|
|
|
|
|
|
if (f_ui != null)
|
|
|
|
|
|
f_ui.ShowNoMatches();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public void RefreshUI() {
|
|
|
|
|
|
if (ui_ != null) ui_.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
public int getRowCountUI() {
|
|
|
|
|
|
return ui_.getRowCount();
|
|
|
|
|
|
}
|
|
|
|
|
|
public void SetCurrentObjectUI(Object pk) {
|
|
|
|
|
|
if (ui_ != null) {
|
|
|
|
|
|
//todo возможно проверить, что текущий объект уже соответствует ключу, и если да, то ничего делать.
|
|
|
|
|
|
ui_.ClearSelection(); //сброс текущего объекта и всего что с ним связано.
|
|
|
|
|
|
ui_.Select(pk);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//столбы/ потом переименовать обратно в getUIColumnNames.сейчас так для скорости переноса.
|
|
|
|
|
|
public String[] getUIColumnNames() {
|
|
|
|
|
|
return new String[]{};
|
|
|
|
|
|
}
|
|
|
|
|
|
protected DataSetControlForm createUI() {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
public boolean hasUI() {
|
|
|
|
|
|
return ui_ != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
public void CheckAll(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 String QName() {
|
|
|
|
|
|
return "\"" + Name + "\"";
|
|
|
|
|
|
}
|
|
|
|
|
|
public String getPKName() {
|
|
|
|
|
|
return "";
|
|
|
|
|
|
} //получить имя ключевого поля. нужно для таблиц.
|
|
|
|
|
|
public String getPluralDescription() {
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
public String getSingleDescription() {
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
//времянки
|
|
|
|
|
|
public Current CurrentName() {
|
|
|
|
|
|
return Current.Undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
public boolean CheckCurrent(TextLog log) {
|
|
|
|
|
|
return Current.Check(log, CurrentName());
|
|
|
|
|
|
}
|
|
|
|
|
|
public boolean hasCurrent() {
|
|
|
|
|
|
return Current.get(CurrentName()) != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
public void dropCurrent() {
|
|
|
|
|
|
Current.set(CurrentName(), null);
|
|
|
|
|
|
}
|
|
|
|
|
|
public D getCurrent() {
|
|
|
|
|
|
return (D) Current.get(CurrentName());
|
|
|
|
|
|
}
|
|
|
|
|
|
public void setCurrent(D o) {
|
|
|
|
|
|
Current.set(CurrentName(), o);
|
|
|
|
|
|
}
|
|
|
|
|
|
//-
|
|
|
|
|
|
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 getCheckedCount() {
|
|
|
|
|
|
return (int) Data.values().stream().filter(d -> d.isVisible() && d.isSelected()).count();
|
|
|
|
|
|
}
|
|
|
|
|
|
public Vector<D> getCheckedItems() {
|
|
|
|
|
|
return Data.values().stream().filter(d -> d.isVisible() && d.isSelected()).collect(Collectors.toCollection(Vector::new));
|
|
|
|
|
|
}
|
|
|
|
|
|
public Vector<K> getCheckedKeys() {
|
|
|
|
|
|
return Data.values().stream().filter(DBObject::isSelected).map(d -> (K) d.getPK()).collect(Collectors.toCollection(Vector::new));
|
|
|
|
|
|
}
|
|
|
|
|
|
//--
|
|
|
|
|
|
public void SaveLastSelections() {
|
|
|
|
|
|
if (hasUI()) {
|
|
|
|
|
|
Object lastPk = null;
|
|
|
|
|
|
if ((CurrentName() != Current.Undefined) && (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() != Current.Undefined) && (lastPk != null)) {
|
|
|
|
|
|
ui_.Select(lastPk);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//---
|
|
|
|
|
|
// применить значение фильтра к фильру объекта напирмер Message.filterValue = text;
|
|
|
|
|
|
public void changeColumnFilterValue(int columnIndex, String text) {
|
|
|
|
|
|
}
|
|
|
|
|
|
public Object getColumnFilterValue(int columnIndex) {
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
2023-12-26 19:12:30 +03:00
|
|
|
|
//--
|
|
|
|
|
|
public void ShowUI() {
|
|
|
|
|
|
for (DataSetFilter<D> filter : filters)
|
|
|
|
|
|
filter.Drop();
|
|
|
|
|
|
//--
|
|
|
|
|
|
if (ui_ != null) {
|
|
|
|
|
|
ui_.Show();
|
|
|
|
|
|
if (f_ui != null)
|
|
|
|
|
|
f_ui.ShowMatchesCount(getRowCountUI());
|
|
|
|
|
|
}
|
|
|
|
|
|
//--
|
|
|
|
|
|
for (DataSetFilter<D> filter : filters)
|
|
|
|
|
|
filter.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
public void ShowUI(Object key) {
|
|
|
|
|
|
for (DataSetFilter<D> filter : filters)
|
|
|
|
|
|
filter.Drop();
|
|
|
|
|
|
//--
|
|
|
|
|
|
if (ui_ != null) {
|
|
|
|
|
|
ui_.Show(key);
|
|
|
|
|
|
if (f_ui != null)
|
|
|
|
|
|
f_ui.ShowMatchesCount(getRowCountUI());
|
|
|
|
|
|
}
|
|
|
|
|
|
//--
|
|
|
|
|
|
for (DataSetFilter<D> filter : filters)
|
|
|
|
|
|
filter.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
public boolean applyFilters(D object) {
|
|
|
|
|
|
for (DataSetFilter<D> filter : filters) {
|
|
|
|
|
|
if (!filter.Validate(object))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2024-03-22 23:08:51 +03:00
|
|
|
|
//--
|
|
|
|
|
|
public Vector<D> getCheckedOrCurrent() {
|
|
|
|
|
|
Vector<D> res = new Vector<>();
|
|
|
|
|
|
if (getCheckedCount() > 0)
|
|
|
|
|
|
res = getCheckedItems();
|
|
|
|
|
|
else {
|
|
|
|
|
|
if (!CurrentName().equals(Current.Undefined)) {
|
|
|
|
|
|
if (getCurrent() != null) {
|
|
|
|
|
|
res.add(getCurrent());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|