📄 imageview.java
字号:
// Clear out state in case someone threw an exception. state = (state | LOADING_FLAG) ^ LOADING_FLAG; } } } /** * Loads the image from the URL <code>getImageURL</code>. This should * only be invoked from <code>refreshImage</code>. */ private void loadImage() { URL src = getImageURL(); Image newImage = null; if (src != null) { Dictionary cache = (Dictionary)getDocument(). getProperty(IMAGE_CACHE_PROPERTY); if (cache != null) { newImage = (Image)cache.get(src); } else { newImage = Toolkit.getDefaultToolkit().createImage(src); if (newImage != null && getLoadsSynchronously()) { // Force the image to be loaded by using an ImageIcon. ImageIcon ii = new ImageIcon(); ii.setImage(newImage); } } } image = newImage; } /** * Recreates and reloads the image. This should * only be invoked from <code>refreshImage</code>. */ private void updateImageSize() { int newWidth = 0; int newHeight = 0; int newState = 0; Image newImage = getImage(); if (newImage != null) { Element elem = getElement(); AttributeSet attr = elem.getAttributes(); // Get the width/height and set the state ivar before calling // anything that might cause the image to be loaded, and thus the // ImageHandler to be called. newWidth = getIntAttr(HTML.Attribute.WIDTH, -1); if (newWidth > 0) { newState |= WIDTH_FLAG; } newHeight = getIntAttr(HTML.Attribute.HEIGHT, -1); if (newHeight > 0) { newState |= HEIGHT_FLAG; } if (newWidth <= 0) { newWidth = newImage.getWidth(imageObserver); if (newWidth <= 0) { newWidth = DEFAULT_WIDTH; } } if (newHeight <= 0) { newHeight = newImage.getHeight(imageObserver); if (newHeight <= 0) { newHeight = DEFAULT_HEIGHT; } } // Make sure the image starts loading: if ((newState & (WIDTH_FLAG | HEIGHT_FLAG)) != 0) { Toolkit.getDefaultToolkit().prepareImage(newImage, newWidth, newHeight, imageObserver); } else { Toolkit.getDefaultToolkit().prepareImage(newImage, -1, -1, imageObserver); } boolean createText = false; synchronized(this) { // If imageloading failed, other thread may have called // ImageLoader which will null out image, hence we check // for it. if (image != null) { if ((newState & WIDTH_FLAG) == WIDTH_FLAG || width == 0) { width = newWidth; } if ((newState & HEIGHT_FLAG) == HEIGHT_FLAG || height == 0) { height = newHeight; } } else { createText = true; if ((newState & WIDTH_FLAG) == WIDTH_FLAG) { width = newWidth; } if ((newState & HEIGHT_FLAG) == HEIGHT_FLAG) { height = newHeight; } } state = state | newState; state = (state | LOADING_FLAG) ^ LOADING_FLAG; } if (createText) { // Only reset if this thread determined image is null updateAltTextView(); } } else { width = height = DEFAULT_HEIGHT; updateAltTextView(); } } /** * Updates the view representing the alt text. */ private void updateAltTextView() { String text = getAltText(); if (text != null) { ImageLabelView newView; newView = new ImageLabelView(getElement(), text); synchronized(this) { altView = newView; } } } /** * Returns the view to use for alternate text. This may be null. */ private View getAltView() { View view; synchronized(this) { view = altView; } if (view != null && view.getParent() == null) { view.setParent(getParent()); } return view; } /** * Invokes <code>preferenceChanged</code> on the event displatching * thread. */ private void safePreferenceChanged() { if (SwingUtilities.isEventDispatchThread()) { Document doc = getDocument(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } preferenceChanged(null, true, true); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } else { SwingUtilities.invokeLater(new Runnable() { public void run() { safePreferenceChanged(); } }); } } /** * ImageHandler implements the ImageObserver to correctly update the * display as new parts of the image become available. */ private class ImageHandler implements ImageObserver { // This can come on any thread. If we are in the process of reloading // the image and determining our state (loading == true) we don't fire // preference changed, or repaint, we just reset the fWidth/fHeight as // necessary and return. This is ok as we know when loading finishes // it will pick up the new height/width, if necessary. public boolean imageUpdate(Image img, int flags, int x, int y, int newWidth, int newHeight ) { if (image == null || image != img || getParent() == null) { return false; } // Bail out if there was an error: if ((flags & (ABORT|ERROR)) != 0) { repaint(0); synchronized(ImageView.this) { if (image == img) { // Be sure image hasn't changed since we don't // initialy synchronize image = null; if ((state & WIDTH_FLAG) != WIDTH_FLAG) { width = DEFAULT_WIDTH; } if ((state & HEIGHT_FLAG) != HEIGHT_FLAG) { height = DEFAULT_HEIGHT; } } if ((state & LOADING_FLAG) == LOADING_FLAG) { // No need to resize or repaint, still in the process // of loading. return false; } } updateAltTextView(); safePreferenceChanged(); return false; } // Resize image if necessary: short changed = 0; if ((flags & ImageObserver.HEIGHT) != 0 && !getElement(). getAttributes().isDefined(HTML.Attribute.HEIGHT)) { changed |= 1; } if ((flags & ImageObserver.WIDTH) != 0 && !getElement(). getAttributes().isDefined(HTML.Attribute.WIDTH)) { changed |= 2; } synchronized(ImageView.this) { if (image != img) { return false; } if ((changed & 1) == 1 && (state & WIDTH_FLAG) == 0) { width = newWidth; } if ((changed & 2) == 2 && (state & HEIGHT_FLAG) == 0) { height = newHeight; } if ((state & LOADING_FLAG) == LOADING_FLAG) { // No need to resize or repaint, still in the process of // loading. return true; } } if (changed != 0) { // May need to resize myself, asynchronously: safePreferenceChanged(); return true; } // Repaint when done or when new pixels arrive: if ((flags & (FRAMEBITS|ALLBITS)) != 0) { repaint(0); } else if ((flags & SOMEBITS) != 0 && sIsInc) { repaint(sIncRate); } return ((flags & ALLBITS) == 0); } } /** * ImageLabelView is used if the image can't be loaded, and * the attribute specified an alt attribute. It overriden a handle of * methods as the text is hardcoded and does not come from the document. */ private class ImageLabelView extends InlineView { private Segment segment; private Color fg; ImageLabelView(Element e, String text) { super(e); reset(text); } public void reset(String text) { segment = new Segment(text.toCharArray(), 0, text.length()); } public void paint(Graphics g, Shape a) { // Don't use supers paint, otherwise selection will be wrong // as our start/end offsets are fake. GlyphPainter painter = getGlyphPainter(); if (painter != null) { g.setColor(getForeground()); painter.paint(this, g, a, getStartOffset(), getEndOffset()); } } public Segment getText(int p0, int p1) { if (p0 < 0 || p1 > segment.array.length) { throw new RuntimeException("ImageLabelView: Stale view"); } segment.offset = p0; segment.count = p1 - p0; return segment; } public int getStartOffset() { return 0; } public int getEndOffset() { return segment.array.length; } public View breakView(int axis, int p0, float pos, float len) { // Don't allow a break return this; } public Color getForeground() { View parent; if (fg == null && (parent = getParent()) != null) { Document doc = getDocument(); AttributeSet attr = parent.getAttributes(); if (attr != null && (doc instanceof StyledDocument)) { fg = ((StyledDocument)doc).getForeground(attr); } } return fg; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -