Skip to content

Commit

Permalink
unifying table model for MaptableModel to Generic
Browse files Browse the repository at this point in the history
  • Loading branch information
nicol authored and nicol committed Jan 20, 2025
1 parent 14d6768 commit d15de1e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ public class StockItemsSynchronizationPanel extends MTGUIComponent {

public StockItemsSynchronizationPanel() {
setLayout(new BorderLayout(0, 0));
model = new MapTableModel<>();
model = new MapTableModel<>() {

private static final long serialVersionUID = 1L;

@Override
public void setValueAt(Object aValue, int row, int column) {
if(column==1)
{
items.get(row).setValue(String.valueOf(aValue));
st.setUpdated(true);
}
}
};
table = UITools.createNewTable(model,false);


Expand Down Expand Up @@ -61,7 +73,7 @@ public StockItemsSynchronizationPanel() {
});

model.setWritable(true);
model.setColumnNames("Plugin", "id");
model.setColumns("Plugin", "id");


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ public ConfigurationPanel() {
private static final long serialVersionUID = 1L;
@Override
public void setValueAt(Object aValue, int row, int column) {
MTGLogger.changeLevel(MTGLogger.getLogger(keys.get(row).getKey()),aValue.toString());
keys.get(row).setValue(Level.toLevel(aValue.toString()));
MTGLogger.changeLevel(MTGLogger.getLogger(items.get(row).getKey()),aValue.toString());
items.get(row).setValue(Level.toLevel(aValue.toString()));
}
@Override
public Class<?> getColumnClass(int columnIndex) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/magic/gui/dashlet/IndexationDashlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public void initGUI() {

panneauHaut.add(cboField);
indexModel = new MapTableModel<>();
indexModel.setColumnNameAt(0, "Term");
indexModel.setColumnNameAt(1, "Occurences");
indexModel.setColumns("Term", "Occurences");
var table = UITools.createNewTable(indexModel,false);

getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
Expand Down
107 changes: 18 additions & 89 deletions src/main/java/org/magic/gui/models/MapTableModel.java
Original file line number Diff line number Diff line change
@@ -1,142 +1,69 @@
package org.magic.gui.models;

import static org.magic.services.tools.MTG.capitalize;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.table.DefaultTableModel;
public class MapTableModel<K,V> extends DefaultTableModel {
import org.magic.gui.abstracts.GenericTableModel;
public class MapTableModel<K,V> extends GenericTableModel<Entry<K, V>> {

private static final long serialVersionUID = 1L;

protected transient List<Entry<K, V>> keys;

protected String[] columnsName =new String[] {"ID","VALUE"};

private boolean writable;

public void setWritable(boolean writable) {
this.writable = writable;
}

public boolean isWritable() {
return writable;
}


public MapTableModel() {
this.keys = new ArrayList<>();
setColumns("ID","VALUE");
writable=false;
}

public List<Entry<K, V>> getValues()
{
return keys;
}

public MapTableModel(Map<K,V> map2)
{
init(map2);
}

public void setColumnNameAt(int index,String name)
{
columnsName[index]=name;
}

public void setColumnNames(String c1,String c2)
{
columnsName[0]=c1;
columnsName[1]=c2;
fireTableStructureChanged();
}


public void init(Set<Entry<K, V>> entrySet) {
this.keys = new ArrayList<>(entrySet);
this.items = new ArrayList<>(entrySet);
fireTableDataChanged();
}


public void init(Map<K, V> map)
{
this.keys = new ArrayList<>(map.entrySet());
this.items = new ArrayList<>(map.entrySet());
fireTableDataChanged();
}


public void addRow(K key, V value)
{

keys.add(new AbstractMap.SimpleEntry<>(key,value));
}

public void clear() {
keys.clear();
}



@Override
public int getRowCount() {
if(keys==null)
return 0;

return keys.size();
items.add(new AbstractMap.SimpleEntry<>(key,value));
}

@Override
public Class<?> getColumnClass(int columnIndex) {
if(keys==null || keys.isEmpty())
if(items==null || items.isEmpty())
return super.getColumnClass(columnIndex);

if(columnIndex==0)
{
return keys.get(0).getKey().getClass();
return items.get(0).getKey().getClass();
}
else
{
if(keys.get(0).getValue()!=null)
return keys.get(0).getValue().getClass();
if(items.get(0).getValue()!=null)
return items.get(0).getValue().getClass();
else
return super.getColumnClass(columnIndex);
}
}


@Override
public int getColumnCount() {
return 2;
}

@Override
public String getColumnName(int column) {
return capitalize(columnsName[column]);
}


@Override
public boolean isCellEditable(int row, int column) {
return writable;
}

@Override
public Object getValueAt(int row, int column) {
if(column==0)
return keys.get(row).getKey();
return items.get(row).getKey();
else
return keys.get(row).getValue();
return items.get(row).getValue();
}

public boolean removeRow(K ed) {

Entry<K,V> removed =null;
for(Entry<K, V> r : keys)
for(Entry<K, V> r : items)
{
if(r.getKey()==ed)
{
Expand All @@ -147,7 +74,7 @@ public boolean removeRow(K ed) {

if(removed!=null)
{
Boolean b = keys.remove(removed);
Boolean b = items.remove(removed);
fireTableDataChanged();
return b;
}
Expand All @@ -156,14 +83,16 @@ public boolean removeRow(K ed) {
}



@Override
public void setValueAt(Object aValue, int row, int column) {
if(column==1)
keys.get(row).setValue((V) aValue);
items.get(row).setValue((V) aValue);
}


public void updateRow(K k, V v) {
getValues().forEach(entry->{
getItems().forEach(entry->{
if(entry.getKey()==k)
{
entry.setValue(v);
Expand Down

0 comments on commit d15de1e

Please sign in to comment.