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