source: trunk/src/org/expeditee/items/widgets/SampleWidget2.java@ 1178

Last change on this file since 1178 was 1178, checked in by bln4, 6 years ago

org.expeditee.items.widgets.SampleWidget1 ->
org.expeditee.items.widgets.SampleWidget2 ->

Code tidying and additional functionality for the purposes of testing.

org.expeditee.items.widgets.SampleWidget3 ->

A new sample widget that was used to assist with debugging widget reimplementation.

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
77 controls.add(lblTitle);
78 controls.add(cmboPrepend);
79 controls.add(tbtnStyle);
80 controls.add(ckbBigFont);
81 sp.setTopComponent(controls);
82
83 //Build user text area.
84 utxt.setFont(BIG_FONT);
85 sp.setBottomComponent(utxt);
86
87 //Initialise state
88 init(args, cmboPrepend, utxt);
89 }
90
91 private void init(String[] args, final JComboBox<String> cmboPrepend, final JTextArea utxt) {
92 if (args != null && args.length >= 1) {
93
94 int selectedItem = 0;
95
96 // extract selected index
97 if (args.length >= 1 && args[0] != null) {
98 try {
99 selectedItem = Integer.parseInt(args[0]);
100 } catch (NumberFormatException e) {
101 e.printStackTrace();
102 }
103 }
104
105 if (selectedItem < 0) {
106 selectedItem = 0;
107 } else if (selectedItem >= cmboPrepend.getItemCount()) {
108 selectedItem = cmboPrepend.getItemCount() - 1;
109 }
110
111 cmboPrepend.setSelectedIndex(selectedItem);
112
113 if (args.length >= 2 && args[1] != null) {
114 utxt.setText(args[1]);
115 }
116
117 if (args.length >= 3 && args[2] != null) {
118 try {
119 int div = Integer.parseInt(args[2]);
120 ((JSplitPane)super._swingComponent).setDividerLocation(div);
121 } catch (NumberFormatException e) {}
122
123 }
124 }
125 }
126
127 private class PrependManager implements ActionListener {
128 private JComboBox<String> toConsult;
129 private JTextArea toAlter;
130 public PrependManager(final JComboBox<String> toConsult, final JTextArea toAlter) {
131 this.toConsult = toConsult;
132 this.toAlter = toAlter;
133 }
134
135 @Override
136 public void actionPerformed(ActionEvent e) {
137 this.toAlter.setText(this.toConsult.getSelectedItem() + " " + this.toAlter.getText());
138 }
139 }
140
141 private class ToggleStyle implements ItemListener {
142 private JTextArea toToggle;
143 public ToggleStyle(final JTextArea toToggle) {
144 this.toToggle = toToggle;
145 }
146
147 @Override
148 public void itemStateChanged(final ItemEvent e) {
149 if(e.getStateChange() == ItemEvent.SELECTED) {
150 toToggle.setFont(toToggle.getFont().deriveFont(Font.BOLD));
151 } else if (e.getStateChange() == ItemEvent.DESELECTED) {
152 toToggle.setFont(toToggle.getFont().deriveFont(Font.PLAIN));
153 }
154 }
155 }
156
157 private class ToggleBigFont implements ItemListener {
158 private JTextArea toToggle;
159
160 public ToggleBigFont(final JTextArea toToggle) {
161 this.toToggle = toToggle;
162 }
163
164 @Override
165 public void itemStateChanged(final ItemEvent e) {
166 if(e.getStateChange() == ItemEvent.SELECTED) {
167 toToggle.setFont(BIG_FONT.deriveFont(toToggle.getFont().getStyle()));
168 } else if(e.getStateChange() == ItemEvent.DESELECTED) {
169 toToggle.setFont(STANDARD_FONT.deriveFont(toToggle.getFont().getStyle()));
170 }
171 }
172 }
173
174 @Override
175 protected String[] getArgs() {
176
177 return new String[] {
178 Integer.toString(cmboPrepend.getSelectedIndex()),
179 utxt.getText(),
180 Integer.toString(((JSplitPane)super._swingComponent).getDividerLocation())
181 };
182 }
183}
Note: See TracBrowser for help on using the repository browser.