⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qitem.java

📁 lumaQQ的源文件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

	/**
	 * @return Returns the level.
	 */
	public int getLevel() {
		return level;
	}
	
	/**
	 * 扩展item数组大小
	 */
	private void expandItemArray() {
		QItem[] newItems = new QItem[items.length << 1];
		System.arraycopy(items, 0, newItems, 0, itemCount);
		items = newItems;
	}
	
	/**
	 * 向item数组中添加一个item到数组中
	 * 
	 * @param item
	 * 		要添加的QItem对象
	 */
	private void internalAddItem(QItem item) {
		if(itemCount >= items.length)
			expandItemArray();
		
		items[itemCount++] = item;
	}
	
	/**
	 * 从item数组中删除一个item,不校验index的合法性
	 * 
	 * @param index
	 * 		item的index
	 */
	private void internalRemoveItem(int index) {
		System.arraycopy(items, index + 1, items, index, itemCount - index - 1);
		items[itemCount - 1] = null;
	}

	/**
	 * @return Returns the itemCount.
	 */
	public int getItemCount() {
		return itemCount;
	}

	/**
	 * @return Returns the items.
	 */
	public QItem[] getItems() {
		checkWidget();
		QItem[] ret = new QItem[itemCount];
		System.arraycopy(items, 0, ret, 0, itemCount);
		return ret;
	}
	
	/**
	 * 得到第index个子item
	 * 
	 * @param index
	 * 		子item索引 
	 * @return
	 * 		QItem对象
	 */
	public QItem getItem(int index) {
		checkWidget();
		checkIndex(index);
		return items[index];
	}

	/**
	 * 检查index是否符合范围
	 * 
	 * @param index		
	 * 		子item索引
	 */
	private void checkIndex(int index) {
		if(index < 0 || index >= itemCount)
			SWT.error(SWT.ERROR_INVALID_ARGUMENT);
	}		
	
	@Override
	public void setText(String string) {
		setText(string, true);
	}
	
	/**
	 * 设置文本
	 * 
	 * @param string
	 * 		新文本
	 * @param redraw
	 * 		true表示立刻重画
	 */
	public void setText(String string, boolean redraw) {
		super.setText(string);
		if(redraw)
			parent.updateItem(this);
	}
	
	@Override
	public void setImage(Image image) {
		setImage(image, true);
	}
	
	/**
	 * 设置图标 
	 * 
	 * @param image
	 * 		图标
	 * @param redraw
	 * 		true表示立刻重画
	 */
	public void setImage(Image image, boolean redraw) {
		super.setImage(image);
		imageBound = (image == null) ? DUMMY_BOUND : image.getBounds();
		if(redraw)
			parent.updateItem(this);
	}
	
	/**
	 * 设置前景色
	 * 
	 * @param color
	 * 		前景色
	 */
	public void setForeground(Color color) {
		setForeground(color, true);
	}
	
	/**
	 * 设置前景色
	 * 
	 * @param color
	 * 		前景色
	 * @param redraw
	 * 		true表示立刻重画
	 */
	public void setForeground(Color color, boolean redraw) {
		checkWidget();
		if(color != null && color.isDisposed())
			SWT.error (SWT.ERROR_INVALID_ARGUMENT);
		foreground = color;
		if(redraw)
			parent.updateItem(this);
	}
	
	/**
	 * @return
	 * 		前景色,如果没有设置,返回父控件的前景色
	 */
	public Color getForeground() {
		return (foreground == null) ? parent.getForeground() : foreground;
	}

	/**
	 * @param decorationImage The decorationImage to set.
	 */
	public void setDecorationImage(Image decorationImage) {
		setDecorationImage(decorationImage, true);
	}
	
	/**
	 * 这是装饰图标
	 * 
	 * @param decorationImage
	 * 		装饰图标对象 
	 * @param redraw
	 * 		true表示立刻重画
	 */
	public void setDecorationImage(Image decorationImage, boolean redraw) {
		checkWidget();
		if(decorationImage != null && decorationImage.isDisposed())
			SWT.error(SWT.ERROR_INVALID_ARGUMENT);
		this.decorationImage = decorationImage;
		if(redraw)
			parent.updateItem(this);
	}
	
	/**
	 * 添加一个附件图标
	 * 
	 * @param index
	 * 		附件图标索引
	 * @param att
	 * 		附件图标
	 */
	public void addAttachment(int index, Image att) {
		addAttachment(index, att, true);
	}
	
	/**
	 * 添加一个附件图标
	 * 
	 * @param index
	 * 		附件图标索引
	 * @param att
	 * 		附件图标
	 * @param redraw
	 * 		true表示立刻重画
	 */
	public void addAttachment(int index, Image att, boolean redraw) {
		if(index < 0 || index >= MAX_ATTACHMENT)
			SWT.error(SWT.ERROR_INVALID_RANGE);
		if(att != null && att.isDisposed())
			SWT.error(SWT.ERROR_INVALID_ARGUMENT);
		if(attachment[index] == att)
			return;
		
		attachment[index] = att;
		if(att == null)
			attachmentBound[index] = null;
		else
			attachmentBound[index] = att.getBounds();
		if(redraw)
			parent.updateItem(this);
	}
	
	/**
	 * 删除一个附件图标
	 * 
	 * @param index
	 * 		附件索引
	 */
	public void removeAttachment(int index) {
		removeAttachment(index, true);
	}
	
	/**
	 * 删除一个item
	 * 
	 * @param index
	 * 		item的索引
	 */
	public void removeItem(int index) {
		checkIndex(index);
		internalRemoveItem(index);
	}
	
	/**
	 * 删除从index开始的后面所有item
	 * 
	 * @param index
	 */
	void removeItemFrom(int index) {
		checkIndex(index);
		for(int i = index; i < itemCount; i++)
			items[index] = null;
		itemCount = index;
	}
	
	/**
	 * 删除一个附件图标
	 * 
	 * @param index
	 * 		附件索引
	 * @param redraw
	 * 		true表示立刻重画
	 */
	public void removeAttachment(int index, boolean redraw) {
		if(index < 0 || index >= MAX_ATTACHMENT)
			SWT.error(SWT.ERROR_INVALID_RANGE);
		if(attachment[index] == null)
			return;
		attachment[index] = null;
		attachmentBound[index] = null;
		if(redraw)
			parent.updateItem(this);
	}
	
	/**
	 * 情况所有鼠标位置相关标志
	 */
	void clearHitTestFlag() {
		mouseOnIcon = false;
	}
	
	/**
	 * 对坐标值进行一些检测,在tree收到鼠标移动事件时调用此方法
	 * 
	 * @param x
	 * 		相对于item的x坐标 
	 * @param y
	 * 		相对于item的y坐标
	 */
	void mouseTest(int x, int y) {
		boolean b = isOnIcon(x, y);
		if(b != mouseOnIcon) {
			mouseOnIcon = b;
			needRedraw = true;
		}
		int i = isOnAttachment(x, y);
		if(i != mouseOnAttachment) {
			mouseOnAttachment = i;
			needRedraw= true;
		}
		mouseOnText = isOnText(x, y);
	}
	
	/**
	 * 判断坐标是否落在某个附件图标内
	 * 
	 * @param x
	 * 		x坐标,这个坐标是相对于item的坐标
	 * @param y
	 * 		y坐标,这个坐标是相对于item的坐标
	 * @return
	 * 		true表示是
	 */
	private int isOnAttachment(int x, int y) {
		int attX, attY;
		int itemWidth = parent.getItemWidth(level);
		int itemHeight = parent.getItemHeight(level);
		switch(parent.getLevelLayout(level)) {
			case HORIZONTAL:
				attX = itemWidth - ITEM_RIGHT_MARGIN;
				attY = 0;
				for(int i = 0; i < MAX_ATTACHMENT; i++) {
					if(attachment[i] == null)
						continue;
					
					// 计算附件图标的位置
					attY = (itemHeight - attachmentBound[i].height) >>> 1;
					attX -= attachmentBound[i].width;
					
					if(containsPoint(attX, attY, attachmentBound[i].width, attachmentBound[i].height, x, y))
						return i;
					
					attX -= ATTACHMENT_SPACING;
				}
				return -1;
			case VERTICAL:
				int size = parent.getLevelImageSize(level);
				int imageX = (itemWidth - size) >>> 1;
				int imageY = ITEM_TOP_MARGIN;
				int evenX = imageX - ATTACHMENT_SPACING;
				int oddX = imageX + size + ATTACHMENT_SPACING;
				for(int i = 0; i < MAX_ATTACHMENT; i++) {
					if(attachment[i] == null)
						continue;
					
					// 计算附件图标的位置
					if(i % 2 == 0) {
						evenX -= attachmentBound[i].width;
						attX = evenX;
					} else {
						attX = oddX;
						oddX += attachmentBound[i].width + ATTACHMENT_SPACING;
					}
					attY = imageY + size - attachmentBound[i].height;
					
					if(containsPoint(attX, attY, attachmentBound[i].width, attachmentBound[i].height, x, y))
						return i;
				}
				return -1;
			default:
				SWT.error(SWT.ERROR_INVALID_RANGE);
				return -1;
		}
	}
	
	/**
	 * 测试pX, pY是否落在前面四个参数指定的矩形中
	 * 
	 * @param x
	 * 		矩形左上角x坐标
	 * @param y
	 * 		矩形左上角y坐标
	 * @param w
	 * 		矩形宽度
	 * @param h
	 * 		矩形高度
	 * @param pX
	 * 		点x坐标
	 * @param pY
	 * 		点y坐标
	 * @return
	 * 		true表示包含
	 */
	private boolean containsPoint(int x, int y, int w, int h, int pX, int pY) {
		return pX >= x && pX < x + w && pY >= y && pY < y + h; 
	}

	/**
	 * 测试x,y表示的坐标是否在icon上方
	 * 
	 * @param x
	 * 		x坐标,这个坐标是相对于item的坐标
	 * @param y
	 * 		y坐标,这个坐标是相对于item的坐标
	 * @return
	 * 		true表示是
	 */
	private boolean isOnIcon(int x, int y) {
		int size = parent.getLevelImageSize(level);
		switch(parent.getLevelLayout(level)) {
			case HORIZONTAL:				
				if(x < ITEM_LEFT_MARING || x >= ITEM_LEFT_MARING + size)
					return false;
				if(y < ITEM_TOP_MARGIN || y >= ITEM_TOP_MARGIN + size)
					return false;
				return true;
			case VERTICAL:
				int width = parent.getItemWidth(level);
				int imageX = (width - size) >>> 1;
				if(x < imageX || x >= imageX + size)
					return false;
				if(y < ITEM_TOP_MARGIN || y >= ITEM_TOP_MARGIN + size)
					return false;
				return true;
			default:
				SWT.error(SWT.ERROR_INVALID_RANGE);
				return false;
		}
	}
	
	/**
	 * 检查鼠标是否在item的文本上
	 * 
	 * @param x
	 * 		x坐标,这个坐标是相对于item的坐标
	 * @param y
	 * 		y坐标,这个坐标是相对于item的坐标
	 * @return
	 * 		true表示是
	 */
	private boolean isOnText(int x, int y) {
		if(mouseOnAttachment != -1 || mouseOnIcon)
			return false;
		
		int size = parent.getLevelImageSize(level);
		switch(parent.getLevelLayout(level)) {
			case HORIZONTAL:				
				int left = ITEM_LEFT_MARING + size + ITEM_IMAGE_TEXT_SPACING;
				int top = (parent.getItemHeight(level) - parent.fontHeight) >>> 1;
				if(x < left || x >= left + textWidth)
					return false;
				if(y < top || y >= top + parent.fontHeight)
					return false;
				return true;
			case VERTICAL:
				left = Math.max(0, (parent.getItemWidth(level) - textWidth) >> 1);
				top = ITEM_TOP_MARGIN + size + ITEM_IMAGE_TEXT_SPACING;
				if(x < left || x >= left + textWidth)
					return false;
				if(y < top || y >= top + parent.fontHeight)
					return false;
				return true;
			default:
				SWT.error(SWT.ERROR_INVALID_RANGE);
				return false;
		}
	}

    /**
     * 画Hover边界,就是鼠标移动到上面显示的边界
     * 
     * @param gc
     * 		GC
     * @param x
     * 		矩形左上角x坐标
     * @param y
     * 		矩形左上角y坐标
     * @param w
     * 		矩形宽度
     * @param h
     * 		矩形高度
     */
    void paintHoverBorder(GC gc, int x, int y, int w, int h) {
        Display disp = getDisplay();
        Color c1 = null;
        Color c2 = null;
        c1 = disp.getSystemColor(SWT.COLOR_DARK_YELLOW);
        c2 = disp.getSystemColor(SWT.COLOR_DARK_YELLOW);
        if(c1 != null && c2 != null)
            paintBevelRect(gc, x, y, w, h, c1, c2);
    }

    /**
     * 画一个Bevel矩形
     * 
     * @param gc
     * 		GC
     * @param x
     * 		矩形左上角x坐标
     * @param y
     * 		矩形左上角y坐标
     * @param w
     * 		矩形宽度
     * @param h
     * 		矩形高度
     * @param topleft
     * 		上边缘颜色
     * @param bottomright
     * 		下边缘颜色
     */
    void paintBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) {
        Color old = gc.getForeground();
        gc.setForeground(bottomright);
        gc.drawLine(x + w, y, x + w, y + h);
        gc.drawLine(x, y + h, x + w, y + h);
        gc.setForeground(topleft);
        gc.drawLine(x, y, (x + w) - 1, y);
        gc.drawLine(x, y, x, (y + h) - 1);
        gc.setForeground(old);
    }

	/**
	 * @return Returns the mouseOnIcon.
	 */
	public boolean isMouseOnIcon() {
		return mouseOnIcon;
	}

	/**
	 * @return Returns the mouseOnAttachment.
	 */
	public int getMouseOnAttachment() {
		return mouseOnAttachment;
	}
	
	/**
	 * @return
	 * 		true表示鼠标在空白区域
	 */
	public boolean isMouseOnBlank() {
		return !isMouseOnIcon() && getMouseOnAttachment() == -1 && !isMouseOnText();
	}
	
	/**
	 * @return
	 * 		文字所占的矩形区域,相对于item左上角来说
	 */
	Rectangle getTextBound() {
		if(getText() == null)
			return DUMMY_BOUND;
		
		int itemWidth = parent.getItemWidth(level);
		int itemHeight = parent.getItemHeight(level);
		int x = parent.getItemIndent(level);
		int max;
		Point size;
		String text = getText();
		Rectangle rect = new Rectangle(0, 0, 0, 0);
		GC gc = new GC(parent);
		switch(parent.getLevelLayout(level)) {
			case HORIZONTAL:
				rect.x = ITEM_LEFT_MARING + parent.getLevelImageSize(level) + ITEM_IMAGE_TEXT_SPACING;
				if(prefix != null)
					rect.x += PREFIX_ICON_SPACING + prefix.getBounds().width;
				int textX = x + rect.x;
				max = itemWidth - textX;
				size = gc.textExtent(text);
				rect.width = Math.min(max, size.x);
				rect.height = size.y;
				rect.y = (itemHeight - parent.fontHeight) >>> 1;
				break;
			case VERTICAL:
				size = gc.textExtent(text);
				rect.x = Math.max(0, (itemWidth - size.x) >> 1);
				rect.y = ITEM_TOP_MARGIN + parent.getLevelImageSize(level) + ITEM_IMAGE_TEXT_SPACING;
				rect.width = Math.min(itemWidth, size.x);
				rect.height = size.y;
				break;
			default:
				SWT.error(SWT.ERROR_INVALID_RANGE);
				break;
		}
		gc.dispose();
		return rect;
	}

	/**
	 * @return Returns the prefix.
	 */
	public Image getPrefix() {
		return prefix;
	}

	/**
	 * @param prefix The prefix to set.
	 */
	public void setPrefix(Image prefix) {
		setPrefix(prefix, true);
	}
	
	public void setPrefix(Image prefix, boolean redraw) {
		this.prefix = prefix;
		if(redraw)
			parent.updateItem(this);
	}

	public boolean isMouseOnText() {
		return mouseOnText;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -