1 | /**
|
---|
2 | * SampleWidget1.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items.widgets;
|
---|
20 |
|
---|
21 | import java.awt.Component;
|
---|
22 | import java.awt.event.ActionEvent;
|
---|
23 | import java.awt.event.ActionListener;
|
---|
24 | import java.awt.event.FocusEvent;
|
---|
25 | import java.awt.event.FocusListener;
|
---|
26 | import java.awt.event.ItemEvent;
|
---|
27 | import java.awt.event.ItemListener;
|
---|
28 |
|
---|
29 | import javax.swing.InputVerifier;
|
---|
30 | import javax.swing.JComboBox;
|
---|
31 | import javax.swing.JComponent;
|
---|
32 |
|
---|
33 | import org.expeditee.items.Text;
|
---|
34 |
|
---|
35 | public class SampleWidget1 extends DataFrameWidget {
|
---|
36 |
|
---|
37 | private static final String[] STRINGS = new String[] { "dog", "fish", "cat", "pig" };
|
---|
38 | //private static final String[] STRINGS = new String[] { "1", "2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4" };
|
---|
39 | private JComboBox<String> cmboOptions;
|
---|
40 |
|
---|
41 | public SampleWidget1(Text source, String[] args) {
|
---|
42 | super(source, new JComboBox<String>(STRINGS), 200, 200, 50, 50);
|
---|
43 | cmboOptions = (JComboBox<String>) _swingComponent;
|
---|
44 | final DisplaySelection listener = new DisplaySelection();
|
---|
45 | cmboOptions.addActionListener(listener);
|
---|
46 | cmboOptions.addItemListener(listener);
|
---|
47 | cmboOptions.addFocusListener(listener);
|
---|
48 | cmboOptions.setInputVerifier(listener);
|
---|
49 | for (Component c : cmboOptions.getComponents()) {
|
---|
50 | c.setFocusable(true);
|
---|
51 | }
|
---|
52 | System.err.println("SampleWidget1::setting light weight popup enabled to false.");
|
---|
53 | cmboOptions.setLightWeightPopupEnabled(false);
|
---|
54 | }
|
---|
55 |
|
---|
56 | private void printParents(final java.awt.Component component) {
|
---|
57 | if(component.getParent() != null) {
|
---|
58 | printParents(component.getParent());
|
---|
59 | System.err.println("Component: " + component);
|
---|
60 | } else {
|
---|
61 | System.err.println("Component: " + component);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | private final class DisplaySelection extends InputVerifier implements ActionListener, ItemListener, FocusListener {
|
---|
66 | @Override
|
---|
67 | public void actionPerformed(final ActionEvent e) {
|
---|
68 | System.err.println("Selected Item: " + cmboOptions.getSelectedItem());
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public void itemStateChanged(final ItemEvent e) {
|
---|
73 | if(e.getStateChange() == ItemEvent.SELECTED) {
|
---|
74 | //System.err.println("Combobox Selected");
|
---|
75 | //System.err.println("SampleWidget1::main component children count: " + cmboOptions.getComponents().length);
|
---|
76 | } else if (e.getStateChange() == ItemEvent.DESELECTED) {
|
---|
77 | //System.err.println("Combobox Lost Selection");
|
---|
78 | //System.err.println("SampleWidget1::main component children count: " + cmboOptions.getComponents().length);
|
---|
79 | } else {
|
---|
80 | //System.err.println("Unexpected Combobox e.getStateChange(): " + e.getStateChange());
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public void focusGained(FocusEvent e) {
|
---|
86 | //System.err.println("Combobox focus gained");
|
---|
87 | //System.err.println("Combobox popup visible: " + cmboOptions.isPopupVisible());
|
---|
88 | //System.err.println("Combobox popup object: " + cmboOptions.getComponentPopupMenu());
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public void focusLost(FocusEvent e) {
|
---|
93 | //System.err.println("Combobox focus lost");
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public boolean verify(JComponent input) {
|
---|
98 | return true;
|
---|
99 | //return !cmboOptions.isPopupVisible();
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | protected String[] getArgs() {
|
---|
105 | String[] stateArgs = new String[1];
|
---|
106 | stateArgs[0] = Integer.toString(cmboOptions.getSelectedIndex());
|
---|
107 | return stateArgs;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** TODO: REVISE
|
---|
111 | @Override
|
---|
112 | public void refresh() {
|
---|
113 | super.refresh();
|
---|
114 | Frame frame = getDataFrame();
|
---|
115 | if(frame != null) {
|
---|
116 | _combo.removeAllItems();
|
---|
117 | for(Text text: frame.getBodyTextItems(false)) {
|
---|
118 | _combo.addItem(text.getText());
|
---|
119 | }
|
---|
120 | _combo.setSelectedIndex(0);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | */
|
---|
124 |
|
---|
125 | }
|
---|