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 |
|
---|
19 | package org.expeditee.items.widgets;
|
---|
20 |
|
---|
21 | import java.awt.FlowLayout;
|
---|
22 | import java.awt.Font;
|
---|
23 | import java.awt.event.ActionEvent;
|
---|
24 | import java.awt.event.ActionListener;
|
---|
25 | import java.awt.event.ItemEvent;
|
---|
26 | import java.awt.event.ItemListener;
|
---|
27 | import java.util.Random;
|
---|
28 |
|
---|
29 | import javax.swing.JCheckBox;
|
---|
30 | import javax.swing.JComboBox;
|
---|
31 | import javax.swing.JLabel;
|
---|
32 | import javax.swing.JPanel;
|
---|
33 | import javax.swing.JSplitPane;
|
---|
34 | import javax.swing.JTextArea;
|
---|
35 | import javax.swing.JToggleButton;
|
---|
36 |
|
---|
37 | import org.expeditee.core.Colour;
|
---|
38 | import org.expeditee.gio.swing.SwingConversions;
|
---|
39 | import org.expeditee.gio.swing.SwingFontManager;
|
---|
40 | import org.expeditee.items.Text;
|
---|
41 |
|
---|
42 | public 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 | }
|
---|