1 7 package javax.swing.text.html; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.io.*; 12 import java.net.MalformedURLException ; 13 import java.net.URL ; 14 import javax.swing.text.*; 15 import javax.swing.*; 16 import javax.swing.border.*; 17 import javax.swing.event.*; 18 import java.util.*; 19 20 29 class HiddenTagView extends EditableView implements DocumentListener { 30 HiddenTagView(Element e) { 31 super(e); 32 yAlign = 1; 33 } 34 35 protected Component createComponent() { 36 JTextField tf = new JTextField(getElement().getName()); 37 Document doc = getDocument(); 38 Font font; 39 if (doc instanceof StyledDocument) { 40 font = ((StyledDocument)doc).getFont(getAttributes()); 41 tf.setFont(font); 42 } 43 else { 44 font = tf.getFont(); 45 } 46 tf.getDocument().addDocumentListener(this); 47 updateYAlign(font); 48 49 JPanel panel = new JPanel(new BorderLayout()); 52 panel.setBackground(null); 53 if (isEndTag()) { 54 panel.setBorder(EndBorder); 55 } 56 else { 57 panel.setBorder(StartBorder); 58 } 59 panel.add(tf); 60 return panel; 61 } 62 63 public float getAlignment(int axis) { 64 if (axis == View.Y_AXIS) { 65 return yAlign; 66 } 67 return 0.5f; 68 } 69 70 public float getMinimumSpan(int axis) { 71 if (axis == View.X_AXIS && isVisible()) { 72 return Math.max(30, super.getPreferredSpan(axis)); 74 } 75 return super.getMinimumSpan(axis); 76 } 77 78 public float getPreferredSpan(int axis) { 79 if (axis == View.X_AXIS && isVisible()) { 80 return Math.max(30, super.getPreferredSpan(axis)); 81 } 82 return super.getPreferredSpan(axis); 83 } 84 85 public float getMaximumSpan(int axis) { 86 if (axis == View.X_AXIS && isVisible()) { 87 return Math.max(30, super.getMaximumSpan(axis)); 89 } 90 return super.getMaximumSpan(axis); 91 } 92 93 public void insertUpdate(DocumentEvent e) { 95 updateModelFromText(); 96 } 97 98 public void removeUpdate(DocumentEvent e) { 99 updateModelFromText(); 100 } 101 102 public void changedUpdate(DocumentEvent e) { 103 updateModelFromText(); 104 } 105 106 public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) { 108 if (!isSettingAttributes) { 109 setTextFromModel(); 110 } 111 } 112 113 115 void updateYAlign(Font font) { 116 Container c = getContainer(); 117 FontMetrics fm = (c != null) ? c.getFontMetrics(font) : 118 Toolkit.getDefaultToolkit().getFontMetrics(font); 119 float h = fm.getHeight(); 120 float d = fm.getDescent(); 121 yAlign = (h - d) / h; 122 } 123 124 void resetBorder() { 125 Component comp = getComponent(); 126 127 if (comp != null) { 128 if (isEndTag()) { 129 ((JPanel)comp).setBorder(EndBorder); 130 } 131 else { 132 ((JPanel)comp).setBorder(StartBorder); 133 } 134 } 135 } 136 137 145 void setTextFromModel() { 146 if (SwingUtilities.isEventDispatchThread()) { 147 _setTextFromModel(); 148 } 149 else { 150 SwingUtilities.invokeLater(new Runnable () { 151 public void run() { 152 _setTextFromModel(); 153 } 154 }); 155 } 156 } 157 158 162 void _setTextFromModel() { 163 Document doc = getDocument(); 164 try { 165 isSettingAttributes = true; 166 if (doc instanceof AbstractDocument) { 167 ((AbstractDocument)doc).readLock(); 168 } 169 JTextComponent text = getTextComponent(); 170 if (text != null) { 171 text.setText(getRepresentedText()); 172 resetBorder(); 173 Container host = getContainer(); 174 if (host != null) { 175 preferenceChanged(this, true, true); 176 host.repaint(); 177 } 178 } 179 } 180 finally { 181 isSettingAttributes = false; 182 if (doc instanceof AbstractDocument) { 183 ((AbstractDocument)doc).readUnlock(); 184 } 185 } 186 } 187 188 196 void updateModelFromText() { 197 if (!isSettingAttributes) { 198 if (SwingUtilities.isEventDispatchThread()) { 199 _updateModelFromText(); 200 } 201 else { 202 SwingUtilities.invokeLater(new Runnable () { 203 public void run() { 204 _updateModelFromText(); 205 } 206 }); 207 } 208 } 209 } 210 211 215 void _updateModelFromText() { 216 Document doc = getDocument(); 217 Object name = getElement().getAttributes().getAttribute 218 (StyleConstants.NameAttribute); 219 if ((name instanceof HTML.UnknownTag ) && 220 (doc instanceof StyledDocument)) { 221 SimpleAttributeSet sas = new SimpleAttributeSet(); 222 JTextComponent textComponent = getTextComponent(); 223 if (textComponent != null) { 224 String text = textComponent.getText(); 225 isSettingAttributes = true; 226 try { 227 sas.addAttribute(StyleConstants.NameAttribute, 228 new HTML.UnknownTag (text)); 229 ((StyledDocument)doc).setCharacterAttributes 230 (getStartOffset(), getEndOffset() - 231 getStartOffset(), sas, false); 232 } 233 finally { 234 isSettingAttributes = false; 235 } 236 } 237 } 238 } 239 240 JTextComponent getTextComponent() { 241 Component comp = getComponent(); 242 243 return (comp == null) ? null : (JTextComponent)((Container)comp). 244 getComponent(0); 245 } 246 247 String getRepresentedText() { 248 String retValue = getElement().getName(); 249 return (retValue == null) ? "" : retValue; 250 } 251 252 boolean isEndTag() { 253 AttributeSet as = getElement().getAttributes(); 254 if (as != null) { 255 Object end = as.getAttribute(HTML.Attribute.ENDTAG); 256 if (end != null && (end instanceof String ) && 257 ((String )end).equals("true")) { 258 return true; 259 } 260 } 261 return false; 262 } 263 264 265 float yAlign; 266 267 boolean isSettingAttributes; 268 269 270 static final int circleR = 3; 274 static final int circleD = circleR * 2; 275 static final int tagSize = 6; 276 static final int padding = 3; 277 static final Color UnknownTagBorderColor = Color.black; 278 static final Border StartBorder = new StartTagBorder(); 279 static final Border EndBorder = new EndTagBorder(); 280 281 282 static class StartTagBorder implements Border, Serializable { 283 public void paintBorder(Component c, Graphics g, int x, int y, 284 int width, int height) { 285 g.setColor(UnknownTagBorderColor); 286 x += padding; 287 width -= (padding * 2); 288 g.drawLine(x, y + circleR, 289 x, y + height - circleR); 290 g.drawArc(x, y + height - circleD - 1, 291 circleD, circleD, 180, 90); 292 g.drawArc(x, y, circleD, circleD, 90, 90); 293 g.drawLine(x + circleR, y, x + width - tagSize, y); 294 g.drawLine(x + circleR, y + height - 1, 295 x + width - tagSize, y + height - 1); 296 297 g.drawLine(x + width - tagSize, y, 298 x + width - 1, y + height / 2); 299 g.drawLine(x + width - tagSize, y + height, 300 x + width - 1, y + height / 2); 301 } 302 303 public Insets getBorderInsets(Component c) { 304 return new Insets(2, 2 + padding, 2, tagSize + 2 + padding); 305 } 306 307 public boolean isBorderOpaque() { 308 return false; 309 } 310 } 312 313 static class EndTagBorder implements Border, Serializable { 314 public void paintBorder(Component c, Graphics g, int x, int y, 315 int width, int height) { 316 g.setColor(UnknownTagBorderColor); 317 x += padding; 318 width -= (padding * 2); 319 g.drawLine(x + width - 1, y + circleR, 320 x + width - 1, y + height - circleR); 321 g.drawArc(x + width - circleD - 1, y + height - circleD - 1, 322 circleD, circleD, 270, 90); 323 g.drawArc(x + width - circleD - 1, y, circleD, circleD, 0, 90); 324 g.drawLine(x + tagSize, y, x + width - circleR, y); 325 g.drawLine(x + tagSize, y + height - 1, 326 x + width - circleR, y + height - 1); 327 328 g.drawLine(x + tagSize, y, 329 x, y + height / 2); 330 g.drawLine(x + tagSize, y + height, 331 x, y + height / 2); 332 } 333 334 public Insets getBorderInsets(Component c) { 335 return new Insets(2, tagSize + 2 + padding, 2, 2 + padding); 336 } 337 338 public boolean isBorderOpaque() { 339 return false; 340 } 341 } 343 344 } | Popular Tags |