Source Code Cross Referenced for WidgetDemo.java in  » 6.0-JDK-Modules » j2me » personal » demos » 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 » 6.0 JDK Modules » j2me » personal.demos 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)WidgetDemo.java	1.6 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License version
009:         * 2 only, as published by the Free Software Foundation. 
010:         * 
011:         * This program is distributed in the hope that it will be useful, but
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:         * General Public License version 2 for more details (a copy is
015:         * included at /legal/license.txt). 
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * version 2 along with this work; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA 
021:         * 
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023:         * Clara, CA 95054 or visit www.sun.com if you need additional
024:         * information or have any questions. 
025:         */
026:        package personal.demos;
027:
028:        import java.awt.*;
029:        import java.awt.datatransfer.*;
030:        import java.awt.event.*;
031:        import java.io.*;
032:        import java.util.ArrayList;
033:        import basis.Builder;
034:        import basis.demos.Demo;
035:
036:        public class WidgetDemo extends Demo implements  ActionListener,
037:                ItemListener, MouseListener, ClipboardOwner {
038:            private GridBagLayout gbl;
039:            private GridBagConstraints gbc;
040:            private Button button = new Button("Button");
041:            private Checkbox checkBox = new Checkbox("Checkbox");
042:            private TextField textField = new TextField();
043:            private TextArea textArea = new TextArea();
044:            private List list = new List(3, false);
045:            private Choice choice = new Choice();
046:            private Popup popup = new Popup();
047:            private Clipboard internalClipboard = new Clipboard("Internal");
048:            private Clipboard systemClipboard;
049:            private Clipboard clipboard = internalClipboard;
050:            private Color[] colors = { Builder.SUN_BLUE, Builder.SUN_YELLOW,
051:                    Builder.SUN_RED };
052:            private String[] strings = {};
053:
054:            public static void main(String args[]) {
055:                new WidgetDemo();
056:            }
057:
058:            public WidgetDemo() {
059:                for (int i = 1; i <= 10; i++) {
060:                    list.add("List " + i);
061:                }
062:                for (int i = 1; i <= 10; i++) {
063:                    choice.add("Choice " + i);
064:                }
065:                button.addActionListener(this );
066:                checkBox.addItemListener(this );
067:                textField.addActionListener(this );
068:                list.addActionListener(this );
069:                list.addItemListener(this );
070:                choice.addItemListener(this );
071:                textArea.addMouseListener(this );
072:                Panel topPanel = new Panel();
073:                Panel leftPanel = new Panel();
074:                Panel rightPanel = new Panel();
075:                leftPanel.setLayout(new GridLayout(3, 1));
076:                leftPanel.add(button);
077:                leftPanel.add(checkBox);
078:                leftPanel.add(choice);
079:                rightPanel.setLayout(new BorderLayout());
080:                rightPanel.add("Center", list);
081:                rightPanel.add("South", textField);
082:                topPanel.setLayout(new GridLayout(1, 2));
083:                topPanel.add(leftPanel);
084:                topPanel.add(rightPanel);
085:                setLayout(new BorderLayout());
086:                add("North", topPanel);
087:                add("Center", textArea);
088:                add("South", popup);
089:            }
090:
091:            private void add(Component component, int gridx, int gridy,
092:                    int gridwidth, int gridheight, int fill, int ipadx,
093:                    int ipady, Insets insets, int anchor, double weightx,
094:                    double weighty) {
095:                gbc.gridx = gridx;
096:                gbc.gridy = gridy;
097:                gbc.gridwidth = gridwidth;
098:                gbc.gridheight = gridheight;
099:                gbc.fill = fill;
100:                gbc.ipadx = ipadx;
101:                gbc.ipady = ipady;
102:                gbc.insets = insets;
103:                gbc.anchor = anchor;
104:                gbc.weightx = weightx;
105:                gbc.weighty = weighty;
106:                gbl.setConstraints(component, gbc);
107:                add(component);
108:            }
109:
110:            public void actionPerformed(ActionEvent event) {
111:                if (event.getSource() == button) {
112:                    textArea.append("Button.\n");
113:                    int index = 0;
114:                    for (int i = 0; i < colors.length; i++) {
115:                        if (button.getBackground() == colors[i]) {
116:                            index = ++i < colors.length ? i : 0;
117:                            break;
118:                        }
119:                    }
120:                    button.setBackground(colors[index]);
121:                    Transferable contents = new TransferableColor(colors[index]);
122:                    clipboard.setContents(contents, this );
123:                    setStatus("Copy to " + clipboard.getName() + " clipboard");
124:                }
125:                if (event.getSource() == textField) {
126:                    textArea.append("TextField: " + textField.getText() + "\n");
127:                }
128:            }
129:
130:            public void itemStateChanged(ItemEvent event) {
131:                if (event.getSource() == checkBox) {
132:                    if ((checkBox.getState()) == true) {
133:                        textArea.append("Checkbox ON.\n");
134:                        if (systemClipboard == null) {
135:                            Toolkit tk = Toolkit.getDefaultToolkit();
136:                            systemClipboard = tk.getSystemClipboard();
137:                        }
138:                        clipboard = systemClipboard;
139:                    } else {
140:                        textArea.append("Checkbox OFF.\n");
141:                        clipboard = internalClipboard;
142:                    }
143:                    setStatus("Using " + clipboard.getName() + " clipboard");
144:                }
145:                if (event.getSource() == list) {
146:                    textArea.append(list.getSelectedItem() + "\n");
147:                }
148:                if (event.getSource() == choice) {
149:                    textArea.append(choice.getSelectedItem() + "\n");
150:                }
151:            }
152:
153:            public void mouseClicked(MouseEvent e) {
154:                int modifier = e.getModifiers();
155:                if ((modifier & InputEvent.BUTTON1_MASK) != 0) {
156:                    return;
157:                }
158:                if ((modifier & InputEvent.BUTTON2_MASK) != 0) {
159:                    return;
160:                }
161:                setStatus("Pasting to " + clipboard.getName() + " clipboard...");
162:                Transferable contents = clipboard.getContents(null);
163:                if (contents == null) {
164:                    setStatus(clipboard.getName() + " clipboard empty!");
165:                    return;
166:                }
167:                String status = "Paste ";
168:                boolean found = false;
169:                DataFlavor[] flavors = contents.getTransferDataFlavors();
170:                for (int i = 0; i < flavors.length; i++) {
171:                    DataFlavor flavor = (DataFlavor) flavors[i];
172:                    status += flavor.getHumanPresentableName() + " ";
173:                    setStatus(status);
174:                    try {
175:                        Object object = contents.getTransferData(flavor);
176:                        if (object instanceof  Color) {
177:                            status += "(Color) ";
178:                            setStatus(status);
179:                            textArea.setBackground((Color) object);
180:                            found = true;
181:                            break;
182:                        }
183:                        ArrayList list = new ArrayList();
184:                        if (object instanceof  String) {
185:                            status += "(String) ";
186:                            setStatus(status);
187:                            String string = (String) object;
188:                            String substring = null;
189:                            while (string != null) {
190:                                int index = string.indexOf('\n');
191:                                if (index >= 0) {
192:                                    substring = string.substring(0, index);
193:                                    string = string.substring(index + 1);
194:                                } else {
195:                                    substring = string;
196:                                    string = null;
197:                                }
198:                                list.add(substring);
199:                            }
200:                        }
201:                        BufferedReader br = null;
202:                        if (object instanceof  InputStream) {
203:                            status += "(InputStream) ";
204:                            setStatus(status);
205:                            InputStream is = (InputStream) object;
206:                            InputStreamReader isr = new InputStreamReader(is);
207:                            br = new BufferedReader(isr);
208:                        }
209:                        if (object instanceof  Reader) {
210:                            status += "(Reader) ";
211:                            setStatus(status);
212:                            Reader reader = (Reader) object;
213:                            br = new BufferedReader(reader);
214:                        }
215:                        if (object instanceof  InputStream
216:                                || object instanceof  Reader) {
217:                            while (true) {
218:                                String string = br.readLine();
219:                                if (string == null) {
220:                                    break;
221:                                }
222:                                list.add(string);
223:                            }
224:                        }
225:                        if (object instanceof  String
226:                                || object instanceof  InputStream
227:                                || object instanceof  Reader) {
228:                            strings = new String[list.size()];
229:                            for (int j = 0; j < list.size(); j++) {
230:                                strings[j] = (String) list.get(j);
231:                            }
232:                            found = true;
233:                            break;
234:                        }
235:                    } catch (UnsupportedFlavorException ufe) {
236:                        setStatus("Unsupported flavor!");
237:                    } catch (IOException ioe) {
238:                        setStatus("IOException!");
239:                    }
240:                }
241:                if (found) {
242:                    repaint();
243:                } else {
244:                    setStatus("No data in " + clipboard.getName()
245:                            + " clipboard!");
246:                }
247:            }
248:
249:            public void mouseEntered(MouseEvent e) {
250:            }
251:
252:            public void mouseExited(MouseEvent e) {
253:            }
254:
255:            public void mousePressed(MouseEvent e) {
256:            }
257:
258:            public void mouseReleased(MouseEvent e) {
259:            }
260:
261:            public void lostOwnership(Clipboard clipboard, Transferable contents) {
262:                setStatus("Lost ownership of " + clipboard.getName()
263:                        + " clipboard!");
264:            }
265:
266:            class Popup extends Component implements  ActionListener {
267:                final Font DEFAULT_FONT = new Font("sanserif", Font.BOLD, 12);
268:                final Color DEFAULT_COLOR = Builder.SUN_RED;
269:                private String label = "Popup";
270:                private Dimension preferredSize;
271:                PopupMenu popupMenu = new PopupMenu("Popup Menu");
272:
273:                Popup() {
274:                    setForeground(DEFAULT_COLOR);
275:                    for (int i = 1; i <= 10; i++) {
276:                        MenuItem mi = new MenuItem("Popup " + i);
277:                        mi.addActionListener(this );
278:                        popupMenu.add(mi);
279:                    }
280:                    add(popupMenu);
281:                    enableEvents(AWTEvent.MOUSE_EVENT_MASK);
282:                }
283:
284:                public Dimension getPreferredSize() {
285:                    if (preferredSize == null) {
286:                        Graphics g = getGraphics();
287:                        FontMetrics fm = g.getFontMetrics(DEFAULT_FONT);
288:                        int fw = fm.stringWidth(label);
289:                        int fh = fm.getHeight();
290:                        preferredSize = new Dimension(fw + 4, fh + 4);
291:                    }
292:                    return preferredSize;
293:                }
294:
295:                public Dimension getMinimumSize() {
296:                    return getPreferredSize();
297:                }
298:
299:                public Dimension getMaximumSize() {
300:                    return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
301:                }
302:
303:                public void paint(Graphics g) {
304:                    Dimension d = getSize();
305:                    g.setColor(getForeground());
306:                    g.fillRect(0, 0, d.width, d.height);
307:                    g.setColor(Color.white);
308:                    g.setFont(DEFAULT_FONT);
309:                    FontMetrics fm = g.getFontMetrics(DEFAULT_FONT);
310:                    int fw = fm.stringWidth(label);
311:                    int fa = fm.getAscent();
312:                    g.drawString(label, (d.width - fw) / 2,
313:                            (d.height + fa) / 2 - 1);
314:                }
315:
316:                protected void processMouseEvent(MouseEvent event) {
317:                    if (event.isPopupTrigger()) {
318:                        popupMenu.show(this , event.getX(), event.getY());
319:                    }
320:                    super .processMouseEvent(event);
321:                }
322:
323:                public void actionPerformed(ActionEvent event) {
324:                    if (event.getSource() == popupMenu) {
325:                        textArea.append(event.getActionCommand() + "\n");
326:                    }
327:                }
328:            }
329:        }
330:
331:        class TransferableColor implements  Transferable {
332:            public static DataFlavor colorFlavor = new DataFlavor(Color.class,
333:                    "Java Color Object");
334:            private static DataFlavor[] flavors = { colorFlavor,
335:                    DataFlavor.stringFlavor };
336:            private Color color;
337:
338:            public TransferableColor(Color color) {
339:                this .color = color;
340:            }
341:
342:            public DataFlavor[] getTransferDataFlavors() {
343:                return (DataFlavor[]) flavors.clone();
344:            }
345:
346:            public boolean isDataFlavorSupported(DataFlavor flavor) {
347:                for (int i = 0; i < flavors.length; i++) {
348:                    if (flavor.equals(flavors[i])) {
349:                        return true;
350:                    }
351:                }
352:                return false;
353:            }
354:
355:            public Object getTransferData(DataFlavor flavor) {
356:                if (flavor.equals(flavors[0])) {
357:                    return color;
358:                }
359:                if (flavor.equals(flavors[1])) {
360:                    return color.toString();
361:                }
362:                return null;
363:            }
364:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.