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

📄 iconsview.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	public void setUI(DesktopPaneUI ui) {
		super.setUI(ui);
		setBackground(UIManager.getColor("List.background"));
	}


/*****************************************************************************/
/***************** INNER CLASSES *********************************************/
/*****************************************************************************/


	class IconLabel extends JLabel {

		/**
		 * 
		 */
		private static final long serialVersionUID = -6453593388015490024L;

		public IconLabel(String name, Icon icon, int format) {
			super(name, icon, format);
			setToolTipText("freaky"); // We must do this for tooltips to work (???).
		}

		// For some reason the tooltip location is goobered, even with this method
		// (it seems to be ignored?).
		public Point getToolTipLocation(MouseEvent e) {
			Point p = e.getPoint();
			p.x += 10;
			p.y += 10;
			return new Point(0,0);//p; <-- ?? Being ignored!
		}

		public String getToolTipText(MouseEvent e) {
			String tip = null;
			// Put (x,y) in IconsView coordinate system.
			MouseEvent e2 = SwingUtilities.convertMouseEvent(this, e, IconsView.this);
			Component component = IconsView.this.getComponentAt(e2.getPoint());
			// Must check class as we can get IconsView...
			if (component!=null && component instanceof IconInternalFrame) {
				IconInternalFrame frame = (IconInternalFrame)component;
				File file = (File)frame.getFile();
				if (file==null || file.isDirectory())
					return null;
				tip = chooser.getToolTipFor(file);
			}
			return tip;
		}

		public boolean isOpaque() {
			return true;
		}

		// Overridden so we can paint the background like it looks in Windows'
		// "Icon View."
		public void paint(Graphics g) {
			g.setColor(IconsView.this.getBackground());
			Rectangle bounds = getBounds();
			IconInternalFrame frame = (IconInternalFrame)getParent().getParent().getParent().getParent();
			if (frame==null || !frame.isSelected()) {
				g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);
			}
			else {
				Icon icon = getIcon();
				int iconHeight = icon.getIconHeight();
				g.fillRect(0,0, bounds.width,iconHeight);
				g.setColor(UIManager.getColor("List.selectionBackground"));
				g.fillRect(0,iconHeight+1,bounds.width,(int)(bounds.height-iconHeight));
			}
			getUI().paint(g, this);
		}

	}


/*****************************************************************************/


	/**
	 * An internal frame representing a file.
	 */
	class IconInternalFrame extends JInternalFrame implements
					java.awt.event.MouseListener, MouseMotionListener {

		/**
		 * 
		 */
		private static final long serialVersionUID = 4663392480765216317L;

		private JLabel label;
		private File file;
		private int defaultHeight;
		private Color unselectedFG;

		public IconInternalFrame(File file, Icon icon, Color fg) {
			this(file, icon, fg, 0,0);
		}

		public IconInternalFrame(File file, Icon icon, Color color, int x, int y) {

			super();
			this.file = file;
			unselectedFG = color;

			// We want to receive mouse events about ourselves.
			enableEvents(AWTEvent.MOUSE_EVENT_MASK);
			enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);


			label = new IconLabel(file.getName(), icon, JLabel.CENTER);
			label.addMouseListener(this);
			label.addMouseMotionListener(this);
			label.setVerticalTextPosition(JLabel.BOTTOM);
			label.setHorizontalTextPosition(JLabel.CENTER);

			getContentPane().add(label);
			//setBorder(BorderFactory.createLineBorder(Color.BLACK));
			setBorder(null);
			pack();
			setLocation(x,y);
			defaultHeight = getPreferredSize().height;
			setSize(DEFAULT_ICON_WIDTH, defaultHeight);
			setVisible(true);

		}

		public File getFile() {
			return file;
		}

		public String getHTMLFileName(String fileName) {

			int br = 0;
			int count = 0;
			Font font = getFont();
			if (font==null) return ""; // Happens at the very beginning.
			FontMetrics fm = getFontMetrics(font);

			String result = "";

			do {
				// Subtract 8 from the width for a little "cushion" on
				// each side, and to prevent the HTML view from messing
				// us up view-wise.
				Segment s = new Segment(fileName.toCharArray(), 0,fileName.length());
				br = Utilities.getBreakLocation(s, fm, 0, getWidth()-8,
											null, 0);
				result += fileName.substring(0,br);
				fileName = fileName.substring(br);
				if (fileName.length()>0)
					result += "<br>";
				count++;
			} while (fileName.length()>0);

			// Make sure the icon is big enough to display all the text...
			setSize(DEFAULT_ICON_WIDTH, defaultHeight+(count-1)*fm.getHeight());

			return result;

		}

		public void setSelected(boolean selected) throws PropertyVetoException {

			super.setSelected(selected);

			// Have the text field/combo box updated too.
			chooser.synchronizeTextFieldWithView();

			if (selected) {

				// Set colors.
				label.setForeground(UIManager.getColor("List.selectionForeground"));

				// Set text to display.
				String text = file.getName();
				String beginning = null, ending = null;
				if (chooser.isUnderlinedFile(file)) {
					beginning = "<html><body><center><u>";
					ending = "</u></center></body></html>";
				}
				else {
					beginning = "<html><body><center>";
					ending = "</center></body></html>";
				}

				label.setText(beginning + getHTMLFileName(text) + ending);

			}
			// FIXME:  Underlining messes up view due to implied <p> usage...
			else {
				// Set colors and text to display.
				label.setForeground(unselectedFG);
				String beginning = null, ending = null;
				if (chooser.isUnderlinedFile(file)) {
					beginning = "";//"<html><u>";
					ending = "";//"</u></html>";
				}
				else {
					beginning = "";
					ending = "";
				}
				label.setText(beginning + file.getName() + ending);
				setSize(DEFAULT_ICON_WIDTH, defaultHeight);
			}
		}

		// Overridden to always have our special UI.
		public void setUI(InternalFrameUI ui) {
			super.setUI(new BasicInternalFrameUI(this) {
					protected MouseInputAdapter createBorderListener(JInternalFrame w) {
						return null;
					}
					protected void installMouseHandlers(JComponent c) {
					}
					});
			setRootPaneCheckingEnabled(false);
			((BasicInternalFrameUI)getUI()).setNorthPane(null);
			setRootPaneCheckingEnabled(true);
		}

		// _x & _y are the mousePressed location in absolute coordinate system
		int _x, _y;
		// __x & __y are the mousePressed location in source view's coordinate system
		int __x, __y;
		Rectangle startingBounds;

		public void mousePressed(MouseEvent e) {

			int modifiers = e.getModifiers();
			if ((modifiers&InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK) {

				Point p = SwingUtilities.convertPoint(this,
											e.getX(), e.getY(), null);
				__x = e.getX();
				__y = e.getY();
				_x = p.x;
				_y = p.y;
				startingBounds = getBounds();

				// If multiselection isn't enabled, or if they don't hold
				// down shift or control while clicking, we need to clear
				// the selection.
				if (!chooser.isMultiSelectionEnabled() ||
					((modifiers&InputEvent.SHIFT_MASK)==0 &&
					 (modifiers&InputEvent.CTRL_MASK)==0))
				{
					clearSelection();
				}

				// Now, select this newly-clicked on file.
				try {
					setSelected(true);
				} catch (PropertyVetoException e1) { }

				Insets i = getInsets();

				Point ep = new Point(__x, __y);

				if (ep.x > i.left && ep.y > i.top && ep.x < getWidth() - i.right) {
					getDesktopManager().beginDraggingFrame(this);
				}

			} // End of if ((e.getModifiers()&InputEvent.BUTTON1_MASK)!=0)

		}


		public void mouseReleased(MouseEvent e) {

			int modifiers = e.getModifiers();

			// If they just released a left-click, stop any dragging.
			if ((modifiers&InputEvent.BUTTON1_MASK)!=0) {
				getDesktopManager().endDraggingFrame(this);	
				_x = 0;
				_y = 0;
				__x = 0;
				__y = 0;
				startingBounds = null;
			}

			// If they just released a right-click, select the item under the
			// mouse.  This check is because of the internal frames and their
			// labels evidently not sending out mouseClicked events unless
			// they are clicked when they already have focus...
			else {

				// If they clicked on a label and not the icons view, we need to
				// convert the click-point to the proper space.
				Point p = e.getPoint();
				Component source = (Component)e.getSource(); // Always an internal frame?
				if (source!=IconsView.this)
					p = SwingUtilities.convertPoint(source, p, IconsView.this);

				// Pretend like the view is the one who received the mouse click,
				// not the internal frame.  Also pretend like it's a click-
				// event and not a release-event, so the popup menu appears.
				mouseListener.mouseClicked(
					new MouseEvent(IconsView.this, MouseEvent.MOUSE_CLICKED,
								e.getWhen(), e.getModifiers(),
								p.x,p.y, 1,
								e.isPopupTrigger(), e.getButton())
				);

			}

		}

		public void mouseDragged(MouseEvent e) {   
			if ( startingBounds == null ) {
				// (STEVE) Yucky work around for bug ID 4106552
				return;
			}
                                     
			Point p = SwingUtilities.convertPoint(this,
										e.getX(), e.getY(), null);
			int deltaX = _x - p.x;
			int deltaY = _y - p.y;
			int newX, newY;
			Insets i = getInsets();
        
			// Move the window.
			if ((e.getModifiers()&InputEvent.BUTTON1_MASK)!=InputEvent.BUTTON1_MASK) {
    	               // don't allow moving of frames if the left mouse
    	               // button was not used.
    	               return;
			}

			int pWidth, pHeight;
			Dimension s = getParent().getSize();
			pWidth = s.width;
			pHeight = s.height;


			newX = startingBounds.x - deltaX;
			newY = startingBounds.y - deltaY;

			// Make sure we stay in-bounds
			if(newX + i.left <= -__x)
				newX = -__x - i.left + 1;
			if(newY + i.top <= -__y)
				newY = -__y - i.top + 1;
			if(newX + __x + i.right >= pWidth)
				newX = pWidth - __x - i.right - 1;
			if(newY + __y + i.bottom >= pHeight)
				newY =  pHeight - __y - i.bottom - 1;

			getDesktopManager().dragFrame(this, newX, newY);

		}

		public void mouseMoved(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

		public void mouseClicked(MouseEvent e) {

			// If they clicked on a label and not the icons view, we need to
			// convert the click-point to the proper space.
			Point p = e.getPoint();
			Component source = (Component)e.getSource(); // Always an internal frame?
			if (source!=IconsView.this)
				p = SwingUtilities.convertPoint(source, p, IconsView.this);

			// Pretend like the view is the one who received the mouse click,
			// not the internal frame.
			mouseListener.mouseClicked(
				new MouseEvent(IconsView.this, e.getID(), e.getWhen(), e.getModifiers(),
							p.x,p.y, e.getClickCount(),
							e.isPopupTrigger(), e.getButton())
			);

		}

		// This is only called for the internal frame itself, not its JLabel,
		// so we never really see it called.  But it's still safe to have it,
		// and it prevents other listeners from hearin' stuff.
		public void processMouseEvent(MouseEvent e) {
			switch (e.getID()) {
				case MouseEvent.MOUSE_PRESSED:
					mousePressed(e);
					return;
				case MouseEvent.MOUSE_RELEASED:
					mouseReleased(e);
					return;
				case MouseEvent.MOUSE_CLICKED:
					mouseClicked(e);
			}
			//super.processMouseEvent(e); // Don't need this...
		}

		public void processMouseMotionEvent(MouseEvent e) {
			switch (e.getID()) {
				case MouseEvent.MOUSE_DRAGGED:
					mouseDragged(e);
					return;
			}
			//super.processMouseMotionEvent(e); // Don't need this...
		}

		public String toString() {
			return "[IconInternalFrame: file==" + getFile() + "]";
		}

	}


/*****************************************************************************/

}

⌨️ 快捷键说明

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