source: trunk/src/org/expeditee/items/widgets/SampleWidget2.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: 5.9 KB
Line 
1/**
2 * SampleWidget2.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.FlowLayout;
22import java.awt.Font;
23import java.awt.event.ActionEvent;
24import java.awt.event.ActionListener;
25import java.awt.event.ItemEvent;
26import java.awt.event.ItemListener;
27import java.util.Random;
28
29import javax.swing.JCheckBox;
30import javax.swing.JComboBox;
31import javax.swing.JLabel;
32import javax.swing.JPanel;
33import javax.swing.JSplitPane;
34import javax.swing.JTextArea;
35import javax.swing.JToggleButton;
36
37import org.expeditee.core.Colour;
38import org.expeditee.gio.swing.SwingConversions;
39import org.expeditee.gio.swing.SwingFontManager;
40import org.expeditee.items.Text;
41
42public class SampleWidget2 extends SwingWidget {
43
44 private static final Font STANDARD_FONT = SwingFontManager.getInstance().getInternalFont(SwingFontManager.getInstance().getDefaultFont());
45 private static final Font BIG_FONT = STANDARD_FONT.deriveFont(STANDARD_FONT.getSize() * 2f);
46 private final JComboBox<String> cmboPrepend;
47 private final JTextArea utxt = new JTextArea();;
48
49 public SampleWidget2(Text source, String[] args) {
50 super(source, new JSplitPane(JSplitPane.VERTICAL_SPLIT), 60, -1, 40, -1);
51 final JSplitPane sp = (JSplitPane)super._swingComponent;
52
53 //Build controls area.
54 final JPanel controls = new JPanel(new FlowLayout());
55 controls.setBackground(SwingConversions.toSwingColor(Colour.FromRGB255(255,228,195)));
56
57 final JLabel lblTitle = new JLabel("This is an example Widget!");
58 lblTitle.setFont(BIG_FONT);
59
60 final JToggleButton tbtnStyle = new JToggleButton("Toggle Style");
61 tbtnStyle.setFont(BIG_FONT);
62 tbtnStyle.addItemListener(new ToggleStyle(utxt));
63
64 final JCheckBox ckbBigFont = new JCheckBox("Big Font");
65 ckbBigFont.setBackground(null);
66 ckbBigFont.setSelected(true);
67 ckbBigFont.setFont(BIG_FONT);
68 ckbBigFont.addItemListener(new ToggleBigFont(utxt));
69
70 final Random r = new Random();
71 final String[] options = new String[] { "Item: " + r.nextInt(100), "Item: " + r.nextInt(100), "Item: " + r.nextInt(100), "Item: " + r.nextInt(100), "Item: " + r.nextInt(100),
72 "Item: " + r.nextInt(100), "Item: " + r.nextInt(100), "Item: " + r.nextInt(100), "Item: " + r.nextInt(100), "Item: " + r.nextInt(100) };
73 cmboPrepend = new JComboBox<String>(options);
74 cmboPrepend.setFont(BIG_FONT);
75 cmboPrepend.addActionListener(new PrependManager(cmboPrepend, utxt));
76 cmboPrepend.setLightWeightPopupEnabled(false);
77
78 controls.add(lblTitle);
79 controls.add(cmboPrepend);
80 controls.add(tbtnStyle);
81 controls.add(ckbBigFont);
82 sp.setTopComponent(controls);
83
84 //Build user text area.
85 utxt.setFont(BIG_FONT);
86 sp.setBottomComponent(utxt);
87
88 //Initialise state
89 init(args, cmboPrepend, utxt);
90 }
91
92 private void init(String[] args, final JComboBox<String> cmboPrepend, final JTextArea utxt) {
93 if (args != null && args.length >= 1) {
94
95 int selectedItem = 0;
96
97 // extract selected index
98 if (args.length >= 1 && args[0] != null) {
99 try {
100 selectedItem = Integer.parseInt(args[0]);
101 } catch (NumberFormatException e) {
102 e.printStackTrace();
103 }
104 }
105
106 if (selectedItem < 0) {
107 selectedItem = 0;
108 } else if (selectedItem >= cmboPrepend.getItemCount()) {
109 selectedItem = cmboPrepend.getItemCount() - 1;
110 }
111
112 cmboPrepend.setSelectedIndex(selectedItem);
113
114 if (args.length >= 2 && args[1] != null) {
115 utxt.setText(args[1]);
116 }
117
118 if (args.length >= 3 && args[2] != null) {
119 try {
120 int div = Integer.parseInt(args[2]);
121 ((JSplitPane)super._swingComponent).setDividerLocation(div);
122 } catch (NumberFormatException e) {}
123
124 }
125 }
126 }
127
128 private class PrependManager implements ActionListener {
129 private JComboBox<String> toConsult;
130 private JTextArea toAlter;
131 public PrependManager(final JComboBox<String> toConsult, final JTextArea toAlter) {
132 this.toConsult = toConsult;
133 this.toAlter = toAlter;
134 }
135
136 @Override
137 public void actionPerformed(ActionEvent e) {
138 this.toAlter.setText(this.toConsult.getSelectedItem() + " " + this.toAlter.getText());
139 }
140 }
141
142 private class ToggleStyle implements ItemListener {
143 private JTextArea toToggle;
144 public ToggleStyle(final JTextArea toToggle) {
145 this.toToggle = toToggle;
146 }
147
148 @Override
149 public void itemStateChanged(final ItemEvent e) {
150 if(e.getStateChange() == ItemEvent.SELECTED) {
151 toToggle.setFont(toToggle.getFont().deriveFont(Font.BOLD));
152 } else if (e.getStateChange() == ItemEvent.DESELECTED) {
153 toToggle.setFont(toToggle.getFont().deriveFont(Font.PLAIN));
154 }
155 }
156 }
157
158 private class ToggleBigFont implements ItemListener {
159 private JTextArea toToggle;
160
161 public ToggleBigFont(final JTextArea toToggle) {
162 this.toToggle = toToggle;
163 }
164
165 @Override
166 public void itemStateChanged(final ItemEvent e) {
167 if(e.getStateChange() == ItemEvent.SELECTED) {
168 toToggle.setFont(BIG_FONT.deriveFont(toToggle.getFont().getStyle()));
169 } else if(e.getStateChange() == ItemEvent.DESELECTED) {
170 toToggle.setFont(STANDARD_FONT.deriveFont(toToggle.getFont().getStyle()));
171 }
172 }
173 }
174
175 @Override
176 protected String[] getArgs() {
177
178 return new String[] {
179 Integer.toString(cmboPrepend.getSelectedIndex()),
180 utxt.getText(),
181 Integer.toString(((JSplitPane)super._swingComponent).getDividerLocation())
182 };
183 }
184}
Note: See TracBrowser for help on using the repository browser.