source: trunk/src/org/expeditee/items/widgets/SampleWidget1.java@ 1511

Last change on this file since 1511 was 1511, checked in by bnemhaus, 4 years ago

Frame::Parse has been updated to include a new boolean parameter. When true, widgets that are created as a result of the parse send not only notify the widget framework that they have been added, but are also visible. When false, they only notify they have been added.

The widget framework now distinguishes between added and visible widgets, this fixes a bug. Bug: when programmatically adding a widget to not the current frame, it never gets properly removed and therefore still catches click events from users. By distinguishing between adding and making visible this is avoided.


Another bug has been fixed. Bug: When setting a text item to have a right anchor, and then subsequently reducing the size of the window, this text item would get a width of zero assigned. This was caused by some issues with the logic of how right margins for items were calculated.

File size: 4.2 KB
Line 
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
19package org.expeditee.items.widgets;
20
21import java.awt.Component;
22import java.awt.event.ActionEvent;
23import java.awt.event.ActionListener;
24import java.awt.event.FocusEvent;
25import java.awt.event.FocusListener;
26import java.awt.event.ItemEvent;
27import java.awt.event.ItemListener;
28
29import javax.swing.InputVerifier;
30import javax.swing.JComboBox;
31import javax.swing.JComponent;
32
33import org.expeditee.items.Text;
34
35public 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}
Note: See TracBrowser for help on using the repository browser.