package org.hwo.pulscounter.ui; import java.awt.BorderLayout; import java.awt.Component; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.border.EmptyBorder; import org.hwo.i18n.Messages; import org.hwo.models.TableMapper.TableColumn; import org.hwo.models.TableMapper.TableMapper; import org.hwo.models.TableMapper.TableMapperListener; import org.hwo.pulscounter.simplescript.SimpleScript; import org.hwo.pulscounter.simplescript.SimpleScript.ScriptCondition; import org.hwo.pulscounter.simplescript.SimpleScript.SimpleScriptElement; import static org.hwo.pulscounter.simplescript.SimpleScript.ScriptCondition.*; import static org.hwo.pulscounter.simplescript.SimpleScript.SimpleScriptElement.*; import org.hwo.ui.JMappedTable; import java.awt.GridBagLayout; import javax.swing.JTable; import java.awt.GridBagConstraints; import javax.swing.JScrollPane; import javax.swing.JLabel; import javax.swing.JMenuItem; import java.awt.Insets; import java.awt.MenuItem; import javax.swing.JTextField; import javax.swing.JTextArea; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Dialog.ModalExclusionType; import java.awt.Dialog.ModalityType; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; public class SimpleScriptEditor extends JDialog { public static boolean show(SimpleScript simpleScript){ return show(null,simpleScript); } public static boolean show(Component parent,SimpleScript simpleScript){ SimpleScriptEditor sse = new SimpleScriptEditor(); sse.setSimpleScript(simpleScript); if (parent != null){ sse.setLocationRelativeTo(parent); } sse.setVisible(true); return sse.isAccepted(); } private SimpleScript simpleScript; private boolean accepted; private final JPanel contentPanel = new JPanel(); private JMappedTable mtScriptElements; private JTextField tfName; private JTextArea taDescription; private JScrollPane spElementTable; /** * Create the dialog. */ public SimpleScriptEditor() { setTitle(Messages.getString("Skript bearbeiten")); setModalityType(ModalityType.APPLICATION_MODAL); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setBounds(100, 100, 921, 474); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); GridBagLayout gbl_contentPanel = new GridBagLayout(); gbl_contentPanel.columnWidths = new int[]{0, 0, 0}; gbl_contentPanel.rowHeights = new int[]{0, 0, 0, 0}; gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; gbl_contentPanel.rowWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE}; contentPanel.setLayout(gbl_contentPanel); MouseAdapter popupAdapter = new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { checkPopup(e); } @Override public void mousePressed(MouseEvent e) { if ((e.getComponent() == spElementTable)){ mtScriptElements.clearSelection(); } checkPopup(e); } @Override public void mouseReleased(MouseEvent e) { checkPopup(e); } private void checkPopup(MouseEvent e){ if (e.isPopupTrigger()){ JPopupMenu popupMenu = new JPopupMenu(); JMenuItem miAdd = new JMenuItem(Messages.getString("1x neu")); miAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { SimpleScriptElement sse = (SimpleScriptElement)mtScriptElements.getTableMapper().getSelectedRow(); mtScriptElements.getTableMapper().addRow(new SimpleScriptElement(),sse); } }); popupMenu.add(miAdd); JMenuItem miAdd5 = new JMenuItem(Messages.getString("5x neu")); miAdd5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { SimpleScriptElement sse = (SimpleScriptElement)mtScriptElements.getTableMapper().getSelectedRow(); mtScriptElements.getTableMapper().addRows(new SimpleScriptElement[]{ new SimpleScriptElement(), new SimpleScriptElement(), new SimpleScriptElement(), new SimpleScriptElement(), new SimpleScriptElement() }, sse); } }); popupMenu.add(miAdd5); SimpleScriptElement sse = (SimpleScriptElement)mtScriptElements.getTableMapper().getSelectedRow(); if (sse != null){ JMenuItem miRemove = new JMenuItem(Messages.getString("Entfernen")); miRemove.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { SimpleScriptElement[] selected = mtScriptElements.getTableMapper().getSelectedRows(new SimpleScriptElement[0]); for (SimpleScriptElement se: selected){ mtScriptElements.getTableMapper().removeRow(se); } } }); popupMenu.add(miRemove); } popupMenu.show(e.getComponent(), e.getX(), e.getY()); } } }; { JLabel lblBezeichnung = new JLabel(Messages.getString("Bezeichnung:")); GridBagConstraints gbc_lblBezeichnung = new GridBagConstraints(); gbc_lblBezeichnung.fill = GridBagConstraints.HORIZONTAL; gbc_lblBezeichnung.anchor = GridBagConstraints.NORTH; gbc_lblBezeichnung.insets = new Insets(0, 0, 5, 5); gbc_lblBezeichnung.gridx = 0; gbc_lblBezeichnung.gridy = 0; contentPanel.add(lblBezeichnung, gbc_lblBezeichnung); } { tfName = new JTextField(); GridBagConstraints gbc_tfName = new GridBagConstraints(); gbc_tfName.anchor = GridBagConstraints.NORTH; gbc_tfName.insets = new Insets(0, 0, 5, 0); gbc_tfName.fill = GridBagConstraints.HORIZONTAL; gbc_tfName.gridx = 1; gbc_tfName.gridy = 0; contentPanel.add(tfName, gbc_tfName); tfName.setColumns(10); } { JLabel lblBescrheibung = new JLabel(Messages.getString("Beschreibung:")); GridBagConstraints gbc_lblBescrheibung = new GridBagConstraints(); gbc_lblBescrheibung.anchor = GridBagConstraints.NORTH; gbc_lblBescrheibung.fill = GridBagConstraints.HORIZONTAL; gbc_lblBescrheibung.insets = new Insets(0, 0, 5, 5); gbc_lblBescrheibung.gridx = 0; gbc_lblBescrheibung.gridy = 1; contentPanel.add(lblBescrheibung, gbc_lblBescrheibung); } { taDescription = new JTextArea(); GridBagConstraints gbc_taDescription = new GridBagConstraints(); gbc_taDescription.insets = new Insets(0, 0, 5, 0); gbc_taDescription.fill = GridBagConstraints.BOTH; gbc_taDescription.gridx = 1; gbc_taDescription.gridy = 1; contentPanel.add(taDescription, gbc_taDescription); } { spElementTable = new JScrollPane(); GridBagConstraints gbc_spElementTable = new GridBagConstraints(); gbc_spElementTable.gridwidth = 2; gbc_spElementTable.fill = GridBagConstraints.BOTH; gbc_spElementTable.gridx = 0; gbc_spElementTable.gridy = 2; spElementTable.addMouseListener(popupAdapter); contentPanel.add(spElementTable, gbc_spElementTable); { mtScriptElements = new JMappedTable(SimpleScriptElement.class); mtScriptElements.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); mtScriptElements.setEditorEnabled(true); mtScriptElements.setFillsViewportHeight(true); mtScriptElements.addMouseListener(popupAdapter); spElementTable.setViewportView(mtScriptElements); } } { JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); getContentPane().add(buttonPane, BorderLayout.SOUTH); { JButton okButton = new JButton(Messages.getString("OK")); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { accept(); } }); okButton.setActionCommand("OK"); buttonPane.add(okButton); getRootPane().setDefaultButton(okButton); } { JButton cancelButton = new JButton(Messages.getString("Cancel")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cancel(); } }); cancelButton.setActionCommand("Cancel"); buttonPane.add(cancelButton); } } mtScriptElements.addTableMapperListener(new TableMapperListener() { @Override public boolean editorRequest(TableMapper tableMapper, Object row) { SimpleScriptElement e = (SimpleScriptElement)row; if (e != null){ if (SimpleScriptElementEditor.show(SimpleScriptEditor.this, e)){ mtScriptElements.repaint(); } } return true; } @Override public void ValueChanged(int row, int column) { // TODO Auto-generated method stub } @Override public void tableRowChanged(TableMapper sender, Object row) { // TODO Auto-generated method stub } }); mtScriptElements.getTableMapper().setEditorEnabled(true); initialize(); } private void accept(){ simpleScript.setName(tfName.getText()); simpleScript.setDescription(taDescription.getText()); simpleScript.setSimpleScriptElements(mtScriptElements.getTableMapper().getRows(SimpleScriptElement.class).toArray(new SimpleScriptElement[0])); accepted = true; setVisible(false); } private void cancel(){ setVisible(false); } public boolean isAccepted() { return accepted; } private void initialize(){ if (this.simpleScript == null){ this.simpleScript = new SimpleScript(); } tfName.setText(simpleScript.getName()); taDescription.setText(simpleScript.getDescription()); mtScriptElements.getTableMapper().setRows(new ArrayList(simpleScript.getSimpleScriptElements())); mtScriptElements.getTableMapper().setSortingEnabled(false); } public SimpleScript getSimpleScript(){ return this.simpleScript; } public void setSimpleScript(SimpleScript simpleScript){ this.simpleScript = simpleScript; initialize(); } }