001 /*
002 * UnicodeCharInputPanel.java
003 * (C) chitec/Dirk Hillbrecht 2002, cantamen/Dirk Hillbrecht 2003.
004 * Modifications for BEE by T.J. Willis
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU General Public License
008 * as published by the Free Software Foundation; either version 2
009 * of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program; if not, write to the Free Software
018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
019 *
020 * $Header: /cvsroot/pavlov/net/sourceforge/bee/UnicodeCharInputPanel.java,v 1.7 2004/06/24 07:56:15 tj_willis Exp $
021 */
022 package net.sourceforge.bee;
023
024 import java.awt.*;
025 import java.awt.font.*;
026 import java.awt.geom.*;
027 import java.awt.event.*;
028 import javax.swing.*;
029
030 // MODIFICATIONS: removed dependencies on biz.chitec.quarterback packages
031 // removed main method
032 // removed resourcebundle references
033 // reformatted with JavaStyle
034 // focusEvent tweaks
035 // commented out the get...Dialog methods. problemmatic in Swing MDI
036 // replacing some GridBag layout with BoxLayout
037 // new internal classes/accessors for JFrame/JDialog
038 // made appropriate parameters final
039 // added preview button
040 /**
041 * Panel to input any Java unicode character.
042 *
043 *@author chitec/Dirk Hillbrecht 2002, cantamen/Dirk Hillbrecht 2003.
044 * Distributed under the terms of the {@link <a
045 * href="http://www.fsf.org/copyleft/gpl.html">GNU GPL</a> }.
046 *
047 *@version $Id: UnicodeCharInputPanel.java,v 1.4 2003/11/18 22:34:27
048 * dirkhillbrecht Exp $
049 */
050 public class UnicodeCharInputPanel extends JPanel
051 {
052
053 private char[] hexchar = "0123456789ABCDEF".toCharArray();
054
055 private final static Insets buttoninsets = new Insets(1, 2, 1, 2);
056
057 private BigToolTipJButton[] lowbutt;
058 private JLabel pagename;
059 private DisplayJButton preview;
060 private ButtonGroup[] uppernibblesbg;
061 private int upperbyte;
062 private String upperbytestring;
063 private Component eventsource;
064 private boolean postglobally;
065 private Component dispatchto;
066 private Box box;
067
068
069 public class UnicodeCharJFrame extends JFrame {
070 private UnicodeCharInputPanel pan;
071
072 public UnicodeCharJFrame()
073 {
074 super("Unicode Characters");
075 pan = UnicodeCharInputPanel.this;
076 add(pan);
077 pack();
078 setFocusable(false);
079 setFocusableWindowState(false);
080 }
081
082 public UnicodeCharInputPanel getUnicodeCharInputPanel(){
083 return pan;
084 }
085 }
086
087 public class UnicodeCharJDialog extends JDialog {
088 private UnicodeCharInputPanel pan;
089
090 public UnicodeCharJDialog(Frame owner)
091 {
092 super(owner,"Unicode Characters",false);
093 pan = UnicodeCharInputPanel.this;//new UnicodeCharInputPanel();
094 add(pan);
095 pack();
096 setFocusable(false);
097 setFocusableWindowState(false);
098 }
099
100 public UnicodeCharInputPanel getUnicodeCharInputPanel(){
101 return pan;
102 }
103 }
104
105 public UnicodeCharJDialog getJDialog(JFrame owner)
106 {
107 //UnicodeCharInputPanel pan = new UnicodeCharInputPanel();
108 return new UnicodeCharJDialog(owner);
109 }
110
111 public UnicodeCharJFrame getJFrame()
112 {
113 //UnicodeCharInputPanel pan = new UnicodeCharInputPanel();
114 return new UnicodeCharJFrame();
115 }
116
117 /**
118 * Creates the Panel.
119 */
120 public UnicodeCharInputPanel()
121 {
122 //super("Unicode Characters");
123 super();
124 javax.swing.ToolTipManager.sharedInstance().setEnabled(true);
125 //rb=RB.getBundle(this);
126 box = new Box(BoxLayout.Y_AXIS);
127 //setFocusable(false);
128 //setFocusableWindowState(false);
129 eventsource = this;
130 postglobally = false;
131 dispatchto = null;
132 JPanel upperbuttonspanel = new JPanel(new GridLayout(2, 16));
133 uppernibblesbg = new ButtonGroup[2];
134 ActionListener upbuttal =
135 new ActionListener()
136 {
137 public void actionPerformed(ActionEvent e)
138 {
139 setCharPage(uppernibblesbg[0].getSelection().getActionCommand() +
140 uppernibblesbg[1].getSelection().getActionCommand());
141 }
142 };
143 ActionListener lowbuttal =
144 new ActionListener()
145 {
146 public void actionPerformed(ActionEvent e)
147 {
148 typeChar(upperbytestring + ((AbstractButton) e.getSource()).getActionCommand());
149 }
150 };
151 for (int i = 0; i < 2; i++) {
152 uppernibblesbg[i] = new ButtonGroup();
153 for (int j = 0; j < 16; j++) {
154 String ac = hexchar[j] + "";
155 AbstractButton butt = new JToggleButton((i == 1 ? "x" : "") + ac + (i == 0 ? "x" : ""));
156 butt.setPreferredSize(new Dimension(32,32));
157 butt.setFocusable(false);
158 butt.setRequestFocusEnabled(false);
159 butt.setActionCommand(ac);
160 butt.setMargin(buttoninsets);
161 butt.addActionListener(upbuttal);
162 upperbuttonspanel.add(butt);
163 butt.setSelected(j == 0);
164 uppernibblesbg[i].add(butt);
165 }
166 }
167 GridBagLayout gbl = new GridBagLayout();
168 Box up = new Box(BoxLayout.X_AXIS);
169 up.setFocusable(false);
170 up.setRequestFocusEnabled(false);
171
172
173 preview = new DisplayJButton();
174 preview.setText(" ");
175 preview.setMinimumSize(new Dimension(64,64));
176 Font f = preview.getFont();
177 Font g = f.deriveFont( 0,(float)(3.0*f.getSize()));
178 preview.setFont(g);
179 /*
180 Graphics2D g2 = (Graphics2D)preview.getGraphics();
181 FontRenderContext frc = g2.getFontRenderContext();
182 Rectangle2D r2 = g.getMaxCharBounds(frc);
183 System.out.println("rect is:" + r2);
184 */
185
186 up.add(preview);
187 up.add(upperbuttonspanel);
188
189 pagename = new JLabel(" ");
190 Box centerpanel = new Box(BoxLayout.Y_AXIS);
191
192 JPanel lbp = new JPanel(new GridLayout(17, 17));
193 lbp.setFocusable(false);
194 lbp.setRequestFocusEnabled(false);
195 lbp.add(new JPanel());
196 for (int i = 0; i < 16; i++) {
197 lbp.add(new JLabel("x" + hexchar[i]));
198 }
199 int lbc = 0;
200 lowbutt = new BigToolTipJButton[256];
201 for (int i = 0; i < 16; i++) {
202 lbp.add(new JLabel(hexchar[i] + "x"));
203 for (int j = 0; j < 16; j++) {
204 BigToolTipJButton butt = new BigToolTipJButton();
205 //butt.setFocusable(false);
206 //butt.setRequestFocusEnabled(false);
207 butt.setActionCommand((i == 0 ? "0" : "") + Integer.toString(lbc, 16));
208 butt.addActionListener(lowbuttal);
209 butt.setMargin(buttoninsets);
210 butt.setToolTipText("N/A");
211 lbp.add(butt);
212 lowbutt[lbc++] = butt;
213 }
214 }
215
216 centerpanel.add(pagename);
217 centerpanel.add(lbp);
218
219 box.add( up);
220 box.add(centerpanel);
221
222 add(box);
223 //pack();
224 setCharPage("00");
225 }
226
227
228 /**
229 * Constructor for the UnicodeCharInputPanel object
230 *
231 *@param eventsourcex Description of the Parameter
232 */
233 public UnicodeCharInputPanel(final Component eventsourcex)
234 {
235 this();
236 setEventSource(eventsourcex);
237 }
238
239
240 /**
241 * Sets the eventSource attribute of the UnicodeCharInputPanel object
242 *
243 *@param eventsourcex The new eventSource value
244 */
245 public void setEventSource(final Component eventsourcex)
246 {
247 eventsource = (eventsourcex == null ? this : eventsourcex);
248 }
249
250
251 /**
252 * Sets the dispatchTarget attribute of the UnicodeCharInputPanel object
253 *
254 *@param dispatchtox The new dispatchTarget value
255 */
256 public void setDispatchTarget(Component dispatchtox)
257 {
258 dispatchto = dispatchtox;
259 }
260
261
262 /**
263 * Sets the postGlobally attribute of the UnicodeCharInputPanel object
264 *
265 *@param b The new postGlobally value
266 */
267 public void setPostGlobally(final boolean b)
268 {
269 postglobally = b;
270 }
271
272
273 /**
274 * Sets the charPage attribute of the UnicodeCharInputPanel object
275 *
276 *@param ux The new charPage value
277 */
278 private void setCharPage(final String ux)
279 {
280 upperbyte = Integer.parseInt(ux, 16) << 8;
281 int lastupperbyte = upperbyte + 255;
282 pagename.setText("Characters");
283 upperbytestring = ux;
284 for (int i = 0; i < 256; i++) {
285 String ch = new Character((char) (upperbyte + i)).toString();
286 lowbutt[i].setText(ch);
287 //lowbutt[i].setToolTip
288 }
289 //pack();
290 }
291
292 private class DisplayJButton extends JButton
293 {
294
295 public Dimension getPreferredSize(){
296 Dimension d= super.getPreferredSize();
297 Dimension x = getSize();
298 if(d.width>x.width) return d;
299 return x;
300 }
301 }
302
303 private class BigToolTipJButton extends JButton implements MouseListener
304 {
305 public BigToolTipJButton(){super();addMouseListener(this);}
306
307 //public
308 /*
309 public JToolTip createToolTip(){
310 System.out.println("Creating tooltip");
311 JToolTip t = new JToolTip();
312 String s = this.getText();
313 if(s==null) s = "N/A";
314 t.setTipText(s);
315 Font f = this.getFont();
316 Font g = f.deriveFont( (float)(4.0*f.getSize()));
317 t.setFont(g);
318 return t;
319 }*/
320
321
322 public void mouseClicked(MouseEvent e){}
323 public void mouseEntered(MouseEvent e){preview.setText(getText());}
324 public void mouseExited(MouseEvent e){preview.setText(" ");}
325 public void mousePressed(MouseEvent e){}
326 public void mouseReleased(MouseEvent e){}
327
328 }
329
330 /**
331 * Sends the "typed" key to whomever it may concern. If postglobally is
332 * activated, the event is put into the global AWT event handling. Otherwise
333 * (and _only_ otherwise) if a certain dispatching target has been given, the
334 * event is passed there.
335 *
336 *@param cx Description of the Parameter
337 */
338 private void typeChar(final String cx)
339 {
340 int code = Integer.parseInt(cx, 16);
341 System.out.println("code is: " + code);
342 Character ch = new Character((char) code);
343 AWTEvent theevent = new KeyEvent(eventsource,
344 KeyEvent.KEY_TYPED,
345 System.currentTimeMillis(),
346 0,
347 KeyEvent.VK_UNDEFINED,
348 ch.charValue());
349
350 if (postglobally) {
351 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(theevent);
352 }
353 else if (dispatchto != null) {
354 dispatchto.dispatchEvent(new FocusEvent(dispatchto, FocusEvent.FOCUS_GAINED));
355 dispatchto.dispatchEvent(theevent);
356 }
357 }
358 }
359