Neu: JObjectSelect, JTimeSpanEditor

thobaben_diagram
Harald Wolff 2015-08-25 09:59:04 +02:00
parent e1d027e285
commit f6cc9b3403
2 changed files with 226 additions and 0 deletions

View File

@ -0,0 +1,102 @@
package org.hwo.ui;
import javax.swing.JComponent;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JObjectSelect extends JComponent {
private JTextField tfDisplay;
private List<Object> itemList;
private Object selectedItem;
public JObjectSelect() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
tfDisplay = new JTextField();
tfDisplay.setEditable(false);
GridBagConstraints gbc_tfDisplay = new GridBagConstraints();
gbc_tfDisplay.insets = new Insets(0, 0, 0, 5);
gbc_tfDisplay.fill = GridBagConstraints.BOTH;
gbc_tfDisplay.gridx = 0;
gbc_tfDisplay.gridy = 0;
add(tfDisplay, gbc_tfDisplay);
tfDisplay.setColumns(10);
JButton btnNewButton = new JButton("...");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showListPopup();
}
});
btnNewButton.setIcon(null);
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.fill = GridBagConstraints.BOTH;
gbc_btnNewButton.gridx = 1;
gbc_btnNewButton.gridy = 0;
add(btnNewButton, gbc_btnNewButton);
itemList = new ArrayList<Object>();
setSelectedItem(null);
}
public Object getSelectedItem(){
return this.selectedItem;
}
public void setSelectedItem(Object item){
this.selectedItem = item;
if (selectedItem != null){
tfDisplay.setText(selectedItem.toString());
} else {
tfDisplay.setText("");
}
}
public Object[] getItems(){
return itemList.toArray();
}
public void setItems(Object[] items){
this.itemList.clear();
this.itemList.addAll(Arrays.asList(items));
}
private void showListPopup(){
/* JPopupMenu popup = new JPopupMenu();
JList<Object> jlist = new JList<Object>(itemList.toArray());
popup.add(jlist);
popup.show(this, 0, getHeight());
*/
Object si = JObjectSelector.execute(getItems());
if (si != null){
setSelectedItem(si);
}
}
}

View File

@ -0,0 +1,124 @@
package org.hwo.ui;
import javax.swing.JComponent;
import java.awt.GridBagLayout;
import javax.swing.JSpinner;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
public class JTimeSpanEditor extends JComponent {
private JSpinner spDays;
private JSpinner spHours;
private JSpinner spMinutes;
private JSpinner spSeconds;
private JLabel lblD;
private JLabel lblNewLabel;
private JLabel label;
public JTimeSpanEditor() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] {0, 0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
spDays = new JSpinner();
GridBagConstraints gbc_spDays = new GridBagConstraints();
gbc_spDays.fill = GridBagConstraints.BOTH;
gbc_spDays.insets = new Insets(0, 0, 0, 5);
gbc_spDays.gridx = 0;
gbc_spDays.gridy = 0;
add(spDays, gbc_spDays);
lblD = new JLabel("d");
GridBagConstraints gbc_lblD = new GridBagConstraints();
gbc_lblD.insets = new Insets(0, 0, 0, 5);
gbc_lblD.gridx = 1;
gbc_lblD.gridy = 0;
add(lblD, gbc_lblD);
spHours = new JSpinner();
GridBagConstraints gbc_spHours = new GridBagConstraints();
gbc_spHours.fill = GridBagConstraints.BOTH;
gbc_spHours.insets = new Insets(0, 0, 0, 5);
gbc_spHours.gridx = 2;
gbc_spHours.gridy = 0;
add(spHours, gbc_spHours);
lblNewLabel = new JLabel(":");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
gbc_lblNewLabel.gridx = 3;
gbc_lblNewLabel.gridy = 0;
add(lblNewLabel, gbc_lblNewLabel);
spMinutes = new JSpinner();
GridBagConstraints gbc_spMinutes = new GridBagConstraints();
gbc_spMinutes.fill = GridBagConstraints.BOTH;
gbc_spMinutes.insets = new Insets(0, 0, 0, 5);
gbc_spMinutes.gridx = 4;
gbc_spMinutes.gridy = 0;
add(spMinutes, gbc_spMinutes);
label = new JLabel(":");
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.insets = new Insets(0, 0, 0, 5);
gbc_label.gridx = 5;
gbc_label.gridy = 0;
add(label, gbc_label);
spSeconds = new JSpinner();
GridBagConstraints gbc_spSeconds = new GridBagConstraints();
gbc_spSeconds.fill = GridBagConstraints.BOTH;
gbc_spSeconds.gridx = 6;
gbc_spSeconds.gridy = 0;
add(spSeconds, gbc_spSeconds);
}
public Integer getValue(){
if (!spSeconds.isEnabled())
return null;
return ((Integer)spSeconds.getValue()) +
((Integer)spMinutes.getValue()) * 60 +
((Integer)spHours.getValue()) * 3600 +
((Integer)spDays.getValue()) * 86400
;
}
public void setValue(Integer seconds){
Integer mod = seconds;
if (mod == null){
spSeconds.setEnabled(false);
spMinutes.setEnabled(false);
spHours.setEnabled(false);
spDays.setEnabled(false);
} else {
spSeconds.setEnabled(true);
spMinutes.setEnabled(true);
spHours.setEnabled(true);
spDays.setEnabled(true);
spDays.setValue(mod / 86400);
mod %= 86400;
spHours.setValue(mod / 3600);
mod %= 3600;
spMinutes.setValue(mod / 60);
mod %= 60;
spSeconds.setValue(mod);
}
}
}