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) 2010, 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.beans.*; |
|
30 import javax.swing.*; |
|
31 import com.oracle.solaris.vp.util.swing.*; |
|
32 |
|
33 @SuppressWarnings({"serial"}) |
|
34 public class TextPaneScrollPane extends ExtScrollPane { |
|
35 // |
|
36 // Instance data |
|
37 // |
|
38 |
|
39 private JTextPane textPane; |
|
40 private int columns; |
|
41 private int columnWidth; |
|
42 private int height; |
|
43 |
|
44 // |
|
45 // Constructors |
|
46 // |
|
47 |
|
48 public TextPaneScrollPane(JTextPane textPane, int columns) { |
|
49 super(getPanel(textPane)); |
|
50 this.textPane = textPane; |
|
51 this.columns = columns; |
|
52 |
|
53 // Schedule recalculation of column width when font changes |
|
54 textPane.addPropertyChangeListener("font", |
|
55 new PropertyChangeListener() { |
|
56 @Override |
|
57 public void propertyChange(PropertyChangeEvent e) { |
|
58 columnWidth = 0; |
|
59 height = 0; |
|
60 } |
|
61 }); |
|
62 |
|
63 // Don't show scroll bars |
|
64 setVerticalScrollBarPolicy( |
|
65 ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); |
|
66 setHorizontalScrollBarPolicy( |
|
67 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); |
|
68 } |
|
69 |
|
70 public TextPaneScrollPane(JTextPane textPane) { |
|
71 this(textPane, GUIUtil.getTextFieldWidth()); |
|
72 } |
|
73 |
|
74 // |
|
75 // Component methods |
|
76 // |
|
77 |
|
78 @Override |
|
79 public Dimension getPreferredSize() { |
|
80 Dimension d = super.getPreferredSize(); |
|
81 |
|
82 // Limit height of scroll pane to initial height of text pane |
|
83 if (height == 0) { |
|
84 height = d.height; |
|
85 } else { |
|
86 d.height = height; |
|
87 } |
|
88 |
|
89 int columns = getColumns(); |
|
90 if (columns >= 0) { |
|
91 int columnWidth = getColumnWidth(); |
|
92 Insets i = textPane.getInsets(); |
|
93 d.width = i.left + i.right + columns * columnWidth; |
|
94 } |
|
95 |
|
96 return d; |
|
97 } |
|
98 |
|
99 // |
|
100 // TextPaneScrollPane methods |
|
101 // |
|
102 |
|
103 protected int getColumnWidth() { |
|
104 if (columnWidth == 0) { |
|
105 FontMetrics metrics = textPane.getFontMetrics(textPane.getFont()); |
|
106 columnWidth = metrics.charWidth('m'); |
|
107 } |
|
108 return columnWidth; |
|
109 } |
|
110 |
|
111 public JTextPane getTextPane() { |
|
112 return textPane; |
|
113 } |
|
114 |
|
115 public int getColumns() { |
|
116 return columns; |
|
117 } |
|
118 |
|
119 public void setColumns(int columns) { |
|
120 this.columns = columns; |
|
121 |
|
122 invalidate(); |
|
123 |
|
124 Container parent = getParent(); |
|
125 if (parent != null && parent instanceof JComponent) { |
|
126 ((JComponent)parent).revalidate(); |
|
127 } |
|
128 } |
|
129 |
|
130 // |
|
131 // Static methods |
|
132 // |
|
133 |
|
134 private static JPanel getPanel(JTextPane textPane) { |
|
135 JPanel panel = new JPanel(new BorderLayout()); |
|
136 panel.add(textPane); |
|
137 return panel; |
|
138 } |
|
139 } |
|