Mercurial > hg > accesspd
comparison java/src/uk/ac/qmul/eecs/ccmi/gui/ResourceFactory.java @ 0:78b7fc5391a2
first import, outcome of NIME 2014 hackaton
author | Fiore Martin <f.martin@qmul.ac.uk> |
---|---|
date | Tue, 08 Jul 2014 16:28:59 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78b7fc5391a2 |
---|---|
1 /* | |
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool | |
3 | |
4 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com) | |
5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) | |
6 | |
7 This program is free software: you can redistribute it and/or modify | |
8 it under the terms of the GNU General Public License as published by | |
9 the Free Software Foundation, either version 3 of the License, or | |
10 (at your option) any later version. | |
11 | |
12 This program is distributed in the hope that it will be useful, | |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 GNU General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 */ | |
20 | |
21 package uk.ac.qmul.eecs.ccmi.gui; | |
22 | |
23 import java.awt.event.ActionListener; | |
24 import java.beans.EventHandler; | |
25 import java.util.MissingResourceException; | |
26 import java.util.ResourceBundle; | |
27 | |
28 import javax.swing.JMenu; | |
29 import javax.swing.JMenuItem; | |
30 import javax.swing.KeyStroke; | |
31 | |
32 /** | |
33 * A factory class for swing components creation support. | |
34 * | |
35 * Components that are created via this class are, in turn created by {@code SpeechMenuFactory} methods. | |
36 * This class handles the labelling and other menu properties (such as accelerators) | |
37 * using the {@code ResourceBundle} passed as argument to the constructor. | |
38 */ | |
39 class ResourceFactory{ | |
40 public ResourceFactory(ResourceBundle bundle){ | |
41 this.bundle = bundle; | |
42 } | |
43 | |
44 public JMenu createMenu(String prefix){ | |
45 String text = bundle.getString(prefix + ".text"); | |
46 JMenu menu = SpeechMenuFactory.getMenu(text); | |
47 try{ | |
48 String mnemonic = bundle.getString(prefix + ".mnemonic"); | |
49 menu.setMnemonic(mnemonic.charAt(0)); | |
50 }catch (MissingResourceException exception){ | |
51 // ok not to set mnemonic | |
52 } | |
53 | |
54 try{ | |
55 String tooltip = bundle.getString(prefix + ".tooltip"); | |
56 menu.setToolTipText(tooltip); | |
57 } | |
58 catch (MissingResourceException exception){ | |
59 // ok not to set tooltip | |
60 } | |
61 return menu; | |
62 } | |
63 | |
64 public JMenuItem createMenuItem(String prefix, | |
65 Object target, String methodName){ | |
66 return createMenuItem(prefix, | |
67 (ActionListener) EventHandler.create( | |
68 ActionListener.class, target, methodName)); | |
69 } | |
70 | |
71 public JMenuItem createMenuItem(String prefix, | |
72 ActionListener listener){ | |
73 String text = bundle.getString(prefix + ".text"); | |
74 JMenuItem menuItem = SpeechMenuFactory.getMenuItem(text); | |
75 return configure(menuItem, prefix, listener); | |
76 } | |
77 | |
78 public JMenuItem createCheckBoxMenuItem(String prefix, | |
79 ActionListener listener){ | |
80 String text = bundle.getString(prefix + ".text"); | |
81 JMenuItem menuItem = SpeechMenuFactory.getJCheckBoxMenuItem(text); | |
82 return configure(menuItem, prefix, listener); | |
83 } | |
84 | |
85 private JMenuItem configure(JMenuItem menuItem, | |
86 String prefix, ActionListener listener){ | |
87 menuItem.addActionListener(listener); | |
88 try{ | |
89 String mnemonic = bundle.getString(prefix + ".mnemonic"); | |
90 menuItem.setMnemonic(mnemonic.charAt(0)); | |
91 }catch (MissingResourceException exception){ | |
92 // ok not to set mnemonic | |
93 } | |
94 | |
95 try{ | |
96 String accelerator = bundle.getString(prefix + ".accelerator"); | |
97 menuItem.setAccelerator(KeyStroke.getKeyStroke(accelerator)); | |
98 }catch (MissingResourceException exception){ | |
99 // ok not to set accelerator | |
100 } | |
101 | |
102 try{ | |
103 String tooltip = bundle.getString(prefix + ".tooltip"); | |
104 menuItem.setToolTipText(tooltip); | |
105 }catch (MissingResourceException exception){ | |
106 // ok not to set tooltip | |
107 } | |
108 return menuItem; | |
109 } | |
110 | |
111 private ResourceBundle bundle; | |
112 } |