|
| 1 | +package processing.mode.android; |
| 2 | + |
| 3 | +import processing.app.Base; |
| 4 | +import processing.app.Preferences; |
| 5 | + |
| 6 | +import javax.swing.*; |
| 7 | +import javax.swing.border.EmptyBorder; |
| 8 | +import java.awt.*; |
| 9 | +import java.awt.event.ActionEvent; |
| 10 | +import java.awt.event.ActionListener; |
| 11 | + |
| 12 | +public class DeviceSelector extends JFrame { |
| 13 | + |
| 14 | + JList list; |
| 15 | + |
| 16 | + public DeviceSelector() { |
| 17 | + super("Android device selector"); |
| 18 | + |
| 19 | + createLayout(); |
| 20 | + } |
| 21 | + |
| 22 | + private void refreshList() { |
| 23 | + Devices devices = Devices.getInstance(); |
| 24 | + java.util.List<Device> deviceList = devices.findMultiple(false); |
| 25 | + |
| 26 | + String[] data = new String[deviceList.size()]; |
| 27 | + for(int i = 0; i < deviceList.size(); i++) { |
| 28 | + data[i] = deviceList.get(0).toString(); |
| 29 | + } |
| 30 | + |
| 31 | + list.setListData(data); |
| 32 | + } |
| 33 | + |
| 34 | + private void createLayout() { |
| 35 | + Container outer = getContentPane(); |
| 36 | + outer.removeAll(); |
| 37 | + |
| 38 | + Box pain = Box.createVerticalBox(); |
| 39 | + pain.setBorder(new EmptyBorder(13, 13, 13, 13)); |
| 40 | + outer.add(pain); |
| 41 | + |
| 42 | + list = new JList(); |
| 43 | + list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
| 44 | + list.setLayoutOrientation(JList.VERTICAL); |
| 45 | + list.setVisibleRowCount(-1); |
| 46 | + |
| 47 | + refreshList(); |
| 48 | + |
| 49 | + JScrollPane listScroller = new JScrollPane(list); |
| 50 | + listScroller.setPreferredSize(new Dimension(250, 80)); |
| 51 | + pain.add(listScroller); |
| 52 | + |
| 53 | + // buttons |
| 54 | + JPanel buttons = new JPanel(); |
| 55 | +// buttons.setPreferredSize(new Dimension(400, 35)); |
| 56 | +// JPanel buttons = new JPanel() { |
| 57 | +// public Dimension getPreferredSize() { |
| 58 | +// return new Dimension(400, 35); |
| 59 | +// } |
| 60 | +// public Dimension getMinimumSize() { |
| 61 | +// return new Dimension(400, 35); |
| 62 | +// } |
| 63 | +// public Dimension getMaximumSize() { |
| 64 | +// return new Dimension(400, 35); |
| 65 | +// } |
| 66 | +// }; |
| 67 | + |
| 68 | +// Box buttons = Box.createHorizontalBox(); |
| 69 | + buttons.setAlignmentX(LEFT_ALIGNMENT); |
| 70 | + JButton okButton = new JButton("OK"); |
| 71 | + Dimension dim = new Dimension(Preferences.BUTTON_WIDTH, |
| 72 | + okButton.getPreferredSize().height); |
| 73 | + okButton.setPreferredSize(dim); |
| 74 | + okButton.addActionListener(new ActionListener() { |
| 75 | + public void actionPerformed(ActionEvent e) { |
| 76 | + setVisible(false); |
| 77 | + } |
| 78 | + }); |
| 79 | + okButton.setEnabled(true); |
| 80 | + |
| 81 | + JButton refreshButton = new JButton("Refresh"); |
| 82 | + refreshButton.setPreferredSize(dim); |
| 83 | + refreshButton.addActionListener(new ActionListener() { |
| 84 | + public void actionPerformed(ActionEvent e) { |
| 85 | + refreshList(); |
| 86 | + } |
| 87 | + }); |
| 88 | + refreshButton.setEnabled(true); |
| 89 | + |
| 90 | + JButton cancelButton = new JButton("Cancel"); |
| 91 | + cancelButton.setPreferredSize(dim); |
| 92 | + cancelButton.addActionListener(new ActionListener() { |
| 93 | + public void actionPerformed(ActionEvent e) { |
| 94 | + setVisible(false); |
| 95 | + } |
| 96 | + }); |
| 97 | + cancelButton.setEnabled(true); |
| 98 | + |
| 99 | + // think different, biznatchios! |
| 100 | + if (Base.isMacOS()) { |
| 101 | + buttons.add(cancelButton); |
| 102 | + |
| 103 | + buttons.add(refreshButton); |
| 104 | +// buttons.add(Box.createHorizontalStrut(8)); |
| 105 | + buttons.add(okButton); |
| 106 | + } else { |
| 107 | + buttons.add(okButton); |
| 108 | + |
| 109 | + buttons.add(refreshButton); |
| 110 | +// buttons.add(Box.createHorizontalStrut(8)); |
| 111 | + buttons.add(cancelButton); |
| 112 | + } |
| 113 | +// buttons.setMaximumSize(new Dimension(300, buttons.getPreferredSize().height)); |
| 114 | + pain.add(buttons); |
| 115 | + |
| 116 | + JRootPane root = getRootPane(); |
| 117 | + root.setDefaultButton(okButton); |
| 118 | + ActionListener disposer = new ActionListener() { |
| 119 | + public void actionPerformed(ActionEvent actionEvent) { |
| 120 | + setVisible(false); |
| 121 | + } |
| 122 | + }; |
| 123 | + processing.app.Toolkit.registerWindowCloseKeys(root, disposer); |
| 124 | + processing.app.Toolkit.setIcon(this); |
| 125 | + |
| 126 | + pack(); |
| 127 | + |
| 128 | + Dimension screen = processing.app.Toolkit.getScreenSize(); |
| 129 | + Dimension windowSize = getSize(); |
| 130 | + |
| 131 | + setLocation((screen.width - windowSize.width) / 2, |
| 132 | + (screen.height - windowSize.height) / 2); |
| 133 | + |
| 134 | + setVisible(true); |
| 135 | + } |
| 136 | +} |
0 commit comments