Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Portifix/src/de/mpg/molgen/buczek/portifix/Gui.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
531 lines (454 sloc)
20.6 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package de.mpg.molgen.buczek.portifix; | |
import java.awt.Component; | |
import java.awt.Container; | |
import java.awt.Toolkit; | |
import java.awt.datatransfer.Clipboard; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.FocusEvent; | |
import java.awt.event.FocusListener; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseListener; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import java.text.ParseException; | |
import java.util.Date; | |
import java.util.Scanner; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.swing.AbstractAction; | |
import javax.swing.JPopupMenu; | |
import javax.swing.SwingUtilities; | |
import javax.swing.TransferHandler; | |
import javax.swing.UIManager; | |
import javax.swing.UnsupportedLookAndFeelException; | |
import javax.swing.event.ListSelectionEvent; | |
import javax.swing.event.ListSelectionListener; | |
import javax.swing.table.DefaultTableCellRenderer; | |
/** | |
* | |
* @author buczek | |
*/ | |
public class Gui extends javax.swing.JFrame { | |
Connection connection; | |
TableModel tableModel; | |
private static final boolean useTestDatabase=false; | |
private static final String connectString= | |
useTestDatabase | |
? "jdbc:firebirdsql://portier-db//project/portier/firebird/data/x.fdb?encoding=UTF8" | |
: "jdbc:firebirdsql://portier-db/portiervision.fdb?encoding=UTF8"; | |
private void filltable() { | |
try { | |
String password=new Scanner(getClass().getResourceAsStream("password.dat"),"UTF-8").useDelimiter("\\A").next(); | |
if (connection == null) { | |
connection = DriverManager.getConnection(connectString, "sysdba",password); | |
tableModel.setConnection(connection); | |
} | |
tableModel.populate(); | |
} catch (SQLException ex) { | |
Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
private boolean isUnique(String... s) { | |
if (s.length == 0) { | |
return true; | |
} | |
for (int i = 1; i < s.length; i++) { | |
if (!s[0].equals(s[i])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private String[] columnValuesOfSelection(int col) { | |
int selected[] = jTable1.getSelectedRows(); | |
String[] ret = new String[selected.length]; | |
for (int i = 0; i < selected.length; i++) { | |
Object o = tableModel.getValueAt(jTable1.convertRowIndexToModel(selected[i]), col); | |
if (o == null) { | |
ret[i] = ""; | |
} else { | |
ret[i] = o.toString(); | |
} | |
} | |
return ret; | |
} | |
private String singleColumnValueOfArray(String s[]) { | |
if (s.length == 0) { | |
return ""; | |
} | |
if (isUnique(s)) { | |
return s[0]; | |
} else { | |
return "..."; | |
} | |
} | |
private String singleColumnValueOfSelection(int col) { | |
return singleColumnValueOfArray(columnValuesOfSelection(col)); | |
} | |
private static void setTreeEnabled(Component component, boolean enabled) { | |
component.setEnabled(enabled); | |
if (component instanceof Container) { | |
Container container = (Container) component; | |
for (Component child : container.getComponents()) { | |
setTreeEnabled(child, enabled); | |
} | |
} | |
} | |
/** | |
* no String.join() in Java 1.7 | |
* | |
* @param delim | |
* @param s1 | |
* @param s2 | |
* @return | |
*/ | |
private String join(String delim, String s1, String s2) { | |
if (s1 != null && s1.length() > 0) { | |
if (s2 != null && s2.length() > 0) { | |
return s1 + delim + s2; | |
} else { | |
return s1; | |
} | |
} else { | |
return s2; | |
} | |
} | |
private void setEditEnabled(boolean enabled) { | |
textField_datum.setEnabled(enabled); | |
} | |
private void updateForm() { | |
int selected[] = jTable1.getSelectedRows(); | |
for (int i = 0; i < selected.length; i++) { | |
selected[i] = jTable1.convertRowIndexToModel(selected[i]); | |
} | |
if (selected.length == 0) { | |
setEditEnabled(false); | |
} else { | |
String[] name = new String[selected.length]; | |
String[] exemplar = new String[selected.length]; | |
for (int i = 0; i < name.length; i++) { | |
name[i] = join(" ", (String) tableModel.getValueAt(selected[i], TableModel.COLUMN_VORNAME), (String) tableModel.getValueAt(selected[i], TableModel.COLUMN_NACHNAME)); | |
exemplar[i] = join(" # ", (String) tableModel.getValueAt(selected[i], TableModel.COLUMN_SCHLUESSELNUMMER), (String) tableModel.getValueAt(selected[i], TableModel.COLUMN_EXEMPLARNUMMER)); | |
} | |
label_name.setText(singleColumnValueOfArray(name)); | |
label_abteilung.setText(singleColumnValueOfSelection(TableModel.COLUMN_ABTEILUNG)); | |
label_schluessel.setText(singleColumnValueOfArray(exemplar)); | |
textField_datum.setText(singleColumnValueOfSelection(TableModel.COLUMN_RUECKGABEDATUM)); | |
setEditEnabled(isUnique(columnValuesOfSelection(TableModel.COLUMN_PERSONAL_ID))); | |
} | |
} | |
private void startEdit() { | |
jTable1.setEnabled(false); | |
// setTreeEnabled(jTable1, false); | |
button_refresh.setEnabled(false); | |
button_ok.setEnabled(true); | |
button_cancel.setEnabled(true); | |
} | |
private void stopEdit() { | |
jTable1.setEnabled(true); | |
// setTreeEnabled(jTable1, true); | |
button_refresh.setEnabled(true); | |
button_ok.setEnabled(false); | |
button_cancel.setEnabled(false); | |
jTable1.requestFocus(); | |
updateForm(); | |
} | |
private void saveThis() throws ParseException { | |
int selected[] = jTable1.getSelectedRows(); | |
String s = textField_datum.getText(); | |
Date d = MyDateFormat.parse(s); | |
for (int i = 0; i < selected.length; i++) { | |
tableModel.setNewDateThis(jTable1.convertRowIndexToModel(selected[i]), d); | |
} | |
} | |
public Gui() { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
filltable(); | |
} | |
}); | |
tableModel = new TableModel(); | |
initComponents(); | |
label_name.setText(""); | |
label_abteilung.setText(""); | |
label_schluessel.setText(""); | |
textField_datum.setText(""); | |
textField_datum.setEnabled(false); | |
if (!useTestDatabase) { | |
testDatabaseLabel.setVisible(false); | |
} | |
jTable1.addMouseListener(popupListener); | |
jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() { | |
@Override | |
public void valueChanged(ListSelectionEvent e) { | |
updateForm(); | |
} | |
}); | |
/* default JTable.DateRenderer uses a Date Format we dont like ( "Feb 20, 1962") . So we set a | |
DefaultTableCellRenderer for Date (and derived classes) which uses toString(). Our MyDate class has | |
a toString() which produces a format we like (eg "20.02.1962"). | |
*/ | |
jTable1.setDefaultRenderer(java.util.Date.class, new DefaultTableCellRenderer()); | |
textField_datum.addFocusListener(new FocusListener() { | |
@Override | |
public void focusGained(FocusEvent e) { | |
if (textField_datum.getText().equals("...")) { | |
textField_datum.setText(""); | |
} | |
startEdit(); | |
} | |
@Override | |
public void focusLost(FocusEvent e) { | |
} | |
}); | |
button_cancel.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
stopEdit(); | |
} | |
}); | |
button_ok.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
try { | |
saveThis(); | |
} catch (ParseException ex) { | |
textField_datum.requestFocus(); | |
return; | |
} | |
stopEdit(); | |
} | |
}); | |
button_refresh.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
filltable(); | |
} | |
}); | |
// Action copy=jTable1.getActionMap().get("copy"); | |
popupMenu.add(new AbstractAction("copy") { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); | |
jTable1.getTransferHandler().exportToClipboard(jTable1, clipboard, TransferHandler.COPY); | |
} | |
}); | |
updateForm(); | |
} | |
/** | |
* This method is called from within the constructor to initialize the form. | |
* WARNING: Do NOT modify this code. The content of this method is always | |
* regenerated by the Form Editor. | |
*/ | |
@SuppressWarnings("unchecked") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents | |
private void initComponents() { | |
jSplitPane1 = new javax.swing.JSplitPane(); | |
jPanel1 = new javax.swing.JPanel(); | |
jPanel2 = new javax.swing.JPanel(); | |
jLabel1 = new javax.swing.JLabel(); | |
jLabel3 = new javax.swing.JLabel(); | |
label_name = new javax.swing.JLabel(); | |
label_abteilung = new javax.swing.JLabel(); | |
button_cancel = new javax.swing.JButton(); | |
button_refresh = new javax.swing.JButton(); | |
button_ok = new javax.swing.JButton(); | |
jPanel3 = new javax.swing.JPanel(); | |
label_schluessel = new javax.swing.JLabel(); | |
jLabel5 = new javax.swing.JLabel(); | |
jLabel7 = new javax.swing.JLabel(); | |
textField_datum = new javax.swing.JTextField(); | |
testDatabaseLabel = new javax.swing.JLabel(); | |
jScrollPane1 = new javax.swing.JScrollPane(); | |
jTable1 = new javax.swing.JTable(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
setTitle("Portifix"); | |
jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); | |
jLabel1.setText("Name:"); | |
jLabel3.setText("Abteilung:"); | |
label_name.setText("Donald Buczek"); | |
label_name.setToolTipText(""); | |
label_abteilung.setText("Allgemein"); | |
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); | |
jPanel2.setLayout(jPanel2Layout); | |
jPanel2Layout.setHorizontalGroup( | |
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(jPanel2Layout.createSequentialGroup() | |
.addContainerGap() | |
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jLabel1) | |
.addComponent(jLabel3)) | |
.addGap(19, 19, 19) | |
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) | |
.addComponent(label_name, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE) | |
.addComponent(label_abteilung, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
.addContainerGap(22, Short.MAX_VALUE)) | |
); | |
jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel3}); | |
jPanel2Layout.setVerticalGroup( | |
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(jPanel2Layout.createSequentialGroup() | |
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(label_name) | |
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(jLabel3) | |
.addComponent(label_abteilung)) | |
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
); | |
button_cancel.setText("Cancel"); | |
button_cancel.setEnabled(false); | |
button_refresh.setText("Refresh"); | |
button_ok.setText("OK"); | |
button_ok.setEnabled(false); | |
label_schluessel.setText("Schlüssel"); | |
jLabel5.setText("Schlüssel:"); | |
jLabel7.setText("Rückgabe bis:"); | |
textField_datum.setColumns(10); | |
textField_datum.setText("01.01.1988"); | |
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); | |
jPanel3.setLayout(jPanel3Layout); | |
jPanel3Layout.setHorizontalGroup( | |
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(jPanel3Layout.createSequentialGroup() | |
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jLabel7) | |
.addComponent(jLabel5)) | |
.addGap(4, 4, 4) | |
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(textField_datum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(label_schluessel, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE))) | |
); | |
jPanel3Layout.setVerticalGroup( | |
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(jPanel3Layout.createSequentialGroup() | |
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(label_schluessel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jLabel5)) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(jLabel7) | |
.addComponent(textField_datum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addGap(34, 34, 34)) | |
); | |
testDatabaseLabel.setText("(test database)"); | |
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); | |
jPanel1.setLayout(jPanel1Layout); | |
jPanel1Layout.setHorizontalGroup( | |
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(jPanel1Layout.createSequentialGroup() | |
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(jPanel1Layout.createSequentialGroup() | |
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addGap(0, 0, Short.MAX_VALUE)) | |
.addGroup(jPanel1Layout.createSequentialGroup() | |
.addContainerGap() | |
.addComponent(button_ok) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(button_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(testDatabaseLabel) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(button_refresh))) | |
.addContainerGap()) | |
); | |
jPanel1Layout.setVerticalGroup( | |
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() | |
.addContainerGap() | |
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) | |
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(button_ok) | |
.addComponent(button_cancel) | |
.addComponent(button_refresh) | |
.addComponent(testDatabaseLabel)) | |
.addGap(0, 0, 0)) | |
); | |
jSplitPane1.setTopComponent(jPanel1); | |
jTable1.setAutoCreateRowSorter(true); | |
jTable1.setModel(tableModel); | |
jScrollPane1.setViewportView(jTable1); | |
jSplitPane1.setRightComponent(jScrollPane1); | |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
getContentPane().setLayout(layout); | |
layout.setHorizontalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING) | |
); | |
pack(); | |
}// </editor-fold>//GEN-END:initComponents | |
/** | |
* @param args the command line arguments | |
* @throws java.sql.SQLException | |
*/ | |
public static void main(String args[]) throws SQLException { | |
try { | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { | |
// if we fail to set the system look and feel, we continue with the default | |
} | |
/* Create and display the form */ | |
java.awt.EventQueue.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
new Gui().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify//GEN-BEGIN:variables | |
private javax.swing.JButton button_cancel; | |
private javax.swing.JButton button_ok; | |
private javax.swing.JButton button_refresh; | |
private javax.swing.JLabel jLabel1; | |
private javax.swing.JLabel jLabel3; | |
private javax.swing.JLabel jLabel5; | |
private javax.swing.JLabel jLabel7; | |
private javax.swing.JPanel jPanel1; | |
private javax.swing.JPanel jPanel2; | |
private javax.swing.JPanel jPanel3; | |
private javax.swing.JScrollPane jScrollPane1; | |
private javax.swing.JSplitPane jSplitPane1; | |
private javax.swing.JTable jTable1; | |
private javax.swing.JLabel label_abteilung; | |
private javax.swing.JLabel label_name; | |
private javax.swing.JLabel label_schluessel; | |
private javax.swing.JLabel testDatabaseLabel; | |
private javax.swing.JTextField textField_datum; | |
// End of variables declaration//GEN-END:variables | |
private final JPopupMenu popupMenu = new JPopupMenu(); | |
private final MouseListener popupListener = new MouseAdapter() { | |
@Override | |
public void mousePressed(MouseEvent e) { | |
maybeShowPopup(e); | |
} | |
@Override | |
public void mouseReleased(MouseEvent e) { | |
maybeShowPopup(e); | |
} | |
private void maybeShowPopup(MouseEvent e) { | |
Component c = e.getComponent(); | |
if (e.isPopupTrigger() && c.isEnabled()) { | |
popupMenu.show(c, e.getX(), e.getY()); | |
} | |
} | |
}; | |
} |