Source Code Cross Referenced for CloseControlPanel.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 java.awt.*;
004:        import java.awt.event.*;
005:        import javax.swing.*;
006:
007:        /** A simple utility to add a close button in your dialogs or frames.
008:         *  use getWasAccepted() AND/OR  getWasCancelled() to know which close type occured.
009:         * <P> if you want to ensure that the user accepted it, check getWasAccepted() if you
010:         * just want to know if the user explicitely cancelled it => getWasCancelled().
011:         *<P> Don't forget that the user nurmally can also close the dialog with the upper right
012:         * close button in the frame title OR ALT+F4.
013:         * You can explicitely defend through setting of the (JDialog dialog).setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
014:         */
015:        public class CloseControlPanel extends JPanel {
016:            private final JButton cancelBT = new JButton("Cancel",
017:                    Icons.sharedCross);
018:
019:            private final JButton okBT = new JButton("Close");
020:
021:            private boolean accepted = false;
022:            private boolean cancelled = false;
023:
024:            public JButton getOkButton() {
025:                return okBT;
026:            }
027:
028:            public boolean getWasAccepted() {
029:                return accepted;
030:            }
031:
032:            public boolean getWasCancelled() {
033:                return cancelled;
034:            }
035:
036:            final Object frame;
037:
038:            public CloseControlPanel(final Object frame, boolean showCancel,
039:                    final boolean shouldCancelCallClose, final String okText) {
040:                super (new FlowLayout(FlowLayout.RIGHT));
041:                this .frame = frame;
042:
043:                setOpaque(false);
044:
045:                add(cancelBT);
046:                if (!showCancel)
047:                    cancelBT.setVisible(false);
048:
049:                okBT.setText(okText);
050:                add(okBT);
051:                okBT.setBackground(Color.orange);
052:
053:                if (okText.equalsIgnoreCase("ok") || showCancel) {
054:                    okBT.setIcon(Icons.sharedOk); // otherwise, the text may be "close" and it's not nice to see an Ok icon.
055:                }
056:
057:                okBT.addActionListener(new ActionListener() {
058:                    public void actionPerformed(ActionEvent e) {
059:                        accepted = true;
060:                        cancelled = false;
061:                        closeFrame();
062:                    }
063:                });
064:                cancelBT.addActionListener(new ActionListener() {
065:                    public void actionPerformed(ActionEvent e) {
066:                        cancelled = true;
067:                        accepted = false;
068:                        if (shouldCancelCallClose) {
069:                            closeFrame();
070:                        }
071:                    }
072:                });
073:
074:                okBT.setMargin(new Insets(0, 2, 0, 2));
075:                cancelBT.setMargin(new Insets(0, 2, 0, 2));
076:
077:                JComponent parentComp = null;
078:                if (frame instanceof  JFrame) {
079:                    try {
080:                        parentComp = (JComponent) ((JFrame) frame)
081:                                .getContentPane();
082:                    } catch (Exception ex) {
083:                        ex.printStackTrace();
084:                    }
085:                } else if (frame instanceof  JDialog) {
086:                    try {
087:                        parentComp = (JComponent) ((JDialog) frame)
088:                                .getContentPane();
089:                    } catch (Exception ex) {
090:                        ex.printStackTrace();
091:                    }
092:                } else if (frame instanceof  JInternalFrame) {
093:                    try {
094:                        parentComp = (JComponent) ((JInternalFrame) frame)
095:                                .getContentPane();
096:                    } catch (Exception ex) {
097:                        ex.printStackTrace();
098:                    }
099:                } else {
100:                    if (frame == null) {
101:                        new Throwable("null frame").printStackTrace();
102:                    } else {
103:                        new Throwable("invalid frame "
104:                                + frame.getClass().getName()).printStackTrace();
105:                    }
106:                }
107:
108:                if (parentComp != null) {
109:                    parentComp.registerKeyboardAction(new ActionListener() {
110:                        public void actionPerformed(ActionEvent ae) {
111:                            cancelAndClose();
112:                        }
113:                    }, "Escape", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0,
114:                            false),
115:                            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
116:                }
117:
118:            } // Constructor
119:
120:            private void closeFrame() {
121:                if (frame instanceof  JFrame) {
122:                    ((JFrame) frame).setVisible(false);
123:                } else if (frame instanceof  JDialog) {
124:                    ((JDialog) frame).setVisible(false);
125:                }
126:            }
127:
128:            public void acceptAndClose() {
129:                accepted = true;
130:                cancelled = false;
131:                closeFrame();
132:            }
133:
134:            public void cancelAndClose() {
135:                accepted = false;
136:                cancelled = true;
137:                closeFrame();
138:            }
139:
140:            public void _setOkButtonText(String txt) {
141:                okBT.setText(txt);
142:            }
143:
144:            public void setOkEnabled(boolean is) {
145:                okBT.setEnabled(is);
146:            }
147:
148:            public void setOkBackground(Color c) {
149:                okBT.setBackground(c);
150:            }
151:
152:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.