1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panels.coreadm.client.swing.path; |
|
27 |
|
28 import java.awt.*; |
|
29 import java.awt.event.*; |
|
30 import javax.swing.*; |
|
31 import javax.swing.text.*; |
|
32 |
|
33 @SuppressWarnings({"serial"}) |
|
34 public class PathField extends JTextPane implements TokenAttributeSetFactory { |
|
35 // |
|
36 // Constructors |
|
37 // |
|
38 |
|
39 public PathField() { |
|
40 setStyledDocument(new PathDocument(this)); |
|
41 |
|
42 setFocusTraversalKeys( |
|
43 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null); |
|
44 setFocusTraversalKeys( |
|
45 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null); |
|
46 |
|
47 // Synth doesn't set values in UIManager, so we're forced into this hack |
|
48 setBackground(new JTextField().getBackground()); |
|
49 // setBackground(UIManager.getColor("TextField.background")); |
|
50 } |
|
51 |
|
52 // |
|
53 // TokenAttributeSetFactory methods |
|
54 // |
|
55 |
|
56 @Override |
|
57 public AttributeSet getAttributeSet(String token) { |
|
58 MutableAttributeSet attrs = new SimpleAttributeSet(); |
|
59 Component renderer = new TokenLabel(token); |
|
60 renderer.setFont(getFont()); |
|
61 StyleConstants.setComponent(attrs, renderer); |
|
62 |
|
63 return attrs; |
|
64 } |
|
65 |
|
66 // |
|
67 // Component methods |
|
68 // |
|
69 |
|
70 @Override |
|
71 public Dimension getPreferredSize() { |
|
72 Dimension d = super.getPreferredSize(); |
|
73 |
|
74 for (int i = 0, n = getDocument().getLength(); i < n; i++) { |
|
75 Element element = getStyledDocument().getCharacterElement(i); |
|
76 |
|
77 // Is a TokenLabel showing? |
|
78 if (PathDocument.isToken(element)) { |
|
79 // Calculated size already accounts for TokenLabel |
|
80 return d; |
|
81 } |
|
82 |
|
83 i = element.getEndOffset(); |
|
84 } |
|
85 |
|
86 // No TokenLabel found, so account for its eventual arrival |
|
87 Insets insets = TokenLabel.BORDER.getBorderInsets(null); |
|
88 d.height += insets.top + insets.bottom; |
|
89 |
|
90 return d; |
|
91 } |
|
92 |
|
93 // |
|
94 // JTextComponent methods |
|
95 // |
|
96 |
|
97 @Override |
|
98 public String getText() { |
|
99 return ((PathDocument)getDocument()).getText(); |
|
100 } |
|
101 |
|
102 // |
|
103 // Static methods |
|
104 // |
|
105 |
|
106 // XXX - Test, remove |
|
107 public static void main(String args[]) { |
|
108 final PathField textPane = new PathField(); |
|
109 // textPane.setFont(new Font("Serif", Font.ITALIC, 36)); |
|
110 StringBuilder buffer = new StringBuilder(); |
|
111 for (String token : PathDocument.TOKENS) { |
|
112 buffer.append(token).append('/'); |
|
113 } |
|
114 textPane.setText(buffer.toString()); |
|
115 |
|
116 JButton b = new JButton("Print tokens"); |
|
117 b.addActionListener( |
|
118 new ActionListener() { |
|
119 @Override |
|
120 public void actionPerformed(ActionEvent e) { |
|
121 System.out.printf("%s\n", textPane.getText()); |
|
122 } |
|
123 }); |
|
124 |
|
125 JScrollPane scroll = new TextPaneScrollPane(textPane); |
|
126 |
|
127 JPanel p = new JPanel(new BorderLayout()); |
|
128 p.add(scroll); |
|
129 p.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15)); |
|
130 |
|
131 JFrame frame = new JFrame(); |
|
132 Container c = frame.getContentPane(); |
|
133 c.setLayout(new BorderLayout()); |
|
134 c.add(p, BorderLayout.CENTER); |
|
135 c.add(b, BorderLayout.SOUTH); |
|
136 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
|
137 frame.pack(); |
|
138 frame.setVisible(true); |
|
139 } |
|
140 } |
|