Source code: jpl2/common/gui/AWTToolkit.java
1 /***********************************************************************
2 * JavaPsionLink 2.0, a java implementation of the psion link protocol
3 * Copyright (C) 2002, 2003 John S Montgomery (john.montgomery@lineone.net)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 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 Lesser 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ************************************************************************/
19 package jpl2.common.gui;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.io.File;
24
25 public class AWTToolkit extends JPLToolkit {
26 private FileDialog chooseFolder = null;
27 private FileDialog saveFile = null;
28 private FileDialog openFile = null;
29 private int windowCount = 0;
30
31 /** Returns either a java.awt.Frame or a javax.swing.JFrame
32 **/
33 protected Frame makeFrameImp() {
34 Frame frame = new Frame();
35 frame.addWindowListener(
36 new WindowAdapter() {
37
38 public void windowClosing( WindowEvent we ) {
39 we.getWindow().dispose();
40 synchronized( getToolkit() ) {
41 windowCount--;
42 }
43 }
44
45 public void windowClosed( WindowEvent we ) {
46 synchronized( getToolkit() ) {
47 if ( windowCount <= 0 )
48 System.exit( 0 );
49 }
50 }
51
52
53 } );
54
55 synchronized( getToolkit() ) {
56 windowCount++;
57 }
58
59 return frame;
60 }
61
62 /** Returns either a java.awt.Dialog or a javax.swing.JDialog
63 **/
64 protected Dialog makeDialogImp( Frame parent, boolean modal ) {
65 return new Dialog( parent, modal );
66 }
67
68 /** Adds a component to a window. In the case of swing getContentPane() must be used.
69 **/
70 public void add( Window w, Component c ) {
71 w.add( c );
72 }
73
74 /** Adds a component to a window. In the case of swing getContentPane() must be used.
75 **/
76 public void add( Window w, Component c, Object constraints ) {
77 w.add( c, constraints );
78 }
79
80 /** Sets a layout for a window. In the case of swing getContentPane() must be used.
81 **/
82 public void setLayout( Window w, LayoutManager l ) {
83 w.setLayout( l );
84 }
85
86 /** Returns either a java.awt.Panel or a javax.swing.JPanel.
87 **/
88 public Container makePanel() {
89 return new Panel();
90 }
91
92 /** Returns either a java.awt.Panel or a javax.swing.JPanel.
93 **/
94 protected Container makeGroupPanelImp( String title ) {
95 return new GroupPanel( title );
96 }
97
98 /** Returns either a java.awt.Button or a javax.swing.JButton.
99 **/
100 protected Component makeButtonImp( String text, String action, ActionListener listener ) {
101 Button button = new Button( text );
102 button.setActionCommand( action );
103 button.addActionListener( listener );
104 return button;
105 }
106
107 /** Make the button the default on a dialog.
108 **/
109 public void setDefaultButton( Dialog dialog, Component button ) {
110 setButtonKey( dialog, button, KeyEvent.VK_ENTER );
111 }
112
113 /** Make the button the "cancel" button on a dialog. ie the button
114 * which dismisses the dialog without takign any action.
115 **/
116 public void setCancelButton( Dialog dialog, Component button ) {
117 setButtonKey( dialog, button, KeyEvent.VK_ESCAPE );
118 }
119
120 private void addKeyListener( Component c, KeyListener kl ) {
121 c.addKeyListener( kl );
122 if ( c instanceof Container ) {
123 Component[] cs = ((Container)c).getComponents();
124 for ( int i = 0; i < cs.length; i++ )
125 addKeyListener( cs[ i ], kl );
126 }
127 }
128
129 /** Associate a key press with the button. ie so a key can be pressed instead of
130 * using the mouse to press the button.
131 **/
132 public void setButtonKey( Dialog dialog, Component button, int keyCode ) {
133 ButtonKeyAdapter adapter = new ButtonKeyAdapter( (Button)button, keyCode );
134 addKeyListener( dialog, adapter );
135 }
136
137 /** Make the button the default on a frame.
138 **/
139 public void setDefaultButton( Frame frame, Component button ) {
140 setButtonKey( frame, button, KeyEvent.VK_ENTER );
141 }
142
143 /** Make the button the "cancel" button on a frame. ie the button
144 * which dismisses the frame without taking any action.
145 **/
146 public void setCancelButton( Frame frame, Component button ) {
147 setButtonKey( frame, button, KeyEvent.VK_ESCAPE );
148 }
149
150 /** Associate a key press with the button. ie so a key can be pressed instead of
151 * using the mouse to press the button.
152 **/
153 public void setButtonKey( Frame frame, Component button, int keyCode ) {
154 ButtonKeyAdapter adapter = new ButtonKeyAdapter( (Button)button, keyCode );
155 addKeyListener( frame, adapter );
156 }
157
158 /** Returns either a java.awt.Label or a javax.swing.JLabel.
159 **/
160 protected Component makeLabelImp( String text, int horizontalAlignment ) {
161 return new Label( text, horizontalAlignment );
162 }
163
164 /** Sets the text on what may be a button or a label.
165 **/
166 public void setTextImp( Component c, String text ) {
167 if ( c instanceof Button ) {
168 ((Button)c).setLabel( text );
169 }
170 else if ( c instanceof Label ) {
171 Label label = (Label)c;
172 label.setText( text );
173 label.invalidate();
174 label.getParent().validate();
175 }
176 else {
177 ((TextComponent)c).setText( text );
178 }
179 }
180
181 /** Returns either a java.awt.MenuBar or a javax.swing.JMenuBar.
182 **/
183 public Object makeMenuBar() {
184 return new MenuBar();
185 }
186
187 /** Sets a frames menu bar.
188 **/
189 public void setMenuBar( Frame frame, Object menuBar ) {
190 frame.setMenuBar( (MenuBar)menuBar );
191 }
192
193 /** Returns either a java.awt.Menu or a javax.swing.JMenu.
194 **/
195 protected Object makeMenuImp( String label ) {
196 return new Menu( label );
197 }
198
199 /** Insert a seperator/divider into the menu.
200 **/
201 public void addSeparator( Object menu ) {
202 ((Menu)menu).addSeparator();
203 }
204
205 /** Remove all items from a menu.
206 **/
207 public void removeAll( Object menu ) {
208 ((Menu)menu).removeAll();
209 }
210
211 /** Add a menu to a menu bar.
212 **/
213 public void addMenu( Object menuBar, Object menu ) {
214 ((MenuBar)menuBar).add( (Menu)menu );
215 }
216
217 /** Set the help menu on a menu bar.
218 **/
219 public void setHelpMenu( Object menuBar, Object menu ) {
220 ((MenuBar)menuBar).setHelpMenu( (Menu)menu );
221 }
222
223 /** Returns either a java.awt.MenuItem or a javax.swing.JMenuItem.
224 **/
225 protected Object makeMenuItemImp( String label, MenuShortcut s, String action, ActionListener listener ) {
226 MenuItem item = s != null? new MenuItem( label, s ) : new MenuItem( label );
227 item.addActionListener( listener );
228 item.setActionCommand( action );
229 return item;
230 }
231
232 /** Add a menu item to a menu.
233 **/
234 public void addMenuItem( Object menu, Object menuItem ) {
235 ((Menu)menu).add( (MenuItem)menuItem );
236 }
237
238 /** Enable disable a menu/menuitem.
239 **/
240 public void setEnabled( Object menuItem, boolean enabled ) {
241 ((MenuItem)menuItem).setEnabled( enabled );
242 }
243
244
245 /** Returns either a java.awt.ScrollPane of a javax.swing.JScrollPane.
246 **/
247 public Container makeScrollPane( Component contents ) {
248 ScrollPane scrollPane = new ScrollPane();
249 scrollPane.add( contents );
250 return scrollPane;
251 }
252
253 /** Returns the viewport size of a scrollpane.
254 **/
255 public Dimension getViewportSize( Container scrollpane ) {
256 return ((ScrollPane)scrollpane).getViewportSize();
257 }
258
259 /** Returns the scroll position of a scrollpane.
260 **/
261 public Point getScrollPosition( Container scrollpane ) {
262 return ((ScrollPane)scrollpane).getScrollPosition();
263 }
264
265 /** Sets the scroll position of a scrollpane.
266 **/
267 public void setScrollPosition( Container scrollpane, int x, int y ) {
268 ((ScrollPane)scrollpane).setScrollPosition( x, y );
269 }
270
271 /** Returns either a java.awt.Choice or a javax.swing.JComboBox.
272 **/
273 public Component makeChoice( String[] items, ItemListener itemListener ) {
274 Choice choice = new Choice();
275 for ( int i = 0; i < items.length; i++ ) {
276 choice.add( items[ i ] );
277 }
278 if ( itemListener != null )
279 choice.addItemListener( itemListener );
280 return choice;
281 }
282
283 /** Set the items on a choice.
284 **/
285 public void setChoiceItems( Component choice, String[] items ) {
286 Choice awtChoice = (Choice)choice;
287 awtChoice.removeAll();
288 for ( int i = 0; i < items.length; i++ ) {
289 awtChoice.add( items[ i ] );
290 }
291 }
292
293 /** Returns either a java.awt.List or a javax.swing.JList.
294 **/
295 public Component makeList( String[] items, ItemListener itemListener ) {
296 List list = new List();
297 for ( int i = 0; i < items.length; i++ ) {
298 list.add( items[ i ] );
299 }
300 if ( itemListener != null )
301 list.addItemListener( itemListener );
302 return list;
303 }
304
305 /** Set the items on a list.
306 **/
307 public void setListItems( Component list, String[] items ) {
308 List awtList = (List)list;
309 awtList.removeAll();
310 for ( int i = 0; i < items.length; i++ ) {
311 awtList.add( items[ i ] );
312 }
313 }
314
315 /** Set the currently selected item on a choice.
316 **/
317 public void setSelected( Component choice, String item ) {
318 if ( choice instanceof Choice ) {
319 Choice ch = (Choice)choice;
320 for ( int i = 0; i < ch.getItemCount(); i++ ) {
321 String itemi = ch.getItem( i );
322 if ( itemi.equals( item ) ) {
323 ch.select( i );
324 break;
325 }
326 }
327 }
328 else {
329 List list = (List)choice;
330 for ( int i = 0; i < list.getItemCount(); i++ ) {
331 String itemi = list.getItem( i );
332 if ( itemi.equals( item ) ) {
333 list.select( i );
334 break;
335 }
336 }
337 }
338 }
339
340 /** Get the currently selected item on a choice.
341 **/
342 public String getSelected( Component choice ) {
343 Object[] selected = ((ItemSelectable )choice).getSelectedObjects();
344 if ( selected == null || selected.length == 0 )
345 return null;
346 return (String)selected[ 0 ];
347 }
348
349 /** Get the currently selected item on a choice.
350 **/
351 public int getSelectedIndex( Component choice ) {
352 if ( choice instanceof Choice )
353 return ((Choice)choice).getSelectedIndex();
354 else
355 return ((List)choice).getSelectedIndex();
356 }
357
358 /** Returns either a ProgressBar or a javax.swing.JProgressBar.
359 **/
360 public Component makeProgressBar() {
361 return new ProgressBar();
362 }
363
364 /** Set the max value on a progress bar.
365 **/
366 public void setProgressBarMax( Component progressBar, int max ) {
367 ((ProgressBar)progressBar).setMaxValue( max );
368 }
369
370 /** Set the current value of a progress bar.
371 **/
372 public void setProgressBarValue( Component progressBar, int value ) {
373 ((ProgressBar)progressBar).setValue( value );
374 }
375
376 /** Returns either a java.awt.TextField or a javax.swing.JTextField.
377 **/
378 public Component makeTextField( String text, int columns, TextListener listener ) {
379 TextField textField = new TextField( text, columns );
380 textField.setBackground( SystemColor.text );
381 if ( listener != null )
382 textField.addTextListener( listener );
383 return textField;
384 }
385
386 public void setEditable( Component textField, boolean editable ) {
387 ((TextField)textField).setEditable( editable );
388 }
389
390 /** Get the text from a text field.
391 **/
392 public String getText( Component textField ) {
393 return ((TextField)textField).getText();
394 }
395
396 /** Returns either a java.awt.Checkbox or a javax.swing.JCheckBox
397 **/
398 protected Component makeCheckBoxImpl( String label, ItemListener listener ) {
399 Checkbox checkBox = label != null? new Checkbox( label ) : new Checkbox();
400 if ( listener != null )
401 checkBox.addItemListener( listener );
402 return checkBox;
403 }
404
405 /** Get the current state of a CheckBox
406 **/
407 public boolean getCheckBoxState( Component checkBox ) {
408 return ((Checkbox)checkBox).getState();
409 }
410
411 /** Set the state of a CheckBox.
412 **/
413 public void setCheckBoxState( Component checkBox, boolean state ) {
414 ((Checkbox)checkBox).setState( state );
415 }
416
417 /** Under this setup can we create a folder/directory
418 * chooser?
419 **/
420 public boolean canChooseFolders() {
421 // in this case we see if we're on a mac, so we
422 // can use option 3 with FileDialog or not
423 return System.getProperty( "mrj.version" ) != null;
424 }
425
426 /** Display a dialog to allow the user to choose a folder (eg for
427 * where to download files to.
428 **/
429 protected File chooseFolderImp( Frame parent, String title ) {
430 if ( canChooseFolders() ) {
431 if ( chooseFolder == null ) {
432 chooseFolder = new FileDialog( parent, title, 3 );
433 }
434
435 chooseFolder.setTitle( title );
436 centreWindow( parent, chooseFolder );
437 chooseFolder.show();
438 String dir = chooseFolder.getDirectory();
439 if ( chooseFolder.getFile() != null && dir != null ) {
440 return new File( dir );
441 }
442
443 }
444 return null;
445 }
446
447
448 /** Display a dialog to allow the user to save a file.
449 **/
450 protected File chooseSaveFileImp( Frame parent, String title ) {
451 if ( saveFile == null ) {
452 saveFile = new FileDialog( parent, title, FileDialog.SAVE );
453 }
454
455 saveFile.setTitle( title );
456 centreWindow( parent, saveFile );
457 saveFile.show();
458 String dir = saveFile.getDirectory();
459 String file = saveFile.getFile();
460 if ( file != null && dir != null ) {
461 return new File( dir, file );
462 }
463 return null;
464 }
465
466 /** Display a dialog to allow the user to open a file.
467 **/
468 protected File chooseOpenFileImp( Frame parent, String title ) {
469 if ( openFile == null ) {
470 openFile = new FileDialog( parent, title, FileDialog.LOAD );
471 }
472
473 openFile.setTitle( title );
474 centreWindow( parent, openFile );
475 openFile.show();
476 String dir = openFile.getDirectory();
477 String file = openFile.getFile();
478 if ( file != null && dir != null ) {
479 return new File( dir, file );
480 }
481 return null;
482 }
483
484
485 }
486