codice:
package prova;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;
import com.parctechnologies.eclipse.CompoundTerm;
import com.parctechnologies.eclipse.EclipseEngine;
import com.parctechnologies.eclipse.EclipseEngineOptions;
import com.parctechnologies.eclipse.EclipseException;
import com.parctechnologies.eclipse.EmbeddedEclipse;
import esempiTable.Person;
public class TabOrari {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new TabOrariFrame().setVisible(true);
} catch (EclipseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
class TabOrariFrame extends JFrame {
private TabOrariModel tableModel;
private JTable table;
private JScrollPane scrollPane;
public TabOrariFrame() throws EclipseException, IOException {
super("Tabella Orari");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(350, 250);
setLocationRelativeTo(null);
// Create some default Eclipse options
EclipseEngineOptions eclipseEngineOptions = new EclipseEngineOptions();
// Object representing the Eclipse process
EclipseEngine eclipse;
// Path of the Eclipse program
File eclipseProgram;
// Connect the Eclipse's standard streams to the JVM's
eclipseEngineOptions.setUseQueues(false);
// Initialise Eclipse
eclipse = EmbeddedEclipse.getInstance(eclipseEngineOptions);
String sep = System.getProperty("file.separator");
// Set up the path of the example Eclipse program to be used.
eclipseProgram = new File(System.getProperty("eclipse.directory") +
sep + "doc" + sep + "examples" + sep +
"JavaInterface" + sep +
"test.pl");
//compile the eclipse program
eclipse.compile(eclipseProgram);
//Query che richiedo ad ECLIPSE
CompoundTerm result = eclipse.rpc("findall(X,percorso(X,H,Min,Max),L)");
//Il risultato della mia query si trova nel terzo argomento (perchè è X l'incognita)
List listatreni = (List)result.arg(3);
//Object firstGoalFirstArg = firstGoal.arg(1);
//System.out.println(listatreni);
listatreni.add(0,"");
System.out.println(listatreni);
// Destroy the Eclipse
((EmbeddedEclipse) eclipse).destroy();
tableModel = new TabOrariModel(listatreni);
//table = new JTable(tableModel);
table = new TabOrariTable(listatreni);
scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
}
class TabOrariTable extends JTable {
private static List listatreni;
public TabOrariTable(List listatreni) {
super(new TabOrariModel(listatreni));
this.listatreni = listatreni;
}
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component rendererComponent = super.prepareRenderer(renderer, row, column);
if (!isCellSelected(row, column)) {
if (row == 0 || column == 0) {
rendererComponent.setBackground(Color.LIGHT_GRAY);
} else {
rendererComponent.setBackground(Color.WHITE);
}
}
//METODO DA CONTROLLARE-NON BENE (colorare di giallo le colonne dispari)
if (!isCellSelected(row, column)) {
for (int i=1; i<listatreni.size(); i++){
int j = Integer.parseInt(listatreni.get(i).toString());
//System.out.print(listatreni.size());
if (Pari(j)){
if (column == i && row !=0){
rendererComponent.setBackground(Color.YELLOW);
}
}
}
}
return rendererComponent;
}
private boolean Pari(int numero) {
boolean test = true;
if (numero % 2 == 0){
test = true;
System.out.print("pari");
} else {
System.out.print("dispari");
}
return test;
}
}
class TabOrariModel extends AbstractTableModel {
private static String[] rowNames = {"","Foggia", "Foggia-Incoronata", "Incoronata", "Incoronata-Ortanova","Ortanova","Ortanova-Cerignola","Cerignola","Cerignola-Candida","Candida","Candida-Trinitapoli","Trinitapoli","Trinitapoli-Ofantino","Ofantino","Ofantino-Barletta","Barletta","Barletta-Trani","Trani","Trani-Bisceglie","Bisceglie","Bisceglie-Molfetta","Molfetta","Molfetta-Giovinazzo","Giovinazzo","Giovinazzo-Bari S.Spirito","Bari S.Spirito","Bari S.Spirito-Bari Z.I.","Bari Z.I.","Bari Z.I.-Bari Parco Nord","Bari Parco Nord","Bari Parco Nord-Bari C.le","Bari C.le"};
//The internal structure of the model is an ArrayList<Person>.
private List listatreni;
public TabOrariModel(List listatreni) {
this.listatreni = listatreni;
}
public int getRowCount() {
return rowNames.length;
}
public int getColumnCount() {
return listatreni.size();
}
public String getRowName(int row) {
return rowNames[row];
}
public String getColumnName(int column) {
//System.out.print(listatreni.get(column).toString());
//return listatreni.get(column).toString();
return null;
}
public boolean isCellEditable(int row, int column) {
return false; // All cells are NOT editable!
}
public Object getValueAt(int row, int column) {
//int v;
if (row == 0) {
if (column == 0) {
return ""; // nella cella (0,0) c'è uno spazio vuoto.
} else {
return listatreni.get(column).toString(); // nelle celle (0,N) c'è il valore della colonna (successivamente ci sarà il num treno).
}
} else {
if (column == 0) {
return getRowName(row); // Nelle celle (N,0) c'è il nome della stazione.
} else {
return null; // Nelle celle (N,N) ci sarà il valore dei minuti..
}
}
//return new Integer(v);
}
}
ora provo come hai detto...