tak napriklad:
/*
* Trieda, ktora na zakladne vstupnych info : celkovy pocet riadkov a pocet riadkov
* na stranu, vypocita, kolko to bude mat stran a vypocita start a end index pre kazdu stranu
*/
package ishs;
/**
*
* @author peter
*/
public class TablePage {
private int actual_page = 1;
private int rows_per_page = 0;
private boolean last_zero = false;
private int last = 0;
private int rows_count = 0;
private int pages_count = 0;
/**
* {@inheritDoc}
* <p>
* inkremetuje aktualnu stranku
*/
public void nextPage() {
if (actual_page == getPagesCount()) {
actual_page = 0;
actual_page++;
} else {
actual_page++;
}
}
/**
* {@inheritDoc}
* <p>
* dekremetuje aktualnu stranku
*/
public void prewiousPage() {
if (actual_page == 1) {
actual_page = getPagesCount() + 1;
actual_page--;
} else {
if(actual_page > 0){
actual_page--;
}
}
}
/**
* {@inheritDoc}
* <p>
* nastavi stranu, ktora ma byt zobrazena
*/
public void setActualPage(int actual_page) {
if (actual_page < getPagesCount() + 1) {
this.actual_page = actual_page;
}
}
/**
* {@inheritDoc}
* <p>
* vrati cislo aktualne zobrazenej strany
*/
public int getActualPage() {
return actual_page;
}
/**
* {@inheritDoc}
* <p>
* ulozi pocet zaznamov
*/
public void setRowsCount(int rows_count) {
this.rows_count = rows_count;
}
/**
* {@inheritDoc}
* <p>
* vrati pocet zaznamov
*/
public int getRowsCount() {
return rows_count;
}
/**
* {@inheritDoc}
* <p>
* ulozi sa zvysok po vypocte poctu stran podla poctu zaznamov.
*/
private void setLast(int last) {
this.last = last;
}
/**
* {@inheritDoc}
* <p>
* vrati zvysok
*/
private int getLast() {
return last;
}
/**
* {@inheritDoc}
* <p>
* vrati, ci bol vysledok vypoctu poctu stran so zvyskom alebo bez
*/
private boolean isLastZero() {
return last_zero;
}
/**
* {@inheritDoc}
* <p>
* ak bol vypocet bez zvysku, vrati true
*/
private void setLastZero(boolean last_zero) {
this.last_zero = last_zero;
}
/**
* {@inheritDoc}
* <p>
* nastavi pozadovany pocet zaznamov na stranu
*/
public void setRowsPerPage(int rows_per_page) {
this.rows_per_page = rows_per_page;
}
/**
* {@inheritDoc}
* <p>
* vrati pozadovany pocet zaznamov na stranu
*/
public int getRowsPerPage() {
return rows_per_page;
}
/**
* {@inheritDoc}
* <p>
* vrati pocet stran
*/
public int getPagesCount() {
return pages_count;
}
/**
* {@inheritDoc}
* <p>
* podla poctu zaznamov a zaznamov na stranu vypocita pocet stran
*/
public void loadPagesCount() {
int ret_val = 0;
if (getRowsPerPage() > 0) {
int rc = getRowsCount();
if (rc % rows_per_page == 0) {
ret_val = rc / rows_per_page;
setLastZero(true);
} else {
ret_val = rc / rows_per_page + 1;
setLastZero(false);
setLast(rc % rows_per_page);
}
} else {
ret_val = 1;
}
if (ret_val == 0) {
pages_count = 1;
} else {
pages_count = ret_val;
}
}
/**
* {@inheritDoc}
* <p>
* vrati pociatocny index na stranu
*/
public int getStartOfPage() {
int ret_val = 0;
if (getRowsPerPage() > 0) {
int page = getActualPage() - 1;
if (page == 0) {
ret_val = 0;
} else {
ret_val = (page * getRowsPerPage());
}
} else {
ret_val = 0;
}
return ret_val;
}
/**
* {@inheritDoc}
* <p>
* vrati konecny index na stranu
*/
public int getEndOfPage() {
int ret_val = 0;
if (getRowsPerPage() > 0) {
if (isLastZero()) {
ret_val = (getStartOfPage() + getRowsPerPage()) - 1;
} else {
if (getActualPage() == getPagesCount()) {
ret_val = (getStartOfPage() + getLast()) - 1;
} else if(getActualPage() < getPagesCount()){
ret_val = (getStartOfPage() + getRowsPerPage()) - 1;
}
}
} else {
ret_val = getRowsCount() - 1;
}
return ret_val;
}
}
vdaka tomuto objektu nemam v tabulke vsetky zaznamy, ale rozdeli mi to na strany, ktore potom zobrazujem a listujem ako v knihe
Tento ubjekt uklada a nacita/zapisuje nastavenia zo/do suboru. Napisem si tam len konstanty (nazvy nastaveni) a potom s tym pracujem
/*
* Objekt pre ukladanie a nacitanie nastaveni
*/
package ishs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
/**
*
* @author peter
*/
public class AppOptions {
public final String ROWS_PAGES_CUST = "rowsPerPageCustomers";
public final String ROWS_PAGES_ARTI = "rowsPerPageArticles";
public final String ROWS_PAGES_ARTI_MC = "rowsPerPageMatchArticles";
public final String ROWS_PAGES_STOR = "rowsPerPageStore";
public final String ROWS_PAGES_WORK = "rowsPerPageWork";
public final String MACHINE = "machine";
public final String DATABASE = "database";
private String file_name_str = "";
public Vector<String> data_set = new Vector();
public AppOptions() {
/**
* {@inheritDoc}
* <p>
* nacita do vectora neparsnute riadky
*/
try {
file_name_str = DDirectory.getDataDirectory() + "config" + File.separator + "ishs.cfg";
BufferedReader in = new BufferedReader(new FileReader(file_name_str));
String str;
while ((str = in.readLine()) != null) {
try {
str = deleteSpace(str);
data_set.add(str);
} catch (Exception e) {
e.printStackTrace();
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* {@inheritDoc}
* <p>
* podla nazvu nastavenia ulozi hodnotu do vectora
*/
public void saveOption(String option_name, String value) {
for (int x = 0; x < data_set.size(); x++) {
String str = extractOptionName(data_set.get(x));
if (option_name.equals(str)) {
data_set.setElementAt(option_name + " = " + value, x);
}
}
}
/**
* {@inheritDoc}
* <p>
* podla nazvu nastavenia ziska danu hodnotu z nacitaneho suboru
*/
public String getOption(String option_name) {
String ret_val = "";
for (int x = 0; x < data_set.size(); x++) {
String str = extractOptionName(data_set.get(x));
if (option_name.equals(str)) {
ret_val = extractOptionValue(data_set.get(x));
}
}
return ret_val;
}
/**
* {@inheritDoc}
* <p>
* zapise vector s novymi nastaveniami na disk
*/
public void writeFile() {
try {
FileWriter out_file = new FileWriter(file_name_str);
PrintWriter out = new PrintWriter(out_file);
for (int x = 0; x < data_set.size(); x++) {
try {
out.println(deleteSpace(data_set.get(x)));
} catch (Exception e) {
e.printStackTrace();
}
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* {@inheritDoc}
* <p>
* z riadku parsne nazov nastavenia
*/
private String extractOptionName(String str) {
return deleteSpace(str.substring(0, str.lastIndexOf(" ") - 2));
}
/**
* {@inheritDoc}
* <p>
* z riadku parsne hodnotu nastavenia
*/
public String extractOptionValue(String str) {
return deleteSpace(str.substring(str.lastIndexOf(" ") + 1, str.length()));
}
/**
* {@inheritDoc}
* <p>
* odstrani medzery
*/
private String deleteSpace(String str) {
String ret_val = "";
ret_val = str.replace(" ", "").replace("=", " = ");
return ret_val;
}
}
Zistenie OS a podla toho prisposobi nazov korenovej zlozky nastaveni
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ishs;
import java.io.File;
/**
*
* @author eXistPierre
*/
public class DDirectory {
private static String name_os = System.getProperty("os.name");
private static String data_directory = "hermestore" + File.separator;
/**
* {@inheritDoc}
* <p>
* vrati nazov priecinka s potrebnymi subormi. Zisti, pod akou platformou je aplikacia pustena
* ak sa jedna o linux, tak ho da ako skryty a bude v home zlozke
*/
public static String getDataDirectory() {
name_os = name_os.substring(0, 1).toLowerCase().trim();
if ("w".equals(name_os.toLowerCase())) {
data_directory = "hermestore" + File.separator;
} else {
data_directory = ".hermestore" + File.separator;
}
return DDirectory.data_directory;
}
/**
* {@inheritDoc}
* <p>
* vrati, ci sa jedna o windows alebo ine platformy
*/
public static boolean isWindows() {
if ("w".equals(name_os.toLowerCase())) {
return true;
} else {
return false;
}
}
}