Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

java / lab7 (1) / lab6 / client / src / gui / RemoteServiceClientImplementation

.java
Скачиваний:
84
Добавлен:
17.04.2018
Размер:
4.4 Кб
Скачать
package gui;

import common.RemoteService;
import common.People;
import table.PeopleTableModel;

import javax.swing.*;
import java.awt.*;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.List;

public class RemoteServiceClientImplementation extends JPanel {

private JButton showAllBtn;
private JButton deletePeopleBtn;
private JTable table;
private JScrollPane scrollPane;

private PeopleTableModel tableModel = new PeopleTableModel(new ArrayList<>(), new String[]{"ID", "Name", "additional"});
private RemoteService service;

RemoteServiceClientImplementation() {
Registry registry = null;
try {
registry = LocateRegistry.getRegistry("localhost", 4396);
} catch (RemoteException e) {
System.err.println(e.getMessage());
}
try {
assert registry != null;
System.out.println(registry.lookup("local/PeopleService").getClass());
service = (RemoteService) registry.lookup("local/PeopleService");
} catch (RemoteException | NotBoundException e) {
System.err.println(e.getMessage());
JOptionPane.showMessageDialog(null, "Connection refused!");
System.exit(0);
}

showAllBtn = new JButton("Show All") {
{
addActionListener(e -> {
List<People> people = new ArrayList<>();
try {
people = service.fetchAllPeople();
} catch (RemoteException e1) {
JOptionPane.showMessageDialog(this, e1.getMessage());
}
tableModel.clearMan();
if(people == null)
return;
for (People man : people) {
tableModel.addMan(man);
}
});
}
};

deletePeopleBtn = new JButton("Delete") {
{
addActionListener(e -> {
try {
if (table.getSelectedRow() > -1) {
People man = tableModel.getPeople().get(table.getSelectedRow());
service.deleteMan(man.getId());
tableModel.removeMan(man);
tableModel.repaint();
}
} catch (RemoteException e1) {
JOptionPane.showMessageDialog(this, e1.getMessage());
}
});
}
};
}

void createAndShowGUI() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | UnsupportedLookAndFeelException ex) {
System.err.println(ex.getMessage());
}
new JFrame("App") {{
setLocationByPlatform(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Dimension dim = new Dimension(900, 600);
setPreferredSize(dim);
setMinimumSize(dim);
setResizable(false);
setLayout(new BorderLayout());

JPanel header = new JPanel() {
{
add(showAllBtn);
}
};

add(BorderLayout.NORTH, header);

JPanel footer = new JPanel() {
{
/* add(addPeopleBtn);
add(editPeopleBtn);*/
add(deletePeopleBtn);
}
};
add(BorderLayout.SOUTH, footer);
table = new JTable();
table.setModel(tableModel);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getTableHeader().setReorderingAllowed(false);
scrollPane = new JScrollPane(table);
add(scrollPane);
pack();
setVisible(true);
}};
}
}
Соседние файлы в папке gui