diff --git a/src/danielcortes/xyz/views/components/KeySelectionRenderer.java b/src/danielcortes/xyz/views/components/KeySelectionRenderer.java new file mode 100644 index 0000000..6b8afc2 --- /dev/null +++ b/src/danielcortes/xyz/views/components/KeySelectionRenderer.java @@ -0,0 +1,99 @@ +package danielcortes.xyz.views.components; + +import javax.swing.*; +import java.awt.*; + +public abstract class KeySelectionRenderer extends DefaultListCellRenderer implements JComboBox.KeySelectionManager { + private long timeFactor; + private long lastTime; + private long time; + private String prefix = ""; + + public KeySelectionRenderer(JComboBox comboBox) { + comboBox.setRenderer(this); + comboBox.setKeySelectionManager(this); + + Long l = (Long) UIManager.get("ComboBox.timeFactor"); + timeFactor = l == null ? 1000L : l.longValue(); + } + + public abstract String getDisplayValue(Object item); + + + @Override + public Component getListCellRendererComponent( + JList list, Object item, int index, boolean isSelected, boolean hasFocus) { + super.getListCellRendererComponent(list, item, index, isSelected, hasFocus); + + if (item != null) { + setText(getDisplayValue(item)); + } + + return this; + } + + + @Override + public int selectionForKey(char aKey, ComboBoxModel model) { + time = System.currentTimeMillis(); + + // Get the index of the currently selected item + + int size = model.getSize(); + int startIndex = -1; + Object selectedItem = model.getSelectedItem(); + + if (selectedItem != null) { + for (int i = 0; i < size; i++) { + if (selectedItem == model.getElementAt(i)) { + startIndex = i; + break; + } + } + } + + + if (time - lastTime < timeFactor) { + if ((prefix.length() == 1) && (aKey == prefix.charAt(0))) { + startIndex++; + } else { + prefix += aKey; + } + } else { + startIndex++; + prefix = "" + aKey; + } + + lastTime = time; + + + if (startIndex < 0 || startIndex >= size) { + startIndex = 0; + } + + int index = getNextMatch(prefix, startIndex, size, model); + + if (index < 0) { + // wrap + index = getNextMatch(prefix, 0, startIndex, model); + } + + return index; + } + + private int getNextMatch(String prefix, int start, int end, ComboBoxModel model) { + for (int i = start; i < end; i++) { + Object item = model.getElementAt(i); + + if (item != null) { + String displayValue = getDisplayValue(item).toLowerCase(); + + if (displayValue.startsWith(prefix)) { + return i; + } + } + } + + return -1; + } +} diff --git a/src/danielcortes/xyz/views/components/table_model/EgresosTableModel.java b/src/danielcortes/xyz/views/components/table_model/EgresosTableModel.java new file mode 100644 index 0000000..94b2c50 --- /dev/null +++ b/src/danielcortes/xyz/views/components/table_model/EgresosTableModel.java @@ -0,0 +1,105 @@ +/* + * MIT License + * + * Copyright (c) 2018-2019 Daniel Cortes + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package danielcortes.xyz.views.components.table_model; + +import danielcortes.xyz.models.egreso.Egreso; + +import javax.swing.table.AbstractTableModel; +import java.text.NumberFormat; +import java.util.ArrayList; + +public class EgresosTableModel extends AbstractTableModel { + private String[] columns; + private ArrayList rows; + private NumberFormat nf; + + + public EgresosTableModel() { + super(); + this.columns = new String[]{"N°", "Descripcion", "Valor", "Tipo"}; + this.rows = new ArrayList<>(); + this.nf = NumberFormat.getIntegerInstance(); + + } + + @Override + public String getColumnName(int col) { + return columns[col]; + } + + @Override + public int getColumnCount() { + return columns.length; + } + + @Override + public int getRowCount() { + return rows.size(); + } + + public void addRow(Egreso egreso) { + rows.add(egreso); + this.fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1); + + } + + public void removeRow(int row) { + this.rows.remove(row); + this.fireTableRowsDeleted(row, row); + } + + public void removeRows() { + int rowCount = getRowCount(); + if (rowCount > 0) { + this.rows.clear(); + this.fireTableRowsDeleted(0, rowCount - 1); + } + } + + public void setEgreso(int editingId, Egreso egreso) { + this.rows.set(editingId, egreso); + this.fireTableRowsUpdated(0, getRowCount() - 1); + } + + @Override + public Object getValueAt(int row, int col) { + switch (col) { + case 0: + return rows.get(row).getNro(); + case 1: + return rows.get(row).getDescripcion(); + case 2: + return nf.format(rows.get(row).getValor()); + case 3: + return rows.get(row).getTipoEgreso(); + } + return null; + } + + public Egreso getEgreso(int row) { + return rows.get(row); + } + +} diff --git a/src/danielcortes/xyz/views/components/table_model/FondoTableModel.java b/src/danielcortes/xyz/views/components/table_model/FondoTableModel.java new file mode 100644 index 0000000..1b3d133 --- /dev/null +++ b/src/danielcortes/xyz/views/components/table_model/FondoTableModel.java @@ -0,0 +1,101 @@ +/* + * MIT License + * + * Copyright (c) 2018-2019 Daniel Cortes + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package danielcortes.xyz.views.components.table_model; + +import danielcortes.xyz.models.calculo_fondo.CalculoFondo; + +import javax.swing.table.AbstractTableModel; +import java.text.NumberFormat; +import java.util.ArrayList; + +public class FondoTableModel extends AbstractTableModel { + + private String[] columns; + private ArrayList rows; + private NumberFormat nf; + + + public FondoTableModel() { + super(); + this.columns = new String[]{"Valor", "Descripcion"}; + this.rows = new ArrayList<>(); + this.nf = NumberFormat.getIntegerInstance(); + + } + + @Override + public String getColumnName(int col) { + return columns[col]; + } + + @Override + public int getColumnCount() { + return columns.length; + } + + @Override + public int getRowCount() { + return rows.size(); + } + + public void addRow(CalculoFondo calculoFondo) { + rows.add(calculoFondo); + this.fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1); + + } + + public void removeRow(int row) { + this.rows.remove(row); + this.fireTableRowsDeleted(row, row); + } + + public void removeRows() { + int rowCount = getRowCount(); + if (rowCount > 0) { + this.rows.clear(); + this.fireTableRowsDeleted(0, rowCount - 1); + } + } + + public void setCalculoFondo(int editingId, CalculoFondo calculoFondo) { + this.rows.set(editingId, calculoFondo); + this.fireTableRowsUpdated(0, getRowCount() - 1); + } + + @Override + public Object getValueAt(int row, int col) { + switch (col) { + case 0: + return nf.format(rows.get(row).getValor()); + case 1: + return rows.get(row).getDescripcion(); + } + return null; + } + + public CalculoFondo getCalculoFondo(int row) { + return rows.get(row); + } +} diff --git a/src/danielcortes/xyz/views/components/table_model/IngresosTableModel.java b/src/danielcortes/xyz/views/components/table_model/IngresosTableModel.java new file mode 100644 index 0000000..e48f67a --- /dev/null +++ b/src/danielcortes/xyz/views/components/table_model/IngresosTableModel.java @@ -0,0 +1,106 @@ +/* + * MIT License + * + * Copyright (c) 2018-2019 Daniel Cortes + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package danielcortes.xyz.views.components.table_model; + +import danielcortes.xyz.models.ingreso.Ingreso; + +import javax.swing.table.AbstractTableModel; +import java.text.NumberFormat; +import java.util.ArrayList; + +public class IngresosTableModel extends AbstractTableModel { + private String[] columns; + private ArrayList rows; + private NumberFormat nf; + + public IngresosTableModel() { + super(); + this.columns = new String[]{"Valor", "N° Z Inicial", "N° Z Final", "N° Inicial", "N° Final", "Tipo"}; + this.rows = new ArrayList<>(); + this.nf = NumberFormat.getIntegerInstance(); + } + + @Override + public String getColumnName(int col) { + return this.columns[col]; + } + + @Override + public int getColumnCount() { + return this.columns.length; + } + + @Override + public int getRowCount() { + return this.rows.size(); + } + + public void addRow(Ingreso ingreso) { + this.rows.add(ingreso); + this.fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1); + } + + public void removeRow(int row) { + this.rows.remove(row); + this.fireTableRowsDeleted(row, row); + } + + public void removeRows() { + int rowCount = getRowCount(); + if (rowCount > 0) { + this.rows.clear(); + this.fireTableRowsDeleted(0, rowCount - 1); + } + } + + @Override + public Object getValueAt(int row, int col) { + switch (col) { + case 0: + return nf.format(this.rows.get(row).getValor()); + case 1: + return this.rows.get(row).getNroZInicial(); + case 2: + return this.rows.get(row).getNroZFinal(); + case 3: + return this.rows.get(row).getNroInicial(); + case 4: + return this.rows.get(row).getNroFinal(); + case 5: + return this.rows.get(row).getTipoIngreso().getNombre(); + } + return null; + } + + public Ingreso getIngreso(int row) { + return this.rows.get(row); + } + + public void setIngreso(int editingId, Ingreso ingreso) { + this.rows.set(editingId, ingreso); + this.fireTableRowsUpdated(getRowCount() - 2, getRowCount() - 1); + } + +}