Skip to content

Commit

Permalink
popup-Menue with copy
Browse files Browse the repository at this point in the history
  • Loading branch information
donald committed Oct 10, 2014
1 parent a64e43c commit 5275904
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/de/mpg/molgen/buczek/portifix/Gui.form
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
<Property name="title" type="java.lang.String" value="Portifix"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
Expand Down
82 changes: 64 additions & 18 deletions src/de/mpg/molgen/buczek/portifix/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@

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;
Expand All @@ -19,7 +24,10 @@
import java.util.Properties;
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.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
Expand Down Expand Up @@ -80,17 +88,16 @@ private String singleColumnValueOfArray(String s[]) {
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);
}
}
component.setEnabled(enabled);
if (component instanceof Container) {
Container container = (Container) component;
for (Component child : container.getComponents()) {
setTreeEnabled(child, enabled);
}
}
}


private void updateForm() {

Expand All @@ -100,7 +107,7 @@ private void updateForm() {
} else {
String[] name = new String[selected.length];
for (int i = 0; i < name.length; i++) {
int mi=jTable1.convertRowIndexToModel(selected[i]);
int mi = jTable1.convertRowIndexToModel(selected[i]);
name[i] = tableModel.getValueAt(mi, 1) + " " + tableModel.getValueAt(mi, 0);
}
jLabel_name.setText(singleColumnValueOfArray(name));
Expand All @@ -113,6 +120,7 @@ private void updateForm() {

private void startEdit() {
jTable1.setEnabled(false);
setTreeEnabled(jTable1, false);
jButton_refresh.setEnabled(false);
jButton_saveall.setEnabled(true);
jButton_savethis.setEnabled(true);
Expand All @@ -121,6 +129,7 @@ private void startEdit() {

private void stopEdit() {
jTable1.setEnabled(true);
setTreeEnabled(jTable1, true);
jButton_refresh.setEnabled(true);
jButton_saveall.setEnabled(false);
jButton_savethis.setEnabled(false);
Expand Down Expand Up @@ -169,6 +178,8 @@ public void run() {
tableModel = new TableModel();
initComponents();

jTable1.addMouseListener(popupListener);

Properties p = new Properties();
p.setProperty("text.today", "today");
p.setProperty("text.month", "month");
Expand Down Expand Up @@ -208,7 +219,8 @@ public void focusGained(FocusEvent e) {
}

@Override
public void focusLost(FocusEvent e) { }
public void focusLost(FocusEvent e) {
}
});

jButton_cancel.addActionListener(new ActionListener() {
Expand All @@ -233,6 +245,17 @@ public void actionPerformed(ActionEvent e) {
});

getRootPane().setDefaultButton(jButton_saveall);

// 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();
}

Expand Down Expand Up @@ -264,6 +287,7 @@ private void initComponents() {
jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Portifix");

jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

Expand Down Expand Up @@ -414,6 +438,7 @@ private void jButton_saveallActionPerformed(java.awt.event.ActionEvent evt) {//G

/**
* @param args the command line arguments
* @throws java.sql.SQLException
*/
public static void main(String args[]) throws SQLException {

Expand All @@ -430,19 +455,16 @@ public static void main(String args[]) throws SQLException {
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Gui().setVisible(true);
}
Expand All @@ -468,4 +490,28 @@ public void run() {
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField_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());
}
}

};

}
40 changes: 40 additions & 0 deletions src/de/mpg/molgen/buczek/portifix/PopupListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;

/**
*
* @author buczek
*/
public class PopupListener extends MouseAdapter {

JPopupMenu popup;

public PopupListener(JPopupMenu popup) {
this.popup = popup;
}

@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}

@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}

private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}

}

0 comments on commit 5275904

Please sign in to comment.