Source Code Cross Referenced for FontSelector.java in  » IDE » tIDE » snow » utils » gui » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE » tIDE » snow.utils.gui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package snow.utils.gui;
002:
003:        import javax.swing.*;
004:        import java.awt.*;
005:        import java.awt.event.*;
006:        import javax.swing.event.*;
007:
008:        public class FontSelector extends JDialog {
009:
010:            private static Font defaultFont = new Font("Dialog", Font.PLAIN, 12);
011:            private static FontSelector dialog;
012:            private static String NameValue;
013:            private static int StyleValue;
014:            private static int SizeValue;
015:            private static String SaveNameValue;
016:            private static int SaveStyleValue;
017:            private static int SaveSizeValue;
018:            private static boolean FontChosen = false;
019:            private JList theFontNamesList;
020:            private JTextField SizeField;
021:            private JList styleList;
022:            private JCheckBox fontBold, fontItalic;
023:            private JLabel sampleTextLabel;
024:            private Container contentPane;
025:            final JButton cancelButton = new JButton("Cancel");
026:            final JButton setButton = new JButton("Set");
027:
028:            private FontSelector(Frame frame, String[] fontNames, String title) {
029:                super (frame, title, true);
030:
031:                // Button Listeners :
032:
033:                cancelButton.addActionListener(new ActionListener() {
034:                    public void actionPerformed(ActionEvent e) {
035:                        FontChosen = false;
036:                        FontSelector.dialog.setVisible(false);
037:                    }
038:                });
039:
040:                setButton.addActionListener(new ActionListener() {
041:                    public void actionPerformed(ActionEvent e) {
042:                        // put the 3 font parms to the outer static parms :
043:                        Object[] selected = theFontNamesList
044:                                .getSelectedValues();
045:                        if ((selected != null) && (selected.length > 0)) {
046:                            boolean valid = true;
047:                            NameValue = selected[0].toString();
048:                            try {
049:                                SizeValue = Integer.parseInt(SizeField
050:                                        .getText());
051:                                if ((SizeValue > 0) && (SizeValue < 65)) {
052:                                    StyleValue = (fontBold.isSelected() ? Font.BOLD
053:                                            : 0)
054:                                            + (fontItalic.isSelected() ? Font.ITALIC
055:                                                    : 0);
056:                                    // and exit
057:                                    FontChosen = true;
058:                                    FontSelector.dialog.setVisible(false);
059:                                } else {
060:                                    valid = false;
061:                                }
062:                            } catch (Exception ex) {
063:                                valid = false;
064:                            }
065:                            if (!valid) {
066:                                JOptionPane.showMessageDialog(
067:                                        FontSelector.this ,
068:                                        "Please enter valid values.",
069:                                        "Invalid Value",
070:                                        JOptionPane.INFORMATION_MESSAGE);
071:                                SizeField.requestFocus();
072:                                SizeField.selectAll();
073:                            }
074:                        }
075:                    }
076:                });
077:                getRootPane().setDefaultButton(setButton);
078:
079:                // make the font face JList :
080:                theFontNamesList = new JList(fontNames);
081:                theFontNamesList
082:                        .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
083:
084:                // look for the initial fontname and select it, if found :
085:                this .theFontNamesList.setSelectedValue(NameValue, true);
086:
087:                theFontNamesList
088:                        .addListSelectionListener(new MyNameListSelectionListener());
089:                JScrollPane listScroller = new JScrollPane(theFontNamesList);
090:
091:                // set the size :
092:                Dimension listDim = new Dimension(SizeValue * 30,
093:                        SizeValue * 10);
094:                listScroller.setPreferredSize(listDim);
095:                listScroller.setMinimumSize(listDim);
096:                listScroller.setMaximumSize(listDim);
097:
098:                //Create a container so that we can add a title around
099:                //the scroll pane.  Can't add a title directly to the
100:                //scroll pane because its background would be white.
101:                //Lay out the label and scroll pane from top to button.
102:                JPanel listPane = new JPanel();
103:                listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS));
104:                listPane.add(Box.createRigidArea(new Dimension(0, 5)));
105:                listPane.add(listScroller);
106:                listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
107:                        10));
108:
109:                // FontSize/Style entry :
110:
111:                Integer theValue = new Integer(SizeValue);
112:                SizeField = new JTextField(theValue.toString(), 4);
113:                SizeField.setHorizontalAlignment(SwingConstants.RIGHT);
114:
115:                fontBold = new JCheckBox("Bold");
116:                if ((StyleValue & Font.BOLD) > 0) {
117:                    fontBold.setSelected(true);
118:                } else {
119:                    fontBold.setSelected(false);
120:                }
121:
122:                fontItalic = new JCheckBox("Italic");
123:                if ((StyleValue & Font.ITALIC) > 0) {
124:                    fontItalic.setSelected(true);
125:                } else {
126:                    fontItalic.setSelected(false);
127:                }
128:
129:                // define an actionlistener for all font change
130:                // events  triggered by the size field :
131:                ActionListener fontActionListener = new ActionListener() {
132:                    public void actionPerformed(ActionEvent evt) {
133:                        Object[] selected = theFontNamesList
134:                                .getSelectedValues();
135:                        if ((selected != null) && (selected.length > 0)) {
136:                            boolean valid = true;
137:                            NameValue = selected[0].toString();
138:                            try {
139:                                SizeValue = Integer.parseInt(SizeField
140:                                        .getText());
141:
142:                                if ((SizeValue > 0) && (SizeValue < 65)) {
143:                                    StyleValue = (fontBold.isSelected() ? Font.BOLD
144:                                            : 0)
145:                                            + (fontItalic.isSelected() ? Font.ITALIC
146:                                                    : 0);
147:                                    Font selectedFont = new Font(NameValue,
148:                                            StyleValue, SizeValue);
149:                                    sampleTextLabel.setFont(selectedFont);
150:                                    dialog.pack();
151:                                    dialog.repaint();
152:                                    sampleTextLabel.repaint();
153:                                } else {
154:                                    valid = false;
155:                                }
156:                            } catch (Exception ex) {
157:                                valid = false;
158:                            }
159:                            if (!valid) {
160:                                JOptionPane.showMessageDialog(
161:                                        FontSelector.this ,
162:                                        "Please enter valid values.",
163:                                        "Invalid Value",
164:                                        JOptionPane.INFORMATION_MESSAGE);
165:                                SizeField.requestFocus();
166:                                SizeField.selectAll();
167:                            }
168:                        }
169:                    }
170:                };
171:                SizeField.addActionListener(fontActionListener);
172:                fontBold.addActionListener(fontActionListener);
173:                fontItalic.addActionListener(fontActionListener);
174:
175:                JPanel stylePane = new JPanel();
176:                stylePane.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
177:                JLabel txtlabel = new JLabel(" Font Size : ", JLabel.CENTER);
178:                txtlabel.setFont(new Font(NameValue, StyleValue, SizeValue));
179:                txtlabel.setForeground(Color.black);
180:                stylePane.add(txtlabel);
181:                stylePane.add(SizeField);
182:                stylePane.add(fontBold);
183:                stylePane.add(fontItalic);
184:
185:                // Cancel and Set Buttons :
186:
187:                JPanel buttonPane = new JPanel();
188:                buttonPane.add(cancelButton);
189:                buttonPane.add(setButton);
190:
191:                // sample text
192:                JPanel samplePane = new JPanel();
193:                samplePane.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
194:                sampleTextLabel = new JLabel("This is a sample text.",
195:                        JLabel.CENTER);
196:                sampleTextLabel.setFont(new Font(NameValue, StyleValue,
197:                        SizeValue));
198:                sampleTextLabel.setForeground(Color.black);
199:                samplePane.add(sampleTextLabel);
200:
201:                JPanel bottomPane = new JPanel();
202:                bottomPane.setLayout(new BorderLayout(5, 5));
203:                bottomPane.add(samplePane, BorderLayout.NORTH);
204:                bottomPane.add(buttonPane, BorderLayout.SOUTH);
205:
206:                this .setLayout(new BorderLayout(5, 5));
207:
208:                //Put everything together, using the content pane's BorderLayout.
209:                contentPane = getContentPane();
210:                contentPane.add(listPane, BorderLayout.NORTH);
211:                contentPane.add(stylePane, BorderLayout.CENTER);
212:                contentPane.add(bottomPane, BorderLayout.SOUTH);
213:
214:                pack();
215:            }
216:
217:            class MyNameListSelectionListener implements  ListSelectionListener {
218:                public void valueChanged(ListSelectionEvent evt) {
219:                    if (!evt.getValueIsAdjusting()) {
220:                        JList this List = (JList) evt.getSource();
221:                        Object[] selected = this List.getSelectedValues();
222:
223:                        if ((selected != null) && (selected.length > 0)) {
224:                            try {
225:                                FontSelector.NameValue = selected[0].toString();
226:                                FontSelector.SizeValue = Integer
227:                                        .parseInt(SizeField.getText());
228:                                FontSelector.StyleValue = (fontBold
229:                                        .isSelected() ? Font.BOLD : 0)
230:                                        + (fontItalic.isSelected() ? Font.ITALIC
231:                                                : 0);
232:
233:                                Font selectedFont = new Font(NameValue,
234:                                        StyleValue, SizeValue);
235:                                sampleTextLabel.setFont(selectedFont);
236:                                dialog.pack();
237:                                dialog.repaint();
238:                                sampleTextLabel.repaint();
239:                            } catch (NumberFormatException ignored) {
240:                                System.out
241:                                        .println("Cannot parse an integer number from "
242:                                                + SizeField.getText());
243:                            }
244:                        }
245:                    }
246:                }
247:            }
248:
249:            public static Font showDialog(Component comp) {
250:                if (dialog != null) {
251:                    dialog.setLocationRelativeTo(comp);
252:                    int selIndex = dialog.theFontNamesList.getSelectedIndex();
253:                    if (selIndex >= 0) {
254:                        Rectangle selRect = dialog.theFontNamesList
255:                                .getCellBounds(selIndex, selIndex);
256:                        dialog.theFontNamesList.scrollRectToVisible(selRect);
257:                    }
258:                    dialog.setVisible(true);
259:                } else {
260:                    System.out
261:                            .println("ListDialog requires you to call initialize "
262:                                    + "before calling showDialog.");
263:                }
264:                if (FontChosen) {
265:                    return new Font(NameValue, StyleValue, SizeValue);
266:                } else {
267:                    return new Font(SaveNameValue, SaveStyleValue,
268:                            SaveSizeValue);
269:                }
270:            }
271:
272:            public static Font chooseFont(Component parentFrame) {
273:                return FontSelector.chooseFont(parentFrame, defaultFont);
274:            }
275:
276:            public static Font chooseFont(Component parentFrame,
277:                    Font defaultFont_)
278:            // version with passed defaultfont
279:            {
280:                defaultFont = defaultFont_; // used, if the cancelbutton is pressed
281:                NameValue = defaultFont.getName(); // static
282:                SizeValue = defaultFont.getSize(); // static
283:                if (SizeValue < 6) {
284:                    SizeValue = 6;
285:                }
286:                if (SizeValue > 100) {
287:                    SizeValue = 100;
288:                }
289:                StyleValue = defaultFont.getStyle(); // static
290:                // backup, case cancel is pressed :
291:                SaveNameValue = NameValue; // static
292:                SaveSizeValue = SizeValue; // static
293:                SaveStyleValue = StyleValue; // static
294:                String[] fontNames = GraphicsEnvironment
295:                        .getLocalGraphicsEnvironment()
296:                        .getAvailableFontFamilyNames();
297:                Frame frame = JOptionPane.getFrameForComponent(parentFrame);
298:                dialog = new FontSelector(frame, fontNames, "Choose a Font");
299:                return FontSelector.showDialog(parentFrame);
300:            }
301:
302:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.