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 | 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 | }
|
---|