001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package test.check;
031:
032: import java.awt.*;
033: import java.text.DecimalFormat;
034:
035: import javax.swing.*;
036:
037: import org.jvnet.lafwidget.LafWidget;
038: import org.jvnet.lafwidget.text.PasswordStrengthChecker;
039: import org.jvnet.lafwidget.utils.LafConstants.PasswordStrength;
040: import org.jvnet.substance.SubstanceLookAndFeel;
041:
042: import com.jgoodies.forms.builder.DefaultFormBuilder;
043: import com.jgoodies.forms.layout.FormLayout;
044:
045: /**
046: * Test application panel for testing {@link JTextArea}, {@link JTextField},
047: * {@link JTextPane}, {@link JPasswordField}, {@link JEditorPane} and
048: * {@link JFormattedTextField} components.
049: *
050: * @author Kirill Grouchnikov
051: */
052: public class TextFieldsPanel extends JPanel {
053: /**
054: * Returns the panel contents.
055: *
056: * @return The panel contents.
057: */
058: private JPanel getContents() {
059: FormLayout lm = new FormLayout(
060: "right:pref, 4dlu, fill:default:grow(1), 4dlu,"
061: + "fill:default:grow(1), 4dlu, fill:default:grow(1)",
062: "");
063: // lm.setColumnGroups(new int[][] { { 3, 5, 7 } });
064: DefaultFormBuilder builder = new DefaultFormBuilder(lm,
065: new ScrollablePanel());
066: builder.setDefaultDialogBorder();
067:
068: JLabel textLabel = new JLabel("Text fields");
069: JLabel formattedTextLabel = new JLabel("Formatted text fields");
070: JLabel passwordLabel = new JLabel("Password fields");
071: // textLabel.setFont(textLabel.getFont().deriveFont(Font.BOLD));
072: // formattedTextLabel.setFont(formattedTextLabel.getFont().deriveFont(
073: // Font.BOLD));
074: // passwordLabel.setFont(passwordLabel.getFont().deriveFont(Font.BOLD));
075:
076: builder.append("", textLabel);
077: builder.append(formattedTextLabel, passwordLabel);
078:
079: JTextField jtf1 = new JTextField("sample text");
080: builder.append("Enabled", jtf1);
081:
082: JFormattedTextField jftf1 = new JFormattedTextField(
083: new DecimalFormat("#,##0.0000"));
084: jftf1.setText("2,430.0000");
085: JPasswordField jpf1 = new JPasswordField("password", 10);
086: builder.append(jftf1, jpf1);
087:
088: JTextField jtfNotEditable = new JTextField("sample text");
089: jtfNotEditable.setEditable(false);
090: builder.append("Not editable", jtfNotEditable);
091:
092: JFormattedTextField jftfNotEditable = new JFormattedTextField(
093: new DecimalFormat("#,##0.0000"));
094: jftfNotEditable.setText("2,430.0000");
095: jftfNotEditable.setEditable(false);
096: JPasswordField jpfNotEditable = new JPasswordField("password",
097: 10);
098: jpfNotEditable.setEditable(false);
099: builder.append(jftfNotEditable, jpfNotEditable);
100:
101: JTextField jtfNotEditableNoLock = new JTextField("sample text");
102: jtfNotEditableNoLock.setEditable(false);
103: jtfNotEditableNoLock.putClientProperty(LafWidget.NO_LOCK_ICON,
104: Boolean.TRUE);
105: builder.append("Not editable no lock", jtfNotEditableNoLock);
106:
107: JFormattedTextField jftfNotEditableNoLock = new JFormattedTextField(
108: new DecimalFormat("#,##0.0000"));
109: jftfNotEditableNoLock.setText("2,430.0000");
110: jftfNotEditableNoLock.setEditable(false);
111: jftfNotEditableNoLock.putClientProperty(LafWidget.NO_LOCK_ICON,
112: Boolean.TRUE);
113: JPasswordField jpfNotEditableNoLock = new JPasswordField(
114: "password", 10);
115: jpfNotEditableNoLock.setEditable(false);
116: jpfNotEditableNoLock.putClientProperty(LafWidget.NO_LOCK_ICON,
117: Boolean.TRUE);
118: builder.append(jftfNotEditableNoLock, jpfNotEditableNoLock);
119:
120: JTextField jtfDisabled = new JTextField("sample text");
121: jtfDisabled.setEnabled(false);
122: builder.append("Disabled", jtfDisabled);
123:
124: JFormattedTextField jftfDisabled = new JFormattedTextField(
125: new DecimalFormat("#,##0.0000"));
126: jftfDisabled.setText("2,430.0000");
127: jftfDisabled.setEnabled(false);
128: JPasswordField jpfDisabled = new JPasswordField("password", 10);
129: jpfDisabled.setEnabled(false);
130: builder.append(jftfDisabled, jpfDisabled);
131:
132: JTextField jtfNonOpaque = new JTextField("sample text");
133: jtfNonOpaque.setOpaque(false);
134: builder.append("Non opaque", jtfNonOpaque);
135:
136: JFormattedTextField jftfNonOpaque = new JFormattedTextField(
137: new DecimalFormat("#,##0.0000"));
138: jftfNonOpaque.setText("2,430.0000");
139: jftfNonOpaque.setOpaque(false);
140: JPasswordField jpfNonOpaque = new JPasswordField("password", 10);
141: jpfNonOpaque.setOpaque(false);
142: builder.append(jftfNonOpaque, jpfNonOpaque);
143:
144: JTextField jtfWatermarkBleed = new JTextField("sample text");
145: jtfWatermarkBleed.putClientProperty(
146: SubstanceLookAndFeel.WATERMARK_TO_BLEED, Boolean.TRUE);
147: builder.append("Watermark bleed", jtfWatermarkBleed);
148:
149: JFormattedTextField jftfWatermarkBleed = new JFormattedTextField(
150: new DecimalFormat("#,##0.0000"));
151: jftfWatermarkBleed.setText("2,430.0000");
152: jftfWatermarkBleed.putClientProperty(
153: SubstanceLookAndFeel.WATERMARK_TO_BLEED, Boolean.TRUE);
154: JPasswordField jpfWatermarkBleed = new JPasswordField(
155: "password", 10);
156: jpfWatermarkBleed.putClientProperty(
157: SubstanceLookAndFeel.WATERMARK_TO_BLEED, Boolean.TRUE);
158: builder.append(jftfWatermarkBleed, jpfWatermarkBleed);
159:
160: JTextField jtf4 = new JTextField("sample text");
161: jtf4.putClientProperty(LafWidget.TEXT_SELECT_ON_FOCUS,
162: Boolean.TRUE);
163: builder.append("Select all on focus", jtf4);
164:
165: JFormattedTextField jftf4 = new JFormattedTextField(
166: new DecimalFormat("#,##0.0000"));
167: jftf4.setText("2,430.0000");
168: jftf4.putClientProperty(LafWidget.TEXT_SELECT_ON_FOCUS,
169: Boolean.TRUE);
170: builder.append(jftf4);
171: builder.nextLine();
172:
173: JTextField jtf4_1 = new JTextField("sample text");
174: jtf4_1.putClientProperty(LafWidget.TEXT_FLIP_SELECT_ON_ESCAPE,
175: Boolean.TRUE);
176: builder.append("Flip selection on ESC", jtf4_1);
177:
178: JFormattedTextField jftf4_1 = new JFormattedTextField(
179: new DecimalFormat("#,##0.0000"));
180: jftf4_1.setText("2,430.0000");
181: jftf4_1.putClientProperty(LafWidget.TEXT_FLIP_SELECT_ON_ESCAPE,
182: Boolean.TRUE);
183: builder.append(jftf4_1);
184: builder.nextLine();
185:
186: JTextField jtf5 = new JTextField("sample text");
187: jtf5.putClientProperty(LafWidget.TEXT_EDIT_CONTEXT_MENU,
188: Boolean.TRUE);
189: builder.append("With context menu", jtf5);
190:
191: JFormattedTextField jftf5 = new JFormattedTextField(
192: new DecimalFormat("#,##0.0000"));
193: jftf5.setText("2,430.0000");
194: jftf5.putClientProperty(LafWidget.TEXT_EDIT_CONTEXT_MENU,
195: Boolean.TRUE);
196: builder.append(jftf5);
197: builder.nextLine();
198:
199: JTextField jtf6 = new JTextField("sample text");
200: jtf6.setMargin(new Insets(2, 2, 2, 2));
201: builder.append("With margin a2", jtf6);
202:
203: JFormattedTextField jftf6 = new JFormattedTextField(
204: new DecimalFormat("#,##0.0000"));
205: jftf6.setText("2,430.0000");
206: jftf6.setMargin(new Insets(2, 2, 2, 2));
207: JPasswordField jpf6 = new JPasswordField("password", 10);
208: jpf6.setMargin(new Insets(2, 2, 2, 2));
209: builder.append(jftf6, jpf6);
210:
211: JTextField jtf60 = new JTextField("sample text");
212: jtf60.setMargin(new Insets(0, 10, 0, 0));
213: builder.append("With margin l10", jtf60);
214:
215: JFormattedTextField jftf60 = new JFormattedTextField(
216: new DecimalFormat("#,##0.0000"));
217: jftf60.setText("2,430.0000");
218: jftf60.setMargin(new Insets(0, 10, 0, 0));
219: JPasswordField jpf60 = new JPasswordField("password", 10);
220: jpf60.setMargin(new Insets(0, 10, 0, 0));
221: builder.append(jftf60, jpf60);
222:
223: JPasswordField jpfEmptyEchoChar = new JPasswordField(
224: "password", 10);
225: jpfEmptyEchoChar.setEchoChar((char) 0);
226: builder.append("Empty echo char", new JLabel());
227: builder.append(new JLabel(), jpfEmptyEchoChar);
228:
229: JPasswordField jpfStrengthChecker = new JPasswordField(
230: "password", 10);
231: try {
232: jpfStrengthChecker.putClientProperty(
233: LafWidget.PASSWORD_STRENGTH_CHECKER,
234: new PasswordStrengthChecker() {
235: public PasswordStrength getStrength(
236: char[] password) {
237: if (password == null)
238: return PasswordStrength.WEAK;
239: int length = password.length;
240: if (length < 3)
241: return PasswordStrength.WEAK;
242: if (length < 6)
243: return PasswordStrength.MEDIUM;
244: return PasswordStrength.STRONG;
245: }
246:
247: public String getDescription(
248: PasswordStrength strength) {
249: if (strength == PasswordStrength.WEAK)
250: return "<html>This password is <b>way</b> too weak</html>";
251: if (strength == PasswordStrength.MEDIUM)
252: return "<html>Come on, you can do<br> a little better than that</html>";
253: if (strength == PasswordStrength.STRONG)
254: return "OK";
255: return null;
256: }
257: });
258: builder.append("Strength checker", new JLabel());
259: builder.append(new JLabel(), jpfStrengthChecker);
260: } catch (Throwable t) {
261: }
262:
263: JPasswordField jpfTwoEchoChar = new JPasswordField("password",
264: 10);
265: jpfTwoEchoChar.putClientProperty(
266: SubstanceLookAndFeel.PASSWORD_ECHO_PER_CHAR,
267: new Integer(2));
268: builder.append("Two echo chars", new JLabel());
269: builder.append(new JLabel(), jpfTwoEchoChar);
270:
271: JLabel editorPaneLabel = new JLabel("Editor panes");
272: JLabel textAreaLabel = new JLabel("Text areas");
273: JLabel textPaneLabel = new JLabel("Text panes");
274: // editorPaneLabel
275: // .setFont(editorPaneLabel.getFont().deriveFont(Font.BOLD));
276: // textAreaLabel.setFont(textAreaLabel.getFont().deriveFont(Font.BOLD));
277: // textPaneLabel.setFont(textPaneLabel.getFont().deriveFont(Font.BOLD));
278:
279: builder.append("", editorPaneLabel);
280: builder.append(textAreaLabel, textPaneLabel);
281:
282: JEditorPane jep1 = new JEditorPane("text/html;",
283: "Sample <b>content</b><br> <u>text</u>");
284: builder.append("Enabled", jep1);
285:
286: JTextArea jta1 = new JTextArea("Sample content text", 3, 20);
287: JTextPane jtp1 = new JTextPane();
288: jtp1.replaceSelection("Sample content text");
289: jtp1.setPreferredSize(new Dimension(100, 40));
290: builder.append(jta1, jtp1);
291:
292: JEditorPane jepNotEditable = new JEditorPane("text/html;",
293: "Sample <b>content</b><br> <u>text</u>");
294: jepNotEditable.setEditable(false);
295: builder.append("Not editable", jepNotEditable);
296:
297: JTextArea jtaNotEditable = new JTextArea("Sample content text",
298: 3, 20);
299: jtaNotEditable.setEditable(false);
300: JTextPane jtpNotEditable = new JTextPane();
301: jtpNotEditable.replaceSelection("Sample content text");
302: jtpNotEditable.setPreferredSize(new Dimension(100, 40));
303: jtpNotEditable.setEditable(false);
304: builder.append(jtaNotEditable, jtpNotEditable);
305:
306: JEditorPane jepNotEditableNoLock = new JEditorPane(
307: "text/html;", "Sample <b>content</b><br> <u>text</u>");
308: jepNotEditableNoLock.setEditable(false);
309: jepNotEditableNoLock.putClientProperty(LafWidget.NO_LOCK_ICON,
310: Boolean.TRUE);
311: builder.append("Not editable no lock", jepNotEditableNoLock);
312:
313: JTextArea jtaNotEditableNoLock = new JTextArea(
314: "Sample content text", 3, 20);
315: jtaNotEditableNoLock.setEditable(false);
316: jtaNotEditableNoLock.putClientProperty(LafWidget.NO_LOCK_ICON,
317: Boolean.TRUE);
318: JTextPane jtpNotEditableNoLock = new JTextPane();
319: jtpNotEditableNoLock.replaceSelection("Sample content text");
320: jtpNotEditableNoLock.setPreferredSize(new Dimension(100, 40));
321: jtpNotEditableNoLock.setEditable(false);
322: jtpNotEditableNoLock.putClientProperty(LafWidget.NO_LOCK_ICON,
323: Boolean.TRUE);
324: builder.append(jtaNotEditableNoLock, jtpNotEditableNoLock);
325:
326: JEditorPane jep3 = new JEditorPane("text/html;",
327: "Sample <b>content</b><br> <u>text</u>");
328: jep3.setEnabled(false);
329: builder.append("Disabled", jep3);
330:
331: JTextArea jta3 = new JTextArea("Sample content text", 3, 20);
332: jta3.setEnabled(false);
333: JTextPane jtp3 = new JTextPane();
334: jtp3.replaceSelection("Sample content text");
335: jtp3.setPreferredSize(new Dimension(100, 40));
336: jtp3.setEnabled(false);
337: builder.append(jta3, jtp3);
338:
339: JEditorPane jepNonOpaque = new JEditorPane("text/html;",
340: "Sample <b>content</b><br> <u>text</u>");
341: jepNonOpaque.setOpaque(false);
342: builder.append("Non opaque", jepNonOpaque);
343:
344: JTextArea jtaNonOpaque = new JTextArea("Sample content text",
345: 3, 20);
346: jtaNonOpaque.setOpaque(false);
347: JTextPane jtpNonOpaque = new JTextPane();
348: jtpNonOpaque.replaceSelection("Sample content text");
349: jtpNonOpaque.setPreferredSize(new Dimension(100, 40));
350: jtpNonOpaque.setOpaque(false);
351: builder.append(jtaNonOpaque, jtpNonOpaque);
352:
353: JEditorPane jepWatermarkBleed = new JEditorPane("text/html;",
354: "Sample <b>content</b><br> <u>text</u>");
355: jepWatermarkBleed.putClientProperty(
356: SubstanceLookAndFeel.WATERMARK_TO_BLEED, Boolean.TRUE);
357: builder.append("Watermark bleed", jepWatermarkBleed);
358:
359: JTextArea jtaWatermarkBleed = new JTextArea(
360: "Sample content text", 3, 20);
361: jtaWatermarkBleed.putClientProperty(
362: SubstanceLookAndFeel.WATERMARK_TO_BLEED, Boolean.TRUE);
363: JTextPane jtpWatermarkBleed = new JTextPane();
364: jtpWatermarkBleed.replaceSelection("Sample content text");
365: jtpWatermarkBleed.setPreferredSize(new Dimension(100, 40));
366: jtpWatermarkBleed.putClientProperty(
367: SubstanceLookAndFeel.WATERMARK_TO_BLEED, Boolean.TRUE);
368: builder.append(jtaWatermarkBleed, jtpWatermarkBleed);
369:
370: JEditorPane jep4 = new JEditorPane("text/html;",
371: "Sample <b>content</b><br> <u>text</u>");
372: jep4.setMargin(new Insets(2, 2, 2, 2));
373: builder.append("With margin", jep4);
374:
375: JTextArea jta4 = new JTextArea("Sample content text", 3, 20);
376: jta4.setMargin(new Insets(2, 2, 2, 2));
377: JTextPane jtp4 = new JTextPane();
378: jtp4.replaceSelection("Sample content text");
379: jtp4.setPreferredSize(new Dimension(100, 40));
380: jtp4.setMargin(new Insets(2, 2, 2, 2));
381: builder.append(jta4, jtp4);
382:
383: JTextArea jtaLineWrap = new JTextArea(
384: "The contents of this text area wrap, but not necessarily between words",
385: 3, 15);
386: jtaLineWrap.setLineWrap(true);
387: builder.append("Line wrap", new JLabel(""));
388: builder.append(jtaLineWrap, new JLabel(""));
389:
390: JTextArea jtaLineWrapWords = new JTextArea(
391: "The contents of this text area wrap, necessarily between words",
392: 3, 15);
393: jtaLineWrapWords.setLineWrap(true);
394: jtaLineWrapWords.setWrapStyleWord(true);
395: builder.append("Line wrap words", new JLabel(""));
396: builder.append(jtaLineWrapWords, new JLabel(""));
397:
398: JTextArea textAreaScroll = new JTextArea(5, 15);
399: for (int i = 0; i < 20; i++) {
400: textAreaScroll
401: .append("Some long long long line with number " + i
402: + "\n");
403: }
404: textAreaScroll.setEditable(false);
405: builder.append("Scrollable", new JScrollPane(textAreaScroll));
406: builder.nextLine();
407:
408: JTextArea textAreaScrollNoLock = new JTextArea(5, 15);
409: for (int i = 0; i < 20; i++) {
410: textAreaScrollNoLock
411: .append("Some long long long line with number " + i
412: + "\n");
413: }
414: textAreaScrollNoLock.setEditable(false);
415: textAreaScrollNoLock.putClientProperty(LafWidget.NO_LOCK_ICON,
416: Boolean.TRUE);
417: builder.append("Scrollable no lock", new JScrollPane(
418: textAreaScrollNoLock));
419:
420: return builder.getPanel();
421: }
422:
423: /**
424: * Creates a test panel with text components.
425: */
426: public TextFieldsPanel() {
427: setLayout(new BorderLayout());
428: this .add(new JScrollPane(getContents()), BorderLayout.CENTER);
429: }
430: }
|